OpenVDB: openvdb_ax/codegen/PointLeafLocalData.h Source File

OpenVDB 13.0.0

Loading...
Searching...
No Matches
PointLeafLocalData.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 codegen/PointLeafLocalData.h
5///
6/// @authors Nick Avramoussis
7///
8/// @brief Thread/Leaf local data used during execution over OpenVDB Points
9///
10
11#ifndef OPENVDB_AX_COMPILER_LEAF_LOCAL_DATA_HAS_BEEN_INCLUDED
12#define OPENVDB_AX_COMPILER_LEAF_LOCAL_DATA_HAS_BEEN_INCLUDED
13
14#include <openvdb/openvdb.h>
15#include <openvdb/version.h>
16#include <openvdb/points/AttributeArray.h>
17#include <openvdb/points/PointAttribute.h>
18#include <openvdb/points/PointDataGrid.h>
19#include <openvdb/points/PointGroup.h>
20#include <openvdb/util/Assert.h>
21
22namespace openvdb {
23 OPENVDB_USE_VERSION_NAMESPACE
24namespace OPENVDB_VERSION_NAME {
25
26namespace ax {
27namespace codegen {
28
29namespace codegen_internal {
30
31
32/// @brief Various functions can request the use and initialization of point data from within
33/// the kernel that does not use the standard attribute handle methods. This data can
34/// then be accessed after execution to perform post-processes such as adding new groups,
35/// adding new string attributes or updating positions.
36///
37/// @note Due to the way string handles work, string write attribute handles cannot
38/// be constructed in parallel, nor can read handles retrieve values in parallel
39/// if there is a chance the shared metadata is being written to (with set()).
40/// As the compiler allows for any arbitrary string setting/getting, leaf local
41/// maps are used for temporary storage per point. The maps use the string array
42/// pointers as a key for later synchronization.
43///
44 struct PointLeafLocalData
45{
46 using UniquePtr = std::unique_ptr<PointLeafLocalData>;
47 using GroupArrayT = openvdb::points::GroupAttributeArray;
48 using GroupHandleT = openvdb::points::GroupWriteHandle;
49
50 using PointStringMap = std::map<uint64_t, std::string>;
51 using StringArrayMap = std::map<points::AttributeArray*, PointStringMap>;
52
53 using LeafNode = openvdb::points::PointDataTree::LeafNodeType;
54
55 /// @brief Construct a new data object to keep track of various data objects
56 /// created per leaf by the point compute generator.
57 ///
58 /// @param count The number of points within the current leaf, used to initialize
59 /// the size of new arrays
60 ///
61 PointLeafLocalData(const size_t count)
62 : mPointCount(count)
63 , mArrays()
64 , mOffset(0)
65 , mHandles()
66 , mStringMap() {}
67
68 ////////////////////////////////////////////////////////////////////////
69
70 /// Group methods
71
72 /// @brief Return a group write handle to a specific group name, creating the
73 /// group array if it doesn't exist. This includes either registering a
74 /// new offset or allocating an entire array. The returned handle is
75 /// guaranteed to be valid.
76 ///
77 /// @param name The group name
78 ///
79 inline GroupHandleT* getOrInsert(const std::string& name)
80 {
81 GroupHandleT* ptr = get(name);
82 if (ptr) return ptr;
83
84 static const size_t maxGroupsInArray =
85#if (OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER > 7 || \
86 (OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER >= 7 && \
87 OPENVDB_LIBRARY_MINOR_VERSION_NUMBER >= 1))
88 points::AttributeSet::Descriptor::groupBits();
89#else
90 // old removed method
91 points::point_group_internal::GroupInfo::groupBits();
92#endif
93
94 if (mArrays.empty() || mOffset == maxGroupsInArray) {
95 OPENVDB_ASSERT(mPointCount < static_cast<size_t>(std::numeric_limits<openvdb::Index>::max()));
96 mArrays.emplace_back(new GroupArrayT(static_cast<openvdb::Index>(mPointCount)));
97 mOffset = 0;
98 }
99
100 GroupArrayT* array = mArrays.back().get();
101 OPENVDB_ASSERT(array);
102
103 std::unique_ptr<GroupHandleT>& handle = mHandles[name];
104 handle.reset(new GroupHandleT(*array, mOffset++));
105 return handle.get();
106 }
107
108 /// @brief Return a group write handle to a specific group name if it exists.
109 /// Returns a nullptr if no group exists of the given name
110 ///
111 /// @param name The group name
112 ///
113 inline GroupHandleT* get(const std::string& name) const
114 {
115 const auto iter = mHandles.find(name);
116 if (iter == mHandles.end()) return nullptr;
117 return iter->second.get();
118 }
119
120 /// @brief Return true if a valid group handle exists
121 ///
122 /// @param name The group name
123 ///
124 inline bool hasGroup(const std::string& name) const {
125 return mHandles.find(name) != mHandles.end();
126 }
127
128 /// @brief Populate a set with all the groups which have been inserted into
129 /// this object. Used to compute a final set of all new groups which
130 /// have been created across all leaf nodes
131 ///
132 /// @param groups The set to populate
133 ///
134 inline void getGroups(std::set<std::string>& groups) const {
135 for (const auto& iter : mHandles) {
136 groups.insert(iter.first);
137 }
138 }
139
140 /// @brief Compact all arrays stored on this object. This does not invalidate
141 /// any active write handles.
142 ///
143 inline void compact() {
144 for (auto& array : mArrays) array->compact();
145 }
146
147
148 ////////////////////////////////////////////////////////////////////////
149
150 /// String methods
151
152 /// @brief Get any new string data associated with a particular point on a
153 /// particular string attribute array. Returns true if data was set,
154 /// false if no data was found.
155 ///
156 /// @param array The array pointer to use as a key lookup
157 /// @param idx The point index
158 /// @param data The string to set if data is stored
159 ///
160 inline bool
161 getNewStringData(const points::AttributeArray* array, const uint64_t idx, std::string& data) const {
162 const auto arrayMapIter = mStringMap.find(const_cast<points::AttributeArray*>(array));
163 if (arrayMapIter == mStringMap.end()) return false;
164 const auto iter = arrayMapIter->second.find(idx);
165 if (iter == arrayMapIter->second.end()) return false;
166 data = iter->second;
167 return true;
168 }
169
170 /// @brief Set new string data associated with a particular point on a
171 /// particular string attribute array.
172 ///
173 /// @param array The array pointer to use as a key lookup
174 /// @param idx The point index
175 /// @param data The string to set
176 ///
177 inline void
178 setNewStringData(points::AttributeArray* array, const uint64_t idx, const std::string& data) {
179 mStringMap[array][idx] = data;
180 }
181
182 /// @brief Remove any new string data associated with a particular point on a
183 /// particular string attribute array. Does nothing if no data exists
184 ///
185 /// @param array The array pointer to use as a key lookup
186 /// @param idx The point index
187 ///
188 inline void
189 removeNewStringData(points::AttributeArray* array, const uint64_t idx) {
190 const auto arrayMapIter = mStringMap.find(array);
191 if (arrayMapIter == mStringMap.end()) return;
192 arrayMapIter->second.erase(idx);
193 if (arrayMapIter->second.empty()) mStringMap.erase(arrayMapIter);
194 }
195
196 /// @brief Insert all new point strings stored across all collected string
197 /// attribute arrays into a StringMetaInserter. Returns false if the
198 /// inserter was not accessed and true if it was potentially modified.
199 ///
200 /// @param inserter The string meta inserter to update
201 ///
202 inline bool
203 insertNewStrings(points::StringMetaInserter& inserter) const {
204 for (const auto& arrayIter : mStringMap) {
205 for (const auto& iter : arrayIter.second) {
206 inserter.insert(iter.second);
207 }
208 }
209 return !mStringMap.empty();
210 }
211
212 /// @brief Returns a const reference to the string array map
213 ///
214 inline const StringArrayMap& getStringArrayMap() const {
215 return mStringMap;
216 }
217
218private:
219
220 const size_t mPointCount;
221 std::vector<std::unique_ptr<GroupArrayT>> mArrays;
222 points::GroupType mOffset;
223 std::map<std::string, std::unique_ptr<GroupHandleT>> mHandles;
224 StringArrayMap mStringMap;
225};
226
227} // codegen_internal
228
229} // namespace compiler
230} // namespace ax
231} // namespace OPENVDB_VERSION_NAME
232} // namespace openvdb
233
234#endif // OPENVDB_AX_COMPILER_LEAF_LOCAL_DATA_HAS_BEEN_INCLUDED
235
Assert.h
OPENVDB_ASSERT
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
AttributeArray.h
Attribute Array storage templated on type and compression codec.
PointAttribute.h
Point attribute manipulation in a VDB Point Grid.
PointDataGrid.h
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
PointGroup.h
Point group manipulation in a VDB Point Grid.
openvdb::v13_0::points::AttributeArray
Base class for storing attribute data.
Definition AttributeArray.h:94
openvdb::v13_0::points::AttributeSet::Descriptor::groupBits
static size_t groupBits()
Return number of bits occupied by a group attribute array.
Definition AttributeSet.h:454
openvdb::v13_0::points::StringMetaInserter
Class to help with insertion of keyed string values into metadata.
Definition AttributeArrayString.h:89
openvdb::v13_0::points::StringMetaInserter::insert
Index insert(const Name &name, Index hint=Index(0))
Insert the string into the metadata using the hint if non-zero.
openvdb::v13_0::points::GroupType
uint8_t GroupType
Definition AttributeSet.h:32
openvdb::v13_0::Index
Index32 Index
Definition Types.h:34
openvdb
Definition Exceptions.h:13
openvdb.h
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::compact
void compact()
Compact all arrays stored on this object. This does not invalidate any active write handles.
Definition PointLeafLocalData.h:143
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::PointStringMap
std::map< uint64_t, std::string > PointStringMap
Definition PointLeafLocalData.h:50
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::GroupHandleT
openvdb::points::GroupWriteHandle GroupHandleT
Definition PointLeafLocalData.h:48
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::PointLeafLocalData
PointLeafLocalData(const size_t count)
Construct a new data object to keep track of various data objects created per leaf by the point compu...
Definition PointLeafLocalData.h:61
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::UniquePtr
std::unique_ptr< PointLeafLocalData > UniquePtr
Definition PointLeafLocalData.h:46
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::hasGroup
bool hasGroup(const std::string &name) const
Return true if a valid group handle exists.
Definition PointLeafLocalData.h:124
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::LeafNode
openvdb::points::PointDataTree::LeafNodeType LeafNode
Definition PointLeafLocalData.h:53
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::get
GroupHandleT * get(const std::string &name) const
Return a group write handle to a specific group name if it exists. Returns a nullptr if no group exis...
Definition PointLeafLocalData.h:113
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::getOrInsert
GroupHandleT * getOrInsert(const std::string &name)
Group methods.
Definition PointLeafLocalData.h:79
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::GroupArrayT
openvdb::points::GroupAttributeArray GroupArrayT
Definition PointLeafLocalData.h:47
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::getGroups
void getGroups(std::set< std::string > &groups) const
Populate a set with all the groups which have been inserted into this object. Used to compute a final...
Definition PointLeafLocalData.h:134
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::getNewStringData
bool getNewStringData(const points::AttributeArray *array, const uint64_t idx, std::string &data) const
String methods.
Definition PointLeafLocalData.h:161
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::getStringArrayMap
const StringArrayMap & getStringArrayMap() const
Returns a const reference to the string array map.
Definition PointLeafLocalData.h:214
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::StringArrayMap
std::map< points::AttributeArray *, PointStringMap > StringArrayMap
Definition PointLeafLocalData.h:51
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::insertNewStrings
bool insertNewStrings(points::StringMetaInserter &inserter) const
Insert all new point strings stored across all collected string attribute arrays into a StringMetaIns...
Definition PointLeafLocalData.h:203
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::removeNewStringData
void removeNewStringData(points::AttributeArray *array, const uint64_t idx)
Remove any new string data associated with a particular point on a particular string attribute array....
Definition PointLeafLocalData.h:189
openvdb::v13_0::ax::codegen::codegen_internal::PointLeafLocalData::setNewStringData
void setNewStringData(points::AttributeArray *array, const uint64_t idx, const std::string &data)
Set new string data associated with a particular point on a particular string attribute array.
Definition PointLeafLocalData.h:178
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:218