Răsfoiți Sursa

Tetgen 'extras' directory and makefiles

Former-commit-id: 0521da6f1773d104bbd7f4d06fd7a9248a977809
jalec 13 ani în urmă
părinte
comite
446e7aeb6c

+ 31 - 12
Makefile

@@ -1,26 +1,41 @@
-all: lib examples
-
 .PHONY: all
-.PHONY: examples
+all: lib examples extras
 
-debug: lib
+# Shared flags etc.
+include Makefile.conf
 
-lib: obj lib/libigl.a
+EXTRA_DIRS=
+ifeq ($(IGL_WITH_TETGEN),1)
+	# append tetgen extra dir
+	EXTRA_DIRS+=include/igl/tetgen
+endif
 
+.PHONY: examples
+.PHONY: extras
+debug: lib
+lib: obj lib/libigl.a
 examples:
 	make -C examples
+extras:
+	for p in  $(EXTRA_DIRS); \
+	do \
+	echo "cd $$p" ; \
+	$(MAKE) -C $$p ; \
+	done
 
+
+#############################################################################
+# SOURCE 
+#############################################################################
 CPP_FILES=$(wildcard include/igl/*.cpp)
 OBJ_FILES=$(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o)))
 
 # include igl headers
 INC+=-Iinclude/
 
-CFLAGS += -Wall -arch i386 -arch x86_64
-# optimized default settings
-all: LFLAGS +=
-all: CFLAGS += -O3 -DNDEBUG -j 
-debug: CFLAGS += -g -Wall -Werror
+#############################################################################
+# DEPENDENCIES
+#############################################################################
 
 # Eigen dependency
 EIGEN3_INC=-I/opt/local/include/eigen3 -I/opt/local/include/eigen3/unsupported
@@ -30,7 +45,6 @@ INC+=$(EIGEN3_INC)
 ANTTWEAKBAR_INC=-I/opt/local/include
 INC+=$(ANTTWEAKBAR_INC)
 
-
 ## OpenGL dependency
 #LIB+=-framework OpenGL
 #LIB+=-framework GLUT
@@ -44,9 +58,14 @@ lib/libigl.a: $(OBJ_FILES)
 	ar cqs $@ $(OBJ_FILES)
 
 obj/%.o: include/igl/%.cpp include/igl/%.h
-	g++ $(CFLAGS) -c -o $@ $< $(INC)
+	g++ $(CFLAGS) $(AFLAGS) -c -o $@ $< $(INC)
 
 clean:
 	rm -f obj/*.o
 	rm -f lib/libigl.a
 	make -C examples clean
+	for p in  $(EXTRA_DIRS); \
+	do \
+	echo "cd $$p" ; \
+	$(MAKE) -C $$p clean; \
+	done

+ 12 - 0
Makefile.conf

@@ -0,0 +1,12 @@
+IGL_WITH_TETGEN=1
+
+#############################################################################
+# FLAGS 
+#############################################################################
+AFLAGS += -arch x86_64 -m64
+CFLAGS += -Wall
+# optimized default settings
+all: LFLAGS +=
+all: CFLAGS += -O3 -DNDEBUG -j 
+debug: CFLAGS += -g -Wall -Werror
+

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

@@ -1 +1 @@
-567ada890571088113289960fab13dc0d1fb938e
+5fb18cdaec3ff1d9fee970dc9d3e8852d5d1cf2e

+ 7 - 0
include/igl/tetgen/README

@@ -0,0 +1,7 @@
+IGL interface to tetgen library
+
+Dependencies:
+  tetgen  
+  
+Travel to $IGL/external/tetgen and issue:
+  make -f Makefile.igl tetlib

+ 71 - 0
include/igl/tetgen/mesh_to_tetgenio.cpp

@@ -0,0 +1,71 @@
+#include "mesh_to_tetgenio.h"
+
+// IGL includes 
+#include <igl/matrix_to_list.h>
+
+// STL includes
+#include <cassert>
+
+IGL_INLINE bool igl::mesh_to_tetgenio(
+  const std::vector<std::vector<REAL > > & V, 
+  const std::vector<std::vector<int> > & F, 
+  tetgenio & in)
+{
+  using namespace std;
+  // all indices start from 0
+  in.firstnumber = 0;
+
+  in.numberofpoints = V.size();
+  in.pointlist = new REAL[in.numberofpoints * 3];
+  // loop over points
+  for(int i = 0; i < (int)V.size(); i++)
+  {
+    assert(V[i].size() == 3);
+    in.pointlist[i*3+0] = V[i][0];
+    in.pointlist[i*3+1] = V[i][1];
+    in.pointlist[i*3+2] = V[i][2];
+  }
+
+  in.numberoffacets = F.size();
+  in.facetlist = new tetgenio::facet[in.numberoffacets];
+  in.facetmarkerlist = NULL;
+
+  // loop over face
+  for(int i = 0;i < (int)F.size(); i++)
+  {
+    tetgenio::facet * f = &in.facetlist[i];
+    f->numberofpolygons = 1;
+    f->polygonlist = new tetgenio::polygon[f->numberofpolygons];
+    f->numberofholes = 0;
+    f->holelist = NULL;
+    tetgenio::polygon * p = &f->polygonlist[0];
+    p->numberofvertices = F[i].size();
+    p->vertexlist = new int[p->numberofvertices];
+    // loop around face
+    for(int j = 0;j < (int)F[i].size(); j++)
+    {
+      p->vertexlist[j] = F[i][j];
+    }
+  }
+  return true;
+}
+
+template <typename DerivedV, typename DerivedF>
+IGL_INLINE bool igl::mesh_to_tetgenio(
+  const Eigen::PlainObjectBase<DerivedV>& V,
+  const Eigen::PlainObjectBase<DerivedF>& F,
+  tetgenio & in)
+{
+  using namespace igl;
+  using namespace std;
+  vector<vector<REAL> > vV;
+  vector<vector<int> > vF;
+  matrix_to_list(V,vV);
+  matrix_to_list(F,vF);
+  return mesh_to_tetgenio(vV,vF,in);
+}
+
+#ifndef IGL_HEADER_ONLY
+// Explicit template specialization
+template bool igl::mesh_to_tetgenio<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<int, -1, -1, 0, -1, -1> > const&, tetgenio&);
+#endif

+ 39 - 0
include/igl/tetgen/mesh_to_tetgenio.h

@@ -0,0 +1,39 @@
+#ifndef IGL_MESH_TO_TETGENIO_H
+#define IGL_MESH_TO_TETGENIO_H
+#include "../igl_inline.h"
+
+#include "tetgen.h" // Defined tetgenio, REAL
+#include <vector>
+#include <Eigen/Core>
+
+namespace igl
+{
+  // Load a vertex list and face list into a tetgenio object
+  // Inputs:
+  //   V  #V by 3 vertex position list
+  //   F  #F list of polygon face indices into V (0-indexed)
+  // Outputs:
+  //   in  tetgenio input object
+  // Returns true on success, false on error
+  IGL_INLINE bool mesh_to_tetgenio(
+    const std::vector<std::vector<REAL > > & V, 
+    const std::vector<std::vector<int> > & F, 
+    tetgenio & in);
+  
+  // Wrapper with Eigen types
+  // Templates:
+  //   DerivedV  real-value: i.e. from MatrixXd
+  //   DerivedF  integer-value: i.e. from MatrixXi
+  template <typename DerivedV, typename DerivedF>
+  IGL_INLINE bool mesh_to_tetgenio(
+    const Eigen::PlainObjectBase<DerivedV>& V,
+    const Eigen::PlainObjectBase<DerivedF>& F,
+    tetgenio & in);
+}
+
+
+#ifdef IGL_HEADER_ONLY
+#  include "mesh_to_tetgenio.cpp"
+#endif
+
+#endif

+ 61 - 0
include/igl/tetgen/read_into_tetgenio.cpp

@@ -0,0 +1,61 @@
+#include "read_into_tetgenio.h"
+#include "mesh_to_tetgenio.h"
+
+// IGL includes
+#include <igl/pathinfo.h>
+#ifndef IGL_NO_EIGEN
+#  define IGL_NO_EIGEN_WAS_NOT_ALREADY_DEFINED
+#  define IGL_NO_EIGEN
+#endif 
+// Include igl headers without including Eigen
+#include <igl/readOBJ.h>
+#ifdef IGL_NO_EIGEN_WAS_NOT_ALREADY_DEFINED
+#  undef IGL_NO_EIGEN
+#endif
+
+// STL includes
+#include <algorithm>
+#include <iostream>
+#include <vector>
+
+IGL_INLINE bool igl::read_into_tetgenio(
+  const std::string & path,
+  tetgenio & in)
+{
+  using namespace igl;
+  using namespace std;
+  // get file extension
+  string dirname,basename,ext,filename;
+  pathinfo(path,dirname,basename,ext,filename);
+  // convert to lower case for easy comparison
+  transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
+  bool success = false;
+
+  char basename_char[1024];
+  strcpy(basename_char,basename.c_str());
+
+  if(ext == "obj")
+  {
+    // read obj into vertex list and face list
+    vector<vector<REAL> > V,TC,N;
+    vector<vector<int>  > F,FTC,FN;
+    success = readOBJ(path,V,TC,N,F,FTC,FN);
+    success &= mesh_to_tetgenio(V,F,in);
+  }else if(ext == "off")
+  {
+    success = in.load_off(basename_char);
+  }else if(ext == "node")
+  {
+    success = in.load_node(basename_char);
+  }else 
+  {
+    if(ext.length() > 0)
+    {
+      cerr<<"^read_into_tetgenio Warning: Unsupported extension ("<<ext<<
+        "): try to load as basename..."<<endl;
+    }
+    success = in.load_tetmesh(basename_char);
+  }
+
+  return success;
+}

+ 44 - 0
include/igl/tetgen/read_into_tetgenio.h

@@ -0,0 +1,44 @@
+#ifndef IGL_READ_INTO_TETGENIO_H
+#define IGL_READ_INTO_TETGENIO_H
+#include "../igl_inline.h"
+
+#include <string>
+#include "tetgen.h" // Defined tetgenio, REAL
+
+namespace igl
+{
+  // Read a mesh or point set into tetgenio (input object for calling tetgen).
+  // Many file formats are already supported by tetgen:
+  //   .off
+  //   .ply
+  //   .node
+  //   .ply
+  //   .medit
+  //   .vtk
+  //   etc.
+  // Noteably it does not support .obj which is loaded by hand here (also
+  // demonstrating how to load points/faces programatically)
+  //
+  // If the file extension is not recognized the filename is assumed to be the
+  // basename of a collection describe a tetmesh, (of which at least the .node
+  // file must exist):
+  //   [filename].node
+  //   [filename].ele
+  //   [filename].face
+  //   [filename].edge
+  //   [filename].vol
+  //
+  // Inputs:
+  //   path  path to file or basename to files
+  // Outputs:
+  //   in  tetgenio input object
+  // Returns true on success, false on error
+  IGL_INLINE bool read_into_tetgenio(const std::string & path, tetgenio & in);
+}
+
+
+#ifdef IGL_HEADER_ONLY
+#  include "read_into_tetgenio.cpp"
+#endif
+
+#endif

+ 94 - 0
include/igl/tetgen/tetgenio_to_tetmesh.cpp

@@ -0,0 +1,94 @@
+#include "tetgenio_to_tetmesh.h"
+
+// IGL includes
+#include <igl/list_to_matrix.h>
+
+// STL includes
+#include <iostream>
+
+IGL_INLINE bool igl::tetgenio_to_tetmesh(
+  const tetgenio & out,
+  std::vector<std::vector<REAL > > & V, 
+  std::vector<std::vector<int> > & T)
+{
+  using namespace std;
+  // process points
+  if(out.pointlist == NULL)
+  {
+    cerr<<"^tetgenio_to_tetmesh Error: point list is NULL\n"<<endl;
+    return false;
+  }
+  V.resize(out.numberofpoints,vector<REAL>(3));
+  // loop over points
+  for(int i = 0;i < out.numberofpoints; i++)
+  {
+    V[i][0] = out.pointlist[i*3+0];
+    V[i][1] = out.pointlist[i*3+1];
+    V[i][2] = out.pointlist[i*3+2];
+  }
+
+
+  // process tets
+  if(out.tetrahedronlist == NULL)
+  {
+    cerr<<"^tetgenio_to_tetmesh Error: tet list is NULL\n"<<endl;
+    return false;
+  }
+
+  // When would this not be 4?
+  assert(out.numberofcorners == 4);
+  T.resize(out.numberoftetrahedra,vector<int>(out.numberofcorners));
+  int min_index = 1e7;
+  int max_index = -1e7;
+  // loop over tetrahedra
+  for(int i = 0; i < out.numberoftetrahedra; i++)
+  {
+    for(int j = 0; j<out.numberofcorners; j++)
+    {
+      int index = out.tetrahedronlist[i * out.numberofcorners + j];
+      T[i][j] = index;
+      min_index = (min_index > index ? index : min_index);
+      max_index = (max_index < index ? index : max_index);
+    }
+  }
+  assert(min_index >= 0);
+  assert(max_index >= 0);
+  assert(max_index < V.size());
+
+  return true;
+}
+
+template <typename DerivedV, typename DerivedT>
+IGL_INLINE bool igl::tetgenio_to_tetmesh(
+  const tetgenio & out,
+  Eigen::PlainObjectBase<DerivedV>& V,
+  Eigen::PlainObjectBase<DerivedT>& T)
+{
+  using namespace igl;
+  using namespace std;
+  vector<vector<REAL> > vV;
+  vector<vector<int> > vT;
+  bool success = tetgenio_to_tetmesh(out,vV,vT);
+  if(!success)
+  {
+    return false;
+  }
+  bool V_rect = list_to_matrix(vV,V);
+  if(!V_rect)
+  {
+    // igl::list_to_matrix(vV,V) already printed error message to std err
+    return false;
+  }
+  bool T_rect = list_to_matrix(vT,T);
+  if(!T_rect)
+  {
+    // igl::list_to_matrix(vT,T) already printed error message to std err
+    return false;
+  }
+  return true;
+}
+
+#ifndef IGL_HEADER_ONLY
+// Explicit template specialization
+template bool igl::tetgenio_to_tetmesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(tetgenio const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
+#endif

+ 38 - 0
include/igl/tetgen/tetgenio_to_tetmesh.h

@@ -0,0 +1,38 @@
+#ifndef IGL_TETGENIO_TO_TETMESH_H
+#define IGL_TETGENIO_TO_TETMESH_H
+#include "../igl_inline.h"
+
+#include "tetgen.h" // Defined tetgenio, REAL
+#include <vector>
+#include <Eigen/Core>
+namespace igl
+{
+  // Extract a tetrahedral mesh from a tetgenio object
+  // Inputs:
+  //   out tetgenio output object
+  // Outputs:
+  //   V  #V by 3 vertex position list
+  //   T  #T by 4 list of tetrahedra indices into V
+  // Returns true on success, false on error
+  IGL_INLINE bool tetgenio_to_tetmesh(
+    const tetgenio & out,
+    std::vector<std::vector<REAL > > & V, 
+    std::vector<std::vector<int> > & T);
+  
+  // Wrapper with Eigen types
+  // Templates:
+  //   DerivedV  real-value: i.e. from MatrixXd
+  //   DerivedT  integer-value: i.e. from MatrixXi
+  template <typename DerivedV, typename DerivedT>
+  IGL_INLINE bool tetgenio_to_tetmesh(
+    const tetgenio & out,
+    Eigen::PlainObjectBase<DerivedV>& V,
+    Eigen::PlainObjectBase<DerivedT>& T);
+}
+
+
+#ifdef IGL_HEADER_ONLY
+#  include "tetgenio_to_tetmesh.cpp"
+#endif
+
+#endif