4 #ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED 5 #define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED 10 #include <tbb/spin_mutex.h> 16 #include <type_traits> 29 template<
typename T, Index Log2Dim>
36 static const Index SIZE = 1 << 3 * Log2Dim;
38 #ifdef OPENVDB_USE_DELAYED_LOADING 41 FileInfo(): bufpos(0) , maskpos(0) {}
42 std::streamoff bufpos;
43 std::streamoff maskpos;
44 io::MappedFile::Ptr mapping;
52 #ifdef OPENVDB_USE_DELAYED_LOADING 63 #ifdef OPENVDB_USE_DELAYED_LOADING 73 #ifdef OPENVDB_USE_DELAYED_LOADING 74 return bool(mOutOfCore);
80 bool empty()
const {
return !mData || this->isOutOfCore(); }
85 inline void fill(
const ValueType&);
92 inline void setValue(
Index i,
const ValueType&);
116 const ValueType* data()
const;
124 inline const ValueType& at(
Index i)
const;
131 ValueType& operator[](
Index i) {
return const_cast<ValueType&
>(this->at(i)); }
135 inline void setOutOfCore(
bool b)
138 #ifdef OPENVDB_USE_DELAYED_LOADING 144 inline void loadValues()
const 146 #ifdef OPENVDB_USE_DELAYED_LOADING 147 if (this->isOutOfCore()) this->doLoad();
150 inline void doLoad()
const;
151 inline bool detachFromFile();
153 using FlagsType = std::atomic<Index32>;
155 #ifdef OPENVDB_USE_DELAYED_LOADING 163 FlagsType mOutOfCore;
164 tbb::spin_mutex mMutex;
167 static const ValueType sZero;
169 friend class ::TestLeaf;
178 template<
typename T, Index Log2Dim>
182 template<
typename T, Index Log2Dim>
187 #ifdef OPENVDB_USE_DELAYED_LOADING 194 template<
typename T, Index Log2Dim>
198 #ifdef OPENVDB_USE_DELAYED_LOADING 199 if (this->isOutOfCore()) {
200 this->detachFromFile();
210 template<
typename T, Index Log2Dim>
215 , mOutOfCore(other.mOutOfCore.load())
218 #ifdef OPENVDB_USE_DELAYED_LOADING 220 mFileInfo =
new FileInfo(*other.mFileInfo);
223 if (other.mData !=
nullptr) {
228 while (n--) *target++ = *source++;
230 #ifdef OPENVDB_USE_DELAYED_LOADING 236 template<
typename T, Index Log2Dim>
242 if (mData) mData[i] = val;
246 template<
typename T, Index Log2Dim>
250 if (&other !=
this) {
251 #ifdef OPENVDB_USE_DELAYED_LOADING 252 if (this->isOutOfCore()) {
253 this->detachFromFile();
258 mOutOfCore.store(other.mOutOfCore.load(std::memory_order_acquire),
259 std::memory_order_release);
260 mFileInfo =
new FileInfo(*other.mFileInfo);
263 if (other.mData !=
nullptr) {
268 while (n--) *target++ = *source++;
270 #ifdef OPENVDB_USE_DELAYED_LOADING 278 template<
typename T, Index Log2Dim>
282 this->detachFromFile();
283 if (mData !=
nullptr) {
286 while (n--) *target++ = val;
291 template<
typename T, Index Log2Dim>
297 const ValueType *target = mData, *source = other.mData;
298 if (!target && !source)
return true;
299 if (!target || !source)
return false;
306 template<
typename T, Index Log2Dim>
310 std::swap(mData, other.mData);
311 #ifdef OPENVDB_USE_DELAYED_LOADING 316 auto tmp = other.mOutOfCore.load(std::memory_order_acquire);
317 tmp = mOutOfCore.exchange(std::move(tmp));
318 other.mOutOfCore.store(std::move(tmp), std::memory_order_release);
323 template<
typename T, Index Log2Dim>
327 size_t n =
sizeof(*this);
328 #ifdef OPENVDB_USE_DELAYED_LOADING 329 if (this->isOutOfCore()) n +=
sizeof(FileInfo);
332 if (mData) n += SIZE *
sizeof(
ValueType);
333 #ifdef OPENVDB_USE_DELAYED_LOADING 336 return static_cast<Index>(n);
340 template<
typename T, Index Log2Dim>
344 size_t n =
sizeof(*this);
346 return static_cast<Index>(n);
350 template<
typename T, Index Log2Dim>
355 if (mData ==
nullptr) {
357 #ifdef OPENVDB_USE_DELAYED_LOADING 359 tbb::spin_mutex::scoped_lock lock(self->mMutex);
361 if (mData ==
nullptr)
self->mData =
new ValueType[SIZE];
366 template<
typename T, Index Log2Dim>
371 if (mData ==
nullptr) {
372 #ifdef OPENVDB_USE_DELAYED_LOADING 374 tbb::spin_mutex::scoped_lock lock(mMutex);
376 if (mData ==
nullptr) mData =
new ValueType[SIZE];
382 template<
typename T, Index Log2Dim>
390 if (mData)
return mData[i];
else return sZero;
394 template<
typename T, Index Log2Dim>
396 LeafBuffer<T, Log2Dim>::deallocate()
399 if (mData !=
nullptr) {
400 #ifdef OPENVDB_USE_DELAYED_LOADING 401 if (this->isOutOfCore())
return false;
411 template<
typename T, Index Log2Dim>
413 LeafBuffer<T, Log2Dim>::doLoad()
const 415 #ifdef OPENVDB_USE_DELAYED_LOADING 416 if (!this->isOutOfCore())
return;
418 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
422 tbb::spin_mutex::scoped_lock lock(self->mMutex);
423 if (!this->isOutOfCore())
return;
425 std::unique_ptr<FileInfo> info(self->mFileInfo);
426 assert(info.get() !=
nullptr);
427 assert(info->mapping.get() !=
nullptr);
428 assert(info->meta.get() !=
nullptr);
431 self->mData =
nullptr;
434 SharedPtr<std::streambuf> buf = info->mapping->createBuffer();
435 std::istream is(buf.get());
440 is.seekg(info->maskpos);
443 is.seekg(info->bufpos);
446 self->setOutOfCore(
false);
451 template<
typename T, Index Log2Dim>
453 LeafBuffer<T, Log2Dim>::detachFromFile()
455 #ifdef OPENVDB_USE_DELAYED_LOADING 456 if (this->isOutOfCore()) {
459 this->setOutOfCore(
false);
471 template<Index Log2Dim>
480 static const Index WORD_COUNT = NodeMaskType::WORD_COUNT;
481 static const Index SIZE = 1 << 3 * Log2Dim;
492 void fill(
bool val) { mData.set(val); }
500 if (mData.isOn(i))
return sOn;
else return sOff;
509 void swap(
LeafBuffer& other) {
if (&other !=
this) std::swap(mData, other.mData); }
543 #endif // OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED Index32 Index
Definition: Types.h:54
const bool & operator[](Index i) const
Definition: LeafBuffer.h:502
static pnanovdb_uint32_t allocate(pnanovdb_uint32_t *poffset, pnanovdb_uint32_t size, pnanovdb_uint32_t alignment)
Definition: pnanovdb_validate_strides.h:20
LeafBuffer(const LeafBuffer &other)
Definition: LeafBuffer.h:490
ValueType StorageType
Definition: LeafBuffer.h:34
bool operator==(const LeafBuffer &other) const
Definition: LeafBuffer.h:504
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
Definition: LeafBuffer.h:82
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
Definition: NodeMasks.h:307
WordType StorageType
Definition: LeafBuffer.h:478
LeafBuffer(const NodeMaskType &other)
Definition: LeafBuffer.h:489
Index memUsageIfLoaded() const
Definition: LeafBuffer.h:512
ValueType ValueType
Definition: LeafBuffer.h:33
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
Definition: LeafBuffer.h:61
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Definition: LeafBuffer.h:80
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:90
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:443
static Index size()
Return the number of values contained in this buffer.
Definition: LeafBuffer.h:111
const bool & getValue(Index i) const
Definition: LeafBuffer.h:495
void fill(bool val)
Definition: LeafBuffer.h:492
Index64 Word
Definition: NodeMasks.h:316
void swap(LeafBuffer &other)
Definition: LeafBuffer.h:509
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:102
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:465
static Index size()
Definition: LeafBuffer.h:513
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: LeafBuffer.h:71
#define OPENVDB_USE_DELAYED_LOADING
Definition: version.h.in:143
static const bool sOff
Definition: LeafBuffer.h:485
Definition: Exceptions.h:13
Index memUsage() const
Definition: LeafBuffer.h:511
~LeafBuffer()
Definition: LeafBuffer.h:491
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:517
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:473
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...
static const bool sOn
Definition: LeafBuffer.h:484
typename NodeMaskType::Word WordType
Definition: LeafBuffer.h:476
LeafBuffer & operator=(const LeafBuffer &b)
Definition: LeafBuffer.h:493
std::shared_ptr< T > SharedPtr
Definition: Types.h:114
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 ...
bool ValueType
Definition: LeafBuffer.h:477
void setValue(Index i, bool val)
Definition: LeafBuffer.h:507
bool operator!=(const LeafBuffer &other) const
Definition: LeafBuffer.h:505
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:520
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
Definition: LeafBuffer.h:30
LeafBuffer()
Definition: LeafBuffer.h:487
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
LeafBuffer()
Default constructor.
Definition: LeafBuffer.h:50
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:37
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:88
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:650
LeafBuffer(bool on)
Definition: LeafBuffer.h:488