36 #ifndef OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED 37 #define OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED 44 #include <boost/type_traits/is_same.hpp> 45 #include <boost/type_traits/is_signed.hpp> 46 #include <boost/utility/enable_if.hpp> 47 #include <tbb/blocked_range.h> 48 #include <tbb/parallel_reduce.h> 62 inline typename GridType::Ptr
clip(
const GridType& grid,
const BBoxd&);
75 inline typename GridType::Ptr
clip(
const GridType& grid,
const Grid<MaskTreeType>& mask);
81 namespace clip_internal {
87 template<
typename TreeT>
91 typedef typename TreeT::ValueType
ValueT;
96 template <
typename LeafNodeType>
99 const LeafNodeT *refLeaf = mAcc.probeConstLeaf(leaf.origin());
101 typename LeafNodeType::ValueOffIter iter = leaf.beginValueOff();
102 for ( ; iter; ++iter) {
103 const Index pos = iter.pos();
117 template<
typename TreeT>
121 typedef typename TreeT::template ValueConverter<ValueMask>::Type
MaskTreeT;
124 CopyLeafNodes(
const TreeT& tree,
const MaskLeafManagerT& leafNodes);
126 void run(
bool threaded =
true);
128 typename TreeT::Ptr
tree()
const {
return mNewTree; }
131 void operator()(
const tbb::blocked_range<size_t>&);
135 const MaskTreeT* mClipMask;
137 const MaskLeafManagerT* mLeafNodes;
138 typename TreeT::Ptr mNewTree;
142 template<
typename TreeT>
145 , mLeafNodes(&leafNodes)
146 , mNewTree(new TreeT(mTree->background()))
151 template<
typename TreeT>
154 , mLeafNodes(rhs.mLeafNodes)
155 , mNewTree(new TreeT(mTree->background()))
160 template<
typename TreeT>
164 if (threaded) tbb::parallel_reduce(mLeafNodes->
getRange(), *
this);
165 else (*
this)(mLeafNodes->
getRange());
169 template<
typename TreeT>
173 typedef typename TreeT::LeafNodeType LeafT;
175 typename MaskLeafT::ValueOnCIter it;
180 for (
size_t n = range.begin(); n != range.end(); ++n) {
181 const MaskLeafT& maskLeaf = mLeafNodes->
leaf(n);
182 const Coord& ijk = maskLeaf.origin();
188 for (it = maskLeaf.cbeginValueOn(); it; ++it) {
189 const Index pos = it.pos();
190 newLeaf->setValueOnly(pos, refLeaf->getValue(pos));
191 newLeaf->setActiveState(pos, refLeaf->isValueOn(pos));
194 typename TreeT::ValueType value;
195 bool isActive = refAcc.
probeValue(ijk, value);
197 for (it = maskLeaf.cbeginValueOn(); it; ++it) {
198 const Index pos = it.pos();
199 newLeaf->setValueOnly(pos, value);
200 newLeaf->setActiveState(pos, isActive);
212 static const char*
name() {
return "bin"; }
217 template<
class TreeT>
219 const Vec3R& inCoord,
typename TreeT::ValueType& result)
222 ijk[0] = int(std::floor(inCoord[0]));
223 ijk[1] = int(std::floor(inCoord[1]));
224 ijk[2] = int(std::floor(inCoord[2]));
225 return inTree.probeValue(ijk, result);
234 template<
typename FromGr
idT,
typename ToGr
idT>
239 ToGridPtrT
operator()(
const FromGridPtrT& grid) {
return ToGridPtrT(
new ToGridT(*grid)); }
244 template<
typename Gr
idT>
256 template<
typename Gr
idT>
257 inline typename boost::disable_if<boost::is_same<ValueMask, typename GridT::BuildType>,
258 typename GridT::template ValueConverter<ValueMask>::Type::Ptr>::type
261 typedef typename GridT::template ValueConverter<ValueMask>::Type MaskGridT;
262 typedef typename MaskGridT::Ptr MaskGridPtrT;
265 MaskGridPtrT mask = MaskGridT::create(
false);
266 mask->topologyUnion(grid);
267 mask->setTransform(grid.constTransform().copy());
272 template<
typename Gr
idT>
273 inline typename boost::enable_if<boost::is_same<ValueMask, typename GridT::BuildType>,
274 typename GridT::Ptr>::type
284 template<
typename Gr
idType>
285 inline typename GridType::Ptr
286 doClip(
const GridType& grid,
const typename GridType::template ValueConverter<ValueMask>::Type& aMask)
288 typedef typename GridType::TreeType TreeT;
289 typedef typename GridType::TreeType::template ValueConverter<ValueMask>::Type
MaskTreeT;
291 const GridClass gridClass = grid.getGridClass();
292 const TreeT&
tree = grid.tree();
294 MaskTreeT mask(
false);
295 mask.topologyUnion(tree);
303 typename MaskTreeT::ValueAllIter iter(mask);
304 iter.setMaxDepth(MaskTreeT::ValueAllIter::LEAF_DEPTH - 1);
306 for ( ; iter; ++iter) {
311 mask.topologyIntersection(aMask.constTree());
313 typename GridType::Ptr outGrid;
319 outGrid = GridType::create(maskOp.
tree());
326 typename TreeT::ValueAllIter it(outGrid->tree());
327 it.setMaxDepth(TreeT::ValueAllIter::LEAF_DEPTH - 1);
329 Coord ijk = it.getCoord();
332 typename TreeT::ValueType value;
333 bool isActive = refAcc.
probeValue(ijk, value);
336 if (!isActive) it.setValueOff();
341 outGrid->setTransform(grid.transform().copy());
342 if (gridClass !=
GRID_LEVEL_SET) outGrid->setGridClass(gridClass);
353 template<
typename Gr
idType>
355 inline typename GridType::Ptr
358 typedef typename GridType::template ValueConverter<ValueMask>::Type MaskGridT;
361 Vec3d idxMin, idxMax;
366 MaskGridT clipMask(
false);
367 clipMask.fill(region,
true,
true);
373 template<
typename Gr
idType1,
typename TreeType2>
375 inline typename GridType1::Ptr
378 typedef typename GridType1::template ValueConverter<ValueMask>::Type MaskGridT1;
379 typedef typename MaskGridT1::Ptr MaskGridPtrT1;
382 typedef typename GridType2::template ValueConverter<ValueMask>::Type MaskGridT2;
383 typedef typename MaskGridT2::Ptr MaskGridPtrT2;
389 if (grid1.constTransform() != maskGrid->constTransform()) {
390 MaskGridPtrT2 resampledMask = MaskGridT2::create(
false);
391 resampledMask->setTransform(grid1.constTransform().copy());
392 tools::resampleToMatch<clip_internal::BoolSampler>(*maskGrid, *resampledMask);
394 maskGrid = resampledMask;
398 MaskGridPtrT1 clipMask =
409 #endif // OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED
GridClass
Definition: Types.h:211
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
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 probeValue(const Coord &xyz, ValueType &value) const
Return the active state of the voxel as well as its value.
Definition: ValueAccessor.h:266
RangeType getRange(size_t grainsize=1) const
Return a tbb::blocked_range of leaf array indices.
Definition: LeafManager.h:378
math::BBox< Vec3d > BBoxd
Definition: Types.h:86
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:115
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:47
Defined various multi-threaded utility functions for trees.
Index32 Index
Definition: Types.h:58
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Vec3< double > Vec3d
Definition: Vec3.h:651
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: ValueAccessor.h:256
static Coord floor(const Vec3< T > &xyz)
Return the largest integer coordinates that are not greater than xyz (node centered conversion)...
Definition: Coord.h:82
Definition: Exceptions.h:39
bool isValueOn(const Coord &xyz) const
Return the active state of the voxel at the given coordinates.
Definition: ValueAccessor.h:263
const Vec3T & min() const
Return a const reference to the minimum point of the BBox.
Definition: BBox.h:81
const Vec3T & max() const
Return a const reference to the maximum point of the BBox.
Definition: BBox.h:84
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
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:259
LeafType & leaf(size_t leafIdx) const
Return a pointer to the leaf node at index leafIdx in the array.
Definition: LeafManager.h:354
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:354
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:214
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
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:515
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:54