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,
139 WRITEMEMCOMPRESS = 0x4,
147 tbb::spin_mutex::scoped_lock lock;
152 using Ptr = std::shared_ptr<AttributeArray>;
153 using ConstPtr = std::shared_ptr<const AttributeArray>;
159 #if OPENVDB_ABI_VERSION_NUMBER >= 5 168 if (mFlags & PARTIALREAD) mCompressedBytes = 0;
170 #if OPENVDB_ABI_VERSION_NUMBER >= 6 172 : mIsUniform(rhs.mIsUniform)
174 , mUsePagedRead(rhs.mUsePagedRead)
175 , mOutOfCore(rhs.mOutOfCore)
185 if (mFlags & PARTIALREAD) mCompressedBytes = 0;
192 else mPageHandle.reset();
212 virtual Index size()
const = 0;
216 virtual Index stride()
const = 0;
220 virtual Index dataSize()
const = 0;
222 #if OPENVDB_ABI_VERSION_NUMBER >= 6 223 virtual Name valueType()
const = 0;
227 virtual Name codecType()
const = 0;
231 virtual Index valueTypeSize()
const = 0;
235 virtual Index storageTypeSize()
const = 0;
238 virtual bool valueTypeIsFloatingPoint()
const = 0;
241 virtual bool valueTypeIsClass()
const = 0;
244 virtual bool valueTypeIsVector()
const = 0;
247 virtual bool valueTypeIsQuaternion()
const = 0;
250 virtual bool valueTypeIsMatrix()
const = 0;
254 virtual size_t memUsage()
const = 0;
259 static Ptr create(
const NamePair& type, Index length, Index stride = 1,
267 virtual const NamePair& type()
const = 0;
269 template<
typename AttributeArrayType>
270 bool isType()
const {
return this->type() == AttributeArrayType::attributeType(); }
273 template<
typename ValueType>
274 bool hasValueType()
const {
return this->type().first == typeNameAsString<ValueType>(); }
278 #if OPENVDB_ABI_VERSION_NUMBER >= 6 284 virtual void set(
const Index n,
const AttributeArray& sourceArray,
const Index sourceIndex) = 0;
286 #if OPENVDB_ABI_VERSION_NUMBER >= 6 287 template<
typename IterT>
311 void copyValuesUnsafe(
const AttributeArray& sourceArray,
const IterT& iter);
315 template<
typename IterT>
316 void copyValues(
const AttributeArray& sourceArray,
const IterT& iter,
bool compact =
true);
320 virtual bool isUniform()
const = 0;
323 virtual void expand(
bool fill =
true) = 0;
325 virtual void collapse() = 0;
327 virtual bool compact() = 0;
337 virtual bool compress() = 0;
343 virtual bool decompress() = 0;
349 void setHidden(
bool state);
351 bool isHidden()
const {
return bool(mFlags & HIDDEN); }
356 void setTransient(
bool state);
364 void setStreaming(
bool state);
372 uint8_t
flags()
const {
return mFlags; }
375 virtual void read(std::istream&) = 0;
378 virtual void write(std::ostream&,
bool outputTransient)
const = 0;
380 virtual void write(std::ostream&)
const = 0;
383 virtual void readMetadata(std::istream&) = 0;
387 virtual void writeMetadata(std::ostream&,
bool outputTransient,
bool paged)
const = 0;
390 virtual void readBuffers(std::istream&) = 0;
393 virtual void writeBuffers(std::ostream&,
bool outputTransient)
const = 0;
402 virtual void loadData()
const = 0;
404 #if OPENVDB_ABI_VERSION_NUMBER >= 6 405 virtual bool isDataLoaded()
const = 0;
416 friend class ::TestAttributeArray;
422 #if OPENVDB_ABI_VERSION_NUMBER >= 6 423 virtual char* dataAsByteArray() = 0;
425 virtual const char* dataAsByteArray()
const = 0;
428 template <
typename IterT>
429 void doCopyValues(
const AttributeArray& sourceArray,
const IterT& iter,
430 bool rangeChecking =
true);
435 void setConstantStride(
bool state);
444 static void unregisterType(
const NamePair& type,
447 #if OPENVDB_ABI_VERSION_NUMBER < 6 449 size_t mCompressedBytes = 0;
451 uint8_t mUsePagedRead = 0;
452 #if OPENVDB_ABI_VERSION_NUMBER >= 5 453 tbb::atomic<Index32> mOutOfCore;
457 #else // #if OPENVDB_ABI_VERSION_NUMBER < 6 459 bool mIsUniform =
true;
462 uint8_t mUsePagedRead = 0;
482 template <
typename T>
490 mGetter(getter), mSetter(setter), mCollapser(collapser), mFiller(filler) { }
502 namespace attribute_traits
534 template <
typename T>
537 template<
typename ValueType>
static void decode(
const ValueType&, ValueType&);
538 template<
typename ValueType>
static void encode(
const ValueType&, ValueType&);
539 static const char*
name() {
return "null"; }
545 template <
typename T>
548 template<
typename StorageType,
typename ValueType>
static void decode(
const StorageType&, ValueType&);
549 template<
typename StorageType,
typename ValueType>
static void encode(
const ValueType&, StorageType&);
550 static const char*
name() {
return "trnc"; }
557 static const char*
name() {
return "fxpt"; }
558 template <
typename ValueType>
static ValueType
encode(
const ValueType& value) {
return value + ValueType(0.5); }
559 template <
typename ValueType>
static ValueType
decode(
const ValueType& value) {
return value - ValueType(0.5); }
566 static const char*
name() {
return "ufxpt"; }
567 template <
typename ValueType>
static ValueType
encode(
const ValueType& value) {
return value; }
568 template <
typename ValueType>
static ValueType
decode(
const ValueType& value) {
return value; }
572 template <
bool OneByte,
typename Range=PositionRange>
575 template <
typename T>
578 template<
typename StorageType,
typename ValueType>
static void decode(
const StorageType&, ValueType&);
579 template<
typename StorageType,
typename ValueType>
static void encode(
const ValueType&, StorageType&);
582 static const std::string
Name = std::string(Range::name()) + (OneByte ?
"8" :
"16");
592 template <
typename T>
597 static const char*
name() {
return "uvec"; }
606 template<
typename ValueType_,
typename Codec_ = NullCodec>
607 #if OPENVDB_ABI_VERSION_NUMBER >= 6 // for ABI=6, class is final to allow for de-virtualization 614 using Ptr = std::shared_ptr<TypedAttributeArray>;
615 using ConstPtr = std::shared_ptr<const TypedAttributeArray>;
619 using StorageType =
typename Codec::template Storage<ValueType>::Type;
625 const ValueType& uniformValue = zeroVal<ValueType>());
648 static Ptr create(
Index n,
Index strideOrTotalSize = 1,
bool constantStride =
true);
657 static const NamePair& attributeType();
662 static bool isRegistered();
664 static void registerType();
666 static void unregisterType();
673 Index stride()
const override {
return hasConstantStride() ? mStrideOrTotalSize : 0; }
677 return hasConstantStride() ? mSize * mStrideOrTotalSize : mStrideOrTotalSize;
680 #if OPENVDB_ABI_VERSION_NUMBER >= 6 681 Name valueType()
const override {
return typeNameAsString<ValueType>(); }
695 bool valueTypeIsFloatingPoint()
const override;
698 bool valueTypeIsClass()
const override;
701 bool valueTypeIsVector()
const override;
704 bool valueTypeIsQuaternion()
const override;
707 bool valueTypeIsMatrix()
const override;
711 size_t memUsage()
const override;
718 template<
typename T>
void getUnsafe(
Index n, T& value)
const;
720 template<
typename T>
void get(
Index n, T& value)
const;
731 template<
typename T>
void setUnsafe(
Index n,
const T& value);
733 template<
typename T>
void set(
Index n,
const T& value);
740 #if OPENVDB_ABI_VERSION_NUMBER >= 6 750 void expand(
bool fill =
true)
override;
752 void collapse()
override;
754 bool compact()
override;
757 void collapse(
const ValueType& uniformValue);
773 void read(std::istream&)
override;
777 void write(std::ostream& os,
bool outputTransient)
const override;
779 void write(std::ostream&)
const override;
782 void readMetadata(std::istream&)
override;
787 void writeMetadata(std::ostream& os,
bool outputTransient,
bool paged)
const override;
790 void readBuffers(std::istream&)
override;
794 void writeBuffers(std::ostream& os,
bool outputTransient)
const override;
804 inline bool isOutOfCore()
const;
807 void loadData()
const override;
809 #if OPENVDB_ABI_VERSION_NUMBER >= 6 810 bool isDataLoaded()
const override;
822 inline bool validData()
const {
return !(isOutOfCore() || (flags() & PARTIALREAD)); }
825 friend class ::TestAttributeArray;
828 inline void doLoad()
const;
831 inline void doLoadUnsafe(
const bool compression =
true)
const;
833 inline bool compressUnsafe();
836 inline void setOutOfCore(
const bool);
841 #if OPENVDB_ABI_VERSION_NUMBER >= 6 842 char* dataAsByteArray()
override;
844 const char* dataAsByteArray()
const override;
847 size_t arrayMemUsage()
const;
853 return TypedAttributeArray::create(n, strideOrTotalSize, constantStride);
856 static tbb::atomic<const NamePair*> sTypeName;
857 std::unique_ptr<StorageType[]> mData;
859 Index mStrideOrTotalSize;
860 #if OPENVDB_ABI_VERSION_NUMBER < 6 // as of ABI=6, this data lives in the base class to reduce memory 861 bool mIsUniform =
true;
862 mutable tbb::spin_mutex mMutex;
872 template <
typename ValueType,
typename CodecType = UnknownCodec>
877 using Ptr = std::shared_ptr<Handle>;
886 static Ptr create(
const AttributeArray& array,
const bool collapseOnDestruction =
true);
898 bool isUniform()
const;
899 bool hasConstantStride()
const;
916 friend class ::TestAttributeArray;
918 template <
bool IsUnknownCodec>
919 typename std::enable_if<IsUnknownCodec, bool>::type compatibleType()
const;
921 template <
bool IsUnknownCodec>
922 typename std::enable_if<!IsUnknownCodec, bool>::type compatibleType()
const;
924 template <
bool IsUnknownCodec>
925 typename std::enable_if<IsUnknownCodec, ValueType>::type
get(
Index index)
const;
927 template <
bool IsUnknownCodec>
928 typename std::enable_if<!IsUnknownCodec, ValueType>::type
get(
Index index)
const;
933 Index mStrideOrTotalSize;
935 bool mCollapseOnDestruction;
943 template <
typename ValueType,
typename CodecType = UnknownCodec>
948 using Ptr = std::shared_ptr<Handle>;
959 void expand(
bool fill =
true);
963 void collapse(
const ValueType& uniformValue);
970 void fill(
const ValueType& value);
972 void set(
Index n,
const ValueType& value);
973 void set(
Index n,
Index m,
const ValueType& value);
978 friend class ::TestAttributeArray;
980 template <
bool IsUnknownCodec>
981 typename std::enable_if<IsUnknownCodec, void>::type
set(
Index index,
const ValueType& value)
const;
983 template <
bool IsUnknownCodec>
984 typename std::enable_if<!IsUnknownCodec, void>::type
set(
Index index,
const ValueType& value)
const;
994 template<
typename ValueType>
996 NullCodec::decode(
const ValueType& data, ValueType& val)
1002 template<
typename ValueType>
1004 NullCodec::encode(
const ValueType& val, ValueType& data)
1010 template<
typename StorageType,
typename ValueType>
1012 TruncateCodec::decode(
const StorageType& data, ValueType& val)
1014 val =
static_cast<ValueType
>(data);
1018 template<
typename StorageType,
typename ValueType>
1020 TruncateCodec::encode(
const ValueType& val, StorageType& data)
1022 data =
static_cast<StorageType
>(val);
1026 template <
bool OneByte,
typename Range>
1027 template<
typename StorageType,
typename ValueType>
1031 val = fixedPointToFloatingPoint<ValueType>(data);
1035 val = Range::template decode<ValueType>(val);
1039 template <
bool OneByte,
typename Range>
1040 template<
typename StorageType,
typename ValueType>
1046 const ValueType newVal = Range::template encode<ValueType>(val);
1048 data = floatingPointToFixedPoint<StorageType>(newVal);
1052 template<
typename T>
1056 val = math::QuantizedUnitVec::unpack(data);
1060 template<
typename T>
1064 data = math::QuantizedUnitVec::pack(val);
1072 #if OPENVDB_ABI_VERSION_NUMBER >= 6 1074 template <
typename IterT>
1075 void AttributeArray::doCopyValues(
const AttributeArray& sourceArray,
const IterT& iter,
1081 assert(sourceArray.
isDataLoaded() && this->isDataLoaded());
1083 assert(this->storageTypeSize()*this->stride() ==
1087 const char*
const sourceBuffer = sourceArray.dataAsByteArray();
1088 char*
const targetBuffer = this->dataAsByteArray();
1089 assert(sourceBuffer && targetBuffer);
1091 if (rangeChecking && this->isUniform()) {
1095 const bool sourceIsUniform = sourceArray.
isUniform();
1097 const Index sourceDataSize = rangeChecking ? sourceArray.
dataSize() : 0;
1098 const Index targetDataSize = rangeChecking ? this->dataSize() : 0;
1100 for (IterT it(iter); it; ++it) {
1101 const Index sourceIndex = sourceIsUniform ? 0 : it.sourceIndex();
1102 const Index targetIndex = it.targetIndex();
1104 if (rangeChecking) {
1105 if (sourceIndex >= sourceDataSize) {
1107 "Cannot copy array data as source index exceeds size of source array.");
1109 if (targetIndex >= targetDataSize) {
1111 "Cannot copy array data as target index exceeds size of target array.");
1115 assert(sourceIndex < sourceArray.
dataSize());
1116 assert(targetIndex < this->dataSize());
1117 if (this->isUniform()) assert(targetIndex ==
Index(0));
1120 const size_t targetOffset(targetIndex * bytes);
1121 const size_t sourceOffset(sourceIndex * bytes);
1123 std::memcpy(targetBuffer + targetOffset, sourceBuffer + sourceOffset, bytes);
1127 template <
typename IterT>
1128 void AttributeArray::copyValuesUnsafe(
const AttributeArray& sourceArray,
const IterT& iter)
1130 this->doCopyValues(sourceArray, iter,
false);
1133 template <
typename IterT>
1134 void AttributeArray::copyValues(
const AttributeArray& sourceArray,
const IterT& iter,
1138 if (bytes != this->storageTypeSize()) {
1155 this->doCopyValues(sourceArray, iter,
true);
1169 template<
typename ValueType_,
typename Codec_>
1173 template<
typename ValueType_,
typename Codec_>
1179 , mStrideOrTotalSize(strideOrTotalSize)
1181 if (constantStride) {
1183 if (strideOrTotalSize == 0) {
1185 "stride to be at least one.")
1190 if (mStrideOrTotalSize < n) {
1192 "a total size of at least the number of elements in the array.")
1196 mStrideOrTotalSize =
std::max(
Index(1), mStrideOrTotalSize);
1197 Codec::encode(uniformValue, this->
data()[0]);
1201 template<
typename ValueType_,
typename Codec_>
1205 , mStrideOrTotalSize(rhs.mStrideOrTotalSize)
1206 #if OPENVDB_ABI_VERSION_NUMBER < 6
1212 std::memcpy(this->
data(), rhs.
data(), this->arrayMemUsage());
1217 template<
typename ValueType_,
typename Codec_>
1223 tbb::spin_mutex::scoped_lock lock(
mMutex);
1224 tbb::spin_mutex::scoped_lock rhsLock(rhs.
mMutex);
1231 mStrideOrTotalSize = rhs.mStrideOrTotalSize;
1236 std::memcpy(this->newDataAsByteArray(), rhs.newDataAsByteArray(), this->arrayMemUsage());
1242 template<
typename ValueType_,
typename Codec_>
1246 if (sTypeName ==
nullptr) {
1248 if (sTypeName.compare_and_swap(s,
nullptr) !=
nullptr)
delete s;
1254 template<
typename ValueType_,
typename Codec_>
1262 template<
typename ValueType_,
typename Codec_>
1270 template<
typename ValueType_,
typename Codec_>
1278 template<
typename ValueType_,
typename Codec_>
1285 template<
typename ValueType_,
typename Codec_>
1295 template<
typename ValueType_,
typename Codec_>
1305 template<
typename ValueType_,
typename Codec_>
1309 tbb::spin_mutex::scoped_lock lock(
mMutex);
1314 template<
typename ValueType_,
typename Codec_>
1318 tbb::spin_mutex::scoped_lock lock(
mMutex);
1323 template<
typename ValueType_,
typename Codec_>
1333 template<
typename ValueType_,
typename Codec_>
1349 template<
typename ValueType_,
typename Codec_>
1355 this->setOutOfCore(
false);
1358 if (mData) mData.reset();
1362 #if OPENVDB_ABI_VERSION_NUMBER >= 6 1363 template<
typename ValueType_,
typename Codec_>
1369 if (std::is_same<ValueType, Quats>::value ||
1370 std::is_same<ValueType, Quatd>::value ||
1371 std::is_same<ValueType, Mat3s>::value ||
1372 std::is_same<ValueType, Mat3d>::value ||
1373 std::is_same<ValueType, Mat4s>::value ||
1374 std::is_same<ValueType, Mat4d>::value)
return true;
1379 return std::is_floating_point<ElementT>::value || std::is_same<half, ElementT>::value;
1383 template<
typename ValueType_,
typename Codec_>
1388 return std::is_class<ValueType>::value && !std::is_same<half, ValueType>::value;
1392 template<
typename ValueType_,
typename Codec_>
1400 template<
typename ValueType_,
typename Codec_>
1405 return !this->
valueType().compare(0, 4,
"quat");
1409 template<
typename ValueType_,
typename Codec_>
1414 return !this->
valueType().compare(0, 3,
"mat");
1419 template<
typename ValueType_,
typename Codec_>
1423 return sizeof(*this) + (bool(mData) ? this->arrayMemUsage() : 0);
1427 template<
typename ValueType_,
typename Codec_>
1439 template<
typename ValueType_,
typename Codec_>
1450 template<
typename ValueType_,
typename Codec_>
1451 template<
typename T>
1455 val =
static_cast<T
>(this->
getUnsafe(n));
1459 template<
typename ValueType_,
typename Codec_>
1460 template<
typename T>
1464 val =
static_cast<T
>(this->
get(n));
1468 template<
typename ValueType_,
typename Codec_>
1476 template<
typename ValueType_,
typename Codec_>
1491 template<
typename ValueType_,
typename Codec_>
1503 template<
typename ValueType_,
typename Codec_>
1504 template<
typename T>
1508 this->
setUnsafe(n, static_cast<ValueType>(val));
1512 template<
typename ValueType_,
typename Codec_>
1513 template<
typename T>
1517 this->
set(n,
static_cast<ValueType>(val));
1521 template<
typename ValueType_,
typename Codec_>
1529 template<
typename ValueType_,
typename Codec_>
1536 sourceTypedArray.
get(sourceIndex, sourceValue);
1538 this->
set(n, sourceValue);
1542 template<
typename ValueType_,
typename Codec_>
1551 tbb::spin_mutex::scoped_lock lock(
mMutex);
1563 template<
typename ValueType_,
typename Codec_>
1570 const ValueType_ val = this->
get(0);
1580 template<
typename ValueType_,
typename Codec_>
1584 this->
collapse(zeroVal<ValueType>());
1588 template<
typename ValueType_,
typename Codec_>
1593 tbb::spin_mutex::scoped_lock lock(
mMutex);
1598 Codec::encode(uniformValue, this->
data()[0]);
1602 template<
typename ValueType_,
typename Codec_>
1610 template<
typename ValueType_,
typename Codec_>
1615 tbb::spin_mutex::scoped_lock lock(
mMutex);
1622 Codec::encode(value, this->
data()[i]);
1627 template<
typename ValueType_,
typename Codec_>
1635 template<
typename ValueType_,
typename Codec_>
1643 template<
typename ValueType_,
typename Codec_>
1651 template<
typename ValueType_,
typename Codec_>
1659 template<
typename ValueType_,
typename Codec_>
1663 #if OPENVDB_ABI_VERSION_NUMBER >= 5 1671 template<
typename ValueType_,
typename Codec_>
1675 #if OPENVDB_ABI_VERSION_NUMBER >= 5 1684 template<
typename ValueType_,
typename Codec_>
1695 tbb::spin_mutex::scoped_lock lock(self->mMutex);
1696 this->doLoadUnsafe();
1700 template<
typename ValueType_,
typename Codec_>
1708 #if OPENVDB_ABI_VERSION_NUMBER >= 6 1709 template<
typename ValueType_,
typename Codec_>
1718 template<
typename ValueType_,
typename Codec_>
1727 template<
typename ValueType_,
typename Codec_>
1734 is.read(reinterpret_cast<char*>(&bytes),
sizeof(
Index64));
1735 bytes = bytes -
sizeof(
Int16) -
sizeof(
Index);
1737 uint8_t
flags = uint8_t(0);
1738 is.read(reinterpret_cast<char*>(&flags),
sizeof(uint8_t));
1741 uint8_t serializationFlags = uint8_t(0);
1742 is.read(reinterpret_cast<char*>(&serializationFlags),
sizeof(uint8_t));
1745 is.read(reinterpret_cast<char*>(&size),
sizeof(
Index));
1754 if (serializationFlags >= 0x10) {
1769 is.read(reinterpret_cast<char*>(&stride),
sizeof(
Index));
1770 mStrideOrTotalSize =
stride;
1773 mStrideOrTotalSize = 1;
1778 template<
typename ValueType_,
typename Codec_>
1787 tbb::spin_mutex::scoped_lock lock(
mMutex);
1791 uint8_t bloscCompressed(0);
1792 if (!
mIsUniform) is.read(reinterpret_cast<char*>(&bloscCompressed),
sizeof(uint8_t));
1797 mCompressedBytes = 0;
1802 if (bloscCompressed == uint8_t(1)) {
1808 if (newBuffer) buffer.reset(newBuffer.release());
1813 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1817 template<
typename ValueType_,
typename Codec_>
1829 const bool delayLoad = (mappedFile.get() !=
nullptr);
1843 tbb::spin_mutex::scoped_lock lock(
mMutex);
1847 this->setOutOfCore(delayLoad);
1851 std::unique_ptr<char[]> buffer =
mPageHandle->read();
1852 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1861 template<
typename ValueType_,
typename Codec_>
1865 this->
write(os,
false);
1869 template<
typename ValueType_,
typename Codec_>
1878 template<
typename ValueType_,
typename Codec_>
1882 if (!outputTransient && this->
isTransient())
return;
1888 #if OPENVDB_ABI_VERSION_NUMBER >= 5 1893 uint8_t serializationFlags(0);
1896 bool strideOfOne(this->
stride() == 1);
1901 if (bloscCompression) this->doLoad();
1903 size_t compressedBytes = 0;
1913 if (bloscCompression && paged) serializationFlags |=
WRITEPAGED;
1915 else if (bloscCompression)
1919 const char* charBuffer =
reinterpret_cast<const char*
>(this->
data());
1920 const size_t inBytes = this->arrayMemUsage();
1927 bytes += (compressedBytes > 0) ? compressedBytes : this->arrayMemUsage();
1931 os.write(reinterpret_cast<const char*>(&bytes),
sizeof(
Index64));
1932 os.write(reinterpret_cast<const char*>(&flags),
sizeof(uint8_t));
1933 os.write(reinterpret_cast<const char*>(&serializationFlags),
sizeof(uint8_t));
1934 os.write(reinterpret_cast<const char*>(&size),
sizeof(
Index));
1937 if (!strideOfOne) os.write(reinterpret_cast<const char*>(&stride),
sizeof(
Index));
1941 template<
typename ValueType_,
typename Codec_>
1945 if (!outputTransient && this->
isTransient())
return;
1954 os.write(reinterpret_cast<const char*>(this->
data()),
sizeof(
StorageType));
1958 std::unique_ptr<char[]> compressedBuffer;
1959 size_t compressedBytes = 0;
1960 const char* charBuffer =
reinterpret_cast<const char*
>(this->
data());
1961 const size_t inBytes = this->arrayMemUsage();
1963 if (compressedBuffer) {
1964 uint8_t bloscCompressed(1);
1965 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1966 os.write(reinterpret_cast<const char*>(compressedBuffer.get()), compressedBytes);
1969 uint8_t bloscCompressed(0);
1970 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1971 os.write(reinterpret_cast<const char*>(this->
data()), inBytes);
1976 uint8_t bloscCompressed(0);
1977 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1978 os.write(reinterpret_cast<const char*>(this->
data()), this->arrayMemUsage());
1983 template<
typename ValueType_,
typename Codec_>
1987 if (!outputTransient && this->
isTransient())
return;
1991 if (!bloscCompression) {
2002 os.
write(reinterpret_cast<const char*>(this->
data()), this->arrayMemUsage());
2006 template<
typename ValueType_,
typename Codec_>
2016 assert(self->mPageHandle);
2019 std::unique_ptr<char[]> buffer =
self->mPageHandle->read();
2021 self->mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
2023 self->mPageHandle.reset();
2027 #if OPENVDB_ABI_VERSION_NUMBER >= 5 2028 self->mOutOfCore =
false;
2035 template<
typename ValueType_,
typename Codec_>
2050 template<
typename ValueType_,
typename Codec_>
2055 if(!otherT)
return false;
2056 if(this->mSize != otherT->mSize ||
2057 this->mStrideOrTotalSize != otherT->mStrideOrTotalSize ||
2059 this->attributeType() != this->
attributeType())
return false;
2065 if (!target && !source)
return true;
2066 if (!target || !source)
return false;
2073 #if OPENVDB_ABI_VERSION_NUMBER >= 6 2074 template<
typename ValueType_,
typename Codec_>
2078 return reinterpret_cast<char*
>(this->
data());
2082 template<
typename ValueType_,
typename Codec_>
2086 return reinterpret_cast<const char*
>(this->
data());
2095 template <
typename CodecType,
typename ValueType>
2116 template <
typename ValueType>
2124 return (*functor)(array, n);
2129 (*functor)(array, n, value);
2138 template <
typename ValueType,
typename CodecType>
2146 template <
typename ValueType,
typename CodecType>
2151 , mCollapseOnDestruction(collapseOnDestruction && array.
isStreaming())
2153 if (!this->compatibleType<std::is_same<CodecType, UnknownCodec>::value>()) {
2168 mGetter = typedAccessor->
mGetter;
2169 mSetter = typedAccessor->
mSetter;
2171 mFiller = typedAccessor->
mFiller;
2174 template <
typename ValueType,
typename CodecType>
2178 if (mCollapseOnDestruction)
const_cast<AttributeArray*
>(this->mArray)->collapse();
2181 template <
typename ValueType,
typename CodecType>
2182 template <
bool IsUnknownCodec>
2183 typename std::enable_if<IsUnknownCodec, bool>::type
2188 return mArray->hasValueType<ValueType>();
2191 template <
typename ValueType,
typename CodecType>
2192 template <
bool IsUnknownCodec>
2193 typename std::enable_if<!IsUnknownCodec, bool>::type
2201 template <
typename ValueType,
typename CodecType>
2208 template <
typename ValueType,
typename CodecType>
2211 Index index = n * mStrideOrTotalSize + m;
2212 assert(index < (mSize * mStrideOrTotalSize));
2216 template <
typename ValueType,
typename CodecType>
2219 return this->get<std::is_same<CodecType, UnknownCodec>::value>(this->index(n, m));
2222 template <
typename ValueType,
typename CodecType>
2223 template <
bool IsUnknownCodec>
2224 typename std::enable_if<IsUnknownCodec, ValueType>::type
2229 return (*mGetter)(mArray, index);
2232 template <
typename ValueType,
typename CodecType>
2233 template <
bool IsUnknownCodec>
2234 typename std::enable_if<!IsUnknownCodec, ValueType>::type
2242 template <
typename ValueType,
typename CodecType>
2248 template <
typename ValueType,
typename CodecType>
2258 template <
typename ValueType,
typename CodecType>
2266 template <
typename ValueType,
typename CodecType>
2270 if (expand) array.
expand();
2273 template <
typename ValueType,
typename CodecType>
2276 this->set<std::is_same<CodecType, UnknownCodec>::value>(this->index(n, 0), value);
2279 template <
typename ValueType,
typename CodecType>
2282 this->set<std::is_same<CodecType, UnknownCodec>::value>(this->index(n, m), value);
2285 template <
typename ValueType,
typename CodecType>
2291 template <
typename ValueType,
typename CodecType>
2297 template <
typename ValueType,
typename CodecType>
2303 template <
typename ValueType,
typename CodecType>
2306 this->mCollapser(const_cast<AttributeArray*>(this->mArray), uniformValue);
2309 template <
typename ValueType,
typename CodecType>
2312 this->mFiller(const_cast<AttributeArray*>(this->mArray), value);
2315 template <
typename ValueType,
typename CodecType>
2316 template <
bool IsUnknownCodec>
2317 typename std::enable_if<IsUnknownCodec, void>::type
2322 (*this->mSetter)(const_cast<AttributeArray*>(this->mArray), index, value);
2325 template <
typename ValueType,
typename CodecType>
2326 template <
bool IsUnknownCodec>
2327 typename std::enable_if<!IsUnknownCodec, void>::type
2335 template <
typename ValueType,
typename CodecType>
2338 assert(this->mArray);
2347 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED Index storageTypeSize() const override
Definition: AttributeArray.h:692
static void unregisterType(const NamePair &type, const ScopedRegistryLock *lock=nullptr)
Remove a attribute type from the registry.
Definition: AttributeArray.h:593
virtual Index stride() const =0
Definition: AttributeArray.h:564
Index size() const
Definition: AttributeArray.h:896
bool isUniform() const
Definition: AttributeArray.h:2243
Accessor base class for AttributeArray storage where type is not available.
Definition: AttributeArray.h:478
Definition: AttributeArray.h:504
ValuePtr mCollapser
Definition: AttributeArray.h:494
AccessorBasePtr getAccessor() const override
Obtain an Accessor that stores getter and setter functors.
Definition: AttributeArray.h:2037
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:415
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.
SetterPtr mSetter
Definition: AttributeArray.h:493
half Type
Definition: AttributeArray.h:505
Definition: AttributeArray.h:141
Definition: AttributeArray.h:512
A Paging wrapper to std::istream that is responsible for reading from a given input stream and creati...
Definition: StreamCompression.h:235
TypedAttributeArray & operator=(const TypedAttributeArray &)
Definition: AttributeArray.h:1219
Typed class for storing attribute data.
Definition: AttributeArray.h:608
bool sizeOnly() const
Definition: StreamCompression.h:283
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.
Index stride() const override
Definition: AttributeArray.h:673
static bool isRegistered()
Return true if this attribute type is registered.
Definition: AttributeArray.h:1256
virtual ~AttributeArray()
Definition: AttributeArray.h:164
bool valueTypeIsQuaternion() const override
Return true if the value type is a quaternion.
Definition: AttributeArray.h:1402
void readBuffers(std::istream &) override
Read attribute buffers from a stream.
Definition: AttributeArray.h:1780
virtual bool isDataLoaded() const =0
Return true if all data has been loaded.
void loadData() const override
Ensures all data is in-core.
Definition: AttributeArray.h:1702
void expand(bool fill=true) override
Replace the single value storage with an array of length size().
Definition: AttributeArray.h:1544
ValueType get(Index n) const
Return the value at index n.
Definition: AttributeArray.h:1441
Index size() const override
Return the number of elements in this array.
Definition: AttributeArray.h:669
Name codecType() const override
Return the name of the codec used by this array (e.g., "trnc" or "fxpt").
Definition: AttributeArray.h:685
static ValueType encode(const ValueType &value)
Definition: AttributeArray.h:558
void write(std::ostream &os, bool outputTransient) const override
Definition: AttributeArray.h:1871
short Type
Definition: AttributeArray.h:506
OPENVDB_DEPRECATED bool decompress() override
Uncompress the attribute array.
Definition: AttributeArray.h:1653
static bool isRegistered(const NamePair &type, const ScopedRegistryLock *lock=nullptr)
Return true if the given attribute type name is registered.
static ValueType encode(const ValueType &value)
Definition: AttributeArray.h:567
static void registerType()
Register this attribute type along with a factory function.
Definition: AttributeArray.h:1264
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
bool isStreaming() const
Return true if this attribute is in streaming mode.
Definition: AttributeArray.h:366
static void unregisterType()
Remove this attribute type from the registry.
Definition: AttributeArray.h:1272
GetterPtr mGetter
Definition: AttributeArray.h:910
std::shared_ptr< Handle > Ptr
Definition: AttributeArray.h:948
Ptr(*)(Index, Index, bool) FactoryMethod
Definition: AttributeArray.h:155
AttributeArray & operator=(const AttributeArray &rhs)
Definition: AttributeArray.h:181
bool operator!=(const AttributeArray &other) const
Definition: AttributeArray.h:413
AttributeArray()
Definition: AttributeArray.h:160
Definition: AttributeArray.h:555
void readMetadata(std::istream &) override
Read attribute metadata from a stream.
Definition: AttributeArray.h:1729
Flag
Definition: AttributeArray.h:127
std::shared_ptr< AccessorBase > AccessorBasePtr
Definition: AttributeArray.h:124
Index32 Index
Definition: Types.h:61
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.
void(*)(AttributeArray *array, const StringIndexType &value) ValuePtr
Definition: AttributeArray.h:883
static const char * name()
Definition: AttributeArray.h:581
void(*)(AttributeArray *array, const Index n, const StringIndexType &value) SetterPtr
Definition: AttributeArray.h:882
Definition: AttributeArray.h:137
Accessor to call unsafe get and set methods based on templated Codec and Value.
Definition: AttributeArray.h:2096
std::shared_ptr< Handle > Ptr
Definition: AttributeArray.h:877
typename T::ValueType ElementType
Definition: Types.h:235
static Ptr create(const AttributeArray &array, const bool collapseOnDestruction=true)
Definition: AttributeArray.h:2140
GetterPtr mGetter
Definition: AttributeArray.h:492
Write-able version of AttributeHandle.
Definition: AttributeArray.h:944
std::shared_ptr< TypedAttributeArray > Ptr
Definition: AttributeArray.h:614
void(*)(AttributeArray *array, const Index n, const T &value) SetterPtr
Definition: AttributeArray.h:486
StorageType Type
Definition: AttributeArray.h:593
AttributeArray::Ptr copy() const override
Definition: AttributeArray.h:1307
compression::PageHandle::Ptr mPageHandle
Definition: AttributeArray.h:466
std::shared_ptr< const AttributeArray > ConstPtr
Definition: AttributeArray.h:153
std::unique_ptr< Handle > ScopedPtr
Definition: AttributeArray.h:949
uint8_t flags() const
Retrieve the attribute array flags.
Definition: AttributeArray.h:372
bool mIsUniform
Definition: AttributeArray.h:459
ValueType getUnsafe(Index n) const
Return the value at index n (assumes uncompressed and in-core)
Definition: AttributeArray.h:1429
std::ostream & getOutputStream()
Set and get the output stream.
Definition: StreamCompression.h:286
OPENVDB_DEPRECATED bool compress() override
Compress the attribute array.
Definition: AttributeArray.h:1637
void(*)(AttributeArray *array, const T &value) ValuePtr
Definition: AttributeArray.h:487
FloatVectorT fixedPointToFloatingPoint(const math::Vec3< IntegerT > &v)
Definition: AttributeArray.h:105
Definition: AttributeArray.h:535
virtual Index dataSize() const =0
Convenience wrappers to using Blosc and reading and writing of Paged data.
std::unique_ptr< PageHandle > Ptr
Definition: StreamCompression.h:198
Codec_ Codec
Definition: AttributeArray.h:618
T(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:485
SetterPtr mSetter
Definition: AttributeArray.h:911
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:65
StringIndexType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:881
std::unique_ptr< Handle > UniquePtr
Definition: AttributeArray.h:878
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:1174
Definition: AttributeArray.h:576
static const char * name()
Definition: AttributeArray.h:597
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:128
data is marked as strided when written
Definition: AttributeArray.h:138
Name valueType() const override
Return the name of the value type of a single element in this array (e.g., "float" or "vec3d")...
Definition: AttributeArray.h:682
Definition: Exceptions.h:91
AttributeArray::Ptr copyUncompressed() const override
Definition: AttributeArray.h:1316
std::string Name
Definition: Name.h:44
T Type
Definition: AttributeArray.h:535
typename attribute_traits::TruncateTrait< T >::Type Type
Definition: AttributeArray.h:546
typename Codec::template Storage< ValueType >::Type StorageType
Definition: AttributeArray.h:619
tbb::atomic< Index32 > mOutOfCore
Definition: AttributeArray.h:463
StorageType * data()
Return the raw data buffer.
Definition: AttributeArray.h:818
bool hasValueType() const
Return true if this attribute has a value type the same as the template parameter.
Definition: AttributeArray.h:274
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...
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form 'someVar << "some text" << ...'.
Definition: logging.h:280
uint8_t mFlags
Definition: AttributeArray.h:461
bool isDataLoaded() const override
Return true if all data has been loaded.
Definition: AttributeArray.h:1711
bool valueTypeIsVector() const override
Return true if the value type is a vector.
Definition: AttributeArray.h:1394
virtual bool isUniform() const =0
Return true if this array is stored as a single uniform value.
void writeMetadata(std::ostream &os, bool outputTransient, bool paged) const override
Definition: AttributeArray.h:1880
T & z()
Definition: Vec3.h:112
size_t mCompressedBytes
Definition: AttributeArray.h:467
bool compact() override
Compact the existing array to become uniform if all values are identical.
Definition: AttributeArray.h:1565
Definition: AttributeArray.h:588
void readPagedBuffers(compression::PagedInputStream &) override
Read attribute buffers from a paged stream.
Definition: AttributeArray.h:1819
void setUnsafe(Index n, const ValueType &value)
Set value at the given index n (assumes uncompressed and in-core)
Definition: AttributeArray.h:1478
virtual Index storageTypeSize() const =0
T & y()
Definition: Vec3.h:111
Definition: Exceptions.h:40
void writePagedBuffers(compression::PagedOutputStream &os, bool outputTransient) const override
Definition: AttributeArray.h:1985
void collapse() override
Replace the existing array with a uniform zero value.
Definition: AttributeArray.h:1582
const NamePair & type() const override
Return the name of this attribute's type.
Definition: AttributeArray.h:659
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:2098
static ValueType decode(const ValueType &value)
Definition: AttributeArray.h:568
Index stride() const
Definition: AttributeArray.h:895
AttributeArray(const AttributeArray &rhs)
Definition: AttributeArray.h:171
static const NamePair & attributeType()
Return the name of this attribute's type (includes codec)
Definition: AttributeArray.h:1244
static const char * name()
Definition: AttributeArray.h:557
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:152
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
Definition: AttributeArray.h:2099
void setConstantStride(bool state)
Specify whether this attribute has a constant stride or not.
Definition: Exceptions.h:84
void writeBuffers(std::ostream &os, bool outputTransient) const override
Definition: AttributeArray.h:1943
static ValueType decode(const ValueType &value)
Definition: AttributeArray.h:559
bool hasConstantStride() const
Definition: AttributeArray.h:2249
Accessor(GetterPtr getter, SetterPtr setter, ValuePtr collapser, ValuePtr filler)
Definition: AttributeArray.h:489
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:1280
void read(std::istream &) override
Read attribute data from a stream.
Definition: AttributeArray.h:1720
bool validData() const
Verify that data is not out-of-core or in a partially-read state.
Definition: AttributeArray.h:822
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
Definition: AttributeArray.h:2120
uint64_t Index64
Definition: Types.h:60
bool isTransient() const
Return true if this attribute is not serialized during stream output.
Definition: AttributeArray.h:358
~TypedAttributeArray() override
Definition: AttributeArray.h:637
static void registerType(const NamePair &type, FactoryMethod, const ScopedRegistryLock *lock=nullptr)
Register a attribute type along with a factory function.
Definition: AttributeArray.h:573
Definition: AttributeArray.h:873
hidden from UIs or iterators
Definition: AttributeArray.h:130
const AttributeArray * mArray
Definition: AttributeArray.h:908
PagedOutputStream & write(const char *str, std::streamsize n)
Writes the given.
uint16_t Type
Definition: AttributeArray.h:514
static const char * name()
Definition: AttributeArray.h:550
uint8_t Type
Definition: AttributeArray.h:513
bool valueTypeIsFloatingPoint() const override
Return true if the value type is floating point.
Definition: AttributeArray.h:1365
tbb::spin_mutex mMutex
Definition: AttributeArray.h:460
ValuePtr mFiller
Definition: AttributeArray.h:913
bool valueTypeIsMatrix() const override
Return true if the value type is a matrix.
Definition: AttributeArray.h:1411
Index dataSize() const override
Return the size of the data in this array.
Definition: AttributeArray.h:676
AttributeHandle(const AttributeArray &array, const bool collapseOnDestruction=true)
Definition: AttributeArray.h:2147
Index valueTypeSize() const override
Return the size in bytes of the value type of a single element in this array.
Definition: AttributeArray.h:688
uint16_t StorageType
Definition: AttributeArray.h:590
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:498
bool hasConstantStride() const
Return true if this attribute has a constant stride.
Definition: AttributeArray.h:369
SerializationFlag
Definition: AttributeArray.h:136
A Paging wrapper to std::ostream that is responsible for writing from a given output stream at interv...
Definition: StreamCompression.h:272
bool sizeOnly() const
Definition: StreamCompression.h:246
OPENVDB_DEPRECATED bool isCompressed() const
Definition: AttributeArray.h:331
const StorageType * data() const
Definition: AttributeArray.h:819
virtual void expand(bool fill=true)=0
If this array is uniform, replace it with an array of length size().
void set(Index n, const ValueType &value)
Set value at the given index n.
Definition: AttributeArray.h:1493
typename attribute_traits::UIntTypeTrait< OneByte, T >::Type Type
Definition: AttributeArray.h:576
Definition: AttributeArray.h:145
PageHandle::Ptr createHandle(std::streamsize n)
Creates a PageHandle to access the next.
Definition: Compression.h:83
size_t memUsage() const override
Return the number of bytes of memory used by this attribute.
Definition: AttributeArray.h:1421
static TypedAttributeArray & cast(AttributeArray &attributeArray)
Cast an AttributeArray to TypedAttributeArray<T>
Definition: AttributeArray.h:1287
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:270
ValueType_ ValueType
Definition: AttributeArray.h:617
Definition: Exceptions.h:92
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: AttributeArray.h:1661
Definition: AttributeArray.h:543
virtual bool valueTypeIsFloatingPoint() const =0
Return true if the value type is floating point.
Definition: AttributeArray.h:532
Definition: AttributeArray.h:529
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
int16_t Int16
Definition: Types.h:62
virtual void loadData() const =0
Ensures all data is in-core.
void fill(const ValueType &value)
Fill the existing array with the given value.
Definition: AttributeArray.h:1612
bool isUniform() const override
Return true if this array is stored as a single uniform value.
Definition: AttributeArray.h:746
SharedPtr< MappedFile > Ptr
Definition: io.h:163
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...
ValuePtr mCollapser
Definition: AttributeArray.h:912
static const char * name()
Definition: AttributeArray.h:539
uint8_t mUsePagedRead
Definition: AttributeArray.h:462
bool valueTypeIsClass() const override
Return true if the value type is a class (ie vector, matrix or quaternion return true) ...
Definition: AttributeArray.h:1385
Base class for storing attribute data.
Definition: AttributeArray.h:118
Definition: AttributeArray.h:546
ValuePtr mFiller
Definition: AttributeArray.h:495
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:2119
bool isHidden() const
Return true if this attribute is hidden (e.g., from UI or iterators).
Definition: AttributeArray.h:351
Definition: AttributeArray.h:122
Definition: Exceptions.h:85
streaming mode collapses attributes when first accessed
Definition: AttributeArray.h:133
IntegerVectorT floatingPointToFixedPoint(const math::Vec3< FloatT > &v)
Definition: AttributeArray.h:95
OPENVDB_API size_t bloscCompressedSize(const char *buffer, const size_t uncompressedBytes)
Convenience wrapper to retrieve the compressed size of buffer when compressed.
static const char * name()
Definition: AttributeArray.h:566
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:110
std::istream & getInputStream()
Definition: StreamCompression.h:249