OpenVDB 12.1.0

Loading...
Searching...
No Matches
Util.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef OPENVDB_UTIL_UTIL_HAS_BEEN_INCLUDED
5#define OPENVDB_UTIL_UTIL_HAS_BEEN_INCLUDED
6
7#include <openvdb/Types.h>
8#include <openvdb/tree/Tree.h>
9#include <openvdb/tools/ValueTransformer.h>
10#include <openvdb/tools/Prune.h>// for tree::pruneInactive
11
12#include <limits>
13
14 namespace openvdb {
15 OPENVDB_USE_VERSION_NAMESPACE
16 namespace OPENVDB_VERSION_NAME {
17 namespace util {
18
19 inline constexpr Index32 INVALID_IDX = std::numeric_limits<Index32>::max();
20
21/// @brief coordinate offset table for neighboring voxels
22 inline constexpr Coord COORD_OFFSETS[26] =
23{
24 Coord( 1, 0, 0), /// Voxel-face adjacent neghbours
25 Coord(-1, 0, 0), /// 0 to 5
26 Coord( 0, 1, 0),
27 Coord( 0, -1, 0),
28 Coord( 0, 0, 1),
29 Coord( 0, 0, -1),
30 Coord( 1, 0, -1), /// Voxel-edge adjacent neghbours
31 Coord(-1, 0, -1), /// 6 to 17
32 Coord( 1, 0, 1),
33 Coord(-1, 0, 1),
34 Coord( 1, 1, 0),
35 Coord(-1, 1, 0),
36 Coord( 1, -1, 0),
37 Coord(-1, -1, 0),
38 Coord( 0, -1, 1),
39 Coord( 0, -1, -1),
40 Coord( 0, 1, 1),
41 Coord( 0, 1, -1),
42 Coord(-1, -1, -1), /// Voxel-corner adjacent neghbours
43 Coord(-1, -1, 1), /// 18 to 25
44 Coord( 1, -1, 1),
45 Coord( 1, -1, -1),
46 Coord(-1, 1, -1),
47 Coord(-1, 1, 1),
48 Coord( 1, 1, 1),
49 Coord( 1, 1, -1)
50};
51
52////////////////////////////////////////
53
54
55/// Return @a voxelCoord rounded to the closest integer coordinates.
56 inline Coord
57 nearestCoord(const Vec3d& voxelCoord)
58{
59 Coord ijk;
60 ijk[0] = int(std::floor(voxelCoord[0]));
61 ijk[1] = int(std::floor(voxelCoord[1]));
62 ijk[2] = int(std::floor(voxelCoord[2]));
63 return ijk;
64}
65
66
67////////////////////////////////////////
68
69
70/// @brief Functor for use with tools::foreach() to compute the boolean intersection
71/// between the value masks of corresponding leaf nodes in two trees
72 template<class TreeType1, class TreeType2>
73 class LeafTopologyIntOp
74{
75 public:
76 LeafTopologyIntOp(const TreeType2& tree): mOtherTree(&tree) {}
77
78 inline void operator()(const typename TreeType1::LeafIter& lIter) const
79 {
80 const Coord xyz = lIter->origin();
81 const typename TreeType2::LeafNodeType* leaf = mOtherTree->probeConstLeaf(xyz);
82 if (leaf) {//leaf node
83 lIter->topologyIntersection(*leaf, zeroVal<typename TreeType1::ValueType>());
84 } else if (!mOtherTree->isValueOn(xyz)) {//inactive tile
85 lIter->setValuesOff();
86 }
87 }
88
89 private:
90 const TreeType2* mOtherTree;
91};
92
93
94/// @brief Functor for use with tools::foreach() to compute the boolean difference
95/// between the value masks of corresponding leaf nodes in two trees
96 template<class TreeType1, class TreeType2>
97 class LeafTopologyDiffOp
98{
99 public:
100 LeafTopologyDiffOp(const TreeType2& tree): mOtherTree(&tree) {}
101
102 inline void operator()(const typename TreeType1::LeafIter& lIter) const
103 {
104 const Coord xyz = lIter->origin();
105 const typename TreeType2::LeafNodeType* leaf = mOtherTree->probeConstLeaf(xyz);
106 if (leaf) {//leaf node
107 lIter->topologyDifference(*leaf, zeroVal<typename TreeType1::ValueType>());
108 } else if (mOtherTree->isValueOn(xyz)) {//active tile
109 lIter->setValuesOff();
110 }
111 }
112
113 private:
114 const TreeType2* mOtherTree;
115};
116
117
118////////////////////////////////////////
119
120
121/// @brief Perform a boolean intersection between two leaf nodes' topology masks.
122/// @return a pointer to a new, boolean-valued tree containing the overlapping voxels.
123 template<class TreeType1, class TreeType2>
124 inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
125 leafTopologyIntersection(const TreeType1& lhs, const TreeType2& rhs, bool threaded = true)
126{
127 typedef typename TreeType1::template ValueConverter<bool>::Type BoolTreeType;
128
129 typename BoolTreeType::Ptr topologyTree(new BoolTreeType(
130 lhs, /*inactiveValue=*/false, /*activeValue=*/true, TopologyCopy()));
131
132 tools::foreach(topologyTree->beginLeaf(),
133 LeafTopologyIntOp<BoolTreeType, TreeType2>(rhs), threaded);
134
135 tools::pruneInactive(*topologyTree, threaded);
136 return topologyTree;
137}
138
139
140/// @brief Perform a boolean difference between two leaf nodes' topology masks.
141/// @return a pointer to a new, boolean-valued tree containing the non-overlapping
142/// voxels from the lhs.
143 template<class TreeType1, class TreeType2>
144 inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
145 leafTopologyDifference(const TreeType1& lhs, const TreeType2& rhs, bool threaded = true)
146{
147 typedef typename TreeType1::template ValueConverter<bool>::Type BoolTreeType;
148
149 typename BoolTreeType::Ptr topologyTree(new BoolTreeType(
150 lhs, /*inactiveValue=*/false, /*activeValue=*/true, TopologyCopy()));
151
152 tools::foreach(topologyTree->beginLeaf(),
153 LeafTopologyDiffOp<BoolTreeType, TreeType2>(rhs), threaded);
154
155 tools::pruneInactive(*topologyTree, threaded);
156 return topologyTree;
157}
158
159} // namespace util
160} // namespace OPENVDB_VERSION_NAME
161} // namespace openvdb
162
163#endif // OPENVDB_UTIL_UTIL_HAS_BEEN_INCLUDED
Prune.h
Defined various multi-threaded utility functions for trees.
Tree.h
ValueTransformer.h
openvdb::v12_1::TopologyCopy
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition Types.h:754
openvdb::v12_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:26
openvdb::v12_1::util::LeafTopologyDiffOp
Functor for use with tools::foreach() to compute the boolean difference between the value masks of co...
Definition Util.h:98
openvdb::v12_1::util::LeafTopologyDiffOp::LeafTopologyDiffOp
LeafTopologyDiffOp(const TreeType2 &tree)
Definition Util.h:100
openvdb::v12_1::util::LeafTopologyDiffOp::operator()
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition Util.h:102
openvdb::v12_1::util::LeafTopologyIntOp
Functor for use with tools::foreach() to compute the boolean intersection between the value masks of ...
Definition Util.h:74
openvdb::v12_1::util::LeafTopologyIntOp::operator()
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition Util.h:78
openvdb::v12_1::util::LeafTopologyIntOp::LeafTopologyIntOp
LeafTopologyIntOp(const TreeType2 &tree)
Definition Util.h:76
openvdb::v12_1::math::Vec3d
Vec3< double > Vec3d
Definition Vec3.h:665
openvdb::v12_1::tools::foreach
void foreach(const IterT &iter, XformOp &op, bool threaded=true, bool shareOp=true)
Definition ValueTransformer.h:382
openvdb::v12_1::tools::pruneInactive
void pruneInactive(TreeT &tree, bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with background tiles any nodes whose values are a...
Definition Prune.h:355
openvdb::v12_1::tree
Definition PointDataGrid.h:170
openvdb::v12_1::util::INVALID_IDX
constexpr Index32 INVALID_IDX
Definition Util.h:19
openvdb::v12_1::util::leafTopologyDifference
TreeType1::template ValueConverter< bool >::Type::Ptr leafTopologyDifference(const TreeType1 &lhs, const TreeType2 &rhs, bool threaded=true)
Perform a boolean difference between two leaf nodes' topology masks.
Definition Util.h:145
openvdb::v12_1::util::COORD_OFFSETS
constexpr Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
Definition Util.h:22
openvdb::v12_1::util::nearestCoord
Coord nearestCoord(const Vec3d &voxelCoord)
Return voxelCoord rounded to the closest integer coordinates.
Definition Util.h:57
openvdb::v12_1::util::leafTopologyIntersection
TreeType1::template ValueConverter< bool >::Type::Ptr leafTopologyIntersection(const TreeType1 &lhs, const TreeType2 &rhs, bool threaded=true)
Perform a boolean intersection between two leaf nodes' topology masks.
Definition Util.h:125
openvdb::v12_1::zeroVal
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:70
openvdb::v12_1::Index32
uint32_t Index32
Definition Types.h:52
openvdb
Definition Exceptions.h:13
Types.h
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