OpenVDB: openvdb/points/AttributeGroup.h Source File

OpenVDB 10.0.1

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