11 #ifndef OPENVDB_TOOLS_SIGNEDFLOODFILL_HAS_BEEN_INCLUDED 12 #define OPENVDB_TOOLS_SIGNEDFLOODFILL_HAS_BEEN_INCLUDED 19 #include <type_traits> 42 template<
typename TreeOrLeafManagerT>
45 size_t grainSize = 1,
Index minLevel = 0);
66 template<
typename TreeOrLeafManagerT>
69 TreeOrLeafManagerT& tree,
70 const typename TreeOrLeafManagerT::ValueType& outsideWidth,
71 const typename TreeOrLeafManagerT::ValueType& insideWidth,
80 template<
typename TreeOrLeafManagerT>
84 using ValueT =
typename TreeOrLeafManagerT::ValueType;
85 using RootT =
typename TreeOrLeafManagerT::RootNodeType;
86 using LeafT =
typename TreeOrLeafManagerT::LeafNodeType;
87 static_assert(std::is_signed<ValueT>::value,
88 "signed flood fill is supported only for signed value grids");
91 : mOutside(
ValueT(math::
Abs(tree.background())))
98 : mOutside(
ValueT(math::
Abs(outsideValue)))
100 , mMinLevel(minLevel)
107 if (LeafT::LEVEL < mMinLevel)
return;
109 if (!leaf.allocate())
return;
111 const typename LeafT::NodeMaskType& valueMask = leaf.getValueMask();
113 typename LeafT::ValueType* buffer =
114 const_cast<typename LeafT::ValueType*
>(&(leaf.getFirstValue()));
116 const Index first = valueMask.findFirstOn();
117 if (first < LeafT::SIZE) {
118 bool xInside = buffer[first]<0, yInside = xInside, zInside = xInside;
119 for (
Index x = 0; x != (1 << LeafT::LOG2DIM); ++x) {
120 const Index x00 = x << (2 * LeafT::LOG2DIM);
121 if (valueMask.isOn(x00)) xInside = buffer[x00] < 0;
123 for (
Index y = 0; y != (1 << LeafT::LOG2DIM); ++y) {
124 const Index xy0 = x00 + (y << LeafT::LOG2DIM);
125 if (valueMask.isOn(xy0)) yInside = buffer[xy0] < 0;
127 for (
Index z = 0; z != (1 << LeafT::LOG2DIM); ++z) {
128 const Index xyz = xy0 + z;
129 if (valueMask.isOn(xyz)) {
130 zInside = buffer[xyz] < 0;
132 buffer[xyz] = zInside ? mInside : mOutside;
138 leaf.fill(buffer[0] < 0 ? mInside : mOutside);
143 template<
typename NodeT>
146 if (NodeT::LEVEL < mMinLevel)
return;
148 const typename NodeT::NodeMaskType& childMask = node.getChildMask();
150 typename NodeT::UnionType* table =
const_cast<typename NodeT::UnionType*
>(node.getTable());
152 const Index first = childMask.findFirstOn();
153 if (first < NodeT::NUM_VALUES) {
154 bool xInside = table[first].getChild()->getFirstValue()<0;
155 bool yInside = xInside, zInside = xInside;
156 for (
Index x = 0; x != (1 << NodeT::LOG2DIM); ++x) {
157 const int x00 = x << (2 * NodeT::LOG2DIM);
158 if (childMask.isOn(x00)) xInside = table[x00].getChild()->getLastValue()<0;
160 for (
Index y = 0; y != (1 << NodeT::LOG2DIM); ++y) {
161 const Index xy0 = x00 + (y << NodeT::LOG2DIM);
162 if (childMask.isOn(xy0)) yInside = table[xy0].getChild()->getLastValue()<0;
164 for (
Index z = 0; z != (1 << NodeT::LOG2DIM); ++z) {
165 const Index xyz = xy0 + z;
166 if (childMask.isOn(xyz)) {
167 zInside = table[xyz].getChild()->getLastValue()<0;
169 table[xyz].setValue(zInside ? mInside : mOutside);
175 const ValueT v = table[0].getValue()<0 ? mInside : mOutside;
176 for (
Index i = 0; i < NodeT::NUM_VALUES; ++i) table[i].setValue(v);
183 if (RootT::LEVEL < mMinLevel)
return;
184 using ChildT =
typename RootT::ChildNodeType;
186 std::map<Coord, ChildT*> nodeKeys;
187 typename RootT::ChildOnIter it = root.beginChildOn();
188 for (; it; ++it) nodeKeys.insert(std::pair<Coord, ChildT*>(it.getCoord(), &(*it)));
189 static const Index DIM = RootT::ChildNodeType::DIM;
193 typename std::map<Coord, ChildT*>::const_iterator b = nodeKeys.begin(), e = nodeKeys.end();
194 if ( b == e )
return;
195 for (
typename std::map<Coord, ChildT*>::const_iterator a = b++; b != e; ++a, ++b) {
196 Coord d = b->first - a->first;
197 if (d[0]!=0 || d[1]!=0 || d[2]==
Int32(DIM))
continue;
198 const ValueT fill[] = { a->second->getLastValue(), b->second->getFirstValue() };
199 if (!(fill[0] < 0) || !(fill[1] < 0))
continue;
201 for (; c[2] != b->first[2]; c[2] += DIM) root.addTile(c, mInside,
false);
203 root.setBackground(mOutside,
false);
207 const ValueT mOutside, mInside;
208 const Index mMinLevel;
215 template<
typename TreeOrLeafManagerT>
217 typename std::enable_if<std::is_signed<typename TreeOrLeafManagerT::ValueType>::value,
void>::type
218 doSignedFloodFill(TreeOrLeafManagerT& tree,
219 typename TreeOrLeafManagerT::ValueType outsideValue,
220 typename TreeOrLeafManagerT::ValueType insideValue,
226 SignedFloodFillOp<TreeOrLeafManagerT> op(outsideValue, insideValue, minLevel);
227 nodes.foreachBottomUp(op, threaded, grainSize);
231 template <
typename TreeOrLeafManagerT>
233 typename std::enable_if<!std::is_signed<typename TreeOrLeafManagerT::ValueType>::value,
void>::type
234 doSignedFloodFill(TreeOrLeafManagerT&,
235 const typename TreeOrLeafManagerT::ValueType&,
236 const typename TreeOrLeafManagerT::ValueType&,
242 "signedFloodFill is supported only for signed value grids");
250 template <
typename TreeOrLeafManagerT>
253 TreeOrLeafManagerT& tree,
254 const typename TreeOrLeafManagerT::ValueType& outsideValue,
255 const typename TreeOrLeafManagerT::ValueType& insideValue,
260 doSignedFloodFill(tree, outsideValue, insideValue, threaded, grainSize, minLevel);
264 template <
typename TreeOrLeafManagerT>
271 const typename TreeOrLeafManagerT::ValueType v = tree.root().background();
272 doSignedFloodFill(tree, v,
math::negative(v), threaded, grainSize, minLevel);
279 #endif // OPENVDB_TOOLS_RESETBACKGROUND_HAS_BEEN_INCLUDED
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
Coord Abs(const Coord &xyz)
Definition: Coord.h:515
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:25
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:81
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
int32_t Int32
Definition: Types.h:33
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:31
Definition: Exceptions.h:13
Library and file format version numbers.
Index32 Index
Definition: Types.h:31
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
Definition: Exceptions.h:64