// Objectif : utiliser du multithreading pour améliorer la rapidité
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <pthread.h>

int PRECISION = 10;
char* filepath = "./in.txt";

int COLONNES;
int LIGNES;
int DISTANCE;
int* IMAGE;
int* ESPACE;

// taille du fichier. update global variables
int* taille(char* filepath){ 
    FILE* ptr;
	char ch;
	ptr = fopen(filepath, "r");
	if (NULL == ptr) {
		printf("file can't be opened \n");
	}
    int lignes = 0;
    int colonnes = 1;
    int end = 1;
	while (!feof(ptr)) {
        ch = fgetc(ptr);
        if (ch == '\n'){
            lignes ++;
            end = 0;
        }
        else if(ch == ',' && end) {
            colonnes ++;
        }
	}

	LIGNES = lignes;
    COLONNES = colonnes;
    DISTANCE = (int)round(sqrt(COLONNES *COLONNES + LIGNES *LIGNES));
}
//retourne la matrice 
int* read_img(char* filepath){ 
    int* matrice = malloc((COLONNES * LIGNES) * sizeof(int));
    
    FILE* ptr;
	char ch;
	ptr = fopen(filepath, "r");

	if (NULL == ptr) {
		printf("file can't be opened \n");
	}
    int c = 0;
	while (!feof(ptr)) {
        ch = fgetc(ptr);
        if (feof(ptr)){
            fclose(ptr);
            IMAGE = matrice;
            return NULL;
        }
        if (ch == '\n'){
            c++;
        }
        else if(ch == ',') {
            c++;
        }
        else if (ch == ' '){}
        else {
            //printf("%c\n", ch);
            matrice[c] *= 10;

            matrice[c] += (int)ch - 48;
        }
    }

}


double rho(int x, int y, double theta){
    return(x * cos(theta) + y * sin(theta));
}


void* espace_hough(void* arg){
    int thread = *(int*)arg; // numero du thread equivalent a la zone de degré qui va être parcourue
    int v_rho;
    double theta_rad;
    for (int y = 0; y < LIGNES; y++){
        for (int x = 0; x < COLONNES; x++){
            if (IMAGE[y*COLONNES + x] > 250){
                for (int theta_deg = 360 * thread ; theta_deg < 360 * (thread+1) ;theta_deg ++){
                   theta_rad = theta_deg /(180.0 * PRECISION) * M_PI;
                   v_rho = rho(x, y, theta_rad); // determine R
                   if (v_rho > 0){
                        ESPACE[v_rho * 360 * PRECISION + theta_deg] += 1;
                   }
                }
            }
        }
    }
    return(NULL);
}



void save_file(int* mat, int PRECISION){
    FILE *ptr;
    ptr = fopen("./out.txt","w");

    for (int j = 0; j < DISTANCE; j++){
        for (int i = 0; i<360 * PRECISION; i++){
            fprintf(ptr, "%d", mat[j*360 * PRECISION + i]);
            if (i != 360 * PRECISION - 1){
                fprintf(ptr, "%s",",");
            }
        }
        fprintf(ptr, "%s","\n");
    }
    
    fclose(ptr);

}


int main(int argc, char *argv[])
{
    if (argc == 2){
        PRECISION = strtol(argv[1], NULL, 10);
    }
    taille(filepath);

    printf("Precision : %d\nLignes : %d\nColonnes : %d\nnombre de pixels : %d\nDistance : %d\n", PRECISION, LIGNES, COLONNES,(LIGNES)*(COLONNES), DISTANCE);
    read_img(filepath);
    
    int* mat_hough = malloc(360 * PRECISION * DISTANCE * sizeof(int));
    for (int i = 0; i<360 * DISTANCE * PRECISION; i++){
        mat_hough[i] = 0;
    }
    ESPACE = mat_hough;

    pthread_t* t = (pthread_t*)malloc(sizeof(pthread_t)*(PRECISION));
    int* tab_int = malloc(PRECISION * sizeof(int));
    for(int i = 0; i < PRECISION; i++){
        tab_int[i] = i;
    }

    for (int i=0; i < PRECISION; i++) {
        pthread_create(&t[i], NULL, espace_hough, (void*)&tab_int[i]);
    }

    for (int i=0; i < PRECISION; i++) {
        pthread_join(t[i], NULL);
    }

    save_file(ESPACE, PRECISION);
    
    return 0;
}

