OpenVDB  12.0.0
AttributeGroup.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: Apache-2.0
3 
4 /// @file points/AttributeGroup.h
5 ///
6 /// @author Dan Bailey
7 ///
8 /// @brief Attribute Group access and filtering for iteration.
9 
10 #ifndef OPENVDB_POINTS_ATTRIBUTE_GROUP_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_ATTRIBUTE_GROUP_HAS_BEEN_INCLUDED
12 
13 #include "AttributeArray.h"
14 #include "AttributeSet.h"
15 #include <openvdb/util/Assert.h>
16 #include <memory>
17 
18 namespace openvdb {
20 namespace OPENVDB_VERSION_NAME {
21 namespace points {
22 
23 
24 ////////////////////////////////////////
25 
26 
27 struct GroupCodec
28 {
31 
32  template <typename T>
33  struct Storage { using Type = StorageType; };
34 
35  static void decode(const StorageType&, ValueType&);
36  static void encode(const ValueType&, StorageType&);
37  static const char* name() { return "grp"; }
38 };
39 
40 
42 
43 
44 ////////////////////////////////////////
45 
46 
47 inline void
48 GroupCodec::decode(const StorageType& data, ValueType& val)
49 {
50  val = data;
51 }
52 
53 
54 inline void
55 GroupCodec::encode(const ValueType& val, StorageType& data)
56 {
57  data = val;
58 }
59 
60 
61 ////////////////////////////////////////
62 
63 
64 inline bool isGroup(const AttributeArray& array)
65 {
66  return array.isType<GroupAttributeArray>();
67 }
68 
69 
70 ////////////////////////////////////////
71 
72 
74 {
75 public:
76  using Ptr = std::shared_ptr<GroupHandle>;
77  using UniquePtr = std::unique_ptr<GroupHandle>;
78 
79  // Dummy class that distinguishes an offset from a bitmask on construction
80  struct BitMask { };
81 
82  using GroupIndex = std::pair<Index, uint8_t>;
83 
84  GroupHandle(const GroupAttributeArray& array, const GroupType& offset);
85  GroupHandle(const GroupAttributeArray& array, const GroupType& bitMask, BitMask);
86 
87  Index size() const { return mArray.size(); }
88  bool isUniform() const { return mArray.isUniform(); }
89 
90  bool get(Index n) const;
91  bool getUnsafe(Index n) const;
92 
93 protected:
96 }; // class GroupHandle
97 
98 
99 ////////////////////////////////////////
100 
101 
103 {
104 public:
105  using Ptr = std::shared_ptr<GroupWriteHandle>;
106  using UniquePtr = std::unique_ptr<GroupWriteHandle>;
107 
108  GroupWriteHandle(GroupAttributeArray& array, const GroupType& offset);
109 
110  /// Set @a on at the given index @a n
111  void set(Index n, bool on);
112  /// Set @a on at the given index @a n (assumes in-core and non-uniform)
113  void setUnsafe(Index n, bool on);
114 
115  /// @brief Set membership for the whole array and attempt to collapse
116  ///
117  /// @param on True or false for inclusion in group
118  ///
119  /// @note This method guarantees that all attributes will have group membership
120  /// changed according to the input bool, however compaction will not be performed
121  /// if other groups that share the same underlying array are non-uniform.
122  /// The return value indicates if the group array ends up being uniform.
123  bool collapse(bool on);
124 
125  /// Compact the existing array to become uniform if all values are identical
126  bool compact();
127 
128 }; // class GroupWriteHandle
129 
130 
131 ////////////////////////////////////////
132 
133 
134 /// Index filtering on group membership
136 {
137 public:
138  GroupFilter(const Name& name, const AttributeSet& attributeSet)
139  : mIndex(attributeSet.groupIndex(name)) { }
140 
141  explicit GroupFilter(const AttributeSet::Descriptor::GroupIndex& index)
142  : mIndex(index) { }
143 
144  inline bool initialized() const { return bool(mHandle); }
145 
146  static index::State state() { return index::PARTIAL; }
147  template <typename LeafT>
148  static index::State state(const LeafT&) { return index::PARTIAL; }
149 
150  template <typename LeafT>
151  void reset(const LeafT& leaf) {
152  mHandle.reset(new GroupHandle(leaf.groupHandle(mIndex)));
153  }
154 
155  template <typename IterT>
156  bool valid(const IterT& iter) const {
157  OPENVDB_ASSERT(mHandle);
158  return mHandle->getUnsafe(*iter);
159  }
160 
161 private:
162  const AttributeSet::Descriptor::GroupIndex mIndex;
163  GroupHandle::Ptr mHandle;
164 }; // class GroupFilter
165 
166 
167 ////////////////////////////////////////
168 
169 
170 } // namespace points
171 
172 } // namespace OPENVDB_VERSION_NAME
173 } // namespace openvdb
174 
175 
176 #endif // OPENVDB_POINTS_ATTRIBUTE_GROUP_HAS_BEEN_INCLUDED
#define OPENVDB_API
Definition: Platform.h:268
uint8_t GroupType
Definition: AttributeSet.h:32
bool isGroup(const AttributeArray &array)
Definition: AttributeGroup.h:64
GroupType StorageType
Definition: AttributeGroup.h:29
static index::State state()
Definition: AttributeGroup.h:146
Attribute Array storage templated on type and compression codec.
Definition: AttributeGroup.h:102
void reset(const LeafT &leaf)
Definition: AttributeGroup.h:151
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:215
Index size() const
Definition: AttributeGroup.h:87
Index filtering on group membership.
Definition: AttributeGroup.h:135
Index32 Index
Definition: Types.h:54
StorageType Type
Definition: AttributeGroup.h:33
bool valid(const IterT &iter) const
Definition: AttributeGroup.h:156
Definition: AttributeGroup.h:80
const GroupAttributeArray & mArray
Definition: AttributeGroup.h:94
bool initialized() const
Definition: AttributeGroup.h:144
Definition: AttributeGroup.h:73
std::unique_ptr< GroupHandle > UniquePtr
Definition: AttributeGroup.h:77
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
Definition: Exceptions.h:13
GroupFilter(const Name &name, const AttributeSet &attributeSet)
Definition: AttributeGroup.h:138
static index::State state(const LeafT &)
Definition: AttributeGroup.h:148
OutGridT const XformOp bool bool
Definition: ValueTransformer.h:609
static const char * name()
Definition: AttributeGroup.h:37
State
Definition: IndexIterator.h:40
bool isUniform() const
Definition: AttributeGroup.h:88
Definition: AttributeGroup.h:33
std::string Name
Definition: Name.h:19
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:39
const GroupType mBitMask
Definition: AttributeGroup.h:95
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
Definition: AttributeGroup.h:27
Definition: IndexIterator.h:42
std::pair< Index, uint8_t > GroupIndex
Definition: AttributeGroup.h:82
Set of Attribute Arrays which tracks metadata about each array.
GroupType ValueType
Definition: AttributeGroup.h:30
std::shared_ptr< GroupHandle > Ptr
Definition: AttributeGroup.h:76
Typed class for storing attribute data.
Definition: AttributeArray.h:511
GroupFilter(const AttributeSet::Descriptor::GroupIndex &index)
Definition: AttributeGroup.h:141
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
Base class for storing attribute data.
Definition: AttributeArray.h:93