sdis_heat_path.c (5042B)
1 /* Copyright (C) 2016-2025 |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 #include "sdis_heat_path.h" 17 18 /* Generate the radiative path routines */ 19 #define SDIS_XD_DIMENSION 2 20 #include "sdis_heat_path_radiative_Xd.h" 21 #define SDIS_XD_DIMENSION 3 22 #include "sdis_heat_path_radiative_Xd.h" 23 24 /* Generate the convective path routines */ 25 #define SDIS_XD_DIMENSION 2 26 #include "sdis_heat_path_convective_Xd.h" 27 #define SDIS_XD_DIMENSION 3 28 #include "sdis_heat_path_convective_Xd.h" 29 30 /******************************************************************************* 31 * Local functions 32 ******************************************************************************/ 33 /* Return the offset into the list of vertices toward the first vertex of the 34 * line strip */ 35 static INLINE size_t 36 line_strip_vertex_offset(const struct sdis_heat_path* path, const size_t istrip) 37 { 38 ASSERT(path); 39 #ifndef NDEBUG 40 { 41 size_t nstrips; 42 SDIS(heat_path_get_line_strips_count(path, &nstrips)); 43 ASSERT(istrip < nstrips); 44 } 45 #endif 46 return istrip == 0 ? 0 : darray_size_t_cdata_get(&path->breaks)[istrip-1] + 1; 47 } 48 49 /******************************************************************************* 50 * Exported functions 51 ******************************************************************************/ 52 res_T 53 sdis_heat_path_get_status 54 (const struct sdis_heat_path* path, enum sdis_heat_path_flag* status) 55 { 56 if(!path || !status) return RES_BAD_ARG; 57 *status = path->status; 58 return RES_OK; 59 } 60 61 res_T 62 sdis_heat_path_get_line_strips_count 63 (const struct sdis_heat_path* path, 64 size_t* nstrips) 65 { 66 if(!path || !nstrips) return RES_BAD_ARG; 67 /* #strips == #breaks + 1 */ 68 *nstrips = darray_size_t_size_get(&path->breaks) + 1; 69 return RES_OK; 70 } 71 72 res_T 73 sdis_heat_path_line_strip_get_vertices_count 74 (const struct sdis_heat_path* path, 75 const size_t istrip, 76 size_t* out_nvertices) 77 { 78 size_t nstrips = 0; 79 size_t ivert_begin = 0; 80 size_t ivert_end = 0; 81 res_T res = RES_OK; 82 83 if(!path || !out_nvertices) { 84 res = RES_BAD_ARG; 85 goto error; 86 } 87 88 res = sdis_heat_path_get_line_strips_count(path, &nstrips); 89 if(res != RES_OK) goto error; 90 91 /* Check the indices of the strip */ 92 if(istrip >= nstrips) { 93 res = RES_BAD_ARG; 94 goto error; 95 } 96 97 if(istrip == 0) { /* First strip */ 98 ivert_begin = 0; 99 } else { 100 ivert_begin = line_strip_vertex_offset(path, istrip); 101 } 102 103 if(istrip == nstrips-1) { /* Last strip */ 104 ivert_end = darray_heat_vertex_size_get(&path->vertices); 105 } else { 106 ivert_end = line_strip_vertex_offset(path, istrip+1); 107 } 108 109 ASSERT(ivert_begin <= ivert_end); 110 *out_nvertices = ivert_end - ivert_begin; 111 112 exit: 113 return res; 114 error: 115 goto exit; 116 } 117 118 res_T 119 sdis_heat_path_line_strip_get_vertex 120 (const struct sdis_heat_path* path, 121 const size_t istrip, 122 const size_t ivert, 123 struct sdis_heat_vertex* vertex) 124 { 125 size_t nverts = 0; 126 size_t ivert_adjusted = 0; 127 res_T res = RES_OK; 128 129 if(!path || !vertex) { 130 res = RES_BAD_ARG; 131 goto error; 132 } 133 134 /* By retrieving the number of vertices, we also check the validity of 135 * istrip: the function will return an error if istrip is invalid */ 136 res = sdis_heat_path_line_strip_get_vertices_count(path, istrip, &nverts); 137 if(res != RES_OK) goto error; 138 139 if(ivert >= nverts) { 140 res = RES_BAD_ARG; 141 goto error; 142 } 143 144 /* Compute the index into the overall list of vertices */ 145 ivert_adjusted = ivert + line_strip_vertex_offset(path, istrip); 146 ASSERT(ivert_adjusted < darray_heat_vertex_size_get(&path->vertices)); 147 148 /* Fetch the vertex */ 149 *vertex = darray_heat_vertex_cdata_get(&path->vertices)[ivert_adjusted]; 150 151 exit: 152 return res; 153 error: 154 goto exit; 155 } 156 157 res_T 158 sdis_heat_path_line_strip_for_each_vertex 159 (const struct sdis_heat_path* path, 160 const size_t istrip, 161 sdis_process_heat_vertex_T func, 162 void* context) 163 { 164 const struct sdis_heat_vertex* vertices = NULL; 165 size_t ivert = 0; 166 size_t offset = 0; 167 size_t nverts = 0; 168 res_T res = RES_OK; 169 170 if(!path || !func) { 171 res = RES_BAD_ARG; 172 goto error; 173 } 174 175 res = sdis_heat_path_line_strip_get_vertices_count(path, istrip, &nverts); 176 if(res != RES_OK) goto error; 177 178 offset = line_strip_vertex_offset(path, istrip); 179 180 vertices = darray_heat_vertex_cdata_get(&path->vertices); 181 FOR_EACH(ivert, 0, nverts) { 182 res = func(vertices+ivert+offset, context); 183 if(res != RES_OK) goto error; 184 } 185 186 exit: 187 return res; 188 error: 189 goto exit; 190 }