12 #ifndef OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
13 #define OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
20 #include <type_traits>
22 #include <tbb/enumerable_thread_specific.h>
23 #include <tbb/parallel_for.h>
24 #include <tbb/parallel_reduce.h>
25 #include <tbb/blocked_range.h>
47 template<
typename Gr
idType,
typename InterruptT>
48 typename GridType::Ptr
50 float halfWidth =
float(LEVEL_SET_HALF_WIDTH),
51 InterruptT* interrupt =
nullptr,
bool threaded =
true);
66 template<
typename Gr
idType>
67 typename GridType::Ptr
69 float halfWidth =
float(LEVEL_SET_HALF_WIDTH),
bool threaded =
true)
71 return createLevelSetSphere<GridType, util::NullInterrupter>(radius,
center,voxelSize,halfWidth,
nullptr,threaded);
84 template<
typename Gr
idT,
typename InterruptT = util::NullInterrupter>
88 using TreeT =
typename GridT::TreeType;
89 using ValueT =
typename GridT::ValueType;
92 "level set grids must have scalar, floating-point value types");
105 : mRadius(radius), mCenter(center), mInterrupt(interrupt)
107 if (mRadius<=0)
OPENVDB_THROW(ValueError,
"radius must be positive");
117 mGrid = createLevelSet<GridT>(voxelSize, halfWidth);
118 this->rasterSphere(voxelSize, halfWidth, threaded);
126 if (!(dx>0.0
f))
OPENVDB_THROW(ValueError,
"voxel size must be positive");
127 if (!(w>1))
OPENVDB_THROW(ValueError,
"half-width must be larger than one");
130 const ValueT r0 = mRadius/dx, rmax = r0 +
w;
133 if (r0 < 1.5
f)
return;
136 const Vec3T c(mCenter[0]/dx, mCenter[1]/dx, mCenter[2]/dx);
144 typename GridT::Accessor accessor = mGrid->getAccessor();
146 if (mInterrupt) mInterrupt->start(
"Generating level set of sphere");
148 tbb::enumerable_thread_specific<TreeT>
pool(mGrid->tree());
150 auto kernel = [&](
const tbb::blocked_range<int>&
r) {
152 int &i = ijk[0], &j = ijk[1], &k = ijk[2],
m=1;
154 typename GridT::Accessor acc(tree);
156 for (i =
r.begin(); i <=
r.end(); ++i) {
159 for (j = jmin; j <= jmax; ++j) {
161 for (k = kmin; k <= kmax; k +=
m) {
167 acc.setValue(ijk, dx*
v);
182 using RangeT = tbb::blocked_range<typename tbb::enumerable_thread_specific<TreeT>::iterator>;
186 Op(
TreeT &tree) : mDelete(false), mTree(&tree) {}
187 Op(
const Op& other,
tbb::split) : mDelete(true), mTree(new
TreeT(other.mTree->background())) {}
188 ~
Op() {
if (mDelete)
delete mTree; }
189 void operator()(RangeT &
r) {
for (
auto i=r.begin(); i!=r.end(); ++i) this->merge(*i);}
190 void join(
Op &other) { this->merge(*(other.mTree)); }
192 } op( mGrid->tree() );
193 tbb::parallel_reduce(RangeT(
pool.begin(),
pool.end(), 4), op);
195 kernel(tbb::blocked_range<int>(imin, imax));
202 if (mInterrupt) mInterrupt->end();
207 InterruptT* mInterrupt;
208 typename GridT::Ptr mGrid;
215 template<
typename Gr
idType,
typename InterruptT>
216 typename GridType::Ptr
218 float halfWidth, InterruptT* interrupt,
bool threaded)
222 "level set grids must have scalar, floating-point value types");
224 using ValueT =
typename GridType::ValueType;
226 return factory.
getLevelSet(ValueT(voxelSize), ValueT(halfWidth), threaded);
233 #endif // OPENVDB_TOOLS_LEVELSETSPHERE_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))
int Ceil(float x)
Return the ceiling of x.
Type Pow2(Type x)
Return x2.
#define OPENVDB_USE_VERSION_NAMESPACE
GLubyte GLubyte GLubyte GLubyte w
void OIIO_API split(string_view str, std::vector< string_view > &result, string_view sep=string_view(), int maxsplit=-1)
Coord Abs(const Coord &xyz)
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
float Sqrt(float x)
Return the square root of a floating-point value.
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
GLsizei const GLfloat * value
int Floor(float x)
Return the floor of x.
bool wasInterrupted(T *i, int percent=-1)
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
#define OPENVDB_THROW(exception, message)
**Note that the tasks the is the thread number *for the pool
math::Vec3< float > Vec3f