浏览代码

Add isovalue support

Former-commit-id: 1194f8921ea06e13f80d3dde6286a7859ad96d04
Francis Williams 6 年之前
父节点
当前提交
12b904a5b8
共有 2 个文件被更改,包括 101 次插入50 次删除
  1. 43 14
      include/igl/copyleft/marching_cubes.cpp
  2. 58 36
      include/igl/copyleft/marching_cubes.h

+ 43 - 14
include/igl/copyleft/marching_cubes.cpp

@@ -63,11 +63,13 @@ class MarchingCubes
   typedef typename MyMap::const_iterator                  MyMapIterator;
 
 public:
+  // Dense index grid version
   MarchingCubes(const Eigen::MatrixBase<DerivedValues> &values,
                 const Eigen::MatrixBase<DerivedPoints> &points,
                 const unsigned x_res,
                 const unsigned y_res,
                 const unsigned z_res,
+                const double isovalue,
                 Eigen::PlainObjectBase<DerivedVertices> &vertices,
                 Eigen::PlainObjectBase<DerivedFaces> &faces)
   {
@@ -125,7 +127,7 @@ public:
 
       // determine cube type
       for (i=0; i<8; ++i)
-        if (values(corner[i]) > 0.0)
+        if (values(corner[i]) > isovalue)
           cubetype |= (1<<i);
 
 
@@ -183,10 +185,11 @@ public:
 
   }
 
