HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AttributeSet.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
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 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 /// @file points/AttributeSet.h
32 ///
33 /// @authors Dan Bailey, Mihai Alden
34 ///
35 /// @brief Set of Attribute Arrays which tracks metadata about each array.
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 
58 ////////////////////////////////////////
59 
60 
61 /// Ordered collection of uniquely-named attribute arrays
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 
75  //////////
76 
77  struct Util
78  {
79  /// Attribute and type name pair.
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 
93  //////////
94 
95  AttributeSet();
96 
97  /// Construct a new AttributeSet from the given AttributeSet.
98  /// @param attributeSet the old attribute set
99  /// @param arrayLength the desired length of the arrays in the new AttributeSet
100  /// @note This constructor is typically used to resize an existing AttributeSet as
101  /// it transfers attribute metadata such as hidden and transient flags
102  AttributeSet(const AttributeSet& attributeSet, Index arrayLength);
103 
104  /// Construct a new AttributeSet from the given Descriptor.
105  /// @param descriptor stored in the new AttributeSet and used in construction
106  /// @param arrayLength the desired length of the arrays in the new AttributeSet
107  /// @note Descriptors do not store attribute metadata such as hidden and transient flags
108  /// which live on the AttributeArrays, so for constructing from an existing AttributeSet
109  /// use the AttributeSet(const AttributeSet&, Index) constructor instead
110  explicit AttributeSet(const DescriptorPtr& descriptor, Index arrayLength = 1);
111 
112  /// Shallow copy constructor, the descriptor and attribute arrays will be shared.
113  AttributeSet(const AttributeSet&);
114 
115  /// Disallow copy assignment, since it wouldn't be obvious whether the copy is deep or shallow.
116  AttributeSet& operator=(const AttributeSet&) = delete;
117 
118  //@{
119  /// @brief Return a reference to this attribute set's descriptor, which might
120  /// be shared with other sets.
121  Descriptor& descriptor() { return *mDescr; }
122  const Descriptor& descriptor() const { return *mDescr; }
123  //@}
124 
125  /// @brief Return a pointer to this attribute set's descriptor, which might be
126  /// shared with other sets
127  DescriptorPtr descriptorPtr() const { return mDescr; }
128 
129  /// Return the number of attributes in this set.
130  size_t size() const { return mAttrs.size(); }
131 
132  /// Return the number of bytes of memory used by this attribute set.
133  size_t memUsage() const;
134 
135  /// @brief Return the position of the attribute array whose name is @a name,
136  /// or @c INVALID_POS if no match is found.
137  size_t find(const std::string& name) const;
138 
139  /// @brief Replace the attribute array whose name is @a name.
140  /// @return The position of the updated attribute array or @c INVALID_POS
141  /// if the given name does not exist or if the replacement failed because
142  /// the new array type does not comply with the descriptor.
143  size_t replace(const std::string& name, const AttributeArray::Ptr&);
144 
145  /// @brief Replace the attribute array stored at position @a pos in this container.
146  /// @return The position of the updated attribute array or @c INVALID_POS
147  /// if replacement failed because the new array type does not comply with
148  /// the descriptor.
149  size_t replace(size_t pos, const AttributeArray::Ptr&);
150 
151  //@{
152  /// @brief Return a pointer to the attribute array whose name is @a name or
153  /// a null pointer if no match is found.
154  const AttributeArray* getConst(const std::string& name) const;
155  const AttributeArray* get(const std::string& name) const;
156  AttributeArray* get(const std::string& name);
157  //@}
158 
159  //@{
160  /// @brief Return a pointer to the attribute array stored at position @a pos
161  /// in this set.
162  const AttributeArray* getConst(size_t pos) const;
163  const AttributeArray* get(size_t pos) const;
164  AttributeArray* get(size_t pos);
165  //@}
166 
167  //@{
168  /// @brief Return the group offset from the name or index of the group
169  /// A group attribute array is a single byte (8-bit), each bit of which
170  /// can denote a group. The group offset is the position of the bit that
171  /// denotes the requested group if all group attribute arrays in the set
172  /// (and only attribute arrays marked as group) were to be laid out linearly
173  /// according to their order in the set.
174  size_t groupOffset(const Name& groupName) const;
175  size_t groupOffset(const Util::GroupIndex& index) const;
176  //@}
177 
178  /// Return the group index from the name of the group
179  Util::GroupIndex groupIndex(const Name& groupName) const;
180  /// Return the group index from the offset of the group
181  /// @note see offset description for groupOffset()
182  Util::GroupIndex groupIndex(const size_t offset) const;
183 
184  /// Return true if the attribute array stored at position @a pos is shared.
185  bool isShared(size_t pos) const;
186  /// @brief If the attribute array stored at position @a pos is shared,
187  /// replace the array with a deep copy of itself that is not
188  /// shared with anyone else.
189  void makeUnique(size_t pos);
190 
191  /// Append attribute @a attribute (simple method)
193  const NamePair& type,
194  const Index strideOrTotalSize = 1,
195  const bool constantStride = true,
197 
198  /// Append attribute @a attribute (descriptor-sharing)
199  /// Requires current descriptor to match @a expected
200  /// On append, current descriptor is replaced with @a replacement
201  AttributeArray::Ptr appendAttribute(const Descriptor& expected, DescriptorPtr& replacement,
202  const size_t pos, const Index strideOrTotalSize = 1,
203  const bool constantStride = true);
204 
205  /// Drop attributes with @a pos indices (simple method)
206  /// Creates a new descriptor for this attribute set
207  void dropAttributes(const std::vector<size_t>& pos);
208 
209  /// Drop attributes with @a pos indices (descriptor-sharing method)
210  /// Requires current descriptor to match @a expected
211  /// On drop, current descriptor is replaced with @a replacement
212  void dropAttributes(const std::vector<size_t>& pos,
213  const Descriptor& expected, DescriptorPtr& replacement);
214 
215  /// Re-name attributes in set to match a provided descriptor
216  /// Replaces own descriptor with @a replacement
217  void renameAttributes(const Descriptor& expected, const DescriptorPtr& replacement);
218 
219  /// Re order attribute set to match a provided descriptor
220  /// Replaces own descriptor with @a replacement
221  void reorderAttributes(const DescriptorPtr& replacement);
222 
223  /// Replace the current descriptor with a @a replacement
224  /// Note the provided Descriptor must be identical to the replacement
225  /// unless @a allowMismatchingDescriptors is true (default is false)
226  void resetDescriptor(const DescriptorPtr& replacement, const bool allowMismatchingDescriptors = false);
227 
228  /// Read the entire set from a stream.
229  void read(std::istream&);
230  /// Write the entire set to a stream.
231  /// @param outputTransient if true, write out transient attributes
232  void write(std::ostream&, bool outputTransient = false) const;
233 
234  /// This will read the attribute descriptor from a stream.
235  void readDescriptor(std::istream&);
236  /// This will write the attribute descriptor to a stream.
237  /// @param outputTransient if true, write out transient attributes
238  void writeDescriptor(std::ostream&, bool outputTransient = false) const;
239 
240  /// This will read the attribute metadata from a stream.
241  void readMetadata(std::istream&);
242  /// This will write the attribute metadata to a stream.
243  /// @param outputTransient if true, write out transient attributes
244  /// @param paged if true, data is written out in pages
245  void writeMetadata(std::ostream&, bool outputTransient = false, bool paged = false) const;
246 
247  /// This will read the attribute data from a stream.
248  void readAttributes(std::istream&);
249  /// This will write the attribute data to a stream.
250  /// @param outputTransient if true, write out transient attributes
251  void writeAttributes(std::ostream&, bool outputTransient = false) const;
252 
253  /// Compare the descriptors and attribute arrays on the attribute sets
254  /// Exit early if the descriptors do not match
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 
265 ////////////////////////////////////////
266 
267 
268 /// @brief An immutable object that stores name, type and AttributeSet position
269 /// for a constant collection of attribute arrays.
270 /// @note The attribute name is actually mutable, but the attribute type
271 /// and position can not be changed after creation.
272 class OPENVDB_API AttributeSet::Descriptor
273 {
274 public:
275  using Ptr = std::shared_ptr<Descriptor>;
276 
281  using ConstIterator = NameToPosMap::const_iterator;
282 
283  /// Utility method to construct a NameAndType sequence.
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 
300  //////////
301 
302  Descriptor();
303 
304  /// Copy constructor
305  Descriptor(const Descriptor&);
306 
307  /// Create a new descriptor from a position attribute type and assumes "P" (for convenience).
308  static Ptr create(const NamePair&);
309 
310  /// Create a new descriptor as a duplicate with a new attribute appended
311  Ptr duplicateAppend(const Name& name, const NamePair& type) const;
312 
313  /// Create a new descriptor as a duplicate with existing attributes dropped
314  Ptr duplicateDrop(const std::vector<size_t>& pos) const;
315 
316  /// Return the number of attributes in this descriptor.
317  size_t size() const { return mTypes.size(); }
318 
319  /// Return the number of attributes with this attribute type
320  size_t count(const NamePair& type) const;
321 
322  /// Return the number of bytes of memory used by this attribute set.
323  size_t memUsage() const;
324 
325  /// @brief Return the position of the attribute array whose name is @a name,
326  /// or @c INVALID_POS if no match is found.
327  size_t find(const std::string& name) const;
328 
329  /// Rename an attribute array
330  size_t rename(const std::string& fromName, const std::string& toName);
331 
332  /// Return the name of the attribute array's type.
333  const Name& valueType(size_t pos) const;
334  /// Return the name of the attribute array's type.
335  const NamePair& type(size_t pos) const;
336 
337  /// Retrieve metadata map
338  MetaMap& getMetadata();
339  const MetaMap& getMetadata() const;
340 
341  /// Return true if the attribute has a default value
342  bool hasDefaultValue(const Name& name) const;
343  /// Get a default value for an existing attribute
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  }
361  /// Set a default value for an existing attribute
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 
368  /// Return true if this descriptor is equal to the given one.
369  bool operator==(const Descriptor&) const;
370  /// Return true if this descriptor is not equal to the given one.
371  bool operator!=(const Descriptor& rhs) const { return !this->operator==(rhs); }
372  /// Return true if this descriptor contains the same attributes
373  /// as the given descriptor, ignoring attribute order
374  bool hasSameAttributes(const Descriptor& rhs) const;
375 
376  /// Return a reference to the name-to-position map.
377  const NameToPosMap& map() const { return mNameMap; }
378  /// Return a reference to the name-to-position group map.
379  const NameToPosMap& groupMap() const { return mGroupMap; }
380 
381  /// Return @c true if group exists
382  bool hasGroup(const Name& group) const;
383  /// Define a group name to offset mapping
384  void setGroup(const Name& group, const size_t offset);
385  /// Drop any mapping keyed by group name
386  void dropGroup(const Name& group);
387  /// Clear all groups
388  void clearGroups();
389 
390  /// Return a unique name for an attribute array based on given name
391  const Name uniqueName(const Name& name) const;
392 
393  /// Return true if the name is valid
394  static bool validName(const Name& name);
395 
396  /// Extract each name from nameStr into includeNames, or into excludeNames if name prefixed with caret
397  static void parseNames( std::vector<std::string>& includeNames,
398  std::vector<std::string>& excludeNames,
399  const std::string& nameStr);
400 
401  /// Serialize this descriptor to the given stream.
402  void write(std::ostream&) const;
403  /// Unserialize this transform from the given stream.
404  void read(std::istream&);
405 
406 protected:
407  /// Append to a vector of names and types from this Descriptor in position order
408  void appendTo(NameAndTypeVec& attrs) const;
409 
410  /// Create a new descriptor from the given attribute and type name pairs
411  /// and copy the group maps and metamap.
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/ )
Definition: ImfName.h:53
ValueType getDefaultValue(const Name &name) const
Get a default value for an existing attribute.
Definition: AttributeSet.h:345
Descriptor & descriptor()
Return a reference to this attribute set's descriptor, which might be shared with other sets...
Definition: AttributeSet.h:121
const Descriptor & descriptor() const
Return a reference to this attribute set's descriptor, which might be shared with other sets...
Definition: AttributeSet.h:122
void dropGroup(PointDataTree &tree, const Name &group, const bool compact=true)
Drops an existing group from the VDB tree.
Definition: PointGroup.h:529
bool operator!=(const Descriptor &rhs) const
Return true if this descriptor is not equal to the given one.
Definition: AttributeSet.h:371
T & value()
Return this metadata's value.
Definition: Metadata.h:266
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
std::shared_ptr< const AttributeSet > ConstPtr
Definition: AttributeSet.h:68
bool operator!=(const AttributeSet &other) const
Definition: AttributeSet.h:256
void read(T &in, bool &v)
Definition: ImfXdr.h:611
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
Utility method to construct a NameAndType sequence.
Definition: AttributeSet.h:284
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
GLdouble n
Definition: glcorearb.h:2007
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) ...
Base class for storing attribute data.
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Templated metadata class to hold specific types.
Definition: Metadata.h:138
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
GLintptr offset
Definition: glcorearb.h:664
std::shared_ptr< const Descriptor > DescriptorConstPtr
Definition: AttributeSet.h:73
void dropAttributes(PointDataTree &tree, const std::vector< size_t > &indices)
Drops attributes from the VDB tree.
const NameToPosMap & groupMap() const
Return a reference to the name-to-position group map.
Definition: AttributeSet.h:379
NameAndType(const std::string &n, const NamePair &t, const Index s=1)
Definition: AttributeSet.h:81
std::pair< Name, Name > NamePair
GLuint const GLchar * name
Definition: glcorearb.h:785
Inserter & add(const NameAndType &nameAndType)
Definition: AttributeSet.h:286
GLint GLsizei count
Definition: glcorearb.h:404
void renameAttributes(PointDataTree &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
GLuint index
Definition: glcorearb.h:785
Base class for storing metadata information in a grid.
Definition: Metadata.h:48
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
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:130
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
Inserter & add(const Name &name, const NamePair &type)
Definition: AttributeSet.h:289
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Attribute Array storage templated on type and compression codec.
void write(T &out, bool v)
Definition: ImfXdr.h:332
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set's descriptor, which might be shared with other sets...
Definition: AttributeSet.h:127
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
const NameToPosMap & map() const
Return a reference to the name-to-position map.
Definition: AttributeSet.h:377