OpenVDB: PointCount.h Source File

OpenVDB 7.0.0

PointCount.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
9
10 #ifndef OPENVDB_POINTS_POINT_COUNT_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_POINT_COUNT_HAS_BEEN_INCLUDED
12
13 #include <openvdb/openvdb.h>
14
15 #include "PointDataGrid.h"
16 #include "PointMask.h"
17 #include "IndexFilter.h"
18
19 #include <tbb/parallel_reduce.h>
20
21 #include <vector>
22
23
24 namespace openvdb {
25 OPENVDB_USE_VERSION_NAMESPACE
26 namespace OPENVDB_VERSION_NAME {
27 namespace points {
28
29
35 template <typename PointDataTreeT, typename FilterT = NullFilter>
36 inline Index64 pointCount( const PointDataTreeT& tree,
37 const FilterT& filter = NullFilter(),
38 const bool inCoreOnly = false,
39 const bool threaded = true);
40
41
49 template <typename PointDataTreeT, typename FilterT = NullFilter>
50 inline Index64 pointOffsets(std::vector<Index64>& pointOffsets,
51 const PointDataTreeT& tree,
52 const FilterT& filter = NullFilter(),
53 const bool inCoreOnly = false,
54 const bool threaded = true);
55
56
61 template <typename PointDataGridT,
62 typename GridT = typename PointDataGridT::template ValueConverter<Int32>::Type,
63 typename FilterT = NullFilter>
64 inline typename GridT::Ptr
65 pointCountGrid( const PointDataGridT& grid,
66 const FilterT& filter = NullFilter());
67
68
75 template <typename PointDataGridT,
76 typename GridT = typename PointDataGridT::template ValueConverter<Int32>::Type,
77 typename FilterT = NullFilter>
78 inline typename GridT::Ptr
79 pointCountGrid( const PointDataGridT& grid,
80 const openvdb::math::Transform& transform,
81 const FilterT& filter = NullFilter());
82
83
85
86
87 template <typename PointDataTreeT, typename FilterT>
88 Index64 pointCount(const PointDataTreeT& tree,
89 const FilterT& filter,
90 const bool inCoreOnly,
91 const bool threaded)
92 {
93 using LeafManagerT = tree::LeafManager<const PointDataTreeT>;
94 using LeafRangeT = typename LeafManagerT::LeafRange;
95
96 auto countLambda =
97 [&filter, &inCoreOnly] (const LeafRangeT& range, Index64 sum) -> Index64 {
98 for (const auto& leaf : range) {
99 if (inCoreOnly && leaf.buffer().isOutOfCore()) continue;
100 auto state = filter.state(leaf);
101 if (state == index::ALL) {
102 sum += leaf.pointCount();
103 } else if (state != index::NONE) {
104 sum += iterCount(leaf.beginIndexAll(filter));
105 }
106 }
107 return sum;
108 };
109
110 LeafManagerT leafManager(tree);
111 if (threaded) {
112 return tbb::parallel_reduce(leafManager.leafRange(), Index64(0), countLambda,
113 [] (Index64 n, Index64 m) -> Index64 { return n + m; });
114 }
115 else {
116 return countLambda(leafManager.leafRange(), Index64(0));
117 }
118 }
119
120
121 template <typename PointDataTreeT, typename FilterT>
122 Index64 pointOffsets( std::vector<Index64>& pointOffsets,
123 const PointDataTreeT& tree,
124 const FilterT& filter,
125 const bool inCoreOnly,
126 const bool threaded)
127 {
128 using LeafT = typename PointDataTreeT::LeafNodeType;
129 using LeafManagerT = typename tree::LeafManager<const PointDataTreeT>;
130
131 // allocate and zero values in point offsets array
132
133 pointOffsets.assign(tree.leafCount(), Index64(0));
134
135 // compute total points per-leaf
136
137 LeafManagerT leafManager(tree);
138 leafManager.foreach(
139 [&pointOffsets, &filter, &inCoreOnly](const LeafT& leaf, size_t pos) {
140 if (inCoreOnly && leaf.buffer().isOutOfCore()) return;
141 auto state = filter.state(leaf);
142 if (state == index::ALL) {
143 pointOffsets[pos] = leaf.pointCount();
144 } else if (state != index::NONE) {
145 pointOffsets[pos] = iterCount(leaf.beginIndexAll(filter));
146 }
147 },
148 threaded);
149
150 // turn per-leaf totals into cumulative leaf totals
151
152 Index64 pointOffset(pointOffsets[0]);
153 for (size_t n = 1; n < pointOffsets.size(); n++) {
154 pointOffset += pointOffsets[n];
155 pointOffsets[n] = pointOffset;
156 }
157
158 return pointOffset;
159 }
160
161
162 template <typename PointDataGridT, typename GridT, typename FilterT>
163 typename GridT::Ptr
164 pointCountGrid( const PointDataGridT& points,
165 const FilterT& filter)
166 {
167 static_assert( std::is_integral<typename GridT::ValueType>::value ||
168 std::is_floating_point<typename GridT::ValueType>::value,
169 "openvdb::points::pointCountGrid must return an integer or floating-point scalar grid");
170
171 // This is safe because the PointDataGrid can only be modified by the deformer
172 using AdapterT = TreeAdapter<typename PointDataGridT::TreeType>;
173 auto& nonConstPoints = const_cast<typename AdapterT::NonConstGridType&>(points);
174
175 return point_mask_internal::convertPointsToScalar<GridT>(
176 nonConstPoints, filter);
177 }
178
179
180 template <typename PointDataGridT, typename GridT, typename FilterT>
181 typename GridT::Ptr
182 pointCountGrid( const PointDataGridT& points,
183 const openvdb::math::Transform& transform,
184 const FilterT& filter)
185 {
186 static_assert( std::is_integral<typename GridT::ValueType>::value ||
187 std::is_floating_point<typename GridT::ValueType>::value,
188 "openvdb::points::pointCountGrid must return an integer or floating-point scalar grid");
189
190 // This is safe because the PointDataGrid can only be modified by the deformer
191 using AdapterT = TreeAdapter<typename PointDataGridT::TreeType>;
192 auto& nonConstPoints = const_cast<typename AdapterT::NonConstGridType&>(points);
193
194 NullDeformer deformer;
195 return point_mask_internal::convertPointsToScalar<GridT>(
196 nonConstPoints, transform, filter, deformer);
197 }
198
199
201
202
203 // deprecated functions
204
205
206 template <typename PointDataTreeT>
207 OPENVDB_DEPRECATED
208 inline Index64 pointCount(const PointDataTreeT& tree, const bool inCoreOnly)
209 {
210 NullFilter filter;
211 return pointCount(tree, filter, inCoreOnly);
212 }
213
214
215 template <typename PointDataTreeT>
216 OPENVDB_DEPRECATED
217 inline Index64 activePointCount(const PointDataTreeT& tree, const bool inCoreOnly = true)
218 {
219 ActiveFilter filter;
220 return pointCount(tree, filter, inCoreOnly);
221 }
222
223
224 template <typename PointDataTreeT>
225 OPENVDB_DEPRECATED
226 inline Index64 inactivePointCount(const PointDataTreeT& tree, const bool inCoreOnly = true)
227 {
228 InactiveFilter filter;
229 return pointCount(tree, filter, inCoreOnly);
230 }
231
232
233 template <typename PointDataTreeT>
234 OPENVDB_DEPRECATED
235 inline Index64 groupPointCount(const PointDataTreeT& tree, const Name& name,
236 const bool inCoreOnly = true)
237 {
238 auto iter = tree.cbeginLeaf();
239 if (!iter || !iter->attributeSet().descriptor().hasGroup(name)) {
240 return Index64(0);
241 }
242 GroupFilter filter(name, iter->attributeSet());
243 return pointCount(tree, filter, inCoreOnly);
244 }
245
246
247 template <typename PointDataTreeT>
248 OPENVDB_DEPRECATED
249 inline Index64 activeGroupPointCount(const PointDataTreeT& tree, const Name& name,
250 const bool inCoreOnly = true)
251 {
252 auto iter = tree.cbeginLeaf();
253 if (!iter || !iter->attributeSet().descriptor().hasGroup(name)) {
254 return Index64(0);
255 }
256 BinaryFilter<GroupFilter, ActiveFilter> filter(GroupFilter(name, iter->attributeSet()), ActiveFilter());
257 return pointCount(tree, filter, inCoreOnly);
258 }
259
260
261 template <typename PointDataTreeT>
262 OPENVDB_DEPRECATED
263 inline Index64 inactiveGroupPointCount(const PointDataTreeT& tree, const Name& name,
264 const bool inCoreOnly = true)
265 {
266 auto iter = tree.cbeginLeaf();
267 if (!iter || !iter->attributeSet().descriptor().hasGroup(name)) {
268 return Index64(0);
269 }
270 BinaryFilter<GroupFilter, InactiveFilter> filter(GroupFilter(name, iter->attributeSet()), InactiveFilter());
271 return pointCount(tree, filter, inCoreOnly);
272 }
273
274
275 template <typename PointDataTreeT>
276 OPENVDB_DEPRECATED
277 inline Index64 getPointOffsets(std::vector<Index64>& offsets, const PointDataTreeT& tree,
278 const std::vector<Name>& includeGroups,
279 const std::vector<Name>& excludeGroups,
280 const bool inCoreOnly = false)
281 {
282 MultiGroupFilter filter(includeGroups, excludeGroups, tree.cbeginLeaf()->attributeSet());
283 return pointOffsets(offsets, tree, filter, inCoreOnly);
284 }
285
286
287 template <typename PointDataGridT,
288 typename GridT = typename PointDataGridT::template ValueConverter<Int32>::Type>
289 OPENVDB_DEPRECATED
290 inline typename GridT::Ptr
291 pointCountGrid(const PointDataGridT& grid,
292 const std::vector<Name>& includeGroups,
293 const std::vector<Name>& excludeGroups)
294 {
295 auto leaf = grid.tree().cbeginLeaf();
296 if (!leaf) return GridT::create(0);
297 MultiGroupFilter filter(includeGroups, excludeGroups, leaf->attributeSet());
298 return pointCountGrid(grid, filter);
299 }
300
301
302 template <typename PointDataGridT,
303 typename GridT = typename PointDataGridT::template ValueConverter<Int32>::Type>
304 OPENVDB_DEPRECATED
305 inline typename GridT::Ptr
306 pointCountGrid(const PointDataGridT& grid,
307 const openvdb::math::Transform& transform,
308 const std::vector<Name>& includeGroups,
309 const std::vector<Name>& excludeGroups)
310 {
311 auto leaf = grid.tree().cbeginLeaf();
312 if (!leaf) return GridT::create(0);
313 MultiGroupFilter filter(includeGroups, excludeGroups, leaf->attributeSet());
314 return pointCountGrid(grid, transform, filter);
315 }
316
317
319
320
321 } // namespace points
322 } // namespace OPENVDB_VERSION_NAME
323 } // namespace openvdb
324
325 #endif // OPENVDB_POINTS_POINT_COUNT_HAS_BEEN_INCLUDED
openvdb::v7_0::points::pointCount
OPENVDB_DEPRECATED Index64 pointCount(const PointDataTreeT &tree, const bool inCoreOnly)
Definition: PointCount.h:208
PointMask.h
Methods for extracting masks from VDB Point grids.
openvdb::v7_0::points::activeGroupPointCount
OPENVDB_DEPRECATED Index64 activeGroupPointCount(const PointDataTreeT &tree, const Name &name, const bool inCoreOnly=true)
Definition: PointCount.h:249
openvdb::v7_0::points::index::NONE
Definition: IndexIterator.h:42
openvdb::v7_0::tree::Tree::ValueConverter< Int32 >::Type
Tree< typename RootNodeType::template ValueConverter< Int32 >::Type > Type
Definition: Tree.h:197
IndexFilter.h
Index filters primarily designed to be used with a FilterIndexIter.
openvdb::v7_0::points::getPointOffsets
OPENVDB_DEPRECATED Index64 getPointOffsets(std::vector< Index64 > &offsets, const PointDataTreeT &tree, const std::vector< Name > &includeGroups, const std::vector< Name > &excludeGroups, const bool inCoreOnly=false)
Definition: PointCount.h:277
openvdb::v7_0::points::InactiveFilter
ValueMaskFilter< false > InactiveFilter
Definition: IndexFilter.h:129
OPENVDB_DEPRECATED
#define OPENVDB_DEPRECATED
Definition: Platform.h:42
PointDataGrid.h
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
openvdb::v7_0::points::ActiveFilter
ValueMaskFilter< true > ActiveFilter
Definition: IndexFilter.h:128
openvdb::v7_0::points::iterCount
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:314
openvdb::v7_0::points::inactiveGroupPointCount
OPENVDB_DEPRECATED Index64 inactiveGroupPointCount(const PointDataTreeT &tree, const Name &name, const bool inCoreOnly=true)
Definition: PointCount.h:263
openvdb::v7_0::tree::LeafManager
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:82
openvdb::v7_0::points::activePointCount
OPENVDB_DEPRECATED Index64 activePointCount(const PointDataTreeT &tree, const bool inCoreOnly=true)
Definition: PointCount.h:217
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::TreeAdapter
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1065
openvdb::v7_0::points::groupPointCount
OPENVDB_DEPRECATED Index64 groupPointCount(const PointDataTreeT &tree, const Name &name, const bool inCoreOnly=true)
Definition: PointCount.h:235
openvdb::v7_0::points::NullFilter
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:50
openvdb::v7_0::points::pointCountGrid
OPENVDB_DEPRECATED GridT::Ptr pointCountGrid(const PointDataGridT &grid, const openvdb::math::Transform &transform, const std::vector< Name > &includeGroups, const std::vector< Name > &excludeGroups)
Definition: PointCount.h:306
openvdb::v7_0::points::inactivePointCount
OPENVDB_DEPRECATED Index64 inactivePointCount(const PointDataTreeT &tree, const bool inCoreOnly=true)
Definition: PointCount.h:226
openvdb::v7_0::Name
std::string Name
Definition: Name.h:17
openvdb::v7_0::points::ValueMaskFilter
Index filtering on active / inactive state of host voxel.
Definition: IndexFilter.h:103
openvdb::v7_0::points::NullDeformer
No-op deformer (adheres to the deformer interface documented in PointMove.h)
Definition: PointMask.h:64
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::points::index::ALL
Definition: IndexIterator.h:43
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::Index64
uint64_t Index64
Definition: Types.h:30
openvdb::v7_0::points::MultiGroupFilter
Definition: IndexFilter.h:135
openvdb.h
openvdb::v7_0::points::GroupFilter
Index filtering on group membership.
Definition: AttributeGroup.h:132
openvdb::v7_0::points::BinaryFilter
Definition: IndexFilter.h:501
openvdb::v7_0::points::pointOffsets
Index64 pointOffsets(std::vector< Index64 > &pointOffsets, const PointDataTreeT &tree, const FilterT &filter=NullFilter(), const bool inCoreOnly=false, const bool threaded=true)
Populate an array of cumulative point offsets per leaf node.
Definition: PointCount.h:122