From 979cd8f947bab1cef172fcd90ad4ede9a11853f6 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Mon, 2 Oct 2023 22:48:38 +0200 Subject: [PATCH] set_arrow() OK (printf + drawing) with no more than two calls < to check ++ --- src/graphics/arrows.c | 20 ++-- src/graphics/graphics.c | 211 ++++++++++++++++++++++++++++++---------- 2 files changed, 171 insertions(+), 60 deletions(-) diff --git a/src/graphics/arrows.c b/src/graphics/arrows.c index 32681f3..8a5b302 100644 --- a/src/graphics/arrows.c +++ b/src/graphics/arrows.c @@ -42,7 +42,7 @@ */ void arrows_write_terminations (long x, long y, long z) { - float max, i, j, k, vx, vy, vz, arrow_basis_width, arrow_tip_padding; + float max, i, j, k, vx, vy, vz, arrow_basis_width, arrow_tip_padding, nuance = 0.24f; max = fmax(x, y); max = fmax(max, z); @@ -79,23 +79,23 @@ void arrows_write_terminations (long x, long y, long z) graphics_write_color(0.3f, 0.3f, 0.3f); graphics_write_color(0.3f, 0.3f, 0.3f); - // X - X (EAST - WEST) axis - arrows tips + // X - X (EAST - WEST) axis - arrows tips (red - green) graphics_write_vertex (vx + (1 / max) - arrow_tip_padding, vy, vz); graphics_write_vertex (vx - (1 / max) + arrow_tip_padding, vy, vz); - graphics_write_color(1.0f, 0.0f, 0.0f); - graphics_write_color(0.0f, 1.0f, 1.0f); + graphics_write_color(1.0f - nuance / 3, 0.0f + nuance, 0.0f + nuance); + graphics_write_color(0.0f + nuance, 1.0f - nuance, 1.0f - nuance); - // Y - Y (ZENITH - NADIR) axis - arrows tips + // Y - Y (ZENITH - NADIR) axis - arrows tips (cyan - magenta) graphics_write_vertex (vx, vy + (1 / max) - arrow_tip_padding, vz); graphics_write_vertex (vx, vy - (1 / max) + arrow_tip_padding, vz); - graphics_write_color(0.0f, 1.0f, 0.0f); - graphics_write_color(1.0f, 0.0f, 1.0f); + graphics_write_color(0.0f + nuance, 1.0f - nuance / 3, 0.0f + nuance); + graphics_write_color(1.0f - nuance, 0.0f + nuance, 1.0f - nuance); - // Z - Z (NORTH - SOUTH) axis - arrows tips + // Z - Z (NORTH - SOUTH) axis - arrows tips (blue - yellow) graphics_write_vertex (vx, vy, vz + (1 / max) - arrow_tip_padding); graphics_write_vertex (vx, vy, vz - (1 / max) + arrow_tip_padding); - graphics_write_color(0.0f, 0.0f, 1.0f); - graphics_write_color(1.0f, 1.0f, 0.0f); + graphics_write_color(0.0f + nuance, 0.0f + nuance, 1.0f - nuance / 4); + graphics_write_color(1.0f - nuance / 3, 1.0f - nuance / 3, 0.0f + nuance); }; } diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 2311288..02874d7 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -44,6 +44,8 @@ static volatile int buffer_lines_size = 0; static volatile int buffer_colors_size = 0; static volatile int buffer_plans_size = 0; +#define TEST 1 + /* * Dynamic array of ptrs to dynamically allocated gl_area_entry */ @@ -550,6 +552,24 @@ bool graphics_shutdown(const void *gl_area) } + + + + + + + + + + + + + + + + + + /* * Prints the arrows[] array * @@ -576,44 +596,16 @@ static void show_arrows_array (GLuint *arrows, int arrows_nb, int x, int y, int */ static void show_user_choices(GLuint *arrows, int arrows_nb, int space_size_x, int space_size_y, int space_size_z, - int prefer, int show_all) + int prefer, int show_all, int show_array) { + if (show_array) printf("\n"); printf("model + user constraints :\tspace size x,y,z = %d,%d,%d\t", space_size_x, space_size_y, space_size_z); if (prefer == 0) printf("prefer = %d <> show all grids", prefer); if (prefer == 1) printf("prefer = %d <> show no grid", prefer); if (prefer > 1) printf("show grids according rule (%d)", prefer); if (show_all) printf("\n\ - initial arrows data arrows_nb = %d", arrows_nb); - 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\ - requested_weight == current_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)\n\ - requested weight == 0 => END",\ - arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4]); return;} + initial arrows data with arrows_nb = %d", arrows_nb); + if (show_array) show_arrows_array (arrows, arrows_nb, space_size_x, space_size_y, space_size_z); } /* @@ -629,7 +621,7 @@ static void show_buffers_states(int space_X, int space_Y, int space_Z, int arrow 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); + 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,\ @@ -650,10 +642,29 @@ static void show_buffers_states(int space_X, int space_Y, int space_Z, int arrow arrows_nb * 14 - (buffer_lines_size - buffer_lines_size_after_cubes),\ (arrows_nb * 14 - (buffer_lines_size - buffer_lines_size_after_cubes)) / arrows_nb, arrows_nb); - printf("\n WARNING In this test, it may happens that two or several arrow basis are drawn superimposed (check the list).\n"); + if (arrows_nb > 1) + printf("\n WARNING In this test, it may happens that some arrows (whatever their weights) are drawn superimposed (check the list).\n"); } + + + + + + + + + + + + + + + + + + /* * Assigns a new weight to the arrow at address (address) @@ -662,7 +673,8 @@ static int rewrite_arrow (GLuint *arrows, int arrows_nb, int address, int weight { *(arrows + address * 5 + 0) = weight; - show_arrows_array (arrows, arrows_nb, x, y, z); + // if (! TEST) + show_arrows_array (arrows, arrows_nb, x, y, z); return arrows_nb; } @@ -685,7 +697,8 @@ static int create_arrow (GLuint *arrows, int arrows_nb, int weight, int site, in arrows_nb ++; - show_arrows_array (arrows, arrows_nb, x, y, z); + // if (! TEST) + show_arrows_array (arrows, arrows_nb, x, y, z); return arrows_nb; } @@ -708,11 +721,14 @@ static int erase_arrow (GLuint *arrows, int arrows_nb, int address, int site, in buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint)); - show_arrows_array (arrows, arrows_nb, x, y, z); + // if (! TEST) + show_arrows_array (arrows, arrows_nb, x, y, z); return arrows_nb; } +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); /* * Calls one of the functions create_arrow(), erase_arrow() or rewrite_arrow() * according to requested weight and address (coord site, x, y, z) @@ -737,7 +753,7 @@ 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 = %2d + ", requested_weight); show_user_action(arrows, arrows_nb, address, requested_weight, current_weight, site, x, y, z); @@ -750,14 +766,91 @@ static int set_arrow (GLuint *arrows, int arrows_nb, int requested_weight, int s if (address >= 0 && current_weight != requested_weight) return rewrite_arrow (arrows, arrows_nb, address/5, requested_weight, site, x, y, z); - if (address >= 0 && current_weight == requested_weight) show_arrows_array(arrows, arrows_nb, x, y, z); + if (! TEST && 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); + if (! TEST && address == -1 && requested_weight == 0) show_arrows_array(arrows, arrows_nb, x, y, z); return arrows_nb; } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +/* + * 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) { // *(arrows + i * 5 + 0) + printf("no such arrow found (%2d,%2d,%2d,%2d)", site, x, y, z); //arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4]); + if (! TEST) printf("\n "); else printf(" "); + printf("=> CREATE"); return;} + + if (address >= 0 && requested_weight == 0) { + printf("arrow (%2d,%2d,%2d,%2d) found at address %2d; current_weight = %2d;", site, x, y, z, address, current_weight); // arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], address/5, current_weight); + if (! TEST) printf("\n "); else printf(" "); + printf("=> ERASE"); return;} + + if (address >= 0 && current_weight != requested_weight) { + printf("arrow (%2d,%2d,%2d,%2d) found at address %2d; current_weight = %2d;", site, x, y, z, address, current_weight); // arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], address/5, current_weight); + if (! TEST) printf("\n "); else printf(" "); + printf("=> MODIFY"); return;} + + if (address >= 0 && current_weight == requested_weight){ + printf("arrow (%2d,%2d,%2d,%2d) found at address %2d;", site, x, y, z, address); // arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4], address/5); + if (! TEST) printf("\n "); else printf(" "); + printf("requested_weight == current_weight => END"); return;} + + if (address == -1 && requested_weight == 0) { + printf("no such arrow found (%2d,%2d,%2d,%2d)", site, x, y, z); // arrows[address + 1], arrows[address + 2], arrows[address + 3], arrows[address + 4]); + if (! TEST) printf("\n "); else printf(" "); + printf("=> END"); return;} +} + + + //#define RAND_MAX /* * Init space and arrows (= initial state) @@ -777,24 +870,31 @@ void main_test_graphics (void) int rand(void); void srand(unsigned int seed); // printf ("Valeur max : %d\n", RAND_MAX); min + rand() % (max+1 - min); - int arbitrary = 11; - int space_X = 1 + rand() % arbitrary, space_Y = 1 + rand() % arbitrary, space_Z = 1 + rand() % arbitrary; + int arbitrary = 5; + int space_X = 1 + rand() % arbitrary, + space_Y = 1 + rand() % arbitrary, + space_Z = 1 + rand() % arbitrary; int density_max = space_X * space_Y * space_Z; int arrows_nb = rand() % density_max / 3; int pref_show_grids = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc // xyz, 0, x, y, z, xy, xz, yz, xyz GLuint arrows[arrows_nb * 5]; + show_user_choices(arrows, arrows_nb, space_X, space_Y, space_Z, pref_show_grids, 1, 0); + // WARNING arrows with same address will be drawn superimposed if (arrows_nb > 0) for (int i = 0; i < arrows_nb; i ++) { arrows[i * 5 + 0] = 1 + rand() % arbitrary; // load / weight - arrows[i * 5 + 1] = rand() % 6; // site - arrows[i * 5 + 2] = rand() % space_X; // x - arrows[i * 5 + 3] = rand() % space_Y; // y - arrows[i * 5 + 4] = rand() % space_Z; // z + arrows[i * 5 + 1] = rand() % 6; // site + arrows[i * 5 + 2] = rand() % space_X; // x + arrows[i * 5 + 3] = rand() % space_Y; // y + arrows[i * 5 + 4] = rand() % space_Z; // z }; - //int arrows_nb = sizeof(arrows) / sizeof(arrows[0]) / 5; forget, during the random tests - show_user_choices(arrows, arrows_nb, space_X, space_Y, space_Z, pref_show_grids, 1); + + // forget the next two lines during the random tests + // arrows[] = {... some values ...} + // int arrows_nb = sizeof(arrows) / sizeof(arrows[0]) / 5; + /* GRID */ grid_write_intersections (space_X, space_Y, space_Z); if (pref_show_grids % 2 == 0) grid_write_x (space_X, space_Y, space_Z); @@ -804,12 +904,23 @@ void main_test_graphics (void) int offset_after_grids = buffer_vertex_size / 3; int buffer_lines_size_after_cubes = buffer_lines_size; int offset_up_to_this_cube_coords = 0, x, y, z; + /* ARROWS */ /* NB The 12 vertices required to draw 6 arrows in each cube * are always defined (whether arrows are drawn or not). */ - if (arrows_nb > 0) arrows_write_terminations (space_X, space_Y, space_Z); + /* Therefore: if (arrows_nb > 0) is not required (next line) */ + arrows_write_terminations (space_X, space_Y, space_Z); -// arrows_nb = set_arrow (arrows, arrows_nb, 0, 0, 0, 0, 0); + for (int k = 0; k < 2; k++) // for (int k = 0; k < arbitrary; k++) TODO + arrows_nb = set_arrow (arrows, arrows_nb, + rand() % arbitrary, // load / weight + rand() % 6, // site, + rand() % space_X, // x + rand() % space_Y, // y + rand() % space_Z); // z + + // ligne suivante inutile: < ce sont les sous-fonctions de set_arrow() qui appellent + // show_user_choices(arrows, arrows_nb, space_X, space_Y, space_Z, pref_show_grids, 1, 1); int weight = 0, site = 0, stx = space_Z * space_Y, sty = space_Z, stz = 1, arrow_offset = 0;