HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LevelSetSphere.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 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 "SignedFloodFill.h"
47 #include <type_traits>
48 
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 = nullptr);
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  using ValueT = typename GridT::ValueType;
108  using Vec3T = typename math::Vec3<ValueT>;
110  "level set grids must have scalar, floating-point value types");
111 
112  /// @brief Constructor
113  ///
114  /// @param radius radius of the sphere in world units
115  /// @param center center of the sphere in world units
116  /// @param interrupt pointer to optional interrupter. Use template
117  /// argument util::NullInterrupter if no interruption is desired.
118  ///
119  /// @note If the radius of the sphere is smaller than
120  /// 1.5*voxelSize, i.e. the sphere is smaller than the Nyquist
121  /// frequency of the grid, it is ignored!
122  LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT* interrupt = nullptr)
123  : mRadius(radius), mCenter(center), mInterrupt(interrupt)
124  {
125  if (mRadius<=0) OPENVDB_THROW(ValueError, "radius must be positive");
126  }
127 
128  /// @return a narrow-band level set of the sphere
129  ///
130  /// @param voxelSize Size of voxels in world units
131  /// @param halfWidth Half-width of narrow-band in voxel units
132  typename GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth)
133  {
134  mGrid = createLevelSet<GridT>(voxelSize, halfWidth);
135  this->rasterSphere(voxelSize, halfWidth);
136  mGrid->setGridClass(GRID_LEVEL_SET);
137  return mGrid;
138  }
139 
140 private:
141  void rasterSphere(ValueT dx, ValueT w)
142  {
143  if (!(dx>0.0f)) OPENVDB_THROW(ValueError, "voxel size must be positive");
144  if (!(w>1)) OPENVDB_THROW(ValueError, "half-width must be larger than one");
145 
146  // Define radius of sphere and narrow-band in voxel units
147  const ValueT r0 = mRadius/dx, rmax = r0 + w;
148 
149  // Radius below the Nyquist frequency
150  if (r0 < 1.5f) return;
151 
152  // Define center of sphere in voxel units
153  const Vec3T c(mCenter[0]/dx, mCenter[1]/dx, mCenter[2]/dx);
154 
155  // Define index coordinates and their respective bounds
156  openvdb::Coord ijk;
157  int &i = ijk[0], &j = ijk[1], &k = ijk[2], m=1;
158  const int imin=math::Floor(c[0]-rmax), imax=math::Ceil(c[0]+rmax);
159  const int jmin=math::Floor(c[1]-rmax), jmax=math::Ceil(c[1]+rmax);
160  const int kmin=math::Floor(c[2]-rmax), kmax=math::Ceil(c[2]+rmax);
161 
162  // Allocate a ValueAccessor for accelerated random access
163  typename GridT::Accessor accessor = mGrid->getAccessor();
164 
165  if (mInterrupt) mInterrupt->start("Generating level set of sphere");
166  // Compute signed distances to sphere using leapfrogging in k
167  for (i = imin; i <= imax; ++i) {
168  if (util::wasInterrupted(mInterrupt)) return;
169  const auto x2 = math::Pow2(i - c[0]);
170  for (j = jmin; j <= jmax; ++j) {
171  const auto x2y2 = math::Pow2(j - c[1]) + x2;
172  for (k = kmin; k <= kmax; k += m) {
173  m = 1;
174  /// Distance in voxel units to sphere
175  const auto v = math::Sqrt(x2y2 + math::Pow2(k-c[2]))-r0;
176  const auto d = math::Abs(v);
177  if (d < w) { // inside narrow band
178  accessor.setValue(ijk, dx*v);// distance in world units
179  } else { // outside narrow band
180  m += math::Floor(d-w);// leapfrog
181  }
182  }//end leapfrog over k
183  }//end loop over j
184  }//end loop over i
185 
186  // Define consistent signed distances outside the narrow-band
187  tools::signedFloodFill(mGrid->tree());
188 
189  if (mInterrupt) mInterrupt->end();
190  }
191 
192  const ValueT mRadius;
193  const Vec3T mCenter;
194  InterruptT* mInterrupt;
195  typename GridT::Ptr mGrid;
196 };// LevelSetSphere
197 
198 
199 ////////////////////////////////////////
200 
201 
202 template<typename GridType, typename InterruptT>
203 typename GridType::Ptr
204 createLevelSetSphere(float radius, const openvdb::Vec3f& center, float voxelSize,
205  float halfWidth, InterruptT* interrupt)
206 {
207  // GridType::ValueType is required to be a floating-point scalar.
209  "level set grids must have scalar, floating-point value types");
210 
211  using ValueT = typename GridType::ValueType;
212  LevelSetSphere<GridType, InterruptT> factory(ValueT(radius), center, interrupt);
213  return factory.getLevelSet(ValueT(voxelSize), ValueT(halfWidth));
214 }
215 
216 } // namespace tools
217 } // namespace OPENVDB_VERSION_NAME
218 } // namespace openvdb
219 
220 #endif // OPENVDB_TOOLS_LEVELSETSPHERE_HAS_BEEN_INCLUDED
221 
222 // Copyright (c) 2012-2018 DreamWorks Animation LLC
223 // All rights reserved. This software is distributed under the
224 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:810
Type Pow2(Type x)
Return x2.
Definition: Math.h:502
const GLdouble * v
Definition: glcorearb.h:836
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT *interrupt=nullptr)
Constructor.
png_uint_32 i
Definition: png.h:2877
GLfloat f
Definition: glcorearb.h:1925
Coord Abs(const Coord &xyz)
Definition: Coord.h:513
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:715
GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth)
Propagate the signs 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...
int Floor(float x)
Return the floor of x.
Definition: Math.h:802
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
bool wasInterrupted(T *i, int percent=-1)
GridType::Ptr createLevelSetSphere(float radius, const openvdb::Vec3f &center, float voxelSize, float halfWidth=float(LEVEL_SET_HALF_WIDTH), InterruptT *interrupt=nullptr)
Return a grid of type GridType containing a narrow-band level set representation of a sphere...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
math::Vec3< float > Vec3f
Definition: Types.h:81