36 #ifndef OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED 37 #define OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED 39 #include <openvdb/Grid.h> 40 #include <openvdb/math/Math.h> 41 #include <openvdb/tree/LeafManager.h> 43 #include <boost/type_traits/is_same.hpp> 44 #include <boost/type_traits/is_signed.hpp> 45 #include <boost/utility/enable_if.hpp> 46 #include <tbb/blocked_range.h> 47 #include <tbb/parallel_reduce.h> 61 inline typename GridType::Ptr
clip(
const GridType& grid,
const BBoxd&);
74 inline typename GridType::Ptr
clip(
const GridType& grid,
const Grid<MaskTreeType>& mask);
80 namespace clip_internal {
86 template<
typename TreeT>
90 typedef typename TreeT::ValueType
ValueT;
95 template <
typename LeafNodeType>
98 const LeafNodeT *refLeaf = mAcc.probeConstLeaf(leaf.origin());
100 typename LeafNodeType::ValueOffIter iter = leaf.beginValueOff();
101 for ( ; iter; ++iter) {
102 const Index pos = iter.pos();
116 template<
typename TreeT>
120 typedef typename TreeT::template ValueConverter<bool>::Type
BoolTreeT;
123 CopyLeafNodes(
const TreeT& tree,
const BoolLeafManagerT& leafNodes);
125 void run(
bool threaded =
true);
127 typename TreeT::Ptr
tree()
const {
return mNewTree; }
130 void operator()(
const tbb::blocked_range<size_t>&);
134 const BoolTreeT* mClipMask;
136 const BoolLeafManagerT* mLeafNodes;
137 typename TreeT::Ptr mNewTree;
141 template<
typename TreeT>
144 , mLeafNodes(&leafNodes)
145 , mNewTree(new TreeT(mTree->background()))
150 template<
typename TreeT>
153 , mLeafNodes(rhs.mLeafNodes)
154 , mNewTree(new TreeT(mTree->background()))
159 template<
typename TreeT>
163 if (threaded) tbb::parallel_reduce(mLeafNodes->
getRange(), *
this);
164 else (*
this)(mLeafNodes->
getRange());
168 template<
typename TreeT>
172 typedef typename TreeT::LeafNodeType LeafT;
174 typename BoolLeafT::ValueOnCIter it;
179 for (
size_t n = range.begin(); n != range.end(); ++n) {
180 const BoolLeafT& maskLeaf = mLeafNodes->
leaf(n);
181 const Coord& ijk = maskLeaf.origin();
187 for (it = maskLeaf.cbeginValueOn(); it; ++it) {
188 const Index pos = it.pos();
189 newLeaf->setValueOnly(pos, refLeaf->getValue(pos));
190 newLeaf->setActiveState(pos, refLeaf->isValueOn(pos));
193 typename TreeT::ValueType value;
194 bool isActive = refAcc.
probeValue(ijk, value);
196 for (it = maskLeaf.cbeginValueOn(); it; ++it) {
197 const Index pos = it.pos();
198 newLeaf->setValueOnly(pos, value);
199 newLeaf->setActiveState(pos, isActive);
211 static const char*
name() {
return "bin"; }
216 template<
class TreeT>
218 const Vec3R& inCoord,
typename TreeT::ValueType& result)
221 ijk[0] = int(std::floor(inCoord[0]));
222 ijk[1] = int(std::floor(inCoord[1]));
223 ijk[2] = int(std::floor(inCoord[2]));
224 return inTree.probeValue(ijk, result);
233 template<
typename FromGr
idT,
typename ToGr
idT>
238 ToGridPtrT
operator()(
const FromGridPtrT& grid) {
return ToGridPtrT(
new ToGridT(*grid)); }
243 template<
typename Gr
idT>
255 template<
typename Gr
idT>
256 inline typename boost::disable_if<boost::is_same<bool, typename GridT::ValueType>,
257 typename GridT::template ValueConverter<bool>::Type::Ptr>::type
260 typedef typename GridT::template ValueConverter<bool>::Type BoolGridT;
261 typedef typename BoolGridT::Ptr BoolGridPtrT;
264 BoolGridPtrT mask = BoolGridT::create(
false);
265 mask->topologyUnion(grid);
266 mask->setTransform(grid.constTransform().copy());
271 template<
typename Gr
idT>
272 inline typename boost::enable_if<boost::is_same<bool, typename GridT::ValueType>,
273 typename GridT::Ptr>::type
283 template<
typename Gr
idType>
284 inline typename GridType::Ptr
285 doClip(
const GridType& grid,
const typename GridType::template ValueConverter<bool>::Type& aMask)
287 typedef typename GridType::TreeType TreeT;
288 typedef typename GridType::TreeType::template ValueConverter<bool>::Type
BoolTreeT;
290 const GridClass gridClass = grid.getGridClass();
291 const TreeT&
tree = grid.tree();
293 BoolTreeT mask(
false);
294 mask.topologyUnion(tree);
302 typename BoolTreeT::ValueAllIter iter(mask);
303 iter.setMaxDepth(BoolTreeT::ValueAllIter::LEAF_DEPTH - 1);
305 for ( ; iter; ++iter) {
310 mask.topologyIntersection(aMask.constTree());
312 typename GridType::Ptr outGrid;
318 outGrid = GridType::create(maskOp.
tree());
325 typename TreeT::ValueAllIter it(outGrid->tree());
326 it.setMaxDepth(TreeT::ValueAllIter::LEAF_DEPTH - 1);
328 Coord ijk = it.getCoord();
331 typename TreeT::ValueType value;
332 bool isActive = refAcc.
probeValue(ijk, value);
335 if (!isActive) it.setValueOff();
340 outGrid->setTransform(grid.transform().copy());
341 if (gridClass !=
GRID_LEVEL_SET) outGrid->setGridClass(gridClass);
352 template<
typename Gr
idType>
354 inline typename GridType::Ptr
357 typedef typename GridType::template ValueConverter<bool>::Type BoolGridT;
360 Vec3d idxMin, idxMax;
362 CoordBBox region(Coord::floor(idxMin), Coord::floor(idxMax));
365 BoolGridT clipMask(
false);
366 clipMask.fill(region,
true,
true);
372 template<
typename Gr
idType,
typename MaskTreeType>
374 inline typename GridType::Ptr
377 typedef typename GridType::template ValueConverter<bool>::Type BoolGridT;
378 typedef typename BoolGridT::Ptr BoolGridPtrT;
381 typedef typename MaskGridType::template ValueConverter<bool>::Type BoolMaskGridT;
382 typedef typename BoolMaskGridT::Ptr BoolMaskGridPtrT;
388 if (grid.constTransform() != boolMaskGrid->constTransform()) {
389 BoolMaskGridPtrT resampledMask = BoolMaskGridT::create(
false);
390 resampledMask->setTransform(grid.constTransform().copy());
391 tools::resampleToMatch<clip_internal::BoolSampler>(*boolMaskGrid, *resampledMask);
393 boolMaskGrid = resampledMask;
397 BoolGridPtrT clipMask =
408 #endif // OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED Index32 Index
Definition: Types.h:58
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:114
Defined various multi-threaded utility functions for trees.
const Vec3T & min() const
Return a const reference to the minimum point of the BBox.
Definition: BBox.h:81
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: ValueAccessor.h:256
GridClass
Definition: Types.h:204
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:54
LeafType & leaf(size_t leafIdx) const
Return a pointer to the leaf node at index leafIdx in the array.
Definition: LeafManager.h:323
const Vec3T & max() const
Return a const reference to the maximum point of the BBox.
Definition: BBox.h:84
math::BBox< Vec3d > BBoxd
Definition: Types.h:86
#define OPENVDB_VERSION_NAME
Definition: version.h:43
bool isValueOn(const Coord &xyz) const
Return the active state of the voxel at the given coordinates.
Definition: ValueAccessor.h:263
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...
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:429
Definition: Exceptions.h:39
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:354
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:481
RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:211
Vec3< double > Vec3d
Definition: Vec3.h:643
Axis-aligned bounding box.
Definition: BBox.h:47
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:393
RangeType getRange(size_t grainsize=1) const
Return a tbb::blocked_range of leaf array indices.
Definition: LeafManager.h:347
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
bool probeValue(const Coord &xyz, ValueType &value) const
Return the active state of the voxel as well as its value.
Definition: ValueAccessor.h:266