diff --git a/Common/CommandLine.cpp b/Common/CommandLine.cpp
index 000fbdd138fcfcee34bf22393e7db08cd0f688df..252f1d44c9f344ebdc9340f0d1083ecf26feebe4 100644
--- a/Common/CommandLine.cpp
+++ b/Common/CommandLine.cpp
@@ -1,4 +1,4 @@
-// $Id: CommandLine.cpp,v 1.60 2005-06-29 10:02:55 tardieu Exp $
+// $Id: CommandLine.cpp,v 1.61 2005-07-03 08:02:23 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -70,8 +70,7 @@ void Print_Usage(char *name){
   Msg(DIRECT, "  -saveall              Save all elements (discard physical group definitions)");
   Msg(DIRECT, "  -o file               Specify mesh output file name");
   Msg(DIRECT, "  -format string        Set output mesh format (msh, unv, gref, p3d)");
-  Msg(DIRECT, "  -algo string          Select mesh algorithm (iso, tri, aniso, netgen)");
-//  Msg(DIRECT, "  -algo string          Select mesh algorithm (iso, tri, aniso, netgen, tetgen)");
+  Msg(DIRECT, "  -algo string          Select mesh algorithm (iso, tri, aniso, netgen, tetgen)");
   Msg(DIRECT, "  -smooth int           Set number of mesh smoothing steps");
   Msg(DIRECT, "  -optimize             Optimize quality of tetrahedral elements");
   Msg(DIRECT, "  -order int            Set mesh order (1, 2)");
@@ -126,9 +125,9 @@ char *Get_BuildOptions(void)
 #if defined(HAVE_NETGEN)
     strcat(opt, "NETGEN ");
 #endif
-//#if defined(HAVE_TETGEN)
-//    strcat(opt, "TETGEN ");
-//#endif
+#if defined(HAVE_TETGEN)
+    strcat(opt, "TETGEN ");
+#endif
 #if defined(HAVE_LIBJPEG)
     strcat(opt, "JPEG ");
 #endif
@@ -412,8 +411,8 @@ void Get_Options(int argc, char *argv[])
             CTX.mesh.algo2d = DELAUNAY_ANISO;
           else if(!strncmp(argv[i], "netgen", 6))
             CTX.mesh.algo3d = FRONTAL_NETGEN;
