Index: /issm/trunk/src/c/Makefile.am
===================================================================
--- /issm/trunk/src/c/Makefile.am	(revision 10204)
+++ /issm/trunk/src/c/Makefile.am	(revision 10205)
@@ -684,4 +684,6 @@
 					./modules/BamgConvertMeshx/BamgConvertMeshx.cpp\
 					./modules/BamgConvertMeshx/BamgConvertMeshx.h\
+					./modules/BamgTriangulatex/BamgTriangulatex.cpp\
+					./modules/BamgTriangulatex/BamgTriangulatex.h\
 					./modules/DragCoefficientAbsGradientx/DragCoefficientAbsGradientx.cpp\
 					./modules/DragCoefficientAbsGradientx/DragCoefficientAbsGradientx.h\
Index: /issm/trunk/src/c/io/Matlab/WriteMatlabData.cpp
===================================================================
--- /issm/trunk/src/c/io/Matlab/WriteMatlabData.cpp	(revision 10204)
+++ /issm/trunk/src/c/io/Matlab/WriteMatlabData.cpp	(revision 10205)
@@ -81,4 +81,31 @@
 }
 /*}}}*/
+/*FUNCTION WriteMatlabData(mxArray** pdataref,int* matrix, int M,int N){{{1*/
+void WriteMatlabData(mxArray** pdataref,int* matrix, int M,int N){
+
+	mxArray* dataref=NULL;
+	mxArray* tdataref=NULL;
+
+	if(matrix){
+
+		/*convert to double matrix*/
+		double* doublematrix=(double*)xmalloc(M*N*sizeof(double));
+		for(int i=0;i<M*N;i++) doublematrix[i]=(double)matrix[i];
+
+		/*data is a double* pointer. Copy into a matrix: */
+		tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
+		mxSetM(tdataref,(mwSize)N);
+		mxSetN(tdataref,(mwSize)M);
+		mxSetPr(tdataref,(double*)doublematrix);
+
+		//transpose
+		mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
+	}
+	else{
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+	}
+	*pdataref=dataref;
+}
+/*}}}*/
 /*FUNCTION WriteMatlabData(mxArray** pdataref,Vec vector){{{1*/
 void WriteMatlabData(mxArray** pdataref,Vec vector){
Index: /issm/trunk/src/c/io/Matlab/matlabio.h
===================================================================
--- /issm/trunk/src/c/io/Matlab/matlabio.h	(revision 10204)
+++ /issm/trunk/src/c/io/Matlab/matlabio.h	(revision 10205)
@@ -18,4 +18,5 @@
 void WriteMatlabData(mxArray** pdataref,Mat matrix);
 void WriteMatlabData(mxArray** pdataref,double* matrix, int M,int N);
+void WriteMatlabData(mxArray** pdataref,int*    matrix, int M,int N);
 void WriteMatlabData(mxArray** pdataref,Vec vector);
 void WriteMatlabData(mxArray** pdataref,double* vector, int M);
Index: /issm/trunk/src/c/modules/BamgTriangulatex/BamgTriangulatex.cpp
===================================================================
--- /issm/trunk/src/c/modules/BamgTriangulatex/BamgTriangulatex.cpp	(revision 10205)
+++ /issm/trunk/src/c/modules/BamgTriangulatex/BamgTriangulatex.cpp	(revision 10205)
@@ -0,0 +1,20 @@
+/*!\file BamgTriangulatex
+ */
+
+#include "./BamgTriangulatex.h"
+
+#include "../../shared/shared.h"
+#include "../../include/include.h"
+#include "../../toolkits/toolkits.h"
+#include "../../objects/objects.h"
+
+using namespace bamg;
+using namespace std;
+
+int BamgTriangulatex(int** pindex,int* pnels,double* x,double* y,int nods){
+
+	Mesh Th(x,y,nods);
+	Th.WriteIndex(pindex,pnels);
+	//delete &Th;
+
+}
Index: /issm/trunk/src/c/modules/BamgTriangulatex/BamgTriangulatex.h
===================================================================
--- /issm/trunk/src/c/modules/BamgTriangulatex/BamgTriangulatex.h	(revision 10205)
+++ /issm/trunk/src/c/modules/BamgTriangulatex/BamgTriangulatex.h	(revision 10205)
@@ -0,0 +1,13 @@
+/*!\file:  BamgTriangulatex.h
+ * \brief header file for Bamg module
+ */ 
+
+#ifndef _BAMGTRIANGULATEX_H
+#define _BAMGTRIANGULATEX_H
+
+#include "../../objects/objects.h"
+
+/* local prototypes: */
+int BamgTriangulatex(int** pindex,int* pnels,double* x,double* y,int nods);
+
+#endif
Index: /issm/trunk/src/c/modules/modules.h
===================================================================
--- /issm/trunk/src/c/modules/modules.h	(revision 10204)
+++ /issm/trunk/src/c/modules/modules.h	(revision 10205)
@@ -12,4 +12,5 @@
 #include "./Bamgx/Bamgx.h"
 #include "./BamgConvertMeshx/BamgConvertMeshx.h"
+#include "./BamgTriangulatex/BamgTriangulatex.h"
 #include "./Chacox/Chacox.h"
 #include "./ComputeBasalStressx/ComputeBasalStressx.h"
Index: /issm/trunk/src/c/objects/Bamg/Mesh.cpp
===================================================================
--- /issm/trunk/src/c/objects/Bamg/Mesh.cpp	(revision 10204)
+++ /issm/trunk/src/c/objects/Bamg/Mesh.cpp	(revision 10205)
@@ -48,4 +48,9 @@
 		SetIntCoor();
 		ReconstructExistingMesh();
+	}
+	/*}}}1*/
+	/*FUNCTION Mesh::Mesh(double* x,double* y,int nods){{{1*/
+	Mesh::Mesh(double* x,double* y,int nods):Gh(*(new Geometry())),BTh(*this){
+		Triangulate(x,y,nods);
 	}
 	/*}}}1*/
