OpenVDB 3.2.0

LevelSetRebuild.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2016 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 //
30
31 #ifndef OPENVDB_TOOLS_LEVELSETREBUILD_HAS_BEEN_INCLUDED
32 #define OPENVDB_TOOLS_LEVELSETREBUILD_HAS_BEEN_INCLUDED
33
34 #include <openvdb/Grid.h>
35 #include <openvdb/Exceptions.h>
36 #include <openvdb/math/Math.h>
37 #include <openvdb/math/Transform.h>
38 #include <openvdb/tools/VolumeToMesh.h>
39 #include <openvdb/tools/MeshToVolume.h>
40 #include <openvdb/util/NullInterrupter.h>
41 #include <openvdb/util/Util.h>
42 #include <boost/type_traits/is_floating_point.hpp>
43 #include <boost/utility/enable_if.hpp>
44 #include <tbb/blocked_range.h>
45 #include <tbb/parallel_for.h>
46
47
48 namespace openvdb {
49 OPENVDB_USE_VERSION_NAMESPACE
50 namespace OPENVDB_VERSION_NAME {
51 namespace tools {
52
53
69 template<class GridType>
70 inline typename GridType::Ptr
71 levelSetRebuild(const GridType& grid, float isovalue = 0,
72 float halfWidth = float(LEVEL_SET_HALF_WIDTH), const math::Transform* xform = NULL);
73
74
89 template<class GridType>
90 inline typename GridType::Ptr
91 levelSetRebuild(const GridType& grid, float isovalue, float exBandWidth, float inBandWidth,
92 const math::Transform* xform = NULL);
93
94
110 template<class GridType, typename InterruptT>
111 inline typename GridType::Ptr
112 levelSetRebuild(const GridType& grid, float isovalue, float exBandWidth, float inBandWidth,
113 const math::Transform* xform = NULL, InterruptT* interrupter = NULL);
114
115
117
118
119 // Internal utility objects and implementation details
120
121 namespace internal {
122
123 class PointListTransform
124 {
125 public:
126 PointListTransform(const PointList& pointsIn, std::vector<Vec3s>& pointsOut,
127 const math::Transform& xform)
128 : mPointsIn(pointsIn)
129 , mPointsOut(&pointsOut)
130 , mXform(xform)
131 {
132 }
133
134 void runParallel()
135 {
136 tbb::parallel_for(tbb::blocked_range<size_t>(0, mPointsOut->size()), *this);
137 }
138
139 void runSerial()
140 {
141 (*this)(tbb::blocked_range<size_t>(0, mPointsOut->size()));
142 }
143
144 inline void operator()(const tbb::blocked_range<size_t>& range) const
145 {
146 for (size_t n = range.begin(); n < range.end(); ++n) {
147 (*mPointsOut)[n] = Vec3s(mXform.worldToIndex(mPointsIn[n]));
148 }
149 }
150
151 private:
152 const PointList& mPointsIn;
153 std::vector<Vec3s> * const mPointsOut;
154 const math::Transform& mXform;
155 };
156
157
158 class PrimCpy
159 {
160 public:
161 PrimCpy(const PolygonPoolList& primsIn, const std::vector<size_t>& indexList,
162 std::vector<Vec4I>& primsOut)
163 : mPrimsIn(primsIn)
164 , mIndexList(indexList)
165 , mPrimsOut(&primsOut)
166 {
167 }
168
169 void runParallel()
170 {
171 tbb::parallel_for(tbb::blocked_range<size_t>(0, mIndexList.size()), *this);
172 }
173
174 void runSerial()
175 {
176 (*this)(tbb::blocked_range<size_t>(0, mIndexList.size()));
177 }
178
179 inline void operator()(const tbb::blocked_range<size_t>& range) const
180 {
181 openvdb::Vec4I quad;
182 quad[3] = openvdb::util::INVALID_IDX;
183 std::vector<Vec4I>& primsOut = *mPrimsOut;
184
185 for (size_t n = range.begin(); n < range.end(); ++n) {
186 size_t index = mIndexList[n];
187 PolygonPool& polygons = mPrimsIn[n];
188
189 // Copy quads
190 for (size_t i = 0, I = polygons.numQuads(); i < I; ++i) {
191 primsOut[index++] = polygons.quad(i);
192 }
193 polygons.clearQuads();
194
195 // Copy triangles (adaptive mesh)
196 for (size_t i = 0, I = polygons.numTriangles(); i < I; ++i) {
197 const openvdb::Vec3I& triangle = polygons.triangle(i);
198 quad[0] = triangle[0];
199 quad[1] = triangle[1];
200 quad[2] = triangle[2];
201 primsOut[index++] = quad;
202 }
203
204 polygons.clearTriangles();
205 }
206 }
207
208 private:
209 const PolygonPoolList& mPrimsIn;
210 const std::vector<size_t>& mIndexList;
211 std::vector<Vec4I> * const mPrimsOut;
212 };
213
214 } // namespace internal
215
216
218
219
226 template<class GridType, typename InterruptT>
227 inline typename boost::enable_if<boost::is_floating_point<typename GridType::ValueType>,
228 typename GridType::Ptr>::type
229 doLevelSetRebuild(const GridType& grid, typename GridType::ValueType iso,
230 typename GridType::ValueType exWidth, typename GridType::ValueType inWidth,
231 const math::Transform* xform, InterruptT* interrupter)
232 {
233 const float
234 isovalue = float(iso),
235 exBandWidth = float(exWidth),
236 inBandWidth = float(inWidth);
237
238 tools::VolumeToMesh mesher(isovalue);
239 mesher(grid);
240
241 math::Transform::Ptr transform = (xform != NULL) ? xform->copy() : grid.transform().copy();
242
243 std::vector<Vec3s> points(mesher.pointListSize());
244
245 { // Copy and transform (required for MeshToVolume) points to grid space.
246 internal::PointListTransform ptnXForm(mesher.pointList(), points, *transform);
247 ptnXForm.runParallel();
248 mesher.pointList().reset(NULL);
249 }
250
251 std::vector<Vec4I> primitives;
252
253 { // Copy primitives.
254 PolygonPoolList& polygonPoolList = mesher.polygonPoolList();
255
256 size_t numPrimitives = 0;
257 std::vector<size_t> indexlist(mesher.polygonPoolListSize());
258
259 for (size_t n = 0, N = mesher.polygonPoolListSize(); n < N; ++n) {
260 const openvdb::tools::PolygonPool& polygons = polygonPoolList[n];
261 indexlist[n] = numPrimitives;
262 numPrimitives += polygons.numQuads();
263 numPrimitives += polygons.numTriangles();
264 }
265
266 primitives.resize(numPrimitives);
267 internal::PrimCpy primCpy(polygonPoolList, indexlist, primitives);
268 primCpy.runParallel();
269 }
270
271 QuadAndTriangleDataAdapter<Vec3s, Vec4I> mesh(points, primitives);
272
273 if (interrupter) {
274 return meshToVolume<GridType>(*interrupter, mesh, *transform, exBandWidth, inBandWidth,
275 DISABLE_RENORMALIZATION, NULL);
276 }
277
278 return meshToVolume<GridType>(mesh, *transform, exBandWidth, inBandWidth,
279 DISABLE_RENORMALIZATION, NULL);
280 }
281
282
285 template<class GridType, typename InterruptT>
286 inline typename boost::disable_if<boost::is_floating_point<typename GridType::ValueType>,
287 typename GridType::Ptr>::type
288 doLevelSetRebuild(const GridType&, typename GridType::ValueType /*isovalue*/,
289 typename GridType::ValueType /*exWidth*/, typename GridType::ValueType /*inWidth*/,
290 const math::Transform*, InterruptT*)
291 {
292 OPENVDB_THROW(TypeError,
293 "level set rebuild is supported only for scalar, floating-point grids");
294 }
295
296
298
299
300 template<class GridType, typename InterruptT>
301 inline typename GridType::Ptr
302 levelSetRebuild(const GridType& grid, float iso, float exWidth, float inWidth,
303 const math::Transform* xform, InterruptT* interrupter)
304 {
305 typedef typename GridType::ValueType ValueT;
306 ValueT
307 isovalue(zeroVal<ValueT>() + ValueT(iso)),
308 exBandWidth(zeroVal<ValueT>() + ValueT(exWidth)),
309 inBandWidth(zeroVal<ValueT>() + ValueT(inWidth));
310
311 return doLevelSetRebuild(grid, isovalue, exBandWidth, inBandWidth, xform, interrupter);
312 }
313
314
315 template<class GridType>
316 inline typename GridType::Ptr
317 levelSetRebuild(const GridType& grid, float iso, float exWidth, float inWidth,
318 const math::Transform* xform)
319 {
320 typedef typename GridType::ValueType ValueT;
321 ValueT
322 isovalue(zeroVal<ValueT>() + ValueT(iso)),
323 exBandWidth(zeroVal<ValueT>() + ValueT(exWidth)),
324 inBandWidth(zeroVal<ValueT>() + ValueT(inWidth));
325
326 return doLevelSetRebuild<GridType, util::NullInterrupter>(
327 grid, isovalue, exBandWidth, inBandWidth, xform, NULL);
328 }
329
330
331 template<class GridType>
332 inline typename GridType::Ptr
333 levelSetRebuild(const GridType& grid, float iso, float halfVal, const math::Transform* xform)
334 {
335 typedef typename GridType::ValueType ValueT;
336 ValueT
337 isovalue(zeroVal<ValueT>() + ValueT(iso)),
338 halfWidth(zeroVal<ValueT>() + ValueT(halfVal));
339
340 return doLevelSetRebuild<GridType, util::NullInterrupter>(
341 grid, isovalue, halfWidth, halfWidth, xform, NULL);
342 }
343
344
345 } // namespace tools
346 } // namespace OPENVDB_VERSION_NAME
347 } // namespace openvdb
348
349 #endif // OPENVDB_TOOLS_LEVELSETREBUILD_HAS_BEEN_INCLUDED
350
351 // Copyright (c) 2012-2016 DreamWorks Animation LLC
352 // All rights reserved. This software is distributed under the
353 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v3_2_0::tools::internal::PointListTransform
Definition: LevelSetRebuild.h:123
openvdb::v3_2_0::util::INVALID_IDX
OPENVDB_API const Index32 INVALID_IDX
openvdb::v3_2_0::tools::internal::PointListTransform::runParallel
void runParallel()
Definition: LevelSetRebuild.h:134
openvdb::v3_2_0::tools::doLevelSetRebuild
boost::disable_if< boost::is_floating_point< typename GridType::ValueType >, typename GridType::Ptr >::type doLevelSetRebuild(const GridType &, typename GridType::ValueType, typename GridType::ValueType, typename GridType::ValueType, const math::Transform *, InterruptT *)
Definition: LevelSetRebuild.h:288
openvdb::v3_2_0::math::Vec3s
Vec3< float > Vec3s
Definition: Vec3.h:650
openvdb::v3_2_0::math::Transform::copy
Ptr copy() const
Definition: Transform.h:77
NullInterrupter.h
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v3_2_0::math::Vec3
Definition: Mat.h:146
openvdb::v3_2_0::tools::PolygonPool::quad
openvdb::Vec4I & quad(size_t n)
Definition: VolumeToMesh.h:144
openvdb::v3_2_0::tools::VolumeToMesh::pointList
PointList & pointList()
Definition: VolumeToMesh.h:4829
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
openvdb::v3_2_0::LEVEL_SET_HALF_WIDTH
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:219
openvdb::v3_2_0::tools::levelSetRebuild
GridType::Ptr levelSetRebuild(const GridType &grid, float isovalue, float exBandWidth, float inBandWidth, const math::Transform *xform=NULL, InterruptT *interrupter=NULL)
Return a new grid of type GridType that contains a narrow-band level set representation of an isosurf...
Definition: LevelSetRebuild.h:302
openvdb::v3_2_0::tools::PolygonPool::triangle
openvdb::Vec3I & triangle(size_t n)
Definition: VolumeToMesh.h:150
openvdb::v3_2_0::tools::VolumeToMesh
Mesh any scalar grid that has a continuous isosurface.
Definition: VolumeToMesh.h:191
Transform.h
openvdb::v3_2_0::tools::PolygonPool
Collection of quads and triangles.
Definition: VolumeToMesh.h:124
openvdb::v3_2_0::tools::internal::PointListTransform::runSerial
void runSerial()
Definition: LevelSetRebuild.h:139
openvdb::v3_2_0::tools::internal::PrimCpy::PrimCpy
PrimCpy(const PolygonPoolList &primsIn, const std::vector< size_t > &indexList, std::vector< Vec4I > &primsOut)
Definition: LevelSetRebuild.h:161
VolumeToMesh.h
Extract polygonal surfaces from scalar volumes.
Exceptions.h
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v3_2_0::tools::PointList
boost::scoped_array< openvdb::Vec3s > PointList
Point and primitive list types.
Definition: VolumeToMesh.h:182
openvdb::v3_2_0::tools::internal::PointListTransform::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: LevelSetRebuild.h:144
openvdb::v3_2_0::math::Transform
Calculate an axis-aligned bounding box in index space from a bounding sphere in world space...
Definition: Transform.h:66
MeshToVolume.h
Convert polygonal meshes that consist of quads and/or triangles into signed or unsigned distance fiel...
openvdb::v3_2_0::tools::VolumeToMesh::polygonPoolListSize
const size_t & polygonPoolListSize() const
Definition: VolumeToMesh.h:4857
openvdb::v3_2_0::tools::DISABLE_RENORMALIZATION
Definition: MeshToVolume.h:101
openvdb
Definition: Exceptions.h:39
openvdb::v3_2_0::tools::internal::PrimCpy::runSerial
void runSerial()
Definition: LevelSetRebuild.h:174
Util.h
openvdb::v3_2_0::tools::internal::PointListTransform::PointListTransform
PointListTransform(const PointList &pointsIn, std::vector< Vec3s > &pointsOut, const math::Transform &xform)
Definition: LevelSetRebuild.h:126
openvdb::v3_2_0::TypeError
Definition: Exceptions.h:87
openvdb::v3_2_0::tools::PolygonPool::numQuads
const size_t & numQuads() const
Definition: VolumeToMesh.h:142
openvdb::v3_2_0::tools::internal::PrimCpy::runParallel
void runParallel()
Definition: LevelSetRebuild.h:169
Grid.h
openvdb::v3_2_0::tools::internal::PrimCpy::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: LevelSetRebuild.h:179
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_2_0::tools::internal::PrimCpy
Definition: LevelSetRebuild.h:158
openvdb::v3_2_0::tools::QuadAndTriangleDataAdapter
Contiguous quad and triangle data adapter class.
Definition: MeshToVolume.h:191
openvdb::v3_2_0::tools::PolygonPoolList
boost::scoped_array< PolygonPool > PolygonPoolList
Point and primitive list types.
Definition: VolumeToMesh.h:183
openvdb::v3_2_0::tools::VolumeToMesh::polygonPoolList
PolygonPoolList & polygonPoolList()
Definition: VolumeToMesh.h:4843
openvdb::v3_2_0::tools::VolumeToMesh::pointListSize
const size_t & pointListSize() const
Definition: VolumeToMesh.h:4836
openvdb::v3_2_0::math::Transform::Ptr
boost::shared_ptr< Transform > Ptr
Definition: Transform.h:69
openvdb::v3_2_0::math::Vec4
Definition: Mat4.h:51
openvdb::v3_2_0::tools::PolygonPool::clearQuads
void clearQuads()
Definition: VolumeToMesh.h:4718
openvdb::v3_2_0::tools::PolygonPool::clearTriangles
void clearTriangles()
Definition: VolumeToMesh.h:4736
openvdb::v3_2_0::tools::PolygonPool::numTriangles
const size_t & numTriangles() const
Definition: VolumeToMesh.h:148