9 #ifndef OPENVDB_POINTS_POINT_ATTRIBUTE_IMPL_HAS_BEEN_INCLUDED
10 #define OPENVDB_POINTS_POINT_ATTRIBUTE_IMPL_HAS_BEEN_INCLUDED
19 namespace point_attribute_internal {
22 template <
typename ValueType>
26 AttributeWriteHandle<ValueType>
handle(array);
27 handle.collapse(uniformValue);
34 StringAttributeWriteHandle
handle(array, descriptor.getMetadata());
35 handle.collapse(uniformValue);
42 template <
typename ValueType,
typename CodecType>
43 struct AttributeTypeConversion
51 template <
typename CodecType>
52 struct AttributeTypeConversion<
Name, CodecType>
54 static const NamePair&
type() {
return StringAttributeArray::attributeType(); }
61 template <
typename Po
intDataTreeT,
typename ValueType>
62 struct MetadataStorage
66 template<
typename AttributeListType>
67 static void add(PointDataTreeT&,
const AttributeListType&) {}
71 template <
typename Po
intDataTreeT>
72 struct MetadataStorage<PointDataTreeT,
Name>
74 static void add(PointDataTreeT& tree,
const Name& uniformValue) {
76 StringMetaInserter inserter(metadata);
77 inserter.insert(uniformValue);
80 template<
typename AttributeListType>
81 static void add(PointDataTreeT& tree,
const AttributeListType&
data) {
83 StringMetaInserter inserter(metadata);
86 for (
size_t i = 0; i < data.size(); i++) {
88 inserter.insert(value);
102 template <
typename Po
intDataTreeT>
106 const Index strideOrTotalSize,
107 const bool constantStride,
110 const bool transient)
112 auto iter = tree.cbeginLeaf();
118 const auto& descriptor = iter->attributeSet().descriptor();
119 const size_t index = descriptor.find(name);
123 "Cannot append an attribute with a non-unique name - " << name <<
".");
128 auto newDescriptor = descriptor.duplicateAppend(name, type);
133 newDescriptor->setDefaultValue(name, *defaultValue);
138 const size_t pos = newDescriptor->find(name);
148 [&](
typename PointDataTreeT::LeafNodeType& leaf,
size_t ) {
149 auto expected = leaf.attributeSet().descriptorPtr();
151 auto attribute = leaf.appendAttribute(*expected, newDescriptor,
152 pos, strideOrTotalSize, constantStride, defaultValue,
156 if (
transient)
attribute->setTransient(
true);
165 template <
typename ValueType,
typename CodecType,
typename Po
intDataTreeT>
169 const Index strideOrTotalSize,
170 const bool constantStride,
173 const bool transient)
176 "ValueType must not be derived from AttributeArray");
178 using point_attribute_internal::AttributeTypeConversion;
180 using point_attribute_internal::MetadataStorage;
183 strideOrTotalSize, constantStride, defaultValue, hidden,
transient);
191 if (!uniformIsDefault) {
193 collapseAttribute<ValueType>(tree, name, uniformValue);
201 template <
typename ValueType,
typename Po
intDataTreeT>
207 "ValueType must not be derived from AttributeArray");
209 auto iter = tree.cbeginLeaf();
213 const auto& descriptor = iter->attributeSet().descriptor();
217 const size_t index = descriptor.find(name);
219 OPENVDB_THROW(KeyError,
"Cannot find attribute name in PointDataTree.");
224 [&](
typename PointDataTreeT::LeafNodeType& leaf,
size_t ) {
225 assert(leaf.hasAttribute(index));
228 array, descriptor, uniformValue);
237 template <
typename Po
intDataTreeT>
239 const std::vector<size_t>&
indices)
241 auto iter = tree.cbeginLeaf();
245 const auto& descriptor = iter->attributeSet().descriptor();
249 const size_t positionIndex = descriptor.find(
"P");
251 std::find(indices.begin(), indices.end(), positionIndex) != indices.end()) {
252 OPENVDB_THROW(KeyError,
"Cannot drop mandatory position attribute.");
257 auto newDescriptor = descriptor.duplicateDrop(indices);
261 [&](
typename PointDataTreeT::LeafNodeType& leaf,
size_t ) {
262 auto expected = leaf.attributeSet().descriptorPtr();
263 leaf.dropAttributes(indices, *expected, newDescriptor);
272 template <
typename Po
intDataTreeT>
274 const std::vector<Name>& names)
276 auto iter = tree.cbeginLeaf();
280 const AttributeSet& attributeSet = iter->attributeSet();
286 const size_t index = descriptor.find(
name);
291 "Cannot drop an attribute that does not exist - " <<
name <<
".");
294 indices.push_back(index);
304 template <
typename Po
intDataTreeT>
308 std::vector<size_t>
indices{index};
313 template <
typename Po
intDataTreeT>
317 std::vector<Name> names{name};
325 template <
typename Po
intDataTreeT>
327 const std::vector<Name>& oldNames,
328 const std::vector<Name>& newNames)
330 if (oldNames.size() != newNames.size()) {
331 OPENVDB_THROW(ValueError,
"Mis-matching sizes of name vectors, cannot rename attributes.");
336 auto iter = tree.beginLeaf();
340 const AttributeSet& attributeSet = iter->attributeSet();
341 const Descriptor::Ptr descriptor = attributeSet.
descriptorPtr();
342 auto newDescriptor = std::make_shared<Descriptor>(*descriptor);
344 for (
size_t i = 0; i < oldNames.size(); i++) {
345 const Name& oldName = oldNames[i];
347 OPENVDB_THROW(KeyError,
"Cannot find requested attribute - " << oldName <<
".");
350 const Name& newName = newNames[i];
353 "Cannot rename attribute as new name already exists - " << newName <<
".");
360 OPENVDB_THROW(KeyError,
"Cannot rename group attribute - " << oldName <<
".");
363 newDescriptor->rename(oldName, newName);
366 for (; iter; ++iter) {
367 iter->renameAttributes(*descriptor, newDescriptor);
372 template <
typename Po
intDataTreeT>
384 template <
typename Po
intDataTreeT>
387 auto iter = tree.beginLeaf();
392 [&](
typename PointDataTreeT::LeafNodeType& leaf,
size_t ) {
393 leaf.compactAttributes();
406 #endif // OPENVDB_POINTS_POINT_ATTRIBUTE_IMPL_HAS_BEEN_INCLUDED
AttributeSet::Descriptor::Ptr makeDescriptorUnique(PointDataTreeT &tree)
Deep copy the descriptor across all leaf nodes.
Descriptor & descriptor()
Return a reference to this attribute set's descriptor, which might be shared with other sets...
GLsizei GLenum const void * indices
GLdouble GLdouble GLint GLint const GLdouble * points
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
GLsizei const GLchar *const * string
GLsizei const GLfloat * value
#define OPENVDB_USE_VERSION_NAMESPACE
void compactAttributes(PointDataTreeT &tree)
Compact attributes in a VDB tree (if possible).
bool isGroup(const AttributeArray &array)
void renameAttributes(PointDataTreeT &tree, const std::vector< Name > &oldNames, const std::vector< Name > &newNames)
Rename attributes in a VDB tree.
void appendAttribute(PointDataTreeT &tree, const Name &name, const NamePair &type, const Index strideOrTotalSize, const bool constantStride, const Metadata *defaultValue, const bool hidden, const bool transient)
Appends a new attribute to the VDB tree (this method does not require a templated AttributeType) ...
Base class for storing attribute data.
static const NamePair & attributeType()
Return the name of this attribute's type (includes codec)
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...
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
void dropAttributes(PointDataTreeT &tree, const std::vector< size_t > &indices)
Drops attributes from the VDB tree.
GLuint const GLchar * name
std::pair< Name, Name > NamePair
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.
void collapseAttribute(PointDataTreeT &tree, const Name &name, const ValueType &uniformValue)
Collapse the attribute into a uniform value.
void dropAttribute(PointDataTreeT &tree, const size_t &index)
Drop one attribute from the VDB tree (convenience method).
Ordered collection of uniquely-named attribute arrays.
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void renameAttribute(PointDataTreeT &tree, const Name &oldName, const Name &newName)
Rename an attribute in a VDB tree.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
DescriptorPtr descriptorPtr() const
Return a pointer to this attribute set's descriptor, which might be shared with other sets...
#define OPENVDB_THROW(exception, message)
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool