OpenVDB: Util.h Source File

OpenVDB 6.0.0

Util.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 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 #include <openvdb/tools/Prune.h>// for tree::pruneInactive
38
39
40 namespace openvdb {
41 OPENVDB_USE_VERSION_NAMESPACE
42 namespace OPENVDB_VERSION_NAME {
43 namespace util {
44
45 OPENVDB_API extern const Index32 INVALID_IDX;
46
48 OPENVDB_API extern const Coord COORD_OFFSETS[26];
49
50
52
53
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
67
68
71 template<class TreeType1, class TreeType2>
72 class LeafTopologyIntOp
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
95 template<class TreeType1, class TreeType2>
96 class LeafTopologyDiffOp
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
118
119
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(),
132 LeafTopologyIntOp<BoolTreeType, TreeType2>(rhs), threaded);
133
134 tools::pruneInactive(*topologyTree, threaded);
135 return topologyTree;
136 }
137
138
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(),
152 LeafTopologyDiffOp<BoolTreeType, TreeType2>(rhs), threaded);
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-2018 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/ )
openvdb::v6_0::tools::pruneInactive
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:381
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
Types.h
openvdb::v6_0::util::leafTopologyIntersection
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
ValueTransformer.h
openvdb::v6_0::util::LeafTopologyDiffOp::LeafTopologyDiffOp
LeafTopologyDiffOp(const TreeType2 &tree)
Definition: Util.h:99
openvdb::v6_0::math::Vec3d
Vec3< double > Vec3d
Definition: Vec3.h:679
openvdb::v6_0::TopologyCopy
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:518
openvdb::v6_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
Tree.h
Prune.h
Defined various multi-threaded utility functions for trees.
openvdb::v6_0::Index32
uint32_t Index32
Definition: Types.h:59
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v6_0::util::COORD_OFFSETS
OPENVDB_API const Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
openvdb::v6_0::util::LeafTopologyIntOp::LeafTopologyIntOp
LeafTopologyIntOp(const TreeType2 &tree)
Definition: Util.h:75
openvdb::v6_0::util::LeafTopologyDiffOp::operator()
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition: Util.h:101
openvdb::v6_0::util::leafTopologyDifference
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
openvdb
Definition: Exceptions.h:40
openvdb::v6_0::tools::foreach
void foreach(const IterT &iter, XformOp &op, bool threaded=true, bool shareOp=true)
Definition: ValueTransformer.h:392
openvdb::v6_0::util::nearestCoord
Coord nearestCoord(const Vec3d &voxelCoord)
Return voxelCoord rounded to the closest integer coordinates.
Definition: Util.h:56
openvdb::v6_0::util::LeafTopologyIntOp::operator()
void operator()(const typename TreeType1::LeafIter &lIter) const
Definition: Util.h:77
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v6_0::util::LeafTopologyDiffOp
Functor for use with tools::foreach() to compute the boolean difference between the value masks of co...
Definition: Util.h:96
openvdb::v6_0::util::LeafTopologyIntOp
Functor for use with tools::foreach() to compute the boolean intersection between the value masks of ...
Definition: Util.h:72
openvdb::v6_0::util::INVALID_IDX
OPENVDB_API const Index32 INVALID_IDX