include "constants.txt"; //constants used: PixToM, velToi, ribbonThickness, Infinity

string id="46";
//string id="37";
//string base = "c:\\Program Files\\ImageJ\\19\\xy304_10_19_a_";
//string base = "c:\\Program Files\\ImageJ\\05\\xy304_20_05_a_";
//string base = "e:\\46\\n5\\xy304_20_46_a_";
string base = "c:\\Program Files (x86)\\ImageJ\\"+id+"\\n5\\xy304_20_"+id+"_a_";


//string base = "c:\\Program Files (x86)\\ImageJ\\"+id+"\\xy316_25_"+id+"_a_";

ofstream foo(id+"Current.txt");
ofstream foo1(id+"CurrentDensityResults.txt");
ofstream foo2(id+"Area.txt");
ofstream foo3(id+"Depth.txt");
ofstream foo4(id+"Mouth.txt");

int dn=2;

int frameNo1,frameNo2;
for(int frameNo=42; frameNo<596; frameNo++) {

/**************** read in two files of boundary coordinates **********/
string s1 = base+"0"+frameNo+".txt";
string s2 = base+frameNo+".txt";
string fileName = frameNo < 100 ? s1:s2;


frameNo1 = frameNo + dn;

s1 = base+"0"+frameNo1+".txt";
s2 = base+frameNo1+".txt";
string fileName1 = frameNo1 < 100 ? s1:s2;

real dt = 1.0/frameRate*dn; //time interval, s
/*********************************************************************/


int n, n1, n2;

/* read in first file */
{
ifstream f(fileName);
 f >> n;
}

//add starting point to the end

real[int] xx(n+1), yy(n+1);


{
 ifstream f(fileName);
 f >> n;
 for(int i=0; i<n; i++) f >> xx[i] >> yy[i];
}

xx[n]=xx[0]; yy[n]=yy[0];

/* read in the second file */

{
ifstream f(fileName1);
 f >> n1;
}

//add starting point to the end

real[int] xx1(n1+1), yy1(n1+1);


{
 ifstream f(fileName1);
 f >> n1;
 for(int i=0; i<n1; i++) f >> xx1[i] >> yy1[i];
}

xx1[n1]=xx1[0]; yy1[n1]=yy1[0];



xx *= PixToM; yy *= PixToM; 
xx1 *= PixToM; yy1 *= PixToM; 


/* parameterise the boundary and mesh the region */


func real g(real[int] &p,real t)
{
 int k=floor(n*t), k1=k+1;
 if(k1>n) k1=0;
 real w = n*t-k;
 return(p[k]*(1-w)+w*p[k1]);
}

border S1(t=0,1.0/n) {x=g(xx,t); y=g(yy,t); label=1;} //left surface
border S2(t=1.0/n,1.0-4.0/n) {x=g(xx,t); y=g(yy,t); label=2;} //pit
border S3(t=1.0-4.0/n,1-3.0/n) {x=g(xx,t); y=g(yy,t); label=3;} //right surface
border S4(t=1.0-3.0/n,1.0-2.0/n) {x=g(xx,t); y=g(yy,t); label=4;} //right side
border S5(t=1.0-2.0/n,1.0-1.0/n) {x=g(xx,t); y=g(yy,t); label=5;} //top
border S6(t=1.0-1.0/n,1.0) {x=g(xx,t); y=g(yy,t); label=6;} //left side


border S7(t=1.0-4.0/n,1.0/n) {x=g(xx,t); y=g(yy,t); label=7;}//pit mouth



mesh Th, Th1;
try {


int np=n-4;


Th = buildmesh(S1(10)+S2(np-1)+S3(10)+S4(10)+S5(20)+S6(10));
//Th1= buildmesh(S2(np-1)+S7(1));

plot(Th,cmm="frameNo="+frameNo,wait=1);


/* get the pit boundary points */
real[int] px(np), py(np);
real t, t0=1.0/n, delt=(1.0-5.0/n)/(np-1);
px[0]=g(xx,t0); py[0]=g(yy,t0);
for(int i=1; i<np; i++) {t=t0+i*delt; px[i]=g(xx,t); py[i]=g(yy,t);}


/* calculate the boundary velocity */

real[int] vel(np-1);

real[int] Dxy(2), normal(2), PP(2), p1(2), p2(2), b(2);
real r, rmin, ar, armin, ss, Det, w;
real[int,int] A(2,2);
bool found;
int jmin;

for(int nn=0; nn<(np-1); nn++) {



	vel[nn]=0;

	
	PP[0] = (px[nn+1]+px[nn])/2.0;
	PP[1] = (py[nn+1]+py[nn])/2.0;
	
	//normal
	Dxy[0] = px[nn+1]-px[nn];
	Dxy[1] = py[nn+1]-py[nn];
        normal[0] = Dxy[1]; normal[1]= -Dxy[0];
	ss = 0;
	for(int i=0; i<2; i++) ss += normal[i]^2; ss = sqrt(ss);
  	for(int i=0; i<2; i++) normal[i] /= ss;


	found = false;
	armin = Infinity;

       
	for(int j=2; j<(n1-3); j++)
	{


		p1[0] = xx1[j-1]; p1[1] = yy1[j-1];
		p2[0] = xx1[j]; p2[1] = yy1[j];

		
		A = [[normal[0],p2[0]-p1[0]],[normal[1],p2[1]-p1[1]]];
		b = [p2[0]-PP[0],p2[1]-PP[1]];
		Det = A(0,0)*A(1,1)-A(0,1)*A(1,0);
		if(Det!=0) {
			r = (A(1,1)*b[0]-A(0,1)*b[1])/Det;
			w = (-A(1,0)*b[0]+A(0,0)*b[1])/Det;

			if(w>=0 & w<=1)
 				{
				found = true; ar = abs(r);
				if(ar<armin) {armin = ar; rmin = r; jmin=j;}
				}
			}

	}



if(found) if(rmin>0) vel[nn] = rmin/dt;

}



/*
Calculate the boundary velocity...
*/

real tol=PixToM;
func real fv(real X, real Y)
{
 int k1= -1;
 bool okx,oky;
 for(int k=0; k<(np-1); k++) {
  okx = px[k]<= X+tol & X<= px[k+1]+tol;
  okx = okx | (tol+px[k]>= X & tol+X>= px[k+1]);
  oky = (py[k]<= Y+tol & Y<= py[k+1]+tol);
  oky = oky | (tol+py[k]>= Y & tol+Y>= py[k+1]);
  if(okx & oky) {k1=k; break;}
  }
 return k1<0 ? 0:vel[k1];
}


//these define the metal surface
real slope = (yy[n-3]-yy[0])/(xx[n-3]-xx[0]), intercept=yy[0]-slope*xx[0];
real midpoint = (yy[n-3]+yy[0])/2.0;


foo<< frameNo << " " << ribbonThickness*int1d(Th,2)(velToi*fv(x,y)) << endl;


real xp, yp;
for(int nn=0; nn<(np-1); nn++) {
	xp = (px[nn+1]+px[nn])/2.0;
	yp = (py[nn+1]+py[nn])/2.0;


foo1 <<  frameNo << " " << xp << " " << yp << " " << velToi*vel[nn] << endl;
}


foo2<< frameNo << " " << Th.area << endl;

foo3<< frameNo << " " << midpoint-py.min << endl;

foo4<< frameNo << " " <<  sqrt((xx[1]-xx[n-4])^2+(yy[1]-yy[n-4])^2)<< endl;

} catch(...) {;}

}//end of loop