star-phor

Radiative transfer solver for photoreactors.
git clone https://www.edstar.cnrs.fr/git/star-phor.git
Log | Files | Refs | README | LICENSE

commit 8375b3d244cb0355e7cb17f25d6468f82bc958f7
parent 10a6ce8ccb080889a085891725a57ebce05ba3f4
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date:   Wed,  7 May 2025 18:17:07 +0200

Refactor: Extract triangle setup logic into separate function

Avoid code duplication by extracting the repetitive logic for retrieving
vertex coordinates and populating the triangle struct when processing
surfaces and volumes into the setup_triangle function.

Diffstat:
Msrc/sphor_config.c | 110+++++++++++++++++++++++++++++++++----------------------------------------------
1 file changed, 46 insertions(+), 64 deletions(-)

diff --git a/src/sphor_config.c b/src/sphor_config.c @@ -127,6 +127,50 @@ are_triangles_reversed } } +static void +setup_triangle + (const struct sphin_geometry_descriptor* geom_desc, + const size_t itri, + struct triangle* tri) +{ + const double* coords[3] = {NULL, NULL, NULL}; + size_t ids[3] = {0, 0, 0}; + + ASSERT(NULL != geom_desc); + ASSERT(NULL != tri); + ASSERT(itri < geom_desc->mesh.triangle_count); + + /* Get the indices of the triangle vertices. They are stored in a one + * dimensional array. So, to retrieve the vertex indices of the i^th triangle, + * one has to multiply i by 3, i.e. the number of vertex per triangle : + * + * 0 1 2 3 4 5 6 7 8 + * +---+---+---+---+---+---+---+---+---+----- + * indices |id0|id1|id2|id0|id1|id2|id0|id1|id2| ... + * +---+---+---+---+---+---+---+---+---+----- + * \__tri 0__/ \__tri 1__/ \__tri 2__/ */ + ids[0] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+0]; + ids[1] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+1]; + ids[2] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+2]; + + /* Get coords for the triangle corresponding to the vertices retrieved. Like + * the indices, the coords are stored in an one dimensional array. + * + * 0 1 2 3 4 5 6 7 8 + * +---+---+---+---+---+---+---+---+---+----- + * coords | x | y | z | x | y | z | x | y | z | ... + * +---+---+---+---+---+---+---+---+---+----- + * \__vert0__/ \__vert1__/ \__vert2__/ */ + coords[0] = geom_desc->mesh.coords + ids[0]*3/*#coords per vertex*/; + coords[1] = geom_desc->mesh.coords + ids[1]*3/*#coords per vertex*/; + coords[2] = geom_desc->mesh.coords + ids[2]*3/*#coords per vertex*/; + + /* Set the retrieved coords to the triangle struct */ + d3_set(tri->v0, coords[0]); + d3_set(tri->v1, coords[1]); + d3_set(tri->v2, coords[2]); +} + /* Return the triangle id if it already exists, append it to the dynamic array * and create an equivalent interface in the interface dynamic array otherwise * and return the newly created id */ @@ -182,8 +226,6 @@ setup_interface_volume_geometry_triangle struct triangle tri = TRIANGLE_NULL; const struct triangle* tri_sphor = NULL; struct interface* interface = NULL; - size_t ids[3] = {0, 0, 0}; - const double* coords[3] = {NULL, NULL, NULL}; enum sphin_side side = SPHIN_SIDE_NONE__; int flip = 0; res_T res = RES_OK; @@ -192,37 +234,8 @@ setup_interface_volume_geometry_triangle ASSERT(NULL != tri2id); ASSERT(NULL != triangles); ASSERT(NULL != geom_desc); - ASSERT(itri < geom_desc->mesh.triangle_count); - /* Get the indices of the triangle vertices. They are stored in a one - * dimensional array. So, to retrieve the vertex indices of the i^th triangle, - * one has to multiply i by 3, i.e. the number of vertex per triangle : - * - * 0 1 2 3 4 5 6 7 8 - * +---+---+---+---+---+---+---+---+---+----- - * indices |id0|id1|id2|id0|id1|id2|id0|id1|id2| ... - * +---+---+---+---+---+---+---+---+---+----- - * \__tri 0__/ \__tri 1__/ \__tri 2__/ */ - ids[0] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+0]; - ids[1] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+1]; - ids[2] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+2]; - - /* Get coords for the triangle corresponding to the vertices retrieved. Like - * the indices, the coords are stored in an one dimensional array. - * - * 0 1 2 3 4 5 6 7 8 - * +---+---+---+---+---+---+---+---+---+----- - * coords | x | y | z | x | y | z | x | y | z | ... - * +---+---+---+---+---+---+---+---+---+----- - * \__vert0__/ \__vert1__/ \__vert2__/ */ - coords[0] = geom_desc->mesh.coords + ids[0]*3/*#coords per vertex*/; - coords[1] = geom_desc->mesh.coords + ids[1]*3/*#coords per vertex*/; - coords[2] = geom_desc->mesh.coords + ids[2]*3/*#coords per vertex*/; - - /* Set the retrieved coords to the triangle struct */ - d3_set(tri.v0, coords[0]); - d3_set(tri.v1, coords[1]); - d3_set(tri.v2, coords[2]); + setup_triangle(geom_desc, itri, &tri); res = register_triangle(sphor, &tri, tri2id, triangles, &itri_sphor); if (RES_OK != res) { goto error; } @@ -344,8 +357,6 @@ setup_interface_surface_geometry_triangle struct triangle tri = TRIANGLE_NULL; const struct triangle* tri_sphor = NULL; struct interface* interface = NULL; - size_t ids[3] = {0, 0, 0}; - const double* coords[3] = {NULL, NULL, NULL}; int flip = 0; short surf_count = 0; enum sphin_side side = SPHIN_SIDE_NONE__; @@ -355,37 +366,8 @@ setup_interface_surface_geometry_triangle ASSERT(NULL != tri2id); ASSERT(NULL != triangles); ASSERT(NULL != geom_desc); - ASSERT(itri < geom_desc->mesh.triangle_count); - /* Get the indices of the triangle vertices. They are stored in a one - * dimensional array. So, to retrieve the vertex indices of the i^th triangle, - * one has to multiply i by 3, i.e. the number of vertex per triangle : - * - * 0 1 2 3 4 5 6 7 8 - * +---+---+---+---+---+---+---+---+---+----- - * indices |id0|id1|id2|id0|id1|id2|id0|id1|id2| ... - * +---+---+---+---+---+---+---+---+---+----- - * \__tri 0__/ \__tri 1__/ \__tri 2__/ */ - ids[0] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+0]; - ids[1] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+1]; - ids[2] = geom_desc->mesh.indices[itri*3/*#indices per triangle*/+2]; - - /* Get coords for the triangle corresponding to the vertices retrieved. Like - * the indices, the coords are stored in an one dimensional array. - * - * 0 1 2 3 4 5 6 7 8 - * +---+---+---+---+---+---+---+---+---+----- - * coords | x | y | z | x | y | z | x | y | z | ... - * +---+---+---+---+---+---+---+---+---+----- - * \__vert0__/ \__vert1__/ \__vert2__/ */ - coords[0] = geom_desc->mesh.coords + ids[0]*3/*#coords per vertex*/; - coords[1] = geom_desc->mesh.coords + ids[1]*3/*#coords per vertex*/; - coords[2] = geom_desc->mesh.coords + ids[2]*3/*#coords per vertex*/; - - /* Set the retrieved coords to the triangle struct */ - d3_set(tri.v0, coords[0]); - d3_set(tri.v1, coords[1]); - d3_set(tri.v2, coords[2]); + setup_triangle(geom_desc, itri, &tri); res = register_triangle(sphor, &tri, tri2id, triangles, &itri_sphor); if (RES_OK != res) { goto error; }