-//          else if(!strncmp(argv[i], "tetgen", 6))
-//            CTX.mesh.algo3d = DELAUNAY_TETGEN;
+          else if(!strncmp(argv[i], "tetgen", 6))
+            CTX.mesh.algo3d = DELAUNAY_TETGEN;
           else {
             fprintf(stderr, ERROR_STR "Unknown mesh algorithm\n");
             exit(1);
diff --git a/Common/DefaultOptions.h b/Common/DefaultOptions.h
index 65692c55f481be76974183663ccd069e75ece027..d379159035bff9912e714c9312d1936f009b0674 100644
--- a/Common/DefaultOptions.h
+++ b/Common/DefaultOptions.h
@@ -834,8 +834,7 @@ StringXNumber MeshOptions_Number[] = {
   { F|O, "Algorithm" , opt_mesh_algo2d , DELAUNAY_ISO ,
     "2D mesh algorithm (1=isotropic, 2=anisotropic, 3=triangle)" }, 
   { F|O, "Algorithm3D" , opt_mesh_algo3d , DELAUNAY_ISO ,
-    "3D mesh algorithm (1=isotropic, 4=netgen)" }, 
-//    "3D mesh algorithm (1=isotropic, 4=netgen, 5=tetgen)" }, 
+    "3D mesh algorithm (1=isotropic, 4=netgen, 5=tetgen)" }, 
   { F,   "AllowDegeneratedExtrude" , opt_mesh_allow_degenerated_extrude , 0. , 
     "Allow the generation of degenerated hexahedra or prisms during extrusion" },
   { F|O, "AngleSmoothNormals" , opt_mesh_angle_smooth_normals , 180.0 ,
diff --git a/Common/Options.cpp b/Common/Options.cpp
index 42673d7526b68e9b336f06cdf02eb10b7438a4a0..e685fb8509d7641543d5d0062adab1c574048501 100644
--- a/Common/Options.cpp
+++ b/Common/Options.cpp
@@ -1,4 +1,4 @@
-// $Id: Options.cpp,v 1.248 2005-06-29 10:02:55 tardieu Exp $
+// $Id: Options.cpp,v 1.249 2005-07-03 08:02:23 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -4431,8 +4431,7 @@ double opt_mesh_algo3d(OPT_ARGS_NUM)
 {
   if(action & GMSH_SET){
     int algo = (int)val;
-    if(algo != DELAUNAY_ISO && algo != FRONTAL_NETGEN){
-//    if(algo != DELAUNAY_ISO && algo != FRONTAL_NETGEN && algo != DELAUNAY_TETGEN){
+    if(algo != DELAUNAY_ISO && algo != FRONTAL_NETGEN && algo != DELAUNAY_TETGEN){
       Msg(WARNING, "Unknown mesh algorithm: keeping existing value");
     }
     else{
@@ -4442,16 +4441,16 @@ double opt_mesh_algo3d(OPT_ARGS_NUM)
 #if defined(HAVE_FLTK)
   if(WID && (action & GMSH_GUI)) {
     switch (CTX.mesh.algo3d) {
-    case DELAUNAY_ISO:
-      WID->mesh_choice[3]->value(0);
-      break;
     case FRONTAL_NETGEN:
-    default:
       WID->mesh_choice[3]->value(1);
       break;
-//    case DELAUNAY_TETGEN:
-//      WID->mesh_choice[3]->value(2);
-//      break;
+    case DELAUNAY_TETGEN:
+      WID->mesh_choice[3]->value(2);
+      break;
+    case DELAUNAY_ISO:
+    default:
+      WID->mesh_choice[3]->value(0);
+      break;
     }
   }
 #endif
diff --git a/DataStr/Tree.h b/DataStr/Tree.h
index 05cabc68c4033d9cd0531fdb18cb8e059eb71acd..99d3751bdec4656bd2eb82437f22a79731b98c9a 100644
--- a/DataStr/Tree.h
+++ b/DataStr/Tree.h
@@ -53,6 +53,13 @@ inline void Tree_Action(Tree_T *tree, void (*action) (void *data, void *dummy))
   avl_foreach(tree->root, action, AVL_FORWARD);
 }
 
+inline void Tree_Action_Inverse(Tree_T *tree, void (*action) (void *data, void *dummy))
+{
+  if(!tree) return;
+
+  avl_foreach(tree->root, action, AVL_BACKWARD);
+}
+
 
 #endif
 
diff --git a/Fltk/Callbacks.cpp b/Fltk/Callbacks.cpp
index b36a1f51324561edb9fe6f889e1e830b6449037f..311f5b4d32bc58945430e84f9722a6b648cac777 100644
--- a/Fltk/Callbacks.cpp
+++ b/Fltk/Callbacks.cpp
@@ -1,4 +1,4 @@
-// $Id: Callbacks.cpp,v 1.358 2005-06-29 10:02:55 tardieu Exp $
+// $Id: Callbacks.cpp,v 1.359 2005-07-03 08:02:23 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -1080,11 +1080,8 @@ void mesh_options_ok_cb(CALLBACK_ARGS)
 		  (WID->mesh_choice[5]->value() == 0) ? 1 : 2);
   opt_mesh_algo3d(0, GMSH_SET,
 		  (WID->mesh_choice[3]->value() == 0) ? DELAUNAY_ISO :
-		  FRONTAL_NETGEN);
-//  opt_mesh_algo3d(0, GMSH_SET,
-//		  (WID->mesh_choice[3]->value() == 0) ? DELAUNAY_ISO :
-//		  (WID->mesh_choice[3]->value() == 1) ? FRONTAL_NETGEN :
-//		  DELAUNAY_TETGEN);
+		  (WID->mesh_choice[3]->value() == 1) ? FRONTAL_NETGEN :
+		  DELAUNAY_TETGEN);
   opt_mesh_color_carousel(0, GMSH_SET, WID->mesh_choice[4]->value());
   opt_mesh_quality_type(0, GMSH_SET, WID->mesh_choice[6]->value());
 }
diff --git a/Fltk/GUI.cpp b/Fltk/GUI.cpp
index aae188936b25a4f47f78a9fe93bf331b41710c5f..9f2726458ae54ac0dee0c3230d53c304d54a014d 100644
--- a/Fltk/GUI.cpp
+++ b/Fltk/GUI.cpp
@@ -1,4 +1,4 @@
-// $Id: GUI.cpp,v 1.445 2005-06-29 10:02:55 tardieu Exp $
+// $Id: GUI.cpp,v 1.446 2005-07-03 08:02:23 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -2214,7 +2214,7 @@ void GUI::create_option_window()
       static Fl_Menu_Item menu_3d_algo[] = {
         {"Isotropic", 0, 0, 0},
         {"Netgen", 0, 0, 0},
-//        {"Tetgen", 0, 0, 0},
+        {"Tetgen", 0, 0, 0},
         {0}
       };
 
diff --git a/Fltk/Makefile b/Fltk/Makefile
index 1d832cfd4b8648b2d97b17dedb2a561a72a9fc4c..98fe1f830f1e9e72cb130210cdb34f2754ef914f 100644
--- a/Fltk/Makefile
+++ b/Fltk/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.70 2005-06-03 17:32:29 geuzaine Exp $
+# $Id: Makefile,v 1.71 2005-07-03 08:02:24 geuzaine Exp $
 #
 # Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 #
@@ -124,14 +124,14 @@ GUI_Extras.o: GUI_Extras.cpp ../Common/Gmsh.h ../Common/Message.h \
   File_Picker.h Shortcut_Window.h ../Graphics/CreateFile.h \
   ../Common/Options.h ../Common/Context.h
 # 1 "/Users/geuzaine/.gmsh/Fltk//"
-Callbacks.o: Callbacks.cpp ../Common/Gmsh.h ../Common/Message.h \
-  ../DataStr/Malloc.h ../DataStr/List.h ../DataStr/Tree.h \
-  ../DataStr/avl.h ../DataStr/Tools.h ../DataStr/List.h ../DataStr/Tree.h \
-  ../Common/GmshUI.h ../Geo/Geo.h ../Geo/CAD.h ../Mesh/Mesh.h \
-  ../Mesh/Vertex.h ../Mesh/Element.h ../Mesh/Vertex.h ../Mesh/Simplex.h \
-  ../Mesh/Vertex.h ../Mesh/Element.h ../Mesh/Face.h ../Mesh/Vertex.h \
-  ../Mesh/Element.h ../Mesh/Edge.h ../Mesh/Vertex.h ../Mesh/Simplex.h \
-  ../Geo/ExtrudeParams.h ../Common/VertexArray.h \
+Callbacks.o: Callbacks.cpp ../Mesh/BDS.h ../Common/Gmsh.h \
+  ../Common/Message.h ../DataStr/Malloc.h ../DataStr/List.h \
+  ../DataStr/Tree.h ../DataStr/avl.h ../DataStr/Tools.h ../DataStr/List.h \
+  ../DataStr/Tree.h ../Common/GmshUI.h ../Geo/Geo.h ../Geo/CAD.h \
+  ../Mesh/Mesh.h ../Mesh/Vertex.h ../Mesh/Element.h ../Mesh/Vertex.h \
+  ../Mesh/Simplex.h ../Mesh/Vertex.h ../Mesh/Element.h ../Mesh/Face.h \
+  ../Mesh/Vertex.h ../Mesh/Element.h ../Mesh/Edge.h ../Mesh/Vertex.h \
+  ../Mesh/Simplex.h ../Geo/ExtrudeParams.h ../Common/VertexArray.h \
   ../Common/SmoothNormals.h ../Mesh/Metric.h ../Mesh/Vertex.h \
   ../Mesh/Simplex.h ../Mesh/Mesh.h ../Mesh/Matrix.h \
   ../Geo/ExtrudeParams.h ../Geo/ExtractContour.h ../Graphics/Draw.h \
diff --git a/Makefile b/Makefile
index 4f36d13790a5e06b1cc199e7ac6fc4ee774e7ab6..f046e2fd07b4a2507b49186b4c42b3ca235ce2b4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.393 2005-06-17 20:29:07 geuzaine Exp $
+# $Id: Makefile,v 1.394 2005-07-03 08:02:23 geuzaine Exp $
 #
 # Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 #
@@ -144,13 +144,15 @@ source-common:
 
 source: source-common
 	cd gmsh-${GMSH_VERSION} && rm -rf CVS */CVS */*/CVS */*/*/CVS */.globalrc\
-          NR Triangle/triangle.* utils/commercial ${GMSH_VERSION_FILE} 
+          NR Triangle/triangle.* Tetgen/tetgen.* Tetgen/predicates.*\
+          utils/commercial ${GMSH_VERSION_FILE} 
 	tar zcvf gmsh-${GMSH_VERSION}-source.tgz gmsh-${GMSH_VERSION}
 
 source-commercial: source-common
 	cd gmsh-${GMSH_VERSION} && rm -rf CVS */CVS */*/CVS */*/*/CVS */.globalrc\
-          MathEval Triangle/triangle.* Netgen/libsrc TODO *.spec doc/gmsh.html doc/FAQ\
-          doc/README.cvs utils/commercial ${GMSH_VERSION_FILE}
+          MathEval Triangle/triangle.* Tetgen/tetgen.* Tetgen/predicates.* Netgen/libsrc\
+          TODO *.spec doc/gmsh.html doc/FAQ doc/README.cvs\
+          utils/commercial ${GMSH_VERSION_FILE}
 	cp -f utils/commercial/README gmsh-${GMSH_VERSION}/README
 	cp -f utils/commercial/LICENSE gmsh-${GMSH_VERSION}/doc/LICENSE
 	cp -f utils/commercial/License.cpp gmsh-${GMSH_VERSION}/Common/License.cpp
diff --git a/Mesh/3D_Mesh.cpp b/Mesh/3D_Mesh.cpp
index b5595be13c484f620fe5e4b378547e3127e71a29..73ccfd1cee118ef76417c950322d22baf7fd24cf 100644
--- a/Mesh/3D_Mesh.cpp
+++ b/Mesh/3D_Mesh.cpp
@@ -1,4 +1,4 @@
-// $Id: 3D_Mesh.cpp,v 1.66 2005-06-29 10:02:55 tardieu Exp $
+// $Id: 3D_Mesh.cpp,v 1.67 2005-07-03 08:02:24 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -875,11 +875,10 @@ void Maillage_Volume(void *data, void *dum)
   }
   else if((v->Typ != 99999) && Mesh_Netgen(v)) {
   }
-//  else if((v->Typ != 99999) && Mesh_Tetgen(v)) {
-//  }
-//  else if((v->Typ == 99999) && (CTX.mesh.algo3d != FRONTAL_NETGEN)
-//                            && (CTX.mesh.algo3d != DELAUNAY_TETGEN)) {
-  else if((v->Typ == 99999) && (CTX.mesh.algo3d != FRONTAL_NETGEN)) {
+  else if((v->Typ != 99999) && Mesh_Tetgen(v)) {
+  }
+  else if((v->Typ == 99999) && (CTX.mesh.algo3d != FRONTAL_NETGEN)
+                            && (CTX.mesh.algo3d != DELAUNAY_TETGEN)) {
     
     Simplexes_New = List_Create(10, 10, sizeof(Simplex *));
     Simplexes_Destroyed = List_Create(10, 10, sizeof(Simplex *));
diff --git a/Mesh/3D_Mesh_Tetgen.cpp b/Mesh/3D_Mesh_Tetgen.cpp
index b0326a2a10c7e8c726c05d377a08adb5b9e9af0f..2b35966d700228e604af4e66b163782fcf0d5c2f 100644
--- a/Mesh/3D_Mesh_Tetgen.cpp
+++ b/Mesh/3D_Mesh_Tetgen.cpp
@@ -1,3 +1,27 @@
+// $Id: 3D_Mesh_Tetgen.cpp,v 1.2 2005-07-03 08:02:24 geuzaine Exp $
+//
+// Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+// USA.
+// 
+// Please report all bugs and problems to <gmsh@geuz.org>.
+//
+// Contributor(s):
+//   Nicolas Tardieu
+//
 
 #include "Gmsh.h"
 #include "Geo.h"
@@ -11,7 +35,7 @@ extern Mesh *THEM;
 
 #if !defined(HAVE_TETGEN)
 
-int Tetgen(Volume * v)
+int Mesh_Tetgen(Volume * v)
 {
   if(CTX.mesh.algo3d == DELAUNAY_TETGEN)
     Msg(GERROR, "Tetgen is not compiled in this version of Gmsh");
@@ -22,7 +46,6 @@ int Tetgen(Volume * v)
 
 #include "tetgen.h"
 
-
 class Tetgen{
  private:
   List_T *_surverts, *_volverts;
@@ -258,6 +281,9 @@ int Mesh_Tetgen(Volume * v)
   if(CTX.mesh.algo3d != DELAUNAY_TETGEN)
     return 0;
 
+  Msg(GERROR, "Tetgen is not ready yet!");
+  return 0;
+
   if(THEM->BGM.Typ == ONFILE){
     Msg(GERROR, "Tetgen is not ready to be used with a background mesh");
     return 0;
@@ -271,6 +297,4 @@ int Mesh_Tetgen(Volume * v)
   return 1;
 }
 
-
-
-#endif // !HAVE_NETGEN
+#endif // !HAVE_TETGEN
diff --git a/Mesh/Makefile b/Mesh/Makefile
index 08441e20011466d6720c8a068be0ec377863740e..00f8514bc6283bde2cfbc7a2a4b51f0e2cd162b4 100644
--- a/Mesh/Makefile
+++ b/Mesh/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.88 2005-06-10 20:59:15 geuzaine Exp $
+# $Id: Makefile,v 1.89 2005-07-03 08:02:24 geuzaine Exp $
 #
 # Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 #
@@ -23,7 +23,7 @@ include ../variables
 
 LIB     = ../lib/libGmshMesh.a
 INCLUDE = -I../Numeric -I../NR -I../Common -I../DataStr -I../Geo -I../Mesh\
-          -I../Graphics -I../Parser -I../Fltk -I../Triangle\
+          -I../Graphics -I../Parser -I../Fltk -I../Triangle -I../Tetgen\
           -I../Netgen -I../Netgen/libsrc/include  -I../Netgen/libsrc/interface
 CFLAGS  = ${OPTIM} ${FLAGS} ${INCLUDE}
 
@@ -53,6 +53,7 @@ SRC = 1D_Mesh.cpp \
         3D_Divide.cpp \
         3D_Bricks.cpp \
         3D_Mesh_Netgen.cpp \
+        3D_Mesh_Tetgen.cpp \
       BDS.cpp \
       MeshQuality.cpp \
       Create.cpp \
@@ -329,7 +330,16 @@ depend:
   ../Common/SmoothNormals.h Metric.h Matrix.h Create.h \
   ../Numeric/Numeric.h ../Common/Context.h
 # 1 "/Users/geuzaine/.gmsh/Mesh//"
-BDS.o: BDS.cpp BDS.h ../Numeric/Numeric.h
+3D_Mesh_Tetgen.o: 3D_Mesh_Tetgen.cpp ../Common/Gmsh.h ../Common/Message.h \
+  ../DataStr/Malloc.h ../DataStr/List.h ../DataStr/Tree.h \
+  ../DataStr/avl.h ../DataStr/Tools.h ../DataStr/List.h ../DataStr/Tree.h \
+  ../Geo/Geo.h Mesh.h Vertex.h Element.h Simplex.h Face.h Edge.h \
+  ../Geo/ExtrudeParams.h ../Common/VertexArray.h \
+  ../Common/SmoothNormals.h Metric.h Matrix.h Create.h \
+  ../Numeric/Numeric.h ../Common/Context.h
+# 1 "/Users/geuzaine/.gmsh/Mesh//"
+BDS.o: BDS.cpp BDS.h ../Numeric/Numeric.h ../Common/GmshMatrix.h \
+  ../Numeric/EigSolve.h
 # 1 "/Users/geuzaine/.gmsh/Mesh//"
 MeshQuality.o: MeshQuality.cpp ../Common/Gmsh.h ../Common/Message.h \
   ../DataStr/Malloc.h ../DataStr/List.h ../DataStr/Tree.h \
diff --git a/Mesh/Mesh.h b/Mesh/Mesh.h
index 980252367fc28a1c6826029d2191272c05580c84..a9276a4d5bebd3ba7ff31b25b08207fee55c59e3 100644
--- a/Mesh/Mesh.h
+++ b/Mesh/Mesh.h
@@ -395,7 +395,7 @@ typedef struct{
 #define DELAUNAY_ANISO 2
 #define DELAUNAY_TRIANGLE 3
 #define FRONTAL_NETGEN 4
-//#define DELAUNAY_TETGEN 5
+#define DELAUNAY_TETGEN 5
 
 struct _Mesh{
   char name[256];
@@ -475,7 +475,7 @@ int AlgorithmeMaillage2DAnisotropeModeJF(Surface *s);
 void Maillage_Automatique_VieuxCode(Surface *pS, Mesh *m, int ori);
 int Mesh_Triangle(Surface *s);
 int Mesh_Netgen(Volume *v);
-//int Mesh_Tetgen(Volume *v);
+int Mesh_Tetgen(Volume *v);
 void Optimize_Netgen(Volume *v);
 void Optimize_Netgen(Mesh *m);
 
diff --git a/Mesh/Print_Mesh.cpp b/Mesh/Print_Mesh.cpp
index 40a096897eb28ee33d968c0e360fd4c99b2fd9c2..49e68e033d264b74b2d8a54e5b5069a5415e1ad8 100644
--- a/Mesh/Print_Mesh.cpp
+++ b/Mesh/Print_Mesh.cpp
@@ -1,4 +1,4 @@
-// $Id: Print_Mesh.cpp,v 1.62 2005-06-25 04:05:40 geuzaine Exp $
+// $Id: Print_Mesh.cpp,v 1.63 2005-07-03 08:02:24 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -1623,7 +1623,6 @@ int _p3d_cmp_entities(const void *a, const void *b)
   return (*e1)->iEnt - (*e2)->iEnt;
 }
 
-
 int _p3d_cmp_surf_num(const void *a, const void *b)
 {
   Surface **e1 = (Surface **) a;
@@ -1674,7 +1673,6 @@ void _p3d_print_hex(List_T *ListHex, int Nu, int Nv, int Nw)
   double coord;
   Hexahedron *pH;
 
-  
   for (c = 0; c < 3; c++) {
     for (k = 0; k < (Nu - 1); k++) {
       for (j = 0; j < (Nv - 1); j++) {
diff --git a/Mesh/Simplex.cpp b/Mesh/Simplex.cpp
index 306acbfbdffe62153545fcfa846209c909e2ed20..0fb506c7fcfc9ac8becdd5922ed5da3e9d7e5c3b 100644
--- a/Mesh/Simplex.cpp
+++ b/Mesh/Simplex.cpp
@@ -1,4 +1,4 @@
-// $Id: Simplex.cpp,v 1.43 2005-06-10 20:59:15 geuzaine Exp $
+// $Id: Simplex.cpp,v 1.44 2005-07-03 08:02:24 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -503,6 +503,32 @@ int compareSimplex(const void *a, const void *b)
   return (q->Num - w->Num);
 }
 
+int compareSimplexBarycenter(const void *a, const void *b)
+{
+  Simplex *q = *(Simplex **) a;
+  Simplex *w = *(Simplex **) b;
+  int nq = q->V[3] ? 4 : q->V[2] ? 3 : q->V[1] ? 2 : 1;
+  int nw = w->V[3] ? 4 : w->V[2] ? 3 : w->V[1] ? 2 : 1;
+  double bq, bw, TOL = CTX.lc * 1.e-6;
+
+  bq = bw = 0.;
+  for(int i = 0; i < nq; i++) bq += q->V[i]->Pos.X;
+  for(int i = 0; i < nw; i++) bw += w->V[i]->Pos.X;
+  if(bq-bw > TOL) return 1; else if(bq-bw < -TOL) return -1;
+
+  bq = bw = 0.;
+  for(int i = 0; i < nq; i++) bq += q->V[i]->Pos.Y;
+  for(int i = 0; i < nw; i++) bw += w->V[i]->Pos.Y;
+  if(bq-bw > TOL) return 1; else if(bq-bw < -TOL) return -1;
+
+  bq = bw = 0.;
+  for(int i = 0; i < nq; i++) bq += q->V[i]->Pos.Z;
+  for(int i = 0; i < nw; i++) bw += w->V[i]->Pos.Z;
+  if(bq-bw > TOL) return 1; else if(bq-bw < -TOL) return -1;
+
+  return 0;
+}
+
 int Simplex::Pt_In_Simplexe(Vertex * v, double uvw[3], double tol)
 {
   double mat[3][3];
diff --git a/Mesh/Simplex.h b/Mesh/Simplex.h
index ffe68f78894f30d932176d7b21b8b9a3be2f89aa..39f6070c653fee47793d257e88e9202c2a76b472 100644
--- a/Mesh/Simplex.h
+++ b/Mesh/Simplex.h
@@ -77,6 +77,7 @@ class Simplex : public SimplexBase {
 };
 
 int compareSimplex(const void *a, const void *b);
+int compareSimplexBarycenter(const void *a, const void *b);
 int compareFace(const void *a, const void *b);
 
 Simplex *Create_Simplex (Vertex *v1, Vertex *v2, Vertex *v3, Vertex *v4);
diff --git a/Tetgen/Makefile b/Tetgen/Makefile
index 885bfc368ecb22750bc1a22fa09d97acb5457eb6..606d759c5c9ebf4c056b4dea191ae0ebc54633b0 100644
--- a/Tetgen/Makefile
+++ b/Tetgen/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.3 2005-06-29 09:52:59 tardieu Exp $
+# $Id: Makefile,v 1.4 2005-07-03 08:02:24 geuzaine Exp $
 #
 # Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 #
@@ -21,14 +21,11 @@
 
 include ../variables
 
-
 LIB     = ../lib/libGmshTetgen.a
-INCLUDE = -I../Common -I.
 # Do not optimize (same as Triangle...) 
-#CFLAGS  = ${OPTIM} ${FLAGS} ${INCLUDE} -DNO_PARALLEL_THREADS -UWIN32 
-CFLAGS  =  ${FLAGS} ${INCLUDE} -DNO_PARALLEL_THREADS -UWIN32 
+CFLAGS  =  ${FLAGS} -DTETLIBRARY
 
-SRC =  predicates.cxx  tetgen.cxx
+SRC = predicates.cxx tetgen.cxx
 OBJ = ${SRC:.cxx=.o}
 
 .SUFFIXES: .o .cxx
@@ -38,8 +35,7 @@ ${LIB}: ${OBJ}
 	${RANLIB} ${LIB}
 
 .cxx.o:
-	${CXX} ${CFLAGS} -c predicates.cxx
-	${CXX} ${CFLAGS} -g -DTETLIBRARY -c tetgen.cxx
+	${CXX} ${CFLAGS} -c $<
 
 clean:
 	rm -f *.o
@@ -53,4 +49,5 @@ depend:
 	rm -f Makefile.new
 
 # DO NOT DELETE THIS LINE
-tetgen.o:predicates.cxx tetgen.cxx tetgen.h
+predicates.o: predicates.cxx tetgen.h
+tetgen.o: tetgen.cxx tetgen.h
diff --git a/configure b/configure
index 8da4454d18febf7e45939266c3b1e53d666b0a48..3ba70a49f13f44c30902bdd2f83ba586f130c16a 100755
--- a/configure
+++ b/configure
@@ -853,6 +853,7 @@ Optional Features:
   --enable-parallel       enable parallel version (default=no)
   --enable-triangle       compile Triangle if available (default=yes)
   --enable-netgen         compile Netgen if available (default=yes)
+  --enable-tetgen         compile Tetgen if available (default=yes)
   --enable-matheval       compile MathEval if available (default=yes)
   --enable-jpeg           enable JPEG support (default=yes)
   --enable-z              enable ZLIB support (default=yes)
@@ -1375,6 +1376,11 @@ fi;
 if test "${enable_netgen+set}" = set; then
   enableval="$enable_netgen"
 
+fi;
+# Check whether --enable-tetgen or --disable-tetgen was given.
+if test "${enable_tetgen+set}" = set; then
+  enableval="$enable_tetgen"
+
 fi;
 # Check whether --enable-matheval or --disable-matheval was given.
 if test "${enable_matheval+set}" = set; then
@@ -3749,9 +3755,9 @@ if test "x${TRIANGLE}" = "xyes"; then
      echo "********************************************************************"
      echo "You are building a version of Gmsh that contains Jonathan"
      echo "Shewchuk's Triangle as an alternative isotropic 2D mesh generator."
-     echo "Please note that by doing so, you agree to Triangle's licensing"
-     echo "requirements stated in ./Triangle/README. (Most notably, you can"
-     echo "only redistribute Gmsh if no compensation is received.)"
+     echo "Please note that by doing so, you agree with Triangle's licensing"
+     echo "requirements stated in ./Triangle/README. (Most notably, you may"
+     echo "then only redistribute Gmsh for non-commercial purposes.)"
      echo "To disable Triangle, run configure again with the --disable-triangle"
      echo "option."
      echo "********************************************************************"
@@ -3765,14 +3771,13 @@ else
      echo "unpack the archive and copy the two files 'triangle.c' and"
      echo "'triangle.h' in the ./Triangle subdirectory. Then run ./configure"
      echo "again."
-     echo "Please note that by doing so, you agree to Triangle's licensing"
+     echo "Please note that by doing so, you agree with Triangle's licensing"
      echo "requirements stated in ./Triangle/README. (Most notably, you may"
      echo "then only redistribute Gmsh if no compensation is received.)"
      echo "********************************************************************"
   fi
 fi
 
-
 echo "$as_me:$LINENO: checking for ./Netgen/libsrc/meshing/meshclass.cpp" >&5
 echo $ECHO_N "checking for ./Netgen/libsrc/meshing/meshclass.cpp... $ECHO_C" >&6
 if test "${ac_cv_file___Netgen_libsrc_meshing_meshclass_cpp+set}" = set; then
@@ -3812,7 +3817,7 @@ if test "x${NETGEN}" = "xyes"; then
 else
   if test "x$enable_netgen" != "xno"; then
      echo "********************************************************************"
-     echo "If you want to use Joachim Schoberl's Netgen as an alternative "
+     echo "If you want to use Joachim Schoberl's Netgen as an alternative"
      echo "3D mesh generator, please download Netgen from the project's"
      echo "web site at http://www.hpfem.jku.at/netgen/, unpack the archive"
      echo "and move the libsrc subdirectory in the ./Netgen subdirectory. Then"
@@ -3823,6 +3828,58 @@ else
   fi
 fi
 
+echo "$as_me:$LINENO: checking for ./Tetgen/tetgen.h" >&5
+echo $ECHO_N "checking for ./Tetgen/tetgen.h... $ECHO_C" >&6
+if test "${ac_cv_file___Tetgen_tetgen_h+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  test "$cross_compiling" = yes &&
+  { { echo "$as_me:$LINENO: error: cannot check for file existence when cross compiling" >&5
+echo "$as_me: error: cannot check for file existence when cross compiling" >&2;}
+   { (exit 1); exit 1; }; }
+if test -r "./Tetgen/tetgen.h"; then
+  ac_cv_file___Tetgen_tetgen_h=yes
+else
+  ac_cv_file___Tetgen_tetgen_h=no
+fi
+fi
+echo "$as_me:$LINENO: result: $ac_cv_file___Tetgen_tetgen_h" >&5
+echo "${ECHO_T}$ac_cv_file___Tetgen_tetgen_h" >&6
+if test $ac_cv_file___Tetgen_tetgen_h = yes; then
+  TETGEN="yes"
+else
+  TETGEN="no"
+fi
+
+if test "x${TETGEN}" = "xyes"; then
+  if test "x$enable_tetgen" != "xno"; then
+     GMSH_DIRS="${GMSH_DIRS} Tetgen"
+     GMSH_LIBS="${GMSH_LIBS} -lGmshTetgen"
+     FLAGS="-DHAVE_TETGEN ${FLAGS}"
+     echo "********************************************************************"
+     echo "You are building a version of Gmsh that contains Hang Si's"
+     echo "Tetgen as an alternative 3D mesh generator."
+     echo "Please note that by doing so, you agree with Tetgen's licensing"
+     echo "requirements stated in ./Tetgen/LICENSE. (Most notably, you may"
+     echo "then only redistribute Gmsh for non-commercial purposes.)"
+     echo "To disable Tetgen, run configure again with the --disable-tetgen"
+     echo "option."
+     echo "********************************************************************"
+  fi
+else
+  if test "x$enable_tetgen" != "xno"; then
+     echo "********************************************************************"
+     echo "If you want to use Hang Si's Tetgen as an alternative"
+     echo "3D mesh generator, please download Tetgen from the project's"
+     echo "web site at http://www.tetgen.berlios.de, unpack the archive"
+     echo "and move the files predicates.cxx, tetgen.cxx and tetgen.h in"
+     echo "the ./Tetgen subdirectory. Then run ./configure again."
+     echo "Please note that by doing so, you agree with Tetgen's licensing"
+     echo "requirements stated in ./Tetgen/LICENSE. (Most notably, you may"
+     echo "then only redistribute Gmsh if no compensation is received.)"
+     echo "********************************************************************"
+  fi
+fi
 
 echo "$as_me:$LINENO: checking for ./MathEval/matheval.cpp" >&5
 echo $ECHO_N "checking for ./MathEval/matheval.cpp... $ECHO_C" >&6
@@ -3855,7 +3912,6 @@ if test "x${MATHEVAL}" = "xyes"; then
   fi
 fi
 
-
 if test "x$enable_gsl" != "xno"; then
   if test "x${GSL_PREFIX}" != "x"; then
     LDFLAGS="-L${GSL_PREFIX}/lib ${LDFLAGS}"
diff --git a/configure.in b/configure.in
index 2be85c3137c1a589f72a7f4bdcd699779a852ac3..e1569aed02bcbd2f7d39d37336047ed8de6a0360 100644
--- a/configure.in
+++ b/configure.in
@@ -1,4 +1,4 @@
-dnl $Id: configure.in,v 1.72 2005-06-29 10:02:54 tardieu Exp $
+dnl $Id: configure.in,v 1.73 2005-07-03 08:02:23 geuzaine Exp $
 dnl
 dnl Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 dnl
@@ -66,6 +66,9 @@ AC_ARG_ENABLE(triangle,
 AC_ARG_ENABLE(netgen,
               AC_HELP_STRING([--enable-netgen],
                              [compile Netgen if available (default=yes)]))
+AC_ARG_ENABLE(tetgen,
+              AC_HELP_STRING([--enable-tetgen],
+                             [compile Tetgen if available (default=yes)]))
 AC_ARG_ENABLE(matheval,
               AC_HELP_STRING([--enable-matheval],
                              [compile MathEval if available (default=yes)]))
@@ -223,9 +226,9 @@ if test "x${TRIANGLE}" = "xyes"; then
      echo "********************************************************************"
      echo "You are building a version of Gmsh that contains Jonathan"
      echo "Shewchuk's Triangle as an alternative isotropic 2D mesh generator."
-     echo "Please note that by doing so, you agree to Triangle's licensing"
-     echo "requirements stated in ./Triangle/README. (Most notably, you can"
-     echo "only redistribute Gmsh if no compensation is received.)"
+     echo "Please note that by doing so, you agree with Triangle's licensing"
+     echo "requirements stated in ./Triangle/README. (Most notably, you may"
+     echo "then only redistribute Gmsh for non-commercial purposes.)"
      echo "To disable Triangle, run configure again with the --disable-triangle"
      echo "option."
      echo "********************************************************************"
@@ -239,14 +242,13 @@ else
      echo "unpack the archive and copy the two files 'triangle.c' and"
      echo "'triangle.h' in the ./Triangle subdirectory. Then run ./configure"
      echo "again."
-     echo "Please note that by doing so, you agree to Triangle's licensing"
+     echo "Please note that by doing so, you agree with Triangle's licensing"
      echo "requirements stated in ./Triangle/README. (Most notably, you may"
      echo "then only redistribute Gmsh if no compensation is received.)"
      echo "********************************************************************"
   fi
 fi
 
-
 dnl Check if Netgen is installed
 AC_CHECK_FILE(./Netgen/libsrc/meshing/meshclass.cpp, NETGEN="yes", NETGEN="no")
 if test "x${NETGEN}" = "xyes"; then
@@ -265,7 +267,7 @@ if test "x${NETGEN}" = "xyes"; then
 else
   if test "x$enable_netgen" != "xno"; then
      echo "********************************************************************"
-     echo "If you want to use Joachim Schoberl's Netgen as an alternative "
+     echo "If you want to use Joachim Schoberl's Netgen as an alternative"
      echo "3D mesh generator, please download Netgen from the project's"
      echo "web site at http://www.hpfem.jku.at/netgen/, unpack the archive"
      echo "and move the libsrc subdirectory in the ./Netgen subdirectory. Then"
@@ -276,36 +278,37 @@ else
   fi
 fi
 
-
-#dnl Check if Tetgen is installed
-#AC_CHECK_FILE(./Tetgen/tetgen.h, TETGEN="yes", TETGEN="no")
-#if test "x${TETGEN}" = "xyes"; then
-#  if test "x$enable_tetgen" != "xno"; then
-#     GMSH_DIRS="${GMSH_DIRS} Tetgen"
-#     GMSH_LIBS="${GMSH_LIBS} -lGmshTetgen"
-#     FLAGS="-DHAVE_TETGEN ${FLAGS}"
-#     echo "********************************************************************"
-#     echo "You are building a version of Gmsh that contains Hang Si's"
-#     echo "Tetgen as an alternative 3D mesh generator. Tetgen is distributed"
-#     echo "under the GNU LGPL: see ./Tetgen/LICENSE for more info."
-#     echo "To disable Tetgen, run configure again with the --disable-tetgen"
-#     echo "option."
-#     echo "********************************************************************"
-#  fi
-#else
-#  if test "x$enable_tetgen" != "xno"; then
-#     echo "********************************************************************"
-#     echo "If you want to use Hang Si's Tetgen as an alternative "
-#     echo "3D mesh generator, please download Tetgen from the project's"
-#     echo "web site at http://www.tetgen.berlios.de, unpack the archive"
-#     echo "and move the files in the ./Tetgen subdirectory. Then"
-#     echo "run ./configure again."
-#     echo "Please note that by doing so, you agree with Tetgen's licensing"
-#     echo "requirements stated in ./Tetgen/LICENSE."
-#     echo "********************************************************************"
-#  fi
-#fi
-
+dnl Check if Tetgen is installed
+AC_CHECK_FILE(./Tetgen/tetgen.h, TETGEN="yes", TETGEN="no")
+if test "x${TETGEN}" = "xyes"; then
+  if test "x$enable_tetgen" != "xno"; then
+     GMSH_DIRS="${GMSH_DIRS} Tetgen"
+     GMSH_LIBS="${GMSH_LIBS} -lGmshTetgen"
+     FLAGS="-DHAVE_TETGEN ${FLAGS}"
+     echo "********************************************************************"
+     echo "You are building a version of Gmsh that contains Hang Si's"
+     echo "Tetgen as an alternative 3D mesh generator."
+     echo "Please note that by doing so, you agree with Tetgen's licensing"
+     echo "requirements stated in ./Tetgen/LICENSE. (Most notably, you may"
+     echo "then only redistribute Gmsh for non-commercial purposes.)"
+     echo "To disable Tetgen, run configure again with the --disable-tetgen"
+     echo "option."
+     echo "********************************************************************"
+  fi
+else
+  if test "x$enable_tetgen" != "xno"; then
+     echo "********************************************************************"
+     echo "If you want to use Hang Si's Tetgen as an alternative"
+     echo "3D mesh generator, please download Tetgen from the project's"
+     echo "web site at http://www.tetgen.berlios.de, unpack the archive"
+     echo "and move the files predicates.cxx, tetgen.cxx and tetgen.h in"
+     echo "the ./Tetgen subdirectory. Then run ./configure again."
+     echo "Please note that by doing so, you agree with Tetgen's licensing"
+     echo "requirements stated in ./Tetgen/LICENSE. (Most notably, you may"
+     echo "then only redistribute Gmsh if no compensation is received.)"
+     echo "********************************************************************"
+  fi
+fi
 
 dnl Check for MathEval
 AC_CHECK_FILE(./MathEval/matheval.cpp, MATHEVAL="yes", MATHEVAL="no")
@@ -317,7 +320,6 @@ if test "x${MATHEVAL}" = "xyes"; then
   fi
 fi
 
-
 dnl Check for GSL
 if test "x$enable_gsl" != "xno"; then
   if test "x${GSL_PREFIX}" != "x"; then
diff --git a/doc/VERSIONS b/doc/VERSIONS
index 2d1d408c1d9efd4211def844ef2cd0968e4c456d..8f42ff8f64648b87b3f8c2be6f9e649c36c9fdbc 100644
--- a/doc/VERSIONS
+++ b/doc/VERSIONS
@@ -1,4 +1,4 @@
-$Id: VERSIONS,v 1.337 2005-06-25 18:11:45 geuzaine Exp $
+$Id: VERSIONS,v 1.338 2005-07-03 08:02:24 geuzaine Exp $
 
 New since 1.60: added support for second order (curved) elements in
 post-processor; new version (1.4) of post-processing file formats; new
@@ -11,7 +11,8 @@ handling of meshes with large number of physical entities; removed
 Discrete Line and Discrete Surface commands (the same functionality
 can now be obtained by simply loading a mesh in .msh format); fixed
 coloring by mesh partition; new "mesh statistics" export format; new
-full-quad recombine option; various small bug fixes and improvements.
+full-quad recombine option; new Plugin(ModulusPhase); various small
+bug fixes and improvements.
 
 New in 1.60: added support for discrete curves; new Window menu on Mac
 OS X; generalized all octree-based plugins (CutGrid, StreamLines,