4#ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
5#define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
11#include <tbb/spin_mutex.h>
30template<
typename T, Index Log2Dim>
39#ifdef OPENVDB_USE_DELAYED_LOADING
42 FileInfo(): bufpos(0) , maskpos(0) {}
43 std::streamoff bufpos;
44 std::streamoff maskpos;
45 io::MappedFile::Ptr mapping;
53#ifdef OPENVDB_USE_DELAYED_LOADING
64#ifdef OPENVDB_USE_DELAYED_LOADING
74#ifdef OPENVDB_USE_DELAYED_LOADING
75 return bool(mOutOfCore);
85#ifdef OPENVDB_USE_DELAYED_LOADING
88 io::MappedFile::Ptr& mappedFile,
const std::streamoff& maskpos);
142 inline void setOutOfCore(
bool b)
145#ifdef OPENVDB_USE_DELAYED_LOADING
151 inline void loadValues()
const
153#ifdef OPENVDB_USE_DELAYED_LOADING
154 if (this->isOutOfCore()) this->doLoad();
157 inline void doLoad()
const;
158 inline bool detachFromFile();
160 using FlagsType = std::atomic<Index32>;
162#ifdef OPENVDB_USE_DELAYED_LOADING
170 FlagsType mOutOfCore;
171 tbb::spin_mutex mMutex;
174 friend class ::TestLeaf;
183template<
typename T, Index Log2Dim>
188#ifdef OPENVDB_USE_DELAYED_LOADING
195template<
typename T, Index Log2Dim>
199#ifdef OPENVDB_USE_DELAYED_LOADING
201 this->detachFromFile();
211template<
typename T, Index Log2Dim>
216 , mOutOfCore(other.mOutOfCore.load())
219#ifdef OPENVDB_USE_DELAYED_LOADING
221 mFileInfo = new FileInfo(*other.mFileInfo);
224 if (other.mData != nullptr) {
226 ValueType* target = mData;
227 const ValueType* source = other.mData;
229 while (n--) *target++ = *source++;
237template<
typename T, Index Log2Dim>
243 if (mData) mData[i] = val;
247template<
typename T, Index Log2Dim>
251 if (&other !=
this) {
252#ifdef OPENVDB_USE_DELAYED_LOADING
254 this->detachFromFile();
259 mOutOfCore.store(other.mOutOfCore.load(std::memory_order_acquire),
260 std::memory_order_release);
261 mFileInfo =
new FileInfo(*other.mFileInfo);
264 if (other.mData !=
nullptr) {
269 while (n--) *target++ = *source++;
271#ifdef OPENVDB_USE_DELAYED_LOADING
278#ifdef OPENVDB_USE_DELAYED_LOADING
279template<
typename T, Index Log2Dim>
282 io::MappedFile::Ptr& mappedFile,
const std::streamoff& maskpos)
284 this->setOutOfCore(
true);
285 mFileInfo =
new FileInfo;
286 mFileInfo->meta = meta;
287 mFileInfo->bufpos = bufpos;
288 mFileInfo->mapping = mappedFile;
289 mFileInfo->maskpos = maskpos;
293template<
typename T, Index Log2Dim>
297 this->detachFromFile();
298 if (mData !=
nullptr) {
301 while (n--) *target++ = val;
306template<
typename T, Index Log2Dim>
312 const ValueType *target = mData, *source = other.mData;
313 if (!target && !source)
return true;
314 if (!target || !source)
return false;
321template<
typename T, Index Log2Dim>
325 std::swap(mData, other.mData);
326#ifdef OPENVDB_USE_DELAYED_LOADING
331 auto tmp = other.mOutOfCore.load(std::memory_order_acquire);
332 tmp = mOutOfCore.exchange(std::move(tmp));
333 other.mOutOfCore.store(std::move(tmp), std::memory_order_release);
338template<
typename T, Index Log2Dim>
342 size_t n =
sizeof(*this);
343#ifdef OPENVDB_USE_DELAYED_LOADING
348#ifdef OPENVDB_USE_DELAYED_LOADING
351 return static_cast<Index>(n);
355template<
typename T, Index Log2Dim>
359 size_t n =
sizeof(*this);
361 return static_cast<Index>(n);
365template<
typename T, Index Log2Dim>
370 if (mData ==
nullptr) {
372#ifdef OPENVDB_USE_DELAYED_LOADING
374 tbb::spin_mutex::scoped_lock lock(self->mMutex);
381template<
typename T, Index Log2Dim>
386 if (mData ==
nullptr) {
387#ifdef OPENVDB_USE_DELAYED_LOADING
389 tbb::spin_mutex::scoped_lock lock(mMutex);
397template<
typename T, Index Log2Dim>
399LeafBuffer<T, Log2Dim>::at(
Index i)
const
406 if (mData)
return mData[i];
else return sZero;
410template<
typename T, Index Log2Dim>
412LeafBuffer<T, Log2Dim>::deallocate()
415 if (mData !=
nullptr) {
416#ifdef OPENVDB_USE_DELAYED_LOADING
417 if (this->isOutOfCore())
return false;
427template<
typename T, Index Log2Dim>
429LeafBuffer<T, Log2Dim>::doLoad()
const
431#ifdef OPENVDB_USE_DELAYED_LOADING
432 if (!this->isOutOfCore())
return;
434 LeafBuffer<T, Log2Dim>* self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
438 tbb::spin_mutex::scoped_lock lock(self->mMutex);
439 if (!this->isOutOfCore())
return;
441 std::unique_ptr<FileInfo> info(self->mFileInfo);
447 self->mData =
nullptr;
451 std::istream is(buf.get());
456 is.seekg(info->maskpos);
459 is.seekg(info->bufpos);
462 self->setOutOfCore(
false);
467template<
typename T, Index Log2Dim>
469LeafBuffer<T, Log2Dim>::detachFromFile()
471#ifdef OPENVDB_USE_DELAYED_LOADING
472 if (this->isOutOfCore()) {
475 this->setOutOfCore(
false);
487template<Index Log2Dim>
499 static inline const bool sOn =
true;
500 static inline const bool sOff =
false;
507 void fill(
bool val) { mData.set(val); }
515 if (mData.isOn(i))
return sOn;
else return sOff;
524 void swap(
LeafBuffer& other) {
if (&other !=
this) std::swap(mData, other.mData); }
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
Tag dispatch class that distinguishes constructors during file input.
Definition Types.h:760
LeafBuffer(const NodeMaskType &other)
Definition LeafBuffer.h:504
~LeafBuffer()
Definition LeafBuffer.h:506
WordType StorageType
Definition LeafBuffer.h:494
util::NodeMask< Log2Dim > NodeMaskType
Definition LeafBuffer.h:491
bool operator!=(const LeafBuffer &other) const
Definition LeafBuffer.h:520
static const Index WORD_COUNT
Definition LeafBuffer.h:496
static const bool sOff
Definition LeafBuffer.h:500
typename NodeMaskType::Word WordType
Definition LeafBuffer.h:492
Index memUsage() const
Definition LeafBuffer.h:526
void swap(LeafBuffer &other)
Definition LeafBuffer.h:524
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition LeafBuffer.h:535
LeafBuffer & operator=(const LeafBuffer &b)
Definition LeafBuffer.h:508
bool operator==(const LeafBuffer &other) const
Definition LeafBuffer.h:519
Index memUsageIfLoaded() const
Definition LeafBuffer.h:527
bool ValueType
Definition LeafBuffer.h:493
LeafBuffer(const LeafBuffer &other)
Definition LeafBuffer.h:505
LeafBuffer(bool on)
Definition LeafBuffer.h:503
const bool & operator[](Index i) const
Definition LeafBuffer.h:517
void fill(bool val)
Definition LeafBuffer.h:507
const bool & getValue(Index i) const
Definition LeafBuffer.h:510
const NodeMaskType & storage() const
Return raw LeafBuffer data.
Definition LeafBuffer.h:538
friend class LeafNode
Definition LeafBuffer.h:542
static Index size()
Definition LeafBuffer.h:528
static const Index SIZE
Definition LeafBuffer.h:497
static const bool sOn
Definition LeafBuffer.h:499
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition LeafBuffer.h:532
LeafBuffer()
Definition LeafBuffer.h:502
void setValue(Index i, bool val)
Definition LeafBuffer.h:522
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
Definition LeafBuffer.h:32
LeafBuffer & operator=(const LeafBuffer &)
Copy the other buffer's values into this buffer.
Definition LeafBuffer.h:249
~LeafBuffer()
Destructor.
Definition LeafBuffer.h:197
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
Definition LeafBuffer.h:62
ValueType StorageType
Definition LeafBuffer.h:35
LeafBuffer(const ValueType &)
Construct a buffer populated with the specified value.
Definition LeafBuffer.h:185
LeafBuffer(const LeafBuffer &)
Copy constructor.
Definition LeafBuffer.h:213
void fill(const ValueType &)
Populate this buffer with a constant value.
Definition LeafBuffer.h:295
util::NodeMask< Log2Dim > NodeMaskType
Definition LeafBuffer.h:36
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:109
void swap(LeafBuffer &)
Exchange this buffer's values with the other buffer's values.
Definition LeafBuffer.h:323
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition LeafBuffer.h:72
Index memUsage() const
Return the memory footprint of this buffer in bytes.
Definition LeafBuffer.h:340
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Definition LeafBuffer.h:81
Index memUsageIfLoaded() const
Definition LeafBuffer.h:357
friend class LeafNode
Definition LeafBuffer.h:176
void setValue(Index i, const ValueType &)
Set the i'th value of this buffer to the specified value.
Definition LeafBuffer.h:239
static Index size()
Return the number of values contained in this buffer.
Definition LeafBuffer.h:118
const ValueType * data() const
Return a const pointer to the array of voxel values.
Definition LeafBuffer.h:367
static const Index SIZE
Definition LeafBuffer.h:37
ValueType * data()
Return a pointer to the array of voxel values.
Definition LeafBuffer.h:383
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition LeafBuffer.h:95
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
Definition LeafBuffer.h:83
LeafBuffer()
Default constructor.
Definition LeafBuffer.h:51
T ValueType
Definition LeafBuffer.h:34
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
Definition LeafBuffer.h:97
bool operator==(const LeafBuffer &) const
Return true if the contents of the other buffer exactly equal the contents of this buffer.
Definition LeafBuffer.h:308
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation.
Definition NodeMasks.h:308
Index64 Word
Definition NodeMasks.h:316
static const Index32 WORD_COUNT
Definition NodeMasks.h:315
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 ...
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition Compression.h:466
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,...
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition Math.h:468
Index32 Index
Definition Types.h:34
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:71
std::shared_ptr< T > SharedPtr
Definition Types.h:95
Definition Exceptions.h:13
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218
#define OPENVDB_USE_DELAYED_LOADING
Definition version.h.in:143