OpenVDB  2.0.0
util/Util.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 //
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>
36 #include <openvdb/tools/ValueTransformer.h>
37 
38 
39 namespace openvdb {
41 namespace OPENVDB_VERSION_NAME {
42 namespace util {
43 
44 OPENVDB_API extern const Index32 INVALID_IDX;
45 
47 OPENVDB_API extern const Coord COORD_OFFSETS[26];
48 
49 
51 
52 
54 inline Coord
55 nearestCoord(const Vec3d& voxelCoord)
56 {
57  Coord ijk;
58  ijk[0] = int(std::floor(voxelCoord[0]));
59  ijk[1] = int(std::floor(voxelCoord[1]));
60  ijk[2] = int(std::floor(voxelCoord[2]));
61  return ijk;
62 }
63 
64 
66 
67 
70 template<class TreeType1, class TreeType2>
72 {
73 public:
74  LeafTopologyIntOp(const TreeType2& tree): mOtherTree(&tree) {}
75 
76  inline void operator()(const typename TreeType1::LeafIter& lIter) const
77  {
78  const Coord xyz = lIter->origin();
79  const typename TreeType2::LeafNodeType* leaf = mOtherTree->probeConstLeaf(xyz);
80  if (leaf) {//leaf node
81  lIter->topologyIntersection(*leaf, zeroVal<typename TreeType1::ValueType>());
82  } else if (!mOtherTree->isValueOn(xyz)) {//inactive tile
83  lIter->setValuesOff();
84  }
85  }
86 
87 private:
88  const TreeType2* mOtherTree;
89 };
90 
91 
94 template<class TreeType1, class TreeType2>
96 {
97 public:
98  LeafTopologyDiffOp(const TreeType2& tree): mOtherTree(&tree) {}
99 
100  inline void operator()(const typename TreeType1::LeafIter& lIter) const
101  {
102  const Coord xyz = lIter->origin();
103  const typename TreeType2::LeafNodeType* leaf = mOtherTree->probeConstLeaf(xyz);
104  if (leaf) {//leaf node
105  lIter->topologyDifference(*leaf, zeroVal<typename TreeType1::ValueType>());
106  } else if (mOtherTree->isValueOn(xyz)) {//active tile
107  lIter->setValuesOff();
108  }
109  }
110 
111 private:
112  const TreeType2* mOtherTree;
113 };
114 
115 
117 
118 
121 template<class TreeType1, class TreeType2>
122 inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
123 leafTopologyIntersection(const TreeType1& lhs, const TreeType2& rhs, bool threaded = true)
124 {
125  typedef typename TreeType1::template ValueConverter<bool>::Type BoolTreeType;
126 
127  typename BoolTreeType::Ptr topologyTree(new BoolTreeType(
128  lhs, /*inactiveValue=*/false, /*activeValue=*/true, TopologyCopy()));
129 
130  tools::foreach(topologyTree->beginLeaf(),
132 
133  topologyTree->pruneInactive();
134  return topologyTree;
135 }
136 
137 
141 template<class TreeType1, class TreeType2>
142 inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
143 leafTopologyDifference(const TreeType1& lhs, const TreeType2& rhs, bool threaded = true)
144 {
145  typedef typename TreeType1::template ValueConverter<bool>::Type BoolTreeType;
146 
147  typename BoolTreeType::Ptr topologyTree(new BoolTreeType(
148  lhs, /*inactiveValue=*/false, /*activeValue=*/true, TopologyCopy()));
149 
150  tools::foreach(topologyTree->beginLeaf(),
152 
153  topologyTree->pruneInactive();
154  return topologyTree;
155 }
156 
157 } // namespace util
158 } // namespace OPENVDB_VERSION_NAME
159 } // namespace openvdb
160 
161 #endif // OPENVDB_UTIL_UTIL_HAS_BEEN_INCLUDED
162 
163 // Copyright (c) 2012-2013 DreamWorks Animation LLC
164 // All rights reserved. This software is distributed under the
165 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:162
OPENVDB_API const Index32 INVALID_IDX
uint32_t Index32
Definition: Types.h:54
Functor for use with tools::foreach() to compute the boolean difference between the value masks of co...
Definition: util/Util.h:95
Definition: Types.h:346
TreeType1::template ValueConverter< bool >::Type::Ptr leafTopologyIntersection(const TreeType1 &lhs, const TreeType2 &rhs, bool threaded=true)
Perform a boolean intersection between two leaf nodes&#39; topology masks.
Definition: util/Util.h:123
LeafTopologyDiffOp(const TreeType2 &tree)
Definition: util/Util.h:98
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition: util/Util.h:76
#define OPENVDB_VERSION_NAME
Definition: version.h:45
Coord nearestCoord(const Vec3d &voxelCoord)
Return voxelCoord rounded to the closest integer coordinates.
Definition: util/Util.h:55
Vec3< double > Vec3d
Definition: Vec3.h:605
TreeType1::template ValueConverter< bool >::Type::Ptr leafTopologyDifference(const TreeType1 &lhs, const TreeType2 &rhs, bool threaded=true)
Perform a boolean difference between two leaf nodes&#39; topology masks.
Definition: util/Util.h:143
void foreach(const IterT &iter, XformOp &op, bool threaded=true, bool shareOp=true)
Definition: ValueTransformer.h:392
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition: util/Util.h:100
OPENVDB_API const Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
Functor for use with tools::foreach() to compute the boolean intersection between the value masks of ...
Definition: util/Util.h:71
LeafTopologyIntOp(const TreeType2 &tree)
Definition: util/Util.h:74