瀏覽代碼

GCC - fixed templating for marching cubes
VS2015 - removed unused code in serializer


Former-commit-id: 55a3de6b754ae7f46bf37478971e0b9f6699c087

Daniele Panozzo 9 年之前
父節點
當前提交
7c00055404

+ 79 - 82
include/igl/copyleft/marching_cubes.cpp

@@ -4,7 +4,7 @@
  *        Copyright (C) 2002 by Computer Graphics Group, RWTH Aachen         *
  *                         www.rwth-graphics.de                              *
  *                                                                           *
- *---------------------------------------------------------------------------* 
+ *---------------------------------------------------------------------------*
  *                                                                           *
  *                                License                                    *
  *                                                                           *
@@ -31,58 +31,59 @@ extern const int edgeTable[256];
 extern const int triTable[256][2][17];
 extern const int polyTable[8][16];
 
-class EdgeKey 
+class EdgeKey
 {
 public:
-  
+
   EdgeKey(unsigned i0, unsigned i1) {
     if (i0 < i1)  { i0_ = i0;  i1_ = i1; }
     else            { i0_ = i1;  i1_ = i0; }
   }
-  
-  bool operator<(const EdgeKey& _rhs) const 
+
+  bool operator<(const EdgeKey& _rhs) const
   {
     if (i0_ != _rhs.i0_)
       return (i0_ < _rhs.i0_);
     else
       return (i1_ < _rhs.i1_);
   }
-  
+
 private:
   unsigned i0_, i1_;
 };
 
 
