diff --git a/include/cglm/io.h b/include/cglm/io.h index c714400..3fde038 100644 --- a/include/cglm/io.h +++ b/include/cglm/io.h @@ -23,61 +23,91 @@ #include #include +#define CGLM_PRINT_PRECISION 5 +#define CGLM_PRINT_MAX_TO_SHORT 1e5 +#define CGLM_PRINT_COLOR "\033[36m" +#define CGLM_PRINT_COLOR_RESET "\033[0m" + CGLM_INLINE void glm_mat4_print(mat4 matrix, FILE * __restrict ostream) { - int i; - int j; + char buff[16]; + int i, j, cw[4], cwi; #define m 4 #define n 4 - fprintf(ostream, "Matrix (float%dx%d):\n", m, n); + fprintf(ostream, "Matrix (float%dx%d): " CGLM_PRINT_COLOR "\n" , m, n); + + cw[0] = cw[1] = cw[2] = cw[3] = 0; for (i = 0; i < m; i++) { - fprintf(ostream, "\t|"); for (j = 0; j < n; j++) { - fprintf(ostream, "%0.4f", matrix[j][i]);; - - if (j != n - 1) - fprintf(ostream, "\t"); + if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT) + cwi = sprintf(buff, "% .*f", CGLM_PRINT_PRECISION, matrix[i][j]); + else + cwi = sprintf(buff, "% g", matrix[i][j]); + cw[i] = GLM_MAX(cw[i], cwi); } - - fprintf(ostream, "|\n"); } - fprintf(ostream, "\n"); + for (i = 0; i < m; i++) { + fprintf(ostream, " |"); + + for (j = 0; j < n; j++) + if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT) + fprintf(ostream, " % *.*f", cw[j], CGLM_PRINT_PRECISION, matrix[j][i]); + else + fprintf(ostream, " % *g", cw[j], matrix[j][i]); + + fprintf(ostream, " |\n"); + } + + fprintf(ostream, CGLM_PRINT_COLOR_RESET "\n"); #undef m #undef n } + CGLM_INLINE void glm_mat3_print(mat3 matrix, FILE * __restrict ostream) { - int i; - int j; + char buff[16]; + int i, j, cw[4], cwi; #define m 3 #define n 3 - fprintf(ostream, "Matrix (float%dx%d):\n", m, n); + fprintf(ostream, "Matrix (float%dx%d): " CGLM_PRINT_COLOR "\n", m, n); + + cw[0] = cw[1] = cw[2] = 0; for (i = 0; i < m; i++) { - fprintf(ostream, "\t|"); for (j = 0; j < n; j++) { - fprintf(ostream, "%0.4f", matrix[j][i]);; - - if (j != n - 1) - fprintf(ostream, "\t"); + if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT) + cwi = sprintf(buff, "% .*f", CGLM_PRINT_PRECISION, matrix[i][j]); + else + cwi = sprintf(buff, "% g", matrix[i][j]); + cw[i] = GLM_MAX(cw[i], cwi); } - - fprintf(ostream, "|\n"); } - fprintf(ostream, "\n"); + for (i = 0; i < m; i++) { + fprintf(ostream, " |"); + + for (j = 0; j < n; j++) + if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT) + fprintf(ostream, " % *.*f", cw[j], CGLM_PRINT_PRECISION, matrix[j][i]); + else + fprintf(ostream, " % *g", cw[j], matrix[j][i]); + + fprintf(ostream, " |\n"); + } + + fprintf(ostream, CGLM_PRINT_COLOR_RESET "\n"); #undef m #undef n @@ -87,27 +117,39 @@ CGLM_INLINE void glm_mat2_print(mat2 matrix, FILE * __restrict ostream) { - int i; - int j; + char buff[16]; + int i, j, cw[4], cwi; #define m 2 #define n 2 - fprintf(ostream, "Matrix (float%dx%d):\n", m, n); + fprintf(ostream, "Matrix (float%dx%d): " CGLM_PRINT_COLOR "\n", m, n); + + cw[0] = cw[1] = 0; for (i = 0; i < m; i++) { - fprintf(ostream, "\t|"); for (j = 0; j < n; j++) { - fprintf(ostream, "%0.4f", matrix[j][i]);; - - if (j != n - 1) - fprintf(ostream, "\t"); + if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT) + cwi = sprintf(buff, "% .*f", CGLM_PRINT_PRECISION, matrix[i][j]); + else + cwi = sprintf(buff, "% g", matrix[i][j]); + cw[i] = GLM_MAX(cw[i], cwi); } - - fprintf(ostream, "|\n"); } - fprintf(ostream, "\n"); + for (i = 0; i < m; i++) { + fprintf(ostream, " |"); + + for (j = 0; j < n; j++) + if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT) + fprintf(ostream, " % *.*f", cw[j], CGLM_PRINT_PRECISION, matrix[j][i]); + else + fprintf(ostream, " % *g", cw[j], matrix[j][i]); + + fprintf(ostream, " |\n"); + } + + fprintf(ostream, CGLM_PRINT_COLOR_RESET "\n"); #undef m #undef n @@ -121,16 +163,16 @@ glm_vec4_print(vec4 vec, #define m 4 - fprintf(ostream, "Vector (float%d):\n\t|", m); + fprintf(ostream, "Vector (float%d): " CGLM_PRINT_COLOR "\n (", m); for (i = 0; i < m; i++) { - fprintf(ostream, "%0.4f", vec[i]); - - if (i != m - 1) - fprintf(ostream, "\t"); + if (vec[i] < CGLM_PRINT_MAX_TO_SHORT) + fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, vec[i]); + else + fprintf(ostream, " % g", vec[i]); } - fprintf(ostream, "|\n\n"); + fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n"); #undef m } @@ -143,16 +185,16 @@ glm_vec3_print(vec3 vec, #define m 3 - fprintf(ostream, "Vector (float%d):\n\t|", m); + fprintf(ostream, "Vector (float%d): " CGLM_PRINT_COLOR "\n (", m); for (i = 0; i < m; i++) { - fprintf(ostream, "%0.4f", vec[i]); - - if (i != m - 1) - fprintf(ostream, "\t"); + if (vec[i] < CGLM_PRINT_MAX_TO_SHORT) + fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, vec[i]); + else + fprintf(ostream, " % g", vec[i]); } - fprintf(ostream, "|\n\n"); + fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n"); #undef m } @@ -165,16 +207,12 @@ glm_ivec3_print(ivec3 vec, #define m 3 - fprintf(ostream, "Vector (int%d):\n\t|", m); + fprintf(ostream, "Vector (int%d): " CGLM_PRINT_COLOR "\n (", m); - for (i = 0; i < m; i++) { - fprintf(ostream, "%d", vec[i]); + for (i = 0; i < m; i++) + fprintf(ostream, " % d", vec[i]); - if (i != m - 1) - fprintf(ostream, "\t"); - } - - fprintf(ostream, "|\n\n"); + fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n"); #undef m } @@ -187,16 +225,16 @@ glm_vec2_print(vec2 vec, #define m 2 - fprintf(ostream, "Vector (float%d):\n\t|", m); + fprintf(ostream, "Vector (float%d): " CGLM_PRINT_COLOR "\n (", m); for (i = 0; i < m; i++) { - fprintf(ostream, "%0.4f", vec[i]); - - if (i != m - 1) - fprintf(ostream, "\t"); + if (vec[i] < CGLM_PRINT_MAX_TO_SHORT) + fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, vec[i]); + else + fprintf(ostream, " % g", vec[i]); } - fprintf(ostream, "|\n\n"); + fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n"); #undef m } @@ -209,16 +247,17 @@ glm_versor_print(versor vec, #define m 4 - fprintf(ostream, "Versor (float%d):\n\t|", m); + fprintf(ostream, "Quaternion (float%d): " CGLM_PRINT_COLOR "\n (", m); for (i = 0; i < m; i++) { - fprintf(ostream, "%0.4f", vec[i]); - - if (i != m - 1) - fprintf(ostream, "\t"); + if (vec[i] < CGLM_PRINT_MAX_TO_SHORT) + fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, vec[i]); + else + fprintf(ostream, " % g", vec[i]); } - fprintf(ostream, "|\n\n"); + + fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n"); #undef m } @@ -232,22 +271,22 @@ glm_aabb_print(vec3 bbox[2], #define m 3 - fprintf(ostream, "AABB (%s):\n", tag ? tag: "float"); + fprintf(ostream, "AABB (%s): " CGLM_PRINT_COLOR "\n", tag ? tag: "float"); for (i = 0; i < 2; i++) { - fprintf(ostream, "\t|"); - + fprintf(ostream, " ("); + for (j = 0; j < m; j++) { - fprintf(ostream, "%0.4f", bbox[i][j]); - - if (j != m - 1) - fprintf(ostream, "\t"); + if (bbox[i][j] < CGLM_PRINT_MAX_TO_SHORT) + fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, bbox[i][j]); + else + fprintf(ostream, " % g", bbox[i][j]); } - fprintf(ostream, "|\n"); + fprintf(ostream, " )\n"); } - fprintf(ostream, "\n"); + fprintf(ostream, CGLM_PRINT_COLOR_RESET "\n"); #undef m }