Function set_arrow() OK + comments & cleaning

This commit is contained in:
Jean Sirmai 2023-09-28 02:47:06 +02:00
parent ad9829aa40
commit 603153293b
Signed by: jean
GPG Key ID: FB3115C340E057E3
1 changed files with 115 additions and 39 deletions

View File

@ -108,6 +108,12 @@ static void graphics_debug_callback(GLenum source, GLenum type, GLuint id,
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/*
* void graphics_erase_line (GLuint a, GLuint b){} TODO and to use ?
* As, in this version only lines can be erased,
* similar functions for vertex and plans are not considered. But ?...
*/
/* /*
* Writes values to describe a line from a to b into the line buffer * Writes values to describe a line from a to b into the line buffer
* *
@ -539,14 +545,30 @@ bool graphics_shutdown(const void *gl_area)
} }
/*
* Prints the arrows[] array
*
* For each arrow the following parameters are displayed :
* - rank in the array
* - weight (or load)
* - coordinates in space (site, x, y, z)
*/
static void show_arrows_array (GLuint *arrows, int arrows_nb, int x, int y, int z) static void show_arrows_array (GLuint *arrows, int arrows_nb, int x, int y, int z)
{ {
printf("\n [rank] load | site x y z"); printf("\n [rank] load | site x y z");
for (int i = 0; i < arrows_nb; i++) for (int i = 0; i < arrows_nb; i++)
printf("\n [%4d] = %2d | %2d, %2d, %2d, %2d", i, *(arrows + i * 5 + 0),\ printf("\n [%4d] = %2d | %2d, %2d, %2d, %2d", i, *(arrows + i * 5 + 0),\
*(arrows + i * 5 + 1), *(arrows + i * 5 + 2), *(arrows + i * 5 + 3), *(arrows + i * 5 + 4)); *(arrows + i * 5 + 1), *(arrows + i * 5 + 2), *(arrows + i * 5 + 3), *(arrows + i * 5 + 4));
if (arrows_nb == 0) printf("\n [NULL] ---- | ---- --- --- ---");
} }
/*
* Prints the initial user choices :
* - space dimension size and appearance (grids)
* - arrows[] array
* NB The space may be empty or saturated with arrows or any value in between
* Only one arrow per possible coordinates with a load max equal to ? TODO
*/
static void show_user_choices(GLuint *arrows, int arrows_nb, static void show_user_choices(GLuint *arrows, int arrows_nb,
int space_size_x, int space_size_y, int space_size_z, int space_size_x, int space_size_y, int space_size_z,
int prefer, int show_all) int prefer, int show_all)
@ -560,9 +582,63 @@ static void show_user_choices(GLuint *arrows, int arrows_nb,
show_arrows_array (arrows, arrows_nb, space_size_x, space_size_y, space_size_z); show_arrows_array (arrows, arrows_nb, space_size_x, space_size_y, space_size_z);
} }
/*
* Prints the result of the function set_arrow()
* and indicates the reasons of the choice (call) this function makes (see this function)
*/
static void show_user_action(GLuint *arrows, int arrows_nb, int address, int requested_weight,
int current_weight, int site, int x, int y, int z)
{
if (address == -1 && requested_weight > 0) {printf("no such arrow found (%d,%d,%d,%d)\n\
requested weight == %d => CREATE",\
arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], requested_weight); return;}
if (address >= 0 && requested_weight == 0) {printf("arrow (%d,%d,%d,%d) found at address %d; current_weight = %d;\n\
requested weight == %d => ERASE",\
arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], address/5, current_weight, requested_weight); return;}
if (address >= 0 && current_weight != requested_weight) {printf("arrow (%d,%d,%d,%d) found at address %d; current_weight = %d;\n\
requested weight != current weight => MODIFY",\
arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], address/5, current_weight); return;}
if (address >= 0 && current_weight == requested_weight){
printf("arrow (%d,%d,%d,%d) found at address %d;\n\
current_weight == requested_weight => END",\
arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], address/5); return;}
if (address == -1 && requested_weight == 0) {printf("no such arrow found (%d,%d,%d,%d) && requested weight == 0 => END",\
arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4]); return;}
}
/* /*
* assigns a new weight to the arrow at address (address) * Prints vertex and lines buffers_states (sizes) at major steps and at the end of a session.
* Major steps are creation of grids and creation of arrows
* Arithmetic verification is provided at each step
*/
static void show_buffers_states(int space_X, int space_Y, int space_Z, int arrows_nb,
int offset_after_grids, int buffer_vertex_size,
int buffer_lines_size_after_cubes, int buffer_lines_size)
{
int offset_after_arrows = buffer_vertex_size / 3, difference = offset_after_arrows - offset_after_grids;
printf("\n - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
printf("\n buffer_vertex_offset_after grids = %6d\t%6d = (x+1)*(y+1)*(z+1); <--> (x,y,z = %d,%d,%d)",\
offset_after_grids, (space_X + 1)*(space_Y + 1)*(space_Z + 1), space_X, space_Y, space_Z);
printf("\n buffer_vertex_offset_after arrows = %6d\t%6d = %d + %d; <--> %d = 12 x %d (there are %d cubes)",\
offset_after_arrows, offset_after_arrows, offset_after_grids,\
difference, difference, difference / 12, difference / 12);
printf("\n buffer_lines_offset after cubes = %6d\t%6d = 2 * (%dx%d + %dx%d + %dx%d); <--> 2 * (x*y + x*z + y*z)",\
buffer_lines_size_after_cubes, ((space_X+1) * (space_Y+1) + (space_X+1) * (space_Z+1) + (space_Y+1) * (space_Z+1)) * 2,
space_X+1, space_Y+1, space_X+1, space_Z+1, space_Y+1, space_Z+1);
printf("\n buffer_lines_offset after arrows = %6d\t%6d = %d + %d; <--> %d = (3 + 4) x 2 x %d (arrows_nb = %d)\n",\
buffer_lines_size, buffer_lines_size, buffer_lines_size_after_cubes,\
buffer_lines_size - buffer_lines_size_after_cubes, arrows_nb * 14, arrows_nb, arrows_nb);
}
/*
* Assigns a new weight to the arrow at address (address)
*/ */
static int rewrite_arrow (GLuint *arrows, int arrows_nb, int address, int weight, int site, int x, int y, int z) static int rewrite_arrow (GLuint *arrows, int arrows_nb, int address, int weight, int site, int x, int y, int z)
{ {
@ -573,6 +649,10 @@ static int rewrite_arrow (GLuint *arrows, int arrows_nb, int address, int weight
return arrows_nb; return arrows_nb;
} }
/*
* Creates a new arrow at address (address)
* NB Weights (or loads) are NOT added
*/
static int create_arrow (GLuint *arrows, int arrows_nb, int weight, int site, int x, int y, int z) static int create_arrow (GLuint *arrows, int arrows_nb, int weight, int site, int x, int y, int z)
{ {
buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint)); buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint));
@ -592,6 +672,9 @@ static int create_arrow (GLuint *arrows, int arrows_nb, int weight, int site, in
return arrows_nb; return arrows_nb;
} }
/*
* Removes an arrow at address (address)
*/
static int erase_arrow (GLuint *arrows, int arrows_nb, int address, int site, int x, int y, int z) static int erase_arrow (GLuint *arrows, int arrows_nb, int address, int site, int x, int y, int z)
{ {
arrows_nb --; arrows_nb --;
@ -612,9 +695,14 @@ static int erase_arrow (GLuint *arrows, int arrows_nb, int address, int site, in
return arrows_nb; return arrows_nb;
} }
/*
* Calls one of the functions create_arrow(), erase_arrow() or rewrite_arrow()
* according to requested weight and address (coord site, x, y, z)
*
* May not call any of these three functions if :
* - Current_weight of an arrow located at the requested address == requested_weight
* - No arrow was found at the requested addres AND current_weight == requested_weight
*/
static int set_arrow (GLuint *arrows, int arrows_nb, int requested_weight, int site, int x, int y, int z) static int set_arrow (GLuint *arrows, int arrows_nb, int requested_weight, int site, int x, int y, int z)
{ {
int address = -1, current_weight = -1; int address = -1, current_weight = -1;
@ -633,32 +721,32 @@ static int set_arrow (GLuint *arrows, int arrows_nb, int requested_weight, int s
printf("\nset_arrow() invoked with requested weight = %d > ", requested_weight); printf("\nset_arrow() invoked with requested weight = %d > ", requested_weight);
if (address == -1 && requested_weight > 0) {printf("no such arrow found\n\ show_user_action(arrows, arrows_nb, address, requested_weight, current_weight, site, x, y, z);
requested weight == %d => CREATE", requested_weight);
return create_arrow (arrows, arrows_nb, requested_weight, site, x, y, z);}
if (address >= 0 && requested_weight == 0) {printf("arrow (%d,%d,%d,%d) found at address %d; current_weight = %d;\n\ if (address == -1 && requested_weight > 0)
requested weight == %d => ERASE",\ return create_arrow (arrows, arrows_nb, requested_weight, site, x, y, z);
arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], address/5, current_weight, requested_weight);
return erase_arrow (arrows, arrows_nb, address, site, x, y, z);}
if (address >= 0 && current_weight != requested_weight) {printf("arrow (%d,%d,%d,%d) found at address %d; current_weight = %d;\n\ if (address >= 0 && requested_weight == 0)
requested weight != current weight => MODIFY",\ return erase_arrow (arrows, arrows_nb, address, site, x, y, z);
arrows[address + 1],arrows[address + 2],arrows[address + 3],arrows[address + 4], address/5, current_weight);
return rewrite_arrow (arrows, arrows_nb, address/5, requested_weight, site, x, y, z);}
if (address >= 0 && current_weight == requested_weight) { if (address >= 0 && current_weight != requested_weight)
printf("arrow (%d,%d,%d,%d) found at address %d; current_weight == requested_weight => END",\ return rewrite_arrow (arrows, arrows_nb, address/5, requested_weight, site, x, y, z);
arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], address/5); return arrows_nb;}
if (address == -1 && requested_weight == 0) {printf("no such arrow found && requested weight == 0 => END"); return arrows_nb;} if (address >= 0 && current_weight == requested_weight) show_arrows_array(arrows, arrows_nb, x, y, z);
if (address == -1 && requested_weight == 0) show_arrows_array(arrows, arrows_nb, x, y, z);
return arrows_nb; return arrows_nb;
} }
/*
* Init space and arrows (= initial state)
* and allows ulterior creations, suppressions or modifications of the arrows[] array
*
* Draws space and arrows
*/
void main_test_graphics (void) void main_test_graphics (void)
{ {
// assert : space dimensions (x,y,z) > 0 TODO NOT checked before init // assert : space dimensions (x,y,z) > 0 TODO NOT checked before init
@ -666,7 +754,7 @@ void main_test_graphics (void)
// assert : no more than one arrow per address TODO NOT checked before init // assert : no more than one arrow per address TODO NOT checked before init
// notify : weights are replaced, NOT added (could be !) TODO // notify : weights are replaced, NOT added (could be !) TODO
int space_X = 7, space_Y = 1, space_Z = 1; int space_X = 4, space_Y = 1, space_Z = 1;
int pref_show_grids = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc int pref_show_grids = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc
// xyz, 0, x, y, z, xy, xz, yz, xyz // xyz, 0, x, y, z, xy, xz, yz, xyz
GLuint arrows[] = { GLuint arrows[] = {
@ -700,9 +788,8 @@ void main_test_graphics (void)
arrows_nb = set_arrow (arrows, arrows_nb, 1, 1, 2, 0, 0); arrows_nb = set_arrow (arrows, arrows_nb, 1, 1, 2, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 8, 1, 3, 0, 0); arrows_nb = set_arrow (arrows, arrows_nb, 8, 1, 3, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 8, 1, 3, 0, 0); arrows_nb = set_arrow (arrows, arrows_nb, 8, 1, 3, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 0, 1, 3, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 0, 1, 1, 0, 0); arrows_nb = set_arrow (arrows, arrows_nb, 0, 1, 1, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 1, 2, 0, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 1, 2, 0, 0, 0);
int weight = 0, site = 0, int weight = 0, site = 0,
stx = space_Z * space_Y, sty = space_Z, stz = 1, arrow_offset = 0; stx = space_Z * space_Y, sty = space_Z, stz = 1, arrow_offset = 0;
@ -727,19 +814,8 @@ void main_test_graphics (void)
} }
} }
int offset_after_arrows = buffer_vertex_size / 3, difference = offset_after_arrows - offset_after_grids; show_buffers_states(space_X, space_Y, space_Z, arrows_nb,
offset_after_grids, buffer_vertex_size,
printf("\n - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); buffer_lines_size_after_cubes, buffer_lines_size);
printf("\n buffer_vertex_offset_after grids = %6d\t%6d = (x+1)*(y+1)*(z+1); <--> (x,y,z = %d,%d,%d)",\
offset_after_grids, (space_X + 1)*(space_Y + 1)*(space_Z + 1), space_X, space_Y, space_Z);
printf("\n buffer_vertex_offset_after arrows = %6d\t%6d = %d + %d; <--> %d = 12 x %d (there are %d cubes)",\
offset_after_arrows, offset_after_arrows, offset_after_grids,\
difference, difference, difference / 12, difference / 12);
printf("\n buffer_lines_offset after cubes = %6d\t%6d = 2 * (%dx%d + %dx%d + %dx%d); <--> 2 * (x*y + x*z + y*z)",\
buffer_lines_size_after_cubes, ((space_X+1) * (space_Y+1) + (space_X+1) * (space_Z+1) + (space_Y+1) * (space_Z+1)) * 2,
space_X+1, space_Y+1, space_X+1, space_Z+1, space_Y+1, space_Z+1);
printf("\n buffer_lines_offset after arrows = %6d\t%6d = %d + %d; <--> %d = (3 + 4) x 2 x %d (arrows_nb = %d)\n",\
buffer_lines_size, buffer_lines_size, buffer_lines_size_after_cubes,\
buffer_lines_size - buffer_lines_size_after_cubes, arrows_nb * 14, arrows_nb, arrows_nb);
} }