Go to the documentation of this file.
31 #ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
32 #define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
37 #include <tbb/atomic.h>
38 #include <tbb/spin_mutex.h>
43 #include <type_traits>
69 #if OPENVDB_ABI_VERSION_NUMBER >= 5
70 using type = tbb::atomic<Index32>;
72 static constexpr
bool IsAtomic =
true;
73 #else // OPENVDB_ABI_VERSION_NUMBER < 5
75 struct Atomic {
union { T* data;
void* ptr; }; tbb::atomic<Index32> i; tbb::spin_mutex mutex; };
76 struct NonAtomic {
union { T* data;
void* ptr; };
Index32 i; tbb::spin_mutex mutex; };
78 #ifndef __INTEL_COMPILER
79 static constexpr
bool IsAtomic = ((
sizeof(Atomic) ==
sizeof(NonAtomic))
81 && (offsetof(Atomic, i) == offsetof(NonAtomic, i)));
86 static constexpr
bool IsAtomic = (
sizeof(Atomic) ==
sizeof(NonAtomic));
88 static constexpr
size_t size =
sizeof(Atomic);
91 using type =
typename std::conditional<IsAtomic, tbb::atomic<Index32>,
Index32>::type;
100 template<
typename T, Index Log2Dim>
107 static const Index SIZE = 1 << 3 * Log2Dim;
109 #if OPENVDB_ABI_VERSION_NUMBER >= 3
120 #if OPENVDB_ABI_VERSION_NUMBER <= 2
124 explicit LeafBuffer(
const ValueType& val): mData(new ValueType[SIZE]) { this->fill(val); }
126 LeafBuffer(
const LeafBuffer& other): mData(new ValueType[SIZE]) { *
this = other; }
128 ~LeafBuffer() {
delete[] mData; }
131 bool isOutOfCore()
const {
return false; }
133 bool empty()
const {
return (mData ==
nullptr); }
135 inline LeafBuffer(): mData(new ValueType[SIZE]) { mOutOfCore = 0; }
138 explicit inline LeafBuffer(
const ValueType&);
140 inline LeafBuffer(
const LeafBuffer&);
149 bool empty()
const {
return !mData || this->isOutOfCore(); }
151 bool allocate() {
if (mData ==
nullptr) mData =
new ValueType[SIZE];
return true; }
155 inline void fill(
const ValueType&);
162 inline void setValue(
Index i,
const ValueType&);
178 inline Index memUsage()
const;
185 const ValueType* data()
const;
193 inline const ValueType& at(
Index i)
const;
200 ValueType& operator[](
Index i) {
return const_cast<ValueType&
>(this->at(i)); }
204 #if OPENVDB_ABI_VERSION_NUMBER <= 2
205 void setOutOfCore(
bool) {}
206 void loadValues()
const {}
207 void doLoad()
const {}
208 bool detachFromFile() {
return false; }
210 inline void setOutOfCore(
bool b) { mOutOfCore = b; }
213 inline void loadValues()
const {
if (this->isOutOfCore()) this->doLoad(); }
214 inline void doLoad()
const;
215 inline bool detachFromFile();
219 #if OPENVDB_ABI_VERSION_NUMBER <= 2
222 using FlagsType =
typename internal::LeafBufferFlags<ValueType>::type;
228 FlagsType mOutOfCore;
229 tbb::spin_mutex mMutex;
232 static const ValueType sZero;
235 friend class ::TestLeaf;
244 #if OPENVDB_ABI_VERSION_NUMBER >= 3
246 template<
typename T, Index Log2Dim>
250 template<
typename T, Index Log2Dim>
260 template<
typename T, Index Log2Dim>
264 if (this->isOutOfCore()) {
265 this->detachFromFile();
272 template<
typename T, Index Log2Dim>
276 , mOutOfCore(other.mOutOfCore)
279 mFileInfo =
new FileInfo(*other.
mFileInfo);
280 }
else if (other.
mData !=
nullptr) {
285 while (n--) *target++ = *source++;
289 #endif // OPENVDB_ABI_VERSION_NUMBER >= 3
292 template<
typename T, Index Log2Dim>
297 #if OPENVDB_ABI_VERSION_NUMBER <= 2
301 if (mData) mData[i] = val;
306 template<
typename T, Index Log2Dim>
310 if (&other !=
this) {
311 #if OPENVDB_ABI_VERSION_NUMBER <= 2
312 if (other.
mData !=
nullptr) {
317 while (n--) *target++ = *source++;
319 #else // OPENVDB_ABI_VERSION_NUMBER >= 3
320 if (this->isOutOfCore()) {
321 this->detachFromFile();
326 mOutOfCore = other.mOutOfCore;
327 mFileInfo =
new FileInfo(*other.
mFileInfo);
328 }
else if (other.
mData !=
nullptr) {
333 while (n--) *target++ = *source++;
341 template<
typename T, Index Log2Dim>
345 this->detachFromFile();
346 if (mData !=
nullptr) {
349 while (n--) *target++ = val;
354 template<
typename T, Index Log2Dim>
361 if (!target && !source)
return true;
362 if (!target || !source)
return false;
369 template<
typename T, Index Log2Dim>
373 std::swap(mData, other.
mData);
374 #if OPENVDB_ABI_VERSION_NUMBER >= 3
375 std::swap(mOutOfCore, other.mOutOfCore);
380 template<
typename T, Index Log2Dim>
384 size_t n =
sizeof(*this);
385 #if OPENVDB_ABI_VERSION_NUMBER <= 2
386 if (mData) n += SIZE *
sizeof(
ValueType);
388 if (this->isOutOfCore()) n +=
sizeof(FileInfo);
389 else if (mData) n += SIZE *
sizeof(
ValueType);
391 return static_cast<Index>(n);
395 template<
typename T, Index Log2Dim>
399 #if OPENVDB_ABI_VERSION_NUMBER >= 3
401 if (mData ==
nullptr) {
404 tbb::spin_mutex::scoped_lock lock(self->mMutex);
405 if (mData ==
nullptr)
self->mData =
new ValueType[SIZE];
411 template<
typename T, Index Log2Dim>
415 #if OPENVDB_ABI_VERSION_NUMBER >= 3
417 if (mData ==
nullptr) {
419 tbb::spin_mutex::scoped_lock lock(mMutex);
420 if (mData ==
nullptr) mData =
new ValueType[SIZE];
427 template<
typename T, Index Log2Dim>
428 inline const typename LeafBuffer<T, Log2Dim>::ValueType&
429 LeafBuffer<T, Log2Dim>::at(
Index i)
const
432 #if OPENVDB_ABI_VERSION_NUMBER <= 2
438 if (mData)
return mData[i];
else return sZero;
443 template<
typename T, Index Log2Dim>
445 LeafBuffer<T, Log2Dim>::deallocate()
447 if (mData !=
nullptr && !this->isOutOfCore()) {
456 #if OPENVDB_ABI_VERSION_NUMBER >= 3
458 template<
typename T, Index Log2Dim>
460 LeafBuffer<T, Log2Dim>::doLoad()
const
462 if (!this->isOutOfCore())
return;
464 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
468 tbb::spin_mutex::scoped_lock lock(self->mMutex);
469 if (!this->isOutOfCore())
return;
471 std::unique_ptr<FileInfo> info(self->mFileInfo);
472 assert(info.get() !=
nullptr);
473 assert(info->mapping.get() !=
nullptr);
474 assert(info->meta.get() !=
nullptr);
477 self->mData =
nullptr;
480 SharedPtr<std::streambuf> buf = info->mapping->createBuffer();
481 std::istream is(buf.get());
486 is.seekg(info->maskpos);
489 is.seekg(info->bufpos);
492 self->setOutOfCore(
false);
496 template<
typename T, Index Log2Dim>
498 LeafBuffer<T, Log2Dim>::detachFromFile()
500 if (this->isOutOfCore()) {
503 this->setOutOfCore(
false);
509 #endif // OPENVDB_ABI_VERSION_NUMBER >= 3
516 template<Index Log2Dim>
525 static const Index WORD_COUNT = NodeMaskType::WORD_COUNT;
526 static const Index SIZE = 1 << 3 * Log2Dim;
537 void fill(
bool val) { mData.set(val); }
545 if (mData.isOn(i))
return sOn;
else return sOff;
585 #endif // OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
void swap(LeafBuffer &other)
Definition: LeafBuffer.h:554
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:158
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:564
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:561
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:415
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: LeafBuffer.h:147
bool operator==(const LeafBuffer &other) const
Definition: LeafBuffer.h:549
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim....
Definition: LeafNode.h:64
LeafBuffer()
Definition: LeafBuffer.h:532
typename NodeMaskType::Word WordType
Definition: LeafBuffer.h:521
Definition: LeafBuffer.h:67
const bool & operator[](Index i) const
Definition: LeafBuffer.h:547
Index32 Index
Definition: Types.h:61
WordType StorageType
Definition: LeafBuffer.h:523
OPENVDB_API bool getHalfFloat(std::ios_base &)
Return true if floating-point values should be quantized to 16 bits when writing to the given stream ...
~LeafBuffer()
Definition: LeafBuffer.h:536
ValueType ValueType
Definition: LeafBuffer.h:104
SharedPtr< io::StreamMetadata > meta
Definition: LeafBuffer.h:116
void setValue(Index i, bool val)
Definition: LeafBuffer.h:552
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:492
tbb::atomic< Index32 > type
The type of LeafBuffer::mOutOfCore.
Definition: LeafBuffer.h:71
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
void fill(bool val)
Definition: LeafBuffer.h:537
LeafBuffer(const NodeMaskType &other)
Definition: LeafBuffer.h:534
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
Definition: LeafBuffer.h:142
ValueType * mData
Definition: LeafBuffer.h:225
io::MappedFile::Ptr mapping
Definition: LeafBuffer.h:115
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation.
Definition: NodeMasks.h:309
LeafBuffer(const LeafBuffer &other)
Definition: LeafBuffer.h:535
LeafBuffer(bool on)
Definition: LeafBuffer.h:533
static const bool sOff
Definition: LeafBuffer.h:530
Definition: LeafBuffer.h:110
uint32_t Index32
Definition: Types.h:59
static Index size()
Definition: LeafBuffer.h:557
Index64 Word
Definition: NodeMasks.h:318
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
Definition: LeafBuffer.h:101
bool operator!=(const LeafBuffer &other) const
Return true if the contents of the other buffer are not exactly equal to the contents of this buffer.
Definition: LeafBuffer.h:172
bool ValueType
Definition: LeafBuffer.h:522
std::streamoff maskpos
Definition: LeafBuffer.h:114
static const bool sOn
Definition: LeafBuffer.h:529
OPENVDB_API void setStreamMetadataPtr(std::ios_base &, SharedPtr< StreamMetadata > &, bool transfer=true)
Associate the given stream with (a shared pointer to) an object that stores metadata (file format,...
ValueType StorageType
Definition: LeafBuffer.h:105
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Definition: LeafBuffer.h:149
FileInfo()
Definition: LeafBuffer.h:112
const bool & getValue(Index i) const
Definition: LeafBuffer.h:540
#define OPENVDB_VERSION_NAME
Definition: version.h:134
FileInfo * mFileInfo
Definition: LeafBuffer.h:226
bool operator!=(const LeafBuffer &other) const
Definition: LeafBuffer.h:550
Index memUsage() const
Definition: LeafBuffer.h:556
std::streamoff bufpos
Definition: LeafBuffer.h:113
LeafBuffer & operator=(const LeafBuffer &b)
Definition: LeafBuffer.h:538
Definition: Exceptions.h:40
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:498
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:160
SharedPtr< MappedFile > Ptr
Definition: io.h:163
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:752
static Index size()
Return the number of values contained in this buffer.
Definition: LeafBuffer.h:180