star-phor-input

File format for describing photoreactor configurations
git clone https://www.edstar.cnrs.fr/git/star-phor-input.git
Log | Files | Refs | README | LICENSE

commit a14c246d84d55c3f07fb925440b4ecc0eef38de3
parent 70a2f4b644f62fc6385a868bbb710a6e985959e7
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date:   Thu, 13 Feb 2025 17:03:58 +0100

Implement parsing of member variables:

- Parse ka for the `sphin_volume` structure.
- Parse geometry for `sphin_volume` and `sphin_surface`.

Diffstat:
Msrc/sphin_surface.c | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Msrc/sphin_volume.c | 95++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
2 files changed, 170 insertions(+), 37 deletions(-)

diff --git a/src/sphin_surface.c b/src/sphin_surface.c @@ -1,4 +1,3 @@ - /* Copyright (C) 2024 Centre National de la Recherche Scientifique * Copyright (C) 2024 Clermont Auvergne INP * Copyright (C) 2024 INSA Lyon @@ -22,25 +21,29 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#define _POSIX_C_SOURCE 200112L /* for strtok_r support */ #include "sphin.h" #include "sphin_c.h" #include "sphin_config.h" +#include "sphin_geometry.h" #include "sphin_surface.h" +#include <rsys/cstr.h> #include <rsys/ref_count.h> #include <rsys/str.h> #include <rsys/text_reader.h> struct sphin_surface { struct str name; - struct sphin* sphin; + + struct brdf* brdf; ref_T ref; }; /******************************************************************************* - * Helper functions + * Helper functions ******************************************************************************/ static res_T surface_create @@ -57,26 +60,21 @@ surface_create ASSERT('\0' != name[0]); /* Name can't be empty */ surface = MEM_CALLOC(sphin->allocator, 1, sizeof(struct sphin_surface)); - if (NULL == surface) { - res = RES_MEM_ERR; - goto error; - } + if (NULL == surface) { res = RES_MEM_ERR; goto error; } ref_init(&surface->ref); SPHIN(ref_get(sphin)); surface->sphin = sphin; - + str_init(sphin->allocator, &surface->name); res = str_set(&surface->name, name); - if (RES_OK != res) { - goto error; - } - + if (RES_OK != res) { goto error; } + exit: *out_surface = surface; return res; error: if (NULL != surface) { - SPHIN(surface_ref_put(surface)); + SPHIN(surface_ref_put(surface)); surface = NULL; } goto exit; @@ -89,7 +87,7 @@ release_surface(ref_T* address) struct sphin* sphin = NULL; ASSERT(NULL != address); - + surface = CONTAINER_OF(address, struct sphin_surface, ref); str_release(&surface->name); sphin = surface->sphin; @@ -97,6 +95,49 @@ release_surface(ref_T* address) SPHIN(ref_put(sphin)); } +static res_T +parse_geometry + (struct sphin_surface* surface, + struct txtrdr* txtrdr, + char* value + ) +{ + struct geometry geom; + struct str filename; + char* token = NULL; + char* token_ptr = NULL; + char* side = NULL; + res_T res = RES_OK; + + ASSERT(NULL != surface); + + str_init(surface->sphin->allocator, &filename); + geometry_init(surface->sphin->allocator, &geom); + + token = strtok_r(value, " ", &token_ptr); + if (NULL == token){ res = RES_BAD_ARG; goto error; } + + side = trim_keyword(token); + if (0 == strcmp(side, "FRONT")){ geom.side = SPHIN_FRONT; } + else if (0 == strcmp(side, "BACK")){ geom.side = SPHIN_BACK; } + else { res = RES_BAD_ARG; goto error; } + + token = strtok_r(NULL, " ", &token_ptr); + if (NULL == token){ res = RES_BAD_ARG; goto error; } + res = str_set(&filename, trim_keyword(token)); + if (RES_OK != res) { goto error; } + + str_copy(&geom.stl_filename, &filename); + + /* TODO: Add geometry to surface->geometries */ + +exit: + str_release(&filename); + return res; +error: + goto exit; +} + /******************************************************************************* * Local functions ******************************************************************************/ @@ -107,24 +148,57 @@ parse_surface const char* name) { struct sphin_surface* surface = NULL; + struct str line; + char* keyword = NULL; + char* token = NULL; + char* token_ptr = NULL; + char* value = NULL; res_T res = RES_OK; ASSERT(NULL != config); ASSERT(NULL != txtrdr); ASSERT(NULL != name); ASSERT('\0' != name[0]); /* Name can't be empty */ - + res = surface_create(config->sphin, name, &surface); - if (RES_OK != res) { - goto error; + if (RES_OK != res) { goto error; } + + res = txtrdr_read_line(txtrdr); + if (RES_OK != res) { goto error; } + + while (NULL != txtrdr_get_line(txtrdr)) { + res = str_set(&line, txtrdr_get_cline(txtrdr)); + if (RES_OK != res) { goto error; } + + /* parse keyword */ + token = strtok_r(str_get(&line), ":", &token_ptr); + if (NULL == token){ res = RES_BAD_ARG; goto error; } + keyword = trim_keyword(token); + if (NULL == keyword){ res = RES_BAD_ARG; goto error; } + + /* parse value */ + token = strtok_r(NULL, "", &token_ptr); + if (NULL == token){ res = RES_BAD_ARG; goto error; } + value = trim_string(token); + if (NULL == value){ res = RES_BAD_ARG; goto error; } + + if (0 == strcmp(keyword, "geometry")){ + res = parse_geometry(surface, txtrdr, value); + } + else if (0 == strcmp(keyword, "brdf")){ + // res = parse_brdf(surface, txtrdr, value); + } + else { + break; + } } exit: - return res; + return res; error: goto exit; } - + /******************************************************************************* * Exported functions ******************************************************************************/ diff --git a/src/sphin_volume.c b/src/sphin_volume.c @@ -21,12 +21,15 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#define _POSIX_C_SOURCE 200112L /* for strtok_r support */ #include "sphin.h" #include "sphin_c.h" #include "sphin_config.h" +#include "sphin_geometry.h" #include "sphin_volume.h" +#include <rsys/cstr.h> /* str_to_double */ #include <rsys/ref_count.h> #include <rsys/str.h> #include <rsys/text_reader.h> @@ -34,6 +37,7 @@ struct sphin_volume { struct str name; double ka; + struct sphin* sphin; ref_T ref; }; @@ -56,19 +60,14 @@ volume_create ASSERT('\0' != name[0]); /* Name can't be empty */ volume = MEM_CALLOC(sphin->allocator, 1, sizeof(struct sphin_volume)); - if (NULL == volume) { - res = RES_MEM_ERR; - goto error; - } + if (NULL == volume) { res = RES_MEM_ERR; goto error; } ref_init(&volume->ref); SPHIN(ref_get(sphin)); volume->sphin = sphin; str_init(sphin->allocator, &volume->name); res = str_set(&volume->name, name); - if (RES_OK != res) { - goto error; - } + if (RES_OK != res) { goto error; } exit: *out_volume = volume; @@ -96,6 +95,72 @@ release_volume(ref_T* address) SPHIN(ref_put(sphin)); } +static res_T +parse_geometry + (struct sphin_volume* volume, + struct txtrdr* txtrdr, + char* value + ) +{ + struct geometry* geom = NULL; + struct str filename; + char* token = NULL; + char* token_ptr = NULL; + char* side = NULL; + res_T res = RES_OK; + + ASSERT(NULL != volume); + + str_init(volume->sphin->allocator, &filename); + geometry_init(volume->sphin->allocator, geom); + + token = strtok_r(value, " ", &token_ptr); + if (NULL == token){ res = RES_BAD_ARG; goto error; } + + side = trim_keyword(token); + if (0 == strcmp(side, "FRONT")){ geom->side = SPHIN_FRONT; } + else if (0 == strcmp(side, "BACK")){ geom->side = SPHIN_BACK; } + else { res = RES_BAD_ARG; goto error; } + + token = strtok_r(NULL, " ", &token_ptr); + if (NULL == token){ res = RES_BAD_ARG; goto error; } + res = str_set(&filename, trim_keyword(token)); + if (RES_OK != res) { goto error; } + + str_copy(&geom->stl_filename, &filename); + + /* TODO: Add geometry to volume->geometries */ + +exit: + str_release(&filename); + return res; +error: + if (geom != NULL) { geometry_release(geom); } + goto exit; +} + +static res_T +parse_ka +(struct sphin_volume* volume, + struct txtrdr* txtrdr, + char* value + ) +{ + res_T res = RES_OK; + double ka; + + ASSERT(NULL != volume); + + res = cstr_to_double(value, &ka); + if (RES_OK != res) { res = RES_BAD_ARG; goto error; } + volume->ka = ka; + +exit: + return res; +error: + goto exit; +} + /******************************************************************************* * Local functions ******************************************************************************/ @@ -121,20 +186,14 @@ parse_volume str_init(config->sphin->allocator, &line); res = volume_create(config->sphin, name, &volume); - if (RES_OK != res) { - goto error; - } + if (RES_OK != res) { goto error; } res = txtrdr_read_line(txtrdr); - if (RES_OK != res) { - goto error; - } + if (RES_OK != res) { goto error; } while (NULL != txtrdr_get_line(txtrdr)) { res = str_set(&line, txtrdr_get_cline(txtrdr)); - if (RES_OK != res) { - goto error; - } + if (RES_OK != res) { goto error; } /* parse keyword */ token = strtok_r(str_get(&line), ":", &token_ptr); @@ -149,10 +208,10 @@ parse_volume if (NULL == value){ res = RES_BAD_ARG; goto error; } if (0 == strcmp(keyword, "geometry")){ - // res = parse_geometry(volume, txtrdr, value); + res = parse_geometry(volume, txtrdr, value); } else if (0 == strcmp(keyword, "ka")){ - // res = parse_ka(volume, txtrdr, value); + res = parse_ka(volume, txtrdr, value); } else { break;