12#ifndef OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
13#define OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
23#include <tbb/enumerable_thread_specific.h>
24#include <tbb/parallel_for.h>
25#include <tbb/parallel_reduce.h>
26#include <tbb/blocked_range.h>
48template<
typename Gr
idType,
typename InterruptT>
52 InterruptT* interrupt =
nullptr,
bool threaded =
true);
67template<
typename Gr
idType>
72 return createLevelSetSphere<GridType, util::NullInterrupter>(radius,center,voxelSize,halfWidth,
nullptr,threaded);
85template<
typename Gr
idT,
typename InterruptT = util::NullInterrupter>
89 using TreeT =
typename GridT::TreeType;
90 using ValueT =
typename GridT::ValueType;
93 "level set grids must have scalar, floating-point value types");
106 : mRadius(radius), mCenter(center), mInterrupt(interrupt)
118 mGrid = createLevelSet<GridT>(voxelSize, halfWidth);
119 this->rasterSphere(voxelSize, halfWidth, threaded);
125 void rasterSphere(ValueT dx, ValueT w,
bool threaded)
131 const ValueT r0 = mRadius/dx, rmax = r0 + w;
134 if (r0 < 1.5f)
return;
137 const Vec3T c(mCenter[0]/dx, mCenter[1]/dx, mCenter[2]/dx);
145 typename GridT::Accessor accessor = mGrid->getAccessor();
147 if (mInterrupt) mInterrupt->start(
"Generating level set of sphere");
149 tbb::enumerable_thread_specific<TreeT> pool(mGrid->tree());
151 auto kernel = [&](
const tbb::blocked_range<int>& r) {
153 int &i = ijk[0], &j = ijk[1], &k = ijk[2], m=1;
154 TreeT &tree = pool.local();
155 typename GridT::Accessor acc(tree);
157 for (i = r.begin(); i <= r.end(); ++i) {
160 for (j = jmin; j <= jmax; ++j) {
161 const auto x2y2 =
math::Pow2(ValueT(j) - c[1]) + x2;
162 for (k = kmin; k <= kmax; k += m) {
168 acc.setValue(ijk, dx*v);
182 tbb::parallel_for(tbb::blocked_range<int>(imin, imax, 128), kernel);
183 using RangeT = tbb::blocked_range<typename tbb::enumerable_thread_specific<TreeT>::iterator>;
187 Op(TreeT &tree) : mDelete(false), mTree(&tree) {}
188 Op(
const Op& other, tbb::split) : mDelete(true), mTree(new TreeT(other.mTree->background())) {}
189 ~Op() {
if (mDelete)
delete mTree; }
190 void operator()(RangeT &r) {
for (
auto i=r.begin(); i!=r.end(); ++i) this->merge(*i);}
191 void join(Op &other) { this->merge(*(other.mTree)); }
193 } op( mGrid->tree() );
194 tbb::parallel_reduce(RangeT(pool.begin(), pool.end(), 4), op);
196 kernel(tbb::blocked_range<int>(imin, imax));
203 if (mInterrupt) mInterrupt->end();
206 const ValueT mRadius;
208 InterruptT* mInterrupt;
209 typename GridT::Ptr mGrid;
216template<
typename Gr
idType,
typename InterruptT>
217typename GridType::Ptr
219 float halfWidth, InterruptT* interrupt,
bool threaded)
223 "level set grids must have scalar, floating-point value types");
225 using ValueT =
typename GridType::ValueType;
227 return factory.
getLevelSet(ValueT(voxelSize), ValueT(halfWidth), threaded);
236#ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION
238#ifdef OPENVDB_INSTANTIATE_LEVELSETSPHERE
242#define _FUNCTION(TreeT) \
243 Grid<TreeT>::Ptr createLevelSetSphere<Grid<TreeT>>(float, const openvdb::Vec3f&, float, float, \
244 util::NullInterrupter*, bool)
ValueT value
Definition: GridBuilder.h:1287
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
Definition: Exceptions.h:65
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:25
int32_t Floor(float x)
Definition: NanoVDB.h:711
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: NanoVDB.h:795
T Abs(T x)
Definition: NanoVDB.h:747
int32_t Ceil(float x)
Definition: NanoVDB.h:720
T Pow2(T x)
Definition: NanoVDB.h:730
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:49
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:343
@ GRID_LEVEL_SET
Definition: Types.h:337
@ MERGE_ACTIVE_STATES
Definition: Types.h:389
Definition: Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
#define OPENVDB_REAL_TREE_INSTANTIATE(Function)
Definition: version.h.in:147