HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LevelSetSphere.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 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 //
29 ///////////////////////////////////////////////////////////////////////////
30 ///
31 /// @file LevelSetSphere.h
32 ///
33 /// @brief Generate a narrow-band level set of sphere.
34 ///
35 /// @note By definition a level set has a fixed narrow band width
36 /// (the half width is defined by LEVEL_SET_HALF_WIDTH in Types.h),
37 /// whereas an SDF can have a variable narrow band width.
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 <hboost/utility.hpp>
47 #include <hboost/type_traits/is_floating_point.hpp>
48 #include "SignedFloodFill.h"
49 
50 namespace openvdb {
52 namespace OPENVDB_VERSION_NAME {
53 namespace tools {
54 
55 /// @brief Return a grid of type @c GridType containing a narrow-band level set
56 /// representation of a sphere.
57 ///
58 /// @param radius radius of the sphere in world units
59 /// @param center center of the sphere in world units
60 /// @param voxelSize voxel size in world units
61 /// @param halfWidth half the width of the narrow band, in voxel units
62 /// @param interrupt a pointer adhering to the util::NullInterrupter interface
63 ///
64 /// @note @c GridType::ValueType must be a floating-point scalar.
65 /// @note The leapfrog algorithm employed in this method is best suited
66 /// for a single large sphere. For multiple small spheres consider
67 /// using the faster algorithm in ParticlesToLevelSet.h
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 
73 /// @brief Return a grid of type @c GridType containing a narrow-band level set
74 /// representation of a sphere.
75 ///
76 /// @param radius radius of the sphere in world units
77 /// @param center center of the sphere in world units
78 /// @param voxelSize voxel size in world units
79 /// @param halfWidth half the width of the narrow band, in voxel units
80 ///
81 /// @note @c GridType::ValueType must be a floating-point scalar.
82 /// @note The leapfrog algorithm employed in this method is best suited
83 /// for a single large sphere. For multiple small spheres consider
84 /// using the faster algorithm in ParticlesToLevelSet.h
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 
94 ////////////////////////////////////////
95 
96 
97 /// @brief Generates a signed distance field (or narrow band level
98 /// set) to a single sphere.
99 ///
100 /// @note The leapfrog algorithm employed in this class is best
101 /// suited for a single large sphere. For multiple small spheres consider
102 /// using the faster algorithm in tools/ParticlesToLevelSet.h
103 template<typename GridT, typename InterruptT = util::NullInterrupter>
105 {
106 public:
107  typedef typename GridT::ValueType ValueT;
108  typedef typename math::Vec3<ValueT> Vec3T;
110 
111  /// @brief Constructor
112  ///
113  /// @param radius radius of the sphere in world units
114  /// @param center center of the sphere in world units
115  /// @param interrupt pointer to optional interrupter. Use template
116  /// argument util::NullInterrupter if no interruption is desired.
117  ///
118  /// @note If the radius of the sphere is smaller than
119  /// 1.5*voxelSize, i.e. the sphere is smaller than the Nyquist
120  /// frequency of the grid, it is ignored!
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 
127  /// @return a narrow-band level set of the sphere
128  ///
129  /// @param voxelSize Size of voxels in world units
130  /// @param halfWidth Half-width of narrow-band in voxel units
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 a 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;
173  /// Distance in voxel units to sphere
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 consistent 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 
198 ////////////////////////////////////////
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.
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-2017 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/ )
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:822
GridType::Ptr createLevelSetSphere(float radius, const openvdb::Vec3f &center, float voxelSize, float halfWidth=float(LEVEL_SET_HALF_WIDTH), InterruptT *interrupt=NULL)
Return a grid of type GridType containing a narrow-band level set representation of a sphere...
Type Pow2(Type x)
Return .
Definition: Math.h:514
const GLdouble * v
Definition: glcorearb.h:836
png_uint_32 i
Definition: png.h:2877
GLfloat f
Definition: glcorearb.h:1925
Coord Abs(const Coord &xyz)
Definition: Coord.h:254
#define OPENVDB_VERSION_NAME
Definition: version.h:43
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.
Definition: Math.h:727
GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth)
Propagates the sign of distance values from the active voxels in the narrow band to the inactive valu...
GLsizei const GLfloat * value
Definition: glcorearb.h:823
Generates a signed distance field (or narrow band level set) to a single sphere.
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...
HBOOST_STATIC_ASSERT(hboost::is_floating_point< ValueT >::value)
int Floor(float x)
Return the floor of x.
Definition: Math.h:814
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
bool wasInterrupted(T *i, int percent=-1)
LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT *interrupt=NULL)
Constructor.
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
math::Vec3< float > Vec3f
Definition: Types.h:77