OpenVDB  8.2.0
Mask.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file Mask.h
5 ///
6 /// @brief Construct boolean mask grids from grids of arbitrary type
7 
8 #ifndef OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
9 #define OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
10 
11 #include <openvdb/Grid.h>
12 #include "LevelSetUtil.h" // for tools::sdfInteriorMask()
13 #include <type_traits> // for std::enable_if, std::is_floating_point
14 
15 
16 namespace openvdb {
18 namespace OPENVDB_VERSION_NAME {
19 namespace tools {
20 
21 /// @brief Given an input grid of any type, return a new, boolean grid
22 /// whose active voxel topology matches the input grid's or,
23 /// if the input grid is a level set, matches the input grid's interior.
24 /// @param grid the grid from which to construct a mask
25 /// @param isovalue for a level set grid, the isovalue that defines the grid's interior
26 /// @sa tools::sdfInteriorMask()
27 template<typename GridType>
28 inline typename GridType::template ValueConverter<bool>::Type::Ptr
29 interiorMask(const GridType& grid, const double isovalue = 0.0);
30 
31 
32 ////////////////////////////////////////
33 
34 /// @cond OPENVDB_DOCS_INTERNAL
35 
36 namespace mask_internal {
37 template<typename GridType>
38 struct Traits {
39  static const bool isBool = std::is_same<typename GridType::ValueType, bool>::value;
40  using BoolGridType = typename GridType::template ValueConverter<bool>::Type;
41  using BoolGridPtrType = typename BoolGridType::Ptr;
42 };
43 
44 template<typename GridType>
45 inline typename std::enable_if<std::is_floating_point<typename GridType::ValueType>::value,
46  typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
47 doLevelSetInteriorMask(const GridType& grid, const double isovalue)
48 {
49  using GridValueT = typename GridType::ValueType;
50  using MaskGridPtrT = typename mask_internal::Traits<GridType>::BoolGridPtrType;
51 
52  // If the input grid is a level set (and floating-point), return a mask of its interior.
53  if (grid.getGridClass() == GRID_LEVEL_SET) {
54  return tools::sdfInteriorMask(grid, static_cast<GridValueT>(isovalue));
55  }
56  return MaskGridPtrT{};
57 }
58 
59 // No-op specialization for non-floating-point grids
60 template<typename GridType>
61 inline typename std::enable_if<!std::is_floating_point<typename GridType::ValueType>::value,
62  typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
63 doLevelSetInteriorMask(const GridType&, const double /*isovalue*/)
64 {
65  using MaskGridPtrT = typename mask_internal::Traits<GridType>::BoolGridPtrType;
66  return MaskGridPtrT{};
67 }
68 
69 template<typename GridType>
70 inline typename std::enable_if<mask_internal::Traits<GridType>::isBool,
71  typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
72 doInteriorMask(const GridType& grid, const double /*isovalue*/)
73 {
74  // If the input grid is already boolean, return a copy of it.
75  return grid.deepCopy();
76 }
77 
78 template<typename GridType>
79 inline typename std::enable_if<!(mask_internal::Traits<GridType>::isBool),
80  typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
81 doInteriorMask(const GridType& grid, const double isovalue)
82 {
83  using MaskGridT = typename mask_internal::Traits<GridType>::BoolGridType;
84 
85  // If the input grid is a level set, return a mask of its interior.
86  if (auto maskGridPtr = doLevelSetInteriorMask(grid, isovalue)) {
87  return maskGridPtr;
88  }
89 
90  // For any other grid type, return a mask of its active voxels.
91  auto maskGridPtr = MaskGridT::create(/*background=*/false);
92  maskGridPtr->setTransform(grid.transform().copy());
93  maskGridPtr->topologyUnion(grid);
94  return maskGridPtr;
95 }
96 
97 } // namespace mask_internal
98 
99 /// @endcond
100 
101 
102 template<typename GridType>
103 inline typename GridType::template ValueConverter<bool>::Type::Ptr
104 interiorMask(const GridType& grid, const double isovalue)
105 {
106  return mask_internal::doInteriorMask(grid, isovalue);
107 }
108 
109 
110 ////////////////////////////////////////
111 
112 } // namespace tools
113 } // namespace OPENVDB_VERSION_NAME
114 } // namespace openvdb
115 
116 #endif // OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
Miscellaneous utility methods that operate primarily or exclusively on level set grids.
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:2273
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:104
@ GRID_LEVEL_SET
Definition: Types.h:337
Definition: Exceptions.h:13
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:180