-template <typename DerivedV, typename DerivedF>
+template <typename DerivedV1, typename DerivedV2, typename DerivedF>
 class MarchingCubes
 {
-  typedef Eigen::PlainObjectBase<DerivedV> PointMatrixType;
-  typedef Eigen::PlainObjectBase<DerivedF> FaceMatrixType;
   typedef std::map<EdgeKey, unsigned>  MyMap;
   typedef typename MyMap::const_iterator   MyMapIterator;
-  
+
 public:
-  MarchingCubes(const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &values,
-                const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> &points,
+  MarchingCubes(
+                const Eigen::PlainObjectBase<DerivedV1> &values,
+                const Eigen::PlainObjectBase<DerivedV2> &points,
                 const unsigned x_res,
                 const unsigned y_res,
                 const unsigned z_res,
-                PointMatrixType &vertices,
-                FaceMatrixType &faces)
+                Eigen::PlainObjectBase<DerivedV2> &vertices,
+                Eigen::PlainObjectBase<DerivedF> &faces)
   {
-    
+    assert(values.cols() == 1);
+    assert(points.cols() == 3);
+
     if(x_res <2 || y_res<2 ||z_res<2)
       return;
     faces.resize(10000,3);
     int num_faces = 0;
-    
+
     vertices.resize(10000,3);
     int num_vertices = 0;
-    
-    
+
+
     unsigned n_cubes  = (x_res-1) * (y_res-1) * (z_res-1);
     assert(unsigned(points.rows()) == x_res * y_res * z_res);
-    
+
     unsigned int         offsets_[8];
     offsets_[0] = 0;
     offsets_[1] = 1;
@@ -92,16 +93,16 @@ public:
     offsets_[5] = 1         + x_res*y_res;
     offsets_[6] = 1 + x_res + x_res*y_res;
     offsets_[7] =     x_res + x_res*y_res;
-    
+
     for (unsigned cube_it =0 ; cube_it < n_cubes; ++cube_it)
     {
-      
+
       unsigned         corner[8];
       typename DerivedF::Scalar samples[12];
       unsigned char    cubetype(0);
       unsigned int     i;
-      
-      
+
+
       // get point indices of corner vertices
       for (i=0; i<8; ++i)
       {
@@ -111,137 +112,133 @@ public:
         unsigned int x = _idx % X;  _idx /= X;
         unsigned int y = _idx % Y;  _idx /= Y;
         unsigned int z = _idx;
-        
+
         // transform to point coordinates
         _idx = x + y*x_res + z*x_res*y_res;
-        
+
         // add offset
         corner[i] = _idx + offsets_[i];
       }
-      
-      
+
+
       // determine cube type
       for (i=0; i<8; ++i)
         if (values[corner[i]] > 0.0)
           cubetype |= (1<<i);
-      
-      
+
+
       // trivial reject ?
       if (cubetype == 0 || cubetype == 255)
         continue;
-      
-      
+
+
       // compute samples on cube's edges
-      if (edgeTable[cubetype]&1)  
+      if (edgeTable[cubetype]&1)
         samples[0]  = add_vertex(values, points, corner[0], corner[1], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&2)  
+      if (edgeTable[cubetype]&2)
         samples[1]  = add_vertex(values, points, corner[1], corner[2], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&4)  
+      if (edgeTable[cubetype]&4)
         samples[2]  = add_vertex(values, points, corner[3], corner[2], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&8)  
+      if (edgeTable[cubetype]&8)
         samples[3]  = add_vertex(values, points, corner[0], corner[3], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&16) 
+      if (edgeTable[cubetype]&16)
         samples[4]  = add_vertex(values, points, corner[4], corner[5], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&32)  
+      if (edgeTable[cubetype]&32)
         samples[5]  = add_vertex(values, points, corner[5], corner[6], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&64)  
+      if (edgeTable[cubetype]&64)
         samples[6]  = add_vertex(values, points, corner[7], corner[6], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&128) 
+      if (edgeTable[cubetype]&128)
         samples[7]  = add_vertex(values, points, corner[4], corner[7], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&256) 
+      if (edgeTable[cubetype]&256)
         samples[8]  = add_vertex(values, points, corner[0], corner[4], vertices, num_vertices, edge2vertex);
-      if (edgeTable[cubetype]&512) 
+      if (edgeTable[cubetype]&512)
         samples[9]  = add_vertex(values, points, corner[1], corner[5], vertices, num_vertices, edge2vertex);
       if (edgeTable[cubetype]&1024)
         samples[10] = add_vertex(values, points, corner[2], corner[6], vertices, num_vertices, edge2vertex);
       if (edgeTable[cubetype]&2048)
         samples[11] = add_vertex(values, points, corner[3], corner[7], vertices, num_vertices, edge2vertex);
-      
-      
-      
+
+
+
       // connect samples by triangles
       for (i=0; triTable[cubetype][0][i] != -1; i+=3 )
       {
         num_faces++;
         if (num_faces > faces.rows())
           faces.conservativeResize(faces.rows()+10000, Eigen::NoChange);
-        
-        faces.row(num_faces-1) << 
+
+        faces.row(num_faces-1) <<
         samples[triTable[cubetype][0][i  ]],
         samples[triTable[cubetype][0][i+1]],
         samples[triTable[cubetype][0][i+2]];
-        
+
       }
-      
+
     }
-    
+
     vertices.conservativeResize(num_vertices, Eigen::NoChange);
     faces.conservativeResize(num_faces, Eigen::NoChange);
-    
+
   };
-  
-  static typename DerivedF::Scalar  add_vertex(const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &values,
-                                               const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> &points,
+
+  static typename DerivedF::Scalar  add_vertex(const Eigen::PlainObjectBase<DerivedV1> &values,
+                                               const Eigen::PlainObjectBase<DerivedV2> &points,
                                                unsigned int i0,
                                                unsigned int i1,
-                                               PointMatrixType &vertices,
+                                               Eigen::PlainObjectBase<DerivedV2> &vertices,
                                                int &num_vertices,
                                                MyMap &edge2vertex)
   {
     // find vertex if it has been computed already
     MyMapIterator it = edge2vertex.find(EdgeKey(i0, i1));
-    if (it != edge2vertex.end()) 
+    if (it != edge2vertex.end())
       return it->second;
     ;
-    
+
     // generate new vertex
-    const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> & p0 = points.row(i0);
-    const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> & p1 = points.row(i1);
-    
-    typename DerivedV::Scalar s0 = fabs(values[i0]);
-    typename DerivedV::Scalar s1 = fabs(values[i1]);
-    typename DerivedV::Scalar t  = s0 / (s0+s1);
-    
-    
+    const Eigen::Matrix<typename DerivedV2::Scalar, 1, 3> & p0 = points.row(i0);
+    const Eigen::Matrix<typename DerivedV2::Scalar, 1, 3> & p1 = points.row(i1);
+
+    typename DerivedV1::Scalar s0 = fabs(values[i0]);
+    typename DerivedV1::Scalar s1 = fabs(values[i1]);
+    typename DerivedV1::Scalar t  = s0 / (s0+s1);
+
+
     num_vertices++;
     if (num_vertices > vertices.rows())
       vertices.conservativeResize(vertices.rows()+10000, Eigen::NoChange);
-    
+
     vertices.row(num_vertices-1)  = (1.0f-t)*p0 + t*p1;
     edge2vertex[EdgeKey(i0, i1)] = num_vertices-1;
-    
+
     return num_vertices-1;
   }
   ;
-  
+
   // maps an edge to the sample vertex generated on it
   MyMap  edge2vertex;
 };
 
 
-template <typename DerivedV, typename DerivedF>
+template <typename DerivedV1, typename DerivedV2, typename DerivedF>
 IGL_INLINE void igl::copyleft::marching_cubes(
-  const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &values,
-  const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> &points,
+  const Eigen::PlainObjectBase<DerivedV1> &values,
+  const Eigen::PlainObjectBase<DerivedV2> &points,
   const unsigned x_res,
   const unsigned y_res,
   const unsigned z_res,
-  Eigen::PlainObjectBase<DerivedV> &vertices,
+  Eigen::PlainObjectBase<DerivedV2> &vertices,
   Eigen::PlainObjectBase<DerivedF> &faces)
 {
-  MarchingCubes<DerivedV, DerivedF> mc(values, 
-                                       points, 
-                                       x_res, 
-                                       y_res, 
-                                       z_res, 
-                                       vertices, 
+  MarchingCubes<DerivedV1, DerivedV2, DerivedF> mc(values,
+                                       points,
+                                       x_res,
+                                       y_res,
+                                       z_res,
+                                       vertices,
                                        faces);
 }
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template specialization
-template void igl::copyleft::marching_cubes<Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
-template void igl::copyleft::marching_cubes<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::Matrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
-template void igl::copyleft::marching_cubes<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 0, -1, 3> >(Eigen::Matrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 0, -1, 3> >&);
-template void igl::copyleft::marching_cubes<Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 0, -1, 3> >(Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 0, -1, 3> >&);
-template void igl::copyleft::marching_cubes<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::Matrix<Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
+template void igl::copyleft::marching_cubes<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
 #endif

+ 10 - 10
include/igl/copyleft/marching_cubes.h

@@ -1,16 +1,16 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #ifndef IGL_COPYLEFT_MARCHINGCUBES_H
 #define IGL_COPYLEFT_MARCHINGCUBES_H
 #include "../igl_inline.h"
 
 #include <Eigen/Core>
-namespace igl 
+namespace igl
 {
   namespace copyleft
   {
@@ -26,7 +26,7 @@ namespace igl
     //  points  #number_of_grid_points x 3 array -- 3-D positions of the grid
     //    points, ordered in x,y,z order:
     //      points[index] = the point at (x,y,z) where :
-    //      x = (index % (xres -1), 
+    //      x = (index % (xres -1),
     //      y = (index / (xres-1)) %(yres-1),
     //      z = index / (xres -1) / (yres -1) ).
     //      where x,y,z index x, y, z dimensions
@@ -38,14 +38,14 @@ namespace igl
     //   vertices  #V by 3 list of mesh vertex positions
     //   faces  #F by 3 list of mesh triangle indices
     //
-    template <typename DerivedV, typename DerivedF>
+    template <typename DerivedV1, typename DerivedV2, typename DerivedF>
       IGL_INLINE void marching_cubes(
-        const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &values,
-        const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> &points,
+        const Eigen::PlainObjectBase<DerivedV1> &values,
+        const Eigen::PlainObjectBase<DerivedV2> &points,
         const unsigned x_res,
         const unsigned y_res,
         const unsigned z_res,
-        Eigen::PlainObjectBase<DerivedV> &vertices,
+        Eigen::PlainObjectBase<DerivedV2> &vertices,
         Eigen::PlainObjectBase<DerivedF> &faces);
   }
 }

+ 0 - 0
include/igl/xml/serialization_test.cpp → include/igl/xml/serialization_test.skip


+ 1 - 1
tutorial/705_MarchingCubes/main.cpp

@@ -65,7 +65,7 @@ int main(int argc, char * argv[])
 )";
   igl::viewer::Viewer viewer;
   viewer.data.set_mesh(SV,SF);
-  viewer.callback_key_down = 
+  viewer.callback_key_down =
     [&](igl::viewer::Viewer & viewer, unsigned char key, int mod)->bool
     {
       switch(key)