OpenVDB  4.0.1
AttributeSet.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2017 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,
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 
272 class OPENVDB_API AttributeSet::Descriptor
273 {
274 public:
275  using Ptr = std::shared_ptr<Descriptor>;
276 
281  using ConstIterator = NameToPosMap::const_iterator;
282 
284  struct Inserter {
286  Inserter& add(const NameAndType& nameAndType) {
287  vec.push_back(nameAndType); return *this;
288  }
289  Inserter& add(const Name& name, const NamePair& type) {
290  vec.emplace_back(name, type); return *this;
291  }
292  Inserter& add(const NameAndTypeVec& other) {
293  for (NameAndTypeVec::const_iterator it = other.begin(), itEnd = other.end(); it != itEnd; ++it) {
294  vec.emplace_back(it->name, it->type);
295  }
296  return *this;
297  }
298  };
299 
301 
302  Descriptor();
303 
305  Descriptor(const Descriptor&);
306 
308  static Ptr create(const NamePair&);
309 
311  Ptr duplicateAppend(const Name& name, const NamePair& type) const;
312 
314  Ptr duplicateDrop(const std::vector<size_t>& pos) const;
315 
317  size_t size() const { return mTypes.size(); }
318 
320  size_t count(const NamePair& type) const;
321 
323  size_t memUsage() const;
324 
327  size_t find(const std::string& name) const;
328 
330  size_t rename(const std::string& fromName, const std::string& toName);
331 
333  const Name& valueType(size_t pos) const;
335  const NamePair& type(size_t pos) const;
336 
338  MetaMap& getMetadata();
339  const MetaMap& getMetadata() const;
340 
342  bool hasDefaultValue(const Name& name) const;
344  template<typename ValueType>
345  ValueType getDefaultValue(const Name& name) const
346  {
347  const size_t pos = find(name);
348  if (pos == INVALID_POS) {
349  OPENVDB_THROW(LookupError, "Cannot find attribute name to set default value.")
350  }
351 
352  std::stringstream ss;
353  ss << "default:" << name;
354 
355  auto metadata = mMetadata.getMetadata<TypedMetadata<ValueType>>(ss.str());
356 
357  if (metadata) return metadata->value();
358 
359  return zeroVal<ValueType>();
360  }
362  void setDefaultValue(const Name& name, const Metadata& defaultValue);
363  // Remove the default value if it exists
364  void removeDefaultValue(const Name& name);
365  // Prune any default values for which the key is no longer present
366  void pruneUnusedDefaultValues();
367 
369  bool operator==(const Descriptor&) const;
371  bool operator!=(const Descriptor& rhs) const { return !this->operator==(rhs); }
374  bool hasSameAttributes(const Descriptor& rhs) const;
375 
377  const NameToPosMap& map() const { return mNameMap; }
379  const NameToPosMap& groupMap() const { return mGroupMap; }
380 
382  bool hasGroup(const Name& group) const;
384  void setGroup(const Name& group, const size_t offset);
386  void dropGroup(const Name& group);
388  void clearGroups();
389 
391  const Name uniqueName(const Name& name) const;
392 
394  static bool validName(const Name& name);
395 
397  static void parseNames( std::vector<std::string>& includeNames,
398  std::vector<std::string>& excludeNames,
399  const std::string& nameStr);
400 
402  void write(std::ostream&) const;
404  void read(std::istream&);
405 
406 protected:
408  void appendTo(NameAndTypeVec& attrs) const;
409 
412  static Ptr create(const NameAndTypeVec&, const NameToPosMap&, const MetaMap&);
413 
414  size_t insert(const std::string& name, const NamePair& typeName);
415 
416 private:
417  friend class ::TestAttributeSet;
418 
419  NameToPosMap mNameMap;
420  std::vector<NamePair> mTypes;
421  NameToPosMap mGroupMap;
422  MetaMap mMetadata;
423  int64_t mReserved[8]; // for future use
424 }; // class Descriptor
425 
426 } // namespace points
427 } // namespace OPENVDB_VERSION_NAME
428 } // namespace openvdb
429 
430 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_HAS_BEEN_INCLUDED
431 
432 // Copyright (c) 2012-2017 DreamWorks Animation LLC
433 // All rights reserved. This software is distributed under the
434 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Base class for storing metadata information in a grid.
Definition: Metadata.h:48
Templated metadata class to hold specific types.
Definition: Metadata.h:138
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
T & value()
Return this metadata&#39;s value.
Definition: Metadata.h:266
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set&#39;s descriptor, which might be shared with other sets...
Definition: AttributeSet.h:127
std::shared_ptr< Descriptor > DescriptorPtr
Definition: AttributeSet.h:72
const NameToPosMap & map() const
Return a reference to the name-to-position map.
Definition: AttributeSet.h:377
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
Attribute Array storage templated on type and compression codec.
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:502
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
Attribute and type name pair.
Definition: AttributeSet.h:80
void appendAttribute(PointDataTree &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:463
std::pair< size_t, uint8_t > GroupIndex
Definition: AttributeSet.h:90
Util::NameAndTypeVec NameAndTypeVec
Definition: AttributeSet.h:278
Definition: AttributeSet.h:77
Utility method to construct a NameAndType sequence.
Definition: AttributeSet.h:284
Base class for storing attribute data.
Definition: AttributeArray.h:118
NameToPosMap::const_iterator ConstIterator
Definition: AttributeSet.h:281
ValueType defaultValue()
Definition: PointAttribute.h:56
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Util::GroupIndex GroupIndex
Definition: AttributeSet.h:279
Definition: Exceptions.h:87
const NameToPosMap & groupMap() const
Return a reference to the name-to-position group map.
Definition: AttributeSet.h:379
Inserter & add(const NameAndType &nameAndType)
Definition: AttributeSet.h:286
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:673
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:65
Definition: Exceptions.h:39
Util::NameToPosMap NameToPosMap
Definition: AttributeSet.h:280
Inserter & add(const Name &name, const NamePair &type)
Definition: AttributeSet.h:289
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
std::vector< NameAndType > NameAndTypeVec
Definition: AttributeSet.h:88
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
NamePair type
Definition: AttributeSet.h:84
void renameAttributes(PointDataTree &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
Definition: PointAttribute.h:691
SharedPtr< Metadata > Ptr
Definition: Metadata.h:51
std::shared_ptr< AttributeArray > Ptr
Definition: AttributeArray.h:142
std::string Name
Definition: Name.h:44
const Descriptor & descriptor() const
Return a reference to this attribute set&#39;s descriptor, which might be shared with other sets...
Definition: AttributeSet.h:122
NameAndType(const std::string &n, const NamePair &t, const Index s=1)
Definition: AttributeSet.h:81
std::shared_ptr< AttributeSet > Ptr
Definition: AttributeSet.h:67
std::shared_ptr< const AttributeSet > ConstPtr
Definition: AttributeSet.h:68
void dropAttributes(PointDataTree &tree, const std::vector< size_t > &indices)
Drops attributes from the VDB tree.
Definition: PointAttribute.h:604
Inserter & add(const NameAndTypeVec &other)
Definition: AttributeSet.h:292
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:130
std::shared_ptr< const Descriptor > DescriptorConstPtr
Definition: AttributeSet.h:73
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Index32 Index
Definition: Types.h:57
NameAndTypeVec vec
Definition: AttributeSet.h:285
bool operator!=(const Descriptor &rhs) const
Return true if this descriptor is not equal to the given one.
Definition: AttributeSet.h:371
bool operator!=(const AttributeSet &other) const
Definition: AttributeSet.h:256
void dropGroup(PointDataTree &tree, const Name &group, const bool compact=true)
Drops an existing group from the VDB tree.
Definition: PointGroup.h:529
std::map< std::string, size_t > NameToPosMap
Definition: AttributeSet.h:89
ValueType getDefaultValue(const Name &name) const
Get a default value for an existing attribute.
Definition: AttributeSet.h:345