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 {
22 OPENVDB_USE_VERSION_NAMESPACE
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 =
74 point_attribute_internal::Default<ValueType>::value(),
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 =
90 point_attribute_internal::Default<ValueType>::value());
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>
164 inline void collapseAttribute(AttributeArray& array,
165 const AttributeSet::Descriptor&, const ValueType& uniformValue)
166 {
167 AttributeWriteHandle<ValueType> handle(array);
168 handle.collapse(uniformValue);
169 }
170
171
172 inline void collapseAttribute(AttributeArray& array,
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>
184 struct AttributeTypeConversion
185 {
186 static const NamePair& type() {
187 return TypedAttributeArray<ValueType, CodecType>::attributeType();
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>
203 struct MetadataStorage
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) {
261 OPENVDB_THROW(KeyError,
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
281 AttributeArray::ScopedRegistryLock lock;
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
316 using point_attribute_internal::AttributeTypeConversion;
317 using point_attribute_internal::Default;
318 using point_attribute_internal::MetadataStorage;
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);
359 point_attribute_internal::collapseAttribute(
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) {
422 OPENVDB_THROW(KeyError,
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) {
484 OPENVDB_THROW(KeyError,
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
openvdb::v7_0::points::dropAttributes
void dropAttributes(PointDataTreeT &tree, const std::vector< Name > &names)
Drops attributes from the VDB tree.
Definition: PointAttribute.h:405
AttributeSet.h
Set of Attribute Arrays which tracks metadata about each array.
openvdb::v7_0::points::StringMetaInserter::insert
void insert(const Name &name)
Insert the string into the metadata.
openvdb::v7_0::tree::LeafManager::foreach
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
openvdb::v7_0::points::AttributeArray
Base class for storing attribute data.
Definition: AttributeArray.h:92
openvdb::v7_0::points::renameAttribute
void renameAttribute(PointDataTreeT &tree, const Name &oldName, const Name &newName)
Rename an attribute in a VDB tree.
Definition: PointAttribute.h:505
openvdb::v7_0::points::StringAttributeWriteHandle::collapse
void collapse()
Set membership for the whole array and attempt to collapse.
openvdb::v7_0::points::renameAttributes
void renameAttributes(PointDataTreeT &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
Definition: PointAttribute.h:458
openvdb::v7_0::points::compactAttributes
void compactAttributes(PointDataTreeT &tree)
Compact attributes in a VDB tree (if possible).
Definition: PointAttribute.h:517
openvdb::v7_0::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition: Metadata.h:26
openvdb::v7_0::KeyError
Definition: Exceptions.h:59
openvdb::v7_0::points::isGroup
bool isGroup(const AttributeArray &array)
Definition: AttributeGroup.h:66
openvdb::v7_0::points::AttributeSet::descriptorPtr
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set's descriptor, which might be shared with other sets.
Definition: AttributeSet.h:104
openvdb::v7_0::points::StringMetaInserter
Definition: AttributeArrayString.h:58
openvdb::v7_0::tree::Tree::LeafNodeType
typename RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:185
OPENVDB_DEPRECATED
#define OPENVDB_DEPRECATED
Definition: Platform.h:42
AttributeGroup.h
Attribute Group access and filtering for iteration.
openvdb::v7_0::NamePair
std::pair< Name, Name > NamePair
Definition: AttributeArray.h:39
PointDataGrid.h
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
openvdb::v7_0::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
openvdb::v7_0::points::point_attribute_internal::MetadataStorage::add
static void add(PointDataTreeT &, const AttributeListType &)
Definition: PointAttribute.h:208
openvdb::v7_0::points::dropAttribute
void dropAttribute(PointDataTreeT &tree, const Name &name)
Drop one attribute from the VDB tree (convenience method).
Definition: PointAttribute.h:446
openvdb::v7_0::points::StringAttributeWriteHandle
Definition: AttributeArrayString.h:141
openvdb::v7_0::points::AttributeWriteHandle
Write-able version of AttributeHandle.
Definition: AttributeArray.h:920
openvdb::v7_0::points::point_attribute_internal::MetadataStorage
Definition: PointAttribute.h:203
openvdb::v7_0::tree::LeafManager
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:82
openvdb::v7_0::points::TypedAttributeArray
Typed class for storing attribute data.
Definition: AttributeArray.h:566
openvdb::v7_0::points::makeDescriptorUnique
AttributeSet::Descriptor::Ptr makeDescriptorUnique(PointDataTreeT &tree)
Deep copy the descriptor across all leaf nodes.
Definition: PointDataGrid.h:1587
openvdb::v7_0::points::point_attribute_internal::AttributeTypeConversion::type
static const NamePair & type()
Definition: PointAttribute.h:186
AttributeArrayString.h
Attribute array storage for string data using Descriptor Metadata.
openvdb::v7_0::points::bloscCompressAttribute
OPENVDB_DEPRECATED void bloscCompressAttribute(PointDataTreeT &, const Name &)
Definition: PointAttribute.h:535
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::points::AttributeArray::ScopedRegistryLock
Definition: AttributeArray.h:119
openvdb::v7_0::points::point_attribute_internal::collapseAttribute
void collapseAttribute(AttributeArray &array, const AttributeSet::Descriptor &descriptor, const Name &uniformValue)
Definition: PointAttribute.h:172
openvdb::v7_0::points::AttributeWriteHandle::collapse
void collapse()
Replace the existing array with a uniform value (zero if none provided).
Definition: AttributeArray.h:2284
openvdb::v7_0::points::AttributeSet
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:35
openvdb::v7_0::points::AttributeSet::getConst
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.
openvdb::v7_0::Index
Index32 Index
Definition: Types.h:31
openvdb::v7_0::points::point_attribute_internal::AttributeTypeConversion
Definition: PointAttribute.h:184
openvdb::v7_0::Name
std::string Name
Definition: Name.h:17
openvdb::v7_0::points::point_attribute_internal::AttributeTypeConversion< Name, CodecType >::type
static const NamePair & type()
Definition: PointAttribute.h:195
openvdb::v7_0::math::isExactlyEqual
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:388
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::points::point_attribute_internal::MetadataStorage< PointDataTreeT, Name >::add
static void add(PointDataTreeT &tree, const AttributeListType &data)
Definition: PointAttribute.h:222
openvdb::v7_0::points::PointDataTree
tree::Tree< tree::RootNode< tree::InternalNode< tree::InternalNode< PointDataLeafNode< PointDataIndex32, 3 >, 4 >, 5 > >> PointDataTree
Point index tree configured to match the default VDB configurations.
Definition: PointDataGrid.h:189
openvdb::v7_0::ValueError
Definition: Exceptions.h:65
openvdb::v7_0::points::point_attribute_internal::MetadataStorage::add
static void add(PointDataTreeT &, const ValueType &)
Definition: PointAttribute.h:205
openvdb::v7_0::points::AttributeSet::descriptor
Descriptor & descriptor()
Return a reference to this attribute set's descriptor, which might be shared with other sets.
Definition: AttributeSet.h:98
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::points::point_attribute_internal::MetadataStorage< PointDataTreeT, Name >::add
static void add(PointDataTreeT &tree, const Name &uniformValue)
Definition: PointAttribute.h:215
openvdb::v7_0::points::point_attribute_internal::Default
Definition: PointAttribute.h:29
openvdb::v7_0::points::point_attribute_internal::Default::value
static ValueType value()
Definition: PointAttribute.h:31
openvdb::v7_0::points::appendAttribute
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
openvdb.h
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82