test_sgf_utils.h (3215B)
1 /* Copyright (C) 2021, 2024 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2015-2018 EDF S.A., France (syrthes-support@edf.fr) 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #ifndef TEST_SGF_UTILS_H 18 #define TEST_SGF_UTILS_H 19 20 #include <rsys/mem_allocator.h> 21 #include <stdio.h> 22 23 struct shape_context { 24 const float* vertices; 25 size_t nvertices; 26 const unsigned* indices; 27 size_t nprimitives; 28 const double* emissivity; 29 const double* specularity; 30 const double* absorption; 31 unsigned dim; 32 }; 33 34 static INLINE void 35 get_indices(const unsigned itri, unsigned ids[], void* data) 36 { 37 const struct shape_context* shape = data; 38 const unsigned id = itri * shape->dim; 39 unsigned i; 40 CHK(shape != NULL); 41 CHK(ids != NULL); 42 CHK(shape->dim == 2 || shape->dim == 3); 43 CHK(itri < shape->nprimitives); 44 FOR_EACH(i, 0, shape->dim) ids[i] = shape->indices[id + i]; 45 } 46 47 static INLINE void 48 get_position(const unsigned ivert, float pos[], void* data) 49 { 50 const struct shape_context* shape = data; 51 const unsigned id = ivert*shape->dim; 52 unsigned i; 53 CHK(shape != NULL); 54 CHK(pos != NULL); 55 CHK(ivert < shape->nvertices); 56 FOR_EACH(i, 0, shape->dim) pos[i] = shape->vertices[id + i]; 57 } 58 59 static INLINE double 60 get_absorption(const unsigned iprim, const unsigned iband, void* ctx) 61 { 62 struct shape_context* shape = ctx; 63 CHK(shape != NULL); 64 (void)iband; 65 return shape->absorption[iprim]; 66 } 67 68 static INLINE double 69 get_emissivity(const unsigned iprim, const unsigned iband, void* ctx) 70 { 71 struct shape_context* shape = ctx; 72 CHK(shape != NULL); 73 (void)iband; 74 return shape->emissivity[iprim]; 75 } 76 77 static INLINE double 78 get_specularity(const unsigned iprim, const unsigned iband, void* ctx) 79 { 80 struct shape_context* shape = ctx; 81 CHK(shape != NULL); 82 (void)iband; 83 return shape->specularity[iprim]; 84 } 85 86 static INLINE double 87 get_reflectivity(const unsigned iprim, const unsigned iband, void* ctx) 88 { 89 return 1 - get_emissivity(iprim, iband, ctx); 90 } 91 92 static INLINE void 93 dump_triangle_mesh(struct shape_context* mesh) 94 { 95 unsigned i; 96 CHK(mesh != NULL); 97 FOR_EACH(i, 0, mesh->nvertices) 98 printf("v %f %f %f\n", SPLIT3(mesh->vertices + i*3)); 99 FOR_EACH(i, 0, mesh->nprimitives) { 100 printf("f %d %d %d\n", 101 mesh->indices[i*3 + 0] + 1, 102 mesh->indices[i*3 + 1] + 1, 103 mesh->indices[i*3 + 2] + 1); 104 } 105 } 106 107 static void 108 check_memory_allocator(struct mem_allocator* allocator) 109 { 110 if(MEM_ALLOCATED_SIZE(allocator)) { 111 char dump[512]; 112 MEM_DUMP(allocator, dump, sizeof(dump)/sizeof(char)); 113 fprintf(stderr, "%s\n", dump); 114 FATAL("Memory leaks\n"); 115 } 116 } 117 118 #endif /* TEST_SGF_UTILS_H */ 119