37 #ifndef OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED 38 #define OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED 50 #include <tbb/spin_mutex.h> 51 #include <tbb/atomic.h> 55 #include <type_traits> 58 class TestAttributeArray;
74 template <
typename IntegerT,
typename FloatT>
78 static_assert(std::is_unsigned<IntegerT>::value,
"IntegerT must be unsigned");
85 template <
typename FloatT,
typename IntegerT>
89 static_assert(std::is_unsigned<IntegerT>::value,
"IntegerT must be unsigned");
93 template <
typename IntegerVectorT,
typename FloatT>
97 return IntegerVectorT(
98 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
x()),
99 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
y()),
100 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
z()));
103 template <
typename FloatVectorT,
typename IntegerT>
108 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
x()),
109 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
y()),
110 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
z()));
131 CONSTANTSTRIDE = 0x8,
138 WRITEMEMCOMPRESS = 0x4,
142 using Ptr = std::shared_ptr<AttributeArray>;
143 using ConstPtr = std::shared_ptr<const AttributeArray>;
162 virtual Index size()
const = 0;
166 virtual Index stride()
const = 0;
170 virtual Index dataSize()
const = 0;
173 virtual size_t memUsage()
const = 0;
178 static bool isRegistered(
const NamePair& type);
180 static void clearRegistry();
183 virtual const NamePair& type()
const = 0;
185 template<
typename AttributeArrayType>
186 bool isType()
const {
return this->type() == AttributeArrayType::attributeType(); }
189 template<
typename ValueType>
190 bool hasValueType()
const {
return this->type().first == typeNameAsString<ValueType>(); }
196 virtual bool isUniform()
const = 0;
199 virtual void expand(
bool fill =
true) = 0;
201 virtual void collapse() = 0;
203 virtual bool compact() = 0;
208 virtual bool compress() = 0;
210 virtual bool decompress() = 0;
216 void setHidden(
bool state);
218 bool isHidden()
const {
return bool(mFlags & HIDDEN); }
223 void setTransient(
bool state);
231 void setStreaming(
bool state);
239 uint8_t
flags()
const {
return mFlags; }
242 virtual void read(std::istream&) = 0;
245 virtual void write(std::ostream&,
bool outputTransient)
const = 0;
247 virtual void write(std::ostream&)
const = 0;
250 virtual void readMetadata(std::istream&) = 0;
254 virtual void writeMetadata(std::ostream&,
bool outputTransient,
bool paged)
const = 0;
257 virtual void readBuffers(std::istream&) = 0;
260 virtual void writeBuffers(std::ostream&,
bool outputTransient)
const = 0;
269 virtual void loadData()
const = 0;
278 friend class ::TestAttributeArray;
286 void setConstantStride(
bool state);
294 static void unregisterType(
const NamePair& type);
296 size_t mCompressedBytes = 0;
298 uint8_t mSerializationFlags = 0;
300 #if OPENVDB_ABI_VERSION_NUMBER >= 5 301 tbb::atomic<Index32> mOutOfCore = 0;
317 template <
typename T>
325 mGetter(getter), mSetter(setter), mCollapser(collapser), mFiller(filler) { }
337 namespace attribute_traits
369 template <
typename T>
372 template<
typename ValueType>
static void decode(
const ValueType&, ValueType&);
373 template<
typename ValueType>
static void encode(
const ValueType&, ValueType&);
374 static const char*
name() {
return "null"; }
380 template <
typename T>
383 template<
typename StorageType,
typename ValueType>
static void decode(
const StorageType&, ValueType&);
384 template<
typename StorageType,
typename ValueType>
static void encode(
const ValueType&, StorageType&);
385 static const char*
name() {
return "trnc"; }
392 static const char*
name() {
return "fxpt"; }
393 template <
typename ValueType>
static ValueType
encode(
const ValueType& value) {
return value + ValueType(0.5); }
394 template <
typename ValueType>
static ValueType
decode(
const ValueType& value) {
return value - ValueType(0.5); }
401 static const char*
name() {
return "ufxpt"; }
402 template <
typename ValueType>
static ValueType
encode(
const ValueType& value) {
return value; }
403 template <
typename ValueType>
static ValueType
decode(
const ValueType& value) {
return value; }
407 template <
bool OneByte,
typename Range=PositionRange>
410 template <
typename T>
413 template<
typename StorageType,
typename ValueType>
static void decode(
const StorageType&, ValueType&);
414 template<
typename StorageType,
typename ValueType>
static void encode(
const ValueType&, StorageType&);
417 static const std::string
Name = std::string(Range::name()) + (OneByte ?
"8" :
"16");
427 template <
typename T>
432 static const char*
name() {
return "uvec"; }
440 template<
typename ValueType_,
typename Codec_ = NullCodec>
444 using Ptr = std::shared_ptr<TypedAttributeArray>;
445 using ConstPtr = std::shared_ptr<const TypedAttributeArray>;
449 using StorageType =
typename Codec::template Storage<ValueType>::Type;
455 const ValueType& uniformValue = zeroVal<ValueType>());
474 static Ptr create(
Index n,
Index strideOrTotalSize = 1,
bool constantStride =
true);
483 static const NamePair& attributeType();
488 static bool isRegistered();
490 static void registerType();
492 static void unregisterType();
499 Index stride()
const override {
return hasConstantStride() ? mStrideOrTotalSize : 0; }
503 return hasConstantStride() ? mSize * mStrideOrTotalSize : mStrideOrTotalSize;
507 size_t memUsage()
const override;
510 ValueType getUnsafe(
Index n)
const;
512 ValueType
get(
Index n)
const;
514 template<
typename T>
void getUnsafe(
Index n, T& value)
const;
516 template<
typename T>
void get(
Index n, T& value)
const;
523 void setUnsafe(
Index n,
const ValueType& value);
525 void set(
Index n,
const ValueType& value);
527 template<
typename T>
void setUnsafe(
Index n,
const T& value);
529 template<
typename T>
void set(
Index n,
const T& value);
543 void expand(
bool fill =
true)
override;
545 void collapse()
override;
547 bool compact()
override;
550 void collapse(
const ValueType& uniformValue);
553 void fill(
const ValueType& value);
556 static void collapse(
AttributeArray* array,
const ValueType& value);
561 bool compress()
override;
563 bool decompress()
override;
566 void read(std::istream&)
override;
570 void write(std::ostream& os,
bool outputTransient)
const override;
572 void write(std::ostream&)
const override;
575 void readMetadata(std::istream&)
override;
580 void writeMetadata(std::ostream& os,
bool outputTransient,
bool paged)
const override;
583 void readBuffers(std::istream&)
override;
587 void writeBuffers(std::ostream& os,
bool outputTransient)
const override;
597 inline bool isOutOfCore()
const;
600 void loadData()
const override;
603 AccessorBasePtr getAccessor()
const override;
607 inline void doLoad()
const;
610 inline void doLoadUnsafe(
const bool compression =
true)
const;
612 inline bool compressUnsafe();
615 inline void setOutOfCore(
const bool);
620 size_t arrayMemUsage()
const;
626 return TypedAttributeArray::create(n, strideOrTotalSize, constantStride);
629 static tbb::atomic<const NamePair*> sTypeName;
630 std::unique_ptr<StorageType[]> mData;
632 Index mStrideOrTotalSize;
633 bool mIsUniform =
false;
634 tbb::spin_mutex mMutex;
643 template <
typename ValueType,
typename CodecType = UnknownCodec>
648 using Ptr = std::shared_ptr<Handle>;
657 static Ptr create(
const AttributeArray& array,
const bool preserveCompression =
true);
669 bool isUniform()
const;
670 bool hasConstantStride()
const;
685 friend class ::TestAttributeArray;
687 template <
bool IsUnknownCodec>
688 typename std::enable_if<IsUnknownCodec, bool>::type compatibleType()
const;
690 template <
bool IsUnknownCodec>
691 typename std::enable_if<!IsUnknownCodec, bool>::type compatibleType()
const;
693 template <
bool IsUnknownCodec>
694 typename std::enable_if<IsUnknownCodec, ValueType>::type
get(
Index index)
const;
696 template <
bool IsUnknownCodec>
697 typename std::enable_if<!IsUnknownCodec, ValueType>::type
get(
Index index)
const;
702 Index mStrideOrTotalSize;
704 bool mCollapseOnDestruction;
712 template <
typename ValueType,
typename CodecType = UnknownCodec>
717 using Ptr = std::shared_ptr<Handle>;
728 void expand(
bool fill =
true);
732 void collapse(
const ValueType& uniformValue);
739 void fill(
const ValueType& value);
741 void set(
Index n,
const ValueType& value);
742 void set(
Index n,
Index m,
const ValueType& value);
745 friend class ::TestAttributeArray;
747 template <
bool IsUnknownCodec>
748 typename std::enable_if<IsUnknownCodec, void>::type
set(
Index index,
const ValueType& value)
const;
750 template <
bool IsUnknownCodec>
751 typename std::enable_if<!IsUnknownCodec, void>::type
set(
Index index,
const ValueType& value)
const;
761 template<
typename ValueType>
763 NullCodec::decode(
const ValueType& data, ValueType& val)
769 template<
typename ValueType>
771 NullCodec::encode(
const ValueType& val, ValueType& data)
777 template<
typename StorageType,
typename ValueType>
779 TruncateCodec::decode(
const StorageType& data, ValueType& val)
781 val =
static_cast<ValueType
>(data);
785 template<
typename StorageType,
typename ValueType>
787 TruncateCodec::encode(
const ValueType& val, StorageType& data)
789 data =
static_cast<StorageType
>(val);
793 template <
bool OneByte,
typename Range>
794 template<
typename StorageType,
typename ValueType>
798 val = fixedPointToFloatingPoint<ValueType>(data);
802 val = Range::template decode<ValueType>(val);
806 template <
bool OneByte,
typename Range>
807 template<
typename StorageType,
typename ValueType>
813 const ValueType newVal = Range::template encode<ValueType>(val);
815 data = floatingPointToFixedPoint<StorageType>(newVal);
823 val = math::QuantizedUnitVec::unpack(data);
831 data = math::QuantizedUnitVec::pack(val);
839 template<
typename ValueType_,
typename Codec_>
843 template<
typename ValueType_,
typename Codec_>
848 , mStrideOrTotalSize(strideOrTotalSize)
851 if (constantStride) {
853 if (strideOrTotalSize == 0) {
855 "stride to be at least one.")
860 if (mStrideOrTotalSize < n) {
862 "a total size of at least the number of elements in the array.")
867 Codec::encode(uniformValue, mData.get()[0]);
871 template<
typename ValueType_,
typename Codec_>
875 , mStrideOrTotalSize(rhs.mStrideOrTotalSize)
876 , mIsUniform(rhs.mIsUniform)
884 }
else if (mIsUniform) {
886 mData.get()[0] = rhs.mData.get()[0];
888 std::unique_ptr<char[]> buffer;
890 const char* charBuffer =
reinterpret_cast<const char*
>(rhs.mData.get());
902 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
905 std::memcpy(mData.get(), rhs.mData.get(), this->arrayMemUsage());
910 template<
typename ValueType_,
typename Codec_>
915 tbb::spin_mutex::scoped_lock lock(mMutex);
923 mStrideOrTotalSize = rhs.mStrideOrTotalSize;
924 mIsUniform = rhs.mIsUniform;
928 }
else if (mIsUniform) {
930 mData.get()[0] = rhs.mData.get()[0];
931 }
else if (this->isCompressed()) {
932 std::unique_ptr<char[]> buffer(
new char[mCompressedBytes]);
933 std::memcpy(buffer.get(), rhs.mData.get(), mCompressedBytes);
934 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
937 std::memcpy(mData.get(), rhs.mData.get(), arrayMemUsage());
943 template<
typename ValueType_,
typename Codec_>
947 if (sTypeName ==
nullptr) {
949 if (sTypeName.compare_and_swap(s,
nullptr) !=
nullptr)
delete s;
955 template<
typename ValueType_,
typename Codec_>
963 template<
typename ValueType_,
typename Codec_>
971 template<
typename ValueType_,
typename Codec_>
979 template<
typename ValueType_,
typename Codec_>
986 template<
typename ValueType_,
typename Codec_>
996 template<
typename ValueType_,
typename Codec_>
1006 template<
typename ValueType_,
typename Codec_>
1014 template<
typename ValueType_,
typename Codec_>
1022 template<
typename ValueType_,
typename Codec_>
1026 if (this->isOutOfCore())
return 0;
1027 if (this->isCompressed())
return mCompressedBytes;
1029 return (mIsUniform ? 1 : this->dataSize()) *
sizeof(StorageType);
1033 template<
typename ValueType_,
typename Codec_>
1035 TypedAttributeArray<ValueType_, Codec_>::allocate()
1039 mData.reset(
new StorageType[1]);
1042 const size_t size(this->dataSize());
1044 mData.reset(
new StorageType[size]);
1049 template<
typename ValueType_,
typename Codec_>
1051 TypedAttributeArray<ValueType_, Codec_>::deallocate()
1054 if (this->isOutOfCore()) {
1055 this->setOutOfCore(
false);
1056 this->mPageHandle.reset();
1058 if (mData) mData.reset();
1062 template<
typename ValueType_,
typename Codec_>
1066 return sizeof(*this) + (bool(mData) ? this->arrayMemUsage() : 0);
1070 template<
typename ValueType_,
typename Codec_>
1074 assert(n < this->dataSize());
1075 assert(!this->isOutOfCore());
1076 assert(!this->isCompressed());
1079 Codec::decode(mData.get()[mIsUniform ? 0 : n], val);
1084 template<
typename ValueType_,
typename Codec_>
1089 if (this->isOutOfCore()) this->doLoad();
1092 return this->getUnsafe(n);
1096 template<
typename ValueType_,
typename Codec_>
1097 template<
typename T>
1101 val =
static_cast<T
>(this->getUnsafe(n));
1105 template<
typename ValueType_,
typename Codec_>
1106 template<
typename T>
1110 val =
static_cast<T
>(this->
get(n));
1114 template<
typename ValueType_,
typename Codec_>
1122 template<
typename ValueType_,
typename Codec_>
1126 assert(n < this->dataSize());
1127 assert(!this->isOutOfCore());
1128 assert(!this->isCompressed());
1129 assert(!this->isUniform());
1134 Codec::encode(val, mData.get()[mIsUniform ? 0 : n]);
1138 template<
typename ValueType_,
typename Codec_>
1143 if (this->isOutOfCore()) this->doLoad();
1144 if (this->isCompressed()) this->decompress();
1145 if (this->isUniform()) this->expand();
1147 this->setUnsafe(n, val);
1151 template<
typename ValueType_,
typename Codec_>
1152 template<
typename T>
1156 this->setUnsafe(n, static_cast<ValueType>(val));
1160 template<
typename ValueType_,
typename Codec_>
1161 template<
typename T>
1165 this->
set(n,
static_cast<ValueType>(val));
1169 template<
typename ValueType_,
typename Codec_>
1177 template<
typename ValueType_,
typename Codec_>
1184 sourceTypedArray.
get(sourceIndex, sourceValue);
1186 this->
set(n, sourceValue);
1190 template<
typename ValueType_,
typename Codec_>
1194 if (!mIsUniform)
return;
1199 tbb::spin_mutex::scoped_lock lock(mMutex);
1205 mCompressedBytes = 0;
1208 for (
Index i = 0; i < this->dataSize(); ++i) mData.get()[i] = val;
1213 template<
typename ValueType_,
typename Codec_>
1217 if (mIsUniform)
return true;
1220 const ValueType_ val = this->
get(0);
1221 for (
Index i = 1; i < this->dataSize(); i++) {
1225 this->collapse(this->
get(0));
1230 template<
typename ValueType_,
typename Codec_>
1234 this->collapse(zeroVal<ValueType>());
1238 template<
typename ValueType_,
typename Codec_>
1243 tbb::spin_mutex::scoped_lock lock(mMutex);
1248 Codec::encode(uniformValue, mData.get()[0]);
1252 template<
typename ValueType_,
typename Codec_>
1260 template<
typename ValueType_,
typename Codec_>
1264 if (this->isOutOfCore()) {
1265 tbb::spin_mutex::scoped_lock lock(mMutex);
1270 const Index size = mIsUniform ? 1 : this->dataSize();
1271 for (
Index i = 0; i < size; ++i) {
1272 Codec::encode(value, mData.get()[i]);
1277 template<
typename ValueType_,
typename Codec_>
1285 template<
typename ValueType_,
typename Codec_>
1291 if (!mIsUniform && !this->isCompressed()) {
1293 tbb::spin_mutex::scoped_lock lock(mMutex);
1295 this->doLoadUnsafe(
false);
1297 if (this->isCompressed())
return true;
1299 return this->compressUnsafe();
1306 template<
typename ValueType_,
typename Codec_>
1311 if (mIsUniform)
return false;
1315 const bool writeCompress = (mSerializationFlags & WRITEMEMCOMPRESS);
1316 const size_t inBytes = writeCompress ? mCompressedBytes : this->arrayMemUsage();
1320 const char* charBuffer =
reinterpret_cast<const char*
>(mData.get());
1323 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1324 mCompressedBytes = outBytes;
1333 template<
typename ValueType_,
typename Codec_>
1337 tbb::spin_mutex::scoped_lock lock(mMutex);
1339 const bool writeCompress = (mSerializationFlags & WRITEMEMCOMPRESS);
1341 if (writeCompress) {
1342 this->doLoadUnsafe(
false);
1346 if (this->isCompressed()) {
1347 this->doLoadUnsafe();
1348 const char* charBuffer =
reinterpret_cast<const char*
>(this->mData.get());
1352 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1353 mCompressedBytes = 0;
1362 template<
typename ValueType_,
typename Codec_>
1366 #if OPENVDB_ABI_VERSION_NUMBER >= 5 1369 return (mFlags & OUTOFCORE);
1374 template<
typename ValueType_,
typename Codec_>
1378 #if OPENVDB_ABI_VERSION_NUMBER >= 5 1381 if (b) mFlags =
static_cast<uint8_t
>(mFlags | OUTOFCORE);
1382 else mFlags =
static_cast<uint8_t
>(mFlags & ~OUTOFCORE);
1387 template<
typename ValueType_,
typename Codec_>
1389 TypedAttributeArray<ValueType_, Codec_>::doLoad()
const 1391 if (!(this->isOutOfCore()))
return;
1393 TypedAttributeArray<ValueType_, Codec_>*
self =
1394 const_cast<TypedAttributeArray<ValueType_, Codec_>*
>(
this);
1398 tbb::spin_mutex::scoped_lock lock(self->mMutex);
1399 this->doLoadUnsafe();
1403 template<
typename ValueType_,
typename Codec_>
1411 template<
typename ValueType_,
typename Codec_>
1415 this->readMetadata(is);
1416 this->readBuffers(is);
1420 template<
typename ValueType_,
typename Codec_>
1427 is.read(reinterpret_cast<char*>(&bytes),
sizeof(
Index64));
1428 bytes = bytes -
sizeof(
Int16) -
sizeof(
Index);
1430 uint8_t flags = uint8_t(0);
1431 is.read(reinterpret_cast<char*>(&flags),
sizeof(uint8_t));
1434 uint8_t serializationFlags = uint8_t(0);
1435 is.read(reinterpret_cast<char*>(&serializationFlags),
sizeof(uint8_t));
1436 mSerializationFlags = serializationFlags;
1439 is.read(reinterpret_cast<char*>(&size),
sizeof(
Index));
1443 if (mFlags >= 0x20) {
1448 if (mSerializationFlags >= 0x10) {
1454 mIsUniform = mSerializationFlags & WRITEUNIFORM;
1455 mCompressedBytes = bytes;
1459 if (mSerializationFlags & WRITESTRIDED) {
1461 is.read(reinterpret_cast<char*>(&stride),
sizeof(
Index));
1462 mStrideOrTotalSize = stride;
1465 mStrideOrTotalSize = 1;
1470 template<
typename ValueType_,
typename Codec_>
1474 if ((mSerializationFlags & WRITEPAGED)) {
1479 tbb::spin_mutex::scoped_lock lock(mMutex);
1483 uint8_t bloscCompressed(0);
1484 if (!mIsUniform) is.read(reinterpret_cast<char*>(&bloscCompressed),
sizeof(uint8_t));
1486 std::unique_ptr<char[]> buffer(
new char[mCompressedBytes]);
1487 is.read(buffer.get(), mCompressedBytes);
1491 mCompressedBytes =
Index64(0);
1493 else if (!(mSerializationFlags & WRITEMEMCOMPRESS)) {
1495 mCompressedBytes =
Index64(0);
1500 if (bloscCompressed == uint8_t(1)) {
1504 const size_t inBytes = this->dataSize() *
sizeof(
StorageType);
1506 if (newBuffer) buffer.reset(newBuffer.release());
1511 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1515 if (mIsUniform) mSerializationFlags &= uint8_t(~WRITEUNIFORM & ~WRITEMEMCOMPRESS & ~WRITEPAGED);
1516 else mSerializationFlags &= uint8_t(~WRITEUNIFORM & ~WRITEPAGED);
1520 template<
typename ValueType_,
typename Codec_>
1524 if (!(mSerializationFlags & WRITEPAGED)) {
1532 const bool delayLoad = (mappedFile.get() !=
nullptr);
1540 assert(mPageHandle);
1542 tbb::spin_mutex::scoped_lock lock(mMutex);
1546 this->setOutOfCore(delayLoad);
1547 is.
read(mPageHandle, mCompressedBytes, delayLoad);
1550 std::unique_ptr<char[]> buffer = mPageHandle->read();
1551 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1558 mCompressedBytes =
Index64(0);
1560 else if (!(mSerializationFlags & WRITEMEMCOMPRESS)) {
1561 mCompressedBytes =
Index64(0);
1566 if (mIsUniform) mSerializationFlags &= uint8_t(~WRITEUNIFORM & ~WRITEMEMCOMPRESS & ~WRITEPAGED);
1567 else mSerializationFlags &= uint8_t(~WRITEUNIFORM & ~WRITEPAGED);
1571 template<
typename ValueType_,
typename Codec_>
1575 this->write(os,
false);
1579 template<
typename ValueType_,
typename Codec_>
1583 this->writeMetadata(os, outputTransient,
false);
1584 this->writeBuffers(os, outputTransient);
1588 template<
typename ValueType_,
typename Codec_>
1592 if (!outputTransient && this->isTransient())
return;
1594 #if OPENVDB_ABI_VERSION_NUMBER >= 5 1595 uint8_t flags(mFlags);
1597 uint8_t flags(mFlags & uint8_t(~OUTOFCORE));
1599 uint8_t serializationFlags(0);
1601 Index stride(mStrideOrTotalSize);
1602 bool strideOfOne(this->stride() == 1);
1607 if (bloscCompression || this->isCompressed()) this->doLoad();
1609 size_t compressedBytes = 0;
1613 serializationFlags |= WRITESTRIDED;
1618 serializationFlags |= WRITEUNIFORM;
1619 if (bloscCompression && paged) serializationFlags |= WRITEPAGED;
1621 else if (bloscCompression && paged)
1623 serializationFlags |= WRITEPAGED;
1624 if (this->isCompressed()) {
1625 serializationFlags |= WRITEMEMCOMPRESS;
1626 const char* charBuffer =
reinterpret_cast<const char*
>(mData.get());
1630 else if (this->isCompressed())
1632 serializationFlags |= WRITEMEMCOMPRESS;
1633 compressedBytes = mCompressedBytes;
1635 else if (bloscCompression)
1637 const char* charBuffer =
reinterpret_cast<const char*
>(mData.get());
1638 const size_t inBytes = this->arrayMemUsage();
1644 bytes += (compressedBytes > 0) ? compressedBytes : this->arrayMemUsage();
1648 os.write(reinterpret_cast<const char*>(&bytes),
sizeof(
Index64));
1649 os.write(reinterpret_cast<const char*>(&flags),
sizeof(uint8_t));
1650 os.write(reinterpret_cast<const char*>(&serializationFlags),
sizeof(uint8_t));
1651 os.write(reinterpret_cast<const char*>(&size),
sizeof(
Index));
1654 if (!strideOfOne) os.write(reinterpret_cast<const char*>(&stride),
sizeof(
Index));
1658 template<
typename ValueType_,
typename Codec_>
1662 if (!outputTransient && this->isTransient())
return;
1666 if (this->isUniform()) {
1667 os.write(reinterpret_cast<const char*>(mData.get()),
sizeof(
StorageType));
1669 else if (this->isCompressed())
1671 uint8_t bloscCompressed(0);
1672 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1673 os.write(reinterpret_cast<const char*>(mData.get()), mCompressedBytes);
1677 std::unique_ptr<char[]> compressedBuffer;
1678 size_t compressedBytes = 0;
1679 const char* charBuffer =
reinterpret_cast<const char*
>(mData.get());
1680 const size_t inBytes = this->arrayMemUsage();
1682 if (compressedBuffer) {
1683 uint8_t bloscCompressed(1);
1684 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1685 os.write(reinterpret_cast<const char*>(compressedBuffer.get()), compressedBytes);
1688 uint8_t bloscCompressed(0);
1689 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1690 os.write(reinterpret_cast<const char*>(mData.get()), inBytes);
1695 uint8_t bloscCompressed(0);
1696 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1697 os.write(reinterpret_cast<const char*>(mData.get()), this->arrayMemUsage());
1702 template<
typename ValueType_,
typename Codec_>
1706 if (!outputTransient && this->isTransient())
return;
1710 if (!bloscCompression) {
1720 std::unique_ptr<char[]> uncompressedBuffer;
1721 if (this->isCompressed()) {
1724 const char* charBuffer =
reinterpret_cast<const char*
>(this->mData.get());
1727 buffer =
reinterpret_cast<const char*
>(uncompressedBuffer.get());
1730 buffer =
reinterpret_cast<const char*
>(mData.get());
1731 bytes = this->arrayMemUsage();
1734 os.
write(buffer, bytes);
1738 template<
typename ValueType_,
typename Codec_>
1742 if (!(this->isOutOfCore()))
return;
1748 assert(self->mPageHandle);
1750 std::unique_ptr<char[]> buffer =
self->mPageHandle->read();
1752 self->mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1754 self->mPageHandle.reset();
1758 if (self->mSerializationFlags & WRITEMEMCOMPRESS) {
1759 if (compression)
self->compressUnsafe();
1760 else self->mCompressedBytes = 0;
1765 #if OPENVDB_ABI_VERSION_NUMBER >= 5 1766 self->mOutOfCore =
false;
1768 self->mFlags &= uint8_t(~OUTOFCORE);
1770 self->mSerializationFlags &= uint8_t(~WRITEUNIFORM & ~WRITEMEMCOMPRESS & ~WRITEPAGED);
1774 template<
typename ValueType_,
typename Codec_>
1789 template<
typename ValueType_,
typename Codec_>
1794 if(!otherT)
return false;
1795 if(this->mSize != otherT->mSize ||
1796 this->mStrideOrTotalSize != otherT->mStrideOrTotalSize ||
1797 this->mIsUniform != otherT->mIsUniform ||
1798 *this->sTypeName != *otherT->sTypeName)
return false;
1803 const StorageType *target = this->mData.get(), *source = otherT->mData.get();
1804 if (!target && !source)
return true;
1805 if (!target || !source)
return false;
1806 Index n = this->mIsUniform ? 1 : mSize;
1815 template <
typename CodecType,
typename ValueType>
1836 template <
typename ValueType>
1844 return (*functor)(array, n);
1849 (*functor)(array, n, value);
1858 template <
typename ValueType,
typename CodecType>
1866 template <
typename ValueType,
typename CodecType>
1869 , mStrideOrTotalSize(array.hasConstantStride() ? array.stride() : 1)
1870 , mSize(array.hasConstantStride() ? array.size() : array.dataSize())
1871 , mCollapseOnDestruction(preserveCompression && array.isStreaming())
1873 if (!this->compatibleType<std::is_same<CodecType, UnknownCodec>::value>()) {
1886 if (preserveCompression && !array.
isStreaming()) {
1888 mLocalArray->decompress();
1889 mArray = mLocalArray.get();
1903 mGetter = typedAccessor->
mGetter;
1904 mSetter = typedAccessor->
mSetter;
1906 mFiller = typedAccessor->
mFiller;
1909 template <
typename ValueType,
typename CodecType>
1913 if (mCollapseOnDestruction)
const_cast<AttributeArray*
>(this->mArray)->collapse();
1916 template <
typename ValueType,
typename CodecType>
1917 template <
bool IsUnknownCodec>
1918 typename std::enable_if<IsUnknownCodec, bool>::type
1923 return mArray->hasValueType<ValueType>();
1926 template <
typename ValueType,
typename CodecType>
1927 template <
bool IsUnknownCodec>
1928 typename std::enable_if<!IsUnknownCodec, bool>::type
1929 AttributeHandle<ValueType, CodecType>::compatibleType()
const 1933 return mArray->isType<TypedAttributeArray<ValueType, CodecType>>();
1936 template <
typename ValueType,
typename CodecType>
1939 Index index = n * mStrideOrTotalSize + m;
1940 assert(index < (mSize * mStrideOrTotalSize));
1944 template <
typename ValueType,
typename CodecType>
1947 return this->get<std::is_same<CodecType, UnknownCodec>::value>(this->index(n, m));
1950 template <
typename ValueType,
typename CodecType>
1951 template <
bool IsUnknownCodec>
1952 typename std::enable_if<IsUnknownCodec, ValueType>::type
1957 return (*mGetter)(mArray, index);
1960 template <
typename ValueType,
typename CodecType>
1961 template <
bool IsUnknownCodec>
1962 typename std::enable_if<!IsUnknownCodec, ValueType>::type
1963 AttributeHandle<ValueType, CodecType>::get(
Index index)
const 1967 return TypedAttributeArray<ValueType, CodecType>::getUnsafe(mArray, index);
1970 template <
typename ValueType,
typename CodecType>
1976 template <
typename ValueType,
typename CodecType>
1986 template <
typename ValueType,
typename CodecType>
1994 template <
typename ValueType,
typename CodecType>
1998 if (expand) array.
expand();
2001 template <
typename ValueType,
typename CodecType>
2004 this->set<std::is_same<CodecType, UnknownCodec>::value>(this->index(n, 0), value);
2007 template <
typename ValueType,
typename CodecType>
2010 this->set<std::is_same<CodecType, UnknownCodec>::value>(this->index(n, m), value);
2013 template <
typename ValueType,
typename CodecType>
2019 template <
typename ValueType,
typename CodecType>
2025 template <
typename ValueType,
typename CodecType>
2031 template <
typename ValueType,
typename CodecType>
2034 this->mCollapser(const_cast<AttributeArray*>(this->mArray), uniformValue);
2037 template <
typename ValueType,
typename CodecType>
2040 this->mFiller(const_cast<AttributeArray*>(this->mArray), value);
2043 template <
typename ValueType,
typename CodecType>
2044 template <
bool IsUnknownCodec>
2045 typename std::enable_if<IsUnknownCodec, void>::type
2050 (*this->mSetter)(const_cast<AttributeArray*>(this->mArray), index, value);
2053 template <
typename ValueType,
typename CodecType>
2054 template <
bool IsUnknownCodec>
2055 typename std::enable_if<!IsUnknownCodec, void>::type
2056 AttributeWriteHandle<ValueType, CodecType>::set(
Index index,
const ValueType& value)
const 2060 TypedAttributeArray<ValueType, CodecType>::setUnsafe(const_cast<AttributeArray*>(this->mArray), index, value);
2068 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED T & y()
Definition: Vec3.h:111
Index stride() const
Definition: AttributeArray.h:666
bool isHidden() const
Return true if this attribute is hidden (e.g., from UI or iterators).
Definition: AttributeArray.h:218
std::ostream & getOutputStream()
Set and get the output stream.
Definition: StreamCompression.h:276
void write(std::ostream &os, bool outputTransient) const override
Definition: AttributeArray.h:1581
bool decompress() override
Uncompress the attribute array.
Definition: AttributeArray.h:1335
short Type
Definition: AttributeArray.h:341
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
void readPagedBuffers(compression::PagedInputStream &) override
Read attribute buffers from a paged stream.
Definition: AttributeArray.h:1522
Typed class for storing attribute data.
Definition: AttributeArray.h:441
void writePagedBuffers(compression::PagedOutputStream &os, bool outputTransient) const override
Definition: AttributeArray.h:1704
Definition: AttributeArray.h:399
Accessor to call unsafe get and set methods based on templated Codec and Value.
Definition: AttributeArray.h:1816
StorageType Type
Definition: AttributeArray.h:428
IntegerVectorT floatingPointToFixedPoint(const math::Vec3< FloatT > &v)
Definition: AttributeArray.h:95
ValuePtr mCollapser
Definition: AttributeArray.h:681
SharedPtr< MappedFile > Ptr
Definition: io.h:152
const AttributeArray * mArray
Definition: AttributeArray.h:677
Flag
Definition: AttributeArray.h:127
void writeBuffers(std::ostream &os, bool outputTransient) const override
Definition: AttributeArray.h:1660
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:1839
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:1818
uint64_t Index64
Definition: Types.h:59
void(*)(AttributeArray *array, const T &value) ValuePtr
Definition: AttributeArray.h:322
Definition: Exceptions.h:90
uint16_t Type
Definition: AttributeArray.h:349
std::shared_ptr< Handle > Ptr
Definition: AttributeArray.h:717
static TypedAttributeArray & cast(AttributeArray &attributeArray)
Cast an AttributeArray to TypedAttributeArray<T>
Definition: AttributeArray.h:988
Ptr(*)(Index, Index, bool) FactoryMethod
Definition: AttributeArray.h:145
ValueType_ ValueType
Definition: AttributeArray.h:447
A Paging wrapper to std::istream that is responsible for reading from a given input stream and creati...
Definition: StreamCompression.h:225
SetterPtr mSetter
Definition: AttributeArray.h:680
std::shared_ptr< TypedAttributeArray > Ptr
Definition: AttributeArray.h:444
void(*)(AttributeArray *array, const Index n, const T &value) SetterPtr
Definition: AttributeArray.h:321
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:488
compression::PageHandle::Ptr mPageHandle
used for out-of-core, paged reading
Definition: AttributeArray.h:305
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:108
ValuePtr mFiller
Definition: AttributeArray.h:330
AttributeArray::Ptr copy() const override
Return a copy of this attribute.
Definition: AttributeArray.h:1008
std::shared_ptr< AccessorBase > AccessorBasePtr
Definition: AttributeArray.h:124
SetterPtr mSetter
Definition: AttributeArray.h:328
OPENVDB_API bool bloscCanCompress()
Returns true if compression is available.
TypedAttributeArray(Index n=1, Index strideOrTotalSize=1, bool constantStride=true, const ValueType &uniformValue=zeroVal< ValueType >())
Default constructor, always constructs a uniform attribute.
Definition: AttributeArray.h:844
std::shared_ptr< PageHandle > Ptr
Definition: StreamCompression.h:197
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
Definition: AttributeArray.h:1840
Definition: AttributeArray.h:378
bool compact() override
Compact the existing array to become uniform if all values are identical.
Definition: AttributeArray.h:1215
void readBuffers(std::istream &) override
Read attribute buffers from a stream.
Definition: AttributeArray.h:1472
bool isStreaming() const
Return true if this attribute is in streaming mode.
Definition: AttributeArray.h:233
OPENVDB_API size_t bloscUncompressedSize(const char *buffer)
Retrieves the uncompressed size of buffer when uncompressed.
static const char * name()
Definition: AttributeArray.h:392
TypedAttributeArray & operator=(const TypedAttributeArray &)
Deep copy assignment operator.
Definition: AttributeArray.h:912
static const char * name()
Definition: AttributeArray.h:432
std::shared_ptr< const AttributeArray > ConstPtr
Definition: AttributeArray.h:143
T Type
Definition: AttributeArray.h:370
Definition: Compression.h:81
ValuePtr mFiller
Definition: AttributeArray.h:682
static ValueType decode(const ValueType &value)
Definition: AttributeArray.h:394
OPENVDB_API void bloscDecompress(char *uncompressedBuffer, const size_t expectedBytes, const size_t bufferBytes, const char *compressedBuffer)
Decompress into the supplied buffer. Will throw if decompression fails or uncompressed buffer has ins...
Index size() const
Definition: AttributeArray.h:667
void read(PageHandle::Ptr &pageHandle, std::streamsize n, bool delayed=true)
Takes a pageHandle and updates the referenced page with the current stream pointer position and if de...
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
Definition: AttributeArray.h:1819
ValuePtr mCollapser
Definition: AttributeArray.h:329
static Ptr create(const AttributeArray &array, const bool preserveCompression=true)
Definition: AttributeArray.h:1860
static ValueType encode(const ValueType &value)
Definition: AttributeArray.h:393
Accessor(GetterPtr getter, SetterPtr setter, ValuePtr collapser, ValuePtr filler)
Definition: AttributeArray.h:324
Codec_ Codec
Definition: AttributeArray.h:448
void readMetadata(std::istream &) override
Read attribute metadata from a stream.
Definition: AttributeArray.h:1422
bool operator!=(const AttributeArray &other) const
Definition: AttributeArray.h:275
std::unique_ptr< Handle > ScopedPtr
Definition: AttributeArray.h:718
Convenience wrappers to using Blosc and reading and writing of Paged data.
virtual AccessorBasePtr getAccessor() const =0
Obtain an Accessor that stores getter and setter functors.
OPENVDB_API void bloscCompress(char *compressedBuffer, size_t &compressedBytes, const size_t bufferBytes, const char *uncompressedBuffer, const size_t uncompressedBytes)
Compress into the supplied buffer.
AttributeArray::Ptr copyUncompressed() const override
Return an uncompressed copy of this attribute (will just return a copy if not compressed).
Definition: AttributeArray.h:1016
Accessor base class for AttributeArray storage where type is not available.
Definition: AttributeArray.h:313
AttributeHandle(const AttributeArray &array, const bool preserveCompression=true)
Definition: AttributeArray.h:1867
static ValueType encode(const ValueType &value)
Definition: AttributeArray.h:402
const NamePair & type() const override
Return the name of this attribute's type.
Definition: AttributeArray.h:485
typename Codec::template Storage< ValueType >::Type StorageType
Definition: AttributeArray.h:449
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:142
bool hasValueType() const
Return true if this attribute has a value type the same as the template parameter.
Definition: AttributeArray.h:190
bool compress() override
Compress the attribute array.
Definition: AttributeArray.h:1287
GetterPtr mGetter
Definition: AttributeArray.h:679
Index32 Index
Definition: Types.h:60
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
bool hasConstantStride() const
Definition: AttributeArray.h:1977
ValueType get(Index n) const
Return the value at index n.
Definition: AttributeArray.h:1086
Index size() const override
Return the number of elements in this array.
Definition: AttributeArray.h:495
OPENVDB_API SharedPtr< MappedFile > getMappedFilePtr(std::ios_base &)
Return a shared pointer to the memory-mapped file with which the given stream is associated, or a null pointer if the stream is not associated with a memory-mapped file.
Base class for storing attribute data.
Definition: AttributeArray.h:118
Definition: Exceptions.h:91
A Paging wrapper to std::ostream that is responsible for writing from a given output stream at interv...
Definition: StreamCompression.h:262
void read(std::istream &) override
Read attribute data from a stream.
Definition: AttributeArray.h:1413
uint8_t flags() const
Retrieve the attribute array flags.
Definition: AttributeArray.h:239
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form 'someVar << "some text" << ...'.
Definition: logging.h:279
uint8_t mSerializationFlags
Definition: AttributeArray.h:298
uint16_t StorageType
Definition: AttributeArray.h:425
bool isCompressed() const
Return true if this array is compressed.
Definition: AttributeArray.h:206
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:110
std::shared_ptr< Handle > Ptr
Definition: AttributeArray.h:648
uint8_t mFlags
Definition: AttributeArray.h:297
static void registerType(const NamePair &type, FactoryMethod)
Register a attribute type along with a factory function.
Definition: Exceptions.h:39
bool isUniform() const
Definition: AttributeArray.h:1971
void set(Index n, const ValueType &value)
Set value at the given index n.
Definition: AttributeArray.h:1140
typename attribute_traits::TruncateTrait< T >::Type Type
Definition: AttributeArray.h:381
Definition: AttributeArray.h:122
static Ptr create(Index n, Index strideOrTotalSize=1, bool constantStride=true)
Return a new attribute array of the given length n and stride with uniform value zero.
Definition: AttributeArray.h:981
void setUnsafe(Index n, const ValueType &value)
Set value at the given index n (assumes uncompressed and in-core)
Definition: AttributeArray.h:1124
void(*)(AttributeArray *array, const Index n, const StringIndexType &value) SetterPtr
Definition: AttributeArray.h:653
GetterPtr mGetter
Definition: AttributeArray.h:327
static const char * name()
Definition: AttributeArray.h:385
size_t mCompressedBytes
Definition: AttributeArray.h:296
SerializationFlag
Definition: AttributeArray.h:135
void loadData() const override
Ensures all data is in-core.
Definition: AttributeArray.h:1405
typename attribute_traits::UIntTypeTrait< OneByte, T >::Type Type
Definition: AttributeArray.h:411
std::string Name
Definition: Name.h:44
static void registerType()
Register this attribute type along with a factory function.
Definition: AttributeArray.h:965
half Type
Definition: AttributeArray.h:340
T(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:320
Definition: AttributeArray.h:367
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:186
static ValueType decode(const ValueType &value)
Definition: AttributeArray.h:403
StringIndexType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:652
std::istream & getInputStream()
Definition: StreamCompression.h:239
static const char * name()
Definition: AttributeArray.h:416
bool isTransient() const
Return true if this attribute is not serialized during stream output.
Definition: AttributeArray.h:225
PagedOutputStream & write(const char *str, std::streamsize n)
Writes the given.
AccessorBasePtr getAccessor() const override
Obtain an Accessor that stores getter and setter functors.
Definition: AttributeArray.h:1776
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: AttributeArray.h:1364
bool hasConstantStride() const
Return true if this attribute has a constant stride.
Definition: AttributeArray.h:236
int16_t Int16
Definition: Types.h:61
bool sizeOnly() const
Definition: StreamCompression.h:273
virtual ~TypedAttributeArray()
Definition: AttributeArray.h:465
void setConstantStride(bool state)
Specify whether this attribute has a constant stride or not.
Definition: AttributeArray.h:408
Definition: AttributeArray.h:339
void fill(const ValueType &value)
Fill the existing array with the given value.
Definition: AttributeArray.h:1262
Definition: AttributeArray.h:644
bool isUniform() const override
Return true if this array is stored as a single uniform value.
Definition: AttributeArray.h:539
OPENVDB_API uint32_t getDataCompression(std::ios_base &)
Return a bitwise OR of compression option flags (COMPRESS_ZIP, COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data is compressed or output data should be compressed.
Definition: AttributeArray.h:381
Definition: Exceptions.h:83
Index dataSize() const override
Return the size of the data in this array.
Definition: AttributeArray.h:502
static bool isRegistered()
Return true if this attribute type is registered.
Definition: AttributeArray.h:957
void expand(bool fill=true) override
Replace the single value storage with an array of length size().
Definition: AttributeArray.h:1192
static const char * name()
Definition: AttributeArray.h:374
Definition: AttributeArray.h:347
Definition: AttributeArray.h:423
OPENVDB_API size_t bloscCompressedSize(const char *buffer, const size_t uncompressedBytes)
Convenience wrapper to retrieve the compressed size of buffer when compressed.
virtual void expand(bool fill=true)=0
If this array is uniform, replace it with an array of length size().
void writeMetadata(std::ostream &os, bool outputTransient, bool paged) const override
Definition: AttributeArray.h:1590
virtual AttributeArray::Ptr copyUncompressed() const =0
Return an uncompressed copy of this attribute (will return a copy if not compressed).
Definition: Exceptions.h:84
Definition: AttributeArray.h:390
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
std::unique_ptr< Handle > UniquePtr
Definition: AttributeArray.h:649
static void unregisterType()
Remove this attribute type from the registry.
Definition: AttributeArray.h:973
Write-able version of AttributeHandle.
Definition: AttributeArray.h:713
void collapse() override
Replace the existing array with a uniform zero value.
Definition: AttributeArray.h:1232
uint8_t Type
Definition: AttributeArray.h:348
static void unregisterType(const NamePair &type)
Remove a attribute type from the registry.
static bool isRegistered(const NamePair &type)
Return true if the given attribute type name is registered.
Definition: AttributeArray.h:364
ValueType getUnsafe(Index n) const
Return the value at index n (assumes uncompressed and in-core)
Definition: AttributeArray.h:1072
static const NamePair & attributeType()
Return the name of this attribute's type (includes codec)
Definition: AttributeArray.h:945
FloatVectorT fixedPointToFloatingPoint(const math::Vec3< IntegerT > &v)
Definition: AttributeArray.h:105
static const char * name()
Definition: AttributeArray.h:401
Definition: AttributeArray.h:428
bool sizeOnly() const
Definition: StreamCompression.h:236
size_t memUsage() const override
Return the number of bytes of memory used by this attribute.
Definition: AttributeArray.h:1064
void(*)(AttributeArray *array, const StringIndexType &value) ValuePtr
Definition: AttributeArray.h:654
Index stride() const override
Definition: AttributeArray.h:499
PageHandle::Ptr createHandle(std::streamsize n)
Creates a PageHandle to access the next.
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:65
T & z()
Definition: Vec3.h:112
Definition: AttributeArray.h:411
Definition: AttributeArray.h:370