/**************************************************************************************/
/*																					  */
/*			Types de forme de la zone d'influence de chaque multiplieur				  */
/*																					  */
/*																					  */
/*	Emilie KOENIG														28/06/2007	  */
/*																					  */
/**************************************************************************************/

#include "main.h"


/**************************************************************************************/
/*			CarrŽ a orientation fixe												  */
/**************************************************************************************/

void shaper_carre(point4d_t pt_car, point4d_t inf_pt, point4d_t sup_pt, double Wi, double ri, double coef,int inf_ind, int sup_ind, int *liste_triee_x, point4d_t *liste_sommets, int version)
{
	int j, indic;

	switch (version)
	{
		case 0:
			for (j=inf_ind; j<=sup_ind; j++)
			{
				indic = liste_triee_x[j];
				if ((liste_sommets[indic].y>=inf_pt.y && liste_sommets[indic].y<=sup_pt.y) && (liste_sommets[indic].z>=inf_pt.z && liste_sommets[indic].z<=sup_pt.z))
					liste_sommets[indic].t *= Wi;
			}
			break;
		case 1:
			for (j=inf_ind; j<=sup_ind; j++)
			{
				indic = liste_triee_x[j];
				if ((liste_sommets[indic].y>=inf_pt.y && liste_sommets[indic].y<=sup_pt.y) && (liste_sommets[indic].z>=inf_pt.z && liste_sommets[indic].z<=sup_pt.z))
					liste_sommets[indic].t *= Wi;
			}
			break;
	}
}


/**************************************************************************************/
/*			Disque																	  */
/**************************************************************************************/
void shaper_disque(point4d_t pt_car, double Wi, double A, double coef, int inf_ind, int sup_ind, int maillage)
{
	int j, indic;
	double x, y, z, norm, den, num, angle;

	x = coef*pt_car.x;
	y = coef*pt_car.y;
	z = coef*pt_car.z;
	norm = sqrt(CARRE(x)+CARRE(y)+CARRE(z));

	for(j=inf_ind; j<=sup_ind; j++)
	{
		if (maillage == 0)
			indic = liste_triee_x[j];
		else if (maillage == 1 || maillage == 2)
			indic = j;
		den = sqrt(CARRE(liste_sommets[indic].x)+CARRE(liste_sommets[indic].y)+CARRE(liste_sommets[indic].z))*norm;
		num = liste_sommets[indic].x*x+liste_sommets[indic].y*y+liste_sommets[indic].z*z;
		angle = acos(num/den);
		if (angle<=A)
			liste_sommets[indic].t *= Wi;
	}
}


/**************************************************************************************/
/*			Shaper cos^2															  */
/**************************************************************************************/
void shaper_cos2(point4d_t pt_car, double Wi, double A, double ri, double coef, int inf_ind, int sup_ind, int maillage)
{
	int i, j, indic;
	double coord, ri2_4, OH, x, y, z, norm, den, num, angle;
	point4d_t pt_test;

	ri2_4 = CARRE(ri)*0.25;

	x = coef*pt_car.x;
	y = coef*pt_car.y;
	z = coef*pt_car.z;
	norm = sqrt(CARRE(x)+CARRE(y)+CARRE(z));

	switch (version)
	{
		case 0:
			for(j=inf_ind; j<=sup_ind; j++)
			{
				if (maillage == 0)
					indic = liste_triee_x[j];
				else if (maillage == 1 || maillage == 2)
					indic = j;
				den = sqrt(CARRE(liste_sommets[indic].x)+CARRE(liste_sommets[indic].y)+CARRE(liste_sommets[indic].z))*norm;
				num = liste_sommets[indic].x*x+liste_sommets[indic].y*y+liste_sommets[indic].z*z;
				angle = acos(num/den);
				if (angle<=A)
				{
					OH = cos(A)*Ray;
					coord = tan(angle)*OH;
					liste_sommets[indic].t *= exp(log(Wi?Wi:EPSILON)*CARRE(cos(T_PI*coord/ri)));
				}
			}
			break;
		case 1:
			for(j=inf_ind; j<=sup_ind; j++)
			{
				if (maillage == 0)
					indic = liste_triee_x[j];
				else if (maillage == 1 || maillage == 2)
					indic = j;
				den = sqrt(CARRE(liste_sommets[indic].x)+CARRE(liste_sommets[indic].y)+CARRE(liste_sommets[indic].z))*norm;
				num = liste_sommets[indic].x*x+liste_sommets[indic].y*y+liste_sommets[indic].z*z;
				angle = acos(num/den);
				if (angle<=A)
				{
					coord = angle*Ray;
					liste_sommets[indic].t *= exp(log(Wi?Wi:EPSILON)*CARRE(cos(T_PI*coord/ri)));
				}
			}
			break;
		case 2:
			for(j=inf_ind; j<=sup_ind; j++)
			{
				if (maillage == 0)
					indic = liste_triee_x[j];
				else if (maillage == 1 || maillage == 2)
					indic = j;
				den = sqrt(CARRE(liste_sommets[indic].x)+CARRE(liste_sommets[indic].y)+CARRE(liste_sommets[indic].z))*norm;
				num = liste_sommets[indic].x*x+liste_sommets[indic].y*y+liste_sommets[indic].z*z;
				angle = acos(num/den);
				if (angle<=A)
				{
					coord = tan(angle/2)*2*Ray;
					liste_sommets[indic].t *= exp(log(Wi?Wi:EPSILON)*CARRE(cos(T_PI*coord/ri)));
				}
			}
			break;
	}
}


