rngrd

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

commit 57b240a6cc06e17daa8471659420e278f00bae37
parent 167385e72f2f6852703af7e460faf4923411c73d
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue, 12 Jul 2022 16:59:19 +0200

Begin the implementation of rngrd_create

Diffstat:
Acmake/CMakeLists.txt | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/rngrd.c | 155+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/rngrd.h | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/rngrd_c.h | 35+++++++++++++++++++++++++++++++++++
Asrc/rngrd_log.c | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/rngrd_log.h | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 588 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -0,0 +1,103 @@ +# Copyright (C) 2022 Centre National de la Recherche Scientifique +# Copyright (C) 2022 Institut de Physique du Globe de Paris +# Copyright (C) 2022 |Meso|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 +# +# 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/>. + +cmake_minimum_required(VERSION 3.1) +project(rngrd C) +enable_testing() + +set(RNGRD_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) +option(NO_TEST "Do not build tests" OFF) + +################################################################################ +# Check dependencies +################################################################################ +find_package(MruMtl 0.1 REQUIRED) +find_package(RCMake 0.4 REQUIRED) +find_package(RSys 0.9 REQUIRED) +find_package(Star3D 0.8 REQUIRED) +find_package(StarMesh REQUIRED) +find_package(StarBuffer REQUIRED) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) +include(rcmake) +include(rcmake_runtime) + +include_directories(${RSys_INCLUDE_DIR}) + +################################################################################ +# Configure and define targets +################################################################################ +set(VERSION_MAJOR 0) +set(VERSION_MINOR 0) +set(VERSION_PATCH 0) +set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) + +set(RNGRD_FILES_SRC + rngrd.c + rngrd_log.c) +set(RNGRD_FILES_INC + rngrd_c.h + rngrd_log.h) +set(RNGRD_FILES_INC_API rngrd.h) +set(RNGRD_FILES_DOC COPYING README.md) + +# Prepend each file in the `RNGRD_FILES_<SRC|INC>' list by `RNGRD_SOURCE_DIR' +rcmake_prepend_path(RNGRD_FILES_SRC ${RNGRD_SOURCE_DIR}) +rcmake_prepend_path(RNGRD_FILES_INC ${RNGRD_SOURCE_DIR}) +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) + +set_target_properties(rngrd PROPERTIES + DEFINE_SYMBOL RNGRD_SHARED_BUILD + VERSION ${VERSION} + SOVERSION ${VERSION_MAJOR}) + +rcmake_setup_devel(rngrd RNGRD ${VERSION} rad-net/rngrd_version.h) + +################################################################################ +# Add tests +################################################################################ +if(NOT NO_TEST) + function(build_test _name) + add_executable(${_name} + ${RNGRD_SOURCE_DIR}/${_name}.c) + target_link_libraries(${_name} rngrd RSys) + endfunction() + + function(new_test _name) + build_test(${_name}) + add_test(${_name} ${_name}) + endfunction() + +endif() + +################################################################################ +# Define output & install directories +################################################################################ +install(TARGETS rngrd + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(FILES ${RNGRD_FILES_INC_API} DESTINATION include/rad-net) +install(FILES ${RNGRD_FILES_DOC} DESTINATION share/doc/rngrd) + diff --git a/src/rngrd.c b/src/rngrd.c @@ -0,0 +1,155 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Meso|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 <rsys/mem_allocator.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static res_T +check_rngrd_create_args(const struct rngrd_create_args* args) +{ + /* Invalid args */ + if(!args) { + return RES_BAD_ARG; + } + /* Filenames cannot be NULL */ + if(!args->smsh_filename + || !args->props_filename + || !args->mtllst_filename) { + return RES_BAD_ARG; + } + /* The name cannot be NULL */ + if(!args->name) { + return RES_BAD_ARG; + } + + return RES_OK; +} + +static res_T +create_rngrd + (const struct rngrd_create_args* args, + struct rngrd** out_ground) +{ + struct rngrd* ground = NULL; + struct mem_allocator* allocator = NULL; + res_T res = RES_OK; + + if(!out_ground) { res = RES_BAD_ARG; goto error;} + res = check_rngrd_create_args(args); + if(res != RES_OK) goto error; + + allocator = args->allocator ? args->allocator : &mem_default_allocator; + ground = MEM_CALLOC(allocator, 1, sizeof(*ground)); + if(!ground) { + if(args->verbose) { + #define ERR_STR "Could not allocate the device of the Rad-Net GRounD library" + if(args->logger) { + logger_print(args->logger, LOG_ERROR, ERR_STR); + } else { + fprintf(stderr, MSG_ERROR_PREFIX ERR_STR); + } + #undef ERR_STR + } + res = RES_MEM_ERR; + goto error; + } + + ref_init(&ground->ref); + ground->allocator = allocator; + ground->verbose = args->verbose ; + if(args->logger) { + ground->logger = args->logger; + } else { + res = setup_log_default(ground); + if(res != RES_OK) { + if(args->verbose) { + fprintf(stderr, MSG_ERROR_PREFIX + "Could not setup the default logger of the Rad-Net GRounD library.\n"); + } + goto error; + } + } +exit: + if(out_ground) *out_ground = ground; + return res; +error: + if(ground) { RNGRD(ref_put(ground)); ground = NULL; } + goto exit; +} +static void +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__); + MEM_RM(ground->allocator, ground); +} + +/******************************************************************************* + * Exported symbols + ******************************************************************************/ +res_T +rngrd_create + (const struct rngrd_create_args* args, + struct rngrd** out_ground) +{ + struct rngrd* ground = NULL; + res_T res = RES_OK; + + res = create_rngrd(args, &ground); + 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 + +exit: + if(out_ground) *out_ground = ground; + return res; +error: + if(ground) { RNGRD(ref_put(ground)); ground = NULL; } + goto exit; +} + +res_T +rngrd_ref_get(struct rngrd* ground) +{ + if(!ground) return RES_BAD_ARG; + ref_get(&ground->ref); + return RES_OK; +} + +res_T +rngrd_ref_put(struct rngrd* ground) +{ + if(!ground) return RES_BAD_ARG; + ref_put(&ground->ref, release_rngrd); + return RES_OK; +} diff --git a/src/rngrd.h b/src/rngrd.h @@ -0,0 +1,93 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Meso|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/>. */ + +#ifndef RNGRD_H +#define RNGRD_H + +#include <rsys/rsys.h> + +/* Library symbol management */ +#if defined(RNGRD_SHARED_BUILD) /* Build shared library */ + #define RNGRD_API extern EXPORT_SYM +#elif defined(RNGRD_STATIC) /* Use/build static library */ + #define RNGRD_API extern LOCAL_SYM +#else /* Use shared library */ + #define RNGRD_API extern IMPORT_SYM +#endif + +/* Helper macro that asserts if the invocation of the rngrd function `Func' + * returns an error. One should use this macro on suvm function calls for + * which no explicit error checking is performed */ +#ifndef NDEBUG + #define RNGRD(Func) ASSERT(rngrd_ ## Func == RES_OK) +#else + #define RNGRD(Func) rngrd_ ## Func +#endif + +/* Forward declaration of external data types */ +struct logger; +struct mem_allocator; + +struct rngrd_create_args { + const char* smsh_filename; /* The Star-Mesh geometry */ + const char* props_filename; /* Per triangle physical properties */ + const char* mtllst_filename; /* List of used materials */ + const char* name; /* Name of the ground */ + + struct logger* logger; /* NULL <=> use default logger */ + struct mem_allocator* allocator; /* NULL <=> use default allocator */ + int verbose; /* Verbosity level */ +}; + +#define RNGRD_CREATE_ARGS_DEFAULT__ { \ + NULL, /* Star-Mesh geometry */ \ + NULL, /* Per-triangle properties */ \ + NULL, /* List of used materials */ \ + "ground geometry", /* Name */ \ + \ + NULL, /* Logger */ \ + NULL, /* Allocator */ \ + 0 /* Verbosity level */ \ +} +static const struct rngrd_create_args RNDGR_CREATE_ARGS_DEFAULT = + RNGRD_CREATE_ARGS_DEFAULT__; + +/* Opaque data types */ +struct rngrd; + +BEGIN_DECLS + +/******************************************************************************* + * API of the Rad-Net GRounD library + ******************************************************************************/ +RNGRD_API res_T +rngrd_create + (const struct rngrd_create_args* args, + struct rngrd** ground); + +RNGRD_API res_T +rngrd_ref_get + (struct rngrd* ground); + +RNGRD_API res_T +rngrd_ref_put + (struct rngrd* ground); + +#endif /* RNGRD_H */ diff --git a/src/rngrd_c.h b/src/rngrd_c.h @@ -0,0 +1,35 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Meso|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/>. */ + +#ifndef RNGRD_C_H +#define RNGRD_C_H + +#include <rsys/logger.h> +#include <rsys/ref_count.h> + +struct rngrd { + int verbose; + struct logger* logger; + struct logger logger__; + struct mem_allocator* allocator; +ref_T ref; +}; + +#endif /* RNGRD_C_H */ diff --git a/src/rngrd_log.c b/src/rngrd_log.c @@ -0,0 +1,129 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Meso|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_c.h" +#include "rngrd_log.h" + +#include <rsys/cstr.h> +#include <rsys/logger.h> + +#include <stdarg.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +log_msg + (const struct rngrd* rngrd, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(rngrd && msg); + if(rngrd->verbose) { + res_T res; (void)res; + res = logger_vprint(rngrd->logger, stream, msg, vargs); + ASSERT(res == RES_OK); + } +} + +static void +print_info(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_INFO_PREFIX"%s", msg); +} + +static void +print_err(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_ERROR_PREFIX"%s", msg); +} + +static void +print_warn(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_WARNING_PREFIX"%s", msg); +} + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +setup_log_default(struct rngrd* rngrd) +{ + res_T res = RES_OK; + ASSERT(rngrd); + + res = logger_init(rngrd->allocator, &rngrd->logger__); + if(res != RES_OK) { + if(rngrd->verbose) { + fprintf(stderr, + MSG_ERROR_PREFIX + "Could not setup the default logger -- %s.\n", + res_to_cstr(res)); + } + goto error; + } + logger_set_stream(&rngrd->logger__, LOG_OUTPUT, print_info, NULL); + logger_set_stream(&rngrd->logger__, LOG_ERROR, print_err, NULL); + logger_set_stream(&rngrd->logger__, LOG_WARNING, print_warn, NULL); + rngrd->logger = &rngrd->logger__; + +exit: + return res; +error: + goto exit; +} + +void +log_info(const struct rngrd* rngrd, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(rngrd && msg); + + va_start(vargs_list, msg); + log_msg(rngrd, LOG_OUTPUT, msg, vargs_list); + va_end(vargs_list); +} + +void +log_err(const struct rngrd* rngrd, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(rngrd && msg); + + va_start(vargs_list, msg); + log_msg(rngrd, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warn(const struct rngrd* rngrd, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(rngrd && msg); + + va_start(vargs_list, msg); + log_msg(rngrd, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} diff --git a/src/rngrd_log.h b/src/rngrd_log.h @@ -0,0 +1,73 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Meso|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/>. */ + +#ifndef RNGRD_LOG_H +#define RNGRD_LOG_H + +#include <rsys/rsys.h> + +#define MSG_INFO_PREFIX "RNGRD:\x1b[1m\x1b[32minfo\x1b[0m: " +#define MSG_ERROR_PREFIX "RNGRD:\x1b[1m\x1b[31merror\x1b[0m: " +#define MSG_WARNING_PREFIX "RNGRD:\x1b[1m\x1b[33mwarning\x1b[0m: " + +struct rngrd; +struct logger; + +extern LOCAL_SYM res_T +setup_log_default + (struct rngrd* rngrd); + +/* Conditionally log a message on the LOG_OUTPUT stream of the rngrd logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_info + (const struct rngrd* rngrd, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_ERROR stream of the rngrd logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_err + (const struct rngrd* rngrd, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_WARNING stream of the rngrd logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_warn + (const struct rngrd* rngrd, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +#endif /* RNGRD_LOG_H */