HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Util.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_UTIL_UTIL_HAS_BEEN_INCLUDED
32 #define OPENVDB_UTIL_UTIL_HAS_BEEN_INCLUDED
33 
34 #include <openvdb/Types.h>
35 #include <openvdb/tree/Tree.h>
37 #include <openvdb/tools/Prune.h>// for tree::pruneInactive
38 
39 
40 namespace openvdb {
42 namespace OPENVDB_VERSION_NAME {
43 namespace util {
44 
45 OPENVDB_API extern const Index32 INVALID_IDX;
46 
47 /// @brief coordinate offset table for neighboring voxels
48 OPENVDB_API extern const Coord COORD_OFFSETS[26];
49 
50 
51 ////////////////////////////////////////
52 
53 
54 /// Return @a voxelCoord rounded to the closest integer coordinates.
55 inline Coord
56 nearestCoord(const Vec3d& voxelCoord)
57 {
58  Coord ijk;
59  ijk[0] = int(std::floor(voxelCoord[0]));
60  ijk[1] = int(std::floor(voxelCoord[1]));
61  ijk[2] = int(std::floor(voxelCoord[2]));
62  return ijk;
63 }
64 
65 
66 ////////////////////////////////////////
67 
68 
69 /// @brief Functor for use with tools::foreach() to compute the boolean intersection
70 /// between the value masks of corresponding leaf nodes in two trees
71 template<class TreeType1, class TreeType2>
73 {
74 public:
75  LeafTopologyIntOp(const TreeType2& tree): mOtherTree(&tree) {}
76 
77  inline void operator()(const typename TreeType1::LeafIter& lIter) const
78  {
79  const Coord xyz = lIter->origin();
80  const typename TreeType2::LeafNodeType* leaf = mOtherTree->probeConstLeaf(xyz);
81  if (leaf) {//leaf node
82  lIter->topologyIntersection(*leaf, zeroVal<typename TreeType1::ValueType>());
83  } else if (!mOtherTree->isValueOn(xyz)) {//inactive tile
84  lIter->setValuesOff();
85  }
86  }
87 
88 private:
89  const TreeType2* mOtherTree;
90 };
91 
92 
93 /// @brief Functor for use with tools::foreach() to compute the boolean difference
94 /// between the value masks of corresponding leaf nodes in two trees
95 template<class TreeType1, class TreeType2>
97 {
98 public:
99  LeafTopologyDiffOp(const TreeType2& tree): mOtherTree(&tree) {}
100 
101  inline void operator()(const typename TreeType1::LeafIter& lIter) const
102  {
103  const Coord xyz = lIter->origin();
104  const typename TreeType2::LeafNodeType* leaf = mOtherTree->probeConstLeaf(xyz);
105  if (leaf) {//leaf node
106  lIter->topologyDifference(*leaf, zeroVal<typename TreeType1::ValueType>());
107  } else if (mOtherTree->isValueOn(xyz)) {//active tile
108  lIter->setValuesOff();
109  }
110  }
111 
112 private:
113  const TreeType2* mOtherTree;
114 };
115 
116 
117 ////////////////////////////////////////
118 
119 
120 /// @brief Perform a boolean intersection between two leaf nodes' topology masks.
121 /// @return a pointer to a new, boolean-valued tree containing the overlapping voxels.
122 template<class TreeType1, class TreeType2>
123 inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
124 leafTopologyIntersection(const TreeType1& lhs, const TreeType2& rhs, bool threaded = true)
125 {
126  typedef typename TreeType1::template ValueConverter<bool>::Type BoolTreeType;
127 
128  typename BoolTreeType::Ptr topologyTree(new BoolTreeType(
129  lhs, /*inactiveValue=*/false, /*activeValue=*/true, TopologyCopy()));
130 
131  tools::foreach(topologyTree->beginLeaf(),
133 
134  tools::pruneInactive(*topologyTree, threaded);
135  return topologyTree;
136 }
137 
138 
139 /// @brief Perform a boolean difference between two leaf nodes' topology masks.
140 /// @return a pointer to a new, boolean-valued tree containing the non-overlapping
141 /// voxels from the lhs.
142 template<class TreeType1, class TreeType2>
143 inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
144 leafTopologyDifference(const TreeType1& lhs, const TreeType2& rhs, bool threaded = true)
145 {
146  typedef typename TreeType1::template ValueConverter<bool>::Type BoolTreeType;
147 
148  typename BoolTreeType::Ptr topologyTree(new BoolTreeType(
149  lhs, /*inactiveValue=*/false, /*activeValue=*/true, TopologyCopy()));
150 
151  tools::foreach(topologyTree->beginLeaf(),
153 
154  tools::pruneInactive(*topologyTree, threaded);
155  return topologyTree;
156 }
157 
158 } // namespace util
159 } // namespace OPENVDB_VERSION_NAME
160 } // namespace openvdb
161 
162 #endif // OPENVDB_UTIL_UTIL_HAS_BEEN_INCLUDED
163 
164 // Copyright (c) 2012-2017 DreamWorks Animation LLC
165 // All rights reserved. This software is distributed under the
166 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
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:124
OPENVDB_API const Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
Functor for use with tools::foreach() to compute the boolean difference between the value masks of co...
Definition: Util.h:96
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition: Util.h:77
Coord nearestCoord(const Vec3d &voxelCoord)
Return voxelCoord rounded to the closest integer coordinates.
Definition: Util.h:56
void foreach(const IterT &iter, XformOp &op, bool threaded=true, bool shareOp=true)
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition: Util.h:101
Functor for use with tools::foreach() to compute the boolean intersection between the value masks of ...
Definition: Util.h:72
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Defined various multi-threaded utility functions for trees.
OPENVDB_API const Index32 INVALID_IDX
int floor(T x)
Definition: ImathFun.h:150
typedef int
Definition: png.h:1175
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:144
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:503
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
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:394