OpenVDB  12.1.0
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>
20 #include <openvdb/util/Assert.h>
21 
22 namespace openvdb {
24 namespace OPENVDB_VERSION_NAME {
25 
26 namespace ax {
27 namespace codegen {
28 
29 namespace 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 ///
45 {
46  using UniquePtr = std::unique_ptr<PointLeafLocalData>;
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
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 
218 private:
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 
Index insert(const Name &name, Index hint=Index(0))
Insert the string into the metadata using the hint if non-zero.
Base class for storing attribute data.
Definition: AttributeArray.h:93
std::unique_ptr< PointLeafLocalData > UniquePtr
Definition: PointLeafLocalData.h:46
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
uint8_t GroupType
Definition: AttributeSet.h:32
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
bool hasGroup(const std::string &name) const
Return true if a valid group handle exists.
Definition: PointLeafLocalData.h:124
TypedAttributeArray< GroupType, GroupCodec > GroupAttributeArray
Definition: AttributeGroup.h:41
std::map< uint64_t, std::string > PointStringMap
Definition: PointLeafLocalData.h:50
const StringArrayMap & getStringArrayMap() const
Returns a const reference to the string array map.
Definition: PointLeafLocalData.h:214
openvdb::points::PointDataTree::LeafNodeType LeafNode
Definition: PointLeafLocalData.h:53
Attribute Array storage templated on type and compression codec.
openvdb::points::GroupAttributeArray GroupArrayT
Definition: PointLeafLocalData.h:47
openvdb::points::GroupWriteHandle GroupHandleT
Definition: PointLeafLocalData.h:48
std::map< points::AttributeArray *, PointStringMap > StringArrayMap
Definition: PointLeafLocalData.h:51
Point attribute manipulation in a VDB Point Grid.
Point group manipulation in a VDB Point Grid.
GroupHandleT * getOrInsert(const std::string &name)
Group methods.
Definition: PointLeafLocalData.h:79
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
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
Definition: Exceptions.h:13
void compact()
Compact all arrays stored on this object. This does not invalidate any active write handles...
Definition: PointLeafLocalData.h:143
bool getNewStringData(const points::AttributeArray *array, const uint64_t idx, std::string &data) const
String methods.
Definition: PointLeafLocalData.h:161
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
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
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
Class to help with insertion of keyed string values into metadata.
Definition: AttributeArrayString.h:88
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
Various functions can request the use and initialization of point data from within the kernel that do...
Definition: PointLeafLocalData.h:44
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:110
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218