test_sg2d_utils.h (4161B)
1 /* Copyright (C) 2019, 2020, 2023 |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_SG2D_UTILS_H 17 #define TEST_SG2D_UTILS_H 18 19 #include <rsys/mem_allocator.h> 20 #include <stdio.h> 21 22 #define OK(Cond) CHK((Cond) == RES_OK) 23 #define BA(Cond) CHK((Cond) == RES_BAD_ARG) 24 #define ME(Cond) CHK((Cond) == RES_MEM_ERR) 25 26 /****************************************************************************** 27 * Memory allocator 28 *****************************************************************************/ 29 static INLINE void 30 check_memory_allocator(struct mem_allocator* allocator) 31 { 32 if(MEM_ALLOCATED_SIZE(allocator)) { 33 char dump[80192]; 34 MEM_DUMP(allocator, dump, sizeof(dump)); 35 fprintf(stderr, "%s\n", dump); 36 FATAL("Memory leaks.\n"); 37 } 38 } 39 40 /****************************************************************************** 41 * Geometry 42 *****************************************************************************/ 43 /* 2D square */ 44 static const double 45 square_vertices[4/*#vertices*/ * 2/*#coords per vertex*/] = { 46 0.0, 0.0, 47 1.0, 0.0, 48 0.0, 1.0, 49 1.0, 1.0 50 }; 51 static const unsigned 52 nvertices = sizeof(square_vertices) / (2 * sizeof(*square_vertices)); 53 /* Distorded square */ 54 static const double 55 box_vertices[4/*#vertices*/ * 2/*#coords per vertex*/] = { 56 0.1, 0.0, 57 1.0, 0.0, 58 0.0, 1.1, 59 1.0, 1.0 60 }; 61 62 /* The following array lists the indices toward the 2D vertices of each 63 * segment. 64 * Y 65 * 2----3 | 66 * | | 0----X 67 * | | 68 * 0----1 69 * 70 * The right-handed geometrical normal is outside the square 71 */ 72 static unsigned 73 box_indices[4/*#segments*/ * 2/*#indices per segment*/] = { 74 0, 2, 75 2, 3, 76 3, 1, 77 1, 0 78 }; 79 static const unsigned 80 nsegments = sizeof(box_indices) / (2 * sizeof(*box_indices)); 81 82 struct context { 83 const double* positions; 84 const unsigned* indices; 85 const unsigned* front_media; 86 const unsigned* back_media; 87 const unsigned* intface; 88 void* custom; 89 double offset[2]; 90 double scale; 91 char reverse_vrtx, reverse_med; 92 }; 93 #define CONTEXT_NULL__ {\ 94 NULL, NULL, NULL, NULL, NULL, NULL, {0,0}, 1, 0, 0\ 95 } 96 97 static const unsigned medium0[4] = { 0, 0, 0, 0 }; 98 static const unsigned medium1[4] = { 1, 1, 1, 1 }; 99 static const unsigned medium2[4] = { 2, 2, 2, 2 }; 100 static const unsigned medium1_3[4] = { 1, 1, 3, 1 }; 101 static const unsigned medium1_bottom0[4] = { 1, 1, 1, 0 }; 102 static const unsigned medium1_top0[4] = { 1, 0, 1, 1 }; 103 104 static const unsigned intface0[4] = { 0, 0, 0, 0 }; 105 static const unsigned intface1[4] = { 1, 1, 1, 1 }; 106 107 static INLINE void 108 get_indices(const unsigned iseg, unsigned ids[2], void* context) 109 { 110 const struct context* ctx = context; 111 ASSERT(ids && ctx); 112 ids[ctx->reverse_vrtx ? 1 : 0] = ctx->indices[iseg * 2 + 0]; 113 ids[ctx->reverse_vrtx ? 0 : 1] = ctx->indices[iseg * 2 + 1]; 114 } 115 116 static INLINE void 117 get_position(const unsigned ivert, double pos[2], void* context) 118 { 119 const struct context* ctx = context; 120 ASSERT(pos && ctx); 121 pos[0] = ctx->positions[ivert * 2 + 0] * ctx->scale + ctx->offset[0]; 122 pos[1] = ctx->positions[ivert * 2 + 1] * ctx->scale + ctx->offset[1]; 123 } 124 125 static INLINE void 126 get_properties 127 (const unsigned iseg, 128 unsigned property[SG2D_PROP_TYPES_COUNT__], 129 void* context) 130 { 131 const struct context* ctx = context; 132 ASSERT(property && ctx); 133 property[ctx->reverse_med ? SG2D_BACK : SG2D_FRONT] = ctx->front_media[iseg]; 134 property[ctx->reverse_med ? SG2D_FRONT : SG2D_BACK] = ctx->back_media[iseg]; 135 property[SG2D_INTFACE] = ctx->intface[iseg]; 136 } 137 138 #endif /* TEST_SG2D_UTILS_H */