/*! \file  ContourtoNodes
    \brief: takes a  contour file, and figures out which nodes  (x,y list)
    are inside this contour. 

	usage:
	[flags]=ContourToNodes(x,y,contours,interptype,edgevalue);
	
	where:

	input:

		x,y: node cooredinates
		
		contours: structure holding sets of vertices making open contours. 
		
		interptype: string definining type of interpolation ('element', or 'node', or 'element and node');

		edgevalue: integer (0, 1 or 2) defining the value associated to the nodes on the edges of the polygons

	output:
		flags: vector of flags (0 or 1), of size nods 
*/
	
#include "./ContourToNodes.h"

void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {

	int i,j;

	/* required input: */
	double* x=NULL;
	double* y=NULL;
	int     edgevalue;
	char*   interptype=NULL;

	/* output: */
	Vector*  flags=NULL;
	int  nods;

	//contours
	mxArray*  matlabstructure=NULL;
	int numcontours;
	Contour** contours=NULL;
	Contour*  contouri=NULL;

	/*Boot module: */
	MODULEBOOT();

	/*checks on arguments on the matlab side: */
	CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,&ContourToNodesUsage);

	
	/*Fetch inputs: */
	FetchData(&x,&nods,NULL,XHANDLE);
	FetchData(&y,NULL,NULL,YHANDLE);
	FetchData(&edgevalue,EDGEVALUEHANDLE);

	//Fetch contours

	if(mxIsChar(FILENAME)){
		/*Call expread on filename to build a contour array in the matlab workspace: */
		mexCallMATLAB( 1, &matlabstructure, 1, (mxArray**)&FILENAME, "expread");
	}
	else{
		/*FILENAME is actually a structure, coming directly from expread: */
		matlabstructure=(mxArray*)FILENAME;
	}

	numcontours=mxGetNumberOfElements(matlabstructure);
	contours=(Contour**)xmalloc(numcontours*sizeof(Contour*));
	for(i=0;i<numcontours;i++){
		//allocate
		contouri=(Contour*)xmalloc(sizeof(Contour));
		//retrieve dimension of this contour.
		contouri->nods=(int)mxGetScalar(mxGetField(matlabstructure,i,"nods"));
		//set pointers.
		contouri->x=mxGetPr(mxGetField(matlabstructure,i,"x"));
		contouri->y=mxGetPr(mxGetField(matlabstructure,i,"y"));
		*(contours+i)=contouri;
	}

	/* Debugging of contours :{{{1*/
	/*for(i=0;i<numcontours;i++){
		_printLine_("\nContour echo: contour number  " << i+1 << " / " << numcontours);
		contouri=*(contours+i);
		_printLine_("   Number of nodes " << contouri->nods);
		for (j=0;j<contouri->nods;j++){
			_printLine_("   " << *(contouri->x+j) << "f " << *(contouri->y+j) << "f");
		}
	}*/
	/*}}}*/

	/*Run interpolation routine: */
	ContourToNodesx(&flags,x,y,nods,contours,numcontours,edgevalue);

	/* output: */
	WriteData(FLAGS,flags);

	/*end module: */
	MODULEEND();

}

void ContourToNodesUsage(void){
	_printLine_("   usage:");
	_printLine_("   [flags]=ContourToNodes(x,y,contourname,edgevalue);\n");
	_printLine_("   where:");
	_printLine_("      x,y: list of nodes.");
	_printLine_("      contourname: name of .exp file containing the contours, or resulting structure from call to expread.");
	_printLine_("      interptype: string definining type of interpolation ('element', or 'node').");
	_printLine_("      edgevalue: integer (0, 1 or 2) defining the value associated to the nodes on the edges of the polygons.");
	_printLine_("      flags: vector of flags (0 or 1), of size nods.");
	_printLine_("");
}