@@ -998,4 +1003,40 @@
 			bamgopts->metric[i*3+2]=vertices[i].m.a22;
 		}
+	}
+	/*}}}1*/
+	/*FUNCTION Mesh::WriteIndex{{{1*/
+	void Mesh::WriteIndex(int** pindex,int* pnels){
+
+		/*Intermediary*/
+		int i,k,num;
+		int verbose=0;
+
+		/*output*/
+		int* index=NULL;
+
+		/*Get number of triangles*/
+		k=0;
+		for (i=0;i<nbt;i++){
+			Triangle &t=triangles[i];
+			if(t.det>0) k++;
+		}
+
+		if (k){
+			index=(int*)xmalloc(3*k*sizeof(double));
+			num=0;
+			for (i=0;i<nbt;i++){
+				Triangle &t=triangles[i];
+				if (t.det>0 && !(t.Hidden(0)||t.Hidden(1) || t.Hidden(2) )){
+					index[num*3+0]=GetId(t[0])+1; //back to M indexing
+					index[num*3+1]=GetId(t[1])+1; //back to M indexing
+					index[num*3+2]=GetId(t[2])+1; //back to M indexing
+					num=num+1;
+				}
+			}
+		}
+
+		/*Assign output pointers*/
+		*pindex=index;
+		*pnels=k;
 	}
 	/*}}}1*/
@@ -4888,4 +4929,31 @@
 }
 /*}}}1*/
