OpenVDB: ChangeBackground.h Source File

OpenVDB 7.0.0

ChangeBackground.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_ChangeBACKGROUND_HAS_BEEN_INCLUDED
12 #define OPENVDB_TOOLS_ChangeBACKGROUND_HAS_BEEN_INCLUDED
13
14 #include <openvdb/math/Math.h> // for isNegative and negative
15 #include <openvdb/Types.h> // for Index typedef
16 #include <openvdb/tree/NodeManager.h>
17
18
19 namespace openvdb {
20 OPENVDB_USE_VERSION_NAMESPACE
21 namespace OPENVDB_VERSION_NAME {
22 namespace tools {
23
35 template<typename TreeOrLeafManagerT>
36 inline void
37 changeBackground(
38 TreeOrLeafManagerT& tree,
39 const typename TreeOrLeafManagerT::ValueType& background,
40 bool threaded = true,
41 size_t grainSize = 32);
42
43
61 template<typename TreeOrLeafManagerT>
62 inline void
63 changeLevelSetBackground(
64 TreeOrLeafManagerT& tree,
65 const typename TreeOrLeafManagerT::ValueType& halfWidth,
66 bool threaded = true,
67 size_t grainSize = 32);
68
69
89 template<typename TreeOrLeafManagerT>
90 inline void
91 changeAsymmetricLevelSetBackground(
92 TreeOrLeafManagerT& tree,
93 const typename TreeOrLeafManagerT::ValueType& outsideWidth,
94 const typename TreeOrLeafManagerT::ValueType& insideWidth,
95 bool threaded = true,
96 size_t grainSize = 32);
97
98
100
101
102 // Replaces the background value in a Tree of any type.
103 template<typename TreeOrLeafManagerT>
104 class ChangeBackgroundOp
105 {
106 public:
107 typedef typename TreeOrLeafManagerT::ValueType ValueT;
108 typedef typename TreeOrLeafManagerT::RootNodeType RootT;
109 typedef typename TreeOrLeafManagerT::LeafNodeType LeafT;
110
111
112 ChangeBackgroundOp(const TreeOrLeafManagerT& tree, const ValueT& newValue)
113 : mOldValue(tree.root().background())
114 , mNewValue(newValue)
115 {
116 }
117 void operator()(RootT& root) const
118 {
119 for (typename RootT::ValueOffIter it = root.beginValueOff(); it; ++it) this->set(it);
120 root.setBackground(mNewValue, false);
121 }
122 void operator()(LeafT& node) const
123 {
124 for (typename LeafT::ValueOffIter it = node.beginValueOff(); it; ++it) this->set(it);
125 }
126 template<typename NodeT>
127 void operator()(NodeT& node) const
128 {
129 typename NodeT::NodeMaskType mask = node.getValueOffMask();
130 for (typename NodeT::ValueOnIter it(mask.beginOn(), &node); it; ++it) this->set(it);
131 }
132 private:
133
134 template<typename IterT>
135 inline void set(IterT& iter) const
136 {
137 if (math::isApproxEqual(*iter, mOldValue)) {
138 iter.setValue(mNewValue);
139 } else if (math::isApproxEqual(*iter, math::negative(mOldValue))) {
140 iter.setValue(math::negative(mNewValue));
141 }
142 }
143 const ValueT mOldValue, mNewValue;
144 };// ChangeBackgroundOp
145
146
147 // Replaces the background value in a Tree assumed to represent a
148 // level set. It is generally faster than ChangeBackgroundOp.
149 // Note that is follows the sign-convention that outside is positive
150 // and inside is negative!
151 template<typename TreeOrLeafManagerT>
152 class ChangeLevelSetBackgroundOp
153 {
154 public:
155 typedef typename TreeOrLeafManagerT::ValueType ValueT;
156 typedef typename TreeOrLeafManagerT::RootNodeType RootT;
157 typedef typename TreeOrLeafManagerT::LeafNodeType LeafT;
158
160 ChangeLevelSetBackgroundOp(const ValueT& outside, const ValueT& inside)
161 : mOutside(outside)
162 , mInside(inside)
163 {
164 if (math::isNegative(mOutside)) {
165 OPENVDB_THROW(ValueError,
166 "ChangeLevelSetBackgroundOp: the outside value cannot be negative!");
167 }
168 if (!math::isNegative(mInside)) {
169 OPENVDB_THROW(ValueError,
170 "ChangeLevelSetBackgroundOp: the inside value must be negative!");
171 }
172 }
173 void operator()(RootT& root) const
174 {
175 for (typename RootT::ValueOffIter it = root.beginValueOff(); it; ++it) this->set(it);
176 root.setBackground(mOutside, false);
177 }
178 void operator()(LeafT& node) const
179 {
180 for(typename LeafT::ValueOffIter it = node.beginValueOff(); it; ++it) this->set(it);
181 }
182 template<typename NodeT>
183 void operator()(NodeT& node) const
184 {
185 typedef typename NodeT::ValueOffIter IterT;
186 for (IterT it(node.getChildMask().beginOff(), &node); it; ++it) this->set(it);
187 }
188 private:
189
190 template<typename IterT>
191 inline void set(IterT& iter) const
192 {
193 //this is safe since we know ValueType is_floating_point
194 ValueT& v = const_cast<ValueT&>(*iter);
195 v = v < 0 ? mInside : mOutside;
196 }
197 const ValueT mOutside, mInside;
198 };// ChangeLevelSetBackgroundOp
199
200
201 template<typename TreeOrLeafManagerT>
202 inline void
203 changeBackground(
204 TreeOrLeafManagerT& tree,
205 const typename TreeOrLeafManagerT::ValueType& background,
206 bool threaded,
207 size_t grainSize)
208 {
209 tree::NodeManager<TreeOrLeafManagerT> linearTree(tree);
210 ChangeBackgroundOp<TreeOrLeafManagerT> op(tree, background);
211 linearTree.foreachTopDown(op, threaded, grainSize);
212 }
213
214
215 template<typename TreeOrLeafManagerT>
216 inline void
217 changeAsymmetricLevelSetBackground(
218 TreeOrLeafManagerT& tree,
219 const typename TreeOrLeafManagerT::ValueType& outsideValue,
220 const typename TreeOrLeafManagerT::ValueType& insideValue,
221 bool threaded,
222 size_t grainSize)
223 {
224 tree::NodeManager<TreeOrLeafManagerT> linearTree(tree);
225 ChangeLevelSetBackgroundOp<TreeOrLeafManagerT> op(outsideValue, insideValue);
226 linearTree.foreachTopDown(op, threaded, grainSize);
227 }
228
229
230 // If the narrow-band is symmetric only one background value is required
231 template<typename TreeOrLeafManagerT>
232 inline void
233 changeLevelSetBackground(
234 TreeOrLeafManagerT& tree,
235 const typename TreeOrLeafManagerT::ValueType& background,
236 bool threaded,
237 size_t grainSize)
238 {
239 changeAsymmetricLevelSetBackground(
240 tree, background, math::negative(background), threaded, grainSize);
241 }
242
243 } // namespace tools
244 } // namespace OPENVDB_VERSION_NAME
245 } // namespace openvdb
246
247 #endif // OPENVDB_TOOLS_CHANGEBACKGROUND_HAS_BEEN_INCLUDED
NodeManager.h
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
openvdb::v7_0::tools::ChangeBackgroundOp::RootT
TreeOrLeafManagerT::RootNodeType RootT
Definition: ChangeBackground.h:108
openvdb::v7_0::math::isApproxEqual
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:351
Types.h
openvdb::v7_0::tools::ChangeLevelSetBackgroundOp::RootT
TreeOrLeafManagerT::RootNodeType RootT
Definition: ChangeBackground.h:156
openvdb::v7_0::tools::ChangeBackgroundOp::operator()
void operator()(NodeT &node) const
Definition: ChangeBackground.h:127
openvdb::v7_0::tools::changeLevelSetBackground
void changeLevelSetBackground(TreeOrLeafManagerT &tree, const typename TreeOrLeafManagerT::ValueType &halfWidth, bool threaded=true, size_t grainSize=32)
Replace the background value in all the nodes of a floating-point tree containing a symmetric narrow-...
Definition: ChangeBackground.h:233
openvdb::v7_0::tools::ChangeLevelSetBackgroundOp
Definition: ChangeBackground.h:153
openvdb::v7_0::math::negative
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:81
openvdb::v7_0::tools::ChangeLevelSetBackgroundOp::operator()
void operator()(RootT &root) const
Definition: ChangeBackground.h:173
openvdb::v7_0::tools::ChangeLevelSetBackgroundOp::operator()
void operator()(NodeT &node) const
Definition: ChangeBackground.h:183
openvdb::v7_0::tools::ChangeBackgroundOp
Definition: ChangeBackground.h:105
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v7_0::tools::ChangeBackgroundOp::operator()
void operator()(RootT &root) const
Definition: ChangeBackground.h:117
openvdb::v7_0::tree::NodeManager
To facilitate threading over the nodes of a tree, cache node pointers in linear arrays,...
Definition: NodeManager.h:31
openvdb::v7_0::math::isNegative
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:311
openvdb::v7_0::tools::ChangeLevelSetBackgroundOp::ValueT
TreeOrLeafManagerT::ValueType ValueT
Definition: ChangeBackground.h:155
openvdb::v7_0::tree::NodeManager::foreachTopDown
void foreachTopDown(const NodeOp &op, bool threaded=true, size_t grainSize=1)
Definition: NodeManager.h:450
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::tools::ChangeBackgroundOp::ChangeBackgroundOp
ChangeBackgroundOp(const TreeOrLeafManagerT &tree, const ValueT &newValue)
Definition: ChangeBackground.h:112
openvdb::v7_0::tools::changeAsymmetricLevelSetBackground
void changeAsymmetricLevelSetBackground(TreeOrLeafManagerT &tree, const typename TreeOrLeafManagerT::ValueType &outsideWidth, const typename TreeOrLeafManagerT::ValueType &insideWidth, bool threaded=true, size_t grainSize=32)
Replace the background values in all the nodes of a floating-point tree containing a possibly asymmet...
Definition: ChangeBackground.h:217
openvdb::v7_0::tools::ChangeBackgroundOp::ValueT
TreeOrLeafManagerT::ValueType ValueT
Definition: ChangeBackground.h:107
openvdb::v7_0::tools::ChangeLevelSetBackgroundOp::ChangeLevelSetBackgroundOp
ChangeLevelSetBackgroundOp(const ValueT &outside, const ValueT &inside)
Constructor for asymmetric narrow-bands.
Definition: ChangeBackground.h:160
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::tools::ChangeLevelSetBackgroundOp::operator()
void operator()(LeafT &node) const
Definition: ChangeBackground.h:178
openvdb::v7_0::tools::changeBackground
void changeBackground(TreeOrLeafManagerT &tree, const typename TreeOrLeafManagerT::ValueType &background, bool threaded=true, size_t grainSize=32)
Replace the background value in all the nodes of a tree.
Definition: ChangeBackground.h:203
openvdb::v7_0::ValueError
Definition: Exceptions.h:65
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::tools::ChangeLevelSetBackgroundOp::LeafT
TreeOrLeafManagerT::LeafNodeType LeafT
Definition: ChangeBackground.h:157
openvdb::v7_0::tools::ChangeBackgroundOp::operator()
void operator()(LeafT &node) const
Definition: ChangeBackground.h:122
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
openvdb::v7_0::tools::ChangeBackgroundOp::LeafT
TreeOrLeafManagerT::LeafNodeType LeafT
Definition: ChangeBackground.h:109