OpenVDB  7.0.0
PointAttribute.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_POINT_ATTRIBUTE_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_POINT_ATTRIBUTE_HAS_BEEN_INCLUDED
12 
13 #include <openvdb/openvdb.h>
14 
15 #include "AttributeArrayString.h"
16 #include "AttributeSet.h"
17 #include "AttributeGroup.h"
18 #include "PointDataGrid.h"
19 
20 
21 namespace openvdb {
23 namespace OPENVDB_VERSION_NAME {
24 namespace points {
25 
26 namespace point_attribute_internal {
27 
28 template <typename ValueType>
29 struct Default
30 {
31  static inline ValueType value() { return zeroVal<ValueType>(); }
32 };
33 
34 } // namespace point_attribute_internal
35 
36 
48 template <typename PointDataTreeT>
49 inline void appendAttribute(PointDataTreeT& tree,
50  const Name& name,
51  const NamePair& type,
52  const Index strideOrTotalSize = 1,
53  const bool constantStride = true,
54  Metadata::Ptr metaDefaultValue = Metadata::Ptr(),
55  const bool hidden = false,
56  const bool transient = false);
57 
68 template <typename ValueType,
69  typename CodecType = NullCodec,
70  typename PointDataTreeT = PointDataTree>
71 inline void appendAttribute(PointDataTreeT& tree,
72  const std::string& name,
73  const ValueType& uniformValue =
75  const Index strideOrTotalSize = 1,
76  const bool constantStride = true,
77  Metadata::Ptr metaDefaultValue = Metadata::Ptr(),
78  const bool hidden = false,
79  const bool transient = false);
80 
86 template <typename ValueType, typename PointDataTreeT>
87 inline void collapseAttribute( PointDataTreeT& tree,
88  const Name& name,
89  const ValueType& uniformValue =
91 
96 template <typename PointDataTreeT>
97 inline void dropAttributes( PointDataTreeT& tree,
98  const std::vector<size_t>& indices);
99 
104 template <typename PointDataTreeT>
105 inline void dropAttributes( PointDataTreeT& tree,
106  const std::vector<Name>& names);
107 
112 template <typename PointDataTreeT>
113 inline void dropAttribute( PointDataTreeT& tree,
114  const size_t& index);
115 
120 template <typename PointDataTreeT>
121 inline void dropAttribute( PointDataTreeT& tree,
122  const Name& name);
123 
133 template <typename PointDataTreeT>
134 inline void renameAttributes(PointDataTreeT& tree,
135  const std::vector<Name>& oldNames,
136  const std::vector<Name>& newNames);
137 
145 template <typename PointDataTreeT>
146 inline void renameAttribute(PointDataTreeT& tree,
147  const Name& oldName,
148  const Name& newName);
149 
153 template <typename PointDataTreeT>
154 inline void compactAttributes(PointDataTreeT& tree);
155 
156 
158 
159 
160 namespace point_attribute_internal {
161 
162 
163 template <typename ValueType>
165  const AttributeSet::Descriptor&, const ValueType& uniformValue)
166 {
167  AttributeWriteHandle<ValueType> handle(array);
168  handle.collapse(uniformValue);
169 }
170 
171 
173  const AttributeSet::Descriptor& descriptor, const Name& uniformValue)
174 {
175  StringAttributeWriteHandle handle(array, descriptor.getMetadata());
176  handle.collapse(uniformValue);
177 }
178 
179 
181 
182 
183 template <typename ValueType, typename CodecType>
185 {
186  static const NamePair& type() {
188  }
189 };
190 
191 
192 template <typename CodecType>
193 struct AttributeTypeConversion<Name, CodecType>
194 {
195  static const NamePair& type() { return StringAttributeArray::attributeType(); }
196 };
197 
198 
200 
201 
202 template <typename PointDataTreeT, typename ValueType>
204 {
205  static void add(PointDataTreeT&, const ValueType&) {}
206 
207  template<typename AttributeListType>
208  static void add(PointDataTreeT&, const AttributeListType&) {}
209 };
210 
211 
212 template <typename PointDataTreeT>
213 struct MetadataStorage<PointDataTreeT, Name>
214 {
215  static void add(PointDataTreeT& tree, const Name& uniformValue) {
216  MetaMap& metadata = makeDescriptorUnique(tree)->getMetadata();
217  StringMetaInserter inserter(metadata);
218  inserter.insert(uniformValue);
219  }
220 
221  template<typename AttributeListType>
222  static void add(PointDataTreeT& tree, const AttributeListType& data) {
223  MetaMap& metadata = makeDescriptorUnique(tree)->getMetadata();
224  StringMetaInserter inserter(metadata);
225  Name value;
226 
227  for (size_t i = 0; i < data.size(); i++) {
228  data.get(value, i);
229  inserter.insert(value);
230  }
231  }
232 };
233 
234 
235 } // namespace point_attribute_internal
236 
237 
239 
240 
241 template <typename PointDataTreeT>
242 inline void appendAttribute(PointDataTreeT& tree,
243  const Name& name,
244  const NamePair& type,
245  const Index strideOrTotalSize,
246  const bool constantStride,
247  Metadata::Ptr metaDefaultValue,
248  const bool hidden,
249  const bool transient)
250 {
251  auto iter = tree.cbeginLeaf();
252 
253  if (!iter) return;
254 
255  // do not append a non-unique attribute
256 
257  const auto& descriptor = iter->attributeSet().descriptor();
258  const size_t index = descriptor.find(name);
259 
260  if (index != AttributeSet::INVALID_POS) {
262  "Cannot append an attribute with a non-unique name - " << name << ".");
263  }
264 
265  // create a new attribute descriptor
266 
267  auto newDescriptor = descriptor.duplicateAppend(name, type);
268 
269  // store the attribute default value in the descriptor metadata
270 
271  if (metaDefaultValue) {
272  newDescriptor->setDefaultValue(name, *metaDefaultValue);
273  }
274 
275  // extract new pos
276 
277  const size_t pos = newDescriptor->find(name);
278 
279  // acquire registry lock to avoid locking when appending attributes in parallel
280 
282 
283  // insert attributes using the new descriptor
284 
285  tree::LeafManager<PointDataTreeT> leafManager(tree);
286  leafManager.foreach(
287  [&](typename PointDataTree::LeafNodeType& leaf, size_t /*idx*/) {
288  auto expected = leaf.attributeSet().descriptorPtr();
289 
290  auto attribute = leaf.appendAttribute(*expected, newDescriptor,
291  pos, strideOrTotalSize, constantStride, &lock);
292 
293  if (hidden) attribute->setHidden(true);
294  if (transient) attribute->setTransient(true);
295  }, /*threaded=*/ true
296  );
297 }
298 
299 
301 
302 
303 template <typename ValueType, typename CodecType, typename PointDataTreeT>
304 inline void appendAttribute(PointDataTreeT& tree,
305  const std::string& name,
306  const ValueType& uniformValue,
307  const Index strideOrTotalSize,
308  const bool constantStride,
309  Metadata::Ptr metaDefaultValue,
310  const bool hidden,
311  const bool transient)
312 {
313  static_assert(!std::is_base_of<AttributeArray, ValueType>::value,
314  "ValueType must not be derived from AttributeArray");
315 
319 
320  appendAttribute(tree, name, AttributeTypeConversion<ValueType, CodecType>::type(),
321  strideOrTotalSize, constantStride, metaDefaultValue, hidden, transient);
322 
323  if (!math::isExactlyEqual(uniformValue, Default<ValueType>::value())) {
324  MetadataStorage<PointDataTreeT, ValueType>::add(tree, uniformValue);
325  collapseAttribute<ValueType>(tree, name, uniformValue);
326  }
327 }
328 
329 
331 
332 
333 template <typename ValueType, typename PointDataTreeT>
334 inline void collapseAttribute( PointDataTreeT& tree,
335  const Name& name,
336  const ValueType& uniformValue)
337 {
338  static_assert(!std::is_base_of<AttributeArray, ValueType>::value,
339  "ValueType must not be derived from AttributeArray");
340 
341  auto iter = tree.cbeginLeaf();
342 
343  if (!iter) return;
344 
345  const auto& descriptor = iter->attributeSet().descriptor();
346 
347  // throw if attribute name does not exist
348 
349  const size_t index = descriptor.find(name);
350  if (index == AttributeSet::INVALID_POS) {
351  OPENVDB_THROW(KeyError, "Cannot find attribute name in PointDataTree.");
352  }
353 
354  tree::LeafManager<PointDataTreeT> leafManager(tree);
355  leafManager.foreach(
356  [&](typename PointDataTree::LeafNodeType& leaf, size_t /*idx*/) {
357  assert(leaf.hasAttribute(index));
358  AttributeArray& array = leaf.attributeArray(index);
360  array, descriptor, uniformValue);
361  }, /*threaded=*/true
362  );
363 }
364 
365 
367 
368 
369 template <typename PointDataTreeT>
370 inline void dropAttributes( PointDataTreeT& tree,
371  const std::vector<size_t>& indices)
372 {
373  auto iter = tree.cbeginLeaf();
374 
375  if (!iter) return;
376 
377  const auto& descriptor = iter->attributeSet().descriptor();
378 
379  // throw if position index present in the indices as this attribute is mandatory
380 
381  const size_t positionIndex = descriptor.find("P");
382  if (positionIndex!= AttributeSet::INVALID_POS &&
383  std::find(indices.begin(), indices.end(), positionIndex) != indices.end()) {
384  OPENVDB_THROW(KeyError, "Cannot drop mandatory position attribute.");
385  }
386 
387  // insert attributes using the new descriptor
388 
389  auto newDescriptor = descriptor.duplicateDrop(indices);
390 
391  tree::LeafManager<PointDataTreeT> leafManager(tree);
392  leafManager.foreach(
393  [&](typename PointDataTree::LeafNodeType& leaf, size_t /*idx*/) {
394  auto expected = leaf.attributeSet().descriptorPtr();
395  leaf.dropAttributes(indices, *expected, newDescriptor);
396  }, /*threaded=*/true
397  );
398 }
399 
400 
402 
403 
404 template <typename PointDataTreeT>
405 inline void dropAttributes( PointDataTreeT& tree,
406  const std::vector<Name>& names)
407 {
408  auto iter = tree.cbeginLeaf();
409 
410  if (!iter) return;
411 
412  const AttributeSet& attributeSet = iter->attributeSet();
413  const AttributeSet::Descriptor& descriptor = attributeSet.descriptor();
414 
415  std::vector<size_t> indices;
416 
417  for (const Name& name : names) {
418  const size_t index = descriptor.find(name);
419 
420  // do not attempt to drop an attribute that does not exist
421  if (index == AttributeSet::INVALID_POS) {
423  "Cannot drop an attribute that does not exist - " << name << ".");
424  }
425 
426  indices.push_back(index);
427  }
428 
429  dropAttributes(tree, indices);
430 }
431 
432 
434 
435 
436 template <typename PointDataTreeT>
437 inline void dropAttribute( PointDataTreeT& tree,
438  const size_t& index)
439 {
440  std::vector<size_t> indices{index};
441  dropAttributes(tree, indices);
442 }
443 
444 
445 template <typename PointDataTreeT>
446 inline void dropAttribute( PointDataTreeT& tree,
447  const Name& name)
448 {
449  std::vector<Name> names{name};
450  dropAttributes(tree, names);
451 }
452 
453 
455 
456 
457 template <typename PointDataTreeT>
458 inline void renameAttributes( PointDataTreeT& tree,
459  const std::vector<Name>& oldNames,
460  const std::vector<Name>& newNames)
461 {
462  if (oldNames.size() != newNames.size()) {
463  OPENVDB_THROW(ValueError, "Mis-matching sizes of name vectors, cannot rename attributes.");
464  }
465 
466  using Descriptor = AttributeSet::Descriptor;
467 
468  auto iter = tree.beginLeaf();
469 
470  if (!iter) return;
471 
472  const AttributeSet& attributeSet = iter->attributeSet();
473  const Descriptor::Ptr descriptor = attributeSet.descriptorPtr();
474  auto newDescriptor = std::make_shared<Descriptor>(*descriptor);
475 
476  for (size_t i = 0; i < oldNames.size(); i++) {
477  const Name& oldName = oldNames[i];
478  if (descriptor->find(oldName) == AttributeSet::INVALID_POS) {
479  OPENVDB_THROW(KeyError, "Cannot find requested attribute - " << oldName << ".");
480  }
481 
482  const Name& newName = newNames[i];
483  if (descriptor->find(newName) != AttributeSet::INVALID_POS) {
485  "Cannot rename attribute as new name already exists - " << newName << ".");
486  }
487 
488  const AttributeArray* array = attributeSet.getConst(oldName);
489  assert(array);
490 
491  if (isGroup(*array)) {
492  OPENVDB_THROW(KeyError, "Cannot rename group attribute - " << oldName << ".");
493  }
494 
495  newDescriptor->rename(oldName, newName);
496  }
497 
498  for (; iter; ++iter) {
499  iter->renameAttributes(*descriptor, newDescriptor);
500  }
501 }
502 
503 
504 template <typename PointDataTreeT>
505 inline void renameAttribute(PointDataTreeT& tree,
506  const Name& oldName,
507  const Name& newName)
508 {
509  renameAttributes(tree, {oldName}, {newName});
510 }
511 
512 
514 
515 
516 template <typename PointDataTreeT>
517 inline void compactAttributes(PointDataTreeT& tree)
518 {
519  auto iter = tree.beginLeaf();
520  if (!iter) return;
521 
522  tree::LeafManager<PointDataTreeT> leafManager(tree);
523  leafManager.foreach(
524  [&](typename PointDataTree::LeafNodeType& leaf, size_t /*idx*/) {
525  leaf.compactAttributes();
526  }, /*threaded=*/ true
527  );
528 }
529 
530 
532 
533 
534 template <typename PointDataTreeT>
535 OPENVDB_DEPRECATED inline void bloscCompressAttribute( PointDataTreeT&,
536  const Name&)
537 {
538  // in-memory compression is no longer supported
539 }
540 
541 
543 
544 
545 } // namespace points
546 } // namespace OPENVDB_VERSION_NAME
547 } // namespace openvdb
548 
549 #endif // OPENVDB_POINTS_POINT_ATTRIBUTE_HAS_BEEN_INCLUDED
Attribute Group access and filtering for iteration.
#define OPENVDB_DEPRECATED
Definition: Platform.h:42
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set&#39;s descriptor, which might be shared with other sets...
Definition: AttributeSet.h:104
void renameAttributes(PointDataTreeT &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
Definition: PointAttribute.h:458
void dropAttribute(PointDataTreeT &tree, const Name &name)
Drop one attribute from the VDB tree (convenience method).
Definition: PointAttribute.h:446
OPENVDB_DEPRECATED void bloscCompressAttribute(PointDataTreeT &, const Name &)
Definition: PointAttribute.h:535
void collapse()
Set membership for the whole array and attempt to collapse.
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
std::string Name
Definition: Name.h:17
Base class for storing attribute data.
Definition: AttributeArray.h:92
static void add(PointDataTreeT &tree, const AttributeListType &data)
Definition: PointAttribute.h:222
Descriptor & descriptor()
Return a reference to this attribute set&#39;s descriptor, which might be shared with other sets...
Definition: AttributeSet.h:98
Definition: Exceptions.h:59
void collapseAttribute(AttributeArray &array, const AttributeSet::Descriptor &descriptor, const Name &uniformValue)
Definition: PointAttribute.h:172
void renameAttribute(PointDataTreeT &tree, const Name &oldName, const Name &newName)
Rename an attribute in a VDB tree.
Definition: PointAttribute.h:505
static void add(PointDataTreeT &, const AttributeListType &)
Definition: PointAttribute.h:208
Definition: AttributeArrayString.h:141
Typed class for storing attribute data.
Definition: AttributeArray.h:566
Definition: Exceptions.h:65
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
const AttributeArray * getConst(const std::string &name) const
Return a pointer to the attribute array whose name is name or a null pointer if no match is found...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
bool isGroup(const AttributeArray &array)
Definition: AttributeGroup.h:66
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:39
Definition: Exceptions.h:13
Write-able version of AttributeHandle.
Definition: AttributeArray.h:920
This class manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional auxil...
Definition: LeafManager.h:82
void appendAttribute(PointDataTreeT &tree, const std::string &name, const ValueType &uniformValue=point_attribute_internal::Default< ValueType >::value(), 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.
Definition: PointAttribute.h:304
void collapse()
Replace the existing array with a uniform value (zero if none provided).
Definition: AttributeArray.h:2284
Definition: AttributeArray.h:490
SharedPtr< Metadata > Ptr
Definition: Metadata.h:26
typename RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:185
void foreach(const LeafOp &op, bool threaded=true, size_t grainSize=1)
Threaded method that applies a user-supplied functor to each leaf node in the LeafManager.
Definition: LeafManager.h:496
AttributeSet::Descriptor::Ptr makeDescriptorUnique(PointDataTreeT &tree)
Deep copy the descriptor across all leaf nodes.
Definition: PointDataGrid.h:1587
Index32 Index
Definition: Types.h:31
Definition: AttributeArrayString.h:58
void insert(const Name &name)
Insert the string into the metadata.
void compactAttributes(PointDataTreeT &tree)
Compact attributes in a VDB tree (if possible).
Definition: PointAttribute.h:517
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
static ValueType value()
Definition: PointAttribute.h:31
Definition: Tree.h:176
Attribute array storage for string data using Descriptor Metadata.
static void add(PointDataTreeT &, const ValueType &)
Definition: PointAttribute.h:205
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:388
static const NamePair & type()
Definition: PointAttribute.h:186
void dropAttributes(PointDataTreeT &tree, const std::vector< Name > &names)
Drops attributes from the VDB tree.
Definition: PointAttribute.h:405
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
static void add(PointDataTreeT &tree, const Name &uniformValue)
Definition: PointAttribute.h:215
Set of Attribute Arrays which tracks metadata about each array.
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:35