star-gs

Literate program for a geometric sensitivity calculation
git clone git://git.meso-star.fr/star-gs.git
Log | Files | Refs | README | LICENSE

commit 9f9039b221a28bc56c2f00e3b1f74958c1c6f95c
parent 1d8b14778254a10a3975a5c3844ad99c46838600
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sun, 25 Apr 2021 12:11:29 +0200

Refactor the organisation of the sgs_geometry files

Move the functions of the geometry of the box in the sgs_geomtry_box.c
file.

Diffstat:
Mcmake/CMakeLists.txt | 2++
Msrc/sgs_geometry.c | 315+++++++++++++++++++++----------------------------------------------------------
Asrc/sgs_geometry_box.c | 149+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sgs_geometry_c.h | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 306 insertions(+), 233 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -51,6 +51,7 @@ set(SGS_FILES_SRC sgs_args.c sgs.c sgs_geometry.c + sgs_geometry_box.c sgs_log.c sgs_main.c) @@ -58,6 +59,7 @@ set(SGS_FILES_INC sgs_args.h sgs_c.h sgs_geometry.h + sgs_geometry_c.h sgs.h sgs_log.h) diff --git a/src/sgs_geometry.c b/src/sgs_geometry.c @@ -20,99 +20,14 @@ #include "sgs.h" #include "sgs_c.h" #include "sgs_geometry.h" +#include "sgs_geometry_c.h" #include "sgs_log.h" #include <star/s3d.h> -#include <rsys/dynamic_array_double.h> -#include <rsys/dynamic_array_int.h> -#include <rsys/dynamic_array_size_t.h> -#include <rsys/ref_count.h> - -struct sgs_geometry { - struct darray_double verts; - struct darray_size_t tris; - struct darray_int tris_type; /* List of enum sgs_surface_type */ - - struct s3d_scene_view* view_sp; - struct s3d_scene_view* view_rt; - - enum sgs_geometry_type type; - - struct sgs* sgs; - ref_T ref; -}; - /******************************************************************************* * Helper functions ******************************************************************************/ -static int -check_box_args(struct sgs* sgs, const struct sgs_geometry_box_args* args) -{ - ASSERT(args); - if(args->lower[0] >= args->upper[0] - || args->lower[1] >= args->upper[1] - || args->lower[2] >= args->upper[2]) { - sgs_log_err(sgs, - "Invalid box definition (lower: %g,%g,%g; upper(%g, %g, %g)\n", - SPLIT3(args->lower), - SPLIT3(args->upper)); - return 0; - } - return 1; -} - -static res_T -geometry_create - (struct sgs* sgs, - struct sgs_geometry** out_geom) -{ - struct sgs_geometry* geom = NULL; - struct mem_allocator* allocator = NULL; - res_T res = RES_OK; - ASSERT(sgs && out_geom); - - allocator = sgs_get_allocator(sgs); - geom = MEM_CALLOC(allocator, 1, sizeof(struct sgs_geometry)); - if(!geom) { - sgs_log_err(sgs, "Could not allocate the geometry.\n"); - res = RES_MEM_ERR; - goto error; - } - ref_init(&geom->ref); - geom->sgs = sgs; - darray_double_init(allocator, &geom->verts); - darray_size_t_init(allocator, &geom->tris); - darray_int_init(allocator, &geom->tris_type); - geom->type = SGS_GEOMETRY_NONE; - -exit: - *out_geom = geom; - return res; -error: - if(geom) { - sgs_geometry_ref_put(geom); - geom = NULL; - } - goto exit; -} - -/* Return the number of vertices */ -static FINLINE size_t -geometry_get_nvertices(const struct sgs_geometry* geom) -{ - ASSERT(geom); - return darray_double_size_get(&geom->verts) / 3/*#coords per vertex*/; -} - -/* Return the number of triangles */ -static FINLINE size_t -geometry_get_ntriangles(const struct sgs_geometry* geom) -{ - ASSERT(geom); - return darray_size_t_size_get(&geom->tris) / 3/*#ids per triangles*/; -} - static void geometry_get_vertex(const unsigned ivert, float pos[3], void* ctx) { @@ -132,120 +47,6 @@ geometry_get_triangle(const unsigned itri, unsigned ids[3], void* ctx) ids[2] = (unsigned)darray_size_t_cdata_get(&geom->tris)[itri*3+2]; } -static res_T -setup_view_rt(struct sgs_geometry* geom) -{ - struct s3d_vertex_data vdata = S3D_VERTEX_DATA_NULL; - struct s3d_shape* shape = NULL; - struct s3d_scene* scene = NULL; - unsigned ntris = 0; - unsigned nverts = 0; - res_T res = RES_OK; - ASSERT(geom); - - nverts = (unsigned)geometry_get_nvertices(geom); - ntris = (unsigned)geometry_get_ntriangles(geom); - - /* Setup the Star-3D vertex data */ - vdata.usage = S3D_POSITION; - vdata.type = S3D_FLOAT3; - vdata.get = geometry_get_vertex; - - /* Setup the Star-3D ray-tracing scene */ - res = s3d_shape_create_mesh(geom->sgs->s3d, &shape); - if(res != RES_OK) goto error; - res = s3d_mesh_setup_indexed_vertices - (shape, ntris, geometry_get_triangle, nverts, &vdata, 1, geom); - if(res != RES_OK) goto error; - res = s3d_scene_create(geom->sgs->s3d, &scene); - if(res != RES_OK) goto error; - res = s3d_scene_attach_shape(scene, shape); - if(res != RES_OK) goto error; - - /* Create the scene view */ - res = s3d_scene_view_create(scene, S3D_TRACE, &geom->view_rt); - if(res != RES_OK) goto error; - -exit: - if(shape) S3D(shape_ref_put(shape)); - if(scene) S3D(scene_ref_put(scene)); - return res; -error: - goto exit; -} - - -static res_T -setup_box_mesh - (struct sgs_geometry* geom, - const struct sgs_geometry_box_args* args) -{ - const double* low = NULL; - const double* upp = NULL; - double* vtx = NULL; - size_t* ids = NULL; - int* types = NULL; - res_T res = RES_OK; - ASSERT(geom && args); - - /* Allocate memory space */ - res = darray_double_resize(&geom->verts, 8/*#vertices*/*3/*#coords per vertex*/); - if(res != RES_OK) goto error; - res = darray_size_t_resize(&geom->tris, 12/*#triangles*/*3/*#ids per triangle*/); - if(res != RES_OK) goto error; - res = darray_int_resize(&geom->tris_type, 12/*#triangles*/); - if(res != RES_OK) goto error; - - /* Fetch allocated memory space */ - vtx = darray_double_data_get(&geom->verts); - ids = darray_size_t_data_get(&geom->tris); - types = darray_int_data_get(&geom->tris_type); - - /* Fetch input args */ - low = args->lower; - upp = args->upper; - - /* Setup the vertices */ - vtx[0*3+0] = low[0]; vtx[0*3+1] = low[1]; vtx[0*3+2] = low[2]; - vtx[1*3+0] = upp[0]; vtx[1*3+1] = low[1]; vtx[1*3+2] = low[2]; - vtx[2*3+0] = low[0]; vtx[2*3+1] = upp[1]; vtx[2*3+2] = low[2]; - vtx[3*3+0] = upp[0]; vtx[3*3+1] = upp[1]; vtx[3*3+2] = low[2]; - vtx[4*3+0] = low[0]; vtx[4*3+1] = low[1]; vtx[4*3+2] = upp[2]; - vtx[5*3+0] = upp[0]; vtx[5*3+1] = low[1]; vtx[5*3+2] = upp[2]; - vtx[6*3+0] = low[0]; vtx[6*3+1] = upp[1]; vtx[6*3+2] = upp[2]; - vtx[7*3+0] = upp[0]; vtx[7*3+1] = upp[1]; vtx[7*3+2] = upp[2]; - - /* Setup the triangles */ - ids[0*3+0] = 0; ids[0*3+1] = 4; ids[0*3+2] = 2; /* -X */ - ids[1*3+0] = 2; ids[1*3+1] = 4; ids[1*3+2] = 6; /* -X */ - ids[2*3+0] = 3; ids[2*3+1] = 7; ids[2*3+2] = 5; /* +X */ - ids[3*3+0] = 5; ids[3*3+1] = 1; ids[3*3+2] = 3; /* +X */ - ids[4*3+0] = 0; ids[4*3+1] = 1; ids[4*3+2] = 5; /* -Y */ - ids[5*3+0] = 5; ids[5*3+1] = 4; ids[5*3+2] = 0; /* -Y */ - ids[6*3+0] = 2; ids[6*3+1] = 6; ids[6*3+2] = 7; /* +Y */ - ids[7*3+0] = 7; ids[7*3+1] = 3; ids[7*3+2] = 2; /* +Y */ - ids[8*3+0] = 0; ids[8*3+1] = 2; ids[8*3+2] = 1; /* -Z */ - ids[9*3+0] = 1; ids[9*3+1] = 2; ids[9*3+2] = 3; /* -Z */ - ids[10*3+0] = 4; ids[10*3+1] = 5; ids[10*3+2] = 6; /* +Z */ - ids[11*3+0] = 6; ids[11*3+1] = 5; ids[11*3+2] = 7; /* +Z */ - - /* Setup the type of the triangles */ - types[0] = types[1] = SGS_SURFACE_X_NEG; - types[2] = types[3] = SGS_SURFACE_X_POS; - types[4] = types[5] = SGS_SURFACE_Y_NEG; - types[6] = types[7] = SGS_SURFACE_Y_POS; - types[8] = types[9] = SGS_SURFACE_Z_NEG; - types[10] = types[11] = SGS_SURFACE_Z_POS; - -exit: - return res; -error: - darray_double_clear(&geom->verts); - darray_size_t_clear(&geom->tris); - darray_int_clear(&geom->tris_type); - goto exit; -} - static void release_geometry(ref_T* ref) { @@ -266,39 +67,6 @@ release_geometry(ref_T* ref) /******************************************************************************* * API functions ******************************************************************************/ -res_T -sgs_geometry_box_create - (struct sgs* sgs, - const struct sgs_geometry_box_args* args, - struct sgs_geometry** out_geom) -{ - struct sgs_geometry* geom = NULL; - res_T res = RES_OK; - ASSERT(sgs && args && out_geom); - - if(!check_box_args(sgs, args)) { - res = RES_BAD_ARG; - goto error; - } - - res = geometry_create(sgs, &geom); - if(res != RES_OK) goto error; - res = setup_box_mesh(geom, args); - if(res != RES_OK) goto error; - res = setup_view_rt(geom); - if(res != RES_OK) goto error; - -exit: - *out_geom = geom; - return res; -error: - if(geom) { - sgs_geometry_ref_put(geom); - geom = NULL; - } - goto exit; -} - void sgs_geometry_ref_get(struct sgs_geometry* geom) { @@ -372,3 +140,84 @@ exit: error: goto exit; } + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +geometry_create + (struct sgs* sgs, + const enum sgs_geometry_type type, + struct sgs_geometry** out_geom) +{ + struct sgs_geometry* geom = NULL; + struct mem_allocator* allocator = NULL; + res_T res = RES_OK; + ASSERT(sgs && (unsigned)type < SGS_GEOMETRY_TYPES_COUNT__ && out_geom); + + allocator = sgs_get_allocator(sgs); + geom = MEM_CALLOC(allocator, 1, sizeof(struct sgs_geometry)); + if(!geom) { + sgs_log_err(sgs, "Could not allocate the geometry.\n"); + res = RES_MEM_ERR; + goto error; + } + ref_init(&geom->ref); + geom->sgs = sgs; + darray_double_init(allocator, &geom->verts); + darray_size_t_init(allocator, &geom->tris); + darray_int_init(allocator, &geom->tris_type); + geom->type = type; + +exit: + *out_geom = geom; + return res; +error: + if(geom) { + sgs_geometry_ref_put(geom); + geom = NULL; + } + goto exit; +} + +res_T +geometry_setup_view_rt(struct sgs_geometry* geom) +{ + struct s3d_vertex_data vdata = S3D_VERTEX_DATA_NULL; + struct s3d_shape* shape = NULL; + struct s3d_scene* scene = NULL; + unsigned ntris = 0; + unsigned nverts = 0; + res_T res = RES_OK; + ASSERT(geom); + + nverts = (unsigned)geometry_get_nvertices(geom); + ntris = (unsigned)geometry_get_ntriangles(geom); + + /* Setup the Star-3D vertex data */ + vdata.usage = S3D_POSITION; + vdata.type = S3D_FLOAT3; + vdata.get = geometry_get_vertex; + + /* Setup the Star-3D ray-tracing scene */ + res = s3d_shape_create_mesh(geom->sgs->s3d, &shape); + if(res != RES_OK) goto error; + res = s3d_mesh_setup_indexed_vertices + (shape, ntris, geometry_get_triangle, nverts, &vdata, 1, geom); + if(res != RES_OK) goto error; + res = s3d_scene_create(geom->sgs->s3d, &scene); + if(res != RES_OK) goto error; + res = s3d_scene_attach_shape(scene, shape); + if(res != RES_OK) goto error; + + /* Create the scene view */ + res = s3d_scene_view_create(scene, S3D_TRACE, &geom->view_rt); + if(res != RES_OK) goto error; + +exit: + if(shape) S3D(shape_ref_put(shape)); + if(scene) S3D(scene_ref_put(scene)); + return res; +error: + goto exit; +} diff --git a/src/sgs_geometry_box.c b/src/sgs_geometry_box.c @@ -0,0 +1,149 @@ +/* Copyright (C) 2021 + * CNRS/RAPSODEE, + * CNRS/LMAP, + * |Meso|Star> (contact@meso-star.com), + * UPS/Laplace. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "sgs_geometry.h" +#include "sgs_geometry_c.h" +#include "sgs_log.h" + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static int +check_box_args(struct sgs* sgs, const struct sgs_geometry_box_args* args) +{ + ASSERT(args); + if(args->lower[0] >= args->upper[0] + || args->lower[1] >= args->upper[1] + || args->lower[2] >= args->upper[2]) { + sgs_log_err(sgs, + "Invalid box definition (lower: %g,%g,%g; upper(%g, %g, %g)\n", + SPLIT3(args->lower), + SPLIT3(args->upper)); + return 0; + } + return 1; +} + +static res_T +setup_box_mesh + (struct sgs_geometry* geom, + const struct sgs_geometry_box_args* args) +{ + const double* low = NULL; + const double* upp = NULL; + double* vtx = NULL; + size_t* ids = NULL; + int* types = NULL; + res_T res = RES_OK; + ASSERT(geom && args); + + /* Allocate memory space */ + res = darray_double_resize(&geom->verts, 8/*#vertices*/*3/*#coords per vertex*/); + if(res != RES_OK) goto error; + res = darray_size_t_resize(&geom->tris, 12/*#triangles*/*3/*#ids per triangle*/); + if(res != RES_OK) goto error; + res = darray_int_resize(&geom->tris_type, 12/*#triangles*/); + if(res != RES_OK) goto error; + + /* Fetch allocated memory space */ + vtx = darray_double_data_get(&geom->verts); + ids = darray_size_t_data_get(&geom->tris); + types = darray_int_data_get(&geom->tris_type); + + /* Fetch input args */ + low = args->lower; + upp = args->upper; + + /* Setup the vertices */ + vtx[0*3+0] = low[0]; vtx[0*3+1] = low[1]; vtx[0*3+2] = low[2]; + vtx[1*3+0] = upp[0]; vtx[1*3+1] = low[1]; vtx[1*3+2] = low[2]; + vtx[2*3+0] = low[0]; vtx[2*3+1] = upp[1]; vtx[2*3+2] = low[2]; + vtx[3*3+0] = upp[0]; vtx[3*3+1] = upp[1]; vtx[3*3+2] = low[2]; + vtx[4*3+0] = low[0]; vtx[4*3+1] = low[1]; vtx[4*3+2] = upp[2]; + vtx[5*3+0] = upp[0]; vtx[5*3+1] = low[1]; vtx[5*3+2] = upp[2]; + vtx[6*3+0] = low[0]; vtx[6*3+1] = upp[1]; vtx[6*3+2] = upp[2]; + vtx[7*3+0] = upp[0]; vtx[7*3+1] = upp[1]; vtx[7*3+2] = upp[2]; + + /* Setup the triangles */ + ids[0*3+0] = 0; ids[0*3+1] = 4; ids[0*3+2] = 2; /* -X */ + ids[1*3+0] = 2; ids[1*3+1] = 4; ids[1*3+2] = 6; /* -X */ + ids[2*3+0] = 3; ids[2*3+1] = 7; ids[2*3+2] = 5; /* +X */ + ids[3*3+0] = 5; ids[3*3+1] = 1; ids[3*3+2] = 3; /* +X */ + ids[4*3+0] = 0; ids[4*3+1] = 1; ids[4*3+2] = 5; /* -Y */ + ids[5*3+0] = 5; ids[5*3+1] = 4; ids[5*3+2] = 0; /* -Y */ + ids[6*3+0] = 2; ids[6*3+1] = 6; ids[6*3+2] = 7; /* +Y */ + ids[7*3+0] = 7; ids[7*3+1] = 3; ids[7*3+2] = 2; /* +Y */ + ids[8*3+0] = 0; ids[8*3+1] = 2; ids[8*3+2] = 1; /* -Z */ + ids[9*3+0] = 1; ids[9*3+1] = 2; ids[9*3+2] = 3; /* -Z */ + ids[10*3+0] = 4; ids[10*3+1] = 5; ids[10*3+2] = 6; /* +Z */ + ids[11*3+0] = 6; ids[11*3+1] = 5; ids[11*3+2] = 7; /* +Z */ + + /* Setup the type of the triangles */ + types[0] = types[1] = SGS_SURFACE_X_NEG; + types[2] = types[3] = SGS_SURFACE_X_POS; + types[4] = types[5] = SGS_SURFACE_Y_NEG; + types[6] = types[7] = SGS_SURFACE_Y_POS; + types[8] = types[9] = SGS_SURFACE_Z_NEG; + types[10] = types[11] = SGS_SURFACE_Z_POS; + +exit: + return res; +error: + darray_double_clear(&geom->verts); + darray_size_t_clear(&geom->tris); + darray_int_clear(&geom->tris_type); + goto exit; +} + +/******************************************************************************* + * API function + ******************************************************************************/ +res_T +sgs_geometry_box_create + (struct sgs* sgs, + const struct sgs_geometry_box_args* args, + struct sgs_geometry** out_geom) +{ + struct sgs_geometry* geom = NULL; + res_T res = RES_OK; + ASSERT(sgs && args && out_geom); + + if(!check_box_args(sgs, args)) { + res = RES_BAD_ARG; + goto error; + } + + res = geometry_create(sgs, SGS_GEOMETRY_BOX, &geom); + if(res != RES_OK) goto error; + res = setup_box_mesh(geom, args); + if(res != RES_OK) goto error; + res = geometry_setup_view_rt(geom); + if(res != RES_OK) goto error; + +exit: + *out_geom = geom; + return res; +error: + if(geom) { + sgs_geometry_ref_put(geom); + geom = NULL; + } + goto exit; +} + diff --git a/src/sgs_geometry_c.h b/src/sgs_geometry_c.h @@ -0,0 +1,73 @@ +/* Copyright (C) 2021 + * CNRS/RAPSODEE, + * CNRS/LMAP, + * |Meso|Star> (contact@meso-star.com), + * UPS/Laplace. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef SGS_GEOMETRY_C_H +#define SGS_GEOMETRY_C_H + +#include <rsys/dynamic_array_double.h> +#include <rsys/dynamic_array_int.h> +#include <rsys/dynamic_array_size_t.h> +#include <rsys/ref_count.h> + +/* Forwar declarations */ +struct sgs; +struct s3d_scene_view; + +struct sgs_geometry { + struct darray_double verts; + struct darray_size_t tris; + struct darray_int tris_type; /* List of enum sgs_surface_type */ + + struct s3d_scene_view* view_sp; + struct s3d_scene_view* view_rt; + + enum sgs_geometry_type type; + + struct sgs* sgs; + ref_T ref; +}; + +extern LOCAL_SYM res_T +geometry_create + (struct sgs* sgs, + const enum sgs_geometry_type type, + struct sgs_geometry** out_geom); + +extern LOCAL_SYM res_T +geometry_setup_view_rt + (struct sgs_geometry* geom); + +/* Return the number of vertices */ +static FINLINE size_t +geometry_get_nvertices(const struct sgs_geometry* geom) +{ + ASSERT(geom); + return darray_double_size_get(&geom->verts) / 3/*#coords per vertex*/; +} + +/* Return the number of triangles */ +static FINLINE size_t +geometry_get_ntriangles(const struct sgs_geometry* geom) +{ + ASSERT(geom); + return darray_size_t_size_get(&geom->tris) / 3/*#ids per triangles*/; +} + +#endif /* SGS_GEOMETRY_C_H */ +