10 #ifndef OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED 11 #define OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED 23 #include <tbb/spin_mutex.h> 29 #include <type_traits> 32 class TestAttributeArray;
48 template <
typename IntegerT,
typename FloatT>
59 template <
typename FloatT,
typename IntegerT>
67 template <
typename IntegerVectorT,
typename FloatT>
71 return IntegerVectorT(
72 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
x()),
73 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
y()),
74 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
z()));
77 template <
typename FloatVectorT,
typename IntegerT>
82 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
x()),
83 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
y()),
84 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
z()));
104 CONSTANTSTRIDE = 0x8,
112 WRITEMEMCOMPRESS = 0x4,
120 tbb::spin_mutex::scoped_lock lock;
125 using Ptr = std::shared_ptr<AttributeArray>;
126 using ConstPtr = std::shared_ptr<const AttributeArray>;
137 if (mFlags & PARTIALREAD) mCompressedBytes = 0;
147 #if OPENVDB_ABI_VERSION_NUMBER < 10 157 virtual Index size()
const = 0;
161 virtual Index stride()
const = 0;
165 virtual Index dataSize()
const = 0;
168 virtual Name valueType()
const = 0;
171 virtual Name codecType()
const = 0;
175 virtual Index valueTypeSize()
const = 0;
179 virtual Index storageTypeSize()
const = 0;
182 virtual bool valueTypeIsFloatingPoint()
const = 0;
185 virtual bool valueTypeIsClass()
const = 0;
188 virtual bool valueTypeIsVector()
const = 0;
191 virtual bool valueTypeIsQuaternion()
const = 0;
194 virtual bool valueTypeIsMatrix()
const = 0;
197 virtual size_t memUsage()
const = 0;
199 #if OPENVDB_ABI_VERSION_NUMBER >= 10 210 static Ptr create(
const NamePair& type, Index length, Index stride = 1,
211 bool constantStride =
true,
221 virtual const NamePair& type()
const = 0;
223 template<
typename AttributeArrayType>
224 bool isType()
const {
return this->type() == AttributeArrayType::attributeType(); }
227 template<
typename ValueType>
228 bool hasValueType()
const {
return this->type().first == typeNameAsString<ValueType>(); }
230 #if OPENVDB_ABI_VERSION_NUMBER < 10 236 virtual void set(
const Index n,
const AttributeArray& sourceArray,
const Index sourceIndex) = 0;
262 template<
typename IterT>
263 void copyValuesUnsafe(
const AttributeArray& sourceArray,
const IterT& iter);
267 template<
typename IterT>
268 void copyValues(
const AttributeArray& sourceArray,
const IterT& iter,
bool compact =
true);
271 virtual bool isUniform()
const = 0;
274 virtual void expand(
bool fill =
true) = 0;
276 virtual void collapse() = 0;
278 virtual bool compact() = 0;
280 #if OPENVDB_ABI_VERSION_NUMBER < 10 285 virtual bool compress() = 0;
290 virtual bool decompress() = 0;
297 void setHidden(
bool state);
299 bool isHidden()
const {
return bool(mFlags & HIDDEN); }
304 void setTransient(
bool state);
312 void setStreaming(
bool state);
320 uint8_t
flags()
const {
return mFlags; }
323 virtual void read(std::istream&) = 0;
326 virtual void write(std::ostream&,
bool outputTransient)
const = 0;
328 virtual void write(std::ostream&)
const = 0;
331 virtual void readMetadata(std::istream&) = 0;
335 virtual void writeMetadata(std::ostream&,
bool outputTransient,
bool paged)
const = 0;
338 virtual void readBuffers(std::istream&) = 0;
341 virtual void writeBuffers(std::ostream&,
bool outputTransient)
const = 0;
350 virtual void loadData()
const = 0;
353 virtual bool isDataLoaded()
const = 0;
361 #if OPENVDB_ABI_VERSION_NUMBER >= 9 367 friend class ::TestAttributeArray;
374 virtual char* dataAsByteArray() = 0;
375 virtual const char* dataAsByteArray()
const = 0;
378 template <
typename IterT>
379 void doCopyValues(
const AttributeArray& sourceArray,
const IterT& iter,
380 bool rangeChecking =
true);
386 void setConstantStride(
bool state);
395 static void unregisterType(
const NamePair& type,
398 bool mIsUniform =
true;
401 uint8_t mUsePagedRead = 0;
419 template <
typename T>
427 mGetter(getter), mSetter(setter), mCollapser(collapser), mFiller(filler) { }
439 namespace attribute_traits
471 template <
typename T>
474 template<
typename ValueType>
static void decode(
const ValueType&, ValueType&);
475 template<
typename ValueType>
static void encode(
const ValueType&, ValueType&);
476 static const char*
name() {
return "null"; }
482 template <
typename T>
485 template<
typename StorageType,
typename ValueType>
static void decode(
const StorageType&, ValueType&);
486 template<
typename StorageType,
typename ValueType>
static void encode(
const ValueType&, StorageType&);
487 static const char*
name() {
return "trnc"; }
494 static const char*
name() {
return "fxpt"; }
495 template <
typename ValueType>
static ValueType
encode(
const ValueType&
value) {
return value + ValueType(0.5); }
496 template <
typename ValueType>
static ValueType
decode(
const ValueType&
value) {
return value - ValueType(0.5); }
503 static const char*
name() {
return "ufxpt"; }
504 template <
typename ValueType>
static ValueType
encode(
const ValueType&
value) {
return value; }
505 template <
typename ValueType>
static ValueType
decode(
const ValueType&
value) {
return value; }
509 template <
bool OneByte,
typename Range=PositionRange>
512 template <
typename T>
515 template<
typename StorageType,
typename ValueType>
static void decode(
const StorageType&, ValueType&);
516 template<
typename StorageType,
typename ValueType>
static void encode(
const ValueType&, StorageType&);
519 static const std::string
Name = std::string(Range::name()) + (OneByte ?
"8" :
"16");
529 template <
typename T>
534 static const char*
name() {
return "uvec"; }
543 template<
typename ValueType_,
typename Codec_ = NullCodec>
547 using Ptr = std::shared_ptr<TypedAttributeArray>;
548 using ConstPtr = std::shared_ptr<const TypedAttributeArray>;
552 using StorageType =
typename Codec::template Storage<ValueType>::Type;
558 const ValueType& uniformValue = zeroVal<ValueType>());
567 #if OPENVDB_ABI_VERSION_NUMBER < 10 575 TypedAttributeArray& operator=(
const TypedAttributeArray&);
577 TypedAttributeArray(TypedAttributeArray&&) =
delete;
579 TypedAttributeArray& operator=(TypedAttributeArray&&) =
delete;
587 #if OPENVDB_ABI_VERSION_NUMBER < 10 595 static Ptr create(
Index n,
Index strideOrTotalSize = 1,
bool constantStride =
true,
596 const Metadata* metadata =
nullptr);
602 static const TypedAttributeArray& cast(
const AttributeArray& attributeArray);
605 static const NamePair& attributeType();
610 static bool isRegistered();
612 static void registerType();
614 static void unregisterType();
621 Index stride()
const override {
return hasConstantStride() ? mStrideOrTotalSize : 0; }
625 return hasConstantStride() ? mSize * mStrideOrTotalSize : mStrideOrTotalSize;
642 bool valueTypeIsFloatingPoint()
const override;
645 bool valueTypeIsClass()
const override;
648 bool valueTypeIsVector()
const override;
651 bool valueTypeIsQuaternion()
const override;
654 bool valueTypeIsMatrix()
const override;
659 #if OPENVDB_ABI_VERSION_NUMBER >= 10 672 template<
typename T>
void getUnsafe(
Index n, T&
value)
const;
674 template<
typename T>
void get(
Index n, T&
value)
const;
685 template<
typename T>
void setUnsafe(
Index n,
const T& value);
687 template<
typename T>
void set(
Index n,
const T&
value);
693 #if OPENVDB_ABI_VERSION_NUMBER < 10 704 void expand(
bool fill =
true)
override;
706 void collapse()
override;
708 bool compact()
override;
711 void collapse(
const ValueType& uniformValue);
721 #if OPENVDB_ABI_VERSION_NUMBER < 10 724 bool compress()
override;
727 bool decompress()
override;
731 void read(std::istream&)
override;
735 void write(std::ostream& os,
bool outputTransient)
const override;
737 void write(std::ostream&)
const override;
740 void readMetadata(std::istream&)
override;
745 void writeMetadata(std::ostream& os,
bool outputTransient,
bool paged)
const override;
748 void readBuffers(std::istream&)
override;
752 void writeBuffers(std::ostream& os,
bool outputTransient)
const override;
762 inline bool isOutOfCore()
const;
765 void loadData()
const override;
768 bool isDataLoaded()
const override;
770 #if OPENVDB_ABI_VERSION_NUMBER >= 9 783 inline bool validData()
const {
return !(isOutOfCore() || (flags() & PARTIALREAD)); }
786 friend class ::TestAttributeArray;
788 TypedAttributeArray(
const TypedAttributeArray&,
const tbb::spin_mutex::scoped_lock&);
791 inline void doLoad()
const;
793 #if OPENVDB_ABI_VERSION_NUMBER >= 10 794 inline void doLoadUnsafe()
const;
797 inline void doLoadUnsafe(
const bool compression =
true)
const;
799 inline bool compressUnsafe();
803 inline void setOutOfCore(
const bool);
809 char* dataAsByteArray()
override;
810 const char* dataAsByteArray()
const override;
812 size_t arrayMemUsage()
const;
819 return TypedAttributeArray::create(n, strideOrTotalSize, constantStride, metadata);
822 static std::unique_ptr<const NamePair> sTypeName;
823 std::unique_ptr<StorageType[]> mData;
825 Index mStrideOrTotalSize;
834 template <
typename ValueType,
typename CodecType = UnknownCodec>
839 using Ptr = std::shared_ptr<Handle>;
848 static Ptr create(
const AttributeArray& array,
const bool collapseOnDestruction =
true);
860 bool isUniform()
const;
861 bool hasConstantStride()
const;
878 friend class ::TestAttributeArray;
880 template <
bool IsUnknownCodec>
881 typename std::enable_if<IsUnknownCodec, bool>::type compatibleType()
const;
883 template <
bool IsUnknownCodec>
884 typename std::enable_if<!IsUnknownCodec, bool>::type compatibleType()
const;
886 template <
bool IsUnknownCodec>
887 typename std::enable_if<IsUnknownCodec, ValueType>::type
get(
Index index)
const;
889 template <
bool IsUnknownCodec>
890 typename std::enable_if<!IsUnknownCodec, ValueType>::type
get(
Index index)
const;
895 Index mStrideOrTotalSize;
897 bool mCollapseOnDestruction;
905 template <
typename ValueType,
typename CodecType = UnknownCodec>
910 using Ptr = std::shared_ptr<Handle>;
921 void expand(
bool fill =
true);
925 void collapse(
const ValueType& uniformValue);
932 void fill(
const ValueType&
value);
940 friend class ::TestAttributeArray;
942 template <
bool IsUnknownCodec>
943 typename std::enable_if<IsUnknownCodec, void>::type
set(
Index index,
const ValueType&
value)
const;
945 template <
bool IsUnknownCodec>
946 typename std::enable_if<!IsUnknownCodec, void>::type
set(
Index index,
const ValueType&
value)
const;
956 template<
typename ValueType>
958 NullCodec::decode(
const ValueType& data, ValueType& val)
964 template<
typename ValueType>
966 NullCodec::encode(
const ValueType& val, ValueType& data)
972 template<
typename StorageType,
typename ValueType>
974 TruncateCodec::decode(
const StorageType& data, ValueType& val)
976 val =
static_cast<ValueType
>(data);
980 template<
typename StorageType,
typename ValueType>
982 TruncateCodec::encode(
const ValueType& val, StorageType& data)
984 data =
static_cast<StorageType
>(val);
988 template <
bool OneByte,
typename Range>
989 template<
typename StorageType,
typename ValueType>
993 val = fixedPointToFloatingPoint<ValueType>(data);
997 val = Range::template decode<ValueType>(val);
1001 template <
bool OneByte,
typename Range>
1002 template<
typename StorageType,
typename ValueType>
1008 const ValueType newVal = Range::template encode<ValueType>(val);
1010 data = floatingPointToFixedPoint<StorageType>(newVal);
1014 template<
typename T>
1018 val = math::QuantizedUnitVec::unpack(data);
1022 template<
typename T>
1026 data = math::QuantizedUnitVec::pack(val);
1034 template <
typename IterT>
1035 void AttributeArray::doCopyValues(
const AttributeArray& sourceArray,
const IterT& iter,
1041 assert(sourceArray.
isDataLoaded() && this->isDataLoaded());
1043 assert(this->storageTypeSize()*this->stride() ==
1047 const char*
const sourceBuffer = sourceArray.dataAsByteArray();
1048 char*
const targetBuffer = this->dataAsByteArray();
1049 assert(sourceBuffer && targetBuffer);
1051 if (rangeChecking && this->isUniform()) {
1055 const bool sourceIsUniform = sourceArray.
isUniform();
1057 const Index sourceDataSize = rangeChecking ? sourceArray.
dataSize() : 0;
1058 const Index targetDataSize = rangeChecking ? this->dataSize() : 0;
1060 for (IterT it(iter); it; ++it) {
1061 const Index sourceIndex = sourceIsUniform ? 0 : it.sourceIndex();
1062 const Index targetIndex = it.targetIndex();
1064 if (rangeChecking) {
1065 if (sourceIndex >= sourceDataSize) {
1067 "Cannot copy array data as source index exceeds size of source array.");
1069 if (targetIndex >= targetDataSize) {
1071 "Cannot copy array data as target index exceeds size of target array.");
1075 assert(sourceIndex < sourceArray.
dataSize());
1076 assert(targetIndex < this->dataSize());
1077 if (this->isUniform()) assert(targetIndex ==
Index(0));
1080 const size_t targetOffset(targetIndex * bytes);
1081 const size_t sourceOffset(sourceIndex * bytes);
1083 std::memcpy(targetBuffer + targetOffset, sourceBuffer + sourceOffset, bytes);
1087 template <
typename IterT>
1088 void AttributeArray::copyValuesUnsafe(
const AttributeArray& sourceArray,
const IterT& iter)
1090 this->doCopyValues(sourceArray, iter,
false);
1093 template <
typename IterT>
1094 void AttributeArray::copyValues(
const AttributeArray& sourceArray,
const IterT& iter,
1098 if (bytes != this->storageTypeSize()) {
1115 this->doCopyValues(sourceArray, iter,
true);
1128 template<
typename ValueType_,
typename Codec_>
1132 template<
typename ValueType_,
typename Codec_>
1138 , mStrideOrTotalSize(strideOrTotalSize)
1140 if (constantStride) {
1142 if (strideOrTotalSize == 0) {
1144 "stride to be at least one.")
1149 if (mStrideOrTotalSize < n) {
1151 "a total size of at least the number of elements in the array.")
1155 mStrideOrTotalSize =
std::max(
Index(1), mStrideOrTotalSize);
1156 Codec::encode(uniformValue, this->
data()[0]);
1160 template<
typename ValueType_,
typename Codec_>
1167 template<
typename ValueType_,
typename Codec_>
1169 const tbb::spin_mutex::scoped_lock& lock)
1172 , mStrideOrTotalSize(rhs.mStrideOrTotalSize)
1176 std::memcpy(static_cast<void*>(this->
data()), rhs.
data(), this->arrayMemUsage());
1181 template<
typename ValueType_,
typename Codec_>
1187 tbb::spin_mutex::scoped_lock lock(
mMutex);
1188 tbb::spin_mutex::scoped_lock rhsLock(rhs.
mMutex);
1195 mStrideOrTotalSize = rhs.mStrideOrTotalSize;
1200 std::memcpy(static_cast<void*>(this->
data()), rhs.
data(), this->arrayMemUsage());
1208 template<
typename ValueType_,
typename Codec_>
1212 static std::once_flag once;
1213 std::call_once(once, []()
1215 sTypeName.reset(
new NamePair(typeNameAsString<ValueType>(), Codec::name()));
1221 template<
typename ValueType_,
typename Codec_>
1229 template<
typename ValueType_,
typename Codec_>
1237 template<
typename ValueType_,
typename Codec_>
1245 template<
typename ValueType_,
typename Codec_>
1254 typedMetadata ? typedMetadata->
value() : zeroVal<ValueType>()));
1257 template<
typename ValueType_,
typename Codec_>
1267 template<
typename ValueType_,
typename Codec_>
1277 template<
typename ValueType_,
typename Codec_>
1285 #if OPENVDB_ABI_VERSION_NUMBER < 10 1286 template<
typename ValueType_,
typename Codec_>
1290 return this->
copy();
1294 template<
typename ValueType_,
typename Codec_>
1304 template<
typename ValueType_,
typename Codec_>
1320 template<
typename ValueType_,
typename Codec_>
1326 this->setOutOfCore(
false);
1329 if (mData) mData.reset();
1333 template<
typename ValueType_,
typename Codec_>
1353 template<
typename ValueType_,
typename Codec_>
1362 template<
typename ValueType_,
typename Codec_>
1370 template<
typename ValueType_,
typename Codec_>
1375 return !this->
valueType().compare(0, 4,
"quat");
1379 template<
typename ValueType_,
typename Codec_>
1384 return !this->
valueType().compare(0, 3,
"mat");
1388 template<
typename ValueType_,
typename Codec_>
1392 return sizeof(*this) + (bool(mData) ? this->arrayMemUsage() : 0);
1395 #if OPENVDB_ABI_VERSION_NUMBER >= 10 1396 template<
typename ValueType_,
typename Codec_>
1405 template<
typename ValueType_,
typename Codec_>
1417 template<
typename ValueType_,
typename Codec_>
1428 template<
typename ValueType_,
typename Codec_>
1429 template<
typename T>
1433 val =
static_cast<T
>(this->
getUnsafe(n));
1437 template<
typename ValueType_,
typename Codec_>
1438 template<
typename T>
1442 val =
static_cast<T
>(this->
get(n));
1446 template<
typename ValueType_,
typename Codec_>
1454 template<
typename ValueType_,
typename Codec_>
1469 template<
typename ValueType_,
typename Codec_>
1481 template<
typename ValueType_,
typename Codec_>
1482 template<
typename T>
1486 this->
setUnsafe(n, static_cast<ValueType>(val));
1490 template<
typename ValueType_,
typename Codec_>
1491 template<
typename T>
1495 this->
set(n,
static_cast<ValueType>(val));
1499 template<
typename ValueType_,
typename Codec_>
1507 #if OPENVDB_ABI_VERSION_NUMBER < 10 1508 template<
typename ValueType_,
typename Codec_>
1515 sourceTypedArray.
get(sourceIndex, sourceValue);
1517 this->
set(n, sourceValue);
1522 template<
typename ValueType_,
typename Codec_>
1531 tbb::spin_mutex::scoped_lock lock(
mMutex);
1543 template<
typename ValueType_,
typename Codec_>
1550 const ValueType_ val = this->
get(0);
1560 template<
typename ValueType_,
typename Codec_>
1564 this->
collapse(zeroVal<ValueType>());
1568 template<
typename ValueType_,
typename Codec_>
1573 tbb::spin_mutex::scoped_lock lock(
mMutex);
1578 Codec::encode(uniformValue, this->
data()[0]);
1582 template<
typename ValueType_,
typename Codec_>
1590 template<
typename ValueType_,
typename Codec_>
1595 tbb::spin_mutex::scoped_lock lock(
mMutex);
1602 Codec::encode(value, this->
data()[i]);
1607 template<
typename ValueType_,
typename Codec_>
1615 #if OPENVDB_ABI_VERSION_NUMBER < 10 1616 template<
typename ValueType_,
typename Codec_>
1624 template<
typename ValueType_,
typename Codec_>
1632 template<
typename ValueType_,
typename Codec_>
1641 template<
typename ValueType_,
typename Codec_>
1649 template<
typename ValueType_,
typename Codec_>
1657 template<
typename ValueType_,
typename Codec_>
1668 tbb::spin_mutex::scoped_lock lock(self->mMutex);
1669 this->doLoadUnsafe();
1673 template<
typename ValueType_,
typename Codec_>
1681 template<
typename ValueType_,
typename Codec_>
1689 template<
typename ValueType_,
typename Codec_>
1698 template<
typename ValueType_,
typename Codec_>
1705 is.read(reinterpret_cast<char*>(&bytes),
sizeof(
Index64));
1706 bytes = bytes -
sizeof(
Int16) -
sizeof(
Index);
1708 uint8_t
flags = uint8_t(0);
1709 is.read(reinterpret_cast<char*>(&flags),
sizeof(uint8_t));
1712 uint8_t serializationFlags = uint8_t(0);
1713 is.read(reinterpret_cast<char*>(&serializationFlags),
sizeof(uint8_t));
1716 is.read(reinterpret_cast<char*>(&size),
sizeof(
Index));
1725 if (serializationFlags >= 0x10) {
1740 is.read(reinterpret_cast<char*>(&stride),
sizeof(
Index));
1741 mStrideOrTotalSize =
stride;
1744 mStrideOrTotalSize = 1;
1749 template<
typename ValueType_,
typename Codec_>
1758 tbb::spin_mutex::scoped_lock lock(
mMutex);
1762 uint8_t bloscCompressed(0);
1763 if (!
mIsUniform) is.read(reinterpret_cast<char*>(&bloscCompressed),
sizeof(uint8_t));
1768 mCompressedBytes = 0;
1773 if (bloscCompressed == uint8_t(1)) {
1779 if (newBuffer) buffer.reset(newBuffer.release());
1784 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1788 template<
typename ValueType_,
typename Codec_>
1797 #ifdef OPENVDB_USE_DELAYED_LOADING 1800 io::MappedFile::Ptr mappedFile = io::getMappedFilePtr(is.
getInputStream());
1801 const bool delayLoad = (mappedFile.get() !=
nullptr);
1816 tbb::spin_mutex::scoped_lock lock(
mMutex);
1820 #ifdef OPENVDB_USE_DELAYED_LOADING 1821 this->setOutOfCore(delayLoad);
1825 #endif // OPENVDB_USE_DELAYED_LOADING 1827 #ifdef OPENVDB_USE_DELAYED_LOADING 1830 std::unique_ptr<char[]> buffer =
mPageHandle->read();
1831 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1833 #ifdef OPENVDB_USE_DELAYED_LOADING 1843 template<
typename ValueType_,
typename Codec_>
1847 this->
write(os,
false);
1851 template<
typename ValueType_,
typename Codec_>
1860 template<
typename ValueType_,
typename Codec_>
1864 if (!outputTransient && this->
isTransient())
return;
1871 uint8_t serializationFlags(0);
1874 bool strideOfOne(this->
stride() == 1);
1879 if (bloscCompression) this->doLoad();
1881 size_t compressedBytes = 0;
1891 if (bloscCompression && paged) serializationFlags |=
WRITEPAGED;
1893 else if (bloscCompression)
1897 const char* charBuffer =
reinterpret_cast<const char*
>(this->
data());
1898 const size_t inBytes = this->arrayMemUsage();
1905 bytes += (compressedBytes > 0) ? compressedBytes : this->arrayMemUsage();
1909 os.write(reinterpret_cast<const char*>(&bytes),
sizeof(
Index64));
1910 os.write(reinterpret_cast<const char*>(&flags),
sizeof(uint8_t));
1911 os.write(reinterpret_cast<const char*>(&serializationFlags),
sizeof(uint8_t));
1912 os.write(reinterpret_cast<const char*>(&size),
sizeof(
Index));
1915 if (!strideOfOne) os.write(reinterpret_cast<const char*>(&stride),
sizeof(
Index));
1919 template<
typename ValueType_,
typename Codec_>
1923 if (!outputTransient && this->
isTransient())
return;
1932 os.write(reinterpret_cast<const char*>(this->
data()),
sizeof(
StorageType));
1936 std::unique_ptr<char[]> compressedBuffer;
1937 size_t compressedBytes = 0;
1938 const char* charBuffer =
reinterpret_cast<const char*
>(this->
data());
1939 const size_t inBytes = this->arrayMemUsage();
1941 if (compressedBuffer) {
1942 uint8_t bloscCompressed(1);
1943 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1944 os.write(reinterpret_cast<const char*>(compressedBuffer.get()), compressedBytes);
1947 uint8_t bloscCompressed(0);
1948 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1949 os.write(reinterpret_cast<const char*>(this->
data()), inBytes);
1954 uint8_t bloscCompressed(0);
1955 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1956 os.write(reinterpret_cast<const char*>(this->
data()), this->arrayMemUsage());
1961 template<
typename ValueType_,
typename Codec_>
1965 if (!outputTransient && this->
isTransient())
return;
1969 if (!bloscCompression) {
1980 os.
write(reinterpret_cast<const char*>(this->
data()), this->arrayMemUsage());
1984 template<
typename ValueType_,
typename Codec_>
1986 #if OPENVDB_ABI_VERSION_NUMBER >= 10 1998 assert(self->mPageHandle);
2001 std::unique_ptr<char[]> buffer =
self->mPageHandle->read();
2003 self->mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
2005 self->mPageHandle.reset();
2009 self->mOutOfCore =
false;
2013 template<
typename ValueType_,
typename Codec_>
2028 template<
typename ValueType_,
typename Codec_>
2033 if(!otherT)
return false;
2034 if(this->mSize != otherT->mSize ||
2035 this->mStrideOrTotalSize != otherT->mStrideOrTotalSize ||
2037 this->attributeType() != this->
attributeType())
return false;
2043 if (!target && !source)
return true;
2044 if (!target || !source)
return false;
2051 template<
typename ValueType_,
typename Codec_>
2055 return reinterpret_cast<char*
>(this->
data());
2059 template<
typename ValueType_,
typename Codec_>
2063 return reinterpret_cast<const char*
>(this->
data());
2071 template <
typename CodecType,
typename ValueType>
2092 template <
typename ValueType>
2100 return (*functor)(array, n);
2105 (*functor)(array, n,
value);
2114 template <
typename ValueType,
typename CodecType>
2122 template <
typename ValueType,
typename CodecType>
2127 , mCollapseOnDestruction(collapseOnDestruction && array.
isStreaming())
2150 template <
typename ValueType,
typename CodecType>
2157 template <
typename ValueType,
typename CodecType>
2158 template <
bool IsUnknownCodec>
2159 typename std::enable_if<IsUnknownCodec, bool>::type
2167 template <
typename ValueType,
typename CodecType>
2168 template <
bool IsUnknownCodec>
2169 typename std::enable_if<!IsUnknownCodec, bool>::type
2177 template <
typename ValueType,
typename CodecType>
2184 template <
typename ValueType,
typename CodecType>
2188 assert(index < (mSize * mStrideOrTotalSize));
2192 template <
typename ValueType,
typename CodecType>
2198 template <
typename ValueType,
typename CodecType>
2199 template <
bool IsUnknownCodec>
2200 typename std::enable_if<IsUnknownCodec, ValueType>::type
2208 template <
typename ValueType,
typename CodecType>
2209 template <
bool IsUnknownCodec>
2210 typename std::enable_if<!IsUnknownCodec, ValueType>::type
2218 template <
typename ValueType,
typename CodecType>
2224 template <
typename ValueType,
typename CodecType>
2234 template <
typename ValueType,
typename CodecType>
2242 template <
typename ValueType,
typename CodecType>
2246 if (expand) array.
expand();
2249 template <
typename ValueType,
typename CodecType>
2255 template <
typename ValueType,
typename CodecType>
2261 template <
typename ValueType,
typename CodecType>
2267 template <
typename ValueType,
typename CodecType>
2273 template <
typename ValueType,
typename CodecType>
2279 template <
typename ValueType,
typename CodecType>
2285 template <
typename ValueType,
typename CodecType>
2288 this->
mFiller(const_cast<AttributeArray*>(this->
mArray), value);
2291 template <
typename ValueType,
typename CodecType>
2292 template <
bool IsUnknownCodec>
2293 typename std::enable_if<IsUnknownCodec, void>::type
2301 template <
typename ValueType,
typename CodecType>
2302 template <
bool IsUnknownCodec>
2303 typename std::enable_if<!IsUnknownCodec, void>::type
2311 template <
typename ValueType,
typename CodecType>
2323 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED static ValueType encode(const ValueType &value)
Definition: AttributeArray.h:495
bool isUniform() const override
Return true if this array is stored as a single uniform value.
Definition: AttributeArray.h:700
static const char * name()
Definition: AttributeArray.h:534
Index32 Index
Definition: Types.h:54
std::shared_ptr< Handle > Ptr
Definition: AttributeArray.h:839
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: AttributeArray.h:1643
bool valueTypeIsVector() const override
Return true if the value type is a vector.
Definition: AttributeArray.h:1364
std::shared_ptr< const AttributeArray > ConstPtr
Definition: AttributeArray.h:126
void readMetadata(std::istream &) override
Read attribute metadata from a stream.
Definition: AttributeArray.h:1700
Definition: AttributeArray.h:483
bool hasValueType() const
Return true if this attribute has a value type the same as the template parameter.
Definition: AttributeArray.h:228
static pnanovdb_uint32_t allocate(pnanovdb_uint32_t *poffset, pnanovdb_uint32_t size, pnanovdb_uint32_t alignment)
Definition: pnanovdb_validate_strides.h:20
ValuePtr mCollapser
Definition: AttributeArray.h:431
uint64_t Index64
Definition: Types.h:53
Definition: AttributeArray.h:492
static Ptr create(AttributeArray &array, const bool expand=true)
Definition: AttributeArray.h:2236
ValueType get(Index n, Index m=0) const
Definition: AttributeArray.h:2193
bool valueTypeIsQuaternion() const override
Return true if the value type is a quaternion.
Definition: AttributeArray.h:1372
Index index(Index n, Index m) const
Definition: AttributeArray.h:2185
Definition: AttributeArray.h:441
bool valueTypeIsFloatingPoint() const override
Return true if the value type is floating point.
Definition: AttributeArray.h:1335
std::atomic< Index32 > mOutOfCore
Definition: AttributeArray.h:402
StorageType Type
Definition: AttributeArray.h:530
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
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.
Definition: AttributeArray.h:513
bool isHidden() const
Return true if this attribute is hidden (e.g., from UI or iterators).
Definition: AttributeArray.h:299
void writeMetadata(std::ostream &os, bool outputTransient, bool paged) const override
Definition: AttributeArray.h:1862
void(*)(AttributeArray *array, const Index &value) ValuePtr
Definition: AttributeArray.h:845
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:224
uint8_t Type
Definition: AttributeArray.h:450
bool mIsUniform
Definition: AttributeArray.h:398
static ValueType decode(const ValueType &value)
Definition: AttributeArray.h:505
A Paging wrapper to std::ostream that is responsible for writing from a given output stream at interv...
Definition: StreamCompression.h:243
Index storageTypeSize() const override
Definition: AttributeArray.h:639
void expand(bool fill=true) override
Replace the single value storage with an array of length size().
Definition: AttributeArray.h:1524
typename Codec::template Storage< ValueType >::Type StorageType
Definition: AttributeArray.h:552
void expand(bool fill=true)
If this array is uniform, replace it with an array of length size().
Definition: AttributeArray.h:2262
void writeBuffers(std::ostream &os, bool outputTransient) const override
Definition: AttributeArray.h:1921
StorageType * data()
Return the raw data buffer.
Definition: AttributeArray.h:779
ValuePtr mFiller
Definition: AttributeArray.h:875
void readPagedBuffers(compression::PagedInputStream &) override
Read attribute buffers from a paged stream.
Definition: AttributeArray.h:1790
typename T::ValueType ElementType
Definition: Types.h:208
AttributeArray()
Definition: AttributeArray.h:132
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:2095
size_t memUsage() const override
Return the number of bytes of memory used by this attribute.
Definition: AttributeArray.h:1390
virtual void expand(bool fill=true)=0
If this array is uniform, replace it with an array of length size().
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
Definition: AttributeArray.h:2075
TypedAttributeArray & operator=(const TypedAttributeArray &)
Definition: AttributeArray.h:1183
Typed class for storing attribute data.
Definition: AttributeArray.h:544
virtual bool isUniform() const =0
Return true if this array is stored as a single uniform value.
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
Definition: AttributeArray.h:2096
Definition: AttributeArray.h:835
static ValueType encode(const ValueType &value)
Definition: AttributeArray.h:504
bool valueTypeIsClass() const override
Return true if the value type is a class (ie vector, matrix or quaternion return true) ...
Definition: AttributeArray.h:1355
Write-able version of AttributeHandle.
Definition: AttributeArray.h:906
Definition: Exceptions.h:65
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:443
T(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:422
std::ostream & getOutputStream()
Set and get the output stream.
Definition: StreamCompression.h:257
Definition: AttributeArray.h:449
ValueType_ ValueType
Definition: AttributeArray.h:550
bool isDataLoaded() const override
Return true if all data has been loaded.
Definition: AttributeArray.h:1683
static TypedAttributeArray & cast(AttributeArray &attributeArray)
Cast an AttributeArray to TypedAttributeArray<T>
Definition: AttributeArray.h:1259
static fileSize_t write(std::ostream &os, const GridHandle< BufferT > &handle, Codec codec)
virtual ~AttributeHandle()
Definition: AttributeArray.h:2151
static Ptr create(const AttributeArray &array, const bool collapseOnDestruction=true)
Definition: AttributeArray.h:2116
static bool isRegistered(const NamePair &type, const ScopedRegistryLock *lock=nullptr)
Return true if the given attribute type name is registered.
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:629
SetterPtr mSetter
Definition: AttributeArray.h:430
bool sizeOnly() const
Definition: StreamCompression.h:254
virtual void loadData() const =0
Ensures all data is in-core.
AttributeHandle(const AttributeArray &array, const bool collapseOnDestruction=true)
Definition: AttributeArray.h:2123
virtual bool valueTypeIsFloatingPoint() const =0
Return true if the value type is floating point.
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...
Definition: AttributeArray.h:510
internal::half half
Definition: Types.h:29
T & z()
Definition: Vec3.h:87
Definition: Compression.h:56
static bool isRegistered()
Return true if this attribute type is registered.
Definition: AttributeArray.h:1223
static void read(std::istream &is, GridHandle< BufferT > &handle, Codec codec)
bool compact()
Compact the existing array to become uniform if all values are identical.
Definition: AttributeArray.h:2274
bool operator!=(const AttributeArray &other) const
Definition: AttributeArray.h:359
Index size() const
Definition: AttributeArray.h:858
static void registerType()
Register this attribute type along with a factory function.
Definition: AttributeArray.h:1231
Definition: Exceptions.h:57
Convenience wrappers to using Blosc and reading and writing of Paged data.
static void unregisterType(const NamePair &type, const ScopedRegistryLock *lock=nullptr)
Remove a attribute type from the registry.
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...
Definition: AttributeArray.h:96
OPENVDB_API size_t bloscCompressedSize(const char *buffer, const size_t uncompressedBytes)
Convenience wrapper to retrieve the compressed size of buffer when compressed.
void fill(const ValueType &value)
Fill the existing array with the given value.
Definition: AttributeArray.h:2286
ValueType get(Index n) const
Return the value at index n.
Definition: AttributeArray.h:1419
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:2074
std::shared_ptr< TypedAttributeArray > Ptr
Definition: AttributeArray.h:547
void loadData() const override
Ensures all data is in-core.
Definition: AttributeArray.h:1675
tbb::spin_mutex mMutex
Definition: AttributeArray.h:399
Definition: AttributeArray.h:472
static ValueType decode(const ValueType &value)
Definition: AttributeArray.h:496
static Ptr create(Index n, Index strideOrTotalSize=1, bool constantStride=true, const Metadata *metadata=nullptr)
Return a new attribute array of the given length n and stride with uniform value zero.
Definition: AttributeArray.h:1247
virtual Index storageTypeSize() const =0
Index size() const override
Return the number of elements in this array.
Definition: AttributeArray.h:617
AccessorBasePtr getAccessor() const override
Obtain an Accessor that stores getter and setter functors.
Definition: AttributeArray.h:2015
Definition: AttributeArray.h:114
Definition: AttributeArray.h:501
IntegerVectorT floatingPointToFixedPoint(const math::Vec3< FloatT > &v)
Definition: AttributeArray.h:69
void writePagedBuffers(compression::PagedOutputStream &os, bool outputTransient) const override
Definition: AttributeArray.h:1963
Codec_ Codec
Definition: AttributeArray.h:551
data is marked as strided when written
Definition: AttributeArray.h:111
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:85
int16_t Int16
Definition: Types.h:55
SerializationFlag
Definition: AttributeArray.h:109
Index stride() const override
Definition: AttributeArray.h:621
std::unique_ptr< PageHandle > Ptr
Definition: StreamCompression.h:172
static const char * name()
Definition: AttributeArray.h:476
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:125
bool isUniform() const
Definition: AttributeArray.h:2219
Definition: Exceptions.h:13
typename attribute_traits::TruncateTrait< T >::Type Type
Definition: AttributeArray.h:483
Name codecType() const override
Return the name of the codec used by this array (e.g., "trnc" or "fxpt").
Definition: AttributeArray.h:632
void fill(const ValueType &value)
Fill the existing array with the given value.
Definition: AttributeArray.h:1592
ValueT value
Definition: GridBuilder.h:1290
bool sizeOnly() const
Definition: StreamCompression.h:217
void(*)(AttributeArray *array, const Index n, const T &value) SetterPtr
Definition: AttributeArray.h:423
AttributeWriteHandle(AttributeArray &array, const bool expand=true)
Definition: AttributeArray.h:2243
uint16_t Type
Definition: AttributeArray.h:451
Definition: Exceptions.h:64
GetterPtr mGetter
Definition: AttributeArray.h:872
std::istream & getInputStream()
Definition: StreamCompression.h:220
bool validData() const
Verify that data is not out-of-core or in a partially-read state.
Definition: AttributeArray.h:783
virtual AccessorBasePtr getAccessor() const =0
Obtain an Accessor that stores getter and setter functors.
Definition: AttributeArray.h:110
void(*)(AttributeArray *array, const T &value) ValuePtr
Definition: AttributeArray.h:424
Accessor base class for AttributeArray storage where type is not available.
Definition: AttributeArray.h:415
PagedOutputStream & write(const char *str, std::streamsize n)
Writes the given.
virtual Index dataSize() const =0
ValuePtr mFiller
Definition: AttributeArray.h:432
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:473
void setUnsafe(Index n, const ValueType &value)
Set value at the given index n (assumes in-core)
Definition: AttributeArray.h:1456
FloatVectorT fixedPointToFloatingPoint(const math::Vec3< IntegerT > &v)
Definition: AttributeArray.h:79
PageHandle::Ptr createHandle(std::streamsize n)
Creates a PageHandle to access the next.
static const char * name()
Definition: AttributeArray.h:518
Definition: AttributeArray.h:525
bool isTransient() const
Return true if this attribute is not serialized during stream output.
Definition: AttributeArray.h:306
void write(std::ostream &os, bool outputTransient) const override
Definition: AttributeArray.h:1853
std::unique_ptr< Handle > UniquePtr
Definition: AttributeArray.h:840
size_t memUsageIfLoaded() const override
Definition: AttributeArray.h:1398
Base class for storing attribute data.
Definition: AttributeArray.h:92
GetterPtr mGetter
Definition: AttributeArray.h:429
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:39
typename attribute_traits::UIntTypeTrait< OneByte, T >::Type Type
Definition: AttributeArray.h:513
short Type
Definition: AttributeArray.h:443
ValuePtr mCollapser
Definition: AttributeArray.h:874
std::shared_ptr< AccessorBase > AccessorBasePtr
Definition: AttributeArray.h:98
~TypedAttributeArray() override
Definition: AttributeArray.h:581
static const char * name()
Definition: AttributeArray.h:503
AttributeArray::Ptr copy() const override
Definition: AttributeArray.h:1279
static void registerType(const NamePair &type, FactoryMethod, const ScopedRegistryLock *lock=nullptr)
Register a attribute type along with a factory function.
uint8_t mFlags
Definition: AttributeArray.h:400
bool isStreaming() const
Return true if this attribute is in streaming mode.
Definition: AttributeArray.h:314
void setConstantStride(bool state)
Specify whether this attribute has a constant stride or not.
compression::PageHandle::Ptr mPageHandle
Definition: AttributeArray.h:405
Accessor to call unsafe get and set methods based on templated Codec and Value.
Definition: AttributeArray.h:2072
T Type
Definition: AttributeArray.h:472
const StorageType * data() const
Definition: AttributeArray.h:780
virtual Index stride() const =0
AttributeArray & array()
Definition: AttributeArray.h:2312
Index valueTypeSize() const override
Return the size in bytes of the value type of a single element in this array.
Definition: AttributeArray.h:635
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form 'someVar << "some text" << ...'.
Definition: logging.h:256
void collapse() override
Replace the existing array with a uniform zero value.
Definition: AttributeArray.h:1562
void collapse()
Replace the existing array with a uniform value (zero if none provided).
Definition: AttributeArray.h:2268
std::shared_ptr< Handle > Ptr
Definition: AttributeArray.h:910
Index stride() const
Definition: AttributeArray.h:857
void set(Index n, const ValueType &value)
Definition: AttributeArray.h:2250
Index(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:843
static const char * name()
Definition: AttributeArray.h:487
Definition: AttributeArray.h:480
Definition: AttributeArray.h:530
void readBuffers(std::istream &) override
Read attribute buffers from a stream.
Definition: AttributeArray.h:1751
bool hasConstantStride() const
Return true if this attribute has a constant stride.
Definition: AttributeArray.h:317
void set(Index n, const ValueType &value)
Set value at the given index n.
Definition: AttributeArray.h:1471
Ptr(*)(Index, Index, bool, const Metadata *) FactoryMethod
Definition: AttributeArray.h:128
Definition: AttributeArray.h:118
virtual ~AttributeArray()
Definition: AttributeArray.h:133
Definition: Exceptions.h:58
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.
A Paging wrapper to std::istream that is responsible for reading from a given input stream and creati...
Definition: StreamCompression.h:206
Definition: AttributeArray.h:469
const char * constDataAsByteArray() const
Indirect virtual function to retrieve the data buffer cast to a char byte array.
Definition: AttributeArray.h:363
uint8_t flags() const
Retrieve the attribute array flags.
Definition: AttributeArray.h:320
virtual bool isDataLoaded() const =0
Return true if all data has been loaded.
uint8_t mUsePagedRead
Definition: AttributeArray.h:401
Index dataSize() const override
Return the size of the data in this array.
Definition: AttributeArray.h:624
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
const AttributeArray & array() const
Definition: AttributeArray.h:2178
void read(std::istream &) override
Read attribute data from a stream.
Definition: AttributeArray.h:1691
static const NamePair & attributeType()
Return the name of this attribute's type (includes codec)
Definition: AttributeArray.h:1210
bool valueTypeIsMatrix() const override
Return true if the value type is a matrix.
Definition: AttributeArray.h:1381
T & y()
Definition: Vec3.h:86
bool compact() override
Compact the existing array to become uniform if all values are identical.
Definition: AttributeArray.h:1545
std::unique_ptr< Handle > ScopedPtr
Definition: AttributeArray.h:911
Flag
Definition: AttributeArray.h:101
bool hasConstantStride() const
Definition: AttributeArray.h:2225
const StorageType * constData() const
Return the raw data buffer.
Definition: AttributeArray.h:772
void(*)(AttributeArray *array, const Index n, const Index &value) SetterPtr
Definition: AttributeArray.h:844
const AttributeArray * mArray
Definition: AttributeArray.h:870
SetterPtr mSetter
Definition: AttributeArray.h:873
Accessor(GetterPtr getter, SetterPtr setter, ValuePtr collapser, ValuePtr filler)
Definition: AttributeArray.h:426
Definition: AttributeArray.h:466
static void unregisterType()
Remove this attribute type from the registry.
Definition: AttributeArray.h:1239
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:1133
std::string Name
Definition: Name.h:17
size_t mCompressedBytes
Definition: AttributeArray.h:406
const NamePair & type() const override
Return the name of this attribute's type.
Definition: AttributeArray.h:607
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212
static const char * name()
Definition: AttributeArray.h:494
uint16_t StorageType
Definition: AttributeArray.h:527
ValueType getUnsafe(Index n) const
Return the value at index n (assumes in-core)
Definition: AttributeArray.h:1407
streaming mode collapses attributes when first accessed
Definition: AttributeArray.h:106