OpenVDB  4.0.2
Mask.h
Go to the documentation of this file.
1 //
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 //
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 {
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<mask_internal::Traits<GridType>::isBool,
76  typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
77 doInteriorMask(const GridType& grid, const double /*isovalue*/)
78 {
79  // If the input grid is already boolean, return a copy of it.
80  return grid.deepCopy();
81 }
82 
83 
85 template<typename GridType>
86 inline typename std::enable_if<!(mask_internal::Traits<GridType>::isBool),
87  typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
88 doInteriorMask(const GridType& grid, const double isovalue)
89 {
90  using GridValueT = typename GridType::ValueType;
91  using MaskGridT = typename mask_internal::Traits<GridType>::BoolGridType;
92 
93  // If the input grid is a level set (and floating-point), return a mask of its interior.
94  if ((grid.getGridClass() == GRID_LEVEL_SET) && std::is_floating_point<GridValueT>::value) {
95  return tools::sdfInteriorMask(grid, static_cast<GridValueT>(isovalue));
96  }
97 
98  // For any other grid type, return a mask of its active voxels.
99  auto maskGridPtr = MaskGridT::create(/*background=*/false);
100  maskGridPtr->setTransform(grid.transform().copy());
101  maskGridPtr->topologyUnion(grid);
102  return maskGridPtr;
103 }
104 
105 } // namespace mask_internal
106 
107 
108 template<typename GridType>
109 inline typename GridType::template ValueConverter<bool>::Type::Ptr
110 interiorMask(const GridType& grid, const double isovalue)
111 {
112  return mask_internal::doInteriorMask(grid, isovalue);
113 }
114 
115 
117 
118 } // namespace tools
119 } // namespace OPENVDB_VERSION_NAME
120 } // namespace openvdb
121 
122 #endif // OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
123 
124 // Copyright (c) 2012-2017 DreamWorks Animation LLC
125 // All rights reserved. This software is distributed under the
126 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
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:110
#define OPENVDB_VERSION_NAME
Definition: version.h:43
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:2291
Definition: Exceptions.h:39
Miscellaneous utility methods that operate primarily or exclusively on level set grids.
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Definition: Types.h:264