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>
42 #if OPENVDB_ABI_VERSION_NUMBER >= 5
43 using type = tbb::atomic<Index32>;
45 static constexpr
bool IsAtomic =
true;
46 #else // OPENVDB_ABI_VERSION_NUMBER < 5
48 struct Atomic {
union { T* data;
void* ptr; }; tbb::atomic<Index32> i; tbb::spin_mutex mutex; };
49 struct NonAtomic {
union { T* data;
void* ptr; };
Index32 i; tbb::spin_mutex mutex; };
51 #ifndef __INTEL_COMPILER
52 static constexpr
bool IsAtomic = ((
sizeof(Atomic) ==
sizeof(NonAtomic))
54 && (offsetof(Atomic, i) == offsetof(NonAtomic, i)));
59 static constexpr
bool IsAtomic = (
sizeof(Atomic) ==
sizeof(NonAtomic));
61 static constexpr
size_t size =
sizeof(Atomic);
64 using type =
typename std::conditional<IsAtomic, tbb::atomic<Index32>,
Index32>::type;
73 template<
typename T, Index Log2Dim>
80 static const Index SIZE = 1 << 3 * Log2Dim;
105 bool empty()
const {
return !mData || this->isOutOfCore(); }
110 inline void fill(
const ValueType&);
117 inline void setValue(
Index i,
const ValueType&);
133 inline Index memUsage()
const;
140 const ValueType* data()
const;
148 inline const ValueType& at(
Index i)
const;
155 ValueType& operator[](
Index i) {
return const_cast<ValueType&>(this->at(i)); }
159 inline void setOutOfCore(
bool b) { mOutOfCore = b; }
162 inline void loadValues()
const {
if (this->isOutOfCore()) this->doLoad(); }
163 inline void doLoad()
const;
164 inline bool detachFromFile();
166 using FlagsType =
typename internal::LeafBufferFlags<ValueType>::type;
172 FlagsType mOutOfCore;
173 tbb::spin_mutex mMutex;
176 static const ValueType sZero;
178 friend class ::TestLeaf;
187 template<
typename T, Index Log2Dim>
191 template<
typename T, Index Log2Dim>
201 template<
typename T, Index Log2Dim>
205 if (this->isOutOfCore()) {
206 this->detachFromFile();
213 template<
typename T, Index Log2Dim>
217 , mOutOfCore(other.mOutOfCore)
220 mFileInfo =
new FileInfo(*other.
mFileInfo);
221 }
else if (other.
mData !=
nullptr) {
226 while (n--) *target++ = *source++;
231 template<
typename T, Index Log2Dim>
237 if (mData) mData[i] = val;
241 template<
typename T, Index Log2Dim>
245 if (&other !=
this) {
246 if (this->isOutOfCore()) {
247 this->detachFromFile();
252 mOutOfCore = other.mOutOfCore;
253 mFileInfo =
new FileInfo(*other.
mFileInfo);
254 }
else if (other.
mData !=
nullptr) {
259 while (n--) *target++ = *source++;
266 template<
typename T, Index Log2Dim>
270 this->detachFromFile();
271 if (mData !=
nullptr) {
274 while (n--) *target++ = val;
279 template<
typename T, Index Log2Dim>
286 if (!target && !source)
return true;
287 if (!target || !source)
return false;
294 template<
typename T, Index Log2Dim>
298 std::swap(mData, other.
mData);
299 std::swap(mOutOfCore, other.mOutOfCore);
303 template<
typename T, Index Log2Dim>
307 size_t n =
sizeof(*this);
308 if (this->isOutOfCore()) n +=
sizeof(FileInfo);
309 else if (mData) n += SIZE *
sizeof(
ValueType);
310 return static_cast<Index>(n);
314 template<
typename T, Index Log2Dim>
319 if (mData ==
nullptr) {
320 LeafBuffer*
self = const_cast<LeafBuffer*>(
this);
322 tbb::spin_mutex::scoped_lock lock(self->mMutex);
323 if (mData ==
nullptr)
self->mData =
new ValueType[SIZE];
328 template<
typename T, Index Log2Dim>
333 if (mData ==
nullptr) {
335 tbb::spin_mutex::scoped_lock lock(mMutex);
336 if (mData ==
nullptr) mData =
new ValueType[SIZE];
342 template<
typename T, Index Log2Dim>
350 if (mData)
return mData[i];
else return sZero;
354 template<
typename T, Index Log2Dim>
356 LeafBuffer<T, Log2Dim>::deallocate()
358 if (mData !=
nullptr && !this->isOutOfCore()) {
367 template<
typename T, Index Log2Dim>
369 LeafBuffer<T, Log2Dim>::doLoad()
const
371 if (!this->isOutOfCore())
return;
373 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
377 tbb::spin_mutex::scoped_lock lock(self->mMutex);
378 if (!this->isOutOfCore())
return;
380 std::unique_ptr<FileInfo> info(self->mFileInfo);
381 assert(info.get() !=
nullptr);
382 assert(info->mapping.get() !=
nullptr);
383 assert(info->meta.get() !=
nullptr);
386 self->mData =
nullptr;
389 SharedPtr<std::streambuf> buf = info->mapping->createBuffer();
390 std::istream is(buf.get());
395 is.seekg(info->maskpos);
398 is.seekg(info->bufpos);
401 self->setOutOfCore(
false);
405 template<
typename T, Index Log2Dim>
407 LeafBuffer<T, Log2Dim>::detachFromFile()
409 if (this->isOutOfCore()) {
412 this->setOutOfCore(
false);
423 template<Index Log2Dim>
432 static const Index WORD_COUNT = NodeMaskType::WORD_COUNT;
433 static const Index SIZE = 1 << 3 * Log2Dim;
444 void fill(
bool val) { mData.set(val); }
452 if (mData.isOn(i))
return sOn;
else return sOff;
471 const WordType*
data()
const {
return const_cast<LeafBuffer*>(
this)->data(); }
492 #endif // OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
Definition: LeafBuffer.h:74
bool operator==(const LeafBuffer &other) const
Definition: LeafBuffer.h:456
SharedPtr< io::StreamMetadata > meta
Definition: LeafBuffer.h:88
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 ...
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:471
SharedPtr< MappedFile > Ptr
Definition: io.h:136
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:468
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
Definition: LeafBuffer.h:107
std::streamoff bufpos
Definition: LeafBuffer.h:85
WordType StorageType
Definition: LeafBuffer.h:430
~LeafBuffer()
Definition: LeafBuffer.h:443
LeafBuffer()
Definition: LeafBuffer.h:439
typename NodeMaskType::Word WordType
Definition: LeafBuffer.h:428
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
const bool & operator[](Index i) const
Definition: LeafBuffer.h:454
tbb::atomic< Index32 > type
The type of LeafBuffer::mOutOfCore.
Definition: LeafBuffer.h:44
LeafBuffer()
Default constructor.
Definition: LeafBuffer.h:92
FileInfo * mFileInfo
Definition: LeafBuffer.h:170
void setValue(Index i, bool val)
Definition: LeafBuffer.h:459
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 sOff
Definition: LeafBuffer.h:437
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Definition: LeafBuffer.h:105
void fill(bool val)
Definition: LeafBuffer.h:444
Definition: LeafBuffer.h:82
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation.
Definition: NodeMasks.h:288
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:465
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:471
ValueType * mData
Definition: LeafBuffer.h:169
LeafBuffer(const NodeMaskType &other)
Definition: LeafBuffer.h:441
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim....
Definition: LeafNode.h:37
io::MappedFile::Ptr mapping
Definition: LeafBuffer.h:87
bool ValueType
Definition: LeafBuffer.h:429
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
LeafBuffer(const LeafBuffer &other)
Definition: LeafBuffer.h:442
ValueType ValueType
Definition: LeafBuffer.h:77
LeafBuffer(bool on)
Definition: LeafBuffer.h:440
static Index size()
Definition: LeafBuffer.h:464
Index32 Index
Definition: Types.h:31
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
Definition: LeafBuffer.h:98
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:388
std::streamoff maskpos
Definition: LeafBuffer.h:86
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
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:127
uint32_t Index32
Definition: Types.h:29
static const bool sOn
Definition: LeafBuffer.h:436
Definition: LeafBuffer.h:40
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:683
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:113
FileInfo()
Definition: LeafBuffer.h:84
Definition: Exceptions.h:13
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: LeafBuffer.h:103
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:115
const bool & getValue(Index i) const
Definition: LeafBuffer.h:447
ValueType StorageType
Definition: LeafBuffer.h:78
void swap(LeafBuffer &other)
Definition: LeafBuffer.h:461
bool operator!=(const LeafBuffer &other) const
Definition: LeafBuffer.h:457
Index memUsage() const
Definition: LeafBuffer.h:463
static Index size()
Return the number of values contained in this buffer.
Definition: LeafBuffer.h:135
Index64 Word
Definition: NodeMasks.h:297
LeafBuffer & operator=(const LeafBuffer &b)
Definition: LeafBuffer.h:445