OpenVDB  8.1.1
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 {
27 namespace OPENVDB_VERSION_NAME {
28 namespace points {
29 
30 
31 using GroupType = uint8_t;
32 
33 
35 
36 
39 {
40 public:
41  enum { INVALID_POS = std::numeric_limits<size_t>::max() };
42 
43  using Ptr = std::shared_ptr<AttributeSet>;
44  using ConstPtr = std::shared_ptr<const AttributeSet>;
45  using UniquePtr = std::unique_ptr<AttributeSet>;
46 
47  class Descriptor;
48 
49  using DescriptorPtr = std::shared_ptr<Descriptor>;
50  using DescriptorConstPtr = std::shared_ptr<const Descriptor>;
51 
53 
54  struct Util
55  {
57  struct NameAndType {
58  NameAndType(const std::string& n, const NamePair& t, const Index s = 1)
59  : name(n), type(t), stride(s) {}
63  };
64 
65  using NameAndTypeVec = std::vector<NameAndType>;
66  using NameToPosMap = std::map<std::string, size_t>;
67  using GroupIndex = std::pair<size_t, uint8_t>;
68  };
69 
71 
72  AttributeSet();
73 
80  AttributeSet(const AttributeSet& attributeSet, Index arrayLength,
81  const AttributeArray::ScopedRegistryLock* lock = nullptr);
82 
90  AttributeSet(const DescriptorPtr& descriptor, Index arrayLength = 1,
91  const AttributeArray::ScopedRegistryLock* lock = nullptr);
92 
94  AttributeSet(const AttributeSet&);
95 
97  AttributeSet& operator=(const AttributeSet&) = delete;
98 
100  Descriptor& descriptor() { return *mDescr; }
103  const Descriptor& descriptor() const { return *mDescr; }
105 
108  DescriptorPtr descriptorPtr() const { return mDescr; }
109 
111  size_t size() const { return mAttrs.size(); }
112 
114  size_t memUsage() const;
115 
118  size_t find(const std::string& name) const;
119 
124  size_t replace(const std::string& name, const AttributeArray::Ptr&);
125 
130  size_t replace(size_t pos, const AttributeArray::Ptr&);
131 
133  const AttributeArray* getConst(const std::string& name) const;
136  const AttributeArray* get(const std::string& name) const;
137  AttributeArray* get(const std::string& name);
139 
141  const AttributeArray* getConst(size_t pos) const;
144  const AttributeArray* get(size_t pos) const;
145  AttributeArray* get(size_t pos);
147 
149  size_t groupOffset(const Name& groupName) const;
156  size_t groupOffset(const Util::GroupIndex& index) const;
158 
160  Util::GroupIndex groupIndex(const Name& groupName) const;
163  Util::GroupIndex groupIndex(const size_t offset) const;
164 
166  std::vector<size_t> groupAttributeIndices() const;
167 
169  bool isShared(size_t pos) const;
173  void makeUnique(size_t pos);
174 
177  const NamePair& type,
178  const Index strideOrTotalSize = 1,
179  const bool constantStride = true,
180  const Metadata* defaultValue = nullptr);
181 
186  AttributeArray::Ptr appendAttribute(const Descriptor& expected, DescriptorPtr& replacement,
187  const size_t pos, const Index strideOrTotalSize = 1,
188  const bool constantStride = true,
189  const Metadata* defaultValue = nullptr,
190  const AttributeArray::ScopedRegistryLock* lock = nullptr);
191 
199  AttributeArray::Ptr removeAttribute(const Name& name);
200 
208  AttributeArray::Ptr removeAttribute(const size_t pos);
209 
222  AttributeArray::Ptr removeAttributeUnsafe(const size_t pos);
223 
226  void dropAttributes(const std::vector<size_t>& pos);
227 
231  void dropAttributes(const std::vector<size_t>& pos,
232  const Descriptor& expected, DescriptorPtr& replacement);
233 
236  void renameAttributes(const Descriptor& expected, const DescriptorPtr& replacement);
237 
240  void reorderAttributes(const DescriptorPtr& replacement);
241 
245  void resetDescriptor(const DescriptorPtr& replacement, const bool allowMismatchingDescriptors = false);
246 
248  void read(std::istream&);
251  void write(std::ostream&, bool outputTransient = false) const;
252 
254  void readDescriptor(std::istream&);
257  void writeDescriptor(std::ostream&, bool outputTransient = false) const;
258 
260  void readMetadata(std::istream&);
264  void writeMetadata(std::ostream&, bool outputTransient = false, bool paged = false) const;
265 
267  void readAttributes(std::istream&);
270  void writeAttributes(std::ostream&, bool outputTransient = false) const;
271 
274  bool operator==(const AttributeSet& other) const;
275  bool operator!=(const AttributeSet& other) const { return !this->operator==(other); }
276 
277 private:
278  using AttrArrayVec = std::vector<AttributeArray::Ptr>;
279 
280  DescriptorPtr mDescr;
281  AttrArrayVec mAttrs;
282 }; // class AttributeSet
283 
285 
286 
288 namespace future {
289  class Container
290  {
291  class Element { };
292  std::vector<std::shared_ptr<Element>> mElements;
293  };
294 }
295 
296 
298 
299 
304 class OPENVDB_API AttributeSet::Descriptor
305 {
306 public:
307  using Ptr = std::shared_ptr<Descriptor>;
308 
313  using ConstIterator = NameToPosMap::const_iterator;
314 
316  struct Inserter {
318  Inserter& add(const NameAndType& nameAndType) {
319  vec.push_back(nameAndType); return *this;
320  }
321  Inserter& add(const Name& name, const NamePair& type) {
322  vec.emplace_back(name, type); return *this;
323  }
324  Inserter& add(const NameAndTypeVec& other) {
325  for (NameAndTypeVec::const_iterator it = other.begin(), itEnd = other.end(); it != itEnd; ++it) {
326  vec.emplace_back(it->name, it->type);
327  }
328  return *this;
329  }
330  };
331 
333 
334  Descriptor();
335 
337  Descriptor(const Descriptor&);
338 
340  static Ptr create(const NamePair&);
341 
343  Ptr duplicateAppend(const Name& name, const NamePair& type) const;
344 
346  Ptr duplicateDrop(const std::vector<size_t>& pos) const;
347 
349  size_t size() const { return mTypes.size(); }
350 
352  size_t count(const NamePair& type) const;
353 
355  size_t memUsage() const;
356 
359  size_t find(const std::string& name) const;
360 
362  size_t rename(const std::string& fromName, const std::string& toName);
363 
365  const Name& valueType(size_t pos) const;
367  const NamePair& type(size_t pos) const;
368 
370  MetaMap& getMetadata();
371  const MetaMap& getMetadata() const;
372 
374  bool hasDefaultValue(const Name& name) const;
376  template<typename ValueType>
377  ValueType getDefaultValue(const Name& name) const
378  {
379  const size_t pos = find(name);
380  if (pos == INVALID_POS) {
381  OPENVDB_THROW(LookupError, "Cannot find attribute name to set default value.")
382  }
383 
384  std::stringstream ss;
385  ss << "default:" << name;
386 
387  auto metadata = mMetadata.getMetadata<TypedMetadata<ValueType>>(ss.str());
388 
389  if (metadata) return metadata->value();
390 
391  return zeroVal<ValueType>();
392  }
394  void setDefaultValue(const Name& name, const Metadata& defaultValue);
395  // Remove the default value if it exists
396  void removeDefaultValue(const Name& name);
397  // Prune any default values for which the key is no longer present
398  void pruneUnusedDefaultValues();
399 
401  bool operator==(const Descriptor&) const;
403  bool operator!=(const Descriptor& rhs) const { return !this->operator==(rhs); }
406  bool hasSameAttributes(const Descriptor& rhs) const;
407 
409  const NameToPosMap& map() const { return mNameMap; }
411  const NameToPosMap& groupMap() const { return mGroupMap; }
412 
414  bool hasGroup(const Name& group) const;
419  void setGroup(const Name& group, const size_t offset,
420  const bool checkValidOffset = false);
422  void dropGroup(const Name& group);
424  void clearGroups();
426  size_t renameGroup(const std::string& fromName, const std::string& toName);
428  const Name uniqueGroupName(const Name& name) const;
429 
431  size_t groupOffset(const Name& groupName) const;
438  size_t groupOffset(const GroupIndex& index) const;
440 
442  GroupIndex groupIndex(const Name& groupName) const;
445  GroupIndex groupIndex(const size_t offset) const;
446 
448  static size_t groupBits() { return sizeof(GroupType) * CHAR_BIT; }
449 
452  size_t availableGroups() const;
453 
456  size_t unusedGroups() const;
457 
459  bool canCompactGroups() const;
460 
464  size_t unusedGroupOffset(size_t hint = std::numeric_limits<size_t>::max()) const;
465 
472  bool requiresGroupMove(Name& sourceName, size_t& sourceOffset, size_t& targetOffset) const;
473 
478  bool groupIndexCollision(const Descriptor& rhs) const;
479 
481  const Name uniqueName(const Name& name) const;
482 
484  static bool validName(const Name& name);
485 
492  static void parseNames( std::vector<std::string>& includeNames,
493  std::vector<std::string>& excludeNames,
494  bool& includeAll,
495  const std::string& nameStr);
496 
499  static void parseNames( std::vector<std::string>& includeNames,
500  std::vector<std::string>& excludeNames,
501  const std::string& nameStr);
502 
504  void write(std::ostream&) const;
506  void read(std::istream&);
507 
508 protected:
510  void appendTo(NameAndTypeVec& attrs) const;
511 
514  static Ptr create(const NameAndTypeVec&, const NameToPosMap&, const MetaMap&);
515 
516  size_t insert(const std::string& name, const NamePair& typeName);
517 
518 private:
519  friend class ::TestAttributeSet;
520 
521  NameToPosMap mNameMap;
522  std::vector<NamePair> mTypes;
523  NameToPosMap mGroupMap;
524  MetaMap mMetadata;
525  // as this change is part of an ABI change, there's no good reason to reduce the reserved
526  // space aside from keeping the memory size of an AttributeSet the same for convenience
527  // (note that this assumes a typical three-pointer implementation for std::vector)
528  future::Container mFutureContainer; // occupies 3 reserved slots
529  int64_t mReserved[5]; // for future use
530 }; // class Descriptor
531 
532 } // namespace points
533 } // namespace OPENVDB_VERSION_NAME
534 } // namespace openvdb
535 
536 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED
std::shared_ptr< AttributeSet > Ptr
Definition: AttributeSet.h:43
Templated metadata class to hold specific types.
Definition: Metadata.h:121
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:240
Util::GroupIndex GroupIndex
Definition: AttributeSet.h:311
Inserter & add(const Name &name, const NamePair &type)
Definition: AttributeSet.h:321
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:74
NameAndType(const std::string &n, const NamePair &t, const Index s=1)
Definition: AttributeSet.h:58
NameToPosMap::const_iterator ConstIterator
Definition: AttributeSet.h:313
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set&#39;s descriptor, which might be shared with other sets...
Definition: AttributeSet.h:108
Attribute Array storage templated on type and compression codec.
Definition: AttributeSet.h:289
const NameToPosMap & groupMap() const
Return a reference to the name-to-position group map.
Definition: AttributeSet.h:411
std::string Name
Definition: Name.h:17
std::shared_ptr< const Descriptor > DescriptorConstPtr
Definition: AttributeSet.h:50
Utility method to construct a NameAndType sequence.
Definition: AttributeSet.h:316
void setGroup(PointDataTreeT &tree, const PointIndexTreeT &indexTree, const std::vector< short > &membership, const Name &group, const bool remove=false)
Sets group membership from a PointIndexTree-ordered vector.
Definition: PointGroup.h:546
std::shared_ptr< Descriptor > DescriptorPtr
Definition: AttributeSet.h:49
NamePair type
Definition: AttributeSet.h:61
std::unique_ptr< AttributeSet > UniquePtr
Definition: AttributeSet.h:45
const NameToPosMap & map() const
Return a reference to the name-to-position map.
Definition: AttributeSet.h:409
Index32 Index
Definition: openvdb/Types.h:50
Base class for storing attribute data.
Definition: AttributeArray.h:92
Attribute and type name pair.
Definition: AttributeSet.h:57
Definition: openvdb/Exceptions.h:60
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:107
std::pair< size_t, uint8_t > GroupIndex
Definition: AttributeSet.h:67
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:38
Util::NameAndTypeVec NameAndTypeVec
Definition: AttributeSet.h:310
T & value()
Return this metadata&#39;s value.
Definition: Metadata.h:249
void replace(std::string &str, const std::string &oldStr, const std::string &newStr)
Definition: util.h:61
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:39
void dropAttributes(PointDataTreeT &tree, const std::vector< size_t > &indices)
Drops attributes from the VDB tree.
Definition: PointAttribute.h:377
Definition: openvdb/Exceptions.h:13
Base class for storing metadata information in a grid.
Definition: Metadata.h:23
ValueType getDefaultValue(const Name &name) const
Get a default value for an existing attribute.
Definition: AttributeSet.h:377
const Descriptor & descriptor() const
Return a reference to this attribute set&#39;s descriptor, which might be shared with other sets...
Definition: AttributeSet.h:103
Definition: AttributeSet.h:54
NameAndTypeVec vec
Definition: AttributeSet.h:317
std::map< std::string, size_t > NameToPosMap
Definition: AttributeSet.h:66
void renameAttributes(PointDataTreeT &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
Definition: PointAttribute.h:465
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:125
std::vector< NameAndType > NameAndTypeVec
Definition: AttributeSet.h:65
bool operator!=(const Descriptor &rhs) const
Return true if this descriptor is not equal to the given one.
Definition: AttributeSet.h:403
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:477
Inserter & add(const NameAndTypeVec &other)
Definition: AttributeSet.h:324
Index64 memUsage(const TreeT &tree, bool threaded=true)
Return the total amount of memory in bytes occupied by this tree.
Definition: Count.h:233
void appendAttribute(PointDataTreeT &tree, const Name &name, const NamePair &type, const Index strideOrTotalSize=1, const bool constantStride=true, const Metadata *defaultValue=nullptr, 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
bool operator!=(const AttributeSet &other) const
Definition: AttributeSet.h:275
Inserter & add(const NameAndType &nameAndType)
Definition: AttributeSet.h:318
uint8_t GroupType
Definition: AttributeSet.h:31
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
static size_t groupBits()
Return number of bits occupied by a group attribute array.
Definition: AttributeSet.h:448
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
void dropGroup(PointDataTreeT &tree, const Name &group, const bool compact=true)
Drops an existing group from the VDB tree.
Definition: PointGroup.h:404
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178
Util::NameToPosMap NameToPosMap
Definition: AttributeSet.h:312
std::shared_ptr< const AttributeSet > ConstPtr
Definition: AttributeSet.h:44
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:111