OpenVDB: openvdb/tools/Mask.h Source File

OpenVDB 12.1.0

Loading...
Searching...
No Matches
Mask.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-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 <openvdb/openvdb.h>
13#include "LevelSetUtil.h" // for tools::sdfInteriorMask()
14#include <type_traits> // for std::enable_if, std::is_floating_point
15
16
17namespace openvdb {
18 OPENVDB_USE_VERSION_NAMESPACE
19namespace OPENVDB_VERSION_NAME {
20namespace tools {
21
22/// @brief Given an input grid of any type, return a new, boolean grid
23/// whose active voxel topology matches the input grid's or,
24/// if the input grid is a level set, matches the input grid's interior.
25/// @param grid the grid from which to construct a mask
26/// @param isovalue for a level set grid, the isovalue that defines the grid's interior
27/// @sa tools::sdfInteriorMask()
28template<typename GridType>
29typename GridType::template ValueConverter<bool>::Type::Ptr
30 interiorMask(const GridType& grid, const double isovalue = 0.0);
31
32
33////////////////////////////////////////
34
35/// @cond OPENVDB_DOCS_INTERNAL
36
37namespace mask_internal {
38template<typename GridType>
39struct 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
45template<typename GridType>
46inline typename std::enable_if<std::is_floating_point<typename GridType::ValueType>::value,
47 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
48doLevelSetInteriorMask(const GridType& grid, const double isovalue)
49{
50 using GridValueT = typename GridType::ValueType;
51 using MaskGridPtrT = typename mask_internal::Traits<GridType>::BoolGridPtrType;
52
53 // If the input grid is a level set (and floating-point), return a mask of its interior.
54 if (grid.getGridClass() == GRID_LEVEL_SET) {
55 return tools::sdfInteriorMask(grid, static_cast<GridValueT>(isovalue));
56 }
57 return MaskGridPtrT{};
58}
59
60// No-op specialization for non-floating-point grids
61template<typename GridType>
62inline typename std::enable_if<!std::is_floating_point<typename GridType::ValueType>::value,
63 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
64doLevelSetInteriorMask(const GridType&, const double /*isovalue*/)
65{
66 using MaskGridPtrT = typename mask_internal::Traits<GridType>::BoolGridPtrType;
67 return MaskGridPtrT{};
68}
69
70template<typename GridType>
71inline typename std::enable_if<mask_internal::Traits<GridType>::isBool,
72 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
73doInteriorMask(const GridType& grid, const double /*isovalue*/)
74{
75 // If the input grid is already boolean, return a copy of it.
76 return grid.deepCopy();
77}
78
79template<typename GridType>
80inline typename std::enable_if<!(mask_internal::Traits<GridType>::isBool),
81 typename mask_internal::Traits<GridType>::BoolGridPtrType>::type
82doInteriorMask(const GridType& grid, const double isovalue)
83{
84 using MaskGridT = typename mask_internal::Traits<GridType>::BoolGridType;
85
86 // If the input grid is a level set, return a mask of its interior.
87 if (auto maskGridPtr = doLevelSetInteriorMask(grid, isovalue)) {
88 return maskGridPtr;
89 }
90
91 // For any other grid type, return a mask of its active voxels.
92 auto maskGridPtr = MaskGridT::create(/*background=*/false);
93 maskGridPtr->setTransform(grid.transform().copy());
94 maskGridPtr->topologyUnion(grid);
95 return maskGridPtr;
96}
97
98} // namespace mask_internal
99
100/// @endcond
101
102
103template<typename GridType>
104typename GridType::template ValueConverter<bool>::Type::Ptr
105interiorMask(const GridType& grid, const double isovalue)
106{
107 return mask_internal::doInteriorMask(grid, isovalue);
108}
109
110
111////////////////////////////////////////
112
113
114// Explicit Template Instantiation
115
116#ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION
117
118#ifdef OPENVDB_INSTANTIATE_MASK
119#include <openvdb/util/ExplicitInstantiation.h>
120#endif
121
122#define _FUNCTION(TreeT) \
123 Grid<TreeT>::ValueConverter<bool>::Type::Ptr interiorMask(const Grid<TreeT>&, const double)
124 OPENVDB_VOLUME_TREE_INSTANTIATE(_FUNCTION)
125#undef _FUNCTION
126
127#endif // OPENVDB_USE_EXPLICIT_INSTANTIATION
128
129
130} // namespace tools
131} // namespace OPENVDB_VERSION_NAME
132} // namespace openvdb
133
134#endif // OPENVDB_TOOLS_MASK_HAS_BEEN_INCLUDED
ExplicitInstantiation.h
Grid.h
LevelSetUtil.h
Miscellaneous utility methods that operate primarily or exclusively on level set grids.
openvdb::v12_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:105
openvdb
Definition Exceptions.h:13
openvdb.h
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218
OPENVDB_VOLUME_TREE_INSTANTIATE
#define OPENVDB_VOLUME_TREE_INSTANTIATE(Function)
Definition version.h.in:165