OpenVDB  1.2.0
LevelSetSphere.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 
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  mGrid->signedFloodFill();
187  if (mInterrupt) mInterrupt->end();
188  }
189 
190  const ValueT mRadius;
191  const Vec3T mCenter;
192  InterruptT* mInterrupt;
193  typename GridT::Ptr mGrid;
194 };// LevelSetSphere
195 
196 
198 
199 
200 template<typename GridType, typename InterruptT>
201 typename GridType::Ptr
202 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
203  float halfWidth, InterruptT* interrupt)
204 {
205  // GridType::ValueType is required to be a floating-point scalar.
206  BOOST_STATIC_ASSERT(boost::is_floating_point<typename GridType::ValueType>::value);
207 
208  typedef typename GridType::ValueType ValueT;
209  LevelSetSphere<GridType, InterruptT> factory(ValueT(radius), center, interrupt);
210  return factory.getLevelSet(ValueT(voxelSize), ValueT(halfWidth));
211 }
212 
213 } // namespace tools
214 } // namespace OPENVDB_VERSION_NAME
215 } // namespace openvdb
216 
217 #endif // OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
218 
219 // Copyright (c) 2012-2013 DreamWorks Animation LLC
220 // All rights reserved. This software is distributed under the
221 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )