OpenVDB 7.0.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
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 {
17 OPENVDB_USE_VERSION_NAMESPACE
18 namespace OPENVDB_VERSION_NAME {
19 namespace tools {
20
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
33
34
35 namespace mask_internal {
36
38 template<typename GridType>
39 struct Traits {
40 static const bool isBool = std::is_same<typename GridType::ValueType, bool>::value;
41 using BoolGridType = typename GridType::template ValueConverter<bool>::Type;
42 using BoolGridPtrType = typename BoolGridType::Ptr;
43 };
44
45
47 template<typename GridType>
48 inline typename std::enable_if<std::is_floating_point<typename GridType::ValueType>::value,
49 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
50 doLevelSetInteriorMask(const GridType& grid, const double isovalue)
51 {
52 using GridValueT = typename GridType::ValueType;
53 using MaskGridPtrT = typename mask_internal::Traits<GridType>::BoolGridPtrType;
54
55 // If the input grid is a level set (and floating-point), return a mask of its interior.
56 if (grid.getGridClass() == GRID_LEVEL_SET) {
57 return tools::sdfInteriorMask(grid, static_cast<GridValueT>(isovalue));
58 }
59 return MaskGridPtrT{};
60 }
61
62
64 // No-op specialization for non-floating-point grids
65 template<typename GridType>
66 inline typename std::enable_if<!std::is_floating_point<typename GridType::ValueType>::value,
67 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
68 doLevelSetInteriorMask(const GridType&, const double /*isovalue*/)
69 {
70 using MaskGridPtrT = typename mask_internal::Traits<GridType>::BoolGridPtrType;
71 return MaskGridPtrT{};
72 }
73
74
76 template<typename GridType>
77 inline typename std::enable_if<mask_internal::Traits<GridType>::isBool,
78 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
79 doInteriorMask(const GridType& grid, const double /*isovalue*/)
80 {
81 // If the input grid is already boolean, return a copy of it.
82 return grid.deepCopy();
83 }
84
85
87 template<typename GridType>
88 inline typename std::enable_if<!(mask_internal::Traits<GridType>::isBool),
89 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
90 doInteriorMask(const GridType& grid, const double isovalue)
91 {
92 using MaskGridT = typename mask_internal::Traits<GridType>::BoolGridType;
93
94 // If the input grid is a level set, return a mask of its interior.
95 if (auto maskGridPtr = doLevelSetInteriorMask(grid, isovalue)) {
96 return maskGridPtr;
97 }
98
99 // For any other grid type, return a mask of its active voxels.
100 auto maskGridPtr = MaskGridT::create(/*background=*/false);
101 maskGridPtr->setTransform(grid.transform().copy());
102 maskGridPtr->topologyUnion(grid);
103 return maskGridPtr;
104 }
105
106 } // namespace mask_internal
107
108
109 template<typename GridType>
110 inline typename GridType::template ValueConverter<bool>::Type::Ptr
111 interiorMask(const GridType& grid, const double isovalue)
112 {
113 return mask_internal::doInteriorMask(grid, isovalue);
114 }
115
116
118
119 } // namespace tools
120 } // namespace OPENVDB_VERSION_NAME
121 } // namespace openvdb
122
123 #endif // OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
LevelSetUtil.h
Miscellaneous utility methods that operate primarily or exclusively on level set grids.
Grid.h
openvdb::v7_0::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:2273
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::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:111
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::GRID_LEVEL_SET
Definition: Types.h:454
openvdb
Definition: Exceptions.h:13