150 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			150 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include "conversion.h"
 | 
						|
 | 
						|
 | 
						|
inline double getRatio(const double echelle)
 | 
						|
{
 | 
						|
	// Un ratio pour passer des distances (en mètres) à des pixels sur l'écran !
 | 
						|
	return (1.0e2 * PPCM) / echelle;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void conversionCoordonneesPixels(sfVector2f *const destinationPix, const Coordonnees *const sourceGPS, const Coordonnees *const pointCentral, const double echelle)
 | 
						|
{
 | 
						|
	const double ratio = getRatio(echelle);
 | 
						|
	double diff;
 | 
						|
	if ((sourceGPS->lon < 0 && pointCentral->lon < 0) || (sourceGPS->lon > 0 && pointCentral->lon > 0))
 | 
						|
	{
 | 
						|
		diff = sourceGPS->lon - pointCentral->lon;
 | 
						|
	}
 | 
						|
	else
 | 
						|
	{
 | 
						|
		if (pointCentral->lon<0)
 | 
						|
		{
 | 
						|
			diff = 360 + fabs(pointCentral->lon) - fabs(sourceGPS->lon);
 | 
						|
		}
 | 
						|
		else
 | 
						|
		{
 | 
						|
			diff = 360 - fabs(pointCentral->lon) - fabs(sourceGPS->lon);
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
 | 
						|
	destinationPix->x = (ratio * RAYON * RADIANS(diff) / REALMAPSIZE + 0.5) * WIDTH;
 | 
						|
	destinationPix->y = (ratio * RAYON * log(tan(PI / 4.0 + RADIANS(pointCentral->lat) / 2.0) / tan(PI / 4.0 + RADIANS(sourceGPS->lat) / 2.0)) / REALMAPSIZE + 0.5) * HEIGHT;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void conversionPixelsCoordonnees(Coordonnees *const destinationGPS, const sfVector2f *const sourcePix, const Coordonnees *const pointCentral, const double echelle)
 | 
						|
{
 | 
						|
	const double ratio = getRatio(echelle);
 | 
						|
 | 
						|
	destinationGPS->lon = DEGRES((sourcePix->x / WIDTH - 0.5) * REALMAPSIZE / (ratio * RAYON)) + pointCentral->lon;
 | 
						|
 | 
						|
	if (destinationGPS->lon < -180)
 | 
						|
	{
 | 
						|
		destinationGPS->lon = 360 + destinationGPS->lon;
 | 
						|
	}
 | 
						|
	else if (destinationGPS->lon > 180)
 | 
						|
	{
 | 
						|
		destinationGPS->lon -= 360;
 | 
						|
	}
 | 
						|
 | 
						|
	destinationGPS->lat = DEGRES(2 * atan(tan(PI / 4 + RADIANS(pointCentral->lat) / 2.0) / exp((sourcePix->y / HEIGHT - 0.5) * REALMAPSIZE / (ratio * RAYON))) - PI / 2);
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void conversionPoints(Point *tabPoint, uint32_t nbElements, const Coordonnees *const pointCentral, const double echelle)
 | 
						|
{
 | 
						|
	if(tabPoint == NULL)
 | 
						|
	{
 | 
						|
		fprintf(stderr, "%s\n", strerror(errno));
 | 
						|
		return;
 | 
						|
	}
 | 
						|
 | 
						|
	for(uint32_t i = 0; i < nbElements; i++)
 | 
						|
	{
 | 
						|
		conversionCoordonneesPixels(&tabPoint[i].posConv, &tabPoint[i].pos, pointCentral, echelle);
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void conversionRec(const Rectangle *const rec, Coordonnees *const hg, Coordonnees *const bd, const Coordonnees *const pointCentral, const double echelle)
 | 
						|
{
 | 
						|
	conversionPixelsCoordonnees(hg, &rec->positionHG, pointCentral, echelle);
 | 
						|
	conversionPixelsCoordonnees(bd, &rec->positionBD, pointCentral, echelle);
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void conversionAgglos(Agglomerat *agglo, uint32_t nbAgglos, const Coordonnees *const pointCentral, const double echelle)
 | 
						|
{
 | 
						|
	if(agglo == NULL)
 | 
						|
	{
 | 
						|
		fprintf(stderr, "%s\n", strerror(errno));
 | 
						|
		return;
 | 
						|
	}
 | 
						|
 | 
						|
	for(uint32_t i = 0; i < nbAgglos; i++)
 | 
						|
	{
 | 
						|
		conversionCoordonneesPixels(&agglo[i].moyConv,&agglo[i].moy,pointCentral,echelle);
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
char** conversionDates(Point *tabInitial, uint32_t nbElements)
 | 
						|
{
 | 
						|
	char **tabConverti = calloc(nbElements, sizeof(*tabConverti));
 | 
						|
 | 
						|
	if(tabConverti == NULL)
 | 
						|
	{
 | 
						|
		fprintf(stderr, "Allocation impossible (1).\n");
 | 
						|
		return NULL;
 | 
						|
	}
 | 
						|
 | 
						|
	for(uint32_t i = 0; i < nbElements; i++)
 | 
						|
	{
 | 
						|
		tabConverti[i] = calloc(DATESIZE, sizeof(*tabConverti[i]));
 | 
						|
 | 
						|
		if(tabConverti[i] == NULL)
 | 
						|
		{
 | 
						|
			fprintf(stderr, "Allocation impossible (2).\n");
 | 
						|
 | 
						|
			for(int32_t j = i - 1; j >= 0; j--)
 | 
						|
			{
 | 
						|
				free(tabConverti[j]);
 | 
						|
			}
 | 
						|
 | 
						|
			free(tabConverti);
 | 
						|
			return NULL;
 | 
						|
		}
 | 
						|
 | 
						|
		strcpy(tabConverti[i], ctime(&tabInitial[i].date));
 | 
						|
	}
 | 
						|
 | 
						|
	return tabConverti;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
char* getRidOfAccents(char *const input)
 | 
						|
{
 | 
						|
	ssize_t input_length = strlen(input);
 | 
						|
 | 
						|
	char* output = NULL;
 | 
						|
	size_t output_length = 0;
 | 
						|
 | 
						|
	// On tente une conversion de la chaîne pour virer les accents
 | 
						|
	if(unac_string("UTF-8", input, input_length, &output, &output_length) == -1)
 | 
						|
	{
 | 
						|
		fprintf(stderr, "Un problème est survenu durant la conversion de la chaîne: \"%s\". Code de retour: %s.\n", input, strerror(errno));
 | 
						|
		if(output != NULL)
 | 
						|
			free(output);
 | 
						|
		return NULL;
 | 
						|
	}
 | 
						|
 | 
						|
	// On peut maintenant libérer l'ancienne chaîne
 | 
						|
	free(input);
 | 
						|
 | 
						|
	// Et renvoyer la nouvelle !
 | 
						|
	return output;
 | 
						|
}
 | 
						|
 |