OpenVDB  6.0.0
AttributeSet.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 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 
102  AttributeSet(const AttributeSet& attributeSet, Index arrayLength);
103 
110  explicit AttributeSet(const DescriptorPtr& descriptor, Index arrayLength = 1);
111 
113  AttributeSet(const AttributeSet&);
114 
116  AttributeSet& operator=(const AttributeSet&) = delete;
117 
119  Descriptor& descriptor() { return *mDescr; }
122  const Descriptor& descriptor() const { return *mDescr; }
124 
127  DescriptorPtr descriptorPtr() const { return mDescr; }
128 
130  size_t size() const { return mAttrs.size(); }
131 
133  size_t memUsage() const;
134 
137  size_t find(const std::string& name) const;
138 
143  size_t replace(const std::string& name, const AttributeArray::Ptr&);
144 
149  size_t replace(size_t pos, const AttributeArray::Ptr&);
150 
152  const AttributeArray* getConst(const std::string& name) const;
155  const AttributeArray* get(const std::string& name) const;
156  AttributeArray* get(const std::string& name);
158 
160  const AttributeArray* getConst(size_t pos) const;
163  const AttributeArray* get(size_t pos) const;
164  AttributeArray* get(size_t pos);
166 
168  size_t groupOffset(const Name& groupName) const;
175  size_t groupOffset(const Util::GroupIndex& index) const;
177 
179  Util::GroupIndex groupIndex(const Name& groupName) const;
182  Util::GroupIndex groupIndex(const size_t offset) const;
183 
185  bool isShared(size_t pos) const;
189  void makeUnique(size_t pos);
190 
193  const NamePair& type,
194  const Index strideOrTotalSize = 1,
195  const bool constantStride = true,
196  Metadata::Ptr defaultValue = Metadata::Ptr());
197 
201  AttributeArray::Ptr appendAttribute(const Descriptor& expected, DescriptorPtr& replacement,
202  const size_t pos, const Index strideOrTotalSize = 1,
203  const bool constantStride = true);
204 
207  void dropAttributes(const std::vector<size_t>& pos);
208 
212  void dropAttributes(const std::vector<size_t>& pos,
213  const Descriptor& expected, DescriptorPtr& replacement);
214 
217  void renameAttributes(const Descriptor& expected, const DescriptorPtr& replacement);
218 
221  void reorderAttributes(const DescriptorPtr& replacement);
222 
226  void resetDescriptor(const DescriptorPtr& replacement, const bool allowMismatchingDescriptors = false);
227 
229  void read(std::istream&);
232  void write(std::ostream&, bool outputTransient = false) const;
233 
235  void readDescriptor(std::istream&);
238  void writeDescriptor(std::ostream&, bool outputTransient = false) const;
239 
241  void readMetadata(std::istream&);
245  void writeMetadata(std::ostream&, bool outputTransient = false, bool paged = false) const;
246 
248  void readAttributes(std::istream&);
251  void writeAttributes(std::ostream&, bool outputTransient = false) const;
252 
255  bool operator==(const AttributeSet& other) const;
256  bool operator!=(const AttributeSet& other) const { return !this->operator==(other); }
257 
258 private:
259  using AttrArrayVec = std::vector<AttributeArray::Ptr>;
260 
261  DescriptorPtr mDescr;
262  AttrArrayVec mAttrs;
263 }; // class AttributeSet
264 
266 
267 
269 #if OPENVDB_ABI_VERSION_NUMBER >= 5
270 namespace future {
271  class Container
272  {
273  class Element { };
274  std::vector<std::shared_ptr<Element>> mElements;
275  };
276 }
277 #endif
278 
279 
281 
282 
287 class OPENVDB_API AttributeSet::Descriptor
288 {
289 public:
290  using Ptr = std::shared_ptr<Descriptor>;
291 
296  using ConstIterator = NameToPosMap::const_iterator;
297 
299  struct Inserter {
301  Inserter& add(const NameAndType& nameAndType) {
302  vec.push_back(nameAndType); return *this;
303  }
304  Inserter& add(const Name& name, const NamePair& type) {
305  vec.emplace_back(name, type); return *this;
306  }
307  Inserter& add(const NameAndTypeVec& other) {
308  for (NameAndTypeVec::const_iterator it = other.begin(), itEnd = other.end(); it != itEnd; ++it) {
309  vec.emplace_back(it->name, it->type);
310  }
311  return *this;
312  }
313  };
314 
316 
317  Descriptor();
318 
320  Descriptor(const Descriptor&);
321 
323  static Ptr create(const NamePair&);
324 
326  Ptr duplicateAppend(const Name& name, const NamePair& type) const;
327 
329  Ptr duplicateDrop(const std::vector<size_t>& pos) const;
330 
332  size_t size() const { return mTypes.size(); }
333 
335  size_t count(const NamePair& type) const;
336 
338  size_t memUsage() const;
339 
342  size_t find(const std::string& name) const;
343 
345  size_t rename(const std::string& fromName, const std::string& toName);
346 
348  const Name& valueType(size_t pos) const;
350  const NamePair& type(size_t pos) const;
351 
353  MetaMap& getMetadata();
354  const MetaMap& getMetadata() const;
355 
357  bool hasDefaultValue(const Name& name) const;
359  template<typename ValueType>
360  ValueType getDefaultValue(const Name& name) const
361  {
362  const size_t pos = find(name);
363  if (pos == INVALID_POS) {
364  OPENVDB_THROW(LookupError, "Cannot find attribute name to set default value.")
365  }
366 
367  std::stringstream ss;
368  ss << "default:" << name;
369 
370  auto metadata = mMetadata.getMetadata<TypedMetadata<ValueType>>(ss.str());
371 
372  if (metadata) return metadata->value();
373 
374  return zeroVal<ValueType>();
375  }
377  void setDefaultValue(const Name& name, const Metadata& defaultValue);
378  // Remove the default value if it exists
379  void removeDefaultValue(const Name& name);
380  // Prune any default values for which the key is no longer present
381  void pruneUnusedDefaultValues();
382 
384  bool operator==(const Descriptor&) const;
386  bool operator!=(const Descriptor& rhs) const { return !this->operator==(rhs); }
389  bool hasSameAttributes(const Descriptor& rhs) const;
390 
392  const NameToPosMap& map() const { return mNameMap; }
394  const NameToPosMap& groupMap() const { return mGroupMap; }
395 
397  bool hasGroup(const Name& group) const;
399  void setGroup(const Name& group, const size_t offset);
401  void dropGroup(const Name& group);
403  void clearGroups();
405  size_t renameGroup(const std::string& fromName, const std::string& toName);
407  const Name uniqueGroupName(const Name& name) const;
408 
410  size_t groupOffset(const Name& groupName) const;
417  size_t groupOffset(const GroupIndex& index) const;
419 
421  GroupIndex groupIndex(const Name& groupName) const;
424  GroupIndex groupIndex(const size_t offset) const;
425 
427  const Name uniqueName(const Name& name) const;
428 
430  static bool validName(const Name& name);
431 
438  static void parseNames( std::vector<std::string>& includeNames,
439  std::vector<std::string>& excludeNames,
440  bool& includeAll,
441  const std::string& nameStr);
442 
445  static void parseNames( std::vector<std::string>& includeNames,
446  std::vector<std::string>& excludeNames,
447  const std::string& nameStr);
448 
450  void write(std::ostream&) const;
452  void read(std::istream&);
453 
454 protected:
456  void appendTo(NameAndTypeVec& attrs) const;
457 
460  static Ptr create(const NameAndTypeVec&, const NameToPosMap&, const MetaMap&);
461 
462  size_t insert(const std::string& name, const NamePair& typeName);
463 
464 private:
465  friend class ::TestAttributeSet;
466 
467  NameToPosMap mNameMap;
468  std::vector<NamePair> mTypes;
469  NameToPosMap mGroupMap;
470  MetaMap mMetadata;
471 #if OPENVDB_ABI_VERSION_NUMBER >= 5
472  // as this change is part of an ABI change, there's no good reason to reduce the reserved
473  // space aside from keeping the memory size of an AttributeSet the same for convenience
474  // (note that this assumes a typical three-pointer implementation for std::vector)
475  future::Container mFutureContainer; // occupies 3 reserved slots
476  int64_t mReserved[5]; // for future use
477 #else
478  int64_t mReserved[8]; // for future use
479 #endif
480 }; // class Descriptor
481 
482 } // namespace points
483 } // namespace OPENVDB_VERSION_NAME
484 } // namespace openvdb
485 
486 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED
487 
488 // Copyright (c) 2012-2018 DreamWorks Animation LLC
489 // All rights reserved. This software is distributed under the
490 // 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:194
Index32 Index
Definition: Types.h:61
Util::NameToPosMap NameToPosMap
Definition: AttributeSet.h:295
const NameToPosMap & map() const
Return a reference to the name-to-position map.
Definition: AttributeSet.h:392
std::map< std::string, size_t > NameToPosMap
Definition: AttributeSet.h:89
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
Attribute Array storage templated on type and compression codec.
std::vector< NameAndType > NameAndTypeVec
Definition: AttributeSet.h:88
NamePair type
Definition: AttributeSet.h:84
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:65
Definition: AttributeSet.h:271
std::shared_ptr< AttributeSet > Ptr
Definition: AttributeSet.h:67
Definition: AttributeSet.h:77
Inserter & add(const NameAndType &nameAndType)
Definition: AttributeSet.h:301
Util::GroupIndex GroupIndex
Definition: AttributeSet.h:294
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
std::shared_ptr< const AttributeSet > ConstPtr
Definition: AttributeSet.h:68
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
NameAndTypeVec vec
Definition: AttributeSet.h:300
Base class for storing metadata information in a grid.
Definition: Metadata.h:50
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:488
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:691
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
void dropAttributes(PointDataTreeT &tree, const std::vector< size_t > &indices)
Drops attributes from the VDB tree.
Definition: PointAttribute.h:529
Templated metadata class to hold specific types.
Definition: Metadata.h:171
void dropGroup(PointDataTree &tree, const Name &group, const bool compact=true)
Drops an existing group from the VDB tree.
Definition: PointGroup.h:545
Util::NameAndTypeVec NameAndTypeVec
Definition: AttributeSet.h:293
const NameToPosMap & groupMap() const
Return a reference to the name-to-position group map.
Definition: AttributeSet.h:394
Definition: Exceptions.h:87
std::shared_ptr< const Descriptor > DescriptorConstPtr
Definition: AttributeSet.h:73
Definition: Exceptions.h:40
void renameAttributes(PointDataTreeT &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
Definition: PointAttribute.h:618
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:144
ValueType getDefaultValue(const Name &name) const
Get a default value for an existing attribute.
Definition: AttributeSet.h:360
Inserter & add(const NameAndTypeVec &other)
Definition: AttributeSet.h:307
SharedPtr< Metadata > Ptr
Definition: Metadata.h:53
Library and file format version numbers.
std::shared_ptr< Descriptor > DescriptorPtr
Definition: AttributeSet.h:72
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
Base class for storing attribute data.
Definition: AttributeArray.h:118
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:130
Utility method to construct a NameAndType sequence.
Definition: AttributeSet.h:299
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:409
NameToPosMap::const_iterator ConstIterator
Definition: AttributeSet.h:296
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
Attribute and type name pair.
Definition: AttributeSet.h:80
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:386
std::string Name
Definition: Name.h:44
bool operator!=(const AttributeSet &other) const
Definition: AttributeSet.h:256
T & value()
Return this metadata's value.
Definition: Metadata.h:299
std::pair< size_t, uint8_t > GroupIndex
Definition: AttributeSet.h:90
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set's descriptor, which might be shared with other sets.
Definition: AttributeSet.h:127
Inserter & add(const Name &name, const NamePair &type)
Definition: AttributeSet.h:304
const Descriptor & descriptor() const
Return a reference to this attribute set's descriptor, which might be shared with other sets.
Definition: AttributeSet.h:122