OpenVDB  3.0.0
LevelSetSphere.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2014 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>
45 #include <openvdb/util/NullInterrupter.h>
46 #include <boost/utility.hpp>
47 #include <boost/type_traits/is_floating_point.hpp>
48 #include "SignedFloodFill.h"
49 
50 namespace openvdb {
52 namespace OPENVDB_VERSION_NAME {
53 namespace tools {
54 
68 template<typename GridType, typename InterruptT>
69 typename GridType::Ptr
70 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
71  float halfWidth = float(LEVEL_SET_HALF_WIDTH), InterruptT* interrupt = NULL);
72 
85 template<typename GridType>
86 typename GridType::Ptr
87 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
88  float halfWidth = float(LEVEL_SET_HALF_WIDTH))
89 {
90  return createLevelSetSphere<GridType, util::NullInterrupter>(radius,center,voxelSize,halfWidth);
91 }
92 
93 
95 
96 
103 template<typename GridT, typename InterruptT = util::NullInterrupter>
105 {
106 public:
107  typedef typename GridT::ValueType ValueT;
108  typedef typename math::Vec3<ValueT> Vec3T;
109  BOOST_STATIC_ASSERT(boost::is_floating_point<ValueT>::value);
110 
121  LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT* interrupt = NULL)
122  : mRadius(radius), mCenter(center), mInterrupt(interrupt)
123  {
124  if (mRadius<=0) OPENVDB_THROW(ValueError, "radius must be positive");
125  }
126 
131  typename GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth)
132  {
133  mGrid = createLevelSet<GridT>(voxelSize, halfWidth);
134  this->rasterSphere(voxelSize, halfWidth);
135  mGrid->setGridClass(GRID_LEVEL_SET);
136  return mGrid;
137  }
138 
139 private:
140  void rasterSphere(ValueT dx, ValueT w)
141  {
142  if (!(dx>0.0f)) OPENVDB_THROW(ValueError, "voxel size must be positive");
143  if (!(w>1)) OPENVDB_THROW(ValueError, "half-width must be larger than one");
144 
145  // Define radius of sphere and narrow-band in voxel units
146  const ValueT r0 = mRadius/dx, rmax = r0 + w;
147 
148  // Radius below the Nyquist frequency
149  if (r0 < 1.5f) return;
150 
151  // Define center of sphere in voxel units
152  const Vec3T c(mCenter[0]/dx, mCenter[1]/dx, mCenter[2]/dx);
153 
154  // Define index coordinates and their respective bounds
155  openvdb::Coord ijk;
156  int &i = ijk[0], &j = ijk[1], &k = ijk[2], m=1;
157  const int imin=math::Floor(c[0]-rmax), imax=math::Ceil(c[0]+rmax);
158  const int jmin=math::Floor(c[1]-rmax), jmax=math::Ceil(c[1]+rmax);
159  const int kmin=math::Floor(c[2]-rmax), kmax=math::Ceil(c[2]+rmax);
160 
161  // Allocate an ValueAccessor for accelerated random access
162  typename GridT::Accessor accessor = mGrid->getAccessor();
163 
164  if (mInterrupt) mInterrupt->start("Generating level set of sphere");
165  // Compute signed distances to sphere using leapfrogging in k
166  for ( i = imin; i <= imax; ++i ) {
167  if (util::wasInterrupted(mInterrupt)) return;
168  const float x2 = math::Pow2(i - c[0]);
169  for ( j = jmin; j <= jmax; ++j ) {
170  const float x2y2 = math::Pow2(j - c[1]) + x2;
171  for (k=kmin; k<=kmax; k += m) {
172  m = 1;
174  const float v = math::Sqrt(x2y2 + math::Pow2(k-c[2]))-r0,
175  d = math::Abs(v);
176  if ( d < w ){ // inside narrow band
177  accessor.setValue(ijk, dx*v);// distance in world units
178  } else {// outside narrow band
179  m += math::Floor(d-w);// leapfrog
180  }
181  }//end leapfrog over k
182  }//end loop over j
183  }//end loop over i
184 
185  // Define consistant signed distances outside the narrow-band
186  tools::signedFloodFill(mGrid->tree());
187 
188  if (mInterrupt) mInterrupt->end();
189  }
190 
191  const ValueT mRadius;
192  const Vec3T mCenter;
193  InterruptT* mInterrupt;
194  typename GridT::Ptr mGrid;
195 };// LevelSetSphere
196 
197 
199 
200 
201 template<typename GridType, typename InterruptT>
202 typename GridType::Ptr
203 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
204  float halfWidth, InterruptT* interrupt)
205 {
206  // GridType::ValueType is required to be a floating-point scalar.
207  BOOST_STATIC_ASSERT(boost::is_floating_point<typename GridType::ValueType>::value);
208 
209  typedef typename GridType::ValueType ValueT;
210  LevelSetSphere<GridType, InterruptT> factory(ValueT(radius), center, interrupt);
211  return factory.getLevelSet(ValueT(voxelSize), ValueT(halfWidth));
212 }
213 
214 } // namespace tools
215 } // namespace OPENVDB_VERSION_NAME
216 } // namespace openvdb
217 
218 #endif // OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
219 
220 // Copyright (c) 2012-2014 DreamWorks Animation LLC
221 // All rights reserved. This software is distributed under the
222 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
int32_t Abs(int32_t i)
Return the absolute value of the given quantity.
Definition: Math.h:284
Definition: Mat.h:146
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Type Pow2(Type x)
Return .
Definition: Math.h:498
GridType::Ptr createLevelSetSphere(float radius, const openvdb::Vec3f &center, float voxelSize, float halfWidth=float(LEVEL_SET_HALF_WIDTH))
Return a grid of type GridType containing a narrow-band level set representation of a sphere...
Definition: LevelSetSphere.h:87
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
Definition: Types.h:210
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:693
#define OPENVDB_VERSION_NAME
Definition: version.h:43
GridT::ValueType ValueT
Definition: LevelSetSphere.h:107
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:216
Propagates the sign of distance values from the active voxels in the narrow band to the inactive valu...
Definition: Exceptions.h:39
GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth)
Definition: LevelSetSphere.h:131
Definition: Exceptions.h:88
int Floor(float x)
Return the floor of x.
Definition: Math.h:780
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:788
math::Vec3< ValueT > Vec3T
Definition: LevelSetSphere.h:108
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT *interrupt=NULL)
Constructor.
Definition: LevelSetSphere.h:121
void signedFloodFill(TreeOrLeafManagerT &tree, bool threaded=true, size_t grainSize=1)
Set the values of all inactive voxels and tiles of a narrow-band level set from the signs of the acti...
Definition: SignedFloodFill.h:282
Generates a signed distance field (or narrow band level set) to a single sphere.
Definition: LevelSetSphere.h:104