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>
52 static_assert(std::is_unsigned<IntegerT>::value,
"IntegerT must be unsigned");
59 template <
typename FloatT,
typename IntegerT>
63 static_assert(std::is_unsigned<IntegerT>::value,
"IntegerT must be unsigned");
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;
781 inline bool validData()
const {
return !(isOutOfCore() || (flags() & PARTIALREAD)); }
784 friend class ::TestAttributeArray;
786 TypedAttributeArray(
const TypedAttributeArray&,
const tbb::spin_mutex::scoped_lock&);
789 inline void doLoad()
const;
791 #if OPENVDB_ABI_VERSION_NUMBER >= 10 792 inline void doLoadUnsafe()
const;
795 inline void doLoadUnsafe(
const bool compression =
true)
const;
797 inline bool compressUnsafe();
801 inline void setOutOfCore(
const bool);
807 char* dataAsByteArray()
override;
808 const char* dataAsByteArray()
const override;
810 size_t arrayMemUsage()
const;
817 return TypedAttributeArray::create(n, strideOrTotalSize, constantStride, metadata);
820 std::unique_ptr<StorageType[]> mData;
822 Index mStrideOrTotalSize;
831 template <
typename ValueType,
typename CodecType = UnknownCodec>
836 using Ptr = std::shared_ptr<Handle>;
845 static Ptr create(
const AttributeArray& array,
const bool collapseOnDestruction =
true);
857 bool isUniform()
const;
858 bool hasConstantStride()
const;
875 friend class ::TestAttributeArray;
877 template <
bool IsUnknownCodec>
878 typename std::enable_if<IsUnknownCodec, bool>::type compatibleType()
const;
880 template <
bool IsUnknownCodec>
881 typename std::enable_if<!IsUnknownCodec, bool>::type compatibleType()
const;
883 template <
bool IsUnknownCodec>
884 typename std::enable_if<IsUnknownCodec, ValueType>::type
get(
Index index)
const;
886 template <
bool IsUnknownCodec>
887 typename std::enable_if<!IsUnknownCodec, ValueType>::type
get(
Index index)
const;
892 Index mStrideOrTotalSize;
894 bool mCollapseOnDestruction;
902 template <
typename ValueType,
typename CodecType = UnknownCodec>
907 using Ptr = std::shared_ptr<Handle>;
918 void expand(
bool fill =
true);
922 void collapse(
const ValueType& uniformValue);
929 void fill(
const ValueType& value);
931 void set(
Index n,
const ValueType& value);
932 void set(
Index n,
Index m,
const ValueType& value);
937 friend class ::TestAttributeArray;
939 template <
bool IsUnknownCodec>
940 typename std::enable_if<IsUnknownCodec, void>::type
set(
Index index,
const ValueType& value)
const;
942 template <
bool IsUnknownCodec>
943 typename std::enable_if<!IsUnknownCodec, void>::type
set(
Index index,
const ValueType& value)
const;
953 template<
typename ValueType>
955 NullCodec::decode(
const ValueType& data, ValueType& val)
961 template<
typename ValueType>
963 NullCodec::encode(
const ValueType& val, ValueType& data)
969 template<
typename StorageType,
typename ValueType>
971 TruncateCodec::decode(
const StorageType& data, ValueType& val)
973 val =
static_cast<ValueType
>(data);
977 template<
typename StorageType,
typename ValueType>
979 TruncateCodec::encode(
const ValueType& val, StorageType& data)
981 data =
static_cast<StorageType
>(val);
985 template <
bool OneByte,
typename Range>
986 template<
typename StorageType,
typename ValueType>
990 val = fixedPointToFloatingPoint<ValueType>(data);
994 val = Range::template decode<ValueType>(val);
998 template <
bool OneByte,
typename Range>
999 template<
typename StorageType,
typename ValueType>
1005 const ValueType newVal = Range::template encode<ValueType>(val);
1007 data = floatingPointToFixedPoint<StorageType>(newVal);
1011 template<
typename T>
1015 val = math::QuantizedUnitVec::unpack(data);
1019 template<
typename T>
1023 data = math::QuantizedUnitVec::pack(val);
1031 template <
typename IterT>
1032 void AttributeArray::doCopyValues(
const AttributeArray& sourceArray,
const IterT& iter,
1038 assert(sourceArray.
isDataLoaded() && this->isDataLoaded());
1040 assert(this->storageTypeSize()*this->stride() ==
1044 const char*
const sourceBuffer = sourceArray.dataAsByteArray();
1045 char*
const targetBuffer = this->dataAsByteArray();
1046 assert(sourceBuffer && targetBuffer);
1048 if (rangeChecking && this->isUniform()) {
1052 const bool sourceIsUniform = sourceArray.
isUniform();
1054 const Index sourceDataSize = rangeChecking ? sourceArray.
dataSize() : 0;
1055 const Index targetDataSize = rangeChecking ? this->dataSize() : 0;
1057 for (IterT it(iter); it; ++it) {
1058 const Index sourceIndex = sourceIsUniform ? 0 : it.sourceIndex();
1059 const Index targetIndex = it.targetIndex();
1061 if (rangeChecking) {
1062 if (sourceIndex >= sourceDataSize) {
1064 "Cannot copy array data as source index exceeds size of source array.");
1066 if (targetIndex >= targetDataSize) {
1068 "Cannot copy array data as target index exceeds size of target array.");
1072 assert(sourceIndex < sourceArray.
dataSize());
1073 assert(targetIndex < this->dataSize());
1074 if (this->isUniform()) assert(targetIndex ==
Index(0));
1077 const size_t targetOffset(targetIndex * bytes);
1078 const size_t sourceOffset(sourceIndex * bytes);
1080 std::memcpy(targetBuffer + targetOffset, sourceBuffer + sourceOffset, bytes);
1084 template <
typename IterT>
1085 void AttributeArray::copyValuesUnsafe(
const AttributeArray& sourceArray,
const IterT& iter)
1087 this->doCopyValues(sourceArray, iter,
false);
1090 template <
typename IterT>
1091 void AttributeArray::copyValues(
const AttributeArray& sourceArray,
const IterT& iter,
1095 if (bytes != this->storageTypeSize()) {
1112 this->doCopyValues(sourceArray, iter,
true);
1126 template<
typename ValueType_,
typename Codec_>
1132 , mStrideOrTotalSize(strideOrTotalSize)
1134 if (constantStride) {
1136 if (strideOrTotalSize == 0) {
1138 "stride to be at least one.")
1143 if (mStrideOrTotalSize < n) {
1145 "a total size of at least the number of elements in the array.")
1149 mStrideOrTotalSize =
std::max(
Index(1), mStrideOrTotalSize);
1150 Codec::encode(uniformValue, this->
data()[0]);
1154 template<
typename ValueType_,
typename Codec_>
1161 template<
typename ValueType_,
typename Codec_>
1163 const tbb::spin_mutex::scoped_lock& lock)
1166 , mStrideOrTotalSize(rhs.mStrideOrTotalSize)
1170 std::memcpy(static_cast<void*>(this->
data()), rhs.
data(), this->arrayMemUsage());
1175 template<
typename ValueType_,
typename Codec_>
1181 tbb::spin_mutex::scoped_lock lock(
mMutex);
1182 tbb::spin_mutex::scoped_lock rhsLock(rhs.
mMutex);
1189 mStrideOrTotalSize = rhs.mStrideOrTotalSize;
1194 std::memcpy(static_cast<void*>(this->
data()), rhs.
data(), this->arrayMemUsage());
1202 template<
typename ValueType_,
typename Codec_>
1207 return NamePair(typeNameAsString<ValueType>(), Codec::name());
1213 template<
typename ValueType_,
typename Codec_>
1221 template<
typename ValueType_,
typename Codec_>
1229 template<
typename ValueType_,
typename Codec_>
1237 template<
typename ValueType_,
typename Codec_>
1246 typedMetadata ? typedMetadata->
value() : zeroVal<ValueType>()));
1249 template<
typename ValueType_,
typename Codec_>
1259 template<
typename ValueType_,
typename Codec_>
1269 template<
typename ValueType_,
typename Codec_>
1277 #if OPENVDB_ABI_VERSION_NUMBER < 10 1278 template<
typename ValueType_,
typename Codec_>
1282 return this->
copy();
1286 template<
typename ValueType_,
typename Codec_>
1296 template<
typename ValueType_,
typename Codec_>
1312 template<
typename ValueType_,
typename Codec_>
1318 this->setOutOfCore(
false);
1321 if (mData) mData.reset();
1325 template<
typename ValueType_,
typename Codec_>
1331 if (std::is_same<ValueType, Quats>::value ||
1332 std::is_same<ValueType, Quatd>::value ||
1333 std::is_same<ValueType, Mat3s>::value ||
1334 std::is_same<ValueType, Mat3d>::value ||
1335 std::is_same<ValueType, Mat4s>::value ||
1336 std::is_same<ValueType, Mat4d>::value)
return true;
1341 return std::is_floating_point<ElementT>::value || std::is_same<math::half, ElementT>::value;
1345 template<
typename ValueType_,
typename Codec_>
1350 return std::is_class<ValueType>::value && !std::is_same<math::half, ValueType>::value;
1354 template<
typename ValueType_,
typename Codec_>
1362 template<
typename ValueType_,
typename Codec_>
1367 return !this->
valueType().compare(0, 4,
"quat");
1371 template<
typename ValueType_,
typename Codec_>
1376 return !this->
valueType().compare(0, 3,
"mat");
1380 template<
typename ValueType_,
typename Codec_>
1384 return sizeof(*this) + (bool(mData) ? this->arrayMemUsage() : 0);
1387 #if OPENVDB_ABI_VERSION_NUMBER >= 10 1388 template<
typename ValueType_,
typename Codec_>
1397 template<
typename ValueType_,
typename Codec_>
1409 template<
typename ValueType_,
typename Codec_>
1420 template<
typename ValueType_,
typename Codec_>
1421 template<
typename T>
1425 val =
static_cast<T
>(this->
getUnsafe(n));
1429 template<
typename ValueType_,
typename Codec_>
1430 template<
typename T>
1434 val =
static_cast<T
>(this->
get(n));
1438 template<
typename ValueType_,
typename Codec_>
1446 template<
typename ValueType_,
typename Codec_>
1461 template<
typename ValueType_,
typename Codec_>
1473 template<
typename ValueType_,
typename Codec_>
1474 template<
typename T>
1478 this->
setUnsafe(n, static_cast<ValueType>(val));
1482 template<
typename ValueType_,
typename Codec_>
1483 template<
typename T>
1487 this->
set(n,
static_cast<ValueType>(val));
1491 template<
typename ValueType_,
typename Codec_>
1499 #if OPENVDB_ABI_VERSION_NUMBER < 10 1500 template<
typename ValueType_,
typename Codec_>
1507 sourceTypedArray.
get(sourceIndex, sourceValue);
1509 this->
set(n, sourceValue);
1514 template<
typename ValueType_,
typename Codec_>
1523 tbb::spin_mutex::scoped_lock lock(
mMutex);
1535 template<
typename ValueType_,
typename Codec_>
1542 const ValueType_ val = this->
get(0);
1552 template<
typename ValueType_,
typename Codec_>
1556 this->
collapse(zeroVal<ValueType>());
1560 template<
typename ValueType_,
typename Codec_>
1565 tbb::spin_mutex::scoped_lock lock(
mMutex);
1570 Codec::encode(uniformValue, this->
data()[0]);
1574 template<
typename ValueType_,
typename Codec_>
1582 template<
typename ValueType_,
typename Codec_>
1587 tbb::spin_mutex::scoped_lock lock(
mMutex);
1594 Codec::encode(value, this->
data()[i]);
1599 template<
typename ValueType_,
typename Codec_>
1607 #if OPENVDB_ABI_VERSION_NUMBER < 10 1608 template<
typename ValueType_,
typename Codec_>
1616 template<
typename ValueType_,
typename Codec_>
1624 template<
typename ValueType_,
typename Codec_>
1633 template<
typename ValueType_,
typename Codec_>
1641 template<
typename ValueType_,
typename Codec_>
1649 template<
typename ValueType_,
typename Codec_>
1660 tbb::spin_mutex::scoped_lock lock(self->mMutex);
1661 this->doLoadUnsafe();
1665 template<
typename ValueType_,
typename Codec_>
1673 template<
typename ValueType_,
typename Codec_>
1681 template<
typename ValueType_,
typename Codec_>
1690 template<
typename ValueType_,
typename Codec_>
1697 is.read(reinterpret_cast<char*>(&bytes),
sizeof(
Index64));
1698 bytes = bytes -
sizeof(
Int16) -
sizeof(
Index);
1700 uint8_t
flags = uint8_t(0);
1701 is.read(reinterpret_cast<char*>(&flags),
sizeof(uint8_t));
1704 uint8_t serializationFlags = uint8_t(0);
1705 is.read(reinterpret_cast<char*>(&serializationFlags),
sizeof(uint8_t));
1708 is.read(reinterpret_cast<char*>(&size),
sizeof(
Index));
1717 if (serializationFlags >= 0x10) {
1732 is.read(reinterpret_cast<char*>(&stride),
sizeof(
Index));
1733 mStrideOrTotalSize =
stride;
1736 mStrideOrTotalSize = 1;
1741 template<
typename ValueType_,
typename Codec_>
1750 tbb::spin_mutex::scoped_lock lock(
mMutex);
1754 uint8_t bloscCompressed(0);
1755 if (!
mIsUniform) is.read(reinterpret_cast<char*>(&bloscCompressed),
sizeof(uint8_t));
1760 mCompressedBytes = 0;
1765 if (bloscCompressed == uint8_t(1)) {
1771 if (newBuffer) buffer.reset(newBuffer.release());
1776 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1780 template<
typename ValueType_,
typename Codec_>
1789 #ifdef OPENVDB_USE_DELAYED_LOADING 1792 io::MappedFile::Ptr mappedFile = io::getMappedFilePtr(is.
getInputStream());
1793 const bool delayLoad = (mappedFile.get() !=
nullptr);
1808 tbb::spin_mutex::scoped_lock lock(
mMutex);
1812 #ifdef OPENVDB_USE_DELAYED_LOADING 1813 this->setOutOfCore(delayLoad);
1817 #endif // OPENVDB_USE_DELAYED_LOADING 1819 #ifdef OPENVDB_USE_DELAYED_LOADING 1822 std::unique_ptr<char[]> buffer =
mPageHandle->read();
1823 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1825 #ifdef OPENVDB_USE_DELAYED_LOADING 1835 template<
typename ValueType_,
typename Codec_>
1839 this->
write(os,
false);
1843 template<
typename ValueType_,
typename Codec_>
1852 template<
typename ValueType_,
typename Codec_>
1856 if (!outputTransient && this->
isTransient())
return;
1863 uint8_t serializationFlags(0);
1865 Index strideOrTotalSize(mStrideOrTotalSize);
1866 bool strideOfOne(this->
stride() == 1);
1871 if (bloscCompression) this->doLoad();
1873 size_t compressedBytes = 0;
1883 if (bloscCompression && paged) serializationFlags |=
WRITEPAGED;
1885 else if (bloscCompression)
1889 const char* charBuffer =
reinterpret_cast<const char*
>(this->
data());
1890 const size_t inBytes = this->arrayMemUsage();
1897 bytes += (compressedBytes > 0) ? compressedBytes : this->arrayMemUsage();
1901 os.write(reinterpret_cast<const char*>(&bytes),
sizeof(
Index64));
1902 os.write(reinterpret_cast<const char*>(&flags),
sizeof(uint8_t));
1903 os.write(reinterpret_cast<const char*>(&serializationFlags),
sizeof(uint8_t));
1904 os.write(reinterpret_cast<const char*>(&size),
sizeof(
Index));
1907 if (!strideOfOne) os.write(reinterpret_cast<const char*>(&strideOrTotalSize),
sizeof(
Index));
1911 template<
typename ValueType_,
typename Codec_>
1915 if (!outputTransient && this->
isTransient())
return;
1924 os.write(reinterpret_cast<const char*>(this->
data()),
sizeof(
StorageType));
1928 std::unique_ptr<char[]> compressedBuffer;
1929 size_t compressedBytes = 0;
1930 const char* charBuffer =
reinterpret_cast<const char*
>(this->
data());
1931 const size_t inBytes = this->arrayMemUsage();
1933 if (compressedBuffer) {
1934 uint8_t bloscCompressed(1);
1935 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1936 os.write(reinterpret_cast<const char*>(compressedBuffer.get()), compressedBytes);
1939 uint8_t bloscCompressed(0);
1940 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1941 os.write(reinterpret_cast<const char*>(this->
data()), inBytes);
1946 uint8_t bloscCompressed(0);
1947 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1948 os.write(reinterpret_cast<const char*>(this->
data()), this->arrayMemUsage());
1953 template<
typename ValueType_,
typename Codec_>
1957 if (!outputTransient && this->
isTransient())
return;
1961 if (!bloscCompression) {
1972 os.
write(reinterpret_cast<const char*>(this->
data()), this->arrayMemUsage());
1976 template<
typename ValueType_,
typename Codec_>
1978 #if OPENVDB_ABI_VERSION_NUMBER >= 10 1990 assert(self->mPageHandle);
1993 std::unique_ptr<char[]> buffer =
self->mPageHandle->read();
1995 self->mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1997 self->mPageHandle.reset();
2001 self->mOutOfCore =
false;
2005 template<
typename ValueType_,
typename Codec_>
2020 template<
typename ValueType_,
typename Codec_>
2025 if(!otherT)
return false;
2026 if(this->mSize != otherT->mSize ||
2027 this->mStrideOrTotalSize != otherT->mStrideOrTotalSize ||
2029 this->attributeType() != this->
attributeType())
return false;
2035 if (!target && !source)
return true;
2036 if (!target || !source)
return false;
2043 template<
typename ValueType_,
typename Codec_>
2047 return reinterpret_cast<char*
>(this->
data());
2051 template<
typename ValueType_,
typename Codec_>
2055 return reinterpret_cast<const char*
>(this->
data());
2063 template <
typename CodecType,
typename ValueType>
2084 template <
typename ValueType>
2092 return (*functor)(array, n);
2097 (*functor)(array, n, value);
2106 template <
typename ValueType,
typename CodecType>
2114 template <
typename ValueType,
typename CodecType>
2119 , mCollapseOnDestruction(collapseOnDestruction && array.
isStreaming())
2121 if (!this->compatibleType<std::is_same<CodecType, UnknownCodec>::value>()) {
2142 template <
typename ValueType,
typename CodecType>
2149 template <
typename ValueType,
typename CodecType>
2150 template <
bool IsUnknownCodec>
2151 typename std::enable_if<IsUnknownCodec, bool>::type
2159 template <
typename ValueType,
typename CodecType>
2160 template <
bool IsUnknownCodec>
2161 typename std::enable_if<!IsUnknownCodec, bool>::type
2169 template <
typename ValueType,
typename CodecType>
2176 template <
typename ValueType,
typename CodecType>
2180 assert(index < (mSize * mStrideOrTotalSize));
2184 template <
typename ValueType,
typename CodecType>
2187 return this->get<std::is_same<CodecType, UnknownCodec>::value>(this->
index(n, m));
2190 template <
typename ValueType,
typename CodecType>
2191 template <
bool IsUnknownCodec>
2192 typename std::enable_if<IsUnknownCodec, ValueType>::type
2200 template <
typename ValueType,
typename CodecType>
2201 template <
bool IsUnknownCodec>
2202 typename std::enable_if<!IsUnknownCodec, ValueType>::type
2210 template <
typename ValueType,
typename CodecType>
2216 template <
typename ValueType,
typename CodecType>
2226 template <
typename ValueType,
typename CodecType>
2234 template <
typename ValueType,
typename CodecType>
2238 if (expand) array.
expand();
2241 template <
typename ValueType,
typename CodecType>
2244 this->set<std::is_same<CodecType, UnknownCodec>::value>(this->
index(n, 0), value);
2247 template <
typename ValueType,
typename CodecType>
2250 this->set<std::is_same<CodecType, UnknownCodec>::value>(this->
index(n, m), value);
2253 template <
typename ValueType,
typename CodecType>
2259 template <
typename ValueType,
typename CodecType>
2265 template <
typename ValueType,
typename CodecType>
2271 template <
typename ValueType,
typename CodecType>
2277 template <
typename ValueType,
typename CodecType>
2280 this->
mFiller(const_cast<AttributeArray*>(this->
mArray), value);
2283 template <
typename ValueType,
typename CodecType>
2284 template <
bool IsUnknownCodec>
2285 typename std::enable_if<IsUnknownCodec, void>::type
2293 template <
typename ValueType,
typename CodecType>
2294 template <
bool IsUnknownCodec>
2295 typename std::enable_if<!IsUnknownCodec, void>::type
2303 template <
typename ValueType,
typename CodecType>
2315 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED 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:1127
void collapse() override
Replace the existing array with a uniform zero value.
Definition: AttributeArray.h:1554
Definition: Exceptions.h:65
Definition: AttributeArray.h:510
bool valueTypeIsQuaternion() const override
Return true if the value type is a quaternion.
Definition: AttributeArray.h:1364
bool hasConstantStride() const
Definition: AttributeArray.h:2217
const NamePair & type() const override
Return the name of this attribute's type.
Definition: AttributeArray.h:607
Definition: AttributeArray.h:480
static Ptr create(const AttributeArray &array, const bool collapseOnDestruction=true)
Definition: AttributeArray.h:2108
bool valueTypeIsFloatingPoint() const override
Return true if the value type is floating point.
Definition: AttributeArray.h:1327
static pnanovdb_uint32_t allocate(pnanovdb_uint32_t *poffset, pnanovdb_uint32_t size, pnanovdb_uint32_t alignment)
Definition: pnanovdb_validate_strides.h:20
static ValueType encode(const ValueType &value)
Definition: AttributeArray.h:504
StorageType Type
Definition: AttributeArray.h:530
GetterPtr mGetter
Definition: AttributeArray.h:869
FloatVectorT fixedPointToFloatingPoint(const math::Vec3< IntegerT > &v)
Definition: AttributeArray.h:79
ValueType get(Index n, Index m=0) const
Definition: AttributeArray.h:2185
bool hasConstantStride() const
Return true if this attribute has a constant stride.
Definition: AttributeArray.h:317
static const char * name()
Definition: AttributeArray.h:534
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
virtual bool isUniform() const =0
Return true if this array is stored as a single uniform value.
Flag
Definition: AttributeArray.h:101
SetterPtr mSetter
Definition: AttributeArray.h:430
compression::PageHandle::Ptr mPageHandle
Definition: AttributeArray.h:405
Definition: AttributeArray.h:441
void(*)(AttributeArray *array, const Index &value) ValuePtr
Definition: AttributeArray.h:842
void expand(bool fill=true) override
Replace the single value storage with an array of length size().
Definition: AttributeArray.h:1516
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:443
ValuePtr mFiller
Definition: AttributeArray.h:432
static bool isRegistered()
Return true if this attribute type is registered.
Definition: AttributeArray.h:1215
virtual ~AttributeHandle()
Definition: AttributeArray.h:2143
virtual bool isDataLoaded() const =0
Return true if all data has been loaded.
static bool isRegistered(const NamePair &type, const ScopedRegistryLock *lock=nullptr)
Return true if the given attribute type name is registered.
const AttributeArray & array() const
Definition: AttributeArray.h:2170
void writeMetadata(std::ostream &os, bool outputTransient, bool paged) const override
Definition: AttributeArray.h:1854
static void registerType()
Register this attribute type along with a factory function.
Definition: AttributeArray.h:1223
Accessor base class for AttributeArray storage where type is not available.
Definition: AttributeArray.h:415
static const char * name()
Definition: AttributeArray.h:476
static fileSize_t write(std::ostream &os, const GridHandle< BufferT > &handle, Codec codec, uint32_t n)
ValueType get(Index n) const
Return the value at index n.
Definition: AttributeArray.h:1411
std::shared_ptr< TypedAttributeArray > Ptr
Definition: AttributeArray.h:547
T Type
Definition: AttributeArray.h:472
Ptr(*)(Index, Index, bool, const Metadata *) FactoryMethod
Definition: AttributeArray.h:128
ValuePtr mFiller
Definition: AttributeArray.h:872
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:118
ValueType getUnsafe(Index n) const
Return the value at index n (assumes in-core)
Definition: AttributeArray.h:1399
internal::half half
Definition: Types.h:29
static ValueType decode(const ValueType &value)
Definition: AttributeArray.h:496
typename T::ValueType ElementType
Definition: Types.h:247
bool isUniform() const override
Return true if this array is stored as a single uniform value.
Definition: AttributeArray.h:700
std::shared_ptr< Handle > Ptr
Definition: AttributeArray.h:907
Definition: AttributeArray.h:832
uint16_t Type
Definition: AttributeArray.h:451
void(*)(AttributeArray *array, const T &value) ValuePtr
Definition: AttributeArray.h:424
Definition: AttributeArray.h:449
void readMetadata(std::istream &) override
Read attribute metadata from a stream.
Definition: AttributeArray.h:1692
void writeBuffers(std::ostream &os, bool outputTransient) const override
Definition: AttributeArray.h:1913
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
Definition: AttributeArray.h:2067
SerializationFlag
Definition: AttributeArray.h:109
typename attribute_traits::UIntTypeTrait< OneByte, T >::Type Type
Definition: AttributeArray.h:513
ValuePtr mCollapser
Definition: AttributeArray.h:431
ValuePtr mCollapser
Definition: AttributeArray.h:871
std::unique_ptr< Handle > ScopedPtr
Definition: AttributeArray.h:908
size_t memUsage() const override
Return the number of bytes of memory used by this attribute.
Definition: AttributeArray.h:1382
TypedAttributeArray & operator=(const TypedAttributeArray &)
Definition: AttributeArray.h:1177
bool isStreaming() const
Return true if this attribute is in streaming mode.
Definition: AttributeArray.h:314
std::unique_ptr< PageHandle > Ptr
Definition: StreamCompression.h:172
Accessor(GetterPtr getter, SetterPtr setter, ValuePtr collapser, ValuePtr filler)
Definition: AttributeArray.h:426
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:125
bool isUniform() const
Definition: AttributeArray.h:2211
Accessor to call unsafe get and set methods based on templated Codec and Value.
Definition: AttributeArray.h:2064
virtual ~AttributeArray()
Definition: AttributeArray.h:133
bool valueTypeIsClass() const override
Return true if the value type is a class (ie vector, matrix or quaternion return true) ...
Definition: AttributeArray.h:1347
Definition: Compression.h:56
ValueType_ ValueType
Definition: AttributeArray.h:550
PageHandle::Ptr createHandle(std::streamsize n)
Creates a PageHandle to access the next.
A Paging wrapper to std::ostream that is responsible for writing from a given output stream at interv...
Definition: StreamCompression.h:243
Name codecType() const override
Return the name of the codec used by this array (e.g., "trnc" or "fxpt").
Definition: AttributeArray.h:632
void expand(bool fill=true)
If this array is uniform, replace it with an array of length size().
Definition: AttributeArray.h:2254
Definition: AttributeArray.h:525
void collapse()
Replace the existing array with a uniform value (zero if none provided).
Definition: AttributeArray.h:2260
void fill(const ValueType &value)
Fill the existing array with the given value.
Definition: AttributeArray.h:1584
Base class for storing attribute data.
Definition: AttributeArray.h:92
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:39
Index(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:840
static ValueType decode(const ValueType &value)
Definition: AttributeArray.h:505
bool sizeOnly() const
Definition: StreamCompression.h:254
static void registerType(const NamePair &type, FactoryMethod, const ScopedRegistryLock *lock=nullptr)
Register a attribute type along with a factory function.
void setConstantStride(bool state)
Specify whether this attribute has a constant stride or not.
static const char * name()
Definition: AttributeArray.h:494
Definition: AttributeArray.h:513
uint64_t Index64
Definition: Types.h:53
typename Codec::template Storage< ValueType >::Type StorageType
Definition: AttributeArray.h:552
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:2066
Definition: AttributeArray.h:492
Convenience wrappers to using Blosc and reading and writing of Paged data.
static ValueType encode(const ValueType &value)
Definition: AttributeArray.h:495
Index size() const override
Return the number of elements in this array.
Definition: AttributeArray.h:617
GetterPtr mGetter
Definition: AttributeArray.h:429
AccessorBasePtr getAccessor() const override
Obtain an Accessor that stores getter and setter functors.
Definition: AttributeArray.h:2007
const char * constDataAsByteArray() const
Indirect virtual function to retrieve the data buffer cast to a char byte array.
Definition: AttributeArray.h:363
std::unique_ptr< Handle > UniquePtr
Definition: AttributeArray.h:837
std::ostream & getOutputStream()
Set and get the output stream.
Definition: StreamCompression.h:257
StorageType * data()
Return the raw data buffer.
Definition: AttributeArray.h:777
void set(Index n, const ValueType &value)
Definition: AttributeArray.h:2242
IntegerVectorT floatingPointToFixedPoint(const math::Vec3< FloatT > &v)
Definition: AttributeArray.h:69
virtual AccessorBasePtr getAccessor() const =0
Obtain an Accessor that stores getter and setter functors.
T & y()
Definition: Vec3.h:86
void write(std::ostream &os, bool outputTransient) const override
Definition: AttributeArray.h:1845
virtual void loadData() const =0
Ensures all data is in-core.
size_t memUsageIfLoaded() const override
Definition: AttributeArray.h:1390
Index size() const
Definition: AttributeArray.h:855
typename attribute_traits::TruncateTrait< T >::Type Type
Definition: AttributeArray.h:483
void loadData() const override
Ensures all data is in-core.
Definition: AttributeArray.h:1667
Definition: AttributeArray.h:114
Definition: AttributeArray.h:501
virtual Index dataSize() const =0
data is marked as strided when written
Definition: AttributeArray.h:111
~TypedAttributeArray() override
Definition: AttributeArray.h:581
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:1239
virtual Index stride() const =0
AttributeArray & array()
Definition: AttributeArray.h:2304
Definition: AttributeArray.h:466
Definition: AttributeArray.h:530
static const char * name()
Definition: AttributeArray.h:503
bool isDataLoaded() const override
Return true if all data has been loaded.
Definition: AttributeArray.h:1675
static TypedAttributeArray & cast(AttributeArray &attributeArray)
Cast an AttributeArray to TypedAttributeArray<T>
Definition: AttributeArray.h:1251
size_t mCompressedBytes
Definition: AttributeArray.h:406
SetterPtr mSetter
Definition: AttributeArray.h:870
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
uint8_t mUsePagedRead
Definition: AttributeArray.h:401
uint8_t Type
Definition: AttributeArray.h:450
void writePagedBuffers(compression::PagedOutputStream &os, bool outputTransient) const override
Definition: AttributeArray.h:1955
Codec_ Codec
Definition: AttributeArray.h:551
static const char * name()
Definition: AttributeArray.h:518
Definition: Exceptions.h:13
Definition: Exceptions.h:64
std::shared_ptr< AccessorBase > AccessorBasePtr
Definition: AttributeArray.h:98
bool validData() const
Verify that data is not out-of-core or in a partially-read state.
Definition: AttributeArray.h:781
AttributeArray()
Definition: AttributeArray.h:132
PagedOutputStream & write(const char *str, std::streamsize n)
Writes the given.
Typed class for storing attribute data.
Definition: AttributeArray.h:544
std::string Name
Definition: Name.h:19
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:2087
Definition: AttributeArray.h:110
void setUnsafe(Index n, const ValueType &value)
Set value at the given index n (assumes in-core)
Definition: AttributeArray.h:1448
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.
bool operator!=(const AttributeArray &other) const
Definition: AttributeArray.h:359
T(*)(const AttributeArray *array, const Index n) GetterPtr
Definition: AttributeArray.h:422
Index32 Index
Definition: Types.h:54
Definition: Exceptions.h:57
static void unregisterType(const NamePair &type, const ScopedRegistryLock *lock=nullptr)
Remove a attribute type from the registry.
Index dataSize() const override
Return the size of the data in this array.
Definition: AttributeArray.h:624
OPENVDB_API size_t bloscCompressedSize(const char *buffer, const size_t uncompressedBytes)
Convenience wrapper to retrieve the compressed size of buffer when compressed.
void read(std::istream &) override
Read attribute data from a stream.
Definition: AttributeArray.h:1683
bool hasValueType() const
Return true if this attribute has a value type the same as the template parameter.
Definition: AttributeArray.h:228
bool valueTypeIsMatrix() const override
Return true if the value type is a matrix.
Definition: AttributeArray.h:1373
bool mIsUniform
Definition: AttributeArray.h:398
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...
bool compact() override
Compact the existing array to become uniform if all values are identical.
Definition: AttributeArray.h:1537
static Ptr create(AttributeArray &array, const bool expand=true)
Definition: AttributeArray.h:2228
T & z()
Definition: Vec3.h:87
uint8_t flags() const
Retrieve the attribute array flags.
Definition: AttributeArray.h:320
short Type
Definition: AttributeArray.h:443
Index index(Index n, Index m) const
Definition: AttributeArray.h:2177
const StorageType * constData() const
Return the raw data buffer.
Definition: AttributeArray.h:771
static void read(std::istream &is, BufferT &buffer, Codec codec)
std::atomic< Index32 > mOutOfCore
Definition: AttributeArray.h:402
bool compact()
Compact the existing array to become uniform if all values are identical.
Definition: AttributeArray.h:2266
Index stride() const override
Definition: AttributeArray.h:621
bool isHidden() const
Return true if this attribute is hidden (e.g., from UI or iterators).
Definition: AttributeArray.h:299
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:224
Definition: AttributeArray.h:483
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form 'someVar << "some text" << ...'.
Definition: logging.h:256
virtual void expand(bool fill=true)=0
If this array is uniform, replace it with an array of length size().
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: AttributeArray.h:1635
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...
void(*)(AttributeArray *array, const Index n, const T &value) SetterPtr
Definition: AttributeArray.h:423
Definition: AttributeArray.h:472
A Paging wrapper to std::istream that is responsible for reading from a given input stream and creati...
Definition: StreamCompression.h:206
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
Definition: AttributeArray.h:2088
std::shared_ptr< Handle > Ptr
Definition: AttributeArray.h:836
static const char * name()
Definition: AttributeArray.h:487
Definition: Exceptions.h:58
std::shared_ptr< const AttributeArray > ConstPtr
Definition: AttributeArray.h:126
void readBuffers(std::istream &) override
Read attribute buffers from a stream.
Definition: AttributeArray.h:1743
Definition: AttributeArray.h:96
void fill(const ValueType &value)
Fill the existing array with the given value.
Definition: AttributeArray.h:2278
Index stride() const
Definition: AttributeArray.h:854
int16_t Int16
Definition: Types.h:55
AttributeHandle(const AttributeArray &array, const bool collapseOnDestruction=true)
Definition: AttributeArray.h:2115
Definition: AttributeArray.h:469
void set(Index n, const ValueType &value)
Set value at the given index n.
Definition: AttributeArray.h:1463
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
uint8_t mFlags
Definition: AttributeArray.h:400
tbb::spin_mutex mMutex
Definition: AttributeArray.h:399
uint16_t StorageType
Definition: AttributeArray.h:527
Write-able version of AttributeHandle.
Definition: AttributeArray.h:903
virtual Index storageTypeSize() const =0
static void unregisterType()
Remove this attribute type from the registry.
Definition: AttributeArray.h:1231
Index storageTypeSize() const override
Definition: AttributeArray.h:639
bool sizeOnly() const
Definition: StreamCompression.h:217
AttributeWriteHandle(AttributeArray &array, const bool expand=true)
Definition: AttributeArray.h:2235
bool isTransient() const
Return true if this attribute is not serialized during stream output.
Definition: AttributeArray.h:306
void(*)(AttributeArray *array, const Index n, const Index &value) SetterPtr
Definition: AttributeArray.h:841
static const NamePair & attributeType()
Return the name of this attribute's type (includes codec)
Definition: AttributeArray.h:1204
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:473
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:85
AttributeArray::Ptr copy() const override
Definition: AttributeArray.h:1271
std::istream & getInputStream()
Definition: StreamCompression.h:220
bool valueTypeIsVector() const override
Return true if the value type is a vector.
Definition: AttributeArray.h:1356
const AttributeArray * mArray
Definition: AttributeArray.h:867
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212
const StorageType * data() const
Definition: AttributeArray.h:778
virtual bool valueTypeIsFloatingPoint() const =0
Return true if the value type is floating point.
streaming mode collapses attributes when first accessed
Definition: AttributeArray.h:106
void readPagedBuffers(compression::PagedInputStream &) override
Read attribute buffers from a paged stream.
Definition: AttributeArray.h:1782
Index valueTypeSize() const override
Return the size in bytes of the value type of a single element in this array.
Definition: AttributeArray.h:635