Skip to content
Snippets Groups Projects
Commit 9b664000 authored by poulpe's avatar poulpe
Browse files

[Update] Add tree2matrix

parent fdf13216
No related branches found
No related tags found
No related merge requests found
......@@ -4,3 +4,4 @@ tests
Exe
exe
.vscode
*.pgm
\ No newline at end of file
......@@ -8,19 +8,19 @@
#include "Matrix.h"
// Alloc memory for matrix
error_code matrix_alloc(matrix *mat, int32_t m, int32_t n)
error_code matrix_alloc(matrix *mat, int32_t col, int32_t lin)
{
mat->data = malloc(m * sizeof(int32_t*));
mat->col = m;
mat->lin = n;
mat->data = malloc(lin * sizeof(int32_t*));
mat->col = col;
mat->lin = lin;
if(mat->data == NULL){
return UNINITIALIZED;
}
for(int i=0;i<m;i+=1)
for(int i=0;i<col;i+=1)
{
mat->data[i] = malloc(n * sizeof(int32_t));
mat->data[i] = malloc(lin * sizeof(int32_t));
}
if(mat->data == NULL){
......@@ -70,16 +70,16 @@ error_code matrix_destroy(matrix *mat){
}
// Init matrix from array of int32
error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n,int32_t data[])
error_code matrix_init_from_array(matrix *mat, int32_t lin, int32_t col,int32_t data[])
{
// if (mat->data != NULL){
// matrix_destroy(mat);
// }
matrix_alloc(mat, m,n);
matrix_alloc(mat, lin,col);
matrix_init(mat);
for(int i=0,k=0;i<m;i+=1)
for(int i=0,k=0;i<lin;i+=1)
{
for(int j=0;j<n;j+=1,k+=1)
for(int j=0;j<col;j+=1,k+=1)
{
mat->data[i][j] = data[k];
}
......
......@@ -20,7 +20,7 @@ typedef struct _matrix {
bool matrix_is_equal(matrix mat1, matrix mat2);
error_code matrix_init(matrix *mat);
error_code matrix_alloc(matrix *mat, int32_t m, int32_t n);
error_code matrix_alloc(matrix *mat, int32_t lin, int32_t col);
error_code matrix_destroy(matrix *mat);
error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n,int32_t data[]);
error_code matrix_to_array(matrix mat,int32_t *table);
......
......@@ -14,16 +14,36 @@ int main()
pgm_read_from_file(p,"buzz.pgm");
uint32_t val = quadtree_get_depth_from_image_size(p->pixels.col);
printf("Depth : %u\n",val);
// matrix_print(p->pixels);
node* tree = quadtree_tree_create(val);
matrix_print(p->pixels);
// matrix_print(p->pixels);
quadtree_matrix2tree(&p->pixels,tree);
quadtree_print(tree,1,"|");
pgm* pp = malloc(1 * sizeof(pgm));
pp->max = p->max;
matrix_alloc(&pp->pixels,p->pixels.lin,p->pixels.col);
// matrix mat;
// matrix_alloc(&mat,p->pixels.lin,p->pixels.col);
// matrix_init(&mat);
quadtree_tree2matrix(tree,&pp->pixels);
// // matrix_print(mat);
// // quadtree_print(tree,1,"|");
// pgm *pp = malloc(1 * sizeof(pgm));
// pp->max = 256;
// pp->pixels = mat;
pgm_write_from_file(pp,"Test.pgm");
matrix_destroy(&p->pixels);
quadtree_tree_destroy(&tree);
matrix_destroy(&pp->pixels);
quadtree_tree_destroy_clean(&tree);
free(p);
free(pp);
return 0;
}
\ No newline at end of file
......@@ -64,7 +64,7 @@ void quadtree_print(node *arbre, int32_t decal, char *sep)
{
printf("%s", sep);
}
printf("%d\n", arbre->info);
printf("%d\n", (uint32_t)arbre->data);
decal++;
if (!quadtree_is_leaf(arbre))
{
......@@ -94,7 +94,7 @@ void quadtree_sum(node *arbre)
}
for (int32_t i = 0; i < CHILDREN; i++)
{
arbre->info += arbre->child[i]->info;
arbre->data += arbre->child[i]->data;
}
}
}
......@@ -106,13 +106,29 @@ node *quadtree_position(int32_t li, int32_t col, node *a, int32_t d)
{
int32_t ligne = (li>>d) & 1; // Permet de sélectionne le bit à considérer en fonction de la profondeur; Exemple: 10 >> 1 = 1
int32_t colonne = (col>>d) & 1; //Exemple: 10 >> 1 = 1;
int32_t index = ligne<<1 | colonne; //Exemple: 1<<1 = 10 | 1(col) = 11=>3
int32_t index = (ligne<<1) | colonne; // Exemple: 1<<1 = 10 | 1(col) = 11=>3
crt = crt->child[index];
d--;
} while (!quadtree_is_leaf(crt));
return crt;
}
double quadtree_position_value(int32_t li, int32_t col, node *a, int32_t d)
{
node *crt = a;
do
{
int32_t ligne = (li>>d) & 1; // Permet de sélectionne le bit à considérer en fonction de la profondeur; Exemple: 10 >> 1 = 1
int32_t colonne = (col>>d) & 1; //Exemple: 10 >> 1 = 1;
int32_t index = (ligne<<1) | colonne; // Exemple: 1<<1 = 10 | 1(col) = 11=>3
crt = crt->child[index];
d--;
} while (!quadtree_is_leaf(crt));
return crt->data;
}
void quadtree_matrix2tree(matrix *mat, node *arbre)
{
int32_t d = quadtree_depth(arbre) - 1;
......@@ -121,11 +137,36 @@ void quadtree_matrix2tree(matrix *mat, node *arbre)
for (int32_t co = 0; co < mat->col; co++)
{
node *crt = quadtree_position(li, co, arbre, d);
crt->info = mat->data[li][co];
crt->data = mat->data[li][co];
}
}
}
void quadtree_tree2matrix(node *tree, matrix *mat)
{
int32_t d = quadtree_depth(tree) - 1;
for (int32_t li = 0; li < mat->lin; li++)
{
for (int32_t co = 0; co < mat->col; co++)
{
mat->data[li][co] = quadtree_position_value(li, co, tree, d);
}
}
}
void quadtree_vertical_symetry(node *tree)
{
// Inversé les colones
}
void quadtree_horizontal_symetry(node *tree)
{
}
void quadtree_central_symetry(node *tree)
{
}
void quadtree_tree_destroy(node **tree)
{
node *a = *tree;
......@@ -141,3 +182,21 @@ void quadtree_tree_destroy(node **tree)
}
}
}
void quadtree_tree_destroy_clean(node **tree)
{
if(NULL != *tree)
{
if(!quadtree_is_leaf(*tree))
{
for (uint32_t i = 0; i < CHILDREN; i+=1)
{
quadtree_tree_destroy_clean(&(*tree)->child[i]);
}
}
else
{
free(*tree);
}
}
}
\ No newline at end of file
......@@ -10,7 +10,8 @@
#define CHILDREN 4
typedef struct _node {
int info;
double data;
double average_2;
struct _node* child[CHILDREN];
} node;
......@@ -37,3 +38,7 @@ node *quadtree_position(int32_t li, int32_t col, node *a, int32_t d);
void quadtree_matrix2tree(matrix *mat, node *arbre);
void quadtree_tree_destroy(node **tree);
void quadtree_tree2matrix(node *tree,matrix* mat);
void quadtree_tree_destroy_clean(node **tree);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment