Go to the documentation of this file.
4 #ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
5 #define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
10 #include <tbb/atomic.h>
11 #include <tbb/spin_mutex.h>
16 #include <type_traits>
43 using type = tbb::atomic<Index32>;
44 static constexpr
bool IsAtomic =
true;
52 template<
typename T, Index Log2Dim>
59 static const Index SIZE = 1 << 3 * Log2Dim;
84 bool empty()
const {
return !mData || this->isOutOfCore(); }
138 inline void setOutOfCore(
bool b) { mOutOfCore = b; }
141 inline void loadValues()
const {
if (this->isOutOfCore()) this->doLoad(); }
142 inline void doLoad()
const;
143 inline bool detachFromFile();
145 using FlagsType =
typename internal::LeafBufferFlags<ValueType>::type;
151 FlagsType mOutOfCore;
152 tbb::spin_mutex mMutex;
155 static const ValueType sZero;
157 friend class ::TestLeaf;
166 template<
typename T, Index Log2Dim>
170 template<
typename T, Index Log2Dim>
180 template<
typename T, Index Log2Dim>
184 if (this->isOutOfCore()) {
185 this->detachFromFile();
192 template<
typename T, Index Log2Dim>
196 , mOutOfCore(other.mOutOfCore)
200 }
else if (other.
mData !=
nullptr) {
205 while (n--) *target++ = *source++;
210 template<
typename T, Index Log2Dim>
216 if (mData) mData[i] = val;
220 template<
typename T, Index Log2Dim>
224 if (&other !=
this) {
225 if (this->isOutOfCore()) {
226 this->detachFromFile();
231 mOutOfCore = other.mOutOfCore;
233 }
else if (other.
mData !=
nullptr) {
238 while (n--) *target++ = *source++;
245 template<
typename T, Index Log2Dim>
249 this->detachFromFile();
250 if (mData !=
nullptr) {
253 while (n--) *target++ = val;
258 template<
typename T, Index Log2Dim>
265 if (!target && !source)
return true;
266 if (!target || !source)
return false;
273 template<
typename T, Index Log2Dim>
277 std::swap(mData, other.
mData);
278 std::swap(mOutOfCore, other.mOutOfCore);
282 template<
typename T, Index Log2Dim>
286 size_t n =
sizeof(*this);
287 if (this->isOutOfCore()) n +=
sizeof(
FileInfo);
288 else if (mData) n += SIZE *
sizeof(
ValueType);
289 return static_cast<Index>(n);
293 template<
typename T, Index Log2Dim>
298 if (mData ==
nullptr) {
301 tbb::spin_mutex::scoped_lock lock(self->mMutex);
302 if (mData ==
nullptr)
self->mData =
new ValueType[SIZE];
307 template<
typename T, Index Log2Dim>
312 if (mData ==
nullptr) {
314 tbb::spin_mutex::scoped_lock lock(mMutex);
315 if (mData ==
nullptr) mData =
new ValueType[SIZE];
321 template<
typename T, Index Log2Dim>
329 if (mData)
return mData[i];
else return sZero;
333 template<
typename T, Index Log2Dim>
335 LeafBuffer<T, Log2Dim>::deallocate()
337 if (mData !=
nullptr && !this->isOutOfCore()) {
346 template<
typename T, Index Log2Dim>
348 LeafBuffer<T, Log2Dim>::doLoad()
const
350 if (!this->isOutOfCore())
return;
352 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
356 tbb::spin_mutex::scoped_lock lock(self->mMutex);
357 if (!this->isOutOfCore())
return;
359 std::unique_ptr<FileInfo> info(self->mFileInfo);
360 assert(info.get() !=
nullptr);
361 assert(info->mapping.get() !=
nullptr);
362 assert(info->meta.get() !=
nullptr);
365 self->mData =
nullptr;
368 SharedPtr<std::streambuf> buf = info->mapping->createBuffer();
369 std::istream is(buf.get());
374 is.seekg(info->maskpos);
377 is.seekg(info->bufpos);
380 self->setOutOfCore(
false);
384 template<
typename T, Index Log2Dim>
386 LeafBuffer<T, Log2Dim>::detachFromFile()
388 if (this->isOutOfCore()) {
391 this->setOutOfCore(
false);
402 template<Index Log2Dim>
431 if (
mData.isOn(i))
return sOn;
else return sOff;
471 #endif // OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
SharedPtr< io::StreamMetadata > meta
Definition: LeafBuffer.h:67
~LeafBuffer()
Destructor.
Definition: LeafBuffer.h:182
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:465
SharedPtr< MappedFile > Ptr
Definition: io.h:136
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:450
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
Definition: LeafBuffer.h:86
Index32 Index
Definition: Types.h:31
std::streamoff bufpos
Definition: LeafBuffer.h:64
static const Index SIZE
Definition: LeafBuffer.h:59
ValueType * data()
Return a pointer to the array of voxel values.
Definition: LeafBuffer.h:309
void fill(const ValueType &)
Populate this buffer with a constant value.
Definition: LeafBuffer.h:247
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,...
LeafBuffer(const ValueType &)
Construct a buffer populated with the specified value.
Definition: LeafBuffer.h:172
LeafBuffer()
Default constructor.
Definition: LeafBuffer.h:71
LeafBuffer(const NodeMaskType &other)
Definition: LeafBuffer.h:420
void setValue(Index i, bool val)
Definition: LeafBuffer.h:438
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:1047
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 ...
Index memUsage() const
Return the memory footprint of this buffer in bytes.
Definition: LeafBuffer.h:284
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:408
FileInfo * mFileInfo
Definition: LeafBuffer.h:149
void fill(bool val)
Definition: LeafBuffer.h:423
LeafBuffer(const LeafBuffer &other)
Definition: LeafBuffer.h:421
~LeafBuffer()
Definition: LeafBuffer.h:422
const bool & operator[](Index i) const
Definition: LeafBuffer.h:433
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Definition: LeafBuffer.h:84
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation.
Definition: NodeMasks.h:308
ValueType * mData
Definition: LeafBuffer.h:148
LeafBuffer & operator=(const LeafBuffer &)
Copy the other buffer's values into this buffer.
Definition: LeafBuffer.h:222
void setValue(Index i, const ValueType &)
Set the i'th value of this buffer to the specified value.
Definition: LeafBuffer.h:212
WordType StorageType
Definition: LeafBuffer.h:409
static Index size()
Definition: LeafBuffer.h:443
LeafBuffer & operator=(const LeafBuffer &b)
Definition: LeafBuffer.h:424
io::MappedFile::Ptr mapping
Definition: LeafBuffer.h:66
bool operator==(const LeafBuffer &) const
Return true if the contents of the other buffer exactly equal the contents of this buffer.
Definition: LeafBuffer.h:260
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim....
Definition: LeafNode.h:38
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
ValueType ValueType
Definition: LeafBuffer.h:56
bool ValueType
Definition: LeafBuffer.h:408
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
Definition: LeafBuffer.h:54
const ValueType * data() const
Return a const pointer to the array of voxel values.
Definition: LeafBuffer.h:295
void swap(LeafBuffer &)
Exchange this buffer's values with the other buffer's values.
Definition: LeafBuffer.h:275
Index64 Word
Definition: NodeMasks.h:316
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
Definition: LeafBuffer.h:77
LeafBuffer(const LeafBuffer &)
Copy constructor.
Definition: LeafBuffer.h:194
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:447
static const bool sOn
Definition: LeafBuffer.h:415
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
std::streamoff maskpos
Definition: LeafBuffer.h:65
const bool & getValue(Index i) const
Definition: LeafBuffer.h:426
Definition: LeafBuffer.h:41
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:106
typename NodeMaskType::Word WordType
Definition: LeafBuffer.h:407
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:92
LeafBuffer(bool on)
Definition: LeafBuffer.h:419
void swap(LeafBuffer &other)
Definition: LeafBuffer.h:440
Definition: LeafBuffer.h:62
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:94
FileInfo()
Definition: LeafBuffer.h:63
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: LeafBuffer.h:82
Definition: Exceptions.h:13
static const Index32 WORD_COUNT
Definition: NodeMasks.h:315
bool operator==(const LeafBuffer &other) const
Definition: LeafBuffer.h:435
tbb::atomic< Index32 > type
The type of LeafBuffer::mOutOfCore.
Definition: LeafBuffer.h:43
ValueType StorageType
Definition: LeafBuffer.h:57
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
LeafBuffer()
Definition: LeafBuffer.h:418
static Index size()
Return the number of values contained in this buffer.
Definition: LeafBuffer.h:114
bool operator!=(const LeafBuffer &other) const
Definition: LeafBuffer.h:436
static const bool sOff
Definition: LeafBuffer.h:416
Index memUsage() const
Definition: LeafBuffer.h:442