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);
136 inline void setOutOfCore(
bool b)
139#ifdef OPENVDB_USE_DELAYED_LOADING
145 inline void loadValues()
const
147#ifdef OPENVDB_USE_DELAYED_LOADING
148 if (this->isOutOfCore()) this->doLoad();
151 inline void doLoad()
const;
152 inline bool detachFromFile();
154 using FlagsType = std::atomic<Index32>;
156#ifdef OPENVDB_USE_DELAYED_LOADING
164 FlagsType mOutOfCore;
165 tbb::spin_mutex mMutex;
168 friend class ::TestLeaf;
177template<
typename T, Index Log2Dim>
182#ifdef OPENVDB_USE_DELAYED_LOADING
189template<
typename T, Index Log2Dim>
193#ifdef OPENVDB_USE_DELAYED_LOADING
195 this->detachFromFile();
205template<
typename T, Index Log2Dim>
210 , mOutOfCore(other.mOutOfCore.load())
213#ifdef OPENVDB_USE_DELAYED_LOADING
215 mFileInfo = new FileInfo(*other.mFileInfo);
218 if (other.mData != nullptr) {
220 ValueType* target = mData;
221 const ValueType* source = other.mData;
223 while (n--) *target++ = *source++;
231template<
typename T, Index Log2Dim>
237 if (mData) mData[i] = val;
241template<
typename T, Index Log2Dim>
245 if (&other !=
this) {
246#ifdef OPENVDB_USE_DELAYED_LOADING
248 this->detachFromFile();
253 mOutOfCore.store(other.mOutOfCore.load(std::memory_order_acquire),
254 std::memory_order_release);
255 mFileInfo =
new FileInfo(*other.mFileInfo);
258 if (other.mData !=
nullptr) {
263 while (n--) *target++ = *source++;
265#ifdef OPENVDB_USE_DELAYED_LOADING
273template<
typename T, Index Log2Dim>
277 this->detachFromFile();
278 if (mData !=
nullptr) {
281 while (n--) *target++ = val;
286template<
typename T, Index Log2Dim>
292 const ValueType *target = mData, *source = other.mData;
293 if (!target && !source)
return true;
294 if (!target || !source)
return false;
301template<
typename T, Index Log2Dim>
305 std::swap(mData, other.mData);
306#ifdef OPENVDB_USE_DELAYED_LOADING
311 auto tmp = other.mOutOfCore.load(std::memory_order_acquire);
312 tmp = mOutOfCore.exchange(std::move(tmp));
313 other.mOutOfCore.store(std::move(tmp), std::memory_order_release);
318template<
typename T, Index Log2Dim>
322 size_t n =
sizeof(*this);
323#ifdef OPENVDB_USE_DELAYED_LOADING
328#ifdef OPENVDB_USE_DELAYED_LOADING
331 return static_cast<Index>(n);
335template<
typename T, Index Log2Dim>
339 size_t n =
sizeof(*this);
341 return static_cast<Index>(n);
345template<
typename T, Index Log2Dim>
350 if (mData ==
nullptr) {
352#ifdef OPENVDB_USE_DELAYED_LOADING
354 tbb::spin_mutex::scoped_lock lock(self->mMutex);
361template<
typename T, Index Log2Dim>
366 if (mData ==
nullptr) {
367#ifdef OPENVDB_USE_DELAYED_LOADING
369 tbb::spin_mutex::scoped_lock lock(mMutex);
377template<
typename T, Index Log2Dim>
379LeafBuffer<T, Log2Dim>::at(
Index i)
const
386 if (mData)
return mData[i];
else return sZero;
390template<
typename T, Index Log2Dim>
392LeafBuffer<T, Log2Dim>::deallocate()
395 if (mData !=
nullptr) {
396#ifdef OPENVDB_USE_DELAYED_LOADING
397 if (this->isOutOfCore())
return false;
407template<
typename T, Index Log2Dim>
409LeafBuffer<T, Log2Dim>::doLoad()
const
411#ifdef OPENVDB_USE_DELAYED_LOADING
412 if (!this->isOutOfCore())
return;
414 LeafBuffer<T, Log2Dim>* self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
418 tbb::spin_mutex::scoped_lock lock(self->mMutex);
419 if (!this->isOutOfCore())
return;
421 std::unique_ptr<FileInfo> info(self->mFileInfo);
427 self->mData =
nullptr;
431 std::istream is(buf.get());
436 is.seekg(info->maskpos);
439 is.seekg(info->bufpos);
442 self->setOutOfCore(
false);
447template<
typename T, Index Log2Dim>
449LeafBuffer<T, Log2Dim>::detachFromFile()
451#ifdef OPENVDB_USE_DELAYED_LOADING
452 if (this->isOutOfCore()) {
455 this->setOutOfCore(
false);
467template<Index Log2Dim>
479 static inline const bool sOn =
true;
480 static inline const bool sOff =
false;
487 void fill(
bool val) { mData.set(val); }
495 if (mData.isOn(i))
return sOn;
else return sOff;
504 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:689
LeafBuffer(const NodeMaskType &other)
Definition LeafBuffer.h:484
~LeafBuffer()
Definition LeafBuffer.h:486
WordType StorageType
Definition LeafBuffer.h:474
util::NodeMask< Log2Dim > NodeMaskType
Definition LeafBuffer.h:471
bool operator!=(const LeafBuffer &other) const
Definition LeafBuffer.h:500
static const Index WORD_COUNT
Definition LeafBuffer.h:476
static const bool sOff
Definition LeafBuffer.h:480
typename NodeMaskType::Word WordType
Definition LeafBuffer.h:472
Index memUsage() const
Definition LeafBuffer.h:506
void swap(LeafBuffer &other)
Definition LeafBuffer.h:504
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition LeafBuffer.h:515
LeafBuffer & operator=(const LeafBuffer &b)
Definition LeafBuffer.h:488
bool operator==(const LeafBuffer &other) const
Definition LeafBuffer.h:499
Index memUsageIfLoaded() const
Definition LeafBuffer.h:507
bool ValueType
Definition LeafBuffer.h:473
LeafBuffer(const LeafBuffer &other)
Definition LeafBuffer.h:485
LeafBuffer(bool on)
Definition LeafBuffer.h:483
const bool & operator[](Index i) const
Definition LeafBuffer.h:497
void fill(bool val)
Definition LeafBuffer.h:487
const bool & getValue(Index i) const
Definition LeafBuffer.h:490
friend class LeafNode
Definition LeafBuffer.h:519
static Index size()
Definition LeafBuffer.h:508
static const Index SIZE
Definition LeafBuffer.h:477
static const bool sOn
Definition LeafBuffer.h:479
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition LeafBuffer.h:512
LeafBuffer()
Definition LeafBuffer.h:482
void setValue(Index i, bool val)
Definition LeafBuffer.h:502
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:243
~LeafBuffer()
Destructor.
Definition LeafBuffer.h:191
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:179
LeafBuffer(const LeafBuffer &)
Copy constructor.
Definition LeafBuffer.h:207
void fill(const ValueType &)
Populate this buffer with a constant value.
Definition LeafBuffer.h:275
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:103
void swap(LeafBuffer &)
Exchange this buffer's values with the other buffer's values.
Definition LeafBuffer.h:303
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:320
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:337
friend class LeafNode
Definition LeafBuffer.h:170
void setValue(Index i, const ValueType &)
Set the i'th value of this buffer to the specified value.
Definition LeafBuffer.h:233
static Index size()
Return the number of values contained in this buffer.
Definition LeafBuffer.h:112
const ValueType * data() const
Return a const pointer to the array of voxel values.
Definition LeafBuffer.h:347
static const Index SIZE
Definition LeafBuffer.h:37
ValueType * data()
Return a pointer to the array of voxel values.
Definition LeafBuffer.h:363
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition LeafBuffer.h:89
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:91
bool operator==(const LeafBuffer &) const
Return true if the contents of the other buffer exactly equal the contents of this buffer.
Definition LeafBuffer.h:288
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:443
Index32 Index
Definition Types.h:54
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:70
std::shared_ptr< T > SharedPtr
Definition Types.h:114
Definition Exceptions.h:13
static pnanovdb_uint32_t allocate(pnanovdb_uint32_t *poffset, pnanovdb_uint32_t size, pnanovdb_uint32_t alignment)
Definition pnanovdb_validate_strides.h:20
#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