39 #ifndef OPENVDB_TOOLS_SIGNEDFLOODFILL_HAS_BEEN_INCLUDED
40 #define OPENVDB_TOOLS_SIGNEDFLOODFILL_HAS_BEEN_INCLUDED
42 #include <boost/utility/enable_if.hpp>
45 #include <boost/static_assert.hpp>
46 #include <boost/type_traits/is_floating_point.hpp>
47 #include <boost/type_traits/is_signed.hpp>
71 template<
typename TreeOrLeafManagerT>
73 signedFloodFill(TreeOrLeafManagerT& tree,
bool threaded =
true,
size_t grainSize = 1);
93 template<
typename TreeOrLeafManagerT>
96 TreeOrLeafManagerT& tree,
97 const typename TreeOrLeafManagerT::ValueType& outsideWidth,
98 const typename TreeOrLeafManagerT::ValueType& insideWidth,
100 size_t grainSize = 1);
106 template<
typename TreeOrLeafManagerT>
110 typedef typename TreeOrLeafManagerT::ValueType
ValueT;
111 typedef typename TreeOrLeafManagerT::RootNodeType
RootT;
112 typedef typename TreeOrLeafManagerT::LeafNodeType
LeafT;
113 BOOST_STATIC_ASSERT(boost::is_floating_point<ValueT>::value || boost::is_signed<ValueT>::value);
116 : mOutside(ValueT(math::
Abs(tree.background())))
117 , mInside(ValueT(math::
negative(mOutside)))
122 : mOutside(ValueT(math::
Abs(outsideValue)))
123 , mInside(ValueT(math::
negative(math::
Abs(insideValue))))
130 #ifndef OPENVDB_2_ABI_COMPATIBLE
131 if (!leaf.allocate())
return;
133 const typename LeafT::NodeMaskType& valueMask = leaf.getValueMask();
135 typename LeafT::ValueType* buffer =
136 const_cast<typename LeafT::ValueType*
>(&(leaf.getFirstValue()));
138 const Index first = valueMask.findFirstOn();
139 if (first < LeafT::SIZE) {
140 bool xInside = buffer[first]<0, yInside = xInside, zInside = xInside;
141 for (
Index x = 0; x != (1 << LeafT::LOG2DIM); ++x) {
142 const Index x00 = x << (2 * LeafT::LOG2DIM);
143 if (valueMask.isOn(x00)) xInside = buffer[x00] < 0;
145 for (
Index y = 0; y != (1 << LeafT::LOG2DIM); ++y) {
146 const Index xy0 = x00 + (y << LeafT::LOG2DIM);
147 if (valueMask.isOn(xy0)) yInside = buffer[xy0] < 0;
149 for (
Index z = 0; z != (1 << LeafT::LOG2DIM); ++z) {
150 const Index xyz = xy0 + z;
151 if (valueMask.isOn(xyz)) {
152 zInside = buffer[xyz] < 0;
154 buffer[xyz] = zInside ? mInside : mOutside;
160 leaf.fill(buffer[0] < 0 ? mInside : mOutside);
165 template<
typename NodeT>
169 const typename NodeT::NodeMaskType& childMask = node.getChildMask();
171 typename NodeT::UnionType* table =
const_cast<typename NodeT::UnionType*
>(node.getTable());
173 const Index first = childMask.findFirstOn();
174 if (first < NodeT::NUM_VALUES) {
175 bool xInside = table[first].getChild()->getFirstValue()<0;
176 bool yInside = xInside, zInside = xInside;
177 for (
Index x = 0; x != (1 << NodeT::LOG2DIM); ++x) {
178 const int x00 = x << (2 * NodeT::LOG2DIM);
179 if (childMask.isOn(x00)) xInside = table[x00].getChild()->getLastValue()<0;
181 for (
Index y = 0; y != (1 << NodeT::LOG2DIM); ++y) {
182 const Index xy0 = x00 + (y << NodeT::LOG2DIM);
183 if (childMask.isOn(xy0)) yInside = table[xy0].getChild()->getLastValue()<0;
185 for (
Index z = 0; z != (1 << NodeT::LOG2DIM); ++z) {
186 const Index xyz = xy0 + z;
187 if (childMask.isOn(xyz)) {
188 zInside = table[xyz].getChild()->getLastValue()<0;
190 table[xyz].setValue(zInside ? mInside : mOutside);
196 const ValueT v = table[0].getValue()<0 ? mInside : mOutside;
197 for (
Index i = 0; i < NodeT::NUM_VALUES; ++i) table[i].setValue(v);
204 typedef typename RootT::ChildNodeType ChildT;
206 std::map<Coord, ChildT*> nodeKeys;
207 typename RootT::ChildOnIter it = root.beginChildOn();
208 for (; it; ++it) nodeKeys.insert(std::pair<Coord, ChildT*>(it.getCoord(), &(*it)));
209 static const Index DIM = RootT::ChildNodeType::DIM;
213 typename std::map<Coord, ChildT*>::const_iterator b = nodeKeys.begin(), e = nodeKeys.end();
214 if ( b == e )
return;
215 for (
typename std::map<Coord, ChildT*>::const_iterator a = b++; b != e; ++a, ++b) {
216 Coord d = b->first - a->first;
217 if (d[0]!=0 || d[1]!=0 || d[2]==
Int32(DIM))
continue;
218 const ValueT fill[] = { a->second->getLastValue(), b->second->getFirstValue() };
219 if (!(fill[0] < 0) || !(fill[1] < 0))
continue;
220 Coord c = a->first + Coord(0u, 0u, DIM);
221 for (; c[2] != b->first[2]; c[2] += DIM) root.addTile(c, mInside,
false);
223 root.setBackground(mOutside,
false);
227 const ValueT mOutside, mInside;
231 template<
typename TreeOrLeafManagerT>
233 typename boost::enable_if_c<
234 boost::is_floating_point<typename TreeOrLeafManagerT::ValueType>::value ||
235 boost::is_signed<typename TreeOrLeafManagerT::ValueType>::value,
void>::type
237 typename TreeOrLeafManagerT::ValueType outsideValue,
238 typename TreeOrLeafManagerT::ValueType insideValue,
248 template <
typename TreeOrLeafManagerT>
250 typename boost::disable_if_c<
251 boost::is_floating_point<typename TreeOrLeafManagerT::ValueType>::value ||
252 boost::is_signed<typename TreeOrLeafManagerT::ValueType>::value,
void>::type
254 const typename TreeOrLeafManagerT::ValueType&,
255 const typename TreeOrLeafManagerT::ValueType&,
260 "signedFloodFill is supported only for signed value grids");
265 template <
typename TreeOrLeafManagerT>
268 TreeOrLeafManagerT& tree,
269 const typename TreeOrLeafManagerT::ValueType& outsideValue,
270 const typename TreeOrLeafManagerT::ValueType& insideValue,
278 template <
typename TreeOrLeafManagerT>
284 const typename TreeOrLeafManagerT::ValueType v = tree.root().background();
292 #endif // OPENVDB_TOOLS_RESETBACKGROUND_HAS_BEEN_INCLUDED
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:116
Index32 Index
Definition: Types.h:58
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
int32_t Abs(int32_t i)
Return the absolute value of the given quantity.
Definition: Math.h:293
void processBottomUp(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:401
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Definition: Exceptions.h:39
To facilitate threading over the nodes of a tree, cache node pointers in linear arrays, one for each level of the tree.
Definition: NodeManager.h:56
int32_t Int32
Definition: Types.h:60
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Definition: Exceptions.h:87