OpenVDB 5.1.0

Mask.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
34
35 #ifndef OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
36 #define OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
37
38 #include <openvdb/Grid.h>
39 #include "LevelSetUtil.h" // for tools::sdfInteriorMask()
40 #include <type_traits> // for std::enable_if, std::is_floating_point
41
42
43 namespace openvdb {
44 OPENVDB_USE_VERSION_NAMESPACE
45 namespace OPENVDB_VERSION_NAME {
46 namespace tools {
47
54 template<typename GridType>
55 inline typename GridType::template ValueConverter<bool>::Type::Ptr
56 interiorMask(const GridType& grid, const double isovalue = 0.0);
57
58
60
61
62 namespace mask_internal {
63
65 template<typename GridType>
66 struct Traits {
67 static const bool isBool = std::is_same<typename GridType::ValueType, bool>::value;
68 using BoolGridType = typename GridType::template ValueConverter<bool>::Type;
69 using BoolGridPtrType = typename BoolGridType::Ptr;
70 };
71
72
74 template<typename GridType>
75 inline typename std::enable_if<std::is_floating_point<typename GridType::ValueType>::value,
76 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
77 doLevelSetInteriorMask(const GridType& grid, const double isovalue)
78 {
79 using GridValueT = typename GridType::ValueType;
80 using MaskGridPtrT = typename mask_internal::Traits<GridType>::BoolGridPtrType;
81
82 // If the input grid is a level set (and floating-point), return a mask of its interior.
83 if (grid.getGridClass() == GRID_LEVEL_SET) {
84 return tools::sdfInteriorMask(grid, static_cast<GridValueT>(isovalue));
85 }
86 return MaskGridPtrT{};
87 }
88
89
91 // No-op specialization for non-floating-point grids
92 template<typename GridType>
93 inline typename std::enable_if<!std::is_floating_point<typename GridType::ValueType>::value,
94 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
95 doLevelSetInteriorMask(const GridType&, const double /*isovalue*/)
96 {
97 using MaskGridPtrT = typename mask_internal::Traits<GridType>::BoolGridPtrType;
98 return MaskGridPtrT{};
99 }
100
101
103 template<typename GridType>
104 inline typename std::enable_if<mask_internal::Traits<GridType>::isBool,
105 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
106 doInteriorMask(const GridType& grid, const double /*isovalue*/)
107 {
108 // If the input grid is already boolean, return a copy of it.
109 return grid.deepCopy();
110 }
111
112
114 template<typename GridType>
115 inline typename std::enable_if<!(mask_internal::Traits<GridType>::isBool),
116 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
117 doInteriorMask(const GridType& grid, const double isovalue)
118 {
119 using MaskGridT = typename mask_internal::Traits<GridType>::BoolGridType;
120
121 // If the input grid is a level set, return a mask of its interior.
122 if (auto maskGridPtr = doLevelSetInteriorMask(grid, isovalue)) {
123 return maskGridPtr;
124 }
125
126 // For any other grid type, return a mask of its active voxels.
127 auto maskGridPtr = MaskGridT::create(/*background=*/false);
128 maskGridPtr->setTransform(grid.transform().copy());
129 maskGridPtr->topologyUnion(grid);
130 return maskGridPtr;
131 }
132
133 } // namespace mask_internal
134
135
136 template<typename GridType>
137 inline typename GridType::template ValueConverter<bool>::Type::Ptr
138 interiorMask(const GridType& grid, const double isovalue)
139 {
140 return mask_internal::doInteriorMask(grid, isovalue);
141 }
142
143
145
146 } // namespace tools
147 } // namespace OPENVDB_VERSION_NAME
148 } // namespace openvdb
149
150 #endif // OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
151
152 // Copyright (c) 2012-2018 DreamWorks Animation LLC
153 // All rights reserved. This software is distributed under the
154 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v5_1::tools::interiorMask
GridType::template ValueConverter< bool >::Type::Ptr interiorMask(const GridType &grid, const double isovalue=0.0)
Given an input grid of any type, return a new, boolean grid whose active voxel topology matches the i...
Definition: Mask.h:138
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::GRID_LEVEL_SET
Definition: Types.h:274
Grid.h
openvdb::v5_1::tools::sdfInteriorMask
GridOrTreeType::template ValueConverter< bool >::Type::Ptr sdfInteriorMask(const GridOrTreeType &volume, typename GridOrTreeType::ValueType isovalue=lsutilGridZero< GridOrTreeType >())
Threaded method to construct a boolean mask that represents interior regions in a signed distance fie...
Definition: LevelSetUtil.h:2300
LevelSetUtil.h
Miscellaneous utility methods that operate primarily or exclusively on level set grids.
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188