rngrd

Describe a surface and its physical properties
git clone git://git.meso-star.fr/rngrd.git
Log | Files | Refs | README | LICENSE

commit 1fbe2373c61f2f8e68ab307628828626c93b2691
parent 14ed926d8dbace6cd2f876ab140b0a03c1714592
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue, 12 Jul 2022 17:57:21 +0200

Build the acceleration data structure

Diffstat:
Mcmake/CMakeLists.txt | 5+++--
Msrc/rngrd.c | 9+++++++--
Msrc/rngrd_c.h | 12+++++++++++-
Asrc/rngrd_setup_mesh.c | 179+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 200 insertions(+), 5 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -51,7 +51,8 @@ set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) set(RNGRD_FILES_SRC rngrd.c - rngrd_log.c) + rngrd_log.c + rngrd_setup_mesh.c) set(RNGRD_FILES_INC rngrd_c.h rngrd_log.h) @@ -65,7 +66,7 @@ rcmake_prepend_path(RNGRD_FILES_INC_API ${RNGRD_SOURCE_DIR}) rcmake_prepend_path(RNGRD_FILES_DOC ${PROJECT_SOURCE_DIR}/../) add_library(rngrd SHARED ${RNGRD_FILES_SRC} ${RNGRD_FILES_INC} ${RNGRD_FILES_INC_API}) -target_link_libraries(rngrd RSys m) +target_link_libraries(rngrd RSys Star3D StarMesh) set_target_properties(rngrd PROPERTIES DEFINE_SYMBOL RNGRD_SHARED_BUILD diff --git a/src/rngrd.c b/src/rngrd.c @@ -22,6 +22,8 @@ #include "rngrd_c.h" #include "rngrd_log.h" +#include <star/s3d.h> + #include <rsys/mem_allocator.h> /******************************************************************************* @@ -105,6 +107,8 @@ release_rngrd(ref_T* ref) struct rngrd* ground = CONTAINER_OF(ref, struct rngrd, ref); ASSERT(ref); if(ground->logger == &ground->logger__) logger_release(&ground->logger__); + if(ground->s3d) S3D(device_ref_put(ground->s3d)); + if(ground->s3d_view) S3D(scene_view_ref_put(ground->s3d_view)); MEM_RM(ground->allocator, ground); } @@ -122,10 +126,11 @@ rngrd_create res = create_rngrd(args, &ground); if(res != RES_OK) goto error; + res = setup_mesh(ground, args); + if(res != RES_OK) goto error; + /* TODO */ #if 0 - res = setup_mesh(args, ground); - if(res != RES_OK) goto error; res = setup_materials(args, ground); if(res != RES_OK) goto error; #endif diff --git a/src/rngrd_c.h b/src/rngrd_c.h @@ -24,12 +24,22 @@ #include <rsys/logger.h> #include <rsys/ref_count.h> +struct rngrd_create_args; + struct rngrd { + struct s3d_device* s3d; + struct s3d_scene_view* s3d_view; + int verbose; struct logger* logger; struct logger logger__; struct mem_allocator* allocator; -ref_T ref; + ref_T ref; }; +extern LOCAL_SYM res_T +setup_mesh + (struct rngrd* ground, + const struct rngrd_create_args* args); + #endif /* RNGRD_C_H */ diff --git a/src/rngrd_setup_mesh.c b/src/rngrd_setup_mesh.c @@ -0,0 +1,179 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université de Reims Champagne-Ardenne + * Copyright (C) 2022 Université de Versaille Saint-Quentin + * Copyright (C) 2022 Université Paul Sabatier (contact@laplace.univ-tlse.fr) + * + * 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 "rngrd.h" +#include "rngrd_c.h" +#include "rngrd_log.h" + +#include <star/s3d.h> +#include <star/smsh.h> + +#include <rsys/cstr.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +get_indices(const unsigned itri, unsigned ids[3], void* ctx) +{ + struct smsh_desc* desc = ctx; + ASSERT(itri < desc->ncells && desc->dcell == 3); + ids[0] = (unsigned)desc->cells[itri*3 + 0]; + ids[1] = (unsigned)desc->cells[itri*3 + 1]; + ids[2] = (unsigned)desc->cells[itri*3 + 2]; +} + +static void +get_position(const unsigned ivert, float pos[3], void* ctx) +{ + struct smsh_desc* desc = ctx; + ASSERT(ivert < desc->nnodes && desc->dnode == 3); + pos[0] = (float)desc->nodes[ivert*3 + 0]; + pos[1] = (float)desc->nodes[ivert*3 + 1]; + pos[2] = (float)desc->nodes[ivert*3 + 2]; +} + +static res_T +check_smsh_desc(const struct rngrd* ground, struct smsh_desc* desc) +{ + res_T res = RES_OK; + ASSERT(ground && desc); + + if(desc->dnode != 3 && desc->dcell != 3) { + log_err(ground, + "The ground mesh must be a 3D triangular mesh " + "(dimension of the mesh: %u; dimension of vertices: %u)\n", + desc->dnode, desc->dcell); + res = RES_BAD_ARG; + goto error; + } + + /* Check number of triangles */ + if(desc->ncells > UINT_MAX) { + log_err(ground, + "The number of triangles cannot exceed %lu whereas it is %lu\n", + (unsigned long)UINT_MAX, (unsigned long)desc->ncells); + res = RES_BAD_ARG; + goto error; + } + + /* Check number of vertices */ + if(desc->nnodes > UINT_MAX) { + log_err(ground, + "The number of veritces cannot exceed %lu whereas it is %lu\n", + (unsigned long)UINT_MAX, (unsigned long)desc->nnodes); + res = RES_BAD_ARG; + goto error; + } + +exit: + return res; +error: + goto exit; +} + +static res_T +setup_s3d(struct rngrd* ground, struct smsh_desc* smsh_desc) +{ + struct s3d_vertex_data vdata; + struct s3d_shape* mesh = NULL; + struct s3d_scene* scene = NULL; + res_T res = RES_OK; + + res = s3d_device_create + (ground->logger, ground->allocator, ground->verbose, &ground->s3d); + if(res != RES_OK) goto error; + res = s3d_shape_create_mesh(ground->s3d, &mesh); + if(res != RES_OK) goto error; + res = s3d_scene_create(ground->s3d, &scene); + if(res != RES_OK) goto error; + res = s3d_scene_attach_shape(scene, mesh); + if(res != RES_OK) goto error; + + vdata.usage = S3D_POSITION; + vdata.type = S3D_FLOAT3; + vdata.get = get_position; + res = s3d_mesh_setup_indexed_vertices(mesh, (unsigned)smsh_desc->ncells, + get_indices, (unsigned)smsh_desc->nnodes, &vdata, 1, smsh_desc); + if(res != RES_OK) goto error; + + /* TODO set the filter function + * TODO demander à Vincent si on peut imposer un sens pour les normales */ + + res = s3d_scene_view_create(scene, S3D_TRACE, &ground->s3d_view); + if(res != RES_OK) goto error; + +exit: + if(mesh) S3D(shape_ref_put(mesh)); + if(scene) S3D(scene_ref_put(scene)); + return res; + +error: + log_err(ground, "Could not setup the Star-3D data structures -- %s\n", + res_to_cstr(res)); + if(ground->s3d) S3D(device_ref_put(ground->s3d)); + if(ground->s3d_view) S3D(scene_view_ref_put(ground->s3d_view)); + ground->s3d = NULL; + ground->s3d_view = NULL; + goto exit; +} + + +/******************************************************************************* + * Local function + ******************************************************************************/ +res_T +setup_mesh(struct rngrd* ground, const struct rngrd_create_args* args) +{ + struct smsh_create_args smsh_args = SMSH_CREATE_ARGS_DEFAULT; + struct smsh_desc smsh_desc = SMSH_DESC_NULL; + struct smsh* smsh = NULL; + res_T res = RES_OK; + ASSERT(args); + + /* Create the Star-Mesh loader */ + smsh_args.logger = ground->logger; + smsh_args.allocator = ground->allocator; + smsh_args.verbose = ground->verbose; + res = smsh_create(&smsh_args, &smsh); + if(res != RES_OK) goto error; + + /* Load and retrieve the Star-Mesh data */ + res = smsh_load(smsh, args->smsh_filename); + if(res != RES_OK) goto error; + res = smsh_get_desc(smsh, &smsh_desc); + if(res != RES_OK) goto error; + res = check_smsh_desc(ground, &smsh_desc); + if(res != RES_OK) goto error; + + /* Setup the Star-3D data structures */ + res = setup_s3d(ground, &smsh_desc); + if(res != RES_OK) goto error; + +exit: + if(smsh) SMSH(ref_put(smsh)); + return res; +error: + if(ground->s3d) { + S3D(device_ref_put(ground->s3d)); + ground->s3d = NULL; + } + goto exit; +}