-
+  // Sparse index grid version
   MarchingCubes(const Eigen::MatrixBase<DerivedValues> &values,
                 const Eigen::MatrixBase<DerivedPoints> &points,
                 const Eigen::MatrixBase<DerivedIndices> &cubes,
+                const double isovalue,
                 Eigen::PlainObjectBase<DerivedVertices> &vertices,
                 Eigen::PlainObjectBase<DerivedFaces> &faces)
   {
@@ -220,7 +223,7 @@ public:
       // determine cube type
       for (int i=0; i<8; ++i)
       {
-        if (values[cube[i]] > 0.0)
+        if (values[cube[i]] > isovalue)
         {
           cubetype |= (1<<i);
         }
@@ -259,8 +262,6 @@ public:
       if (edgeTable[cubetype]&2048)
         samples[11] = add_vertex(values, points, cube[3], cube[7], vertices, num_vertices, edge2vertex);
 
-
-
       // connect samples by triangles
       for (int i=0; triTable[cubetype][0][i] != -1; i+=3 )
       {
@@ -283,13 +284,13 @@ public:
 
   }
 
-  static typename DerivedFaces::Scalar  add_vertex(const Eigen::MatrixBase<DerivedValues> &values,
-                                               const Eigen::MatrixBase<DerivedPoints> &points,
-                                               unsigned int i0,
-                                               unsigned int i1,
-                                               Eigen::PlainObjectBase<DerivedVertices> &vertices,
-                                               int &num_vertices,
-                                               MyMap &edge2vertex)
+  static typename DerivedFaces::Scalar add_vertex(const Eigen::MatrixBase<DerivedValues> &values,
+                                                  const Eigen::MatrixBase<DerivedPoints> &points,
+                                                  unsigned int i0,
+                                                  unsigned int i1,
+                                                  Eigen::PlainObjectBase<DerivedVertices> &vertices,
+                                                  int &num_vertices,
+                                                  MyMap &edge2vertex)
   {
     // find vertex if it has been computed already
     MyMapIterator it = edge2vertex.find(EdgeKey(i0, i1));
@@ -331,12 +332,40 @@ IGL_INLINE void igl::copyleft::marching_cubes(
   const unsigned x_res,
   const unsigned y_res,
   const unsigned z_res,
+  const double isovalue,
   Eigen::PlainObjectBase<DerivedVertices> &vertices,
   Eigen::PlainObjectBase<DerivedFaces> &faces)
 {
   typedef Eigen::MatrixXi Shim; /* DerivedIndices shim type is unused in this instantiation*/
   MarchingCubes<DerivedValues, DerivedPoints, DerivedVertices, Shim, DerivedFaces>
-          mc(values, points, x_res, y_res, z_res, vertices, faces);
+          mc(values, points, x_res, y_res, z_res, isovalue, vertices, faces);
+}
+
+template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedFaces>
+IGL_INLINE void igl::copyleft::marching_cubes(
+  const Eigen::MatrixBase<DerivedValues> &values,
+  const Eigen::MatrixBase<DerivedPoints> &points,
+  const unsigned x_res,
+  const unsigned y_res,
+  const unsigned z_res,
+  Eigen::PlainObjectBase<DerivedVertices> &vertices,
+  Eigen::PlainObjectBase<DerivedFaces> &faces)
+{
+  typedef Eigen::MatrixXi Shim; /* DerivedIndices shim type is unused in this instantiation*/
+  MarchingCubes<DerivedValues, DerivedPoints, DerivedVertices, Shim, DerivedFaces>
+          mc(values, points, x_res, y_res, z_res, 0.0 /*isovalue*/, vertices, faces);
+}
+
+template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
+IGL_INLINE void igl::copyleft::marching_cubes(
+  const Eigen::MatrixBase<DerivedValues>& values,
+  const Eigen::MatrixBase<DerivedPoints>& points,
+  const Eigen::MatrixBase<DerivedIndices>& indices,
+  const double isovalue,
+  Eigen::PlainObjectBase<DerivedVertices>& vertices,
+  Eigen::PlainObjectBase<DerivedFaces> &faces)
+{
+  MarchingCubes<DerivedValues, DerivedPoints, DerivedVertices, DerivedIndices, DerivedFaces> mc(values, points, indices, isovalue, vertices, faces);
 }
 
 template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
@@ -347,7 +376,7 @@ IGL_INLINE void igl::copyleft::marching_cubes(
   Eigen::PlainObjectBase<DerivedVertices> &vertices,
   Eigen::PlainObjectBase<DerivedFaces> &faces)
 {
-  MarchingCubes<DerivedValues, DerivedPoints, DerivedVertices, DerivedIndices, DerivedFaces> mc(values, points, indices, vertices, faces);
+  MarchingCubes<DerivedValues, DerivedPoints, DerivedVertices, DerivedIndices, DerivedFaces> mc(values, points, indices, 0.0 /*isovalue*/, vertices, faces);
 }
 
 #ifdef IGL_STATIC_LIBRARY

+ 58 - 36
include/igl/copyleft/marching_cubes.h

@@ -14,10 +14,10 @@ namespace igl
 {
   namespace copyleft
   {
-    // marching_cubes( values, points, x_res, y_res, z_res, vertices, faces )
+    // marching_cubes( values, points, x_res, y_res, z_res, isovalue, vertices, faces )
     //
-    // performs marching cubes reconstruction on the grid defined by values, and
-    // points, and generates vertices and faces
+    // performs marching cubes reconstruction on a grid defined by values, and
+    // points, and generates a mesh defined by vertices and faces
     //
     // Input:
     //  values  #number_of_grid_points x 1 array -- the scalar values of an
@@ -34,51 +34,73 @@ namespace igl
     //  xres  resolutions of the grid in x dimension
     //  yres  resolutions of the grid in y dimension
     //  zres  resolutions of the grid in z dimension
+    //  isovalue  the isovalue of the surface to reconstruct
     // Output:
     //   vertices  #V by 3 list of mesh vertex positions
     //   faces  #F by 3 list of mesh triangle indices
     //
-    template <
-      typename DerivedValues,
-      typename DerivedPoints,
-      typename DerivedVertices,
-      typename DerivedFaces>
-      IGL_INLINE void marching_cubes(
+    template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedFaces>
+    IGL_INLINE void marching_cubes(
         const Eigen::MatrixBase<DerivedValues> &values,
         const Eigen::MatrixBase<DerivedPoints> &points,
         const unsigned x_res,
         const unsigned y_res,
         const unsigned z_res,
+        const double isovalue,
         Eigen::PlainObjectBase<DerivedVertices> &vertices,
         Eigen::PlainObjectBase<DerivedFaces> &faces);
 
-      // marching_cubes( values, points, indices, vertices, faces )
-      //
-      // Perform marching cubes reconstruction on the grid cells defined by indices.
-      // The indices parameter is an nx8 dense array of index values into the points and values arrays.
-      // Each row of indices represents a cube for which to generate vertices and faces over.
-      //
-      // Input:
-      //  values  #number_of_grid_points x 1 array -- the scalar values of an
-      //    implicit function defined on the grid points (<0 in the inside of the
-      //    surface, 0 on the border, >0 outside)
-      //  points  #number_of_grid_points x 3 array -- 3-D positions of the grid
-      //    points, ordered in x,y,z order:
-      //  indices  #cubes x 8 array -- one row for each cube where each value is
-      //    the index of a vertex in points and a scalar in values.
-      //    i.e. points[indices[i, j]] = the position of the j'th vertex of the i'th cube
-      // Output:
-      //   vertices  #V by 3 list of mesh vertex positions
-      //   faces  #F by 3 list of mesh triangle indices
-      // Note: The winding direction of the cube indices will affect the output winding of the faces
-      //
-      template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
-      IGL_INLINE void marching_cubes(
-        const Eigen::MatrixBase<DerivedValues> &values,
-        const Eigen::MatrixBase<DerivedPoints> &points,
-        const Eigen::MatrixBase<DerivedIndices> &indices,
-        Eigen::PlainObjectBase<DerivedVertices> &vertices,
-        Eigen::PlainObjectBase<DerivedFaces> &faces);
+    // Overload of the above function where the isovalue defaults to 0.0
+    template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedFaces>
+    IGL_INLINE void marching_cubes(
+      const Eigen::MatrixBase<DerivedValues> &values,
+      const Eigen::MatrixBase<DerivedPoints> &points,
+      const unsigned x_res,
+      const unsigned y_res,
+      const unsigned z_res,
+      Eigen::PlainObjectBase<DerivedVertices> &vertices,
+      Eigen::PlainObjectBase<DerivedFaces> &faces);
+
+
+
+    // marching_cubes( values, points, indices, vertices, faces )
+    //
+    // Perform marching cubes reconstruction on the grid cells defined by indices.
+    // The indices parameter is an nx8 dense array of index values into the points and values arrays.
+    // Each row of indices represents a cube for which to generate vertices and faces over.
+    //
+    // Input:
+    //  values  #number_of_grid_points x 1 array -- the scalar values of an
+    //    implicit function defined on the grid points (<0 in the inside of the
+    //    surface, 0 on the border, >0 outside)
+    //  points  #number_of_grid_points x 3 array -- 3-D positions of the grid
+    //    points, ordered in x,y,z order:
+    //  indices  #cubes x 8 array -- one row for each cube where each value is
+    //    the index of a vertex in points and a scalar in values.
+    //    i.e. points[indices[i, j]] = the position of the j'th vertex of the i'th cube
+    // Output:
+    //   vertices  #V by 3 list of mesh vertex positions
+    //   faces  #F by 3 list of mesh triangle indices
+    // Note: The winding direction of the cube indices will affect the output winding of the faces
+    //
+    template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
+    IGL_INLINE void marching_cubes(
+      const Eigen::MatrixBase<DerivedValues> &values,
+      const Eigen::MatrixBase<DerivedPoints> &points,
+      const Eigen::MatrixBase<DerivedIndices> &indices,
+      const double isovalue,
+      Eigen::PlainObjectBase<DerivedVertices> &vertices,
+      Eigen::PlainObjectBase<DerivedFaces> &faces);
+
+    // Overload of the above function where the isovalue defaults to 0.0
+    template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
+    IGL_INLINE void marching_cubes(
+      const Eigen::MatrixBase<DerivedValues> &values,
+      const Eigen::MatrixBase<DerivedPoints> &points,
+      const Eigen::MatrixBase<DerivedIndices> &indices,
+      Eigen::PlainObjectBase<DerivedVertices> &vertices,
+      Eigen::PlainObjectBase<DerivedFaces> &faces);
+
   }
 
 }