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/spin_mutex.h>
16 #include <type_traits>
29 template<
typename T, Index Log2Dim>
36 static const Index SIZE = 1 << 3 * Log2Dim;
61 bool empty()
const {
return !mData || this->isOutOfCore(); }
66 inline void fill(
const ValueType&);
73 inline void setValue(
Index i,
const ValueType&);
96 const ValueType* data()
const;
104 inline const ValueType& at(
Index i)
const;
111 ValueType& operator[](
Index i) {
return const_cast<ValueType&
>(this->at(i)); }
115 inline void setOutOfCore(
bool b) { mOutOfCore = b; }
118 inline void loadValues()
const {
if (this->isOutOfCore()) this->doLoad(); }
119 inline void doLoad()
const;
120 inline bool detachFromFile();
122 using FlagsType = std::atomic<Index32>;
128 FlagsType mOutOfCore;
129 tbb::spin_mutex mMutex;
132 static const ValueType sZero;
134 friend class ::TestLeaf;
143 template<
typename T, Index Log2Dim>
147 template<
typename T, Index Log2Dim>
157 template<
typename T, Index Log2Dim>
161 if (this->isOutOfCore()) {
162 this->detachFromFile();
169 template<
typename T, Index Log2Dim>
173 , mOutOfCore(other.mOutOfCore.load())
176 mFileInfo =
new FileInfo(*other.
mFileInfo);
177 }
else if (other.
mData !=
nullptr) {
182 while (n--) *target++ = *source++;
187 template<
typename T, Index Log2Dim>
193 if (mData) mData[i] = val;
197 template<
typename T, Index Log2Dim>
201 if (&other !=
this) {
202 if (this->isOutOfCore()) {
203 this->detachFromFile();
208 mOutOfCore.store(other.mOutOfCore.load(std::memory_order_acquire),
209 std::memory_order_release);
210 mFileInfo =
new FileInfo(*other.
mFileInfo);
211 }
else if (other.
mData !=
nullptr) {
216 while (n--) *target++ = *source++;
223 template<
typename T, Index Log2Dim>
227 this->detachFromFile();
228 if (mData !=
nullptr) {
231 while (n--) *target++ = val;
236 template<
typename T, Index Log2Dim>
243 if (!target && !source)
return true;
244 if (!target || !source)
return false;
251 template<
typename T, Index Log2Dim>
255 std::swap(mData, other.
mData);
261 auto tmp = other.mOutOfCore.load(std::memory_order_acquire);
262 tmp = mOutOfCore.exchange(std::move(tmp));
263 other.mOutOfCore.store(std::move(tmp), std::memory_order_release);
267 template<
typename T, Index Log2Dim>
271 size_t n =
sizeof(*this);
272 if (this->isOutOfCore()) n +=
sizeof(FileInfo);
273 else if (mData) n += SIZE *
sizeof(
ValueType);
274 return static_cast<Index>(n);
278 template<
typename T, Index Log2Dim>
283 if (mData ==
nullptr) {
286 tbb::spin_mutex::scoped_lock lock(self->mMutex);
287 if (mData ==
nullptr)
self->mData =
new ValueType[SIZE];
292 template<
typename T, Index Log2Dim>
297 if (mData ==
nullptr) {
299 tbb::spin_mutex::scoped_lock lock(mMutex);
300 if (mData ==
nullptr) mData =
new ValueType[SIZE];
306 template<
typename T, Index Log2Dim>
307 inline const typename LeafBuffer<T, Log2Dim>::ValueType&
308 LeafBuffer<T, Log2Dim>::at(
Index i)
const
314 if (mData)
return mData[i];
else return sZero;
318 template<
typename T, Index Log2Dim>
320 LeafBuffer<T, Log2Dim>::deallocate()
322 if (mData !=
nullptr && !this->isOutOfCore()) {
331 template<
typename T, Index Log2Dim>
333 LeafBuffer<T, Log2Dim>::doLoad()
const
335 if (!this->isOutOfCore())
return;
337 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
341 tbb::spin_mutex::scoped_lock lock(self->mMutex);
342 if (!this->isOutOfCore())
return;
344 std::unique_ptr<FileInfo> info(self->mFileInfo);
345 assert(info.get() !=
nullptr);
346 assert(info->mapping.get() !=
nullptr);
347 assert(info->meta.get() !=
nullptr);
350 self->mData =
nullptr;
353 SharedPtr<std::streambuf> buf = info->mapping->createBuffer();
354 std::istream is(buf.get());
359 is.seekg(info->maskpos);
362 is.seekg(info->bufpos);
365 self->setOutOfCore(
false);
369 template<
typename T, Index Log2Dim>
371 LeafBuffer<T, Log2Dim>::detachFromFile()
373 if (this->isOutOfCore()) {
376 this->setOutOfCore(
false);
387 template<Index Log2Dim>
396 static const Index WORD_COUNT = NodeMaskType::WORD_COUNT;
397 static const Index SIZE = 1 << 3 * Log2Dim;
408 void fill(
bool val) { mData.set(val); }
416 if (mData.isOn(i))
return sOn;
else return sOff;
456 #endif // OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:477
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(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
Definition: LeafBuffer.h:54
ValueType ValueType
Definition: LeafBuffer.h:33
FileInfo()
Definition: LeafBuffer.h:40
std::streamoff maskpos
Definition: LeafBuffer.h:42
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178
LeafBuffer()
Default constructor.
Definition: LeafBuffer.h:48
SharedPtr< MappedFile > Ptr
Definition: io.h:136
FileInfo * mFileInfo
Definition: LeafBuffer.h:126
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:444
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:435
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:83
ValueType * mData
Definition: LeafBuffer.h:125
ValueType StorageType
Definition: LeafBuffer.h:34
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Definition: LeafBuffer.h:61
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation.
Definition: NodeMasks.h:307
Definition: LeafBuffer.h:38
LeafBuffer(const NodeMaskType &other)
Definition: LeafBuffer.h:405
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:465
std::streamoff bufpos
Definition: LeafBuffer.h:41
void setValue(Index i, bool val)
Definition: LeafBuffer.h:423
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
Definition: LeafBuffer.h:63
Tag dispatch class that distinguishes constructors during file input.
Definition: openvdb/Types.h:566
Index32 Index
Definition: openvdb/Types.h:50
LeafBuffer(const LeafBuffer &other)
Definition: LeafBuffer.h:406
void fill(bool val)
Definition: LeafBuffer.h:408
~LeafBuffer()
Definition: LeafBuffer.h:407
const bool & operator[](Index i) const
Definition: LeafBuffer.h:418
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:69
std::shared_ptr< T > SharedPtr
Definition: openvdb/Types.h:110
WordType StorageType
Definition: LeafBuffer.h:394
static Index size()
Definition: LeafBuffer.h:428
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 ...
LeafBuffer & operator=(const LeafBuffer &b)
Definition: LeafBuffer.h:409
Index64 Word
Definition: NodeMasks.h:316
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:71
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: LeafBuffer.h:59
static Index size()
Return the number of values contained in this buffer.
Definition: LeafBuffer.h:91
bool ValueType
Definition: LeafBuffer.h:393
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:432
static const bool sOn
Definition: LeafBuffer.h:400
SharedPtr< io::StreamMetadata > meta
Definition: LeafBuffer.h:44
typename NodeMaskType::Word WordType
Definition: LeafBuffer.h:392
const bool & getValue(Index i) const
Definition: LeafBuffer.h:411
LeafBuffer(bool on)
Definition: LeafBuffer.h:404
void swap(LeafBuffer &other)
Definition: LeafBuffer.h:425
Definition: openvdb/Exceptions.h:13
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
Definition: LeafBuffer.h:30
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim....
Definition: LeafNode.h:37
bool operator==(const LeafBuffer &other) const
Definition: LeafBuffer.h:420
LeafBuffer()
Definition: LeafBuffer.h:403
static const bool sOff
Definition: LeafBuffer.h:401
bool operator!=(const LeafBuffer &other) const
Definition: LeafBuffer.h:421
Index memUsage() const
Definition: LeafBuffer.h:427
io::MappedFile::Ptr mapping
Definition: LeafBuffer.h:43