36 #ifndef OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED 37 #define OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED 45 #include <tbb/blocked_range.h> 46 #include <tbb/parallel_reduce.h> 47 #include <type_traits> 64 template<
typename Gr
idType>
65 inline typename GridType::Ptr
66 clip(
const GridType& grid,
const BBoxd& bbox,
bool keepInterior =
true);
75 template<
typename Gr
idType>
76 inline typename GridType::Ptr
77 clip(
const GridType& grid,
const math::NonlinearFrustumMap& frustum,
bool keepInterior =
true);
91 template<
typename Gr
idType,
typename MaskTreeType>
92 inline typename GridType::Ptr
93 clip(
const GridType& grid,
const Grid<MaskTreeType>& mask,
bool keepInterior =
true);
99 namespace clip_internal {
107 template<
typename TreeT>
111 using ValueT =
typename TreeT::ValueType;
116 template<
typename LeafNodeType>
119 const auto* refLeaf = mAcc.probeConstLeaf(leaf.origin());
121 for (
auto iter = leaf.beginValueOff(); iter; ++iter) {
122 const auto pos = iter.pos();
136 template<
typename TreeT>
140 using MaskTreeT =
typename TreeT::template ValueConverter<MaskValueType>::Type;
145 void run(
bool threaded =
true);
147 typename TreeT::Ptr
tree()
const {
return mNewTree; }
150 void operator()(
const tbb::blocked_range<size_t>&);
154 const MaskTreeT* mClipMask;
156 const MaskLeafManagerT* mLeafNodes;
157 typename TreeT::Ptr mNewTree;
161 template<
typename TreeT>
164 , mLeafNodes(&leafNodes)
165 , mNewTree(new TreeT(mTree->background()))
170 template<
typename TreeT>
173 , mLeafNodes(rhs.mLeafNodes)
174 , mNewTree(new TreeT(mTree->background()))
179 template<
typename TreeT>
183 if (threaded) tbb::parallel_reduce(mLeafNodes->getRange(), *
this);
184 else (*
this)(mLeafNodes->getRange());
188 template<
typename TreeT>
195 for (
auto n = range.begin(); n != range.end(); ++n) {
196 const auto& maskLeaf = mLeafNodes->leaf(n);
197 const auto& ijk = maskLeaf.origin();
203 for (
auto it = maskLeaf.cbeginValueOn(); it; ++it) {
204 const auto pos = it.pos();
205 newLeaf->setValueOnly(pos, refLeaf->getValue(pos));
206 newLeaf->setActiveState(pos, refLeaf->isValueOn(pos));
209 typename TreeT::ValueType value;
210 bool isActive = refAcc.
probeValue(ijk, value);
212 for (
auto it = maskLeaf.cbeginValueOn(); it; ++it) {
213 const auto pos = it.pos();
214 newLeaf->setValueOnly(pos, value);
215 newLeaf->setActiveState(pos, isActive);
227 static const char*
name() {
return "bin"; }
232 template<
class TreeT>
234 const Vec3R& inCoord,
typename TreeT::ValueType& result)
236 return inTree.probeValue(
Coord::floor(inCoord), result);
245 template<
typename FromGr
idT,
typename ToGr
idT>
255 template<
typename Gr
idT>
269 template<
typename Gr
idT>
270 inline typename std::enable_if<!std::is_same<MaskValueType, typename GridT::BuildType>::value,
271 typename GridT::template ValueConverter<MaskValueType>::Type::Ptr>::type
272 convertToMaskGrid(
const GridT& grid)
274 using MaskGridT =
typename GridT::template ValueConverter<MaskValueType>::Type;
275 auto mask = MaskGridT::create(
false);
276 mask->topologyUnion(grid);
277 mask->setTransform(grid.constTransform().copy());
283 template<
typename Gr
idT>
284 inline typename std::enable_if<std::is_same<MaskValueType, typename GridT::BuildType>::value,
285 typename GridT::ConstPtr>::type
286 convertToMaskGrid(
const GridT& grid)
296 template<
typename Gr
idType>
297 inline typename GridType::Ptr
299 const GridType& grid,
300 const typename GridType::template ValueConverter<MaskValueType>::Type& clipMask,
303 using TreeT =
typename GridType::TreeType;
304 using MaskTreeT =
typename GridType::TreeType::template ValueConverter<MaskValueType>::Type;
306 const auto gridClass = grid.getGridClass();
307 const auto& tree = grid.tree();
309 MaskTreeT gridMask(
false);
310 gridMask.topologyUnion(tree);
314 leafNodes.foreach(MaskInteriorVoxels<TreeT>(tree));
318 typename MaskTreeT::ValueAllIter iter(gridMask);
319 iter.setMaxDepth(MaskTreeT::ValueAllIter::LEAF_DEPTH - 1);
321 for ( ; iter; ++iter) {
327 gridMask.topologyIntersection(clipMask.constTree());
329 gridMask.topologyDifference(clipMask.constTree());
332 typename GridType::Ptr outGrid;
335 tree::LeafManager<const MaskTreeT> leafNodes(gridMask);
336 CopyLeafNodes<TreeT> maskOp(tree, leafNodes);
338 outGrid = GridType::create(maskOp.tree());
342 tree::ValueAccessor<const TreeT> refAcc(tree);
343 tree::ValueAccessor<const MaskTreeT> maskAcc(gridMask);
345 typename TreeT::ValueAllIter it(outGrid->tree());
346 it.setMaxDepth(TreeT::ValueAllIter::LEAF_DEPTH - 1);
348 Coord ijk = it.getCoord();
350 if (maskAcc.isValueOn(ijk)) {
351 typename TreeT::ValueType value;
352 bool isActive = refAcc.probeValue(ijk, value);
355 if (!isActive) it.setValueOff();
360 outGrid->setTransform(grid.transform().copy());
361 if (gridClass !=
GRID_LEVEL_SET) outGrid->setGridClass(gridClass);
373 template<
typename Gr
idType>
374 inline typename GridType::Ptr
375 clip(
const GridType& grid,
const BBoxd& bbox,
bool keepInterior)
378 using MaskGridT =
typename GridType::template ValueConverter<MaskValueT>::Type;
381 Vec3d idxMin, idxMax;
386 MaskGridT clipMask(
false);
387 clipMask.fill(region,
true,
true);
389 return clip_internal::doClip(grid, clipMask, keepInterior);
394 template<
typename SrcGr
idType,
typename ClipTreeType>
395 inline typename SrcGridType::Ptr
400 using SrcMaskGridType =
typename SrcGridType::template ValueConverter<MaskValueT>::Type;
401 using ClipMaskGridType =
typename ClipGridType::template ValueConverter<MaskValueT>::Type;
404 auto maskGrid = clip_internal::convertToMaskGrid(clipGrid);
407 if (srcGrid.constTransform() != maskGrid->constTransform()) {
408 auto resampledMask = ClipMaskGridType::create(
false);
409 resampledMask->setTransform(srcGrid.constTransform().copy());
410 tools::resampleToMatch<clip_internal::BoolSampler>(*maskGrid, *resampledMask);
412 maskGrid = resampledMask;
416 auto clipMask = clip_internal::ConvertGrid<
417 ClipMaskGridType, SrcMaskGridType>()(maskGrid);
420 return clip_internal::doClip(srcGrid, *clipMask, keepInterior);
425 template<
typename Gr
idType>
426 inline typename GridType::Ptr
429 using ValueT =
typename GridType::ValueType;
430 using TreeT =
typename GridType::TreeType;
431 using LeafT =
typename TreeT::LeafNodeType;
433 const auto& gridXform = inGrid.transform();
434 const auto frustumIndexBBox = frustumMap.
getBBox();
437 auto frustumContainsCoord = [&](
const Coord& ijk) ->
bool {
438 auto xyz = gridXform.indexToWorld(ijk);
440 return frustumIndexBBox.isInside(xyz);
445 auto toFrustumIndexSpace = [&](
const CoordBBox& inBBox) ->
BBoxd {
446 const Coord bounds[2] = { inBBox.
min(), inBBox.
max() };
449 for (
int i = 0; i < 8; ++i) {
450 ijk[0] = bounds[(i & 1) >> 0][0];
451 ijk[1] = bounds[(i & 2) >> 1][1];
452 ijk[2] = bounds[(i & 4) >> 2][2];
453 auto xyz = gridXform.indexToWorld(ijk);
461 #if OPENVDB_ABI_VERSION_NUMBER <= 3 462 auto outGrid = inGrid.copy(CP_NEW);
464 auto outGrid = inGrid.copyWithNewTree();
471 const auto& bg = outGrid->background();
473 auto outAcc = outGrid->getAccessor();
479 auto tileIter = inGrid.beginValueAll();
480 tileIter.setMaxDepth(GridType::ValueAllIter::LEAF_DEPTH - 1);
482 for ( ; tileIter; ++tileIter) {
483 const bool tileActive = tileIter.isValueOn();
484 const auto& tileValue = tileIter.getValue();
490 tileIter.getBoundingBox(tileBBox);
491 const auto tileFrustumBBox = toFrustumIndexSpace(tileBBox);
494 enum class CopyTile { kNone, kPartial, kFull };
495 auto copyTile = CopyTile::kNone;
497 if (frustumIndexBBox.isInside(tileFrustumBBox)) {
498 copyTile = CopyTile::kFull;
499 }
else if (frustumIndexBBox.hasOverlap(tileFrustumBBox)) {
500 copyTile = CopyTile::kPartial;
503 if (!frustumIndexBBox.hasOverlap(tileFrustumBBox)) {
504 copyTile = CopyTile::kFull;
505 }
else if (!frustumIndexBBox.isInside(tileFrustumBBox)) {
506 copyTile = CopyTile::kPartial;
510 case CopyTile::kNone:
512 case CopyTile::kFull:
514 outAcc.addTile(tileIter.getLevel(), tileBBox.
min(), tileValue, tileActive);
516 case CopyTile::kPartial:
518 for (std::vector<CoordBBox> bboxVec = { tileBBox }; !bboxVec.
empty(); ) {
523 if (bboxVec.back().volume() > 64 && bboxVec.back().is_divisible()) {
525 bboxVec.emplace_back(bboxVec.back(), tbb::split{});
528 auto subBBox = bboxVec.back();
533 if (!frustumIndexBBox.hasOverlap(toFrustumIndexSpace(subBBox)))
continue;
535 if (frustumIndexBBox.isInside(toFrustumIndexSpace(subBBox)))
continue;
539 for (
const auto& ijk: subBBox) {
540 if (frustumContainsCoord(ijk) == keepInterior) {
542 outAcc.setValueOn(ijk, tileValue);
544 outAcc.setValueOff(ijk, tileValue);
557 for (
auto leafIter = inGrid.constTree().beginLeaf(); leafIter; ++leafIter) {
558 const auto leafBBox = leafIter->getNodeBoundingBox();
559 const auto leafFrustumBBox = toFrustumIndexSpace(leafBBox);
561 if (frustumIndexBBox.hasOverlap(leafFrustumBBox)) {
562 outAcc.touchLeaf(leafBBox.min());
565 if (!frustumIndexBBox.hasOverlap(leafFrustumBBox)
566 || !frustumIndexBBox.isInside(leafFrustumBBox))
568 outAcc.touchLeaf(leafBBox.min());
576 outLeafNodes.foreach(
577 [&](LeafT& leaf,
size_t ) {
578 auto inAcc = inGrid.getConstAccessor();
580 for (
auto voxelIter = leaf.beginValueAll(); voxelIter; ++voxelIter) {
581 const auto ijk = voxelIter.getCoord();
582 if (frustumContainsCoord(ijk) == keepInterior) {
583 const bool active = inAcc.probeValue(ijk, val);
584 voxelIter.setValue(val);
585 voxelIter.setValueOn(active);
598 #endif // OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
static Coord floor(const Vec3< T > &xyz)
Return the largest integer coordinates that are not greater than xyz (node centered conversion).
Definition: Coord.h:83
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:267
const Vec3T & max() const
Return a const reference to the maximum point of this bounding box.
Definition: BBox.h:91
Vec3< double > Vec3d
Definition: Vec3.h:679
This map is composed of three steps. First it will take a box of size (Lx X Ly X Lz) defined by a mem...
Definition: Maps.h:1905
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:55
Defined various multi-threaded utility functions for trees.
void expand(ElementType padding)
Pad this bounding box.
Definition: BBox.h:348
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
Vec3d applyInverseMap(const Vec3d &in) const override
Return the pre-image of in under the map.
Definition: Maps.h:2117
const BBoxd & getBBox() const
Return the bounding box that defines the frustum in pre-image space.
Definition: Maps.h:2377
const Coord & min() const
Definition: Coord.h:337
Definition: Exceptions.h:40
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:264
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:110
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:358
static Coord min()
Return the smallest possible coordinate.
Definition: Coord.h:70
const Vec3T & min() const
Return a const reference to the minimum point of this bounding box.
Definition: BBox.h:89
const TreeType & tree() const
Return a const reference to tree associated with this manager.
Definition: LeafManager.h:343
LeafNodeT * touchLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists,...
Definition: ValueAccessor.h:386
math::BBox< Vec3d > BBoxd
Definition: Types.h:91
static Coord max()
Return the largest possible coordinate.
Definition: Coord.h:73
const LeafNodeT * probeConstLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z), or nullptr if no such node exists.
Definition: ValueAccessor.h:422
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:338
bool empty() const
Return true if this bounding box is empty (i.e., encloses no coordinates).
Definition: Coord.h:372