Browse Source

marching cubes example

Former-commit-id: b5ad91e78e0ab851096d114f0fdf9ad70086e7f7
dolga 13 years ago
parent
commit
d6c4ac9b75

+ 22 - 0
examples/marching_cubes/Makefile

@@ -0,0 +1,22 @@
+
+.PHONY: all
+
+all: example
+
+.PHONY: example
+
+igl_lib=../../
+eigen=-I/opt/local/include/eigen3
+
+CFLAGS=-g
+inc=-I$(igl_lib)/include $(eigen)
+lib=-L$(igl_lib)/lib -ligl
+
+example: example.o
+	g++ $(CFLAGS) -o example example.o $(lib)
+
+example.o: example.cpp
+	g++ $(CFLAGS) -c example.cpp -o example.o $(inc)
+clean:
+	rm -f example.o
+	rm -f example

+ 1 - 0
examples/marching_cubes/example.REMOVED.git-id

@@ -0,0 +1 @@
+e770d901d76d378e70a986caf0931763ce22f768

+ 69 - 0
examples/marching_cubes/example.cpp

@@ -0,0 +1,69 @@
+#include <igl/marching_cubes.h>
+
+typedef float ScalarType;
+typedef unsigned IndexType;
+int main(int argc, char * argv[])
+{
+  Eigen::Matrix<ScalarType, 1, 3> bb_min, bb_max;
+  bb_min<<0.,0.,0.;
+  bb_max<<1.,1.,1.;
+  
+  //diagonal
+  Eigen::Matrix<ScalarType, 1, 3> diff = bb_max - bb_min;
+  
+  
+  //center of the sphere
+  Eigen::Matrix<ScalarType, 1, 3> center = 0.5 * (bb_min + bb_max);
+  
+  IndexType xres, yres, zres;
+  xres = yres = zres = 10;
+  ScalarType radius = 0.42;
+
+  //steps in x,y,z direction
+  ScalarType dx = diff[0] / (ScalarType)(xres-1);
+  ScalarType dy = diff[1] / (ScalarType)(yres-1);
+  ScalarType dz = diff[2] / (ScalarType)(zres-1);
+  
+  
+  Eigen::Matrix<ScalarType, Eigen::Dynamic, 3> points(xres*yres*zres,3);
+  Eigen::Matrix<ScalarType, Eigen::Dynamic, 1> values(xres*yres*zres,1);
+  
+  Eigen::Matrix<ScalarType, Eigen::Dynamic, 3> vertices;
+  
+  Eigen::Matrix<IndexType, Eigen::Dynamic, 3> faces;
+
+  std::cerr<<"Sphere -- construct grid"<<std::endl;
+  for (unsigned int x=0; x<xres; ++x)
+    for (unsigned int y=0; y<yres; ++y)
+      for (unsigned int z=0; z<zres; ++z)
+      {
+        int index = x + y*xres + z*xres*yres;
+        points.row(index) = bb_min + 
+        ScalarType(x)*Eigen::Matrix<ScalarType, 1, 3>(dx,0.,0.) + 
+        ScalarType(y)*Eigen::Matrix<ScalarType, 1, 3>(0.,dy,0.) + 
+        ScalarType(z)*Eigen::Matrix<ScalarType, 1, 3>(0.,0.,dz);
+        
+        values[index] = (points.row(index) - center).squaredNorm() - radius*radius;
+      }
+  
+  
+  std::cerr<<"Sphere -- marching cubes"<<std::endl;
+  igl::marching_cubes(values, 
+                      points, 
+                      xres, 
+                      yres, 
+                      zres, 
+                      vertices,
+                      faces);
+
+  std::cerr<<"Sphere -- saving"<<std::endl;
+  FILE * fid = fopen("sphere.obj","w");
+  for (unsigned i = 0; i<vertices.rows(); ++i)
+    fprintf(fid,"v %.10g %.10g %.10g\n",vertices(i,0),vertices(i,1),vertices(i,2));
+  for (unsigned i = 0; i<faces.rows(); ++i)
+    fprintf(fid,"f %d %d %d\n",faces(i,0)+1,faces(i,1)+1,faces(i,2)+1);
+  fclose(fid);
+
+  std::cerr<<"Sphere -- done."<<std::endl;
+  return 0;
+}