62 #ifndef OPENVDB_TOOLS_PARTICLES_TO_LEVELSET_HAS_BEEN_INCLUDED
63 #define OPENVDB_TOOLS_PARTICLES_TO_LEVELSET_HAS_BEEN_INCLUDED
80 #include <tbb/parallel_reduce.h>
81 #include <tbb/blocked_range.h>
85 #include <type_traits>
98 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT = util::NullInterrupter>
99 inline void particlesToSdf(
const ParticleListT&, GridT&, InterrupterT* =
nullptr);
105 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT = util::NullInterrupter>
106 inline void particlesToSdf(
const ParticleListT&, GridT&, Real radius, InterrupterT* =
nullptr);
115 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT = util::NullInterrupter>
116 inline void particleTrailsToSdf(
const ParticleListT&, GridT&, Real delta=1, InterrupterT* =
nullptr);
122 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT = util::NullInterrupter>
123 inline void particlesToMask(
const ParticleListT&, GridT&, InterrupterT* =
nullptr);
129 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT = util::NullInterrupter>
130 inline void particlesToMask(
const ParticleListT&, GridT&, Real radius, InterrupterT* =
nullptr);
139 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT = util::NullInterrupter>
140 inline void particleTrailsToMask(
const ParticleListT&, GridT&,Real delta=1,InterrupterT* =
nullptr);
147 namespace p2ls_internal {
152 template<
typename VisibleT,
typename BlindT>
class BlindData;
157 template<
typename SdfGridT,
158 typename AttributeT =
void,
159 typename InterrupterT = util::NullInterrupter>
244 template<
typename ParticleListT>
253 template<
typename ParticleListT>
271 template<
typename ParticleListT>
275 using BlindType = p2ls_internal::BlindData<SdfType, AttType>;
279 template<
typename ParticleListT,
typename Gr
idT>
struct Raster;
282 typename AttGridType::Ptr mAttGrid;
283 BlindGridType* mBlindGrid;
284 InterrupterT* mInterrupter;
285 Real mDx, mHalfWidth;
287 size_t mMinCount, mMaxCount;
292 template<
typename SdfGr
idT,
typename AttributeT,
typename InterrupterT>
297 mInterrupter(interrupter),
298 mDx(grid.voxelSize()[0]),
299 mHalfWidth(grid.background()/mDx),
306 if (!mSdfGrid->hasUniformVoxels()) {
307 OPENVDB_THROW(RuntimeError,
"ParticlesToLevelSet only supports uniform voxels!");
310 mBlindGrid =
new BlindGridType(BlindType(grid.background()));
311 mBlindGrid->setTransform(mSdfGrid->transform().copy());
315 template<
typename SdfGr
idT,
typename AttributeT,
typename InterrupterT>
316 template<
typename ParticleListT>
321 Raster<ParticleListT, SdfGridT>
r(*
this, mSdfGrid, pa);
322 r.rasterizeSpheres();
324 Raster<ParticleListT, BlindGridType>
r(*
this, mBlindGrid, pa);
325 r.rasterizeSpheres();
329 template<
typename SdfGr
idT,
typename AttributeT,
typename InterrupterT>
330 template<
typename ParticleListT>
335 Raster<ParticleListT, SdfGridT>
r(*
this, mSdfGrid, pa);
336 r.rasterizeSpheres(radius/mDx);
338 Raster<ParticleListT, BlindGridType>
r(*
this, mBlindGrid, pa);
339 r.rasterizeSpheres(radius/mDx);
343 template<
typename SdfGr
idT,
typename AttributeT,
typename InterrupterT>
344 template<
typename ParticleListT>
349 Raster<ParticleListT, SdfGridT>
r(*
this, mSdfGrid, pa);
350 r.rasterizeTrails(delta);
352 Raster<ParticleListT, BlindGridType>
r(*
this, mBlindGrid, pa);
353 r.rasterizeTrails(delta);
358 template<
typename SdfGr
idT,
typename AttributeT,
typename InterrupterT>
377 using AttTreeT =
typename AttGridType::TreeType;
378 using AttLeafT =
typename AttTreeT::LeafNodeType;
379 using BlindTreeT =
typename BlindGridType::TreeType;
380 using BlindLeafIterT =
typename BlindTreeT::LeafCIter;
381 using BlindLeafT =
typename BlindTreeT::LeafNodeType;
382 using SdfTreeT =
typename SdfGridType::TreeType;
383 using SdfLeafT =
typename SdfTreeT::LeafNodeType;
386 const BlindTreeT& blindTree = mBlindGrid->tree();
389 typename AttTreeT::Ptr attTree(
new AttTreeT(
390 blindTree, blindTree.background().blind(), openvdb::TopologyCopy()));
392 mAttGrid =
typename AttGridType::Ptr(
new AttGridType(attTree));
393 mAttGrid->setTransform(mBlindGrid->transform().copy());
395 typename SdfTreeT::Ptr sdfTree;
399 sdfTree.reset(
new SdfTreeT(blindTree,
404 leafNodes.
foreach([&](AttLeafT& attLeaf,
size_t ) {
405 if (
const auto* blindLeaf = blindTree.probeConstLeaf(attLeaf.origin())) {
406 for (
auto iter = attLeaf.beginValueOn(); iter; ++iter) {
407 const auto pos = iter.pos();
408 attLeaf.setValueOnly(pos, blindLeaf->getValue(pos).blind());
413 const auto blindAcc = mBlindGrid->getConstAccessor();
414 auto iter = attTree->beginValueOn();
415 iter.setMaxDepth(AttTreeT::ValueOnIter::LEAF_DEPTH - 1);
416 for ( ; iter; ++iter) {
417 iter.modifyValue([&](
AttType&
v) { v = blindAcc.getValue(iter.getCoord()).blind(); });
422 sdfTree.reset(
new SdfTreeT(blindTree, blindTree.background().visible(),
TopologyCopy()));
423 for (BlindLeafIterT
n = blindTree.cbeginLeaf();
n; ++
n) {
424 const BlindLeafT& leaf = *
n;
425 const openvdb::Coord xyz = leaf.origin();
427 SdfLeafT* sdfLeaf = sdfTree->probeLeaf(xyz);
428 AttLeafT* attLeaf = attTree->probeLeaf(xyz);
430 typename BlindLeafT::ValueOnCIter m=leaf.cbeginValueOn();
433 const BlindType&
v = leaf.getValue(k);
434 sdfLeaf->setValueOnly(k, v.visible());
435 attLeaf->setValueOnly(k, v.blind());
439 const openvdb::Index k = m.pos();
440 const BlindType&
v = *m;
441 sdfLeaf->setValueOnly(k, v.visible());
442 attLeaf->setValueOnly(k, v.blind());
449 if (mSdfGrid->empty()) {
450 mSdfGrid->setTree(sdfTree);
453 mSdfGrid->tree().topologyUnion(*sdfTree);
467 template<
typename SdfGr
idT,
typename AttributeT,
typename InterrupterT>
468 template<
typename ParticleListT,
typename Gr
idT>
473 using SdfT =
typename ParticlesToLevelSetT::SdfType;
474 using AttT =
typename ParticlesToLevelSetT::AttType;
476 using AccessorT =
typename GridT::Accessor;
477 using TreeT =
typename GridT::TreeType;
478 using LeafNodeT =
typename TreeT::LeafNodeType;
486 Raster(ParticlesToLevelSetT& parent, GridT* grid,
const ParticleListT& particles)
488 , mParticles(particles)
495 mPointPartitioner =
new PointPartitionerT;
496 mPointPartitioner->construct(particles, mGrid->transform());
501 : mParent(other.mParent)
502 , mParticles(other.mParticles)
503 , mGrid(new GridT(*other.mGrid, openvdb::
ShallowCopy()))
509 , mPointPartitioner(other.mPointPartitioner)
521 delete mPointPartitioner;
527 mMinCount = mMaxCount = 0;
528 if (mParent.mInterrupter) {
529 mParent.mInterrupter->start(
"Rasterizing particles to level set using spheres");
531 mTask = std::bind(&Raster::rasterSpheres, std::placeholders::_1, std::placeholders::_2);
533 if (mParent.mInterrupter) mParent.mInterrupter->end();
538 mMinCount = radius < mParent.mRmin ? mParticles.size() : 0;
539 mMaxCount = radius > mParent.mRmax ? mParticles.size() : 0;
540 if (mMinCount>0 || mMaxCount>0) {
541 mParent.mMinCount = mMinCount;
542 mParent.mMaxCount = mMaxCount;
544 if (mParent.mInterrupter) {
545 mParent.mInterrupter->start(
546 "Rasterizing particles to level set using const spheres");
548 mTask = std::bind(&Raster::rasterFixedSpheres,
549 std::placeholders::_1, std::placeholders::_2, radius);
551 if (mParent.mInterrupter) mParent.mInterrupter->end();
557 mMinCount = mMaxCount = 0;
558 if (mParent.mInterrupter) {
559 mParent.mInterrupter->start(
"Rasterizing particles to level set using trails");
561 mTask = std::bind(&Raster::rasterTrails,
562 std::placeholders::_1, std::placeholders::_2, delta);
564 if (mParent.mInterrupter) mParent.mInterrupter->end();
568 void operator()(
const tbb::blocked_range<size_t>&
r)
572 mParent.mMinCount = mMinCount;
573 mParent.mMaxCount = mMaxCount;
577 void join(Raster& other)
584 mGrid->topologyUnion(*other.mGrid);
590 mMinCount += other.mMinCount;
591 mMaxCount += other.mMaxCount;
596 Raster&
operator=(
const Raster&) {
return *
this; }
599 bool ignoreParticle(Real R)
601 if (R < mParent.mRmin) {
605 if (R > mParent.mRmax) {
614 void rasterSpheres(
const tbb::blocked_range<size_t>&
r)
616 AccessorT acc = mGrid->getAccessor();
618 const Real invDx = 1 / mParent.mDx;
624 for (
size_t n = r.begin(),
N = r.
end();
n <
N; ++
n) {
626 typename PointPartitionerT::IndexIterator iter = mPointPartitioner->indices(
n);
627 for ( ; run && iter; ++iter) {
629 mParticles.getPosRad(
id, pos, rad);
630 const Real R = invDx * rad;
631 if (this->ignoreParticle(R))
continue;
632 const Vec3R P = mMap.applyInverseMap(pos);
633 this->getAtt<DisableT>(
id, att);
634 run = this->makeSphere(P, R, att, acc);
642 void rasterFixedSpheres(
const tbb::blocked_range<size_t>& r, Real R)
644 AccessorT acc = mGrid->getAccessor();
649 for (
size_t n = r.begin(), N = r.end();
n <
N; ++
n) {
651 for (
auto iter = mPointPartitioner->indices(
n); iter; ++iter) {
653 this->getAtt<DisableT>(
id, att);
654 mParticles.getPos(
id, pos);
655 const Vec3R P = mMap.applyInverseMap(pos);
656 this->makeSphere(P, R, att, acc);
664 void rasterTrails(
const tbb::blocked_range<size_t>& r, Real delta)
666 AccessorT acc = mGrid->getAccessor();
671 const Vec3R origin = mMap.applyInverseMap(
Vec3R(0,0,0));
672 const Real Rmin = mParent.mRmin, invDx = 1 / mParent.mDx;
675 for (
size_t n = r.begin(), N = r.end();
n <
N; ++
n) {
677 typename PointPartitionerT::IndexIterator iter = mPointPartitioner->indices(
n);
678 for ( ; run && iter; ++iter) {
680 mParticles.getPosRadVel(
id, pos, rad, vel);
681 const Real R0 = invDx * rad;
682 if (this->ignoreParticle(R0))
continue;
683 this->getAtt<DisableT>(
id, att);
684 const Vec3R P0 = mMap.applyInverseMap(pos);
685 const Vec3R V = mMap.applyInverseMap(vel) - origin;
686 const Real speed = V.length(), invSpeed = 1.0 / speed;
687 const Vec3R Nrml = -V * invSpeed;
690 while (run && d <= speed) {
691 run = this->makeSphere(P, R, att, acc);
692 P += 0.5 * delta * R * Nrml;
694 R = R0 - (R0 - Rmin) * d * invSpeed;
705 if (mParent.mGrainSize>0) {
706 tbb::parallel_reduce(
707 tbb::blocked_range<size_t>(0, bucketCount, mParent.mGrainSize), *
this);
709 (*this)(tbb::blocked_range<size_t>(0, bucketCount));
727 template <
bool IsMaskT = OutputIsMask>
729 makeSphere(
const Vec3R& P, Real R,
const AttT& att, AccessorT& acc)
733 w = mParent.mHalfWidth,
741 const ValueT inside = -mGrid->background();
745 for (Coord
c = lo;
c.x() <= hi.x(); ++
c.x()) {
748 thread::cancelGroupExecution();
752 for (
c.y() = lo.y();
c.y() <= hi.y(); ++
c.y()) {
754 for (
c.z() = lo.z();
c.z() <= hi.z(); ++
c.z()) {
756 #if defined __INTEL_COMPILER
757 _Pragma(
"warning (push)")
758 _Pragma("warning (disable:186)")
760 if (x2y2z2 >= max2 || (!acc.probeValue(
c, v) && (v < ValueT(0))))
762 #if defined __INTEL_COMPILER
763 _Pragma(
"warning (pop)")
765 if (x2y2z2 <= min2) {
766 acc.setValueOff(
c, inside);
771 const ValueT d = Merge(static_cast<SdfT>(dx*(
math::Sqrt(x2y2z2)-R)), att);
772 if (d < v) acc.setValue(
c, d);
781 template <
bool IsMaskT = OutputIsMask>
783 makeSphere(
const Vec3R& p, Real r,
const AttT& att, AccessorT& acc)
797 const std::vector<CoordBBox> padding{
798 CoordBBox(outLo.x(), outLo.y(), outLo.z(), inLo.x()-1, outHi.y(), outHi.z()),
799 CoordBBox(inHi.x()+1, outLo.y(), outLo.z(), outHi.x(), outHi.y(), outHi.z()),
800 CoordBBox(outLo.x(), outLo.y(), outLo.z(), outHi.x(), inLo.y()-1, outHi.z()),
801 CoordBBox(outLo.x(), inHi.y()+1, outLo.z(), outHi.x(), outHi.y(), outHi.z()),
802 CoordBBox(outLo.x(), outLo.y(), outLo.z(), outHi.x(), outHi.y(), inLo.z()-1),
803 CoordBBox(outLo.x(), outLo.y(), inHi.z()+1, outHi.x(), outHi.y(), outHi.z()),
805 const ValueT onValue = Merge(SdfT(1), att);
809 acc.tree().sparseFill(
CoordBBox(inLo, inHi), onValue);
812 for (
const auto& bbox: padding) {
814 thread::cancelGroupExecution();
817 const Coord &bmin = bbox.min(), &bmax = bbox.max();
820 for (c = bmin, cx = c.x(); c.x() <= bmax.x(); ++c.x(), cx += 1) {
822 for (c.y() = bmin.y(), cy = c.y(); c.y() <= bmax.y(); ++c.y(), cy += 1) {
824 for (c.z() = bmin.z(), cz = c.z(); c.z() <= bmax.z(); ++c.z(), cz += 1) {
826 if (x2y2z2 < rSquared) {
827 acc.setValue(c, onValue);
836 using FuncType =
typename std::function<void (Raster*, const tbb::blocked_range<size_t>&)>;
838 template<
typename DisableType>
840 getAtt(
size_t, AttT&)
const {}
842 template<
typename DisableType>
844 getAtt(
size_t n, AttT&
a)
const { mParticles.getAtt(n, a); }
848 Merge(T
s,
const AttT&)
const {
return s; }
852 Merge(T
s,
const AttT& a)
const {
return ValueT(s,a); }
854 ParticlesToLevelSetT& mParent;
855 const ParticleListT& mParticles;
857 const math::MapBase& mMap;
858 size_t mMinCount, mMaxCount;
861 PointPartitionerT* mPointPartitioner;
869 namespace p2ls_internal {
875 template<
typename VisibleT,
typename BlindT>
879 using type = VisibleT;
880 using VisibleType = VisibleT;
881 using BlindType = BlindT;
884 explicit BlindData(VisibleT v) : mVisible(v), mBlind(
zeroVal<BlindType>()) {}
885 BlindData(VisibleT v, BlindT
b) : mVisible(v), mBlind(b) {}
886 BlindData(
const BlindData&) =
default;
887 BlindData&
operator=(
const BlindData&) =
default;
888 const VisibleT& visible()
const {
return mVisible; }
889 const BlindT& blind()
const {
return mBlind; }
891 bool operator==(
const BlindData& rhs)
const {
return mVisible == rhs.mVisible; }
893 bool operator< (
const BlindData& rhs)
const {
return mVisible < rhs.mVisible; }
894 bool operator> (
const BlindData& rhs)
const {
return mVisible > rhs.mVisible; }
895 BlindData
operator+(
const BlindData& rhs)
const {
return BlindData(mVisible + rhs.mVisible); }
896 BlindData
operator-(
const BlindData& rhs)
const {
return BlindData(mVisible - rhs.mVisible); }
897 BlindData
operator-()
const {
return BlindData(-mVisible, mBlind); }
906 template<
typename VisibleT,
typename BlindT>
907 inline std::ostream& operator<<(std::ostream& ostr, const BlindData<VisibleT, BlindT>& rhs)
909 ostr << rhs.visible();
915 template<
typename VisibleT,
typename BlindT>
916 inline BlindData<VisibleT, BlindT>
Abs(
const BlindData<VisibleT, BlindT>&
x)
918 return BlindData<VisibleT, BlindT>(
math::Abs(x.visible()), x.blind());
923 template<
typename VisibleT,
typename BlindT,
typename T>
924 inline BlindData<VisibleT, BlindT>
925 operator+(
const BlindData<VisibleT, BlindT>& x,
const T& rhs)
927 return BlindData<VisibleT, BlindT>(x.visible() +
static_cast<VisibleT
>(rhs), x.blind());
939 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT>
944 "particlesToSdf requires an SDF grid with floating-point values");
948 " try Grid::setGridClass(openvdb::GRID_LEVEL_SET)");
956 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT>
961 "particlesToSdf requires an SDF grid with floating-point values");
965 " try Grid::setGridClass(openvdb::GRID_LEVEL_SET)");
973 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT>
978 "particleTrailsToSdf requires an SDF grid with floating-point values");
982 " try Grid::setGridClass(openvdb::GRID_LEVEL_SET)");
990 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT>
995 "particlesToMask requires a boolean-valued grid");
1001 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT>
1006 "particlesToMask requires a boolean-valued grid");
1012 template<
typename Gr
idT,
typename ParticleListT,
typename InterrupterT>
1017 "particleTrailsToMask requires a boolean-valued grid");
1027 #endif // OPENVDB_TOOLS_PARTICLES_TO_LEVELSET_HAS_BEEN_INCLUDED
size_t getMaxCount() const
Return the number of particles that were ignored because they were larger than the maximum radius...
Real getHalfWidth() const
Return the half-width of the narrow band in voxel units.
int Ceil(float x)
Return the ceiling of x.
Functions to efficiently perform various compositing operations on grids.
Tag dispatch class that distinguishes shallow copy constructors from deep copy constructors.
Type Pow2(Type x)
Return x2.
Real getRmax() const
Return the largest radius allowed in voxel units.
AttGridType::Ptr attributeGrid()
Return a pointer to the grid containing the optional user-defined attribute.
Real getVoxelSize() const
Return the size of a voxel in world units.
GLsizei const GLfloat * value
void finalize(bool prune=false)
This method syncs up the level set and attribute grids and therefore needs to be called before any of...
IMATH_HOSTDEVICE constexpr Plane3< T > operator-(const Plane3< T > &plane) IMATH_NOEXCEPT
Reflect the pla.
#define OPENVDB_LOG_WARN(mesg)
GLboolean GLboolean GLboolean GLboolean a
GLuint GLsizei GLsizei * length
#define OPENVDB_USE_VERSION_NAMESPACE
GA_API const UT_StringHolder P
void setGrainSize(int grainSize)
Set the grain size used for threading.
typename SdfGridT::ValueType SdfType
ParticlesToLevelSet(SdfGridT &grid, InterrupterT *interrupt=nullptr)
Constructor using an existing boolean or narrow-band level set grid.
Spatially partitions points using a parallel radix-based sorting algorithm.
#define OPENVDB_ASSERT(X)
SYS_FORCE_INLINE const_iterator end() const
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
int getGrainSize() const
Return the grain size used for threading.
OIIO_FORCEINLINE vbool4 operator>(const vint4 &a, const vint4 &b)
GLint GLint GLsizei GLint GLenum GLenum type
Coord Abs(const Coord &xyz)
void rasterizeSpheres(const ParticleListT &pa)
Rasterize each particle as a sphere with the particle's position and radius.
typename std::conditional< DisableT::value, size_t, AttributeT >::type AttType
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
static const bool OutputIsMask
void setRmax(Real Rmax)
Set the largest radius allowed in voxel units.
float Sqrt(float x)
Return the square root of a floating-point value.
Defined various multi-threaded utility functions for trees.
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
bool operator<(const GU_TetrahedronFacet &a, const GU_TetrahedronFacet &b)
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
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.
typename SdfGridT::template ValueConverter< AttType >::Type AttGridType
GLboolean GLboolean GLboolean b
GA_API const UT_StringHolder transform
IMATH_HOSTDEVICE constexpr Quat< T > operator+(const Quat< T > &q1, const Quat< T > &q2) IMATH_NOEXCEPT
Quaterion addition.
size_t getMinCount() const
Return the number of particles that were ignored because they were smaller than the minimum radius...
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
bool ignoredParticles() const
Return true if any particles were ignored due to their size.
void setRmin(Real Rmin)
Set the smallest radius allowed in voxel units.
void rasterizeTrails(const ParticleListT &pa, Real delta=1.0)
Rasterize each particle as a trail comprising the CSG union of spheres of decreasing radius...
LeafData & operator=(const LeafData &)=delete
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GA_API const UT_StringHolder N
InterrupterT InterrupterType
int Floor(float x)
Return the floor of x.
GLubyte GLubyte GLubyte GLubyte w
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
void OIIO_UTIL_API split(string_view str, std::vector< string_view > &result, string_view sep=string_view(), int maxsplit=-1)
typename std::is_void< AttributeT >::type DisableT
bool wasInterrupted(T *i, int percent=-1)
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Real getRmin() const
Return the smallest radius allowed in voxel units.
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
#define OPENVDB_THROW(exception, message)
#define OPENVDB_NO_FP_EQUALITY_WARNING_END