OpenVDB 14.0.0
Loading...
Searching...
No Matches
Count.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 Count.h
5///
6/// @brief Functions to count tiles, nodes or voxels in a grid
7///
8/// @author Dan Bailey
9///
10
11#ifndef OPENVDB_TOOLS_COUNT_HAS_BEEN_INCLUDED
12#define OPENVDB_TOOLS_COUNT_HAS_BEEN_INCLUDED
13
14#include <openvdb/version.h>
15#include <openvdb/math/Stats.h>
18
19namespace openvdb {
21namespace OPENVDB_VERSION_NAME {
22namespace tools {
23
24
25/// @brief Return the total number of active voxels in the tree.
26template <typename TreeT>
27Index64 countActiveVoxels(const TreeT& tree, bool threaded = true);
28
29
30/// @brief Return the total number of active voxels in the tree that intersects
31/// a bounding box.
32template <typename TreeT>
33Index64 countActiveVoxels(const TreeT& tree, const CoordBBox& bbox, bool threaded = true);
34
35
36/// @brief Return the total number of active voxels stored in leaf nodes.
37template <typename TreeT>
38Index64 countActiveLeafVoxels(const TreeT& tree, bool threaded = true);
39
40
41/// @brief Return the total number of active voxels stored in leaf nodes that intersects
42/// a bounding box.
43template <typename TreeT>
44Index64 countActiveLeafVoxels(const TreeT& tree, const CoordBBox& bbox, bool threaded = true);
45
46
47/// @brief Return the total number of inactive voxels in the tree.
48template <typename TreeT>
49Index64 countInactiveVoxels(const TreeT& tree, bool threaded = true);
50
51
52/// @brief Return the total number of inactive voxels stored in leaf nodes.
53template <typename TreeT>
54Index64 countInactiveLeafVoxels(const TreeT& tree, bool threaded = true);
55
56
57/// @brief Return the total number of active tiles in the tree.
58template <typename TreeT>
59Index64 countActiveTiles(const TreeT& tree, bool threaded = true);
60
61
62/// @brief Return the total amount of memory in bytes occupied by this tree.
63template <typename TreeT>
64Index64 memUsage(const TreeT& tree, bool threaded = true);
65
66
67
68/// @brief Return the minimum and maximum active values in this tree.
69/// @note Returns zeroVal<ValueType> for empty trees.
70template <typename TreeT>
71math::MinMax<typename TreeT::ValueType> minMax(const TreeT& tree, bool threaded = true);
72
73
74////////////////////////////////////////
75
76/// @cond OPENVDB_DOCS_INTERNAL
77
78namespace count_internal {
79
80/// @brief A DynamicNodeManager operator to count active voxels in a tree
81template<typename TreeType>
82struct ActiveVoxelCountOp
83{
84 using LeafT = typename TreeType::LeafNodeType;
85
86 ActiveVoxelCountOp() = default;
87 ActiveVoxelCountOp(const ActiveVoxelCountOp&, tbb::split) { }
88
89 // accumulate all voxels in active tile children
90 template<typename NodeT>
91 bool operator()(const NodeT& node, size_t)
92 {
93 for (auto iter = node.cbeginValueOn(); iter; ++iter) {
94 count += NodeT::ChildNodeType::NUM_VOXELS;
95 }
96 return true;
97 }
98
99 // accumulate all active voxels in the leaf
100 bool operator()(const LeafT& leaf, size_t)
101 {
102 count += leaf.onVoxelCount();
103 return false;
104 }
105
106 void join(const ActiveVoxelCountOp& other)
107 {
108 count += other.count;
109 }
110
111 openvdb::Index64 count{0};
112}; // struct ActiveVoxelCountOp
113
114/// @brief A DynamicNodeManager operator to count active voxels in a tree
115/// that fall within a provided bounding box
116template<typename TreeType>
117struct ActiveVoxelCountBBoxOp
118{
119 using LeafT = typename TreeType::LeafNodeType;
120
121 explicit ActiveVoxelCountBBoxOp(const CoordBBox& bbox)
122 : mBBox(bbox) { }
123 ActiveVoxelCountBBoxOp(const ActiveVoxelCountBBoxOp& other, tbb::split)
124 : mBBox(other.mBBox) { }
125
126 // accumulate all voxels in active tile children bounded by the bbox
127 template<typename NodeT>
128 bool operator()(const NodeT& node, size_t)
129 {
130 if (!mBBox.hasOverlap(node.getNodeBoundingBox())) return false;
131
132 // count any overlapping regions in active tiles
133 for (auto iter = node.cbeginValueOn(); iter; ++iter) {
134 CoordBBox bbox(CoordBBox::createCube(iter.getCoord(), NodeT::ChildNodeType::DIM));
135
136 if (!bbox.hasOverlap(mBBox)) {
137 // box is completely outside the active tile
138 continue;
139 } else if (bbox.isInside(mBBox)) {
140 // bbox is completely inside the active tile
141 count += mBBox.volume();
142 } else if (mBBox.isInside(bbox)) {
143 // active tile is completely inside bbox
144 count += bbox.volume();
145 } else {
146 // partial overlap between tile and bbox
147 bbox.intersect(mBBox);
148 count += bbox.volume();
149 }
150 }
151
152 // return true if any child nodes overlap with the bounding box
153 for (auto iter = node.cbeginChildOn(); iter; ++iter) {
154 if (mBBox.hasOverlap(iter->getNodeBoundingBox())) return true;
155 }
156
157 // otherwise return false to prevent recursion along this branch
158 return false;
159 }
160
161 // accumulate all active voxels in the leaf bounded by the bbox
162 inline bool operator()(const LeafT& leaf, size_t)
163 {
164 // note: the true/false return value does nothing
165
166 CoordBBox bbox = leaf.getNodeBoundingBox();
167
168 if (mBBox.isInside(bbox)) {
169 // leaf node is completely inside bbox
170 count += leaf.onVoxelCount();
171 } else if (!bbox.hasOverlap(mBBox)) {
172 // bbox is completely outside the leaf node
173 return false;
174 } else if (leaf.isDense()) {
175 // partial overlap between dense leaf node and bbox
176 bbox.intersect(mBBox);
177 count += bbox.volume();
178 } else {
179 // partial overlap between sparse leaf node and bbox
180 for (auto i = leaf.cbeginValueOn(); i; ++i) {
181 if (mBBox.isInside(i.getCoord())) ++count;
182 }
183 }
184 return false;
185 }
186
187 void join(const ActiveVoxelCountBBoxOp& other)
188 {
189 count += other.count;
190 }
191
192 openvdb::Index64 count{0};
193private:
194 CoordBBox mBBox;
195}; // struct ActiveVoxelCountBBoxOp
196
197/// @brief A DynamicNodeManager operator to count inactive voxels in a tree
198template<typename TreeType>
199struct InactiveVoxelCountOp
200{
201 using RootT = typename TreeType::RootNodeType;
202 using LeafT = typename TreeType::LeafNodeType;
203
204 InactiveVoxelCountOp() = default;
205 InactiveVoxelCountOp(const InactiveVoxelCountOp&, tbb::split) { }
206
207 // accumulate all inactive voxels in the root node
208 bool operator()(const RootT& root, size_t)
209 {
210 for (auto iter = root.cbeginValueOff(); iter; ++iter) {
211 // background tiles are not considered to contain inactive voxels
212 if (!math::isApproxEqual(*iter, root.background())) {
213 count += RootT::ChildNodeType::NUM_VOXELS;
214 }
215 }
216 return true;
217 }
218
219 // accumulate all voxels in inactive tile children
220 template<typename NodeT>
221 bool operator()(const NodeT& node, size_t)
222 {
223 for (auto iter = node.cbeginValueOff(); iter; ++iter) {
224 if (node.isChildMaskOff(iter.pos())) {
225 count += NodeT::ChildNodeType::NUM_VOXELS;
226 }
227 }
228 return true;
229 }
230
231 // accumulate all inactive voxels in the leaf
232 bool operator()(const LeafT& leaf, size_t)
233 {
234 count += leaf.offVoxelCount();
235 return false;
236 }
237
238 void join(const InactiveVoxelCountOp& other)
239 {
240 count += other.count;
241 }
242
243 openvdb::Index64 count{0};
244}; // struct InactiveVoxelCountOp
245
246/// @brief A DynamicNodeManager operator to count active tiles in a tree
247template<typename TreeType>
248struct ActiveTileCountOp
249{
250 using RootT = typename TreeType::RootNodeType;
251 using LeafT = typename TreeType::LeafNodeType;
252
253 ActiveTileCountOp() = default;
254 ActiveTileCountOp(const ActiveTileCountOp&, tbb::split) { }
255
256 // accumulate all active tiles in root node
257 bool operator()(const RootT& root, size_t)
258 {
259 for (auto iter = root.cbeginValueOn(); iter; ++iter) count++;
260 return true;
261 }
262
263 // accumulate all active tiles in internal node
264 template<typename NodeT>
265 bool operator()(const NodeT& node, size_t)
266 {
267 count += node.getValueMask().countOn();
268 return true;
269 }
270
271 // do nothing (leaf nodes cannot contain tiles)
272 bool operator()(const LeafT&, size_t)
273 {
274 return false;
275 }
276
277 void join(const ActiveTileCountOp& other)
278 {
279 count += other.count;
280 }
281
282 openvdb::Index64 count{0};
283}; // struct ActiveTileCountOp
284
285/// @brief A DynamicNodeManager operator to sum the number of bytes of memory used
286template<typename TreeType>
287struct MemUsageOp
288{
289 using RootT = typename TreeType::RootNodeType;
290 using LeafT = typename TreeType::LeafNodeType;
291
292 MemUsageOp() = default;
293 MemUsageOp(const MemUsageOp& other) = default;
294 MemUsageOp(const MemUsageOp&, tbb::split) : mCount(0) {}
295
296 // accumulate size of the root node in bytes
297 bool operator()(const RootT& root, size_t)
298 {
299 mCount += sizeof(root);
300 return true;
301 }
302
303 // accumulate size of all child nodes in bytes
304 template<typename NodeT>
305 bool operator()(const NodeT& node, size_t)
306 {
307 mCount += NodeT::NUM_VALUES * sizeof(typename NodeT::UnionType) +
308 node.getChildMask().memUsage() + node.getValueMask().memUsage() +
309 sizeof(Coord);
310 return true;
311 }
312
313 // accumulate size of leaf node in bytes
314 bool operator()(const LeafT& leaf, size_t)
315 {
316 mCount += leaf.memUsage();
317 return false;
318 }
319
320 void join(const MemUsageOp& other)
321 {
322 mCount += other.mCount;
323 }
324
325 openvdb::Index64 mCount{0};
326}; // struct MemUsageOp
327
328/// @brief A DynamicNodeManager operator to find the minimum and maximum active values in this tree.
329template<typename TreeType>
330struct MinMaxValuesOp
331{
332 using ValueT = typename TreeType::ValueType;
333
334 explicit MinMaxValuesOp()
335 : min(zeroVal<ValueT>())
336 , max(zeroVal<ValueT>())
337 , seen_value(false) {}
338
339 MinMaxValuesOp(const MinMaxValuesOp&, tbb::split)
340 : MinMaxValuesOp() {}
341
342 template <typename NodeType>
343 bool operator()(NodeType& node, size_t)
344 {
345 if (auto iter = node.cbeginValueOn()) {
346 if (!seen_value) {
347 seen_value = true;
348 min = max = *iter;
349 ++iter;
350 }
351
352 for (; iter; ++iter) {
353 const ValueT val = *iter;
354
355 if (math::cwiseLessThan(val, min))
356 min = val;
357
358 if (math::cwiseGreaterThan(val, max))
359 max = val;
360 }
361 }
362
363 return true;
364 }
365
366 bool join(const MinMaxValuesOp& other)
367 {
368 if (!other.seen_value) return true;
369
370 if (!seen_value) {
371 min = other.min;
372 max = other.max;
373 }
374 else {
375 if (math::cwiseLessThan(other.min, min))
376 min = other.min;
377 if (math::cwiseGreaterThan(other.max, max))
378 max = other.max;
379 }
380
381 seen_value = true;
382 return true;
383 }
384
385 ValueT min, max;
386
387private:
388
389 bool seen_value;
390}; // struct MinMaxValuesOp
391
392} // namespace count_internal
393
394/// @endcond
395
396
397////////////////////////////////////////
398
399
400template <typename TreeT>
401Index64 countActiveVoxels(const TreeT& tree, bool threaded)
402{
403 count_internal::ActiveVoxelCountOp<TreeT> op;
405 nodeManager.reduceTopDown(op, threaded);
406 return op.count;
407}
408
409
410template <typename TreeT>
411Index64 countActiveVoxels(const TreeT& tree, const CoordBBox& bbox, bool threaded)
412{
413 if (bbox.empty()) return Index64(0);
414 else if (bbox == CoordBBox::inf()) return countActiveVoxels(tree, threaded);
415
416 count_internal::ActiveVoxelCountBBoxOp<TreeT> op(bbox);
418 nodeManager.reduceTopDown(op, threaded);
419 return op.count;
420}
421
422
423template <typename TreeT>
424Index64 countActiveLeafVoxels(const TreeT& tree, bool threaded)
425{
426 count_internal::ActiveVoxelCountOp<TreeT> op;
427 // use a leaf manager instead of a node manager
429 leafManager.reduce(op, threaded);
430 return op.count;
431}
432
433
434template <typename TreeT>
435Index64 countActiveLeafVoxels(const TreeT& tree, const CoordBBox& bbox, bool threaded)
436{
437 if (bbox.empty()) return Index64(0);
438 else if (bbox == CoordBBox::inf()) return countActiveLeafVoxels(tree, threaded);
439
440 count_internal::ActiveVoxelCountBBoxOp<TreeT> op(bbox);
441 // use a leaf manager instead of a node manager
443 leafManager.reduce(op, threaded);
444 return op.count;
445}
446
447
448template <typename TreeT>
449Index64 countInactiveVoxels(const TreeT& tree, bool threaded)
450{
451 count_internal::InactiveVoxelCountOp<TreeT> op;
453 nodeManager.reduceTopDown(op, threaded);
454 return op.count;
455}
456
457
458template <typename TreeT>
459Index64 countInactiveLeafVoxels(const TreeT& tree, bool threaded)
460{
461 count_internal::InactiveVoxelCountOp<TreeT> op;
462 // use a leaf manager instead of a node manager
464 leafManager.reduce(op, threaded);
465 return op.count;
466}
467
468
469template <typename TreeT>
470Index64 countActiveTiles(const TreeT& tree, bool threaded)
471{
472 count_internal::ActiveTileCountOp<TreeT> op;
473 // exclude leaf nodes as they cannot contain tiles
474 tree::DynamicNodeManager<const TreeT, TreeT::DEPTH-2> nodeManager(tree);
475 nodeManager.reduceTopDown(op, threaded);
476 return op.count;
477}
478
479
480template <typename TreeT>
481Index64 memUsage(const TreeT& tree, bool threaded)
482{
483 count_internal::MemUsageOp<TreeT> op;
485 nodeManager.reduceTopDown(op, threaded);
486 return op.mCount + sizeof(tree);
487}
488
489
490template <typename TreeT>
492{
493 using ValueT = typename TreeT::ValueType;
494
495 count_internal::MinMaxValuesOp<TreeT> op;
497 nodeManager.reduceTopDown(op, threaded);
498
499 return math::MinMax<ValueT>(op.min, op.max);
500}
501
502
503} // namespace tools
504} // namespace OPENVDB_VERSION_NAME
505} // namespace openvdb
506
507#endif // OPENVDB_TOOLS_COUNT_HAS_BEEN_INCLUDED
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
Classes to compute statistics and histograms.
static CoordBBox inf()
Return an "infinite" bounding box, as defined by the Coord value range.
Definition Coord.h:322
Axis-aligned bounding box of signed integer coordinates.
Definition Coord.h:252
bool empty() const
Return true if this bounding box is empty (i.e., encloses no coordinates).
Definition Coord.h:359
Templated class to compute the minimum and maximum values.
Definition Stats.h:32
Definition NodeManager.h:891
void reduceTopDown(NodeOp &op, bool threaded=true, size_t leafGrainSize=1, size_t nonLeafGrainSize=1)
Threaded method that processes nodes with a user supplied functor.
Definition NodeManager.h:1044
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition LeafManager.h:86
void reduce(LeafOp &op, bool threaded=true, size_t grainSize=1)
Threaded method that applies a user-supplied functor to each leaf node in the LeafManager....
Definition LeafManager.h:533
math::BBox< Coord > CoordBBox
Definition Math.h:2838
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition Composite.h:110
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition Composite.h:106
Index64 countActiveLeafVoxels(const TreeT &tree, bool threaded=true)
Return the total number of active voxels stored in leaf nodes.
Definition Count.h:424
math::MinMax< typename TreeT::ValueType > minMax(const TreeT &tree, bool threaded=true)
Return the minimum and maximum active values in this tree.
Definition Count.h:491
Index64 countInactiveVoxels(const TreeT &tree, bool threaded=true)
Return the total number of inactive voxels in the tree.
Definition Count.h:449
Index64 countActiveVoxels(const TreeT &tree, bool threaded=true)
Return the total number of active voxels in the tree.
Definition Count.h:401
Index64 memUsage(const TreeT &tree, bool threaded=true)
Return the total amount of memory in bytes occupied by this tree.
Definition Count.h:481
Index64 countInactiveLeafVoxels(const TreeT &tree, bool threaded=true)
Return the total number of inactive voxels stored in leaf nodes.
Definition Count.h:459
Index64 countActiveTiles(const TreeT &tree, bool threaded=true)
Return the total number of active tiles in the tree.
Definition Count.h:470
Definition PointDataGrid.h:48
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:71
uint64_t Index64
Definition Types.h:33
Definition Exceptions.h:13
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:291