test_sg3d_utils.h (5011B)
1 /* Copyright (C) 2019, 2020, 2023, 2024 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #ifndef TEST_SG3D_UTILS_H 17 #define TEST_SG3D_UTILS_H 18 19 #include <star/sg3d.h> 20 21 #include <rsys/mem_allocator.h> 22 #include <rsys/double3.h> 23 24 #include <stdio.h> 25 26 #define OK(Cond) CHK((Cond) == RES_OK) 27 #define BA(Cond) CHK((Cond) == RES_BAD_ARG) 28 #define ME(Cond) CHK((Cond) == RES_MEM_ERR) 29 30 31 /****************************************************************************** 32 * Memory allocator 33 *****************************************************************************/ 34 static INLINE void 35 check_memory_allocator(struct mem_allocator* allocator) 36 { 37 if(MEM_ALLOCATED_SIZE(allocator)) { 38 char dump[80192]; 39 MEM_DUMP(allocator, dump, sizeof(dump)); 40 fprintf(stderr, "%s\n", dump); 41 FATAL("Memory leaks.\n"); 42 } 43 } 44 45 /****************************************************************************** 46 * Geometry 47 *****************************************************************************/ 48 /* 3D cube */ 49 static const double cube_vertices[8/*#vertices*/ * 3/*#coords per vertex*/] = { 50 0.0, 0.0, 0.0, 51 1.0, 0.0, 0.0, 52 0.0, 1.0, 0.0, 53 1.0, 1.0, 0.0, 54 0.0, 0.0, 1.0, 55 1.0, 0.0, 1.0, 56 0.0, 1.0, 1.0, 57 1.0, 1.0, 1.0 58 }; 59 static const unsigned nvertices = sizeof(cube_vertices) / (3*sizeof(double)); 60 /* Distorded cube */ 61 static const double box_vertices[8/*#vertices*/ * 3/*#coords per vertex*/] = { 62 0.1, 0.0, 0.0, 63 1.0, 0.0, 0.0, 64 0.0, 1.0, 0.0, 65 1.0, 1.0, 0.0, 66 0.0, 0.0, 1.1, 67 1.0, 0.0, 1.0, 68 0.0, 1.0, 1.0, 69 1.0, 1.1, 1.0 70 }; 71 72 /* The following array lists the indices toward the 3D vertices of each 73 * triangle. 74 * ,2---,3 ,2----3 75 * ,' | ,'/| ,'/| \ | 76 * 6----7' / | 6' / | \ | Y 77 * |', | / ,1 | / ,0---,1 | 78 * | ',|/,' |/,' | ,' o--X 79 * 4----5' 4----5' / 80 * Front, right Back, left and Z 81 * and Top faces bottom faces 82 * 83 * The right-handed geometrical normal is outside the cube */ 84 static const unsigned 85 cube_indices[12/*#triangles*/ * 3/*#indices per triangle*/] = { 86 0, 2, 1, 1, 2, 3, /* Front face */ 87 0, 4, 2, 2, 4, 6, /* Left face*/ 88 4, 5, 6, 6, 5, 7, /* Back face */ 89 3, 7, 1, 1, 7, 5, /* Right face */ 90 2, 6, 3, 3, 6, 7, /* Top face */ 91 0, 1, 4, 4, 1, 5 /* Bottom face */ 92 }; 93 static const unsigned 94 ntriangles = sizeof(cube_indices) / (3 * sizeof(*cube_indices)); 95 96 struct context { 97 const double* positions; 98 const unsigned* indices; 99 const unsigned* front_media; 100 const unsigned* back_media; 101 const unsigned* intface; 102 void* custom; 103 double offset[3]; 104 double scale[3]; 105 char reverse_vrtx, reverse_med; 106 }; 107 #define CONTEXT_NULL__ {\ 108 NULL, NULL, NULL, NULL, NULL, NULL, {0,0,0}, {1, 1, 1}, 0, 0\ 109 } 110 111 static const unsigned medium0[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 112 static const unsigned medium1[12] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; 113 static const unsigned medium2[12] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }; 114 static const unsigned medium1_3[12] = { 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1 }; 115 static const unsigned medium1_back0[12] = { 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1 }; 116 static const unsigned medium1_front0[12] = { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; 117 118 static const unsigned intface0[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 119 static const unsigned intface1[12] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; 120 121 static INLINE void 122 get_indices(const unsigned itri, unsigned ids[3], void* context) 123 { 124 const struct context* ctx = context; 125 ASSERT(ids && ctx); 126 ids[0] = ctx->indices[itri * 3 + 0]; 127 ids[ctx->reverse_vrtx ? 2 : 1] = ctx->indices[itri * 3 + 1]; 128 ids[ctx->reverse_vrtx ? 1 : 2] = ctx->indices[itri * 3 + 2]; 129 } 130 131 static INLINE void 132 get_position(const unsigned ivert, double pos[3], void* context) 133 { 134 const struct context* ctx = context; 135 double tmp[3]; 136 ASSERT(pos && ctx); 137 d3_add(pos, d3_mul(tmp, ctx->positions + ivert * 3, ctx->scale), ctx->offset); 138 } 139 140 static INLINE void 141 get_properties 142 (const unsigned itri, 143 unsigned property[SG3D_PROP_TYPES_COUNT__], 144 void* context) 145 { 146 const struct context* ctx = context; 147 ASSERT(property && ctx); 148 property[ctx->reverse_med ? SG3D_BACK : SG3D_FRONT] = ctx->front_media[itri]; 149 property[ctx->reverse_med ? SG3D_FRONT : SG3D_BACK] = ctx->back_media[itri]; 150 property[SG3D_INTFACE] = ctx->intface[itri]; 151 } 152 153 #endif /* TEST_SG3D_UTILS_H */