#include "main.h"

void synthese(int maillage)
{
	// Donnees calculees 
	int nb_points, i, N_intervalle;
	double umax, cste, H1, nb_points_mean;
	
	// Indices des points correspondants aux bornes de la zone d'influence
	double rr, rrr, intervalle, sigma, angle_int_1, angle_int_2, angle_int_L, interv6, interv2;
	
	// Points uniformement repartis sur la sphere
	double *Wi;
	double u;
	
	printf("Ray = %f, L = %f, r_min = %f\n", Ray, L, r_min);
	
	/*		Calcul du nombre de points a generer	*/
	//if (Ray >= 10)
	//	nb_points_mean = C*(1.0/(CARRE(r_min))-1.0)*8.0*CARRE(Ray);
	// nb_points = C*(1.0/(r_min*r_min)-1.0)*8.0*CARRE(Ray/L);
	//nb_points = 8.0*(1/CARRE(L))*((2*Ray*(1/CARRE(r_min)-1))+(2*L*Ray*(1/r_min-1))+(CARRE(L)*log(r_min)));
	//else
	//{	
/*		rr = r_min;
		intervalle = (1-r_min)/100000;
		rrr = rr+intervalle;*/
		N_intervalle = 100000;
		rr = r_min ;
		intervalle = (L-rr)/N_intervalle;
		interv6=intervalle/6;
		interv2=intervalle/2;
		rrr = rr+interv2;
		
		if (version == STEREOGRAPHIQUE)
		{
			angle_int_1 = 2*atan(rr/(4.0*Ray));
			angle_int_2 = 2*atan(rrr/(4.0*Ray));
			angle_int_L = 2*atan(L/(4.0*Ray));
		}
		else
		{
			angle_int_1 = rr/2/Ray;
			angle_int_2 = rrr/2/Ray;
			angle_int_L = L/2/Ray;
		}
		
		//nb_points_mean = 0.5*intervalle*((2.0/((1-cos(angle_int_1))*rr))+(2.0/((1-cos(angle_int_2))*CARRE(Ray+rrr)*rrr)));
		nb_points_mean = interv6 * ( 2.0/((1-cos(angle_int_1))*rr) + 2.0/((1-cos(angle_int_L))*L) + 4*2.0/((1-cos(angle_int_2))*rrr));
		//printf("nb_points_mean initial = %f\n", nb_points_mean);
	
		for(i=1; i<=N_intervalle-1; i++)
		{
			if (version == STEREOGRAPHIQUE)
			{
				angle_int_1 = 2*atan(rr/(4.0*Ray));
				angle_int_2 = 2*atan(rrr/(4.0*Ray));
				angle_int_L = 2*atan(L/(4.0*Ray));
			}
			else
			{
				angle_int_1 = rr/2/Ray;
				angle_int_2 = rrr/2/Ray;
				angle_int_L = L/2/Ray;
			}
			rr = rr+intervalle;
			rrr = rr+interv2;
			//nb_points_mean = nb_points_mean + 0.5*intervalle*((2.0/((1-cos(angle_int))*rr))+(2.0/((1-cos(rrr/2))*CARRE(Ray+rrr)*rrr)));
			nb_points_mean = nb_points_mean + interv6*( 2*2.0/((1-cos(angle_int_1))*rr) + 4*2.0/((1-cos(angle_int_2))*rrr) );
		}
		//printf("nb_points_mean = %f\n", nb_points_mean);

	//}
    
    //u = (double)randn();
	u = (double)randu();
	//nb_points_mean = (16/CARRE(L))*((CARRE(Ray)/2)*((1/CARRE(r_min))-1)+2*L*((1/r_min)-1)-CARRE(L)*log(r_min));
	sigma = sqrt(nb_points_mean);
	nb_points = (int)((u*sigma)+nb_points_mean);
    //nb_points = 5555555;
	printf("%d multiplieurs sont a generer\n",nb_points);
	
	// Initialisation du generateur de Mersennes de points aleatoires
	if (graine == (unsigned long) 0)
		graine = time(NULL);
	rand_init(graine);
	printf("graine = %lu \n",graine);
	
	/** Calcul des constantes utilisees pour le calcul des valeurs des multiplieurs et du parametre de normalisation H1 **/
	Wi = (double *)malloc(nb_points*sizeof(double));
	if (loi == LOG_POISSON)
	{
		printf("loi log Poisson\n");
		H1 = C*(1.0-param1);
		for (i=0; i<nb_points; ++i)
			Wi[i] = param1;
	}
	else if (loi == LOG_NORMAL)
	{
		printf("loi log Normale\n");
		cste = sqrt(param2);
		H1 = C*(1.0-exp(param1+param2/2.0));
		for (i=0; i<nb_points; ++i)
		{
			u = (double)randu();
			Wi[i] = exp(cste*u+param1);
		}
	}
	else if (loi == LOG_EXP)
	{
		printf("loi log Exponentielle\n");
		cste = pow(1.0+param1,1.0/param1);
		H1 = 0.0;
		for (i=0; i<nb_points; ++i)
		{
			u = (double)randu();
			Wi[i] = pow(cste*u,param1);
		}
	}
	else //if (loi == LOG_BESSEL)
	{
		printf("loi log Bessel\n");
		cste = 1.0/param1;
		H1 = C*(1.0/(param1+1.0));
		for (i=0; i<nb_points; ++i)
		{
			u = (double)randu();
			Wi[i] = pow(u,cste);
		}
	}

	for (i=0; i<nb_sommets; i++)
		liste_sommets[i].t = 1.0/pow(r_min, H1);
	
	
	umax = 1.0/(CARRE(r_min));
	
	if (version == ECLAIREMENT_CONE)
	{
		printf("version : cone d'eclairement\n");
		synthese_eclairement(nb_points, umax, Wi);
	}
	else if (version == ANGLE_SOLIDE)
	{
		printf("version : angle solide\n");
		synthese_angle(nb_points, umax, Wi);
	}
	else //if (version == STEREOGRAPHIQUE)
	{
		printf("version : dilatation stereographique\n");
		synthese_stereo(nb_points, umax, Wi);
	}
}
