10 #ifndef OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED
24 #include <tbb/spin_mutex.h>
30 #include <type_traits>
33 class TestAttributeArray;
49 template <
typename IntegerT,
typename FloatT>
60 template <
typename FloatT,
typename IntegerT>
68 template <
typename IntegerVectorT,
typename FloatT>
72 return IntegerVectorT(
73 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
x()),
74 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
y()),
75 floatingPointToFixedPoint<typename IntegerVectorT::ValueType>(v.
z()));
78 template <
typename FloatVectorT,
typename IntegerT>
83 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
x()),
84 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
y()),
85 fixedPointToFloatingPoint<typename FloatVectorT::ValueType>(v.
z()));
105 CONSTANTSTRIDE = 0x8,
113 WRITEMEMCOMPRESS = 0x4,
121 tbb::spin_mutex::scoped_lock lock;
126 using Ptr = std::shared_ptr<AttributeArray>;
127 using ConstPtr = std::shared_ptr<const AttributeArray>;
138 if (
mFlags & PARTIALREAD) mCompressedBytes = 0;
158 virtual Index dataSize()
const = 0;
161 virtual Name valueType()
const = 0;
164 virtual Name codecType()
const = 0;
168 virtual Index valueTypeSize()
const = 0;
172 virtual Index storageTypeSize()
const = 0;
175 virtual bool valueTypeIsFloatingPoint()
const = 0;
178 virtual bool valueTypeIsClass()
const = 0;
181 virtual bool valueTypeIsVector()
const = 0;
184 virtual bool valueTypeIsQuaternion()
const = 0;
187 virtual bool valueTypeIsMatrix()
const = 0;
190 virtual size_t memUsage()
const = 0;
202 bool constantStride =
true,
204 const ScopedRegistryLock* lock =
nullptr);
207 static bool isRegistered(
const NamePair&
type,
const ScopedRegistryLock* lock =
nullptr);
209 static void clearRegistry(
const ScopedRegistryLock* lock =
nullptr);
214 template<
typename AttributeArrayType>
215 bool isType()
const {
return this->
type() == AttributeArrayType::attributeType(); }
218 template<
typename ValueType>
244 template<
typename IterT>
245 void copyValuesUnsafe(
const AttributeArray& sourceArray,
const IterT& iter);
249 template<
typename IterT>
250 void copyValues(
const AttributeArray& sourceArray,
const IterT& iter,
bool compact =
true);
253 virtual bool isUniform()
const = 0;
256 virtual void expand(
bool fill =
true) = 0;
258 virtual void collapse() = 0;
260 virtual bool compact() = 0;
266 void setHidden(
bool state);
273 void setTransient(
bool state);
281 void setStreaming(
bool state);
292 virtual void read(std::istream&) = 0;
295 virtual void write(std::ostream&,
bool outputTransient)
const = 0;
297 virtual void write(std::ostream&)
const = 0;
300 virtual void readMetadata(std::istream&) = 0;
304 virtual void writeMetadata(std::ostream&,
bool outputTransient,
bool paged)
const = 0;
307 virtual void readBuffers(std::istream&) = 0;
310 virtual void writeBuffers(std::ostream&,
bool outputTransient)
const = 0;
319 virtual void loadData()
const = 0;
322 virtual bool isDataLoaded()
const = 0;
334 friend class ::TestAttributeArray;
341 virtual char* dataAsByteArray() = 0;
342 virtual const char* dataAsByteArray()
const = 0;
345 template <
typename IterT>
346 void doCopyValues(
const AttributeArray& sourceArray,
const IterT& iter,
347 bool rangeChecking =
true);
353 void setConstantStride(
bool state);
365 bool mIsUniform =
true;
368 uint8_t mUsePagedRead = 0;
386 template <
typename T>
406 namespace attribute_traits
438 template <
typename T>
443 static const char*
name() {
return "null"; }
449 template <
typename T>
452 template<
typename StorageType,
typename ValueType>
static void decode(
const StorageType&,
ValueType&);
453 template<
typename StorageType,
typename ValueType>
static void encode(
const ValueType&, StorageType&);
454 static const char*
name() {
return "trnc"; }
461 static const char*
name() {
return "fxpt"; }
470 static const char*
name() {
return "ufxpt"; }
476 template <
bool OneByte,
typename Range=PositionRange>
479 template <
typename T>
482 template<
typename StorageType,
typename ValueType>
static void decode(
const StorageType&,
ValueType&);
483 template<
typename StorageType,
typename ValueType>
static void encode(
const ValueType&, StorageType&);
486 static const std::string
Name = std::string(
Range::name()) + (OneByte ?
"8" :
"16");
496 template <
typename T>
501 static const char*
name() {
return "uvec"; }
510 template<
typename ValueType_,
typename Codec_ = NullCodec>
514 using Ptr = std::shared_ptr<TypedAttributeArray>;
515 using ConstPtr = std::shared_ptr<const TypedAttributeArray>;
525 const ValueType& uniformValue = zeroVal<ValueType>());
551 const Metadata* metadata =
nullptr);
669 void read(std::istream&)
override;
673 void write(std::ostream& os,
bool outputTransient)
const override;
675 void write(std::ostream&)
const override;
683 void writeMetadata(std::ostream& os,
bool outputTransient,
bool paged)
const override;
690 void writeBuffers(std::ostream& os,
bool outputTransient)
const override;
722 friend class ::TestAttributeArray;
727 inline void doLoad()
const;
729 inline void doLoadUnsafe()
const;
732 inline void setOutOfCore(
const bool);
738 char* dataAsByteArray()
override;
739 const char* dataAsByteArray()
const override;
741 size_t arrayMemUsage()
const;
751 std::unique_ptr<StorageType[]> mData;
753 Index mStrideOrTotalSize;
762 template <
typename ValueType,
typename CodecType = UnknownCodec>
767 using Ptr = std::shared_ptr<Handle>;
806 friend class ::TestAttributeArray;
808 template <
bool IsUnknownCodec>
811 template <
bool IsUnknownCodec>
814 template <
bool IsUnknownCodec>
817 template <
bool IsUnknownCodec>
820 Index mStrideOrTotalSize;
822 bool mCollapseOnDestruction;
830 template <
typename ValueType,
typename CodecType = UnknownCodec>
835 using Ptr = std::shared_ptr<Handle>;
865 friend class ::TestAttributeArray;
867 template <
bool IsUnknownCodec>
870 template <
bool IsUnknownCodec>
881 template<
typename ValueType>
889 template<
typename ValueType>
897 template<
typename StorageType,
typename ValueType>
905 template<
typename StorageType,
typename ValueType>
909 data =
static_cast<StorageType
>(
val);
913 template <
bool OneByte,
typename Range>
914 template<
typename StorageType,
typename ValueType>
918 val = fixedPointToFloatingPoint<ValueType>(
data);
922 val = Range::template decode<ValueType>(
val);
926 template <
bool OneByte,
typename Range>
927 template<
typename StorageType,
typename ValueType>
933 const ValueType newVal = Range::template encode<ValueType>(
val);
935 data = floatingPointToFixedPoint<StorageType>(newVal);
959 template <
typename IterT>
960 void AttributeArray::doCopyValues(
const AttributeArray& sourceArray,
const IterT& iter,
972 const char*
const sourceBuffer = sourceArray.dataAsByteArray();
973 char*
const targetBuffer = this->dataAsByteArray();
976 if (rangeChecking && this->
isUniform()) {
977 OPENVDB_THROW(IndexError,
"Cannot copy array data as target array is uniform.");
980 const bool sourceIsUniform = sourceArray.
isUniform();
982 const Index sourceDataSize = rangeChecking ? sourceArray.
dataSize() : 0;
983 const Index targetDataSize = rangeChecking ? this->
dataSize() : 0;
985 for (IterT it(iter); it; ++it) {
986 const Index sourceIndex = sourceIsUniform ? 0 : it.sourceIndex();
987 const Index targetIndex = it.targetIndex();
990 if (sourceIndex >= sourceDataSize) {
992 "Cannot copy array data as source index exceeds size of source array.");
994 if (targetIndex >= targetDataSize) {
996 "Cannot copy array data as target index exceeds size of target array.");
1005 const size_t targetOffset(targetIndex *
bytes);
1006 const size_t sourceOffset(sourceIndex *
bytes);
1008 std::memcpy(targetBuffer + targetOffset, sourceBuffer + sourceOffset,
bytes);
1012 template <
typename IterT>
1015 this->doCopyValues(sourceArray, iter,
false);
1018 template <
typename IterT>
1024 OPENVDB_THROW(TypeError,
"Cannot copy array data due to mis-match in storage type sizes.");
1040 this->doCopyValues(sourceArray, iter,
true);
1054 template<
typename ValueType_,
typename Codec_>
1060 , mStrideOrTotalSize(strideOrTotalSize)
1062 if (constantStride) {
1064 if (strideOrTotalSize == 0) {
1065 OPENVDB_THROW(ValueError,
"Creating a TypedAttributeArray with a constant stride requires that " \
1066 "stride to be at least one.")
1071 if (mStrideOrTotalSize < n) {
1072 OPENVDB_THROW(ValueError,
"Creating a TypedAttributeArray with a non-constant stride must have " \
1073 "a total size of at least the number of elements in the array.")
1077 mStrideOrTotalSize =
std::max(
Index(1), mStrideOrTotalSize);
1078 Codec::encode(uniformValue, this->
data()[0]);
1082 template<
typename ValueType_,
typename Codec_>
1089 template<
typename ValueType_,
typename Codec_>
1091 const tbb::spin_mutex::scoped_lock& lock)
1094 , mStrideOrTotalSize(rhs.mStrideOrTotalSize)
1098 std::memcpy(static_cast<void*>(this->
data()), rhs.
data(), this->arrayMemUsage());
1103 template<
typename ValueType_,
typename Codec_>
1104 TypedAttributeArray<ValueType_, Codec_>&
1109 tbb::spin_mutex::scoped_lock lock(mMutex);
1110 tbb::spin_mutex::scoped_lock rhsLock(rhs.
mMutex);
1117 mStrideOrTotalSize = rhs.mStrideOrTotalSize;
1120 if (this->validData()) {
1122 std::memcpy(static_cast<void*>(this->
data()), rhs.
data(), this->arrayMemUsage());
1130 template<
typename ValueType_,
typename Codec_>
1141 template<
typename ValueType_,
typename Codec_>
1149 template<
typename ValueType_,
typename Codec_>
1157 template<
typename ValueType_,
typename Codec_>
1165 template<
typename ValueType_,
typename Codec_>
1174 typedMetadata ? typedMetadata->
value() : zeroVal<ValueType>()));
1177 template<
typename ValueType_,
typename Codec_>
1187 template<
typename ValueType_,
typename Codec_>
1197 template<
typename ValueType_,
typename Codec_>
1205 template<
typename ValueType_,
typename Codec_>
1209 if (this->isOutOfCore())
return 0;
1211 return (mIsUniform ? 1 : this->dataSize()) *
sizeof(StorageType);
1215 template<
typename ValueType_,
typename Codec_>
1217 TypedAttributeArray<ValueType_, Codec_>::allocate()
1221 mData.reset(
new StorageType[1]);
1224 const size_t size(this->dataSize());
1226 mData.reset(
new StorageType[
size]);
1231 template<
typename ValueType_,
typename Codec_>
1233 TypedAttributeArray<ValueType_, Codec_>::deallocate()
1236 if (this->isOutOfCore()) {
1237 this->setOutOfCore(
false);
1238 this->mPageHandle.reset();
1240 if (mData) mData.reset();
1244 template<
typename ValueType_,
typename Codec_>
1264 template<
typename ValueType_,
typename Codec_>
1273 template<
typename ValueType_,
typename Codec_>
1281 template<
typename ValueType_,
typename Codec_>
1286 return !this->valueType().compare(0, 4,
"quat");
1290 template<
typename ValueType_,
typename Codec_>
1295 return !this->valueType().compare(0, 3,
"mat");
1299 template<
typename ValueType_,
typename Codec_>
1303 return sizeof(*this) + (
bool(mData) ? this->arrayMemUsage() : 0);
1307 template<
typename ValueType_,
typename Codec_>
1311 return sizeof(*this) + (mIsUniform ? 1 : this->dataSize()) *
sizeof(
StorageType);
1315 template<
typename ValueType_,
typename Codec_>
1322 Codec::decode(this->
data()[mIsUniform ? 0 : n], val);
1327 template<
typename ValueType_,
typename Codec_>
1331 if (n >= this->dataSize())
OPENVDB_THROW(IndexError,
"Out-of-range access.");
1332 if (this->isOutOfCore()) this->doLoad();
1334 return this->getUnsafe(n);
1338 template<
typename ValueType_,
typename Codec_>
1339 template<
typename T>
1343 val =
static_cast<T>(this->getUnsafe(n));
1347 template<
typename ValueType_,
typename Codec_>
1348 template<
typename T>
1352 val =
static_cast<T>(this->
get(
n));
1356 template<
typename ValueType_,
typename Codec_>
1364 template<
typename ValueType_,
typename Codec_>
1375 Codec::encode(val, this->
data()[mIsUniform ? 0 : n]);
1379 template<
typename ValueType_,
typename Codec_>
1383 if (n >= this->dataSize())
OPENVDB_THROW(IndexError,
"Out-of-range access.");
1384 if (this->isOutOfCore()) this->doLoad();
1385 if (this->isUniform()) this->expand();
1387 this->setUnsafe(n, val);
1391 template<
typename ValueType_,
typename Codec_>
1392 template<
typename T>
1396 this->setUnsafe(n, static_cast<ValueType>(val));
1400 template<
typename ValueType_,
typename Codec_>
1401 template<
typename T>
1405 this->
set(n, static_cast<ValueType>(val));
1409 template<
typename ValueType_,
typename Codec_>
1417 template<
typename ValueType_,
typename Codec_>
1421 if (!mIsUniform)
return;
1426 tbb::spin_mutex::scoped_lock lock(mMutex);
1433 for (
Index i = 0; i < this->dataSize(); ++i) this->
data()[i] =
val;
1438 template<
typename ValueType_,
typename Codec_>
1442 if (mIsUniform)
return true;
1445 const ValueType_
val = this->
get(0);
1446 for (
Index i = 1; i < this->dataSize(); i++) {
1450 this->collapse(this->
get(0));
1455 template<
typename ValueType_,
typename Codec_>
1459 this->collapse(zeroVal<ValueType>());
1463 template<
typename ValueType_,
typename Codec_>
1468 tbb::spin_mutex::scoped_lock lock(mMutex);
1473 Codec::encode(uniformValue, this->
data()[0]);
1477 template<
typename ValueType_,
typename Codec_>
1485 template<
typename ValueType_,
typename Codec_>
1489 if (this->isOutOfCore()) {
1490 tbb::spin_mutex::scoped_lock lock(mMutex);
1495 const Index size = mIsUniform ? 1 : this->dataSize();
1497 Codec::encode(value, this->
data()[i]);
1502 template<
typename ValueType_,
typename Codec_>
1510 template<
typename ValueType_,
typename Codec_>
1518 template<
typename ValueType_,
typename Codec_>
1526 template<
typename ValueType_,
typename Codec_>
1528 TypedAttributeArray<ValueType_, Codec_>::doLoad()
const
1530 if (!(this->isOutOfCore()))
return;
1532 TypedAttributeArray<ValueType_, Codec_>*
self =
1533 const_cast<TypedAttributeArray<ValueType_, Codec_>*
>(
this);
1537 tbb::spin_mutex::scoped_lock lock(self->mMutex);
1538 this->doLoadUnsafe();
1542 template<
typename ValueType_,
typename Codec_>
1550 template<
typename ValueType_,
typename Codec_>
1554 return !this->isOutOfCore();
1558 template<
typename ValueType_,
typename Codec_>
1562 this->readMetadata(is);
1563 this->readBuffers(is);
1567 template<
typename ValueType_,
typename Codec_>
1574 is.read(reinterpret_cast<char*>(&bytes),
sizeof(
Index64));
1575 bytes = bytes -
sizeof(
Int16) -
sizeof(
Index);
1577 uint8_t
flags = uint8_t(0);
1578 is.read(reinterpret_cast<char*>(&flags),
sizeof(uint8_t));
1581 uint8_t serializationFlags = uint8_t(0);
1582 is.read(reinterpret_cast<char*>(&serializationFlags),
sizeof(uint8_t));
1585 is.read(reinterpret_cast<char*>(&size),
sizeof(
Index));
1594 if (serializationFlags >= 0x10) {
1595 OPENVDB_THROW(IoError,
"Unknown attribute serialization flags for VDB file format.");
1600 mIsUniform = serializationFlags & WRITEUNIFORM;
1601 mUsePagedRead = serializationFlags & WRITEPAGED;
1602 mCompressedBytes = bytes;
1607 if (serializationFlags & WRITESTRIDED) {
1609 is.read(reinterpret_cast<char*>(&stride),
sizeof(
Index));
1610 mStrideOrTotalSize =
stride;
1613 mStrideOrTotalSize = 1;
1618 template<
typename ValueType_,
typename Codec_>
1622 if (mUsePagedRead) {
1624 OPENVDB_THROW(IoError,
"Cannot read paged AttributeArray buffers.");
1627 tbb::spin_mutex::scoped_lock lock(mMutex);
1631 uint8_t bloscCompressed(0);
1632 if (!mIsUniform) is.read(reinterpret_cast<char*>(&bloscCompressed),
sizeof(uint8_t));
1635 std::unique_ptr<char[]>
buffer(
new char[mCompressedBytes]);
1636 is.read(buffer.get(), mCompressedBytes);
1637 mCompressedBytes = 0;
1642 if (bloscCompressed == uint8_t(1)) {
1646 const size_t inBytes = this->dataSize() *
sizeof(
StorageType);
1648 if (newBuffer) buffer.reset(newBuffer.release());
1653 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1657 template<
typename ValueType_,
typename Codec_>
1661 if (!mUsePagedRead) {
1666 #ifdef OPENVDB_USE_DELAYED_LOADING
1669 io::MappedFile::Ptr mappedFile = io::getMappedFilePtr(is.
getInputStream());
1670 const bool delayLoad = (mappedFile.get() !=
nullptr);
1675 size_t compressedBytes(mCompressedBytes);
1676 mCompressedBytes = 0;
1685 tbb::spin_mutex::scoped_lock lock(mMutex);
1689 #ifdef OPENVDB_USE_DELAYED_LOADING
1690 this->setOutOfCore(delayLoad);
1691 is.
read(mPageHandle, std::streamsize(mPageHandle->size()), delayLoad);
1693 is.
read(mPageHandle, std::streamsize(mPageHandle->size()),
false);
1694 #endif // OPENVDB_USE_DELAYED_LOADING
1696 #ifdef OPENVDB_USE_DELAYED_LOADING
1699 std::unique_ptr<char[]>
buffer = mPageHandle->read();
1700 mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1701 mPageHandle.reset();
1702 #ifdef OPENVDB_USE_DELAYED_LOADING
1712 template<
typename ValueType_,
typename Codec_>
1716 this->write(os,
false);
1720 template<
typename ValueType_,
typename Codec_>
1724 this->writeMetadata(os, outputTransient,
false);
1725 this->writeBuffers(os, outputTransient);
1729 template<
typename ValueType_,
typename Codec_>
1733 if (!outputTransient && this->isTransient())
return;
1735 if (
mFlags & PARTIALREAD) {
1736 OPENVDB_THROW(IoError,
"Cannot write out a partially-read AttributeArray.");
1740 uint8_t serializationFlags(0);
1742 Index strideOrTotalSize(mStrideOrTotalSize);
1743 bool strideOfOne(this->
stride() == 1);
1748 if (bloscCompression) this->doLoad();
1750 size_t compressedBytes = 0;
1754 serializationFlags |= WRITESTRIDED;
1759 serializationFlags |= WRITEUNIFORM;
1760 if (bloscCompression && paged) serializationFlags |= WRITEPAGED;
1762 else if (bloscCompression)
1764 if (paged) serializationFlags |= WRITEPAGED;
1766 const char* charBuffer =
reinterpret_cast<const char*
>(this->
data());
1767 const size_t inBytes = this->arrayMemUsage();
1774 bytes += (compressedBytes > 0) ? compressedBytes : this->arrayMemUsage();
1778 os.write(reinterpret_cast<const char*>(&bytes),
sizeof(
Index64));
1779 os.write(reinterpret_cast<const char*>(&flags),
sizeof(uint8_t));
1780 os.write(reinterpret_cast<const char*>(&serializationFlags),
sizeof(uint8_t));
1781 os.write(reinterpret_cast<const char*>(&size),
sizeof(
Index));
1784 if (!strideOfOne) os.write(reinterpret_cast<const char*>(&strideOrTotalSize),
sizeof(
Index));
1788 template<
typename ValueType_,
typename Codec_>
1792 if (!outputTransient && this->isTransient())
return;
1794 if (
mFlags & PARTIALREAD) {
1795 OPENVDB_THROW(IoError,
"Cannot write out a partially-read AttributeArray.");
1800 if (this->isUniform()) {
1801 os.write(reinterpret_cast<const char*>(this->
data()),
sizeof(
StorageType));
1805 std::unique_ptr<char[]> compressedBuffer;
1806 size_t compressedBytes = 0;
1807 const char* charBuffer =
reinterpret_cast<const char*
>(this->
data());
1808 const size_t inBytes = this->arrayMemUsage();
1810 if (compressedBuffer) {
1811 uint8_t bloscCompressed(1);
1812 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1813 os.write(reinterpret_cast<const char*>(compressedBuffer.get()), compressedBytes);
1816 uint8_t bloscCompressed(0);
1817 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1818 os.write(reinterpret_cast<const char*>(this->
data()), inBytes);
1823 uint8_t bloscCompressed(0);
1824 os.write(reinterpret_cast<const char*>(&bloscCompressed),
sizeof(uint8_t));
1825 os.write(reinterpret_cast<const char*>(this->
data()), this->arrayMemUsage());
1830 template<
typename ValueType_,
typename Codec_>
1834 if (!outputTransient && this->isTransient())
return;
1838 if (!bloscCompression) {
1843 if (
mFlags & PARTIALREAD) {
1844 OPENVDB_THROW(IoError,
"Cannot write out a partially-read AttributeArray.");
1849 os.
write(reinterpret_cast<const char*>(this->
data()), this->arrayMemUsage());
1853 template<
typename ValueType_,
typename Codec_>
1857 if (!(this->isOutOfCore()))
return;
1866 std::unique_ptr<char[]>
buffer =
self->mPageHandle->read();
1868 self->mData.reset(reinterpret_cast<StorageType*>(buffer.release()));
1870 self->mPageHandle.reset();
1874 self->mOutOfCore =
false;
1878 template<
typename ValueType_,
typename Codec_>
1893 template<
typename ValueType_,
typename Codec_>
1898 if(!otherT)
return false;
1899 if(this->mSize != otherT->mSize ||
1900 this->mStrideOrTotalSize != otherT->mStrideOrTotalSize ||
1902 this->attributeType() != this->attributeType())
return false;
1908 if (!target && !
source)
return true;
1909 if (!target || !
source)
return false;
1910 Index n = this->mIsUniform ? 1 : mSize;
1916 template<
typename ValueType_,
typename Codec_>
1918 TypedAttributeArray<ValueType_, Codec_>::dataAsByteArray()
1920 return reinterpret_cast<char*
>(this->
data());
1924 template<
typename ValueType_,
typename Codec_>
1926 TypedAttributeArray<ValueType_, Codec_>::dataAsByteArray()
const
1928 return reinterpret_cast<const char*
>(this->
data());
1936 template <
typename CodecType,
typename ValueType>
1957 template <
typename ValueType>
1965 return (*functor)(array,
n);
1970 (*functor)(array,
n,
value);
1979 template <
typename ValueType,
typename CodecType>
1980 typename AttributeHandle<ValueType, CodecType>::Ptr
1987 template <
typename ValueType,
typename CodecType>
1990 , mStrideOrTotalSize(array.hasConstantStride() ? array.
stride() : 1)
1991 , mSize(array.hasConstantStride() ? array.
size() : array.dataSize())
1992 , mCollapseOnDestruction(collapseOnDestruction && array.isStreaming())
1995 OPENVDB_THROW(TypeError,
"Cannot bind handle due to incompatible type of AttributeArray.");
2015 template <
typename ValueType,
typename CodecType>
2019 if (mCollapseOnDestruction)
const_cast<AttributeArray*
>(this->mArray)->collapse();
2022 template <
typename ValueType,
typename CodecType>
2023 template <
bool IsUnknownCodec>
2029 return mArray->hasValueType<
ValueType>();
2032 template <
typename ValueType,
typename CodecType>
2033 template <
bool IsUnknownCodec>
2035 AttributeHandle<ValueType, CodecType>::compatibleType()
const
2039 return mArray->isType<TypedAttributeArray<ValueType, CodecType>>();
2042 template <
typename ValueType,
typename CodecType>
2049 template <
typename ValueType,
typename CodecType>
2057 template <
typename ValueType,
typename CodecType>
2063 template <
typename ValueType,
typename CodecType>
2064 template <
bool IsUnknownCodec>
2070 return (*mGetter)(mArray, index);
2073 template <
typename ValueType,
typename CodecType>
2074 template <
bool IsUnknownCodec>
2083 template <
typename ValueType,
typename CodecType>
2086 return mArray->isUniform();
2089 template <
typename ValueType,
typename CodecType>
2092 return mArray->hasConstantStride();
2099 template <
typename ValueType,
typename CodecType>
2107 template <
typename ValueType,
typename CodecType>
2111 if (expand) array.
expand();
2114 template <
typename ValueType,
typename CodecType>
2120 template <
typename ValueType,
typename CodecType>
2126 template <
typename ValueType,
typename CodecType>
2132 template <
typename ValueType,
typename CodecType>
2138 template <
typename ValueType,
typename CodecType>
2144 template <
typename ValueType,
typename CodecType>
2147 this->mCollapser(const_cast<AttributeArray*>(this->mArray), uniformValue);
2150 template <
typename ValueType,
typename CodecType>
2153 this->mFiller(const_cast<AttributeArray*>(this->mArray), value);
2156 template <
typename ValueType,
typename CodecType>
2157 template <
bool IsUnknownCodec>
2163 (*this->mSetter)(const_cast<AttributeArray*>(this->mArray), index,
value);
2166 template <
typename ValueType,
typename CodecType>
2167 template <
bool IsUnknownCodec>
2176 template <
typename ValueType,
typename CodecType>
2188 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED
bool isUniform() const override
Return true if this array is stored as a single uniform value.
void readBuffers(std::istream &) override
Read attribute buffers from a stream.
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
static void unregisterType()
Remove this attribute type from the registry.
std::unique_ptr< Handle > UniquePtr
ValueType getUnsafe(Index n) const
Return the value at index n (assumes in-core)
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
void loadData() const override
Ensures all data is in-core.
virtual void expand(bool fill=true)=0
If this array is uniform, replace it with an array of length size().
~TypedAttributeArray() override
virtual Index storageTypeSize() const =0
std::unique_ptr< PageHandle > Ptr
GLdouble GLdouble GLint GLint const GLdouble * points
size_t memUsage() const override
Return the number of bytes of memory used by this attribute.
Index stride() const override
Write-able version of AttributeHandle.
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
bool isHidden() const
Return true if this attribute is hidden (e.g., from UI or iterators).
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
std::unique_ptr< Handle > ScopedPtr
bool hasConstantStride() const
virtual Index stride() const =0
bool isStreaming() const
Return true if this attribute is in streaming mode.
static const char * name()
bool compact()
Compact the existing array to become uniform if all values are identical.
PagedOutputStream & write(const char *str, std::streamsize n)
Writes the given.
typename T::ValueType ElementType
static TypedAttributeArray & cast(AttributeArray &attributeArray)
Cast an AttributeArray to TypedAttributeArray<T>
typename attribute_traits::TruncateTrait< T >::Type Type
GLsizei const GLfloat * value
std::shared_ptr< AccessorBase > AccessorBasePtr
static void decode(const StorageType &, ValueType &)
virtual ~AttributeHandle()
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.
void collapse() override
Replace the existing array with a uniform zero value.
bool valueTypeIsFloatingPoint() const override
Return true if the value type is floating point.
static void registerType(const NamePair &type, FactoryMethod, const ScopedRegistryLock *lock=nullptr)
Register a attribute type along with a factory function.
#define OPENVDB_LOG_WARN(mesg)
IntegerT floatingPointToFixedPoint(const FloatT s)
void(*)(AttributeArray *array, const Index n, const T &value) SetterPtr
ValueType get(Index n) const
Return the value at index n.
ValueType(*)(const AttributeArray *array, const Index n) GetterPtr
GLuint GLsizei GLsizei * length
#define OPENVDB_USE_VERSION_NAMESPACE
static ValueType encode(const ValueType &value)
StorageType * data()
Return the raw data buffer.
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
**But if you need a or simply need to know when the task has note that the like this
bool hasConstantStride() const
Return true if this attribute has a constant stride.
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
bool compact() override
Compact the existing array to become uniform if all values are identical.
bool valueTypeIsMatrix() const override
Return true if the value type is a matrix.
A Paging wrapper to std::istream that is responsible for reading from a given input stream and creati...
static void encode(const ValueType &, StorageType &)
void expand(bool fill=true)
If this array is uniform, replace it with an array of length size().
static const char * name()
void copyValues(const AttributeArray &sourceArray, const IterT &iter, bool compact=true)
Like copyValuesUnsafe(), but if compact is true, attempt to collapse this array.
const AttributeArray * mArray
std::ostream & getOutputStream()
Set and get the output stream.
const NamePair & type() const override
Return the name of this attribute's type.
static void encode(const math::Vec3< T > &, StorageType &)
void collapse()
Replace the existing array with a uniform value (zero if none provided).
void(*)(AttributeArray *array, const Index n, const Index &value) SetterPtr
TypedAttributeArray & operator=(const TypedAttributeArray &)
static Ptr create(AttributeArray &array, const bool expand=true)
static void decode(const StorageType &, ValueType &)
void(*)(AttributeArray *array, const Index &value) ValuePtr
AttributeArray::Ptr copy() const override
TypedAttributeArray(Index n=1, Index strideOrTotalSize=1, bool constantStride=true, const ValueType &uniformValue=zeroVal< ValueType >())
Default constructor, always constructs a uniform attribute.
typename attribute_traits::UIntTypeTrait< OneByte, T >::Type Type
std::shared_ptr< Handle > Ptr
#define OPENVDB_ASSERT(X)
static const char * name()
void(*)(AttributeArray *array, const T &value) ValuePtr
static const char * name()
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
void setConstantStride(bool state)
Specify whether this attribute has a constant stride or not.
bool operator!=(const AttributeArray &other) const
GLint GLint GLsizei GLint GLenum GLenum type
virtual Index dataSize() const =0
Base class for storing attribute data.
AttributeWriteHandle(AttributeArray &array, const bool expand=true)
const StorageType * data() const
void writeBuffers(std::ostream &os, bool outputTransient) const override
Index storageTypeSize() const override
FloatT fixedPointToFloatingPoint(const IntegerT s)
Index size() const override
Return the number of elements in this array.
virtual bool valueTypeIsFloatingPoint() const =0
Return true if the value type is floating point.
AttributeHandle(const AttributeArray &array, const bool collapseOnDestruction=true)
static void unregisterType(const NamePair &type, const ScopedRegistryLock *lock=nullptr)
Remove a attribute type from the registry.
constexpr auto set(type rhs) -> int
Accessor to call unsafe get and set methods based on templated Codec and Value.
static uint16_t pack(const Vec3< T > &vec)
Index index(Index n, Index m) const
virtual AccessorBasePtr getAccessor() const =0
Obtain an Accessor that stores getter and setter functors.
virtual void loadData() const =0
Ensures all data is in-core.
static const NamePair & attributeType()
Return the name of this attribute's type (includes codec)
GLsizei GLsizei GLchar * source
GLint GLenum GLboolean GLsizei stride
const StorageType * constData() const
Return the raw data buffer.
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 readMetadata(std::istream &) override
Read attribute metadata from a stream.
Index valueTypeSize() const override
Return the size in bytes of the value type of a single element in this array.
std::shared_ptr< AttributeArray > Ptr
static bool isRegistered(const NamePair &type, const ScopedRegistryLock *lock=nullptr)
Return true if the given attribute type name is registered.
std::shared_ptr< Handle > Ptr
Convenience wrappers to using Blosc and reading and writing of Paged data.
const char * constDataAsByteArray() const
Indirect virtual function to retrieve the data buffer cast to a char byte array.
void readPagedBuffers(compression::PagedInputStream &) override
Read attribute buffers from a paged stream.
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...
virtual bool isUniform() const =0
Return true if this array is stored as a single uniform value.
std::istream & getInputStream()
GLuint const GLchar * name
std::pair< Name, Name > NamePair
void writeMetadata(std::ostream &os, bool outputTransient, bool paged) const override
bool isTransient() const
Return true if this attribute is not serialized during stream output.
static ValueType encode(const ValueType &value)
GLboolean GLboolean GLboolean b
bool hasValueType() const
Return true if this attribute has a value type the same as the template parameter.
Name codecType() const override
Return the name of the codec used by this array (e.g., "trnc" or "fxpt").
static ValueType decode(const ValueType &value)
std::shared_ptr< const AttributeArray > ConstPtr
static bool isRegistered()
Return true if this attribute type is registered.
virtual bool isDataLoaded() const =0
Return true if all data has been loaded.
AttributeHandle & operator=(const AttributeHandle &)=default
static ValueType decode(const ValueType &value)
void(*)(AttributeArray *array, const Index n, const ValueType &value) SetterPtr
PageHandle::Ptr createHandle(std::streamsize n)
Creates a PageHandle to access the next.
virtual ~AttributeArray()
static Ptr create(const AttributeArray &array, const bool collapseOnDestruction=true)
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.
static void decode(const StorageType &, math::Vec3< T > &)
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
void copyValuesUnsafe(const AttributeArray &sourceArray, const IterT &iter)
Copy values into this array from a source array to a target array as referenced by an iterator...
void writePagedBuffers(compression::PagedOutputStream &os, bool outputTransient) const override
uint8_t flags() const
Retrieve the attribute array flags.
Name valueType() const override
Return the name of the value type of a single element in this array (e.g., "float" or "vec3d")...
const AttributeArray & array() const
void set(Index n, const ValueType &value)
Set value at the given index n.
Typed class for storing attribute data.
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
LeafData & operator=(const LeafData &)=delete
OPENVDB_API size_t bloscCompressedSize(const char *buffer, const size_t uncompressedBytes)
Convenience wrapper to retrieve the compressed size of buffer when compressed.
bool isDataLoaded() const override
Return true if all data has been loaded.
void fill(const ValueType &value)
Fill the existing array with the given value.
virtual ~AccessorBase()=default
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void write(std::ostream &os, bool outputTransient) const override
AccessorBasePtr getAccessor() const override
Obtain an Accessor that stores getter and setter functors.
static const char * name()
static const char * name()
std::shared_ptr< TypedAttributeArray > Ptr
bool valueTypeIsVector() const override
Return true if the value type is a vector.
A Paging wrapper to std::ostream that is responsible for writing from a given output stream at interv...
typename Codec::template Storage< ValueType >::Type StorageType
void set(Index n, const ValueType &value)
void read(std::istream &) override
Read attribute data from a stream.
static void set(SetterPtr functor, AttributeArray *array, const Index n, const ValueType &value)
Setter that calls the supplied functor.
virtual bool compact()=0
Compact the existing array to become uniform if all values are identical.
static void encode(const ValueType &, StorageType &)
static Vec3s unpack(const uint16_t data)
Accessor base class for AttributeArray storage where type is not available.
Ptr(*)(Index, Index, bool, const Metadata *) FactoryMethod
static void decode(const ValueType &, ValueType &)
compression::PageHandle::Ptr mPageHandle
T(*)(const AttributeArray *array, const Index n) GetterPtr
bool valueTypeIsQuaternion() const override
Return true if the value type is a quaternion.
static void set(SetterPtr, AttributeArray *array, const Index n, const ValueType &value)
streaming mode collapses attributes when first accessed
void setUnsafe(Index n, const ValueType &value)
Set value at the given index n (assumes in-core)
void fill(const ValueType &value)
Fill the existing array with the given value.
size_t memUsageIfLoaded() const override
bool isType() const
Return true if this attribute is of the same type as the template parameter.
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.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
static void registerType()
Register this attribute type along with a factory function.
bool valueTypeIsClass() const override
Return true if the value type is a class (ie vector, matrix or quaternion return true) ...
Accessor(GetterPtr getter, SetterPtr setter, ValuePtr collapser, ValuePtr filler)
void expand(bool fill=true) override
Replace the single value storage with an array of length size().
bool validData() const
Verify that data is not out-of-core or in a partially-read state.
ValueType get(Index n, Index m=0) const
static void encode(const ValueType &, ValueType &)
Index dataSize() const override
Return the size of the data in this array.
Index(*)(const AttributeArray *array, const Index n) GetterPtr
#define OPENVDB_THROW(exception, message)
std::atomic< Index32 > mOutOfCore
virtual ~AttributeWriteHandle()=default