31 #ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED 32 #define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED 34 #include <openvdb/Types.h> 35 #include <openvdb/io/Compression.h> 36 #include <openvdb/util/NodeMasks.h> 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 131 bool isOutOfCore()
const {
return false; }
133 bool empty()
const {
return (mData ==
nullptr); }
149 bool empty()
const {
return !mData || this->isOutOfCore(); }
151 bool allocate() {
if (mData ==
nullptr) mData =
new ValueType[SIZE];
return true; }
178 inline Index memUsage()
const;
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 228 FlagsType mOutOfCore;
229 tbb::spin_mutex mMutex;
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>
432 #if OPENVDB_ABI_VERSION_NUMBER <= 2 438 if (mData)
return mData[i];
else return sZero;
443 template<
typename T, Index Log2Dim>
447 if (mData !=
nullptr && !this->isOutOfCore()) {
456 #if OPENVDB_ABI_VERSION_NUMBER >= 3 458 template<
typename T, Index Log2Dim>
462 if (!this->isOutOfCore())
return;
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;
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>
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 const bool & operator[](Index i) const
Definition: LeafBuffer.h:547
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
Definition: LeafBuffer.h:142
SharedPtr< io::StreamMetadata > meta
Definition: LeafBuffer.h:116
ValueType ValueType
Definition: LeafBuffer.h:104
Index64 Word
Definition: NodeMasks.h:318
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:395
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...
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:160
FileInfo * mFileInfo
Definition: LeafBuffer.h:226
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
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
std::streamoff maskpos
Definition: LeafBuffer.h:114
LeafBuffer()
Definition: LeafBuffer.h:532
Definition: LeafBuffer.h:67
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
bool ValueType
Definition: LeafBuffer.h:522
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
tbb::atomic< Index32 > type
The type of LeafBuffer::mOutOfCore.
Definition: LeafBuffer.h:71
LeafBuffer(const NodeMaskType &other)
Definition: LeafBuffer.h:534
typename NodeMaskType::Word WordType
Definition: LeafBuffer.h:521
io::MappedFile::Ptr mapping
Definition: LeafBuffer.h:115
Definition: Exceptions.h:40
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 ...
static const bool sOff
Definition: LeafBuffer.h:530
const bool & getValue(Index i) const
Definition: LeafBuffer.h:540
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: LeafBuffer.h:147
Index memUsage() const
Definition: LeafBuffer.h:556
std::streamoff bufpos
Definition: LeafBuffer.h:113
void swap(LeafBuffer &other)
Definition: LeafBuffer.h:554
Index32 Index
Definition: Types.h:61
LeafBuffer(const LeafBuffer &other)
Definition: LeafBuffer.h:535
static const bool sOn
Definition: LeafBuffer.h:529
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:488
Definition: LeafBuffer.h:110
bool operator==(const LeafBuffer &other) const
Definition: LeafBuffer.h:549
FileInfo()
Definition: LeafBuffer.h:112
WordType StorageType
Definition: LeafBuffer.h:523
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:561
LeafBuffer & operator=(const LeafBuffer &b)
Definition: LeafBuffer.h:538
static Index size()
Definition: LeafBuffer.h:557
void load(std::istream &is)
Definition: NodeMasks.h:571
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
Definition: LeafBuffer.h:101
LeafBuffer(bool on)
Definition: LeafBuffer.h:533
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
SharedPtr< MappedFile > Ptr
Definition: io.h:152
void setValue(Index i, bool val)
Definition: LeafBuffer.h:552
void fill(bool val)
Definition: LeafBuffer.h:537
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:158
static Index size()
Return the number of values contained in this buffer.
Definition: LeafBuffer.h:180
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:339
ValueType * mData
Definition: LeafBuffer.h:225
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:520
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:564
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:64
bool operator!=(const LeafBuffer &other) const
Definition: LeafBuffer.h:550
uint32_t Index32
Definition: Types.h:59
~LeafBuffer()
Definition: LeafBuffer.h:536
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
Definition: NodeMasks.h:309