OpenVDB  0.104.0
util/Util.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012 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  tree::ValueAccessor<const TreeType2> otherValueAccessor(*mOtherTree);
78  typename TreeType1::LeafNodeType::ValueOnIter vIter = lIter.getLeaf()->beginValueOn();
79  for ( ; vIter; ++vIter) {
80  if (!otherValueAccessor.isValueOn(vIter.getCoord())) vIter.setValueOff();
81  }
82  }
83 
84 private:
85  const TreeType2* mOtherTree;
86 };
87 
88 
91 template<class TreeType1, class TreeType2>
93 {
94 public:
95  LeafTopologyDiffOp(const TreeType2& tree): mOtherTree(&tree) {}
96 
97  inline void operator()(const typename TreeType1::LeafIter& lIter) const {
98  tree::ValueAccessor<const TreeType2> otherValueAccessor(*mOtherTree);
99  typename TreeType1::LeafNodeType::ValueOnIter vIter = lIter.getLeaf()->beginValueOn();
100  for ( ; vIter; ++vIter) {
101  if (otherValueAccessor.isValueOn(vIter.getCoord())) vIter.setValueOff();
102  }
103  }
104 
105 private:
106  const TreeType2* mOtherTree;
107 };
108 
109 
111 
112 
115 template<class TreeType1, class TreeType2>
116 inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
117 leafTopologyIntersection(const TreeType1& lhs, const TreeType2& rhs, bool threaded = true)
118 {
119  typedef typename TreeType1::template ValueConverter<bool>::Type BoolTreeType;
120 
121  typename BoolTreeType::Ptr topologyTree(new BoolTreeType(
122  lhs, /*inactiveValue=*/false, /*activeValue=*/true, TopologyCopy()));
123 
124  tools::foreach(topologyTree->beginLeaf(),
126 
127  topologyTree->pruneInactive();
128  return topologyTree;
129 }
130 
131 
135 template<class TreeType1, class TreeType2>
136 inline typename TreeType1::template ValueConverter<bool>::Type::Ptr
137 leafTopologyDifference(const TreeType1& lhs, const TreeType2& rhs, bool threaded = true)
138 {
139  typedef typename TreeType1::template ValueConverter<bool>::Type BoolTreeType;
140 
141  typename BoolTreeType::Ptr topologyTree(new BoolTreeType(
142  lhs, /*inactiveValue=*/false, /*activeValue=*/true, TopologyCopy()));
143 
144  tools::foreach(topologyTree->beginLeaf(),
146 
147  topologyTree->pruneInactive();
148  return topologyTree;
149 }
150 
151 } // namespace util
152 } // namespace OPENVDB_VERSION_NAME
153 } // namespace openvdb
154 
155 #endif // OPENVDB_UTIL_UTIL_HAS_BEEN_INCLUDED
156 
157 // Copyright (c) 2012 DreamWorks Animation LLC
158 // All rights reserved. This software is distributed under the
159 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )