Index: /issm/trunk/src/c/Bamgx/Bamgx.cpp
===================================================================
--- /issm/trunk/src/c/Bamgx/Bamgx.cpp	(revision 2770)
+++ /issm/trunk/src/c/Bamgx/Bamgx.cpp	(revision 2771)
@@ -11,6 +11,4 @@
 #include "../include/macros.h"
 #include "../toolkits/toolkits.h"
-#include "../EnumDefinitions/EnumDefinitions.h"
-
 
 /*Bamg: */
@@ -30,11 +28,6 @@
 using namespace bamg;
 using namespace std;
-#define   NBVMAX 50000   
-
-
-int Bamgx(double** pelementsout,double** pxout,double** pyout, int* pnelout,int* pnodsout, 
-		double* elements, double* x, double* y, int nel, int nods, double* metric, double gradation, int splitpbedge, int nbv){
-	
-
+
+int Bamgx(BamgMesh* bamgmesh,BamgGeom* bamggeom,BamgArgs* bamgargs){
 	
 	int noerr=1;
@@ -47,16 +40,8 @@
 
 	/*Bamg variables: */
-	double time0, time1,time2,time3,time4;
-	//   2 way for uses ---
-	//   1 first mesh 
-	//   or adaptation 
-	
-	//  double *sol=0;
-	//  int adaptation = 0 ;
-	Int4 i;
+	int i,j,num;
 	hinterpole=1; // by def interpolation a h 
-	//  int Err=0;
 	int fileout=0;
-	int nbvx = NBVMAX;
+	int nbvx = 50000;
 	int iso =0,AbsError=0,nbjacoby=1,allquad=0;
 	int NoMeshReconstruction=0;
@@ -81,20 +66,24 @@
 	double power=1;
 	Triangles *Thr=0,*Thb=0;
-
-	char * fgeom=0,*fmeshback=0,*fmeshout=0,*fmeshr=0,*fmetrix=0,*famfmt=0,*fmsh=0,
+	char *fmeshback=0,*fmeshout=0,*fmeshr=0,*fmetrix=0,*famfmt=0,*fmsh=0,
 		 *fnopo=0,*fftq=0,*fam=0,*famdba=0,*rbb=0,*rBB=0,*wbb=0,*wBB=0,
 		 *fMbb=0,*foM=0,*fMBB=0;
-	
 	long int verbosity =10;
-
 	char *datargv[128] ;
 	int datargc=1;
 	datargv[0]= datargv[1]=0;// for create a error if no parameter 
 
-	//Recover command line options: */
-	nbvx=nbv;
+	/*testing*/
+	int splitpbedge=1;
+	int nel=0;
+	int nods=0;
+	double* x=NULL;
+	double* y=NULL;
+	double* elements=NULL;
+
+	printf("NumVertices = %i\nNumEdges = %i\n",bamggeom->NumVertices,bamggeom->NumEdges);
+
+	/*Recover options from inputs: */
 	if(splitpbedge)SplitEdgeWith2Boundary=1;
-
-	/*Rest of the code taken directly from bamg.cpp, with our own mods: */
 
 	// some verification
@@ -121,6 +110,62 @@
 		rBBeqMBB = !strcmp(rBB,fMBB);
 
-	time0 = CPUtime();
-
+	if(1){
+		if (verbosity) 
+		 printf("Construction of a mesh from a given geometry\n");
+
+		Geometry Gh(bamgargs->geomfile);
+		hmin = Max(hmin,Gh.MinimalHmin());
+		hmax = Min(hmax,Gh.MaximalHmax());
+		
+		//build metric if not given in input
+		for(i=0;i<Gh.nbv;i++)
+		  {
+			MetricAnIso M=Gh[i];
+			MatVVP2x2 Vp(M/coef);
+			Vp.Maxh(hmin);
+			Vp.Minh(hmax);
+			Gh.vertices[i].m = Vp;
+		  }
+
+		//generate mesh
+		Triangles Th(nbvx,Gh);
+
+		/*Build output{{{2*/
+		nelout=Th.nbt-Th.NbOutT; //number of triangles - number of external triangles
+		nodsout=Th.nbv;
+
+		xout=(double*)xmalloc(nodsout*sizeof(double));
+		yout=(double*)xmalloc(nodsout*sizeof(double));
+		for (i=0;i<nodsout;i++){
+			xout[i]=Th.vertices[i].r.x;
+			yout[i]=Th.vertices[i].r.y;
+		}
+
+		elementsout=(double*)xmalloc(3*nelout*sizeof(double));
+		num=0;
+		for(i=0;i<Th.nbt;i++){ 
+			Triangle &t=Th.triangles[i];
+			if (t.link){
+				//write the element only if it is part of the mesh (no boundary element)
+				elementsout[3*num+0]=Th.Number(t[0])+1;
+				elementsout[3*num+1]=Th.Number(t[1])+1;
+				elementsout[3*num+2]=Th.Number(t[2])+1;
+				num=num+1;
+			}
+		}
+
+		/*Assign output pointers*/
+		bamgmesh->numberofelements=nelout;
+		bamgmesh->numberofnodes=nodsout;
+		bamgmesh->index=elementsout;
+		bamgmesh->x=xout;
+		bamgmesh->y=yout;
+
+		return noerr;
+		/*}}}2*/
+	}
+
+	if (0){
+	/*Anisotropic mesh adaptation {{{1*/
 	/*Read background mesh from simple delaunay triangulation: */
 	Triangles BTh(elements,x,y,nel,nods,nbvx,cutoffradian); // read the background mesh 
@@ -132,5 +177,4 @@
 
 	BTh.MakeQuadTree();
-	time1 = CPUtime();
 	if (fmetrix) 
 		BTh.ReadMetric(fmetrix,hmin,hmax,coef);
@@ -182,5 +226,4 @@
 		anisomax=1;
 	BTh.BoundAnisotropy(anisomax,hminaniso);
-	time2 = CPUtime();
 	if(foM)
 	{
@@ -215,9 +258,5 @@
 			Th.SplitInternalEdgeWithBorderVertices();
 		Th.ReNumberingTheTriangleBySubDomain();
-		time3 = CPUtime();
-
-		if(verbosity>0){
-			cout << " Cpu time for meshing          : " << time3-time2 << "s  Nb Vertices/s = " <<  (Th.nbv) /(time3-time2) << endl;
-		}
+
 		if(verbosity>3) Th.ShowHistogram();
 		if(NbSmooth>0) Th.SmoothingVertex(NbSmooth,omega);
@@ -243,5 +282,4 @@
 					cout << " -- interpolation P1  files : " << rBB<< " -> " << wBB <<endl;
 			}
-			double time00  = CPUtime();	   
 			const int dim=2;
 			// optimisation read only si rbb != fMbb
@@ -336,17 +374,9 @@
 				if (solBB) 
 					delete [] solBB;
-				double  time04 = CPUtime();
-				if(verbosity>0) 
-					cout << " Cpu time for interpolation " << time04-time00 << " s" <<endl;
-
-		}
-		time4 = CPUtime();
+
+		}
 
 		if(verbosity>0) 
 		{
-			cout << " Cpu time for meshing with io  : " << time4-time0
-				<< "s  Nb Vertices/s = " << Th.nbv/(time4-time0)
-				<< endl
-				<< " Nb vertices = " << Th.nbv;
 			if (Th.nbt-Th.NbOutT-Th.NbOfQuad*2) 
 				cout  << " Nb Triangles = " << (Th.nbt-Th.NbOutT-Th.NbOfQuad*2);
@@ -356,20 +386,17 @@
 		}
 
-
 		// cout << "delete &Th " ;
 		delete &Th;
 		//cout << " end " << endl;
 	}
-
-	throw ErrorException(__FUNCT__," almost there\n");
-
-
 	/*Assign output pointers:*/
-	*pelementsout=elementsout;
-	*pxout=xout;
-	*pyout=yout;
-	*pnelout=nelout;
-	*pnodsout=nodsout;
+	bamgmesh->numberofelements=nelout;
+	bamgmesh->numberofnodes=nodsout;
+	bamgmesh->index=elementsout;
+	bamgmesh->x=xout;
+	bamgmesh->y=yout;
 
 	return noerr;
+	/*}}}*/
+	}
 }
Index: /issm/trunk/src/c/Bamgx/Bamgx.h
===================================================================
--- /issm/trunk/src/c/Bamgx/Bamgx.h	(revision 2770)
+++ /issm/trunk/src/c/Bamgx/Bamgx.h	(revision 2771)
@@ -9,6 +9,5 @@
 
 /* local prototypes: */
-int     Bamgx(double** pelementsout,double** pxout,double** pyout, int* pnelout,int* pnodsout, 
-		double* elements, double* x, double* y, int nel, int nods, double* metric, double gradation, int splitpbedge, int nbv);
+int     Bamgx(BamgMesh* bamgmesh,BamgGeom* bamggeom,BamgArgs* bamgargs);
 
 #endif  /* _BAMGX_H */
Index: /issm/trunk/src/c/Bamgx/MeshRead.cpp
===================================================================
--- /issm/trunk/src/c/Bamgx/MeshRead.cpp	(revision 2770)
+++ /issm/trunk/src/c/Bamgx/MeshRead.cpp	(revision 2771)
@@ -1,14 +1,3 @@
-// -*- Mode : c++ -*-
-//
-// SUMMARY  :      
-// USAGE    :        
-// ORG      : 
-// AUTHOR   : Frederic Hecht
-// E-MAIL   : hecht@ann.jussieu.fr
-//
-
-/*
- 
- This file is part of Freefem++
+/*This file is largely inspired by Freefem++
  
  Freefem++ is free software; you can redistribute it and/or modify
Index: /issm/trunk/src/c/objects/BamgArgs.h
===================================================================
--- /issm/trunk/src/c/objects/BamgArgs.h	(revision 2771)
+++ /issm/trunk/src/c/objects/BamgArgs.h	(revision 2771)
@@ -0,0 +1,13 @@
+/*!\file:  BamgArgs.h
+ * \brief place holder for optimization function arguments
+ */ 
+
+#ifndef BAMGARGS_H_
+#define BAMGARGS_H_
+
+struct BamgArgs{
+
+	char* geomfile;
+
+};
+#endif
Index: /issm/trunk/src/c/objects/BamgGeom.h
===================================================================
--- /issm/trunk/src/c/objects/BamgGeom.h	(revision 2771)
+++ /issm/trunk/src/c/objects/BamgGeom.h	(revision 2771)
@@ -0,0 +1,17 @@
+/*!\file:  BamgGeom.h
+ * \brief place holder for optimization function arguments
+ */ 
+
+#ifndef BAMGGEOM_H_
+#define BAMGGEOM_H_
+
+struct BamgGeom{
+
+	int     NumVertices;
+	double* Vertices; 
+	int     NumEdges;
+	double* Edges;
+	double* hVertices;
+
+};
+#endif
Index: /issm/trunk/src/c/objects/BamgMesh.h
===================================================================
--- /issm/trunk/src/c/objects/BamgMesh.h	(revision 2771)
+++ /issm/trunk/src/c/objects/BamgMesh.h	(revision 2771)
@@ -0,0 +1,17 @@
+/*!\file:  BamgMesh.h
+ * \brief place holder for optimization function arguments
+ */ 
+
+#ifndef BAMGMESH_H_
+#define BAMGMESH_H_
+
+struct BamgMesh{
+
+	int     numberofelements;
+	int     numberofnodes;
+	double* x;
+	double* y;
+	double* index;
+
+};
+#endif
Index: /issm/trunk/src/c/objects/objects.h
===================================================================
--- /issm/trunk/src/c/objects/objects.h	(revision 2770)
+++ /issm/trunk/src/c/objects/objects.h	(revision 2771)
@@ -39,4 +39,7 @@
 #include "./OptArgs.h"
 #include "./OptPars.h"
+#include "./BamgGeom.h"
+#include "./BamgMesh.h"
+#include "./BamgArgs.h"
 
 
Index: /issm/trunk/src/m/classes/public/bamg.m
===================================================================
--- /issm/trunk/src/m/classes/public/bamg.m	(revision 2771)
+++ /issm/trunk/src/m/classes/public/bamg.m	(revision 2771)
@@ -0,0 +1,37 @@
+function md=bamg(md,varargin)
+%BAMG - mesh generation
+%
+%   Usage:
+%      md=bamg(md,varargin);
+
+%process options {{{1
+options=pairoptions(varargin{:});
+
+%initialize the structures required as input of Bamg
+bamg_options=struct();
+bamg_geometry=struct();
+
+%fill out the geometry
+bamg_geometry.Vertices=getfieldvalue(options,'Vertices',NaN);
+bamg_geometry.NumVertices=size(bamg_geometry.Vertices,1);
+bamg_geometry.Edges=getfieldvalue(options,'Edges',NaN);
+bamg_geometry.NumEdges=size(bamg_geometry.Edges,1);
+bamg_geometry.hVertices=getfieldvalue(options,'hVertices',NaN);
+
+bamg_options.fgeom=getfieldvalueerr(options,'fgeom');
+%}}}
+
+%call Bamg
+[elements,x,y]=Bamg(bamg_geometry,bamg_options);
+
+% plug results onto model {{{1
+md.x=x;
+md.y=y;
+md.elements=elements;
+
+%Fill in rest of fields:
+md.numberofelements=length(md.elements);
+md.numberofgrids=length(md.x);
+md.z=zeros(md.numberofgrids,1);
+md.type='2d';
+%}}}
Index: /issm/trunk/src/mex/Bamg/Bamg.cpp
===================================================================
--- /issm/trunk/src/mex/Bamg/Bamg.cpp	(revision 2770)
+++ /issm/trunk/src/mex/Bamg/Bamg.cpp	(revision 2771)
@@ -2,7 +2,5 @@
  *\brief: bamg module.
  */
-
 #include "./Bamg.h"
-
 
 void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){
@@ -10,21 +8,17 @@
 	/*diverse: */
 	int   noerr=1;
+	int   i;
+	BamgArgs bamgargs;
+	BamgMesh bamgmesh;
+	BamgGeom bamggeom;
 
 	/*inputs: */
-	double* elements=NULL;
-	int     dummy;
-	double* x=NULL;
-	double* y=NULL;
-	double* metric=NULL;
-	int     nods,nel;
-	double  gradation;
-	int     splitpbedge;
-	int     nbv;
+	char*   geomfile=NULL;
+	int     NumVertices;
+	double* Vertices=NULL;
+	int     NumEdges;
+	double* Edges=NULL;
+	double* hVertices=NULL;
 
-	/*output: */
-	double* elementsout=NULL;
-	double* xout=NULL;
-	double* yout=NULL;
-	int     nodsout,nelout;
 
 	/*Boot module: */
@@ -34,29 +28,35 @@
 	CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,&BamgUsage);
 
-	/*Input datasets: */
-	FetchData(&elements,&nel,&dummy,ELEMENTS);
-	FetchData(&x,&nods,X);
-	FetchData(&y,&nods,Y);
-	FetchData(&metric,&nods,METRIC);
-	FetchData(&gradation,GRADATION);
-	FetchData(&splitpbedge,SPLITPBEDGE);
-	FetchData(&nbv,NBV);
-	
+	/*process inputs*/
+	FetchData(&geomfile,mxGetField(BAMGOPTIONS,0,"fgeom"));
+
+	FetchData(&NumVertices,mxGetField(BAMGGEOMETRY,0,"NumVertices"));
+	FetchData(&Vertices,NULL,NULL,mxGetField(BAMGGEOMETRY,0,"Vertices"));
+	FetchData(&NumEdges,mxGetField(BAMGGEOMETRY,0,"NumEdges"));
+	FetchData(&Edges,NULL,NULL,mxGetField(BAMGGEOMETRY,0,"Edges"));
+	FetchData(&hVertices,NULL,NULL,mxGetField(BAMGGEOMETRY,0,"hVertices"));
+
+	/*create bamg geometry input*/
+	bamgargs.geomfile=geomfile;
+
+	bamggeom.NumVertices=NumVertices;
+	bamggeom.Vertices=Vertices;
+	bamggeom.NumEdges=NumEdges;
+	bamggeom.Edges=Edges;
+	bamggeom.hVertices=hVertices;
+
 	/*!Generate internal degree of freedom numbers: */
-	Bamgx(&elementsout,&xout,&yout,&nelout,&nodsout,elements,x,y,nel,nods,metric,gradation,splitpbedge,nbv);
+	Bamgx(&bamgmesh,&bamggeom,&bamgargs);
 
 	/*write output datasets: */
-	WriteData(ELEMENTSOUT,elementsout,nelout,3);
-	WriteData(XOUT,xout,nodsout);
-	WriteData(YOUT,yout,nodsout);
+	WriteData(ELEMENTSOUT,bamgmesh.index,bamgmesh.numberofelements,3);
+	WriteData(XOUT,bamgmesh.x,bamgmesh.numberofnodes);
+	WriteData(YOUT,bamgmesh.y,bamgmesh.numberofnodes);
 
 	/*Free ressources: */
-	xfree((void**)&elements);
-	xfree((void**)&x);
-	xfree((void**)&y);
-	xfree((void**)&metric);
-	xfree((void**)&elementsout);
-	xfree((void**)&xout);
-	xfree((void**)&yout);
+	xfree((void**)&geomfile);
+	xfree((void**)&Vertices);
+	xfree((void**)&Edges);
+	xfree((void**)&hVertices);
 
 	/*end module: */
Index: /issm/trunk/src/mex/Bamg/Bamg.h
===================================================================
--- /issm/trunk/src/mex/Bamg/Bamg.h	(revision 2770)
+++ /issm/trunk/src/mex/Bamg/Bamg.h	(revision 2771)
@@ -18,11 +18,6 @@
     
 /* serial input macros: */
-#define ELEMENTS (mxArray*)prhs[0]
-#define X (mxArray*)prhs[1]
-#define Y (mxArray*)prhs[2]
-#define METRIC (mxArray*)prhs[3]
-#define GRADATION (mxArray*)prhs[4]
-#define SPLITPBEDGE (mxArray*)prhs[5]
-#define NBV (mxArray*)prhs[6]
+#define BAMGGEOMETRY (mxArray*)prhs[0]
+#define BAMGOPTIONS  (mxArray*)prhs[1]
 
 /* serial output macros: */
@@ -35,5 +30,5 @@
 #define NLHS  3
 #undef NRHS
-#define NRHS  7
+#define NRHS  2
 
 #endif  /* _BAMG_H */
