#include <stdio.h>


extern double *ListTri;
extern double *texture;
extern int     NBTri;
extern int     NbPts;
extern double *ListCoordX;
extern double *ListCoordY;
extern double *ListCoordZ;
extern double *ListCoordX_tore;
extern double *ListCoordY_tore;
extern double *ListCoordZ_tore;
extern double *texture_tore;
extern double *ListCoordX_E;
extern double *ListCoordY_E;
extern double *ListCoordZ_E;
extern double *texture_E;
extern double *texture_E_Affiche;
extern double *ListPhi;
extern double *ListTheta;
extern int NbLat;

/* ######################################################## */
/* Affichage des coordonnees d'un triangle sur l'ecran      */
/* ######################################################## */
void PrintTriangle( double **t )
{
  int i;
  
  for (i=0; i<3; i++) {
    printf( "(%f, %f, %f) ", t[i][0], t[i][1], t[i][2] );
  }
  printf( "\n" );
}


/* ######################################################## */
/* Affichage des coordonnees d'une liste de triangles       */
/* ######################################################## */
void PrintListTriangle( double ***list, int nb )
{
  int i;
  
  for (i=0; i<nb; i++) {
    PrintTriangle( list[i] );
  }
}


/* ######################################################## */
/* Lecture des triangles des coordonnees d'un fichier texte */
/* ######################################################## */
void LectFich3d( int *nb, FILE *fp)
{
  int          i;

  fscanf( fp, "%d", nb );
  
  //ListTri = (double *) malloc( (*nb) * 9 * sizeof(double) );
	
  int taille = (* nb) * 9 ;
  ListTri = new double[taille] ;
  for ( i=0; i<*nb; i++ ) {
    fscanf( fp, "%lf %lf %lf %lf %lf %lf %lf %lf %lf",
	    ListTri+i*9, ListTri+i*9+1, ListTri+i*9+2,
	    ListTri+i*9+3, ListTri+i*9+4, ListTri+i*9+5,
	    ListTri+i*9+6, ListTri+i*9+7, ListTri+i*9+8);
  }
  fclose( fp );
}



/* ######################################################## */
/* Lecture des triangles des coordonnees et de texture      */
/* ######################################################## */
void LectFich4d( int *nb, FILE *fp )
{
  int          i;

  fscanf( fp, "%d", nb );
  
  //ListTri = (double *) malloc( (*nb) * 9 * sizeof(double) );
  int taille = (* nb) * 9 ;
  ListTri = new double[taille] ;

  //texture = (double *) malloc( 3 * (*nb) * sizeof(double) );
  taille = (* nb) * 5 ;
  texture = new double[taille] ;

  for ( i=0; i<*nb; i++ ) {
    fscanf( fp, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
	    ListTri+i*9, ListTri+i*9+1, ListTri+i*9+2,
	    texture+3*i,
	    ListTri+i*9+3, ListTri+i*9+4, ListTri+i*9+5,
	    texture+3*i+1,
	    ListTri+i*9+6, ListTri+i*9+7, ListTri+i*9+8,
	    texture+3*i+2 );
	    
  }
  fclose( fp );
}


void LectFichEqui4d(int *nb, FILE *fp)
{
	int i, j, taille;
	
	fscanf(fp, "%d", nb);
	taille = (* nb)*(* nb);
	ListCoordX = new double[taille];
	ListCoordY = new double[taille];
	ListCoordZ = new double[taille];
	texture = new double[taille];
	texture_E = new double[taille];
	ListCoordX_E = new double[taille];
	ListCoordY_E = new double[taille];
	ListCoordZ_E = new double[taille];
	texture_E_Affiche = new double[taille];
	
	for (i=0; i<taille; i++)
	{
		fscanf(fp, "%lf %lf %lf %lf", &ListCoordX[i], &ListCoordY[i], &ListCoordZ[i], &texture[i]);
	}
	fclose(fp);

}

void LectFichHEALPix4d(int *nb, FILE *fp)
{
	int i, j, taille;
	
	fscanf(fp, "%d", nb);
	taille = (* nb);
	ListCoordX = new double[taille];
	ListCoordY = new double[taille];
	ListCoordZ = new double[taille];
	ListTheta = new double[taille];
	ListPhi = new double[taille];
	texture = new double[taille];
	
	for (i=0; i<taille; i++)
	{
		fscanf(fp, "%lf %lf %lf %lf", &ListCoordX[i], &ListCoordY[i], &ListCoordZ[i], &texture[i]);
	}
	for (i=0; i<taille; i++)
	{
		fscanf(fp, "%lf", &ListTheta[i]);
	}
	for (i=0; i<taille; i++)
	{
		fscanf(fp, "%lf", &ListPhi[i]);
	}
	fclose(fp);

}



void LectFichElevation(FILE *fp)
{
	int i, j, taille, nb, tmp;
	double x, y, z;
	
	fscanf(fp, "%d", &tmp);
	fscanf(fp, "%d", &tmp);
	fscanf(fp, "%d", &nb);
	taille = (nb)*(nb);
	
	
	for (i=0; i<taille; i++)
	{
		fscanf(fp, "%lf %lf %lf %lf", &x, &y, &z, &texture_E[i]);
	}
	fclose(fp);

}

void LectFichTore(FILE *fp)
{
	int i, j;
	
	printf("NbPts = %d\n", NbPts);
	
	ListCoordX_tore = new double[NbPts];
	ListCoordY_tore = new double[NbPts];
	ListCoordZ_tore = new double[NbPts];
	texture_tore = new double[NbPts];
	
	for (i=0; i<NbPts; i++)
	{
		fscanf(fp, "%lf %lf %lf %lf", &ListCoordX_tore[i], &ListCoordY_tore[i], &ListCoordZ_tore[i], &texture_tore[i]);
	}
	fclose(fp);

}




