OpenVDB  6.2.0
LevelSetSphere.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 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 //
38 
39 #ifndef OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
40 #define OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
41 
42 #include <openvdb/Grid.h>
43 #include <openvdb/Types.h>
44 #include <openvdb/math/Math.h>
46 #include "SignedFloodFill.h"
47 #include <type_traits>
48 
49 #include <tbb/enumerable_thread_specific.h>
50 #include <tbb/parallel_for.h>
51 #include <tbb/parallel_reduce.h>
52 #include <tbb/blocked_range.h>
53 #include <thread>
54 
55 namespace openvdb {
57 namespace OPENVDB_VERSION_NAME {
58 namespace tools {
59 
74 template<typename GridType, typename InterruptT>
75 typename GridType::Ptr
76 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
77  float halfWidth = float(LEVEL_SET_HALF_WIDTH),
78  InterruptT* interrupt = nullptr, bool threaded = true);
79 
93 template<typename GridType>
94 typename GridType::Ptr
95 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
96  float halfWidth = float(LEVEL_SET_HALF_WIDTH), bool threaded = true)
97 {
98  return createLevelSetSphere<GridType, util::NullInterrupter>(radius,center,voxelSize,halfWidth,nullptr,threaded);
99 }
100 
101 
103 
104 
111 template<typename GridT, typename InterruptT = util::NullInterrupter>
113 {
114 public:
115  using TreeT = typename GridT::TreeType;
116  using ValueT = typename GridT::ValueType;
117  using Vec3T = typename math::Vec3<ValueT>;
118  static_assert(std::is_floating_point<ValueT>::value,
119  "level set grids must have scalar, floating-point value types");
120 
131  LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT* interrupt = nullptr)
132  : mRadius(radius), mCenter(center), mInterrupt(interrupt)
133  {
134  if (mRadius<=0) OPENVDB_THROW(ValueError, "radius must be positive");
135  }
136 
142  typename GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth, bool threaded = true)
143  {
144  mGrid = createLevelSet<GridT>(voxelSize, halfWidth);
145  this->rasterSphere(voxelSize, halfWidth, threaded);
146  mGrid->setGridClass(GRID_LEVEL_SET);
147  return mGrid;
148  }
149 
150 private:
151  void rasterSphere(ValueT dx, ValueT w, bool threaded)
152  {
153  if (!(dx>0.0f)) OPENVDB_THROW(ValueError, "voxel size must be positive");
154  if (!(w>1)) OPENVDB_THROW(ValueError, "half-width must be larger than one");
155 
156  // Define radius of sphere and narrow-band in voxel units
157  const ValueT r0 = mRadius/dx, rmax = r0 + w;
158 
159  // Radius below the Nyquist frequency
160  if (r0 < 1.5f) return;
161 
162  // Define center of sphere in voxel units
163  const Vec3T c(mCenter[0]/dx, mCenter[1]/dx, mCenter[2]/dx);
164 
165  // Define bounds of the voxel coordinates
166  const int imin=math::Floor(c[0]-rmax), imax=math::Ceil(c[0]+rmax);
167  const int jmin=math::Floor(c[1]-rmax), jmax=math::Ceil(c[1]+rmax);
168  const int kmin=math::Floor(c[2]-rmax), kmax=math::Ceil(c[2]+rmax);
169 
170  // Allocate a ValueAccessor for accelerated random access
171  typename GridT::Accessor accessor = mGrid->getAccessor();
172 
173  if (mInterrupt) mInterrupt->start("Generating level set of sphere");
174 
175  tbb::enumerable_thread_specific<TreeT> pool(mGrid->tree());
176 
177  auto kernel = [&](const tbb::blocked_range<int>& r) {
178  openvdb::Coord ijk;
179  int &i = ijk[0], &j = ijk[1], &k = ijk[2], m=1;
180  TreeT &tree = pool.local();
181  typename GridT::Accessor acc(tree);
182  // Compute signed distances to sphere using leapfrogging in k
183  for (i = r.begin(); i <= r.end(); ++i) {
184  if (util::wasInterrupted(mInterrupt)) return;
185  const auto x2 = math::Pow2(ValueT(i) - c[0]);
186  for (j = jmin; j <= jmax; ++j) {
187  const auto x2y2 = math::Pow2(ValueT(j) - c[1]) + x2;
188  for (k = kmin; k <= kmax; k += m) {
189  m = 1;
190  // Distance in voxel units to sphere
191  const auto v = math::Sqrt(x2y2 + math::Pow2(ValueT(k)-c[2]))-r0;
192  const auto d = math::Abs(v);
193  if (d < w) { // inside narrow band
194  acc.setValue(ijk, dx*v);// distance in world units
195  } else { // outside narrow band
196  m += math::Floor(d-w);// leapfrog
197  }
198  }//end leapfrog over k
199  }//end loop over j
200  }//end loop over i
201  };// kernel
202 
203  if (threaded) {
204  // The code blow is making use of a TLS container to minimize the number of concurrent trees
205  // initially populated by tbb::parallel_for and subsequently merged by tbb::parallel_reduce.
206  // Experiments have demonstrated this approach to outperform others, including serial reduction
207  // and a custom concurrent reduction implementation.
208  tbb::parallel_for(tbb::blocked_range<int>(imin, imax, 128), kernel);
209  using RangeT = tbb::blocked_range<typename tbb::enumerable_thread_specific<TreeT>::iterator>;
210  struct Op {
211  const bool mDelete;
212  TreeT *mTree;
213  Op(TreeT &tree) : mDelete(false), mTree(&tree) {}
214  Op(const Op& other, tbb::split) : mDelete(true), mTree(new TreeT(other.mTree->background())) {}
215  ~Op() { if (mDelete) delete mTree; }
216  void operator()(RangeT &r) { for (auto i=r.begin(); i!=r.end(); ++i) this->merge(*i);}
217  void join(Op &other) { this->merge(*(other.mTree)); }
218  void merge(TreeT &tree) { mTree->merge(tree, openvdb::MERGE_ACTIVE_STATES); }
219  } op( mGrid->tree() );
220  tbb::parallel_reduce(RangeT(pool.begin(), pool.end(), 4), op);
221  } else {
222  kernel(tbb::blocked_range<int>(imin, imax));//serial
223  mGrid->tree().merge(*pool.begin(), openvdb::MERGE_ACTIVE_STATES);
224  }
225 
226  // Define consistent signed distances outside the narrow-band
227  tools::signedFloodFill(mGrid->tree(), threaded);
228 
229  if (mInterrupt) mInterrupt->end();
230  }
231 
232  const ValueT mRadius;
233  const Vec3T mCenter;
234  InterruptT* mInterrupt;
235  typename GridT::Ptr mGrid;
236 };// LevelSetSphere
237 
238 
240 
241 
242 template<typename GridType, typename InterruptT>
243 typename GridType::Ptr
244 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
245  float halfWidth, InterruptT* interrupt, bool threaded)
246 {
247  // GridType::ValueType is required to be a floating-point scalar.
248  static_assert(std::is_floating_point<typename GridType::ValueType>::value,
249  "level set grids must have scalar, floating-point value types");
250 
251  using ValueT = typename GridType::ValueType;
252  LevelSetSphere<GridType, InterruptT> factory(ValueT(radius), center, interrupt);
253  return factory.getLevelSet(ValueT(voxelSize), ValueT(halfWidth), threaded);
254 }
255 
256 } // namespace tools
257 } // namespace OPENVDB_VERSION_NAME
258 } // namespace openvdb
259 
260 #endif // OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
261 
262 // Copyright (c) DreamWorks Animation LLC
263 // All rights reserved. This software is distributed under the
264 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
typename GridT::TreeType TreeT
Definition: LevelSetSphere.h:115
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Coord Abs(const Coord &xyz)
Definition: Coord.h:542
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:52
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:510
GridType::Ptr createLevelSetSphere(float radius, const openvdb::Vec3f &center, float voxelSize, float halfWidth=float(LEVEL_SET_HALF_WIDTH), bool threaded=true)
Return a grid of type GridType containing a narrow-band level set representation of a sphere.
Definition: LevelSetSphere.h:95
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:735
Definition: Mat.h:197
Definition: Types.h:556
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:128
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
Type Pow2(Type x)
Return x2.
Definition: Math.h:522
Definition: Exceptions.h:40
Definition: Types.h:504
typename GridT::ValueType ValueT
Definition: LevelSetSphere.h:116
Generates a signed distance field (or narrow band level set) to a single sphere.
Definition: LevelSetSphere.h:112
LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT *interrupt=nullptr)
Constructor.
Definition: LevelSetSphere.h:131
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
Definition: Exceptions.h:92
GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth, bool threaded=true)
Definition: LevelSetSphere.h:142
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
typename math::Vec3< ValueT > Vec3T
Definition: LevelSetSphere.h:117
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:830
int Floor(float x)
Return the floor of x.
Definition: Math.h:822
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76