CVB++ 15.0
cube.hpp
1#pragma once
2
3#include "meta_data.hpp"
4#include "../data_type.hpp"
5#include "cube_range.hpp"
6#include "../_cexports/c_spectral.h"
7#include "spectral.hpp"
8#include "../image.hpp"
9#include "linear_access_data.hpp"
10
11namespace Cvb
12{
13 CVB_BEGIN_INLINE_NS
14
15 template <>
16 inline HandleGuard<Spectral::Cube>::HandleGuard(void *handle) noexcept
17 : HandleGuard<Spectral::Cube>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
18 {
19 }
20
21 namespace Spectral
22 {
23
57
58 class Cube
59 {
60 public:
61 Cube(const Cube &other) = delete;
62 Cube &operator=(const Cube &other) = delete;
63 Cube(Cube &&other) = default;
64 Cube &operator=(Cube &&other) = default;
65 virtual ~Cube() = default;
66
68
78 static std::unique_ptr<Cube> Create(int numSamples, int numLines, int numBands, DataType dataType,
80 {
81 return Internal::DoResCallObjectOut<Cube>([&](void *&cubeOut) {
82 return CVB_CALL_CAPI(CVSCreateContinuousCube(numSamples, numLines, numBands, dataType.NativeDescriptor(),
83 static_cast<CExports::CVSCubeEncoding>(bufferLayout), cubeOut));
84 });
85 }
86
88
96 {
97 return Internal::DoResCallObjectOut<Cube>([&](void *&cubeOut) {
98 return CVB_CALL_CAPI(CVSCreateDuplicatedContinuousCube(handle_.Handle(), cubeOut));
99 });
100 }
101
103
113 {
115 for (auto pImg : images)
116 imgArr.push_back(pImg->Handle());
117 return Internal::DoResCallObjectOut<Cube>([&](void *&cubeOut) {
118 return CVB_CALL_CAPI(CVSCreateStackedCube(imgArr.data(), imgArr.size(),
119 static_cast<CExports::CVSCubeEncoding>(bufferLayout), cubeOut));
120 });
121 }
122
124
130 {
131 CExports::CVSCubeType cType = Internal::DoResCallValueOut<CExports::CVSCubeType>(
132 [&](CExports::CVSCubeType &val) { return CVB_CALL_CAPI(CVSCubeGetType(handle_.Handle(), val)); });
133 return static_cast<CubeType>(cType);
134 }
135
137
145 class DataType DataType() const
146 {
147 CExports::cvbdatatype_t cType = Internal::DoResCallValueOut<CExports::cvbdatatype_t>(
148 [&](CExports::cvbdatatype_t &val) { return CVB_CALL_CAPI(CVSCubeGetDatatype(handle_.Handle(), val)); });
150 }
151
153
164 {
165 return Internal::DoResCallObjectOut<Cube>([&](void *&obj) {
166 return CVB_CALL_CAPI(
167 CVSCreateConvertedCube(handle_.Handle(), static_cast<CExports::CVSCubeEncoding>(targetView), obj));
168 });
169 }
170
172
188 {
189 bool mapTarget = !(this->Type() == CubeType::StackedCube);
190 CExports::CVSCuboid cCuboid = {0};
191 cCuboid.Samples = {static_cast<CExports::cvbdim_t>(spectralCubeoid.Samples().Min()),
192 static_cast<CExports::cvbdim_t>(spectralCubeoid.Samples().Max())};
193 cCuboid.Lines = {static_cast<CExports::cvbdim_t>(spectralCubeoid.Lines().Min()),
194 static_cast<CExports::cvbdim_t>(spectralCubeoid.Lines().Max())};
195 cCuboid.Bands = {static_cast<CExports::cvbdim_t>(spectralCubeoid.Bands().Min()),
196 static_cast<CExports::cvbdim_t>(spectralCubeoid.Bands().Max())};
197 return Internal::DoResCallObjectOut<Cube>([&](void *&obj) {
198 return CVB_CALL_CAPI(
199 CVSCreateCroppedCube(handle_.Handle(), cCuboid, static_cast<CExports::cvbbool_t>(mapTarget), obj));
200 });
201 }
202
204
216 {
217 return Internal::DoResCallObjectOut<Image>(
218 [&](void *&obj) { return CVB_CALL_CAPI(CVSCubeGetBufferView(handle_.Handle(), obj)); });
219 }
220
222
231 {
232 return Internal::DoResCallObjectOut<Spectral::MetaData>(
233 [&](void *&obj) { return CVB_CALL_CAPI(CVSCubeGetMetaData(handle_.Handle(), obj)); });
234 }
235
237
243 bool TryLinearAccess(LinearAccessData &linearAccessOut) const noexcept
244 {
245 void *pBase = nullptr;
246 intptr_t sampleInc = 0, lineInc = 0, bandInc = 0;
247 auto code = CExports::MakeErrorCode(
248 CVB_CALL_CAPI(CVSCubeGetLinearAccess(handle_.Handle(), pBase, sampleInc, lineInc, bandInc)));
249 if (code < 0)
250 return false;
251 linearAccessOut = Spectral::LinearAccessData(reinterpret_cast<uintptr_t>(pBase), sampleInc, lineInc, bandInc);
252 return true;
253 }
254
256
262 {
263 LinearAccessData linAcc_;
264 if (!TryLinearAccess(linAcc_))
265 Utilities::SystemInfo::ThrowLastError(static_cast<CExports::cvbres_t>(CExports::CVC_E_ERROR));
266 return linAcc_;
267 }
268
270
277 template <typename T>
278 std::vector<T> SpectralDensity(int sampleIndex, int lineIndex) const
279 {
280 std::vector<T> tmpSpecDens;
281 SpectralDensity(sampleIndex, lineIndex, tmpSpecDens);
282 return tmpSpecDens;
283 }
284
286
293 static std::unique_ptr<Cube> Load(Cvb::String EnviHeader, Cvb::String EnviBinary)
294 {
295 return Internal::DoResCallObjectOut<Cube>([&](void *&cubeOut) {
296 return CVB_CALL_CAPI(CVSLoadEnviFileTyped(EnviHeader.c_str(), EnviBinary.c_str(), cubeOut));
297 });
298 }
299
301
307 void Save(Cvb::String EnviHeader, Cvb::String EnviBinary) const
308 {
309 CVB_CALL_CAPI_CHECKED(CVSWriteEnviFileTyped(handle_.Handle(), EnviHeader.c_str(), EnviBinary.c_str()));
310 }
311
313
316 void *Handle() const noexcept
317 {
318 return handle_.Handle();
319 }
320
322
330 static std::unique_ptr<Cube> FromHandle(HandleGuard<Cube> &&guard)
331 {
332 if (!guard.Handle())
333 throw std::runtime_error("handle must not be null");
334 return std::unique_ptr<Cube>(new Cube(std::move(guard)));
335 }
336
338
347 bool TryBufferView(ImagePtr &bufferViewOut) const noexcept
348 {
349 CExports::IMG objH = nullptr;
350 if (CExports::MakeErrorCode(CVB_CALL_CAPI(CVSCubeGetBufferView(handle_.Handle(), objH))) < 0)
351 return false;
352 bufferViewOut = Image::FromHandle(HandleGuard<Image>(objH));
353 return true;
354 }
355
357
362 bool TryMetaData(MetaDataPtr &metaDataOut) const noexcept
363 {
364 CExports::CVSMETADATA objH = nullptr;
365 if (CExports::MakeErrorCode(CVB_CALL_CAPI(CVSCubeGetMetaData(handle_.Handle(), objH))) < 0)
366 return false;
367 metaDataOut = MetaData::FromHandle(HandleGuard<Spectral::MetaData>(objH));
368 return true;
369 }
370
371 protected:
372 explicit Cube(HandleGuard<Cube> &&guard) noexcept
373 : handle_(std::move(guard))
374 {
375 }
376
377 private:
378 void SpectralDensity(int sampleIndex, int lineIndex, std::vector<int> &specDens) const noexcept
379 {
380 size_t sz = 0;
381 CVB_CALL_CAPI_CHECKED(CVSCubeGetSpectrumAsInteger(handle_.Handle(), sampleIndex, lineIndex,
382 reinterpret_cast<CExports::cvbval_t *>(0), sz));
384 CVB_CALL_CAPI_CHECKED(
385 CVSCubeGetSpectrumAsInteger(handle_.Handle(), sampleIndex, lineIndex, tmpBuff.data(), sz));
386 // convert int64_t to int
387 std::vector<int> tmpSpecDens(sz);
388 for (auto e : tmpBuff)
389 tmpSpecDens.push_back(static_cast<int>(e));
390 specDens = tmpSpecDens;
391 }
392
393 void SpectralDensity(int sampleIndex, int lineIndex, std::vector<double> &specDens) const noexcept
394 {
395 size_t sz = 0;
396 CVB_CALL_CAPI_CHECKED(
397 CVSCubeGetSpectrumAsDouble(handle_.Handle(), sampleIndex, lineIndex, reinterpret_cast<double *>(0), sz));
398 std::vector<double> tmpBuff(sz);
399 CVB_CALL_CAPI_CHECKED(CVSCubeGetSpectrumAsDouble(handle_.Handle(), sampleIndex, lineIndex, tmpBuff.data(), sz));
400 specDens = tmpBuff;
401 }
402
403 private:
404 HandleGuard<Cube> handle_;
405 };
406
407 } // namespace Spectral
408
409 using Spectral::Cube;
410
411 CVB_END_INLINE_NS
412} // namespace Cvb
int NativeDescriptor() const noexcept
Native data type descriptor.
Definition data_type.hpp:312
static DataType FromNativeDescriptor(int dataTypeDescriptor) noexcept
Construct a data type descriptor from one of the native library's descriptor values.
Definition data_type.hpp:31
static std::unique_ptr< Image > FromHandle(HandleGuard< Image > &&guard)
Creates an image from a classic API handle.
Definition decl_image.hpp:155
Spectral Cube object.
Definition cube.hpp:59
bool TryMetaData(MetaDataPtr &metaDataOut) const noexcept
Retrieves the meta data object of the cube without throwing an exception.
Definition cube.hpp:362
std::vector< T > SpectralDensity(int sampleIndex, int lineIndex) const
Retrieves spectral density at given pixel.
Definition cube.hpp:278
MetaDataPtr MetaData() const
Retrieves the meta data object of the cube.
Definition cube.hpp:230
std::unique_ptr< Cube > Map(CubeRange spectralCubeoid)
Creates a cropped cube.
Definition cube.hpp:187
LinearAccessData LinearAccess() const
Retrieve linear access of cube data.
Definition cube.hpp:261
static std::unique_ptr< Cube > Create(int numSamples, int numLines, int numBands, DataType dataType, CubeEncoding bufferLayout=CubeEncoding::BandInterleavedByLine)
Creates a Continuous Cube object.
Definition cube.hpp:78
class DataType DataType() const
Retrieves the data type of this cube object.
Definition cube.hpp:145
std::unique_ptr< Cube > Convert(CubeEncoding targetView) const
Converts cube to given cube encoding targetView.
Definition cube.hpp:163
std::unique_ptr< Cube > Clone()
Copies the memory to a new instance of a cube.
Definition cube.hpp:95
bool TryLinearAccess(LinearAccessData &linearAccessOut) const noexcept
Retrieve linear access of cube data without throwing an exception.
Definition cube.hpp:243
static std::unique_ptr< Cube > FromHandle(HandleGuard< Cube > &&guard)
Creates cube from a classic API handle.
Definition cube.hpp:330
CubeType Type() const
Retrieves the type of this cube object.
Definition cube.hpp:129
void Save(Cvb::String EnviHeader, Cvb::String EnviBinary) const
Writes this cube object to ENVI-format.
Definition cube.hpp:307
bool TryBufferView(ImagePtr &bufferViewOut) const noexcept
Retrieves the buffer view of the cube without throwing an exception.
Definition cube.hpp:347
static std::unique_ptr< Cube > FromImages(const std::vector< ImagePtr > &images, CubeEncoding bufferLayout)
Initializes a stacked cube using an array of images.
Definition cube.hpp:112
static std::unique_ptr< Cube > Load(Cvb::String EnviHeader, Cvb::String EnviBinary)
Static function Loads a cube from ENVI.
Definition cube.hpp:293
void * Handle() const noexcept
Returns C-API style handle to the Cube.
Definition cube.hpp:316
Cvb::ImagePtr BufferView() const
Retrieves the buffer view of the cube.
Definition cube.hpp:215
3D rectangle in the samples, lines and bands domain.
Definition cube_range.hpp:22
ValueRange< int > Samples() const noexcept
Gets the range regarding the samples dimension.
Definition cube_range.hpp:48
ValueRange< int > Bands() const noexcept
Gets the range regarding bands dimension.
Definition cube_range.hpp:92
ValueRange< int > Lines() const noexcept
Gets the range regarding lines dimension.
Definition cube_range.hpp:70
Linear access properties.
Definition linear_access_data.hpp:20
static std::unique_ptr< MetaData > FromHandle(HandleGuard< MetaData > &&guard)
Creates metadata from a classic API handle.
Definition meta_data.hpp:255
cvbbool_t ReleaseObject(OBJ &Object)
T move(T... args)
Namespace for the Spectral package.
Definition arithmetic.hpp:14
std::shared_ptr< MetaData > MetaDataPtr
Convenience shared pointer for MetaData.
Definition spectral.hpp:104
CubeType
Defines the type of the cube.
Definition spectral.hpp:231
@ StackedCube
Stacked Cube with potentially non-linear buffer.
Definition spectral.hpp:239
CubeEncoding
View Perspective: Defines how the mapping between a typical x-y image and samples-lines-bands is done...
Definition spectral.hpp:217
@ BandInterleavedByLine
Definition spectral.hpp:220
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
std::shared_ptr< Image > ImagePtr
Convenience shared pointer for Image.
Definition global.hpp:86
Angle Max(Angle a, Angle b) noexcept
Returns the bigger of two angles.
Definition angle.hpp:495
Angle Min(Angle a, Angle b) noexcept
Returns the smaller of two angles.
Definition angle.hpp:512