OpenVDB  6.2.1
AttributeSet.h
Go to the documentation of this file.
1 //
3 // Copyright (c) DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
36 
37 #ifndef OPENVDB_POINTS_ATTRIBUTE_SET_HAS_BEEN_INCLUDED
38 #define OPENVDB_POINTS_ATTRIBUTE_SET_HAS_BEEN_INCLUDED
39 
40 #include "AttributeArray.h"
41 #include <openvdb/version.h>
42 #include <openvdb/MetaMap.h>
43 
44 #include <limits>
45 #include <memory>
46 #include <vector>
47 
48 
49 class TestAttributeSet;
50 
51 
52 namespace openvdb {
54 namespace OPENVDB_VERSION_NAME {
55 namespace points {
56 
57 
59 
60 
63 {
64 public:
65  enum { INVALID_POS = std::numeric_limits<size_t>::max() };
66 
67  using Ptr = std::shared_ptr<AttributeSet>;
68  using ConstPtr = std::shared_ptr<const AttributeSet>;
69 
70  class Descriptor;
71 
72  using DescriptorPtr = std::shared_ptr<Descriptor>;
73  using DescriptorConstPtr = std::shared_ptr<const Descriptor>;
74 
76 
77  struct Util
78  {
80  struct NameAndType {
81  NameAndType(const std::string& n, const NamePair& t, const Index s = 1)
82  : name(n), type(t), stride(s) {}
86  };
87 
88  using NameAndTypeVec = std::vector<NameAndType>;
89  using NameToPosMap = std::map<std::string, size_t>;
90  using GroupIndex = std::pair<size_t, uint8_t>;
91  };
92 
94 
95  AttributeSet();
96 
103  AttributeSet(const AttributeSet& attributeSet, Index arrayLength,
104  const AttributeArray::ScopedRegistryLock* lock = nullptr);
105 
113  AttributeSet(const DescriptorPtr& descriptor, Index arrayLength = 1,
114  const AttributeArray::ScopedRegistryLock* lock = nullptr);
115 
117  AttributeSet(const AttributeSet&);
118 
120  AttributeSet& operator=(const AttributeSet&) = delete;
121 
123  Descriptor& descriptor() { return *mDescr; }
126  const Descriptor& descriptor() const { return *mDescr; }
128 
131  DescriptorPtr descriptorPtr() const { return mDescr; }
132 
134  size_t size() const { return mAttrs.size(); }
135 
137  size_t memUsage() const;
138 
141  size_t find(const std::string& name) const;
142 
147  size_t replace(const std::string& name, const AttributeArray::Ptr&);
148 
153  size_t replace(size_t pos, const AttributeArray::Ptr&);
154 
156  const AttributeArray* getConst(const std::string& name) const;
159  const AttributeArray* get(const std::string& name) const;
160  AttributeArray* get(const std::string& name);
162 
164  const AttributeArray* getConst(size_t pos) const;
167  const AttributeArray* get(size_t pos) const;
168  AttributeArray* get(size_t pos);
170 
172  size_t groupOffset(const Name& groupName) const;
179  size_t groupOffset(const Util::GroupIndex& index) const;
181 
183  Util::GroupIndex groupIndex(const Name& groupName) const;
186  Util::GroupIndex groupIndex(const size_t offset) const;
187 
189  bool isShared(size_t pos) const;
193  void makeUnique(size_t pos);
194 
197  const NamePair& type,
198  const Index strideOrTotalSize = 1,
199  const bool constantStride = true,
200  Metadata::Ptr defaultValue = Metadata::Ptr());
201 
206  AttributeArray::Ptr appendAttribute(const Descriptor& expected, DescriptorPtr& replacement,
207  const size_t pos, const Index strideOrTotalSize = 1,
208  const bool constantStride = true,
209  const AttributeArray::ScopedRegistryLock* lock = nullptr);
210 
213  void dropAttributes(const std::vector<size_t>& pos);
214 
218  void dropAttributes(const std::vector<size_t>& pos,
219  const Descriptor& expected, DescriptorPtr& replacement);
220 
223  void renameAttributes(const Descriptor& expected, const DescriptorPtr& replacement);
224 
227  void reorderAttributes(const DescriptorPtr& replacement);
228 
232  void resetDescriptor(const DescriptorPtr& replacement, const bool allowMismatchingDescriptors = false);
233 
235  void read(std::istream&);
238  void write(std::ostream&, bool outputTransient = false) const;
239 
241  void readDescriptor(std::istream&);
244  void writeDescriptor(std::ostream&, bool outputTransient = false) const;
245 
247  void readMetadata(std::istream&);
251  void writeMetadata(std::ostream&, bool outputTransient = false, bool paged = false) const;
252 
254  void readAttributes(std::istream&);
257  void writeAttributes(std::ostream&, bool outputTransient = false) const;
258 
261  bool operator==(const AttributeSet& other) const;
262  bool operator!=(const AttributeSet& other) const { return !this->operator==(other); }
263 
264 private:
265  using AttrArrayVec = std::vector<AttributeArray::Ptr>;
266 
267  DescriptorPtr mDescr;
268  AttrArrayVec mAttrs;
269 }; // class AttributeSet
270 
272 
273 
275 #if OPENVDB_ABI_VERSION_NUMBER >= 5
276 namespace future {
277  class Container
278  {
279  class Element { };
280  std::vector<std::shared_ptr<Element>> mElements;
281  };
282 }
283 #endif
284 
285 
287 
288 
293 class OPENVDB_API AttributeSet::Descriptor
294 {
295 public:
296  using Ptr = std::shared_ptr<Descriptor>;
297 
302  using ConstIterator = NameToPosMap::const_iterator;
303 
305  struct Inserter {
307  Inserter& add(const NameAndType& nameAndType) {
308  vec.push_back(nameAndType); return *this;
309  }
310  Inserter& add(const Name& name, const NamePair& type) {
311  vec.emplace_back(name, type); return *this;
312  }
313  Inserter& add(const NameAndTypeVec& other) {
314  for (NameAndTypeVec::const_iterator it = other.begin(), itEnd = other.end(); it != itEnd; ++it) {
315  vec.emplace_back(it->name, it->type);
316  }
317  return *this;
318  }
319  };
320 
322 
323  Descriptor();
324 
326  Descriptor(const Descriptor&);
327 
329  static Ptr create(const NamePair&);
330 
332  Ptr duplicateAppend(const Name& name, const NamePair& type) const;
333 
335  Ptr duplicateDrop(const std::vector<size_t>& pos) const;
336 
338  size_t size() const { return mTypes.size(); }
339 
341  size_t count(const NamePair& type) const;
342 
344  size_t memUsage() const;
345 
348  size_t find(const std::string& name) const;
349 
351  size_t rename(const std::string& fromName, const std::string& toName);
352 
354  const Name& valueType(size_t pos) const;
356  const NamePair& type(size_t pos) const;
357 
359  MetaMap& getMetadata();
360  const MetaMap& getMetadata() const;
361 
363  bool hasDefaultValue(const Name& name) const;
365  template<typename ValueType>
366  ValueType getDefaultValue(const Name& name) const
367  {
368  const size_t pos = find(name);
369  if (pos == INVALID_POS) {
370  OPENVDB_THROW(LookupError, "Cannot find attribute name to set default value.")
371  }
372 
373  std::stringstream ss;
374  ss << "default:" << name;
375 
376  auto metadata = mMetadata.getMetadata<TypedMetadata<ValueType>>(ss.str());
377 
378  if (metadata) return metadata->value();
379 
380  return zeroVal<ValueType>();
381  }
383  void setDefaultValue(const Name& name, const Metadata& defaultValue);
384  // Remove the default value if it exists
385  void removeDefaultValue(const Name& name);
386  // Prune any default values for which the key is no longer present
387  void pruneUnusedDefaultValues();
388 
390  bool operator==(const Descriptor&) const;
392  bool operator!=(const Descriptor& rhs) const { return !this->operator==(rhs); }
395  bool hasSameAttributes(const Descriptor& rhs) const;
396 
398  const NameToPosMap& map() const { return mNameMap; }
400  const NameToPosMap& groupMap() const { return mGroupMap; }
401 
403  bool hasGroup(const Name& group) const;
405  void setGroup(const Name& group, const size_t offset);
407  void dropGroup(const Name& group);
409  void clearGroups();
411  size_t renameGroup(const std::string& fromName, const std::string& toName);
413  const Name uniqueGroupName(const Name& name) const;
414 
416  size_t groupOffset(const Name& groupName) const;
423  size_t groupOffset(const GroupIndex& index) const;
425 
427  GroupIndex groupIndex(const Name& groupName) const;
430  GroupIndex groupIndex(const size_t offset) const;
431 
433  const Name uniqueName(const Name& name) const;
434 
436  static bool validName(const Name& name);
437 
444  static void parseNames( std::vector<std::string>& includeNames,
445  std::vector<std::string>& excludeNames,
446  bool& includeAll,
447  const std::string& nameStr);
448 
451  static void parseNames( std::vector<std::string>& includeNames,
452  std::vector<std::string>& excludeNames,
453  const std::string& nameStr);
454 
456  void write(std::ostream&) const;
458  void read(std::istream&);
459 
460 protected:
462  void appendTo(NameAndTypeVec& attrs) const;
463 
466  static Ptr create(const NameAndTypeVec&, const NameToPosMap&, const MetaMap&);
467 
468  size_t insert(const std::string& name, const NamePair& typeName);
469 
470 private:
471  friend class ::TestAttributeSet;
472 
473  NameToPosMap mNameMap;
474  std::vector<NamePair> mTypes;
475  NameToPosMap mGroupMap;
476  MetaMap mMetadata;
477 #if OPENVDB_ABI_VERSION_NUMBER >= 5
478  // as this change is part of an ABI change, there's no good reason to reduce the reserved
479  // space aside from keeping the memory size of an AttributeSet the same for convenience
480  // (note that this assumes a typical three-pointer implementation for std::vector)
481  future::Container mFutureContainer; // occupies 3 reserved slots
482  int64_t mReserved[5]; // for future use
483 #else
484  int64_t mReserved[8]; // for future use
485 #endif
486 }; // class Descriptor
487 
488 } // namespace points
489 } // namespace OPENVDB_VERSION_NAME
490 } // namespace openvdb
491 
492 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED
493 
494 // Copyright (c) DreamWorks Animation LLC
495 // All rights reserved. This software is distributed under the
496 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
const NameToPosMap & map() const
Return a reference to the name-to-position map.
Definition: AttributeSet.h:398
Inserter & add(const NameAndType &nameAndType)
Definition: AttributeSet.h:307
bool operator!=(const AttributeSet &other) const
Definition: AttributeSet.h:262
std::shared_ptr< Descriptor > DescriptorPtr
Definition: AttributeSet.h:72
Definition: AttributeSet.h:77
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
Attribute Array storage templated on type and compression codec.
std::map< std::string, size_t > NameToPosMap
Definition: AttributeSet.h:89
std::vector< NameAndType > NameAndTypeVec
Definition: AttributeSet.h:88
Index32 Index
Definition: Types.h:61
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:134
NamePair type
Definition: AttributeSet.h:84
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set&#39;s descriptor, which might be shared with other sets...
Definition: AttributeSet.h:131
Attribute and type name pair.
Definition: AttributeSet.h:80
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:65
NameAndType(const std::string &n, const NamePair &t, const Index s=1)
Definition: AttributeSet.h:81
bool operator!=(const Descriptor &rhs) const
Return true if this descriptor is not equal to the given one.
Definition: AttributeSet.h:392
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:128
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:692
void dropAttributes(PointDataTreeT &tree, const std::vector< size_t > &indices)
Drops attributes from the VDB tree.
Definition: PointAttribute.h:397
std::string Name
Definition: Name.h:44
void dropGroup(PointDataTree &tree, const Name &group, const bool compact=true)
Drops an existing group from the VDB tree.
Definition: PointGroup.h:546
Definition: AttributeSet.h:277
Definition: Exceptions.h:40
NameToPosMap::const_iterator ConstIterator
Definition: AttributeSet.h:302
Definition: Exceptions.h:87
Templated metadata class to hold specific types.
Definition: Metadata.h:171
void renameAttributes(PointDataTreeT &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
Definition: PointAttribute.h:485
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:152
Inserter & add(const NameAndTypeVec &other)
Definition: AttributeSet.h:313
const NameToPosMap & groupMap() const
Return a reference to the name-to-position group map.
Definition: AttributeSet.h:400
Util::GroupIndex GroupIndex
Definition: AttributeSet.h:300
Library and file format version numbers.
NameAndTypeVec vec
Definition: AttributeSet.h:306
std::shared_ptr< AttributeSet > Ptr
Definition: AttributeSet.h:67
ValueType getDefaultValue(const Name &name) const
Get a default value for an existing attribute.
Definition: AttributeSet.h:366
const Descriptor & descriptor() const
Return a reference to this attribute set&#39;s descriptor, which might be shared with other sets...
Definition: AttributeSet.h:126
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:498
Utility method to construct a NameAndType sequence.
Definition: AttributeSet.h:305
Util::NameToPosMap NameToPosMap
Definition: AttributeSet.h:301
std::shared_ptr< const AttributeSet > ConstPtr
Definition: AttributeSet.h:68
SharedPtr< Metadata > Ptr
Definition: Metadata.h:53
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:269
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
Inserter & add(const Name &name, const NamePair &type)
Definition: AttributeSet.h:310
std::pair< size_t, uint8_t > GroupIndex
Definition: AttributeSet.h:90
T & value()
Return this metadata&#39;s value.
Definition: Metadata.h:299
Base class for storing attribute data.
Definition: AttributeArray.h:118
Util::NameAndTypeVec NameAndTypeVec
Definition: AttributeSet.h:299
std::shared_ptr< const Descriptor > DescriptorConstPtr
Definition: AttributeSet.h:73
Base class for storing metadata information in a grid.
Definition: Metadata.h:50