OpenVDB: LevelSetSphere.h Source File

OpenVDB 6.1.0

LevelSetSphere.h
Go to the documentation of this file.
1 //
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 //
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 "SignedFloodFill.h"
47 #include <type_traits>
48
49
50 namespace openvdb {
51 OPENVDB_USE_VERSION_NAMESPACE
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 = nullptr);
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>
104 class LevelSetSphere
105 {
106 public:
107 using ValueT = typename GridT::ValueType;
108 using Vec3T = typename math::Vec3<ValueT>;
109 static_assert(std::is_floating_point<ValueT>::value,
110 "level set grids must have scalar, floating-point value types");
111
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
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(ValueT(i) - c[0]);
170 for (j = jmin; j <= jmax; ++j) {
171 const auto x2y2 = math::Pow2(ValueT(j) - c[1]) + x2;
172 for (k = kmin; k <= kmax; k += m) {
173 m = 1;
175 const auto v = math::Sqrt(x2y2 + math::Pow2(ValueT(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
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.
208 static_assert(std::is_floating_point<typename GridType::ValueType>::value,
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/ )
openvdb::v6_1::math::Abs
Coord Abs(const Coord &xyz)
Definition: Coord.h:536
Types.h
openvdb::v6_1::tools::LevelSetSphere
Generates a signed distance field (or narrow band level set) to a single sphere.
Definition: LevelSetSphere.h:104
openvdb::v6_1::tools::LevelSetSphere::ValueT
typename GridT::ValueType ValueT
Definition: LevelSetSphere.h:107
NullInterrupter.h
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
openvdb::v6_1::tools::LevelSetSphere::LevelSetSphere
LevelSetSphere(ValueT radius, const Vec3T &center, InterruptT *interrupt=nullptr)
Constructor.
Definition: LevelSetSphere.h:122
openvdb::v6_1::tools::LevelSetSphere::Vec3T
typename math::Vec3< ValueT > Vec3T
Definition: LevelSetSphere.h:108
openvdb::v6_1::tools::createLevelSetSphere
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
openvdb::v6_1::util::wasInterrupted
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
openvdb::v6_1::tools::signedFloodFill
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...
Definition: SignedFloodFill.h:294
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
SignedFloodFill.h
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
openvdb
Definition: Exceptions.h:40
openvdb::v6_1::tools::LevelSetSphere::getLevelSet
GridT::Ptr getLevelSet(ValueT voxelSize, ValueT halfWidth)
Definition: LevelSetSphere.h:132
openvdb::v6_1::math::Vec3
Definition: Mat.h:197
openvdb::v6_1::math::Sqrt
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:715
openvdb::v6_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:52
openvdb::v6_1::math::Ceil
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:810
openvdb::v6_1::math::Floor
int Floor(float x)
Return the floor of x.
Definition: Math.h:802
Grid.h
openvdb::v6_1::LEVEL_SET_HALF_WIDTH
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:510
openvdb::v6_1::ValueError
Definition: Exceptions.h:92
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
openvdb::v6_1::GRID_LEVEL_SET
Definition: Types.h:504
openvdb::v6_1::math::Pow2
Type Pow2(Type x)
Return x2.
Definition: Math.h:502