8 #ifndef OPENVDB_TOOLS_LEVELSETMEASURE_HAS_BEEN_INCLUDED
9 #define OPENVDB_TOOLS_LEVELSETMEASURE_HAS_BEEN_INCLUDED
23 #include <tbb/parallel_for.h>
24 #include <tbb/parallel_sort.h>
25 #include <tbb/parallel_invoke.h>
27 #include <type_traits>
42 template<
class Gr
idType>
54 template<
class Gr
idType>
64 template<
class Gr
idType>
75 template<
class Gr
idType>
82 template<
typename RealT>
87 DiracDelta(RealT eps) : mC(0.5/eps), mD(2*math::
pi<RealT>()*mC), mE(eps) {}
91 const RealT mC, mD, mE;
102 template<
typename Gr
idT,
typename InterruptT = util::NullInterrupter>
112 "level set measure is supported only for scalar, floating-point grids");
138 Real area(
bool useWorldUnits =
true);
176 using LeafT =
typename TreeType::LeafNodeType;
177 using VoxelCIterT =
typename LeafT::ValueOnCIter;
178 using LeafRange =
typename ManagerType::LeafRange;
179 using LeafIterT =
typename LeafRange::Iterator;
180 using ManagerPtr = std::unique_ptr<ManagerType>;
181 using BufferPtr = std::unique_ptr<double[]>;
190 InterruptT *mInterrupter;
191 double mDx, mArea, mVolume, mTotMeanCurvature, mTotGausCurvature;
193 bool mUpdateArea, mUpdateCurvature;
196 bool checkInterrupter();
200 MeasureArea(
LevelSetMeasure* parent) : mParent(parent), mStencil(*mParent->mGrid)
202 if (parent->mInterrupter) parent->mInterrupter->start(
"Measuring area and volume of level set");
203 if (parent->mGrainSize>0) {
206 (*this)(parent->mLeafs->leafRange());
208 tbb::parallel_invoke([&](){parent->mArea = parent->reduce(0);},
209 [&](){parent->mVolume = parent->reduce(1)/3.0;});
210 parent->mUpdateArea =
false;
211 if (parent->mInterrupter) parent->mInterrupter->end();
213 MeasureArea(
const MeasureArea& other) : mParent(other.mParent), mStencil(*mParent->mGrid) {}
214 void operator()(
const LeafRange&
range)
const;
216 mutable math::GradStencil<GridT, false> mStencil;
219 struct MeasureCurvatures
221 MeasureCurvatures(
LevelSetMeasure* parent) : mParent(parent), mStencil(*mParent->mGrid)
223 if (parent->mInterrupter) parent->mInterrupter->start(
"Measuring curvatures of level set");
224 if (parent->mGrainSize>0) {
227 (*this)(parent->mLeafs->leafRange());
229 tbb::parallel_invoke([&](){parent->mTotMeanCurvature = parent->reduce(0);},
230 [&](){parent->mTotGausCurvature = parent->reduce(1);});
231 parent->mUpdateCurvature =
false;
232 if (parent->mInterrupter) parent->mInterrupter->end();
234 MeasureCurvatures(
const MeasureCurvatures& other) : mParent(other.mParent), mStencil(*mParent->mGrid) {}
235 void operator()(
const LeafRange&
range)
const;
237 mutable math::CurvatureStencil<GridT, false> mStencil;
242 double *
first = mBuffer.get() + offset*mLeafs->leafCount(), *
last = first + mLeafs->leafCount();
243 tbb::parallel_sort(first,
last);
245 while(first !=
last) sum += *first++;
252 template<
typename Gr
idT,
typename InterruptT>
255 : mInterrupter(interrupt)
261 template<
typename Gr
idT,
typename InterruptT>
265 if (!grid.hasUniformVoxels()) {
267 "The transform must have uniform scale for the LevelSetMeasure to function");
271 "LevelSetMeasure only supports level sets;"
272 " try setting the grid class to \"level set\"");
276 "LevelSetMeasure does not support empty grids;");
279 mDx = grid.voxelSize()[0];
280 mLeafs = std::make_unique<ManagerType>(mGrid->tree());
281 mBuffer = std::make_unique<double[]>(2*mLeafs->leafCount());
282 mUpdateArea = mUpdateCurvature =
true;
285 template<
typename Gr
idT,
typename InterruptT>
289 if (mUpdateArea) {MeasureArea m(
this);};
295 template<
typename Gr
idT,
typename InterruptT>
299 if (mUpdateArea) {MeasureArea m(
this);};
300 double volume = mVolume;
301 if (useWorldUnits) volume *=
math::Pow3(mDx) ;
305 template<
typename Gr
idT,
typename InterruptT>
309 if (mUpdateCurvature) {MeasureCurvatures m(
this);};
310 return mTotMeanCurvature * (useWorldUnits ? mDx : 1);
313 template<
typename Gr
idT,
typename InterruptT>
317 if (mUpdateCurvature) {MeasureCurvatures m(
this);};
318 return mTotGausCurvature;
321 template<
typename Gr
idT,
typename InterruptT>
325 const Real x = this->totGaussianCurvature(
true) / (2.0*math::pi<Real>());
331 template<
typename Gr
idT,
typename InterruptT>
336 thread::cancelGroupExecution();
342 template<
typename Gr
idT,
typename InterruptT>
344 LevelSetMeasure<GridT, InterruptT>::
345 MeasureArea::operator()(
const LeafRange&
range)
const
347 using Vec3T = math::Vec3<ValueType>;
349 mParent->checkInterrupter();
350 const Real invDx = 1.0/mParent->mDx;
351 const DiracDelta<Real> DD(1.5);
352 const size_t leafCount = mParent->mLeafs->leafCount();
353 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
354 Real sumA = 0, sumV = 0;
355 for (VoxelCIterT voxelIter = leafIter->cbeginValueOn(); voxelIter; ++voxelIter) {
356 const Real dd = DD(invDx * (*voxelIter));
358 mStencil.moveTo(voxelIter);
359 const Coord& p = mStencil.getCenterCoord();
360 const Vec3T
g = mStencil.gradient();
361 sumA += dd*g.length();
362 sumV += dd*(g[0]*
Real(p[0]) + g[1]*
Real(p[1]) + g[2]*
Real(p[2]));
365 double*
ptr = mParent->mBuffer.get() + leafIter.pos();
372 template<
typename Gr
idT,
typename InterruptT>
374 LevelSetMeasure<GridT, InterruptT>::
375 MeasureCurvatures::operator()(
const LeafRange& range)
const
377 using Vec3T = math::Vec3<ValueType>;
379 mParent->checkInterrupter();
380 const Real dx = mParent->mDx, dx2=dx*dx, invDx = 1.0/dx;
381 const DiracDelta<Real> DD(1.5);
383 const size_t leafCount = mParent->mLeafs->leafCount();
384 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
385 Real sumM = 0, sumG = 0;
386 for (VoxelCIterT voxelIter = leafIter->cbeginValueOn(); voxelIter; ++voxelIter) {
387 const Real dd = DD(invDx * (*voxelIter));
389 mStencil.moveTo(voxelIter);
390 const Vec3T g = mStencil.gradient();
391 const Real dA = dd*g.length();
392 mStencil.curvatures(mean, gauss);
394 sumG += dA*gauss*dx2;
397 double* ptr = mParent->mBuffer.get() + leafIter.pos();
409 template<
class Gr
idT>
412 doLevelSetArea(
const GridT& grid,
bool useWorldUnits)
414 LevelSetMeasure<GridT> m(grid);
415 return m.area(useWorldUnits);
418 template<
class Gr
idT>
421 doLevelSetArea(
const GridT&,
bool)
424 "level set area is supported only for scalar, floating-point grids");
430 template<
class Gr
idT>
434 return doLevelSetArea<GridT>(grid, useWorldUnits);
442 template<
class Gr
idT>
445 doLevelSetVolume(
const GridT& grid,
bool useWorldUnits)
447 LevelSetMeasure<GridT> m(grid);
448 return m.volume(useWorldUnits);
451 template<
class Gr
idT>
454 doLevelSetVolume(
const GridT&,
bool)
457 "level set volume is supported only for scalar, floating-point grids");
463 template<
class Gr
idT>
467 return doLevelSetVolume<GridT>(grid, useWorldUnits);
475 template<
class Gr
idT>
478 doLevelSetEulerCharacteristic(
const GridT& grid)
480 LevelSetMeasure<GridT> m(grid);
481 return m.eulerCharacteristic();
484 template<
class Gr
idT>
487 doLevelSetEulerCharacteristic(
const GridT&)
490 "level set euler characteristic is supported only for scalar, floating-point grids");
497 template<
class Gr
idT>
501 return doLevelSetEulerCharacteristic(grid);
509 template<
class Gr
idT>
512 doLevelSetEuler(
const GridT& grid)
514 LevelSetMeasure<GridT> m(grid);
515 return m.eulerCharacteristics();
519 template<
class Gr
idT>
522 doLevelSetGenus(
const GridT& grid)
524 LevelSetMeasure<GridT> m(grid);
528 template<
class Gr
idT>
531 doLevelSetGenus(
const GridT&)
534 "level set genus is supported only for scalar, floating-point grids");
540 template<
class Gr
idT>
544 return doLevelSetGenus(grid);
553 #ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION
555 #ifdef OPENVDB_INSTANTIATE_LEVELSETMEASURE
559 #define _FUNCTION(TreeT) \
560 Real levelSetArea(const Grid<TreeT>&, bool)
564 #define _FUNCTION(TreeT) \
565 Real levelSetVolume(const Grid<TreeT>&, bool)
569 #define _FUNCTION(TreeT) \
570 int levelSetEulerCharacteristic(const Grid<TreeT>&)
574 #define _FUNCTION(TreeT) \
575 int levelSetGenus(const Grid<TreeT>&)
582 #endif // OPENVDB_USE_EXPLICIT_INSTANTIATION
589 #endif // OPENVDB_TOOLS_LEVELSETMEASURE_HAS_BEEN_INCLUDED
void parallel_for(int64_t start, int64_t end, std::function< void(int64_t index)> &&task, parallel_options opt=parallel_options(0, Split_Y, 1))
SYS_API double cos(double x)
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
Type Pow2(Type x)
Return x2.
GridType
List of types that are currently supported by NanoVDB.
GLsizei const GLfloat * value
#define OPENVDB_REAL_TREE_INSTANTIATE(Function)
#define OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_INSTANTIATE_CLASS
Coord Abs(const Coord &xyz)
ValueAccessors are designed to help accelerate accesses into the OpenVDB Tree structures by storing c...
float Round(float x)
Return x rounded to the nearest integer.
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Type Pow3(Type x)
Return x3.
__hostdev__ uint64_t last(uint32_t i) const
__hostdev__ constexpr T pi()
Pi constant taken from Boost to match old behaviour.
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
bool wasInterrupted(T *i, int percent=-1)
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
#define OPENVDB_THROW(exception, message)
GA_API const UT_StringHolder area