36 #ifndef OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED
37 #define OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED
39 #include <openvdb/Grid.h>
40 #include <openvdb/tree/LeafManager.h>
42 #include <boost/type_traits/is_same.hpp>
43 #include <boost/type_traits/is_signed.hpp>
44 #include <boost/utility/enable_if.hpp>
45 #include <tbb/blocked_range.h>
46 #include <tbb/parallel_reduce.h>
60 inline typename GridType::Ptr
clip(
const GridType& grid,
const BBoxd&);
73 inline typename GridType::Ptr
clip(
const GridType& grid,
const Grid<MaskTreeType>& mask);
79 namespace clip_internal {
83 inline typename boost::enable_if<boost::is_signed<T>,
bool>::type
88 inline typename boost::disable_if<boost::is_signed<T>,
bool>::type
95 template<
typename TreeT>
99 typedef typename TreeT::ValueType
ValueT;
104 template <
typename LeafNodeType>
107 const LeafNodeT *refLeaf = mAcc.probeConstLeaf(leaf.origin());
109 typename LeafNodeType::ValueOffIter iter = leaf.beginValueOff();
110 for ( ; iter; ++iter) {
111 const Index pos = iter.pos();
112 leaf.setActiveState(pos,
lessThanZero(refLeaf->getValue(pos)));
125 template<
typename TreeT>
129 typedef typename TreeT::template ValueConverter<bool>::Type
BoolTreeT;
132 CopyLeafNodes(
const TreeT& tree,
const BoolLeafManagerT& leafNodes);
134 void run(
bool threaded =
true);
136 typename TreeT::Ptr
tree()
const {
return mNewTree; }
139 void operator()(
const tbb::blocked_range<size_t>&);
143 const BoolTreeT* mClipMask;
145 const BoolLeafManagerT* mLeafNodes;
146 typename TreeT::Ptr mNewTree;
150 template<
typename TreeT>
153 , mLeafNodes(&leafNodes)
154 , mNewTree(new TreeT(mTree->background()))
159 template<
typename TreeT>
162 , mLeafNodes(rhs.mLeafNodes)
163 , mNewTree(new TreeT(mTree->background()))
168 template<
typename TreeT>
172 if (threaded) tbb::parallel_reduce(mLeafNodes->getRange(), *
this);
173 else (*
this)(mLeafNodes->getRange());
177 template<
typename TreeT>
181 typedef typename TreeT::LeafNodeType LeafT;
183 typename BoolLeafT::ValueOnCIter it;
188 for (
size_t n = range.begin(); n != range.end(); ++n) {
189 const BoolLeafT& maskLeaf = mLeafNodes->leaf(n);
190 const Coord& ijk = maskLeaf.origin();
196 for (it = maskLeaf.cbeginValueOn(); it; ++it) {
197 const Index pos = it.pos();
198 newLeaf->setValueOnly(pos, refLeaf->getValue(pos));
199 newLeaf->setActiveState(pos, refLeaf->isValueOn(pos));
202 typename TreeT::ValueType value;
203 bool isActive = refAcc.
probeValue(ijk, value);
205 for (it = maskLeaf.cbeginValueOn(); it; ++it) {
206 const Index pos = it.pos();
207 newLeaf->setValueOnly(pos, value);
208 newLeaf->setActiveState(pos, isActive);
220 static const char*
name() {
return "bin"; }
225 template<
class TreeT>
227 const Vec3R& inCoord,
typename TreeT::ValueType& result)
230 ijk[0] = int(std::floor(inCoord[0]));
231 ijk[1] = int(std::floor(inCoord[1]));
232 ijk[2] = int(std::floor(inCoord[2]));
233 return inTree.probeValue(ijk, result);
242 template<
typename FromGr
idT,
typename ToGr
idT>
252 template<
typename Gr
idT>
264 template<
typename Gr
idT>
265 inline typename boost::disable_if<boost::is_same<bool, typename GridT::ValueType>,
266 typename GridT::template ValueConverter<bool>::Type::Ptr>::type
269 typedef typename GridT::template ValueConverter<bool>::Type BoolGridT;
270 typedef typename BoolGridT::Ptr BoolGridPtrT;
273 BoolGridPtrT mask = BoolGridT::create(
false);
274 mask->topologyUnion(grid);
275 mask->setTransform(grid.constTransform().copy());
280 template<
typename Gr
idT>
281 inline typename boost::enable_if<boost::is_same<bool, typename GridT::ValueType>,
282 typename GridT::Ptr>::type
292 template<
typename Gr
idType>
293 inline typename GridType::Ptr
294 doClip(
const GridType& grid,
const typename GridType::template ValueConverter<bool>::Type& aMask)
296 typedef typename GridType::TreeType TreeT;
297 typedef typename GridType::TreeType::template ValueConverter<bool>::Type BoolTreeT;
299 const GridClass gridClass = grid.getGridClass();
300 const TreeT& tree = grid.tree();
302 BoolTreeT mask(
false);
303 mask.topologyUnion(tree);
311 typename BoolTreeT::ValueAllIter iter(mask);
312 iter.setMaxDepth(BoolTreeT::ValueAllIter::LEAF_DEPTH - 1);
314 for ( ; iter; ++iter) {
319 mask.topologyIntersection(aMask.constTree());
321 typename GridType::Ptr outGrid;
327 outGrid = GridType::create(maskOp.
tree());
334 typename TreeT::ValueAllIter it(outGrid->tree());
335 it.setMaxDepth(TreeT::ValueAllIter::LEAF_DEPTH - 1);
337 Coord ijk = it.getCoord();
340 typename TreeT::ValueType value;
341 bool isActive = refAcc.
probeValue(ijk, value);
344 if (!isActive) it.setValueOff();
349 outGrid->setTransform(grid.transform().copy());
350 if (gridClass !=
GRID_LEVEL_SET) outGrid->setGridClass(gridClass);
361 template<
typename Gr
idType>
363 inline typename GridType::Ptr
366 typedef typename GridType::template ValueConverter<bool>::Type BoolGridT;
369 Vec3d idxMin, idxMax;
371 CoordBBox region(Coord::floor(idxMin), Coord::floor(idxMax));
374 BoolGridT clipMask(
false);
375 clipMask.fill(region,
true,
true);
381 template<
typename Gr
idType,
typename MaskTreeType>
383 inline typename GridType::Ptr
386 typedef typename GridType::template ValueConverter<bool>::Type BoolGridT;
387 typedef typename BoolGridT::Ptr BoolGridPtrT;
390 typedef typename MaskGridType::template ValueConverter<bool>::Type BoolMaskGridT;
391 typedef typename BoolMaskGridT::Ptr BoolMaskGridPtrT;
397 if (grid.constTransform() != boolMaskGrid->constTransform()) {
398 BoolMaskGridPtrT resampledMask = BoolMaskGridT::create(
false);
399 resampledMask->setTransform(grid.constTransform().copy());
400 tools::resampleToMatch<clip_internal::BoolSampler>(*boolMaskGrid, *resampledMask);
402 boolMaskGrid = resampledMask;
406 BoolGridPtrT clipMask =
417 #endif // OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:109
Defined various multi-threaded utility functions for trees.
void foreach(const LeafOp &op, bool threaded=true, size_t grainSize=1)
Threaded method that applies a user-supplied functor to each leaf node in the LeafManager.
Definition: LeafManager.h:476
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: ValueAccessor.h:210
#define OPENVDB_VERSION_NAME
Definition: version.h:43
OPENVDB_API void calculateBounds(const Transform &t, const Vec3d &minWS, const Vec3d &maxWS, Vec3d &minIS, Vec3d &maxIS)
Calculate an axis-aligned bounding box in index space from an axis-aligned bounding box in world spac...
bool isValueOn(const Coord &xyz) const
Return the active state of the voxel at the given coordinates.
Definition: ValueAccessor.h:217
Index32 Index
Definition: Types.h:59
const LeafNodeT * probeConstLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z), or NULL if no such node exists...
Definition: ValueAccessor.h:383
Definition: Exceptions.h:39
Vec3< double > Vec3d
Definition: Vec3.h:629
LeafNodeT * touchLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, create one, but preserve the values and active states of all voxels.
Definition: ValueAccessor.h:347
GridClass
Definition: Types.h:208
const Vec3T & min() const
Return a const reference to the minimum point of the BBox.
Definition: BBox.h:81
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:54
const Vec3T & max() const
Return a const reference to the maximum point of the BBox.
Definition: BBox.h:84
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
math::BBox< Vec3d > BBoxd
Definition: Types.h:87
bool probeValue(const Coord &xyz, ValueType &value) const
Return the active state of the voxel as well as its value.
Definition: ValueAccessor.h:220
RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:211