OpenVDB  6.0.0
ParticlesToLevelSet.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
88 
89 #ifndef OPENVDB_TOOLS_PARTICLES_TO_LEVELSET_HAS_BEEN_INCLUDED
90 #define OPENVDB_TOOLS_PARTICLES_TO_LEVELSET_HAS_BEEN_INCLUDED
91 
92 #include <tbb/parallel_reduce.h>
93 #include <tbb/blocked_range.h>
94 #include <openvdb/Types.h>
95 #include <openvdb/Grid.h>
96 #include <openvdb/math/Math.h>
97 #include <openvdb/math/Transform.h>
99 #include <openvdb/util/logging.h>
101 #include "Composite.h" // for csgUnion()
102 #include "PointPartitioner.h"
103 #include "Prune.h"
104 #include "SignedFloodFill.h"
105 #include <functional>
106 #include <iostream>
107 #include <type_traits>
108 #include <vector>
109 
110 
111 namespace openvdb {
113 namespace OPENVDB_VERSION_NAME {
114 namespace tools {
115 
120 template<typename GridT, typename ParticleListT, typename InterrupterT = util::NullInterrupter>
121 inline void particlesToSdf(const ParticleListT&, GridT&, InterrupterT* = nullptr);
122 
127 template<typename GridT, typename ParticleListT, typename InterrupterT = util::NullInterrupter>
128 inline void particlesToSdf(const ParticleListT&, GridT&, Real radius, InterrupterT* = nullptr);
129 
137 template<typename GridT, typename ParticleListT, typename InterrupterT = util::NullInterrupter>
138 inline void particleTrailsToSdf(const ParticleListT&, GridT&, Real delta=1, InterrupterT* =nullptr);
139 
144 template<typename GridT, typename ParticleListT, typename InterrupterT = util::NullInterrupter>
145 inline void particlesToMask(const ParticleListT&, GridT&, InterrupterT* = nullptr);
146 
151 template<typename GridT, typename ParticleListT, typename InterrupterT = util::NullInterrupter>
152 inline void particlesToMask(const ParticleListT&, GridT&, Real radius, InterrupterT* = nullptr);
153 
161 template<typename GridT, typename ParticleListT, typename InterrupterT = util::NullInterrupter>
162 inline void particleTrailsToMask(const ParticleListT&, GridT&,Real delta=1,InterrupterT* =nullptr);
163 
164 
166 
167 
168 namespace p2ls_internal {
169 // This is a simple type that combines a distance value and a particle
170 // attribute. It's required for attribute transfer which is performed
171 // in the ParticlesToLevelSet::Raster member class defined below.
173 template<typename VisibleT, typename BlindT> class BlindData;
174 }
175 
176 
177 template<typename SdfGridT,
178  typename AttributeT = void,
179  typename InterrupterT = util::NullInterrupter>
181 {
182 public:
183  using DisableT = typename std::is_void<AttributeT>::type;
184  using InterrupterType = InterrupterT;
185 
186  using SdfGridType = SdfGridT;
187  using SdfType = typename SdfGridT::ValueType;
188 
189  using AttType = typename std::conditional<DisableT::value, size_t, AttributeT>::type;
190  using AttGridType = typename SdfGridT::template ValueConverter<AttType>::Type;
191 
192  static const bool OutputIsMask = std::is_same<SdfType, bool>::value;
193 
212  explicit ParticlesToLevelSet(SdfGridT& grid, InterrupterT* interrupt = nullptr);
213 
214  ~ParticlesToLevelSet() { delete mBlindGrid; }
215 
224  void finalize(bool prune = false);
225 
229  typename AttGridType::Ptr attributeGrid() { return mAttGrid; }
230 
232  Real getVoxelSize() const { return mDx; }
233 
235  Real getHalfWidth() const { return mHalfWidth; }
236 
238  Real getRmin() const { return mRmin; }
240  void setRmin(Real Rmin) { mRmin = math::Max(Real(0),Rmin); }
241 
243  Real getRmax() const { return mRmax; }
245  void setRmax(Real Rmax) { mRmax = math::Max(mRmin,Rmax); }
246 
248  bool ignoredParticles() const { return mMinCount>0 || mMaxCount>0; }
251  size_t getMinCount() const { return mMinCount; }
254  size_t getMaxCount() const { return mMaxCount; }
255 
257  int getGrainSize() const { return mGrainSize; }
260  void setGrainSize(int grainSize) { mGrainSize = grainSize; }
261 
264  template<typename ParticleListT>
265  void rasterizeSpheres(const ParticleListT& pa);
266 
273  template<typename ParticleListT>
274  void rasterizeSpheres(const ParticleListT& pa, Real radius);
275 
291  template<typename ParticleListT>
292  void rasterizeTrails(const ParticleListT& pa, Real delta=1.0);
293 
294 private:
295  using BlindType = p2ls_internal::BlindData<SdfType, AttType>;
296  using BlindGridType = typename SdfGridT::template ValueConverter<BlindType>::Type;
297 
299  template<typename ParticleListT, typename GridT> struct Raster;
300 
301  SdfGridType* mSdfGrid;
302  typename AttGridType::Ptr mAttGrid;
303  BlindGridType* mBlindGrid;
304  InterrupterT* mInterrupter;
305  Real mDx, mHalfWidth;
306  Real mRmin, mRmax; // ignore particles outside this range of radii in voxel
307  size_t mMinCount, mMaxCount; // counters for ignored particles
308  int mGrainSize;
309 }; // class ParticlesToLevelSet
310 
311 
312 template<typename SdfGridT, typename AttributeT, typename InterrupterT>
313 inline ParticlesToLevelSet<SdfGridT, AttributeT, InterrupterT>::
314 ParticlesToLevelSet(SdfGridT& grid, InterrupterT* interrupter) :
315  mSdfGrid(&grid),
316  mBlindGrid(nullptr),
317  mInterrupter(interrupter),
318  mDx(grid.voxelSize()[0]),
319  mHalfWidth(grid.background()/mDx),
320  mRmin(1.5),// corresponds to the Nyquist grid sampling frequency
321  mRmax(100.0),// corresponds to a huge particle (probably too large!)
322  mMinCount(0),
323  mMaxCount(0),
324  mGrainSize(1)
325 {
326  if (!mSdfGrid->hasUniformVoxels()) {
327  OPENVDB_THROW(RuntimeError, "ParticlesToLevelSet only supports uniform voxels!");
328  }
329  if (!DisableT::value) {
330  mBlindGrid = new BlindGridType(BlindType(grid.background()));
331  mBlindGrid->setTransform(mSdfGrid->transform().copy());
332  }
333 }
334 
335 template<typename SdfGridT, typename AttributeT, typename InterrupterT>
336 template<typename ParticleListT>
338 rasterizeSpheres(const ParticleListT& pa)
339 {
340  if (DisableT::value) {
341  Raster<ParticleListT, SdfGridT> r(*this, mSdfGrid, pa);
342  r.rasterizeSpheres();
343  } else {
344  Raster<ParticleListT, BlindGridType> r(*this, mBlindGrid, pa);
345  r.rasterizeSpheres();
346  }
347 }
348 
349 template<typename SdfGridT, typename AttributeT, typename InterrupterT>
350 template<typename ParticleListT>
352 rasterizeSpheres(const ParticleListT& pa, Real radius)
353 {
354  if (DisableT::value) {
355  Raster<ParticleListT, SdfGridT> r(*this, mSdfGrid, pa);
356  r.rasterizeSpheres(radius/mDx);
357  } else {
358  Raster<ParticleListT, BlindGridType> r(*this, mBlindGrid, pa);
359  r.rasterizeSpheres(radius/mDx);
360  }
361 }
362 
363 template<typename SdfGridT, typename AttributeT, typename InterrupterT>
364 template<typename ParticleListT>
366 rasterizeTrails(const ParticleListT& pa, Real delta)
367 {
368  if (DisableT::value) {
369  Raster<ParticleListT, SdfGridT> r(*this, mSdfGrid, pa);
370  r.rasterizeTrails(delta);
371  } else {
372  Raster<ParticleListT, BlindGridType> r(*this, mBlindGrid, pa);
373  r.rasterizeTrails(delta);
374  }
375 }
376 
377 
378 template<typename SdfGridT, typename AttributeT, typename InterrupterT>
379 inline void
381 {
383 
384  if (!mBlindGrid) {
385  if (prune) {
386  if (OutputIsMask) {
387  tools::prune(mSdfGrid->tree());
388  } else {
389  tools::pruneLevelSet(mSdfGrid->tree());
390  }
391  }
392  return;
393  }
394 
395  if (prune) tools::prune(mBlindGrid->tree());
396 
397  using AttTreeT = typename AttGridType::TreeType;
398  using AttLeafT = typename AttTreeT::LeafNodeType;
399  using BlindTreeT = typename BlindGridType::TreeType;
400  using BlindLeafIterT = typename BlindTreeT::LeafCIter;
401  using BlindLeafT = typename BlindTreeT::LeafNodeType;
402  using SdfTreeT = typename SdfGridType::TreeType;
403  using SdfLeafT = typename SdfTreeT::LeafNodeType;
404 
405  // Use topology copy constructors since output grids have the same topology as mBlindDataGrid
406  const BlindTreeT& blindTree = mBlindGrid->tree();
407 
408  // Create the output attribute grid.
409  typename AttTreeT::Ptr attTree(new AttTreeT(
410  blindTree, blindTree.background().blind(), openvdb::TopologyCopy()));
411  // Note this overwrites any existing attribute grids!
412  mAttGrid = typename AttGridType::Ptr(new AttGridType(attTree));
413  mAttGrid->setTransform(mBlindGrid->transform().copy());
414 
415  typename SdfTreeT::Ptr sdfTree; // the output mask or level set tree
416 
417  // Extract the attribute grid and the mask or level set grid from mBlindDataGrid.
418  if (OutputIsMask) {
419  sdfTree.reset(new SdfTreeT(blindTree,
420  /*off=*/SdfType(0), /*on=*/SdfType(1), TopologyCopy()));
421 
422  // Copy leaf voxels in parallel.
423  tree::LeafManager<AttTreeT> leafNodes(*attTree);
424  leafNodes.foreach([&](AttLeafT& attLeaf, size_t /*leafIndex*/) {
425  if (const auto* blindLeaf = blindTree.probeConstLeaf(attLeaf.origin())) {
426  for (auto iter = attLeaf.beginValueOn(); iter; ++iter) {
427  const auto pos = iter.pos();
428  attLeaf.setValueOnly(pos, blindLeaf->getValue(pos).blind());
429  }
430  }
431  });
432  // Copy tiles serially.
433  const auto blindAcc = mBlindGrid->getConstAccessor();
434  auto iter = attTree->beginValueOn();
435  iter.setMaxDepth(AttTreeT::ValueOnIter::LEAF_DEPTH - 1);
436  for ( ; iter; ++iter) {
437  iter.modifyValue([&](AttType& v) { v = blindAcc.getValue(iter.getCoord()).blind(); });
438  }
439  } else {
440  // Here we exploit the fact that by design level sets have no active tiles.
441  // Only leaf voxels can be active.
442  sdfTree.reset(new SdfTreeT(blindTree, blindTree.background().visible(), TopologyCopy()));
443  for (BlindLeafIterT n = blindTree.cbeginLeaf(); n; ++n) {
444  const BlindLeafT& leaf = *n;
445  const openvdb::Coord xyz = leaf.origin();
446  // Get leafnodes that were allocated during topology construction!
447  SdfLeafT* sdfLeaf = sdfTree->probeLeaf(xyz);
448  AttLeafT* attLeaf = attTree->probeLeaf(xyz);
449  // Use linear offset (vs coordinate) access for better performance!
450  typename BlindLeafT::ValueOnCIter m=leaf.cbeginValueOn();
451  if (!m) {//no active values in leaf node so copy everything
452  for (openvdb::Index k = 0; k!=BlindLeafT::SIZE; ++k) {
453  const BlindType& v = leaf.getValue(k);
454  sdfLeaf->setValueOnly(k, v.visible());
455  attLeaf->setValueOnly(k, v.blind());
456  }
457  } else {//only copy active values (using flood fill for the inactive values)
458  for(; m; ++m) {
459  const openvdb::Index k = m.pos();
460  const BlindType& v = *m;
461  sdfLeaf->setValueOnly(k, v.visible());
462  attLeaf->setValueOnly(k, v.blind());
463  }
464  }
465  }
466  tools::signedFloodFill(*sdfTree);//required since we only transferred active voxels!
467  }
468 
469  if (mSdfGrid->empty()) {
470  mSdfGrid->setTree(sdfTree);
471  } else {
472  if (OutputIsMask) {
473  mSdfGrid->tree().topologyUnion(*sdfTree);
474  tools::prune(mSdfGrid->tree());
475  } else {
476  tools::csgUnion(mSdfGrid->tree(), *sdfTree, /*prune=*/true);
477  }
478  }
479 
481 }
482 
483 
485 
486 
487 template<typename SdfGridT, typename AttributeT, typename InterrupterT>
488 template<typename ParticleListT, typename GridT>
489 struct ParticlesToLevelSet<SdfGridT, AttributeT, InterrupterT>::Raster
490 {
491  using DisableT = typename std::is_void<AttributeT>::type;
492  using ParticlesToLevelSetT = ParticlesToLevelSet<SdfGridT, AttributeT, InterrupterT>;
493  using SdfT = typename ParticlesToLevelSetT::SdfType; // type of signed distance values
494  using AttT = typename ParticlesToLevelSetT::AttType; // type of particle attribute
495  using ValueT = typename GridT::ValueType;
496  using AccessorT = typename GridT::Accessor;
497  using TreeT = typename GridT::TreeType;
498  using LeafNodeT = typename TreeT::LeafNodeType;
499  using PointPartitionerT = PointPartitioner<Index32, LeafNodeT::LOG2DIM>;
500 
501  static const bool
502  OutputIsMask = std::is_same<SdfT, bool>::value,
503  DoAttrXfer = !DisableT::value;
504 
506  Raster(ParticlesToLevelSetT& parent, GridT* grid, const ParticleListT& particles)
507  : mParent(parent)
508  , mParticles(particles)
509  , mGrid(grid)
510  , mMap(*(mGrid->transform().baseMap()))
511  , mMinCount(0)
512  , mMaxCount(0)
513  , mIsCopy(false)
514  {
515  mPointPartitioner = new PointPartitionerT;
516  mPointPartitioner->construct(particles, mGrid->transform());
517  }
518 
520  Raster(Raster& other, tbb::split)
521  : mParent(other.mParent)
522  , mParticles(other.mParticles)
523  , mGrid(new GridT(*other.mGrid, openvdb::ShallowCopy()))
524  , mMap(other.mMap)
525  , mMinCount(0)
526  , mMaxCount(0)
527  , mTask(other.mTask)
528  , mIsCopy(true)
529  , mPointPartitioner(other.mPointPartitioner)
530  {
531  mGrid->newTree();
532  }
533 
534  virtual ~Raster()
535  {
536  // Copy-constructed Rasters own temporary grids that have to be deleted,
537  // while the original has ownership of the bucket array.
538  if (mIsCopy) {
539  delete mGrid;
540  } else {
541  delete mPointPartitioner;
542  }
543  }
544 
545  void rasterizeSpheres()
546  {
547  mMinCount = mMaxCount = 0;
548  if (mParent.mInterrupter) {
549  mParent.mInterrupter->start("Rasterizing particles to level set using spheres");
550  }
551  mTask = std::bind(&Raster::rasterSpheres, std::placeholders::_1, std::placeholders::_2);
552  this->cook();
553  if (mParent.mInterrupter) mParent.mInterrupter->end();
554  }
555 
556  void rasterizeSpheres(Real radius)
557  {
558  mMinCount = radius < mParent.mRmin ? mParticles.size() : 0;
559  mMaxCount = radius > mParent.mRmax ? mParticles.size() : 0;
560  if (mMinCount>0 || mMaxCount>0) {//skipping all particles!
561  mParent.mMinCount = mMinCount;
562  mParent.mMaxCount = mMaxCount;
563  } else {
564  if (mParent.mInterrupter) {
565  mParent.mInterrupter->start(
566  "Rasterizing particles to level set using const spheres");
567  }
568  mTask = std::bind(&Raster::rasterFixedSpheres,
569  std::placeholders::_1, std::placeholders::_2, radius);
570  this->cook();
571  if (mParent.mInterrupter) mParent.mInterrupter->end();
572  }
573  }
574 
575  void rasterizeTrails(Real delta=1.0)
576  {
577  mMinCount = mMaxCount = 0;
578  if (mParent.mInterrupter) {
579  mParent.mInterrupter->start("Rasterizing particles to level set using trails");
580  }
581  mTask = std::bind(&Raster::rasterTrails,
582  std::placeholders::_1, std::placeholders::_2, delta);
583  this->cook();
584  if (mParent.mInterrupter) mParent.mInterrupter->end();
585  }
586 
588  void operator()(const tbb::blocked_range<size_t>& r)
589  {
590  assert(mTask);
591  mTask(this, r);
592  mParent.mMinCount = mMinCount;
593  mParent.mMaxCount = mMaxCount;
594  }
595 
597  void join(Raster& other)
598  {
600  if (OutputIsMask) {
601  if (DoAttrXfer) {
602  tools::compMax(*mGrid, *other.mGrid);
603  } else {
604  mGrid->topologyUnion(*other.mGrid);
605  }
606  } else {
607  tools::csgUnion(*mGrid, *other.mGrid, /*prune=*/true);
608  }
610  mMinCount += other.mMinCount;
611  mMaxCount += other.mMaxCount;
612  }
613 
614 private:
616  Raster& operator=(const Raster&) { return *this; }
617 
619  bool ignoreParticle(Real R)
620  {
621  if (R < mParent.mRmin) {// below the cutoff radius
622  ++mMinCount;
623  return true;
624  }
625  if (R > mParent.mRmax) {// above the cutoff radius
626  ++mMaxCount;
627  return true;
628  }
629  return false;
630  }
631 
634  void rasterSpheres(const tbb::blocked_range<size_t>& r)
635  {
636  AccessorT acc = mGrid->getAccessor(); // local accessor
637  bool run = true;
638  const Real invDx = 1 / mParent.mDx;
639  AttT att;
640  Vec3R pos;
641  Real rad;
642 
643  // Loop over buckets
644  for (size_t n = r.begin(), N = r.end(); n < N; ++n) {
645  // Loop over particles in bucket n.
646  typename PointPartitionerT::IndexIterator iter = mPointPartitioner->indices(n);
647  for ( ; run && iter; ++iter) {
648  const Index32& id = *iter;
649  mParticles.getPosRad(id, pos, rad);
650  const Real R = invDx * rad;// in voxel units
651  if (this->ignoreParticle(R)) continue;
652  const Vec3R P = mMap.applyInverseMap(pos);
653  this->getAtt<DisableT>(id, att);
654  run = this->makeSphere(P, R, att, acc);
655  }//end loop over particles
656  }//end loop over buckets
657  }
658 
662  void rasterFixedSpheres(const tbb::blocked_range<size_t>& r, Real R)
663  {
664  AccessorT acc = mGrid->getAccessor(); // local accessor
665  AttT att;
666  Vec3R pos;
667 
668  // Loop over buckets
669  for (size_t n = r.begin(), N = r.end(); n < N; ++n) {
670  // Loop over particles in bucket n.
671  for (auto iter = mPointPartitioner->indices(n); iter; ++iter) {
672  const Index32& id = *iter;
673  this->getAtt<DisableT>(id, att);
674  mParticles.getPos(id, pos);
675  const Vec3R P = mMap.applyInverseMap(pos);
676  this->makeSphere(P, R, att, acc);
677  }
678  }
679  }
680 
684  void rasterTrails(const tbb::blocked_range<size_t>& r, Real delta)
685  {
686  AccessorT acc = mGrid->getAccessor(); // local accessor
687  bool run = true;
688  AttT att;
689  Vec3R pos, vel;
690  Real rad;
691  const Vec3R origin = mMap.applyInverseMap(Vec3R(0,0,0));
692  const Real Rmin = mParent.mRmin, invDx = 1 / mParent.mDx;
693 
694  // Loop over buckets
695  for (size_t n = r.begin(), N = r.end(); n < N; ++n) {
696  // Loop over particles in bucket n.
697  typename PointPartitionerT::IndexIterator iter = mPointPartitioner->indices(n);
698  for ( ; run && iter; ++iter) {
699  const Index32& id = *iter;
700  mParticles.getPosRadVel(id, pos, rad, vel);
701  const Real R0 = invDx * rad;
702  if (this->ignoreParticle(R0)) continue;
703  this->getAtt<DisableT>(id, att);
704  const Vec3R P0 = mMap.applyInverseMap(pos);
705  const Vec3R V = mMap.applyInverseMap(vel) - origin; // exclude translation
706  const Real speed = V.length(), invSpeed = 1.0 / speed;
707  const Vec3R Nrml = -V * invSpeed; // inverse normalized direction
708  Vec3R P = P0; // local position of instance
709  Real R = R0, d = 0; // local radius and length of trail
710  for (size_t m = 0; run && d <= speed ; ++m) {
711  run = this->makeSphere(P, R, att, acc);
712  P += 0.5 * delta * R * Nrml; // adaptive offset along inverse velocity direction
713  d = (P - P0).length(); // current length of trail
714  R = R0 - (R0 - Rmin) * d * invSpeed; // R = R0 -> mRmin(e.g. 1.5)
715  }//end loop over sphere instances
716  }//end loop over particles
717  }//end loop over buckets
718  }
719 
720  void cook()
721  {
722  // parallelize over the point buckets
723  const Index32 bucketCount = Index32(mPointPartitioner->size());
724 
725  if (mParent.mGrainSize>0) {
726  tbb::parallel_reduce(
727  tbb::blocked_range<size_t>(0, bucketCount, mParent.mGrainSize), *this);
728  } else {
729  (*this)(tbb::blocked_range<size_t>(0, bucketCount));
730  }
731  }
732 
740  bool makeSphere(const Vec3R& P, Real R, const AttT& att, AccessorT& acc)
741  {
743  if (OutputIsMask) {
744  return makeSphereMask(P, R, att, acc);
745  } else {
746  return makeNarrowBandSphere(P, R, att, acc);
747  }
749  }
750 
765  bool makeNarrowBandSphere(const Vec3R& P, Real R, const AttT& att, AccessorT& acc)
766  {
767  const Real
768  dx = mParent.mDx,
769  w = mParent.mHalfWidth,
770  max = R + w, // maximum distance in voxel units
771  max2 = math::Pow2(max), // square of maximum distance in voxel units
772  min2 = math::Pow2(math::Max(Real(0), R - w)); // square of minimum distance
773  // Bounding box of the sphere
774  const Coord
775  lo(math::Floor(P[0]-max),math::Floor(P[1]-max),math::Floor(P[2]-max)),
776  hi(math::Ceil( P[0]+max),math::Ceil( P[1]+max),math::Ceil( P[2]+max));
777  const ValueT inside = -mGrid->background();
778 
779  ValueT v;
780  size_t count = 0;
781  for (Coord c = lo; c.x() <= hi.x(); ++c.x()) {
782  //only check interrupter every 32'th scan in x
783  if (!(count++ & ((1<<5)-1)) && util::wasInterrupted(mParent.mInterrupter)) {
784  tbb::task::self().cancel_group_execution();
785  return false;
786  }
787  const Real x2 = math::Pow2(c.x() - P[0]);
788  for (c.y() = lo.y(); c.y() <= hi.y(); ++c.y()) {
789  const Real x2y2 = x2 + math::Pow2(c.y() - P[1]);
790  for (c.z() = lo.z(); c.z() <= hi.z(); ++c.z()) {
791  const Real x2y2z2 = x2y2 + math::Pow2(c.z()-P[2]); // squared dist from c to P
792  if (x2y2z2 >= max2 || (!acc.probeValue(c, v) && (v < ValueT(0))))
793  continue;//outside narrow band of the particle or inside existing level set
794  if (x2y2z2 <= min2) {//inside narrow band of the particle.
795  acc.setValueOff(c, inside);
796  continue;
797  }
798  // convert signed distance from voxel units to world units
799  //const ValueT d=dx*(math::Sqrt(x2y2z2) - R);
800  const ValueT d = Merge(static_cast<SdfT>(dx*(math::Sqrt(x2y2z2)-R)), att);
801  if (d < v) acc.setValue(c, d);//CSG union
802  }//end loop over z
803  }//end loop over y
804  }//end loop over x
805  return true;
806  }
807 
810  bool makeSphereMask(const Vec3R& p, Real r, const AttT& att, AccessorT& acc)
811  {
812  const Real
813  rSquared = r * r, // sphere radius squared, in voxel units
814  inW = r / math::Sqrt(6.0); // half the width in voxel units of an inscribed cube
815  const Coord
816  // Bounding box of the sphere
817  outLo(math::Floor(p[0] - r), math::Floor(p[1] - r), math::Floor(p[2] - r)),
818  outHi(math::Ceil(p[0] + r), math::Ceil(p[1] + r), math::Ceil(p[2] + r)),
819  // Bounds of the inscribed cube
820  inLo(math::Ceil(p[0] - inW), math::Ceil(p[1] - inW), math::Ceil(p[2] - inW)),
821  inHi(math::Floor(p[0] + inW), math::Floor(p[1] + inW), math::Floor(p[2] + inW));
822  // Bounding boxes of regions comprising out - in
824  const std::vector<CoordBBox> padding{
825  CoordBBox(outLo.x(), outLo.y(), outLo.z(), inLo.x()-1, outHi.y(), outHi.z()),
826  CoordBBox(inHi.x()+1, outLo.y(), outLo.z(), outHi.x(), outHi.y(), outHi.z()),
827  CoordBBox(outLo.x(), outLo.y(), outLo.z(), outHi.x(), inLo.y()-1, outHi.z()),
828  CoordBBox(outLo.x(), inHi.y()+1, outLo.z(), outHi.x(), outHi.y(), outHi.z()),
829  CoordBBox(outLo.x(), outLo.y(), outLo.z(), outHi.x(), outHi.y(), inLo.z()-1),
830  CoordBBox(outLo.x(), outLo.y(), inHi.z()+1, outHi.x(), outHi.y(), outHi.z()),
831  };
832  const ValueT onValue = Merge(SdfT(1), att);
833 
834  // Sparsely fill the inscribed cube.
836  acc.tree().sparseFill(CoordBBox(inLo, inHi), onValue);
837 
838  // Densely fill the remaining regions.
839  for (const auto& bbox: padding) {
840  if (util::wasInterrupted(mParent.mInterrupter)) {
841  tbb::task::self().cancel_group_execution();
842  return false;
843  }
844  const Coord &bmin = bbox.min(), &bmax = bbox.max();
845  Coord c;
846  Real cx, cy, cz;
847  for (c = bmin, cx = c.x(); c.x() <= bmax.x(); ++c.x(), cx += 1) {
848  const Real x2 = math::Pow2(cx - p[0]);
849  for (c.y() = bmin.y(), cy = c.y(); c.y() <= bmax.y(); ++c.y(), cy += 1) {
850  const Real x2y2 = x2 + math::Pow2(cy - p[1]);
851  for (c.z() = bmin.z(), cz = c.z(); c.z() <= bmax.z(); ++c.z(), cz += 1) {
852  const Real x2y2z2 = x2y2 + math::Pow2(cz - p[2]);
853  if (x2y2z2 < rSquared) {
854  acc.setValue(c, onValue);
855  }
856  }
857  }
858  }
859  }
860  return true;
861  }
862 
863  using FuncType = typename std::function<void (Raster*, const tbb::blocked_range<size_t>&)>;
864 
865  template<typename DisableType>
866  typename std::enable_if<DisableType::value>::type
867  getAtt(size_t, AttT&) const {}
868 
869  template<typename DisableType>
870  typename std::enable_if<!DisableType::value>::type
871  getAtt(size_t n, AttT& a) const { mParticles.getAtt(n, a); }
872 
873  template<typename T>
874  typename std::enable_if<std::is_same<T, ValueT>::value, ValueT>::type
875  Merge(T s, const AttT&) const { return s; }
876 
877  template<typename T>
878  typename std::enable_if<!std::is_same<T, ValueT>::value, ValueT>::type
879  Merge(T s, const AttT& a) const { return ValueT(s,a); }
880 
881  ParticlesToLevelSetT& mParent;
882  const ParticleListT& mParticles;//list of particles
883  GridT* mGrid;
884  const math::MapBase& mMap;
885  size_t mMinCount, mMaxCount;//counters for ignored particles!
886  FuncType mTask;
887  const bool mIsCopy;
888  PointPartitionerT* mPointPartitioner;
889 }; // struct ParticlesToLevelSet::Raster
890 
891 
893 
894 
895 namespace p2ls_internal {
896 
897 // This is a simple type that combines a distance value and a particle
898 // attribute. It's required for attribute transfer which is defined in the
899 // Raster class above.
901 template<typename VisibleT, typename BlindT>
902 class BlindData
903 {
904 public:
905  using type = VisibleT;
906  using VisibleType = VisibleT;
907  using BlindType = BlindT;
908 
909  BlindData() {}
910  explicit BlindData(VisibleT v) : mVisible(v), mBlind(zeroVal<BlindType>()) {}
911  BlindData(VisibleT v, BlindT b) : mVisible(v), mBlind(b) {}
912  BlindData(const BlindData&) = default;
913  BlindData& operator=(const BlindData&) = default;
914  const VisibleT& visible() const { return mVisible; }
915  const BlindT& blind() const { return mBlind; }
917  bool operator==(const BlindData& rhs) const { return mVisible == rhs.mVisible; }
919  bool operator< (const BlindData& rhs) const { return mVisible < rhs.mVisible; }
920  bool operator> (const BlindData& rhs) const { return mVisible > rhs.mVisible; }
921  BlindData operator+(const BlindData& rhs) const { return BlindData(mVisible + rhs.mVisible); }
922  BlindData operator+(const VisibleT& rhs) const { return BlindData(mVisible + rhs); }
923  BlindData operator-(const BlindData& rhs) const { return BlindData(mVisible - rhs.mVisible); }
924  BlindData operator-() const { return BlindData(-mVisible, mBlind); }
925 
926 protected:
927  VisibleT mVisible;
928  BlindT mBlind;
929 };
930 
932 // Required by several of the tree nodes
933 template<typename VisibleT, typename BlindT>
934 inline std::ostream& operator<<(std::ostream& ostr, const BlindData<VisibleT, BlindT>& rhs)
935 {
936  ostr << rhs.visible();
937  return ostr;
938 }
939 
941 // Required by math::Abs
942 template<typename VisibleT, typename BlindT>
943 inline BlindData<VisibleT, BlindT> Abs(const BlindData<VisibleT, BlindT>& x)
944 {
945  return BlindData<VisibleT, BlindT>(math::Abs(x.visible()), x.blind());
946 }
947 
948 } // namespace p2ls_internal
949 
950 
952 
953 
954 // The following are convenience functions for common use cases.
955 
956 template<typename GridT, typename ParticleListT, typename InterrupterT>
957 inline void
958 particlesToSdf(const ParticleListT& plist, GridT& grid, InterrupterT* interrupt)
959 {
960  static_assert(std::is_floating_point<typename GridT::ValueType>::value,
961  "particlesToSdf requires an SDF grid with floating-point values");
962 
963  if (grid.getGridClass() != GRID_LEVEL_SET) {
964  OPENVDB_LOG_WARN("particlesToSdf requires a level set grid;"
965  " try Grid::setGridClass(openvdb::GRID_LEVEL_SET)");
966  }
967 
968  ParticlesToLevelSet<GridT> p2ls(grid, interrupt);
969  p2ls.rasterizeSpheres(plist);
970  tools::pruneLevelSet(grid.tree());
971 }
972 
973 template<typename GridT, typename ParticleListT, typename InterrupterT>
974 inline void
975 particlesToSdf(const ParticleListT& plist, GridT& grid, Real radius, InterrupterT* interrupt)
976 {
977  static_assert(std::is_floating_point<typename GridT::ValueType>::value,
978  "particlesToSdf requires an SDF grid with floating-point values");
979 
980  if (grid.getGridClass() != GRID_LEVEL_SET) {
981  OPENVDB_LOG_WARN("particlesToSdf requires a level set grid;"
982  " try Grid::setGridClass(openvdb::GRID_LEVEL_SET)");
983  }
984 
985  ParticlesToLevelSet<GridT> p2ls(grid, interrupt);
986  p2ls.rasterizeSpheres(plist, radius);
987  tools::pruneLevelSet(grid.tree());
988 }
989 
990 template<typename GridT, typename ParticleListT, typename InterrupterT>
991 inline void
992 particleTrailsToSdf(const ParticleListT& plist, GridT& grid, Real delta, InterrupterT* interrupt)
993 {
994  static_assert(std::is_floating_point<typename GridT::ValueType>::value,
995  "particleTrailsToSdf requires an SDF grid with floating-point values");
996 
997  if (grid.getGridClass() != GRID_LEVEL_SET) {
998  OPENVDB_LOG_WARN("particlesToSdf requires a level set grid;"
999  " try Grid::setGridClass(openvdb::GRID_LEVEL_SET)");
1000  }
1001 
1002  ParticlesToLevelSet<GridT> p2ls(grid, interrupt);
1003  p2ls.rasterizeTrails(plist, delta);
1004  tools::pruneLevelSet(grid.tree());
1005 }
1006 
1007 template<typename GridT, typename ParticleListT, typename InterrupterT>
1008 inline void
1009 particlesToMask(const ParticleListT& plist, GridT& grid, InterrupterT* interrupt)
1010 {
1011  static_assert(std::is_same<bool, typename GridT::ValueType>::value,
1012  "particlesToMask requires a boolean-valued grid");
1013  ParticlesToLevelSet<GridT> p2ls(grid, interrupt);
1014  p2ls.rasterizeSpheres(plist);
1015  tools::prune(grid.tree());
1016 }
1017 
1018 template<typename GridT, typename ParticleListT, typename InterrupterT>
1019 inline void
1020 particlesToMask(const ParticleListT& plist, GridT& grid, Real radius, InterrupterT* interrupt)
1021 {
1022  static_assert(std::is_same<bool, typename GridT::ValueType>::value,
1023  "particlesToMask requires a boolean-valued grid");
1024  ParticlesToLevelSet<GridT> p2ls(grid, interrupt);
1025  p2ls.rasterizeSpheres(plist, radius);
1026  tools::prune(grid.tree());
1027 }
1028 
1029 template<typename GridT, typename ParticleListT, typename InterrupterT>
1030 inline void
1031 particleTrailsToMask(const ParticleListT& plist, GridT& grid, Real delta, InterrupterT* interrupt)
1032 {
1033  static_assert(std::is_same<bool, typename GridT::ValueType>::value,
1034  "particleTrailsToMask requires a boolean-valued grid");
1035  ParticlesToLevelSet<GridT> p2ls(grid, interrupt);
1036  p2ls.rasterizeTrails(plist, delta);
1037  tools::prune(grid.tree());
1038 }
1039 
1040 } // namespace tools
1041 } // namespace OPENVDB_VERSION_NAME
1042 } // namespace openvdb
1043 
1044 #endif // OPENVDB_TOOLS_PARTICLES_TO_LEVELSET_HAS_BEEN_INCLUDED
1045 
1046 // Copyright (c) 2012-2018 DreamWorks Animation LLC
1047 // All rights reserved. This software is distributed under the
1048 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition: Metadata.h:401
void pruneLevelSet(TreeT &tree, bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing nodes whose values are all inactive with inactive ...
Definition: Prune.h:416
void particlesToSdf(const ParticleListT &, GridT &, InterrupterT *=nullptr)
Populate a scalar, floating-point grid with CSG-unioned level set spheres described by the given part...
Definition: ParticlesToLevelSet.h:958
math::Vec3< Real > Vec3R
Definition: Types.h:79
Definition: Exceptions.h:90
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:810
typename SdfGridT::ValueType SdfType
Definition: ParticlesToLevelSet.h:187
Index32 Index
Definition: Types.h:61
Real getVoxelSize() const
Return the size of a voxel in world units.
Definition: ParticlesToLevelSet.h:232
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
size_t getMaxCount() const
Return the number of particles that were ignored because they were larger than the maximum radius.
Definition: ParticlesToLevelSet.h:254
Definition: PointPartitioner.h:106
void setRmin(Real Rmin)
Set the smallest radius allowed in voxel units.
Definition: ParticlesToLevelSet.h:240
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:715
Functions to efficiently perform various compositing operations on grids.
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
void csgUnion(GridOrTreeT &a, GridOrTreeT &b, bool prune=true)
Given two level set grids, replace the A grid with the union of A and B.
Definition: Composite.h:1145
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:518
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
void particleTrailsToSdf(const ParticleListT &, GridT &, Real delta=1, InterrupterT *=nullptr)
Populate a scalar, floating-point grid with CSG-unioned trails of level set spheres with decreasing r...
Definition: ParticlesToLevelSet.h:992
void compMax(GridOrTreeT &a, GridOrTreeT &b)
Given grids A and B, compute max(a, b) per voxel (using sparse traversal). Store the result in the A ...
Definition: Composite.h:748
typename SdfGridT::template ValueConverter< AttType >::Type AttGridType
Definition: ParticlesToLevelSet.h:190
void setGrainSize(int grainSize)
Set the grain size used for threading.
Definition: ParticlesToLevelSet.h:260
bool ignoredParticles() const
Return true if any particles were ignored due to their size.
Definition: ParticlesToLevelSet.h:248
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
static const bool OutputIsMask
Definition: ParticlesToLevelSet.h:192
int Floor(float x)
Return the floor of x.
Definition: Math.h:802
Vec3< typename promote< T, Coord::ValueType >::type > operator-(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be subtracted from a Vec3.
Definition: Coord.h:549
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:86
void signedFloodFill(TreeOrLeafManagerT &tree, bool threaded=true, size_t grainSize=1, Index minLevel=0)
Set the values of all inactive voxels and tiles of a narrow-band level set from the signs of the acti...
Definition: SignedFloodFill.h:294
bool operator<(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:207
Tag dispatch class that distinguishes shallow copy constructors from deep copy constructors.
Definition: Types.h:515
Spatially partitions points using a parallel radix-based sorting algorithm.
Defined various multi-threaded utility functions for trees.
Definition: Types.h:277
void particlesToMask(const ParticleListT &, GridT &, Real radius, InterrupterT *=nullptr)
Activate a boolean grid wherever it intersects the fixed-size spheres described by the given particle...
Definition: ParticlesToLevelSet.h:1020
uint32_t Index32
Definition: Types.h:59
typename std::is_void< AttributeT >::type DisableT
Definition: ParticlesToLevelSet.h:183
std::string operator+(const std::string &s, bool)
Needed to support the (zeroVal<ValueType>() + val) idiom when ValueType is std::string.
Definition: Math.h:96
void particlesToMask(const ParticleListT &, GridT &, InterrupterT *=nullptr)
Activate a boolean grid wherever it intersects the spheres described by the given particle positions ...
Definition: ParticlesToLevelSet.h:1009
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:488
void particleTrailsToMask(const ParticleListT &, GridT &, Real delta=1, InterrupterT *=nullptr)
Activate a boolean grid wherever it intersects trails of spheres with decreasing radius,...
Definition: ParticlesToLevelSet.h:1031
typename std::conditional< DisableT::value, size_t, AttributeT >::type AttType
Definition: ParticlesToLevelSet.h:189
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
Real getHalfWidth() const
Return the half-width of the narrow band in voxel units.
Definition: ParticlesToLevelSet.h:235
Real getRmax() const
Return the largest radius allowed in voxel units.
Definition: ParticlesToLevelSet.h:243
AttGridType::Ptr attributeGrid()
Return a pointer to the grid containing the optional user-defined attribute.
Definition: ParticlesToLevelSet.h:229
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form 'someVar << "some text" << ...'.
Definition: logging.h:280
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
void finalize(bool prune=false)
This method syncs up the level set and attribute grids and therefore needs to be called before any of...
Definition: ParticlesToLevelSet.h:380
void rasterizeTrails(const ParticleListT &pa, Real delta=1.0)
Rasterize each particle as a trail comprising the CSG union of spheres of decreasing radius.
Definition: ParticlesToLevelSet.h:366
void particlesToSdf(const ParticleListT &, GridT &, Real radius, InterrupterT *=nullptr)
Populate a scalar, floating-point grid with fixed-size, CSG-unioned level set spheres described by th...
Definition: ParticlesToLevelSet.h:975
Real getRmin() const
Return the smallest radius allowed in voxel units.
Definition: ParticlesToLevelSet.h:238
Definition: Exceptions.h:40
size_t getMinCount() const
Return the number of particles that were ignored because they were smaller than the minimum radius.
Definition: ParticlesToLevelSet.h:251
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition: Platform.h:129
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:75
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:110
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition: Math.h:549
void setRmax(Real Rmax)
Set the largest radius allowed in voxel units.
Definition: ParticlesToLevelSet.h:245
InterrupterT InterrupterType
Definition: ParticlesToLevelSet.h:184
Dummy NOOP interrupter class defining interface.
Definition: NullInterrupter.h:52
void rasterizeSpheres(const ParticleListT &pa)
Rasterize each particle as a sphere with the particle's position and radius.
Definition: ParticlesToLevelSet.h:338
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:74
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:130
Coord Abs(const Coord &xyz)
Definition: Coord.h:513
int getGrainSize() const
Return the grain size used for threading.
Definition: ParticlesToLevelSet.h:257
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:219
#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...
Definition: ParticlesToLevelSet.h:180
SdfGridT SdfGridType
Definition: ParticlesToLevelSet.h:186
void prune(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any nodes whose values are all the same...
Definition: Prune.h:361
~ParticlesToLevelSet()
Definition: ParticlesToLevelSet.h:214
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:524
double Real
Definition: Types.h:67
Type Pow2(Type x)
Return x2.
Definition: Math.h:502