#include "stdafx.h"
#include "math.h"
#include "BemSolve.h"

#include "geom.h"
#include "BemSolveDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBemSolveDoc-function for integration

double CBemSolveDoc::VnInteg (double xm,double ym,double tt,double txy,double& qx,double& qy)
{
	double sumzr,zr,x1,x2,y1,y2,ch,bx,by,sx,sy;
	int j;
   	sumzr=qx=qy=0.0f;
   	for (j=0;j<TriElems.GetSize();j++)
   	{
   		CIntElem* elt=GetIntElm(j);
    	zr=sx=sy=0.0f;
    	x1=elt->xp[0];x2=elt->xp[1];
    	y1=elt->yp[0];y2=elt->yp[1];
    	ch=Cell (x1,x2,y1,y2,xm,ym,tt,txy,bx,by);
    	zr+=ch;sx+=bx;sy+=by;
    	x1=elt->xp[1];x2=elt->xp[2];
    	y1=elt->yp[1];y2=elt->yp[2];
    	ch=Cell (x1,x2,y1,y2,xm,ym,tt,txy,bx,by);
    	zr+=ch;sx+=bx;sy+=by;
    	x1=elt->xp[2];x2=elt->xp[0];
    	y1=elt->yp[2];y2=elt->yp[0];
    	ch=Cell (x1,x2,y1,y2,xm,ym,tt,txy,bx,by);
    	zr+=ch;sx+=bx;sy+=by;
    	sumzr+=zr;qx+=sx;qy+=sy;
   	}
   	return sumzr;
}
/*---------------------------------------------------------*/
/*  INVEL - analytical integration of velocity             */
/*---------------------------------------------------------*/
void CBemSolveDoc::InVel (double xp,double yyp,double x1,double yy1,double x2,double yy2,double& hx,double& hy,double& gx,double& gy,double tt,double ts)
{
 	double yp,y1,y2,ra,rb,r,la,lb,nx,ny,d,xob,ko,eo,a2,b2,c2,fa,fb,
   		yoa,yob,dx1,dx2,dy2,dy1,s2,pi=3.14159265,pi2;
 	yp=yyp*ts;y2=yy2*ts;y1=yy1*ts;pi2=pi*0.5;
 	dx1=x1-x2;dy1=y1-y2;
 	c2=dx1*dx1+dy1*dy1;
 	dx1=x1-xp;dx2=x2-xp;dy1=y1-yp;dy2=y2-yp;
 	a2=dx1*dx1+dy1*dy1;
 	b2=dx2*dx2+dy2*dy2;
 	ra=sqrt(a2);rb=sqrt(b2);r=sqrt(c2);
 	la=log(ra);lb=log(rb);
 	ny=(x1-x2)/r;nx=(y2-y1)/r;
 	xob=x2*nx+y2*ny;ko=xp*nx+yp*ny;
 	d=xob-ko;
 	yoa=-ny*x1+nx*y1;
 	yob=-ny*x2+nx*y2;eo=-ny*xp+nx*yp;
 	if (fabs(d)<1e-10)
	{
		rb=yob-eo;ra=yoa-eo;
  		gx=(lb-la)*ny;
  		gy=-nx*(lb-la);
  		hx=hy=0.0;
 		gx*=tt;gy*=tt;
	}
	else
	{
 		fb=atan2 (yob-eo,d);fa=atan2 (yoa-eo,d);
 		if (fabs(fa)==pi2||fabs(fb)==pi2)
		{
			rb=yob-eo;ra=yoa-eo;
  			gx=(lb-la)*ny;
  			gy=-nx*(lb-la);
  			hx=hy=0.0;
 			gx*=tt;gy*=tt;
		}
		else
		{
 			s2=fa-fb;
 			gx=nx*s2+ny*(lb-la);
 			gy=ny*s2-nx*(lb-la);
 			hx=dy2/b2-dy1/a2;
 			hy=-dx2/b2+dx1/a2;
 			hx*=tt;hy*=tt;
 			gx*=tt;gy*=tt;
		}
	}
}
/*--------------------------------------------------*/
/*  INTEG - anal.integration of elements h a g      */
/*--------------------------------------------------*/
void CBemSolveDoc::Integ (double xp,double yp,double x1,double y1,double x2,double y2,double& hp,double& gp,double tt,double txy)
{
 	double r1,r2,r,a2,b2,c2,dx1,dx2,dy1,dy2,ln1,ln2,xo1,yo1,yo2,yop,xop,nx,ny,pi=3.1412569,
       dist,pi2,fi2,fi1,tg1,tg2;
 	pi2=0.5*pi;
 	dx1=x1-xp;dx2=x2-xp;dy1=y1-yp;dy2=y2-yp;
 	a2=dx1*dx1+txy*dy1*dy1;
 	b2=dx2*dx2+txy*dy2*dy2;
 	dx1=x1-x2;dy1=y1-y2;
 	c2=dx1*dx1+txy*dy1*dy1;
 	r1=sqrt(a2);r2=sqrt(b2);r=sqrt(c2);
 	ln1=log(r1);ln2=log(r2);
 	nx=(y2-y1)/r;ny=(x1-x2)/r;
 	xo1=nx*x1+ny*y1;xop=xp*nx+yp*ny;dist=xo1-xop;
 	yo2=-ny*x2+nx*y2;yo1=-ny*x1+nx*y1;yop=-ny*xp+nx*yp;
 	if (fabs(dist)<1e-8)
	{
 		r2=yo2-yop;r1=yo1-yop;
 		gp=-tt*(r2*(ln2-1)-r1*(ln1-1));
 		hp=0;
	}
	else
	{
 		tg1=(yo1-yop)/dist;tg2=(yo2-yop)/dist;
 		fi2=atan(tg2);fi1=atan(tg1);
 		if (fabs(fi2)==pi2||fabs(fi1)==pi2)
		{
 			r2=yo2-yop;r1=yo1-yop;
 			gp=-tt*(r2*(ln2-1)-r1*(ln1-1));
 			hp=0;
		}
		else
		{
 			hp=-tt*(fi2-fi1);
 			gp=-dist*tt*(tg2*(ln2-1)-tg1*(ln1-1)-fi1+fi2);
		}
	}
}
/*-------------------------------------*/
/* Cell - anal.integration in triangle */
/*-------------------------------------*/
double CBemSolveDoc::Cell (double x1,double x2,double y1,double y2,double x0,double y0,double tt,double txy,double& bx,double& by)
{
 	double e0,d,r,nx,ny,x01,k0,y02,y01,t1,t2,f1,f2,l1,l2,b,dx,dy,pi=3.14159265;
 	double p1,p2;
 	dx=x1-x2;dy=(y2-y1)*txy;
 	r=sqrt (dx*dx+dy*dy);
 	nx=dy/r;ny=dx/r;
 	x01=x1*nx+y1*ny;k0=x0*nx+y0*ny;
 	d=x01-k0;
 	if (fabs(d)<1e-8) {bx=by=0.0f;return(0.0);}
 	y01=-ny*x1+nx*y1;y02=-ny*x2+nx*y2;
 	e0=-ny*x0+nx*y0;
 	t2=(y02-e0)/(d+1e-20);t1=(y01-e0)/(d+1e-20);
 	f2=atan (t2);f1=atan (t1);
 	l1=log (fabs(d/cos(f1)));l2=log (fabs(d/cos(f2)));
 	p1=t1*(l1-1.5)+f1;p2=t2*(l2-1.5)+f2;
 	b=tt*d*d/2.0*(p2-p1);
	bx=-tt*d*((f2-f1)*nx-(l2-l1)*ny);
	by=-tt*d*((l2-l1)*nx+(f2-f1)*ny);
 	return b;
}

