OpenVDB  0.104.0
LevelSetSphere.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012 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/openvdb.h>
43 #include <openvdb/util/NullInterrupter.h>
44 #include <boost/utility.hpp>
45 #include <boost/type_traits/is_floating_point.hpp>
46 
47 namespace openvdb {
49 namespace OPENVDB_VERSION_NAME {
50 namespace tools {
51 
52 
66 template<typename GridType, typename InterruptT>
67 typename GridType::Ptr
68 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
69  float halfWidth = float(LEVEL_SET_HALF_WIDTH), InterruptT* interrupt = NULL);
70 
83 template<typename GridType>
84 typename GridType::Ptr
85 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
86  float halfWidth = float(LEVEL_SET_HALF_WIDTH))
87 {
88  return createLevelSetSphere<GridType, util::NullInterrupter>(radius,center,voxelSize,halfWidth);
89 }
90 
92 
93 
100 template<typename GridT, typename InterruptT = util::NullInterrupter>
102 {
103 public:
104  typedef typename GridT::ValueType ValueT;
105  typedef typename math::Vec3<ValueT> Vec3T;
106  BOOST_STATIC_ASSERT(boost::is_floating_point<ValueT>::value);
107 
118  LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT* interrupt = NULL)
119  : mRadius(radius), mCenter(center), mInterrupt(interrupt)
120  {
121  if (mRadius<=0) OPENVDB_THROW(ValueError, "radius must be positive");
122  }
123 
128  typename GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth)
129  {
130  mGrid = createLevelSet<GridT>(voxelSize, halfWidth);
131  this->rasterSphere(voxelSize, halfWidth);
132  mGrid->setGridClass(GRID_LEVEL_SET);
133  return mGrid;
134  }
135 
136 
137 private:
138 
139  void rasterSphere(ValueT dx, ValueT w)
140  {
141  if (!(dx>0.0f)) OPENVDB_THROW(ValueError, "voxel size must be positive");
142  if (!(w>1)) OPENVDB_THROW(ValueError, "half-width must be larger than one");
143 
144  // Define radius of sphere and narrow-band in voxel units
145  const ValueT r0 = mRadius/dx, rmax = r0 + w;
146 
147  // Radius below the Nyquist frequency
148  if (r0 < 1.5f) return;
149 
150  // Define center of sphere in voxel units
151  const Vec3T c(mCenter[0]/dx, mCenter[1]/dx, mCenter[2]/dx);
152 
153  // Define index coordinates and their respective bounds
154  openvdb::Coord ijk;
155  int &i = ijk[0], &j = ijk[1], &k = ijk[2], m=1;
156  const int imin=math::Floor(c[0]-rmax), imax=math::Ceil(c[0]+rmax);
157  const int jmin=math::Floor(c[1]-rmax), jmax=math::Ceil(c[1]+rmax);
158  const int kmin=math::Floor(c[2]-rmax), kmax=math::Ceil(c[2]+rmax);
159 
160  // Allocate an ValueAccessor for accelerated random access
161  typename GridT::Accessor accessor = mGrid->getAccessor();
162 
163  if (mInterrupt) mInterrupt->start("Generating level set of sphere");
164  // Compute signed distances to sphere using leapfrogging in k
165  for ( i = imin; i <= imax; ++i ) {
166  if (util::wasInterrupted(mInterrupt)) return;
167  const float x2 = math::Pow2(i - c[0]);
168  for ( j = jmin; j <= jmax; ++j ) {
169  const float x2y2 = math::Pow2(j - c[1]) + x2;
170  for (k=kmin; k<=kmax; k += m) {
171  m = 1;
173  const float v = math::Sqrt(x2y2 + math::Pow2(k-c[2]))-r0,
174  d = math::Abs(v);
175  if ( d < w )// inside narrow band
176  accessor.setValue(ijk, dx*v);// distance in world units
177  else// outside narrow band
178  m += math::Floor(d-w);// leapfrog
179  }//end leapfrog over k
180  }//end loop over j
181  }//end loop over i
182 
183  // Define consistant signed distances outside the narrow-band
184  mGrid->signedFloodFill();
185  if (mInterrupt) mInterrupt->end();
186  }
187 
188  const ValueT mRadius;
189  const Vec3T mCenter;
190  InterruptT* mInterrupt;
191  typename GridT::Ptr mGrid;
192 };// LevelSetSphere
193 
194 
196 
197 template<typename GridType, typename InterruptT>
198 typename GridType::Ptr
199 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
200  float halfWidth, InterruptT* interrupt)
201 {
202  // GridType::ValueType is required to be a floating-point scalar.
203  BOOST_STATIC_ASSERT(boost::is_floating_point<typename GridType::ValueType>::value);
204 
205  typedef typename GridType::ValueType ValueT;
206  LevelSetSphere<GridType, InterruptT> factory(ValueT(radius), center, interrupt);
207  return factory.getLevelSet(ValueT(voxelSize), ValueT(halfWidth));
208 }
209 
210 
212 
213 
228 template<typename InterruptT>
230  float voxelSize, float halfWidth, InterruptT* interrupt)
231 {
232  LevelSetSphere<FloatGrid, InterruptT> s(radius, center, &interrupt);
233  return s.getLevelSet(voxelSize, halfWidth);
234 }
235 
236 } // namespace tools
237 } // namespace OPENVDB_VERSION_NAME
238 } // namespace openvdb
239 
240 #endif // OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
241 
242 // Copyright (c) 2012 DreamWorks Animation LLC
243 // All rights reserved. This software is distributed under the
244 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )