test_s2d_utils.h (2980B)
1 /* Copyright (C) 2016-2021, 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 S2D_UTILS_H 17 #define S2D_UTILS_H 18 19 #include <rsys/mem_allocator.h> 20 #include <stdio.h> 21 22 struct line_segments_desc { 23 const float* vertices; 24 const unsigned* indices; 25 }; 26 27 /******************************************************************************* 28 * Geometries 29 ******************************************************************************/ 30 static const float square_verts[] = { 31 11.f, 9.f, 32 9.f, 9.f, 33 9.f, 11.f, 34 11.f, 11.f 35 }; 36 const unsigned square_nverts = sizeof(square_verts)/(sizeof(float)*2); 37 38 const unsigned square_ids[] = { 39 0, 1, /* Bottom */ 40 1, 2, /* Left */ 41 2, 3, /* Top */ 42 3, 0 /* Right */ 43 }; 44 const unsigned square_nsegs = sizeof(square_ids)/(sizeof(unsigned)*2); 45 46 static const struct line_segments_desc square_desc = { square_verts, square_ids }; 47 48 static const float line_verts[] = { 9.f, 10.f, 11.f, 10.f }; 49 const unsigned line_nverts = sizeof(line_verts)/(sizeof(float)*2); 50 const unsigned line_ids[] = { 0, 1 }; 51 const unsigned line_nsegs = sizeof(line_ids)/(sizeof(unsigned)*2); 52 static const struct line_segments_desc line_desc = { line_verts, line_ids }; 53 54 static INLINE void 55 line_segments_get_ids(const unsigned isegment, unsigned ids[2], void* data) 56 { 57 const unsigned id = isegment * 2; 58 const struct line_segments_desc* desc = data; 59 CHK(desc != NULL); 60 ids[0] = desc->indices[id + 0]; 61 ids[1] = desc->indices[id + 1]; 62 } 63 64 static INLINE void 65 line_segments_get_position(const unsigned ivert, float position[2], void* data) 66 { 67 const unsigned id = ivert * 2; 68 const struct line_segments_desc* desc = data; 69 CHK(desc != NULL); 70 position[0] = desc->vertices[id + 0]; 71 position[1] = desc->vertices[id + 1]; 72 } 73 74 /******************************************************************************* 75 * Miscellaneous function 76 ******************************************************************************/ 77 static INLINE float 78 rand_canonic(void) 79 { 80 int r; 81 while((r = rand()) == RAND_MAX); 82 return (float)r / (float)RAND_MAX; 83 } 84 85 static INLINE void 86 check_memory_allocator(struct mem_allocator* allocator) 87 { 88 if(MEM_ALLOCATED_SIZE(allocator)) { 89 char dump[512]; 90 MEM_DUMP(allocator, dump, sizeof(dump)/sizeof(char)); 91 fprintf(stderr, "%s\n", dump); 92 FATAL("Memory leaks\n"); 93 } 94 } 95 96 #endif /* S2D_UTILS_H */ 97