CVB++ 14.1
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
49
50 class Cube
51 {
52 public:
53
54 Cube(const Cube& other) = delete;
55 Cube& operator=(const Cube& other) = delete;
56 Cube(Cube&& other) = default;
57 Cube& operator=(Cube&& other) = default;
58 virtual ~Cube() = default;
59
61
71 static std::unique_ptr<Cube> Create(int numSamples, int numLines, int numBands,
73 {
74 return Internal::DoResCallObjectOut<Cube>([&](void* & cubeOut)
75 {
76 return CVB_CALL_CAPI(CVSCreateContinuousCube(numSamples, numLines, numBands,
77 dataType.NativeDescriptor(), static_cast<CExports::CVSCubeEncoding>(bufferLayout),
78 cubeOut));
79 });
80 }
81
82
84
92 {
93 return Internal::DoResCallObjectOut<Cube>([&](void* & cubeOut)
94 {
95 return CVB_CALL_CAPI(CVSCreateDuplicatedContinuousCube(handle_.Handle(),
96 cubeOut));
97 });
98 }
99
100
102
112 {
114 for (auto pImg : images)
115 imgArr.push_back(pImg->Handle());
116 return Internal::DoResCallObjectOut<Cube>([&](void* & cubeOut)
117 {
118 return CVB_CALL_CAPI(CVSCreateStackedCube(imgArr.data(),
119 imgArr.size(), static_cast<CExports::CVSCubeEncoding>(bufferLayout),
120 cubeOut));
121 });
122 }
123
124
126
132 {
133 CExports::CVSCubeType cType = Internal::DoResCallValueOut<CExports::CVSCubeType>([&](CExports::CVSCubeType& val)
134 {
135 return CVB_CALL_CAPI(CVSCubeGetType(handle_.Handle(), val));
136 });
137 return static_cast<CubeType>(cType);
138 }
139
140
142
150 class DataType DataType() const
151 {
152 CExports::cvbdatatype_t cType =
153 Internal::DoResCallValueOut<CExports::cvbdatatype_t>([&](CExports::cvbdatatype_t& val)
154 {
155 return CVB_CALL_CAPI(CVSCubeGetDatatype(handle_.Handle(), val));
156 });
158 }
159
160
162
173 {
174 return Internal::DoResCallObjectOut<Cube>([&](void* & obj)
175 {
176 return CVB_CALL_CAPI(CVSCreateConvertedCube(handle_.Handle(),
177 static_cast<CExports::CVSCubeEncoding>(targetView),
178 obj));
179 });
180 }
181
182
184
200 {
201 bool mapTarget = !(this->Type() == CubeType::StackedCube);
202 CExports::CVSCuboid cCuboid = { 0 };
203 cCuboid.Samples = { static_cast<CExports::cvbdim_t>(spectralCubeoid.Samples().Min()), static_cast<CExports::cvbdim_t>(spectralCubeoid.Samples().Max()) };
204 cCuboid.Lines = { static_cast<CExports::cvbdim_t>(spectralCubeoid.Lines().Min()), static_cast<CExports::cvbdim_t>(spectralCubeoid.Lines().Max()) };
205 cCuboid.Bands = { static_cast<CExports::cvbdim_t>(spectralCubeoid.Bands().Min()), static_cast<CExports::cvbdim_t>(spectralCubeoid.Bands().Max()) };
206 return Internal::DoResCallObjectOut<Cube>([&](void* & obj)
207 {
208 return CVB_CALL_CAPI(CVSCreateCroppedCube(handle_.Handle(),
209 cCuboid,
210 static_cast<CExports::cvbbool_t>(mapTarget),
211 obj));
212 });
213 }
214
215
217
229 {
230 return Internal::DoResCallObjectOut<Image>([&](void* & obj)
231 {
232 return CVB_CALL_CAPI(CVSCubeGetBufferView(handle_.Handle(),
233 obj));
234 });
235 }
236
237
239
248 {
249 return Internal::DoResCallObjectOut<Spectral::MetaData>([&](void* & obj)
250 {
251 return CVB_CALL_CAPI(CVSCubeGetMetaData(handle_.Handle(),
252 obj));
253 });
254 }
255
256
258
264 bool TryLinearAccess(LinearAccessData& linearAccessOut) const noexcept
265 {
266 void* pBase = nullptr;
267 intptr_t sampleInc = 0, lineInc = 0, bandInc = 0;
268 auto code = CExports::MakeErrorCode(CVB_CALL_CAPI(CVSCubeGetLinearAccess(handle_.Handle(), pBase, sampleInc, lineInc, bandInc)));
269 if (code < 0)
270 return false;
271 linearAccessOut = Spectral::LinearAccessData(reinterpret_cast<uintptr_t>(pBase), sampleInc, lineInc, bandInc);
272 return true;
273 }
274
275
277
283 {
284 LinearAccessData linAcc_;
285 if (!TryLinearAccess(linAcc_))
286 Utilities::SystemInfo::ThrowLastError(static_cast<CExports::cvbres_t>(CExports::CVC_E_ERROR));
287 return linAcc_;
288 }
289
290
292
299 template<typename T>
300 std::vector<T> SpectralDensity(int sampleIndex, int lineIndex) const
301 {
302 std::vector<T> tmpSpecDens;
303 SpectralDensity(sampleIndex, lineIndex, tmpSpecDens);
304 return tmpSpecDens;
305 }
306
308
315 static std::unique_ptr<Cube> Load(Cvb::String EnviHeader, Cvb::String EnviBinary)
316 {
317 return Internal::DoResCallObjectOut<Cube>([&](void* & cubeOut)
318 {
319 return CVB_CALL_CAPI(CVSLoadEnviFileTyped(
320 EnviHeader.c_str(), EnviBinary.c_str(),
321 cubeOut));
322 });
323 }
324
326
332 void Save(Cvb::String EnviHeader, Cvb::String EnviBinary) const
333 {
334 CVB_CALL_CAPI_CHECKED(CVSWriteEnviFileTyped(
335 handle_.Handle(),
336 EnviHeader.c_str(), EnviBinary.c_str()));
337 }
338
339
341
344 void* Handle() const noexcept
345 {
346 return handle_.Handle();
347 }
348
349
351
359 static std::unique_ptr<Cube> FromHandle(HandleGuard<Cube>&& guard)
360 {
361 if (!guard.Handle())
362 throw std::runtime_error("handle must not be null");
363 return std::unique_ptr<Cube>(new Cube(std::move(guard)));
364 }
365
366
368
377 bool TryBufferView(ImagePtr& bufferViewOut) const noexcept
378 {
379 CExports::IMG objH = nullptr;
380 if (CExports::MakeErrorCode(CVB_CALL_CAPI(CVSCubeGetBufferView(handle_.Handle(), objH))) < 0)
381 return false;
382 bufferViewOut = Image::FromHandle(HandleGuard<Image>(objH));
383 return true;
384 }
385
386
388
393 bool TryMetaData(MetaDataPtr& metaDataOut) const noexcept
394 {
395 CExports::CVSMETADATA objH = nullptr;
396 if (CExports::MakeErrorCode(CVB_CALL_CAPI(CVSCubeGetMetaData(handle_.Handle(), objH))) < 0)
397 return false;
398 metaDataOut = MetaData::FromHandle(HandleGuard<Spectral::MetaData>(objH));
399 return true;
400 }
401
402
403 protected:
404
405
406 explicit Cube(HandleGuard<Cube>&& guard) noexcept
407 : handle_(std::move(guard))
408 {
409 }
410
411 private:
412
413
414 void SpectralDensity(int sampleIndex, int lineIndex, std::vector<int>& specDens) const noexcept
415 {
416 size_t sz = 0;
417 CVB_CALL_CAPI_CHECKED(CVSCubeGetSpectrumAsInteger(handle_.Handle(), sampleIndex, lineIndex,
418 reinterpret_cast<CExports::cvbval_t*>(0), sz));
420 CVB_CALL_CAPI_CHECKED(CVSCubeGetSpectrumAsInteger(handle_.Handle(), sampleIndex, lineIndex,
421 tmpBuff.data(), sz));
422 // convert int64_t to int
423 std::vector<int> tmpSpecDens(sz);
424 for (auto e : tmpBuff)
425 tmpSpecDens.push_back(static_cast<int>(e));
426 specDens = tmpSpecDens;
427 }
428
429 void SpectralDensity(int sampleIndex, int lineIndex, std::vector<double>& specDens) const noexcept
430 {
431 size_t sz = 0;
432 CVB_CALL_CAPI_CHECKED(CVSCubeGetSpectrumAsDouble(handle_.Handle(), sampleIndex, lineIndex,
433 reinterpret_cast<double*>(0), sz));
434 std::vector<double> tmpBuff(sz);
435 CVB_CALL_CAPI_CHECKED(CVSCubeGetSpectrumAsDouble(handle_.Handle(), sampleIndex, lineIndex,
436 tmpBuff.data(), sz));
437 specDens = tmpBuff;
438 }
439
440 private:
441
442 HandleGuard<Cube> handle_;
443 };
444
445 }
446
447 using Spectral::Cube;
448
449 CVB_END_INLINE_NS
450}
Data type description for an image plane.
Definition: data_type.hpp:28
int NativeDescriptor() const noexcept
Native data type descriptor.
Definition: data_type.hpp:322
static DataType FromNativeDescriptor(int dataTypeDescriptor) noexcept
Construct a data type descriptor from one of the native library's descriptor values.
Definition: data_type.hpp:37
static std::unique_ptr< Image > FromHandle(HandleGuard< Image > &&guard)
Creates an image from a classic API handle.
Definition: decl_image.hpp:153
Spectral Cube object.
Definition: cube.hpp:51
bool TryMetaData(MetaDataPtr &metaDataOut) const noexcept
Retrieves the meta data object of the cube without throwing an exception.
Definition: cube.hpp:393
std::vector< T > SpectralDensity(int sampleIndex, int lineIndex) const
Retrieves spectral density at given pixel.
Definition: cube.hpp:300
MetaDataPtr MetaData() const
Retrieves the meta data object of the cube.
Definition: cube.hpp:247
std::unique_ptr< Cube > Map(CubeRange spectralCubeoid)
Creates a cropped cube.
Definition: cube.hpp:199
LinearAccessData LinearAccess() const
Retrieve linear access of cube data.
Definition: cube.hpp:282
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:71
std::unique_ptr< Cube > Convert(CubeEncoding targetView) const
Converts cube to given cube encoding targetView.
Definition: cube.hpp:172
std::unique_ptr< Cube > Clone()
Copies the memory to a new instance of a cube.
Definition: cube.hpp:91
bool TryLinearAccess(LinearAccessData &linearAccessOut) const noexcept
Retrieve linear access of cube data without throwing an exception.
Definition: cube.hpp:264
static std::unique_ptr< Cube > FromHandle(HandleGuard< Cube > &&guard)
Creates cube from a classic API handle.
Definition: cube.hpp:359
CubeType Type() const
Retrieves the type of this cube object.
Definition: cube.hpp:131
void Save(Cvb::String EnviHeader, Cvb::String EnviBinary) const
Writes this cube object to ENVI-format.
Definition: cube.hpp:332
bool TryBufferView(ImagePtr &bufferViewOut) const noexcept
Retrieves the buffer view of the cube without throwing an exception.
Definition: cube.hpp:377
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:111
static std::unique_ptr< Cube > Load(Cvb::String EnviHeader, Cvb::String EnviBinary)
Static function Loads a cube from ENVI.
Definition: cube.hpp:315
void * Handle() const noexcept
Returns C-API style handle to the Cube.
Definition: cube.hpp:344
Cvb::ImagePtr BufferView() const
Retrieves the buffer view of the cube.
Definition: cube.hpp:228
3D rectangle in the samples, lines and bands domain.
Definition: cube_range.hpp:24
ValueRange< int > Samples() const noexcept
Gets the range regarding the samples dimension.
Definition: cube_range.hpp:55
ValueRange< int > Bands() const noexcept
Gets the range regarding bands dimension.
Definition: cube_range.hpp:101
ValueRange< int > Lines() const noexcept
Gets the range regarding lines dimension.
Definition: cube_range.hpp:78
Linear access properties.
Definition: linear_access_data.hpp:21
static std::unique_ptr< MetaData > FromHandle(HandleGuard< MetaData > &&guard)
Creates metadata from a classic API handle.
Definition: meta_data.hpp:273
T Min() const noexcept
Gets the minimum value.
Definition: value_range.hpp:50
T Max() const noexcept
Gets the maximum value.
Definition: value_range.hpp:72
@ CVC_E_ERROR
Definition: CVCError.h:61
CubeType
Defines the type of the cube.
Definition: spectral.hpp:225
@ StackedCube
Stacked Cube with potentially non-linear buffer.
CubeEncoding
View Perspective: Defines how the mapping between a typical x-y image and samples-lines-bands is done...
Definition: spectral.hpp:211
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24