HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
14 namespace openvdb {
16 namespace OPENVDB_VERSION_NAME {
17 namespace util {
18 
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>
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>
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(),
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(),
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
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
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
constexpr Index32 INVALID_IDX
Definition: Util.h:19
IMATH_HOSTDEVICE constexpr int floor(T x) IMATH_NOEXCEPT
Definition: ImathFun.h:112
constexpr Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
Definition: Util.h:22
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:239
Functor for use with tools::foreach() to compute the boolean difference between the value masks of co...
Definition: Util.h:97
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition: Util.h:78
Coord nearestCoord(const Vec3d &voxelCoord)
Return voxelCoord rounded to the closest integer coordinates.
Definition: Util.h:57
void foreach(const IterT &iter, XformOp &op, bool threaded=true, bool shareOp=true)
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:73
Defined various multi-threaded utility functions for trees.
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
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:683
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:119
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