+/*FUNCTION Mesh::Triangulate{{{1*/
+void Mesh::Triangulate(double* x,double* y,int nods){
+
+	int verbose=0;
+	int i;
+	Metric M1(1);
+
+	/*Initialize mesh*/
+	Init(nods);//this resets nbv to 0
+	nbv=nods;
+
+	//Vertices
+	if(verbose) printf("Reading vertices (%i)\n",nbv);
+	for (i=0;i<nbv;i++){
+		vertices[i].r.x=x[i];
+		vertices[i].r.y=y[i];
+		vertices[i].ReferenceNumber=1;
+		vertices[i].DirOfSearch =NoDirOfSearch;
+		vertices[i].m=M1;
+		vertices[i].color=0;
+	}
+	maxnbt=2*maxnbv-2; // for filling The Holes and quadrilaterals 
+
+	/*Insert Vertices*/
+	Insert();
+}
+/*}}}1*/
 	/*FUNCTION Mesh::TriangulateFromGeom0{{{1*/
 	void Mesh::TriangulateFromGeom0(BamgOpts* bamgopts){
@@ -4900,7 +4968,7 @@
 		const int          MaxSubEdge  = 10;
 
-		R2                 AB;
+		R2          AB;
 		GeomVertex *a, *b;
-		BamgVertex        *va, *vb;
+		BamgVertex *va,*vb;
 		GeomEdge   *e;
 
Index: /issm/trunk/src/c/objects/Bamg/Mesh.h
===================================================================
--- /issm/trunk/src/c/objects/Bamg/Mesh.h	(revision 10204)
+++ /issm/trunk/src/c/objects/Bamg/Mesh.h	(revision 10205)
@@ -57,5 +57,6 @@
 			//Constructors/Destructors
 			Mesh(BamgGeom* bamggeom,BamgMesh* bamgmesh,BamgOpts* bamgopts);
-			Mesh(double* index,double* x,double* y,int nods,int nels);
+			Mesh(double* index,double* x,double* y,int nods,int nels);/*MeshConvert*/
+			Mesh(double* x,double* y,int nods); /*BamgTriangulate*/
 			Mesh(Mesh &,Geometry * pGh=0,Mesh* pBTh=0,long maxnbv_in=0 ); //copy operator
 			Mesh(const Mesh &,const int *flag,const int *bb,BamgOpts* bamgopts); // truncature
@@ -114,4 +115,5 @@
 			void ReadMetric(const BamgOpts* bamgopts);
 			void WriteMetric(BamgOpts* bamgopts);
+			void WriteIndex(int** pindex,int* pnels);
 			void AddMetric(BamgOpts* bamgopts);
 			void BuildMetric0(BamgOpts* bamgopts);
@@ -146,4 +148,5 @@
 			void TriangulateFromGeom1(BamgOpts* bamgopts,int KeepVertices=1);// the real constructor mesh adaption
 			void TriangulateFromGeom0(BamgOpts* bamgopts);// the real constructor mesh generator
+			void Triangulate(double* x,double* y,int nods);
 			void Init(long);
 	};