/**************************************************************************************/
/*			Shaper exp																  */
/**************************************************************************************/
void shaper_exp(point4d_t pt_car, double Wi, double A, double ri, double coef, int inf_ind, int sup_ind, int maillage)
{
	int i, j, indic;
	double coord, ri2_4, OH, x, y, z, norm, den, num, angle;
	point4d_t pt_test;

	ri2_4 = CARRE(ri)*0.25;

	x = coef*pt_car.x;
	y = coef*pt_car.y;
	z = coef*pt_car.z;
	norm = sqrt(CARRE(x)+CARRE(y)+CARRE(z));

	switch (version)
	{
		case 0:
			for (j=0; j<=nb_sommets; j++)
			{
				if (maillage == 0)
					indic = liste_triee_x[j];
				else if (maillage == 1 || maillage == 2)
					indic = j;
				den = sqrt(CARRE(liste_sommets[indic].x)+CARRE(liste_sommets[indic].y)+CARRE(liste_sommets[indic].z))*norm;
				num = liste_sommets[indic].x*x+liste_sommets[indic].y*y+liste_sommets[indic].z*z;
				angle = acos(num/den);
				if (angle<=A)
				{
					OH = cos(A)*Ray;
					coord = CARRE(tan(angle)*OH);
					liste_sommets[indic].t *= exp(log(Wi?Wi:EPSILON)*exp(1.0-1.0/(1.0+EPSILON-CARRE(coord/ri2_4))));
				}
			}
			break;
		case 1:
			for (j=0; j<=nb_sommets; j++)
			{
				if (maillage == 0)
					indic = liste_triee_x[j];
				else if (maillage == 1 || maillage == 2)
					indic = j;
				den = sqrt(CARRE(liste_sommets[indic].x)+CARRE(liste_sommets[indic].y)+CARRE(liste_sommets[indic].z))*norm;
				num = liste_sommets[indic].x*x+liste_sommets[indic].y*y+liste_sommets[indic].z*z;
				angle = acos(num/den);
				if (angle<=A)
				{
					coord = CARRE(angle*Ray);
					liste_sommets[indic].t *= exp(log(Wi?Wi:EPSILON)*exp(1.0-1.0/(1.0+EPSILON-CARRE(coord/ri2_4))));
				}
			}
			break;
		case 2:
			for (j=0; j<=nb_sommets; j++)
			{
				if (maillage == 0)
					indic = liste_triee_x[j];
				else if (maillage == 1 || maillage == 2)
					indic = j;
				den = sqrt(CARRE(liste_sommets[indic].x)+CARRE(liste_sommets[indic].y)+CARRE(liste_sommets[indic].z))*norm;
				num = liste_sommets[indic].x*x+liste_sommets[indic].y*y+liste_sommets[indic].z*z;
				angle = acos(num/den);
				if (angle<=A)
				{
					coord = CARRE(tan(angle/2)*2*Ray);
					liste_sommets[indic].t *= exp(log(Wi?Wi:EPSILON)*exp(1.0-1.0/(1.0+EPSILON-CARRE(coord/ri2_4))));
				}
			}
			break;
	}
}
