OpenVDB 11.0.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: MPL-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>
10#include <openvdb/tools/Prune.h>// for tree::pruneInactive
11
12#include <limits>
13
14namespace openvdb {
16namespace OPENVDB_VERSION_NAME {
17namespace util {
18
19inline constexpr Index32 INVALID_IDX = std::numeric_limits<Index32>::max();
20
21/// @brief coordinate offset table for neighboring voxels
22inline 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.
56inline Coord
57nearestCoord(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
72template<class TreeType1, class TreeType2>
74{
75public:
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
89private:
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
96template<class TreeType1, class TreeType2>
98{
99public:
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
113private:
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.
123template<class TreeType1, class TreeType2>
124inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
125leafTopologyIntersection(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(),
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.
143template<class TreeType1, class TreeType2>
144inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
145leafTopologyDifference(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(),
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
Defined various multi-threaded utility functions for trees.
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition Types.h:683
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:25
Functor for use with tools::foreach() to compute the boolean difference between the value masks of co...
Definition Util.h:98
LeafTopologyDiffOp(const TreeType2 &tree)
Definition Util.h:100
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition Util.h:102
Functor for use with tools::foreach() to compute the boolean intersection between the value masks of ...
Definition Util.h:74
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition Util.h:78
LeafTopologyIntOp(const TreeType2 &tree)
Definition Util.h:76
constexpr Index32 INVALID_IDX
Definition Util.h:19
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
constexpr Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
Definition Util.h:22
Coord nearestCoord(const Vec3d &voxelCoord)
Return voxelCoord rounded to the closest integer coordinates.
Definition Util.h:57
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
uint32_t Index32
Definition Types.h:52
Definition Exceptions.h:13
#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:212