Index: /issm/trunk/src/mex/BamgTriangulate/BamgTriangulate.cpp
===================================================================
--- /issm/trunk/src/mex/BamgTriangulate/BamgTriangulate.cpp	(revision 10205)
+++ /issm/trunk/src/mex/BamgTriangulate/BamgTriangulate.cpp	(revision 10205)
@@ -0,0 +1,58 @@
+/*\file BamgTriangulate.c
+ *\brief: bamg module.
+ */
+#include "./BamgTriangulate.h"
+
+void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){
+
+	/*input: */
+	double* x=NULL;
+	double* y=NULL;
+	int     x_cols;
+	int     y_rows,y_cols;
+	int nods;
+
+	/*Output*/
+	int* index=NULL;
+	int  nels;
+
+	/*Intermediary*/
+	int verbose=0;
+
+	/*Boot module: */
+	MODULEBOOT();
+
+	/*checks on arguments on the matlab side: */
+	CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,&BamgTriangulateUsage);
+
+	/*Input datasets: */
+	if (verbose) printf("Fetching inputs\n");
+	FetchMatlabData(&x,&nods,&x_cols,XHANDLE);
+	FetchMatlabData(&y,&y_rows,&y_cols,YHANDLE);
+
+	/*Check inputs*/
+	if(y_rows!=nods)         _error_("x and y do not have the same length");
+	if(x_cols>1 || y_cols>1) _error_("x and y should have only one column");
+	if(nods<3)               _error_("At least 3 points are required");
+
+	/* Run core computations: */
+	if (verbose) printf("Call core\n");
+	BamgTriangulatex(&index,&nels,x,y,nods);
+
+	/*Write output*/
+	WriteMatlabData(INDEX,index,nels,3);
+
+	/*end module: */
+	MODULEEND();
+}
+
+void BamgTriangulateUsage(void)
+{
+	_printf_(true,"BAMGTRIANGULATE - Delaunay Triangulation of a list of points");
+	_printf_(true,"\n");
+	_printf_(true,"   Usage:\n");
+	_printf_(true,"      index=BamgTriangulate(x,y);\n");
+	_printf_(true,"      index: index of the triangulation\n");
+	_printf_(true,"      x,y: coordinates of the nodes\n");
+	_printf_(true,"\n");
+}
Index: /issm/trunk/src/mex/BamgTriangulate/BamgTriangulate.h
===================================================================
--- /issm/trunk/src/mex/BamgTriangulate/BamgTriangulate.h	(revision 10205)
+++ /issm/trunk/src/mex/BamgTriangulate/BamgTriangulate.h	(revision 10205)
@@ -0,0 +1,32 @@
+/*!\file BamgTriangulate.h
+ * \brief: prototype for Data Interpolation mex module.
+ */
+
+#ifndef _BAMGTRIANGULATE_H
+#define _BAMGTRIANGULATE_H
+
+/* local prototypes: */
+void BamgTriangulateUsage(void);
+
+#include "../../c/modules/modules.h"
+#include "../../c/Container/Container.h"
+#include "../../c/shared/shared.h"
+
+#undef __FUNCT__ 
+#define __FUNCT__  "BamgTriangulate"
+
+
+/* serial input macros: */
+#define XHANDLE prhs[0]
+#define YHANDLE prhs[1]
+
+/* serial output macros: */
+#define INDEX (mxArray**)&plhs[0]
+
+/* serial arg counts: */
+#undef NLHS
+#define NLHS  1
+#undef NRHS
+#define NRHS  2
+
+#endif
Index: /issm/trunk/src/mex/Makefile.am
===================================================================
--- /issm/trunk/src/mex/Makefile.am	(revision 10204)
+++ /issm/trunk/src/mex/Makefile.am	(revision 10205)
@@ -10,4 +10,5 @@
 				BamgMesher\
 				BamgConvertMesh\
+				BamgTriangulate\
 				Chaco\
 				ComputeBasalStress\
@@ -128,4 +129,7 @@
 					BamgConvertMesh/BamgConvertMesh.h
 
+BamgTriangulate_SOURCES = BamgTriangulate/BamgTriangulate.cpp\
+								  BamgTriangulate/BamgTriangulate.h
+
 Chaco_SOURCES = Chaco/Chaco.cpp\
 					Chaco/Chaco.h
Index: /issm/trunk/src/mex/Test/Test.cpp
===================================================================
--- /issm/trunk/src/mex/Test/Test.cpp	(revision 10204)
+++ /issm/trunk/src/mex/Test/Test.cpp	(revision 10205)
@@ -7,13 +7,19 @@
 void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){
 
-	Options *options = NULL;
-	double   test;
-	char    *name    = NULL;
-	bool     logical;
-	double  *matrix    = NULL;
-	int      numel;
+	char *string = NULL;
+	PetscErrorCode ierr;
 
-	MODULEBOOT();
-	MODULEEND();
+	FetchMatlabData(&string,STRING);
+
+	 if (!strcmp(string,"Init")){
+		 ierr=PetscInitializeNoArguments();
+		 ierr=PetscPopSignalHandler();
+	 }
+	 else if(!strcmp(string,"Finalize")) {
+		 ierr=PetscFinalize();
+	 }
+	 else{
+		 printf("NOTHING to be done\n");
+	 }
 }
 
Index: /issm/trunk/src/mex/Test/Test.h
===================================================================
--- /issm/trunk/src/mex/Test/Test.h	(revision 10204)
+++ /issm/trunk/src/mex/Test/Test.h	(revision 10205)
@@ -14,5 +14,5 @@
     
 /* serial input macros: */
-#define DATASET (mxArray*)prhs[0]
+#define STRING (mxArray*)prhs[0]
 
 /* serial arg counts: */
