OpenVDB 7.0.0

AttributeSet.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
9
10 #ifndef OPENVDB_POINTS_ATTRIBUTE_SET_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_ATTRIBUTE_SET_HAS_BEEN_INCLUDED
12
13 #include "AttributeArray.h"
14 #include <openvdb/version.h>
15 #include <openvdb/MetaMap.h>
16
17 #include <limits>
18 #include <memory>
19 #include <vector>
20
21
22 class TestAttributeSet;
23
24
25 namespace openvdb {
26 OPENVDB_USE_VERSION_NAMESPACE
27 namespace OPENVDB_VERSION_NAME {
28 namespace points {
29
30
32
33
35 class OPENVDB_API AttributeSet
36 {
37 public:
38 enum { INVALID_POS = std::numeric_limits<size_t>::max() };
39
40 using Ptr = std::shared_ptr<AttributeSet>;
41 using ConstPtr = std::shared_ptr<const AttributeSet>;
42
43 class Descriptor;
44
45 using DescriptorPtr = std::shared_ptr<Descriptor>;
46 using DescriptorConstPtr = std::shared_ptr<const Descriptor>;
47
49
50 struct Util
51 {
53 struct NameAndType {
54 NameAndType(const std::string& n, const NamePair& t, const Index s = 1)
55 : name(n), type(t), stride(s) {}
56 Name name;
57 NamePair type;
58 Index stride;
59 };
60
61 using NameAndTypeVec = std::vector<NameAndType>;
62 using NameToPosMap = std::map<std::string, size_t>;
63 using GroupIndex = std::pair<size_t, uint8_t>;
64 };
65
67
68 AttributeSet();
69
76 AttributeSet(const AttributeSet& attributeSet, Index arrayLength,
77 const AttributeArray::ScopedRegistryLock* lock = nullptr);
78
86 AttributeSet(const DescriptorPtr& descriptor, Index arrayLength = 1,
87 const AttributeArray::ScopedRegistryLock* lock = nullptr);
88
90 AttributeSet(const AttributeSet&);
91
93 AttributeSet& operator=(const AttributeSet&) = delete;
94
96 Descriptor& descriptor() { return *mDescr; }
99 const Descriptor& descriptor() const { return *mDescr; }
101
104 DescriptorPtr descriptorPtr() const { return mDescr; }
105
107 size_t size() const { return mAttrs.size(); }
108
110 size_t memUsage() const;
111
114 size_t find(const std::string& name) const;
115
120 size_t replace(const std::string& name, const AttributeArray::Ptr&);
121
126 size_t replace(size_t pos, const AttributeArray::Ptr&);
127
129 const AttributeArray* getConst(const std::string& name) const;
132 const AttributeArray* get(const std::string& name) const;
133 AttributeArray* get(const std::string& name);
135
137 const AttributeArray* getConst(size_t pos) const;
140 const AttributeArray* get(size_t pos) const;
141 AttributeArray* get(size_t pos);
143
145 size_t groupOffset(const Name& groupName) const;
152 size_t groupOffset(const Util::GroupIndex& index) const;
154
156 Util::GroupIndex groupIndex(const Name& groupName) const;
159 Util::GroupIndex groupIndex(const size_t offset) const;
160
162 bool isShared(size_t pos) const;
166 void makeUnique(size_t pos);
167
169 AttributeArray::Ptr appendAttribute(const Name& name,
170 const NamePair& type,
171 const Index strideOrTotalSize = 1,
172 const bool constantStride = true,
173 Metadata::Ptr defaultValue = Metadata::Ptr());
174
179 AttributeArray::Ptr appendAttribute(const Descriptor& expected, DescriptorPtr& replacement,
180 const size_t pos, const Index strideOrTotalSize = 1,
181 const bool constantStride = true,
182 const AttributeArray::ScopedRegistryLock* lock = nullptr);
183
186 void dropAttributes(const std::vector<size_t>& pos);
187
191 void dropAttributes(const std::vector<size_t>& pos,
192 const Descriptor& expected, DescriptorPtr& replacement);
193
196 void renameAttributes(const Descriptor& expected, const DescriptorPtr& replacement);
197
200 void reorderAttributes(const DescriptorPtr& replacement);
201
205 void resetDescriptor(const DescriptorPtr& replacement, const bool allowMismatchingDescriptors = false);
206
208 void read(std::istream&);
211 void write(std::ostream&, bool outputTransient = false) const;
212
214 void readDescriptor(std::istream&);
217 void writeDescriptor(std::ostream&, bool outputTransient = false) const;
218
220 void readMetadata(std::istream&);
224 void writeMetadata(std::ostream&, bool outputTransient = false, bool paged = false) const;
225
227 void readAttributes(std::istream&);
230 void writeAttributes(std::ostream&, bool outputTransient = false) const;
231
234 bool operator==(const AttributeSet& other) const;
235 bool operator!=(const AttributeSet& other) const { return !this->operator==(other); }
236
237 private:
238 using AttrArrayVec = std::vector<AttributeArray::Ptr>;
239
240 DescriptorPtr mDescr;
241 AttrArrayVec mAttrs;
242 }; // class AttributeSet
243
245
246
248 #if OPENVDB_ABI_VERSION_NUMBER >= 5
249 namespace future {
250 class Container
251 {
252 class Element { };
253 std::vector<std::shared_ptr<Element>> mElements;
254 };
255 }
256 #endif
257
258
260
261
266 class OPENVDB_API AttributeSet::Descriptor
267 {
268 public:
269 using Ptr = std::shared_ptr<Descriptor>;
270
271 using NameAndType = Util::NameAndType;
272 using NameAndTypeVec = Util::NameAndTypeVec;
273 using GroupIndex = Util::GroupIndex;
274 using NameToPosMap = Util::NameToPosMap;
275 using ConstIterator = NameToPosMap::const_iterator;
276
278 struct Inserter {
279 NameAndTypeVec vec;
280 Inserter& add(const NameAndType& nameAndType) {
281 vec.push_back(nameAndType); return *this;
282 }
283 Inserter& add(const Name& name, const NamePair& type) {
284 vec.emplace_back(name, type); return *this;
285 }
286 Inserter& add(const NameAndTypeVec& other) {
287 for (NameAndTypeVec::const_iterator it = other.begin(), itEnd = other.end(); it != itEnd; ++it) {
288 vec.emplace_back(it->name, it->type);
289 }
290 return *this;
291 }
292 };
293
295
296 Descriptor();
297
299 Descriptor(const Descriptor&);
300
302 static Ptr create(const NamePair&);
303
305 Ptr duplicateAppend(const Name& name, const NamePair& type) const;
306
308 Ptr duplicateDrop(const std::vector<size_t>& pos) const;
309
311 size_t size() const { return mTypes.size(); }
312
314 size_t count(const NamePair& type) const;
315
317 size_t memUsage() const;
318
321 size_t find(const std::string& name) const;
322
324 size_t rename(const std::string& fromName, const std::string& toName);
325
327 const Name& valueType(size_t pos) const;
329 const NamePair& type(size_t pos) const;
330
332 MetaMap& getMetadata();
333 const MetaMap& getMetadata() const;
334
336 bool hasDefaultValue(const Name& name) const;
338 template<typename ValueType>
339 ValueType getDefaultValue(const Name& name) const
340 {
341 const size_t pos = find(name);
342 if (pos == INVALID_POS) {
343 OPENVDB_THROW(LookupError, "Cannot find attribute name to set default value.")
344 }
345
346 std::stringstream ss;
347 ss << "default:" << name;
348
349 auto metadata = mMetadata.getMetadata<TypedMetadata<ValueType>>(ss.str());
350
351 if (metadata) return metadata->value();
352
353 return zeroVal<ValueType>();
354 }
356 void setDefaultValue(const Name& name, const Metadata& defaultValue);
357 // Remove the default value if it exists
358 void removeDefaultValue(const Name& name);
359 // Prune any default values for which the key is no longer present
360 void pruneUnusedDefaultValues();
361
363 bool operator==(const Descriptor&) const;
365 bool operator!=(const Descriptor& rhs) const { return !this->operator==(rhs); }
368 bool hasSameAttributes(const Descriptor& rhs) const;
369
371 const NameToPosMap& map() const { return mNameMap; }
373 const NameToPosMap& groupMap() const { return mGroupMap; }
374
376 bool hasGroup(const Name& group) const;
378 void setGroup(const Name& group, const size_t offset);
380 void dropGroup(const Name& group);
382 void clearGroups();
384 size_t renameGroup(const std::string& fromName, const std::string& toName);
386 const Name uniqueGroupName(const Name& name) const;
387
389 size_t groupOffset(const Name& groupName) const;
396 size_t groupOffset(const GroupIndex& index) const;
398
400 GroupIndex groupIndex(const Name& groupName) const;
403 GroupIndex groupIndex(const size_t offset) const;
404
406 const Name uniqueName(const Name& name) const;
407
409 static bool validName(const Name& name);
410
417 static void parseNames( std::vector<std::string>& includeNames,
418 std::vector<std::string>& excludeNames,
419 bool& includeAll,
420 const std::string& nameStr);
421
424 static void parseNames( std::vector<std::string>& includeNames,
425 std::vector<std::string>& excludeNames,
426 const std::string& nameStr);
427
429 void write(std::ostream&) const;
431 void read(std::istream&);
432
433 protected:
435 void appendTo(NameAndTypeVec& attrs) const;
436
439 static Ptr create(const NameAndTypeVec&, const NameToPosMap&, const MetaMap&);
440
441 size_t insert(const std::string& name, const NamePair& typeName);
442
443 private:
444 friend class ::TestAttributeSet;
445
446 NameToPosMap mNameMap;
447 std::vector<NamePair> mTypes;
448 NameToPosMap mGroupMap;
449 MetaMap mMetadata;
450 #if OPENVDB_ABI_VERSION_NUMBER >= 5
451 // as this change is part of an ABI change, there's no good reason to reduce the reserved
452 // space aside from keeping the memory size of an AttributeSet the same for convenience
453 // (note that this assumes a typical three-pointer implementation for std::vector)
454 future::Container mFutureContainer; // occupies 3 reserved slots
455 int64_t mReserved[5]; // for future use
456 #else
457 int64_t mReserved[8]; // for future use
458 #endif
459 }; // class Descriptor
460
461 } // namespace points
462 } // namespace OPENVDB_VERSION_NAME
463 } // namespace openvdb
464
465 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED
openvdb::v7_0::points::AttributeSet::GroupIndex
Util::GroupIndex GroupIndex
Definition: AttributeSet.h:273
openvdb::v7_0::Metadata
Base class for storing metadata information in a grid.
Definition: Metadata.h:23
openvdb::v7_0::TypedMetadata::value
T & value()
Return this metadata's value.
Definition: Metadata.h:272
openvdb::v7_0::points::AttributeArray
Base class for storing attribute data.
Definition: AttributeArray.h:92
openvdb::v7_0::points::AttributeSet::NameToPosMap
Util::NameToPosMap NameToPosMap
Definition: AttributeSet.h:274
openvdb::v7_0::LookupError
Definition: Exceptions.h:60
openvdb::v7_0::points::AttributeSet::ConstPtr
std::shared_ptr< const AttributeSet > ConstPtr
Definition: AttributeSet.h:41
openvdb::v7_0::points::renameAttributes
void renameAttributes(PointDataTreeT &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
Definition: PointAttribute.h:458
openvdb::v7_0::points::AttributeSet::map
const NameToPosMap & map() const
Return a reference to the name-to-position map.
Definition: AttributeSet.h:371
openvdb::v7_0::points::setGroup
void setGroup(PointDataTree &tree, const PointIndexTree &indexTree, const std::vector< short > &membership, const Name &group, const bool remove=false)
Sets group membership from a PointIndexTree-ordered vector.
Definition: PointGroup.h:665
openvdb::v7_0::points::AttributeSet::Util::NameAndType::stride
Index stride
Definition: AttributeSet.h:58
openvdb::v7_0::points::AttributeSet::operator!=
bool operator!=(const Descriptor &rhs) const
Return true if this descriptor is not equal to the given one.
Definition: AttributeSet.h:365
openvdb::v7_0::points::AttributeSet::Inserter::vec
NameAndTypeVec vec
Definition: AttributeSet.h:279
openvdb::v7_0::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition: Metadata.h:26
openvdb::v7_0::points::AttributeArray::Ptr
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:126
version.h
Library and file format version numbers.
AttributeArray.h
Attribute Array storage templated on type and compression codec.
openvdb::v7_0::points::AttributeSet::Ptr
std::shared_ptr< AttributeSet > Ptr
Definition: AttributeSet.h:40
openvdb::v7_0::points::AttributeSet::DescriptorPtr
std::shared_ptr< Descriptor > DescriptorPtr
Definition: AttributeSet.h:45
openvdb::v7_0::points::AttributeSet::descriptorPtr
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set's descriptor, which might be shared with other sets.
Definition: AttributeSet.h:104
MetaMap.h
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
openvdb::v7_0::points::AttributeSet::Util
Definition: AttributeSet.h:50
openvdb::v7_0::NamePair
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:39
openvdb::v7_0::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
openvdb::v7_0::points::appendAttribute
void appendAttribute(PointDataTreeT &tree, const Name &name, const NamePair &type, const Index strideOrTotalSize=1, const bool constantStride=true, Metadata::Ptr metaDefaultValue=Metadata::Ptr(), const bool hidden=false, const bool transient=false)
Appends a new attribute to the VDB tree (this method does not require a templated AttributeType)
Definition: PointAttribute.h:242
openvdb::v7_0::points::AttributeSet::NameAndTypeVec
Util::NameAndTypeVec NameAndTypeVec
Definition: AttributeSet.h:272
openvdb::v7_0::points::AttributeSet::groupMap
const NameToPosMap & groupMap() const
Return a reference to the name-to-position group map.
Definition: AttributeSet.h:373
openvdb::v7_0::TypedMetadata
Templated metadata class to hold specific types.
Definition: Metadata.h:144
openvdb::v7_0::points::AttributeSet::operator!=
bool operator!=(const AttributeSet &other) const
Definition: AttributeSet.h:235
openvdb::v7_0::tools::composite::max
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:106
openvdb::v7_0::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:471
openvdb::v7_0::points::AttributeSet::descriptor
const Descriptor & descriptor() const
Definition: AttributeSet.h:99
openvdb::v7_0::points::AttributeSet::getDefaultValue
ValueType getDefaultValue(const Name &name) const
Get a default value for an existing attribute.
Definition: AttributeSet.h:339
openvdb::v7_0::points::AttributeSet::ConstIterator
NameToPosMap::const_iterator ConstIterator
Definition: AttributeSet.h:275
openvdb::v7_0::points::AttributeSet::Inserter::add
Inserter & add(const NameAndType &nameAndType)
Definition: AttributeSet.h:280
openvdb::v7_0::points::dropAttributes
void dropAttributes(PointDataTreeT &tree, const std::vector< size_t > &indices)
Drops attributes from the VDB tree.
Definition: PointAttribute.h:370
openvdb::v7_0::points::AttributeSet::Util::NameAndType
Attribute and type name pair.
Definition: AttributeSet.h:53
openvdb::v7_0::points::AttributeSet::Util::NameAndTypeVec
std::vector< NameAndType > NameAndTypeVec
Definition: AttributeSet.h:61
openvdb::v7_0::points::AttributeSet::Util::NameAndType::type
NamePair type
Definition: AttributeSet.h:57
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::points::dropGroup
void dropGroup(PointDataTree &tree, const Name &group, const bool compact=true)
Drops an existing group from the VDB tree.
Definition: PointGroup.h:519
openvdb::v7_0::points::AttributeArray::ScopedRegistryLock
Definition: AttributeArray.h:119
openvdb::v7_0::points::AttributeSet::Util::NameAndType::name
Name name
Definition: AttributeSet.h:56
openvdb::v7_0::points::AttributeSet::size
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:107
openvdb::v7_0::points::AttributeSet
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:35
openvdb::v7_0::points::AttributeSet::Inserter::add
Inserter & add(const NameAndTypeVec &other)
Definition: AttributeSet.h:286
openvdb::v7_0::Index
Index32 Index
Definition: Types.h:31
openvdb::v7_0::Name
std::string Name
Definition: Name.h:17
openvdb::v7_0::points::AttributeSet::DescriptorConstPtr
std::shared_ptr< const Descriptor > DescriptorConstPtr
Definition: AttributeSet.h:46
openvdb::v7_0::points::AttributeSet::Util::NameToPosMap
std::map< std::string, size_t > NameToPosMap
Definition: AttributeSet.h:62
openvdb::v7_0::points::AttributeSet::Inserter
Utility method to construct a NameAndType sequence.
Definition: AttributeSet.h:278
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::points::AttributeSet::Util::GroupIndex
std::pair< size_t, uint8_t > GroupIndex
Definition: AttributeSet.h:63
openvdb::v7_0::points::future::Container
Definition: AttributeSet.h:250
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::points::AttributeSet::Util::NameAndType::NameAndType
NameAndType(const std::string &n, const NamePair &t, const Index s=1)
Definition: AttributeSet.h:54
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
openvdb::v7_0::points::AttributeSet::Inserter::add
Inserter & add(const Name &name, const NamePair &type)
Definition: AttributeSet.h:283