OpenVDB  8.1.1
Activate.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
10 
11 #ifndef OPENVDB_TOOLS_ACTIVATE_HAS_BEEN_INCLUDED
12 #define OPENVDB_TOOLS_ACTIVATE_HAS_BEEN_INCLUDED
13 
14 #include <openvdb/Types.h>
15 #include <openvdb/Grid.h>
16 #include <openvdb/math/Math.h> // for isApproxEqual()
18 
19 
20 namespace openvdb {
22 namespace OPENVDB_VERSION_NAME {
23 namespace tools {
24 
27 template<typename GridOrTree>
28 inline void activate(
29  GridOrTree&,
30  const typename GridOrTree::ValueType& value,
31  const typename GridOrTree::ValueType& tolerance = zeroVal<typename GridOrTree::ValueType>(),
32  const bool threaded = true
33 );
34 
35 
38 template<typename GridOrTree>
39 inline void deactivate(
40  GridOrTree&,
41  const typename GridOrTree::ValueType& value,
42  const typename GridOrTree::ValueType& tolerance = zeroVal<typename GridOrTree::ValueType>(),
43  const bool threaded = true
44 );
45 
46 
48 
49 
50 namespace activate_internal {
51 
52 template<typename TreeT, bool IgnoreTolerance = false>
53 struct ActivateOp
54 {
55 public:
56  using RootT = typename TreeT::RootNodeType;
57  using LeafT = typename TreeT::LeafNodeType;
58  using ValueT = typename TreeT::ValueType;
59 
60  explicit ActivateOp(const ValueT& value,
61  const ValueT& tolerance = zeroVal<ValueT>())
62  : mValue(value)
63  , mTolerance(tolerance) { }
64 
65  inline bool check(const ValueT& value) const {
66  // math::isApproxEqual is marginally more expensive,
67  // so opt to do direct comparison if tolerance is ignored
68  if (IgnoreTolerance) return value == mValue;
69  return math::isApproxEqual(value, mValue, mTolerance);
70  }
71 
72  bool operator()(RootT& root, size_t) const
73  {
74  for (auto it = root.beginValueOff(); it; ++it) {
75  if (check(*it)) it.setValueOn(/*on=*/true);
76  }
77  return true;
78  }
79 
80  template<typename NodeT>
81  bool operator()(NodeT& node, size_t) const
82  {
83  // only iterate if there are inactive tiles
84  if (!node.isValueMaskOn()) {
85  for (auto it = node.beginValueOff(); it; ++it) {
86  if (check(*it)) it.setValueOn(/*on=*/true);
87  }
88  }
89  // return false if there are no child nodes below this node
90  return !node.isChildMaskOff();
91  }
92 
93  bool operator()(LeafT& leaf, size_t) const
94  {
95  // early-exit if there are no inactive values
96  if (leaf.isValueMaskOn()) return true;
97  for (auto it = leaf.beginValueOff(); it; ++it) {
98  if (check(*it)) it.setValueOn(/*on=*/true);
99  }
100  return true;
101  }
102 
103 private:
104  const ValueT mValue;
105  const ValueT mTolerance;
106 };// ActivateOp
107 
108 template<typename TreeT, bool IgnoreTolerance = false>
110 {
111 public:
112  using RootT = typename TreeT::RootNodeType;
113  using LeafT = typename TreeT::LeafNodeType;
114  using ValueT = typename TreeT::ValueType;
115 
116  explicit DeactivateOp(const ValueT& value,
117  const ValueT& tolerance = zeroVal<ValueT>())
118  : mValue(value)
119  , mTolerance(tolerance) { }
120 
121  inline bool check(const ValueT& value) const {
122  if (IgnoreTolerance) return value == mValue;
123  return math::isApproxEqual(value, mValue, mTolerance);
124  }
125 
126  bool operator()(RootT& root, size_t) const
127  {
128  for (auto it = root.beginValueOn(); it; ++it) {
129  if (check(*it)) it.setValueOn(/*on=*/false);
130  }
131  return true;
132  }
133 
134  template<typename NodeT>
135  bool operator()(NodeT& node, size_t) const
136  {
137  // only iterate if there are active tiles
138  if (!node.isValueMaskOff()) {
139  for (auto it = node.beginValueOn(); it; ++it) {
140  if (check(*it)) it.setValueOn(/*on=*/false);
141  }
142  }
143  // return false if there are no child nodes below this node
144  return !node.isChildMaskOff();
145  }
146 
147  bool operator()(LeafT& leaf, size_t) const
148  {
149  // early-exit if there are no active values
150  if (leaf.isValueMaskOff()) return true;
151  for (auto it = leaf.beginValueOn(); it; ++it) {
152  if (check(*it)) it.setValueOn(/*on=*/false);
153  }
154  return true;
155  }
156 
157 private:
158  const ValueT mValue;
159  const ValueT mTolerance;
160 };// DeactivateOp
161 
162 } // namespace activate_internal
163 
164 
166 
167 
168 template<typename GridOrTree>
169 inline void
170 activate(GridOrTree& gridOrTree, const typename GridOrTree::ValueType& value,
171  const typename GridOrTree::ValueType& tolerance,
172  const bool threaded)
173 {
174  using Adapter = TreeAdapter<GridOrTree>;
175  using TreeType = typename Adapter::TreeType;
176  using ValueType = typename TreeType::ValueType;
177 
178  TreeType& tree = Adapter::tree(gridOrTree);
179 
180  tree::DynamicNodeManager<TreeType> nodeManager(tree);
181 
182  if (tolerance == zeroVal<ValueType>()) {
183  activate_internal::ActivateOp<TreeType, /*IgnoreTolerance=*/true> op(value);
184  nodeManager.foreachTopDown(op, threaded);
185  } else {
186  activate_internal::ActivateOp<TreeType> op(value, tolerance);
187  nodeManager.foreachTopDown(op, threaded);
188  }
189 }
190 
191 
192 template<typename GridOrTree>
193 inline void
194 deactivate(GridOrTree& gridOrTree, const typename GridOrTree::ValueType& value,
195  const typename GridOrTree::ValueType& tolerance,
196  const bool threaded)
197 {
198  using Adapter = TreeAdapter<GridOrTree>;
199  using TreeType = typename Adapter::TreeType;
200  using ValueType = typename TreeType::ValueType;
201 
202  TreeType& tree = Adapter::tree(gridOrTree);
203 
204  tree::DynamicNodeManager<TreeType> nodeManager(tree);
205 
206  if (tolerance == zeroVal<ValueType>()) {
207  activate_internal::DeactivateOp<TreeType, /*IgnoreTolerance=*/true> op(value);
208  nodeManager.foreachTopDown(op, threaded);
209  } else {
210  activate_internal::DeactivateOp<TreeType> op(value, tolerance);
211  nodeManager.foreachTopDown(op, threaded);
212  }
213 }
214 
215 } // namespace tools
216 } // namespace OPENVDB_VERSION_NAME
217 } // namespace openvdb
218 
219 #endif // OPENVDB_TOOLS_ACTIVATE_HAS_BEEN_INCLUDED
void foreachTopDown(const NodeOp &op, bool threaded=true, size_t grainSize=1)
Threaded method that applies a user-supplied functor to all the nodes in the tree.
Definition: NodeManager.h:968
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
void activate(GridOrTree &, const typename GridOrTree::ValueType &value, const typename GridOrTree::ValueType &tolerance=zeroVal< typename GridOrTree::ValueType >(), const bool threaded=true)
Mark as active any inactive tiles or voxels in the given grid or tree whose values are equal to value...
Definition: Activate.h:170
Definition: NodeManager.h:36
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1070
bool check(const ValueT &value) const
Definition: Activate.h:121
bool operator()(RootT &root, size_t) const
Definition: Activate.h:72
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
typename TreeT::LeafNodeType LeafT
Definition: Activate.h:113
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition: Math.h:407
bool operator()(NodeT &node, size_t) const
Definition: Activate.h:135
bool operator()(LeafT &leaf, size_t) const
Definition: Activate.h:147
typename TreeT::LeafNodeType LeafT
Definition: Activate.h:57
bool operator()(NodeT &node, size_t) const
Definition: Activate.h:81
Definition: openvdb/Exceptions.h:13
DeactivateOp(const ValueT &value, const ValueT &tolerance=zeroVal< ValueT >())
Definition: Activate.h:116
bool operator()(LeafT &leaf, size_t) const
Definition: Activate.h:93
ActivateOp(const ValueT &value, const ValueT &tolerance=zeroVal< ValueT >())
Definition: Activate.h:60
typename TreeT::RootNodeType RootT
Definition: Activate.h:112
bool check(const ValueT &value) const
Definition: Activate.h:65
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
typename TreeT::ValueType ValueT
Definition: Activate.h:114
typename TreeT::RootNodeType RootT
Definition: Activate.h:56
void deactivate(GridOrTree &, const typename GridOrTree::ValueType &value, const typename GridOrTree::ValueType &tolerance=zeroVal< typename GridOrTree::ValueType >(), const bool threaded=true)
Mark as inactive any active tiles or voxels in the given grid or tree whose values are equal to value...
Definition: Activate.h:194
bool operator()(RootT &root, size_t) const
Definition: Activate.h:126
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178
typename TreeT::ValueType ValueT
Definition: Activate.h:58