star-4v_s

An invariant property of diffuse random walks
git clone git://git.meso-star.fr/star-4v_s.git
Log | Files | Refs | README | LICENSE

commit d5b569fe161f5b76d119227b22adcbc07442e8fa
parent 1d9521d15c8899cd8e63ba67bef7495ead11712d
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sun, 13 Oct 2024 18:08:46 +0200

Command line syntax update and minor refactoring

Use POSIX utility conventions for argument syntax. Their analysis has
also been moved to a separate file.

Rework the main source file to make it easier to read.

Diffstat:
MMakefile | 2+-
Msrc/s4vs.c | 122++++++++++++++++++++++++++++++++++++-------------------------------------------
Asrc/s4vs_args.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/s4vs_args.h | 38++++++++++++++++++++++++++++++++++++++
Msrc/s4vs_realization.h | 2++
5 files changed, 172 insertions(+), 67 deletions(-)

diff --git a/Makefile b/Makefile @@ -19,7 +19,7 @@ include config.mk -SRC = src/s4vs.c src/s4vs_realization.c +SRC = src/s4vs.c src/s4vs_args.c src/s4vs_realization.c OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) diff --git a/src/s4vs.c b/src/s4vs.c @@ -13,17 +13,21 @@ * 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 <rsys/cstr.h> -#include <rsys/float3.h> -#include <rsys/mem_allocator.h> -#include <rsys/clock_time.h> +#include "s4vs_args.h" +#include "s4vs_realization.h" #include <star/s3d.h> #include <star/s3daw.h> #include <star/smc.h> -#include "s4vs_realization.h" +#include <rsys/cstr.h> +#include <rsys/float3.h> +#include <rsys/mem_allocator.h> +#include <rsys/clock_time.h> +/******************************************************************************* + * Helper functions + ******************************************************************************/ static res_T compute_4v_s (struct s3d_scene* scene, @@ -117,27 +121,48 @@ error: static res_T import_obj(const char* filename, struct s3d_scene** out_scene) { + /* Star-3D */ struct s3d_device* s3d = NULL; struct s3d_scene* scene = NULL; + + /* Star-3DAW */ struct s3daw* s3daw = NULL; - const int VERBOSE = 0; size_t ishape, nshapes; + + /* Miscellaneous */ + char dump[64]; + struct time t0, t1; res_T res = RES_OK; - ASSERT(out_scene); - - #define CALL(Func) if(RES_OK != (res = Func)) goto error - CALL(s3d_device_create(NULL, NULL, VERBOSE, &s3d)); - CALL(s3daw_create(NULL, NULL, NULL, NULL, s3d, VERBOSE, &s3daw)); - CALL(s3daw_load(s3daw, filename)); - CALL(s3daw_get_shapes_count(s3daw, &nshapes)); - CALL(s3d_scene_create(s3d, &scene)); + + ASSERT(out_scene); /* Pre-condition */ + + S3D(device_create(NULL, NULL, 0/*verbose?*/, &s3d)); + S3DAW(create(NULL, NULL, NULL, NULL, s3d, 1/*verbose?*/, &s3daw)); + + time_current(&t0); + if(filename) { + res = s3daw_load(s3daw, filename); + } else { + res = s3daw_load_stream(s3daw, stdin); + } + if(res != RES_OK) { + fprintf(stderr, "Error loading obj -- %s\n", res_to_cstr(res)); + goto error; + } + time_current(&t1); + time_sub(&t0, &t1, &t0); + time_dump(&t0, TIME_ALL, NULL, dump, sizeof(dump)); + printf("Obj loaded in %s\n", dump); + + S3DAW(get_shapes_count(s3daw, &nshapes)); + S3D(scene_create(s3d, &scene)); + FOR_EACH(ishape, 0, nshapes) { - struct s3d_shape* shape; - CALL(s3daw_get_shape(s3daw, ishape, &shape)); - CALL(s3d_mesh_set_hit_filter_function(shape, s4vs_discard_self_hit, NULL)); - CALL(s3d_scene_attach_shape(scene, shape)); + struct s3d_shape* shape = NULL; + S3DAW(get_shape(s3daw, ishape, &shape)); + S3D(mesh_set_hit_filter_function(shape, s4vs_discard_self_hit, NULL)); + S3D(scene_attach_shape(scene, shape)); } - #undef CALL exit: /* Release memory */ @@ -153,61 +178,26 @@ error: goto exit; } +/******************************************************************************* + * The program + ******************************************************************************/ int main(int argc, char* argv[]) { - char dump[64]; - struct s3d_scene* scene = NULL; - struct time t0, t1; - unsigned long nrealisations = 10000; - double ks = 0.0; + struct s4vs_args args = S4VS_ARGS_DEFAULT; + struct s3d_scene* scn = NULL; res_T res = RES_OK; int err = 0; - /* Check command arguments */ - if(argc < 2 || argc > 4) { - fprintf(stderr, - "usage: %s obj_file [samples_count [k_scattering]]\n", argv[0]); - goto error; - } - - /* Import file's content in the scene */ - time_current(&t0); - res = import_obj(argv[1], &scene); - if(res != RES_OK) { - fprintf(stderr, "Couldn't import `%s'\n", argv[1]); - goto error; - } - time_sub(&t0, time_current(&t1), &t0); - time_dump(&t0, TIME_ALL, NULL, dump, sizeof(dump)); - printf("Obj loaded in %s.\n", dump); - - /* Set number of realizations */ - if(argc >= 3) { - res = cstr_to_ulong(argv[2], &nrealisations); - if(nrealisations <= 0 || res != RES_OK) { - fprintf(stderr, "Invalid number of realisations `%s'\n", argv[2]); - goto error; - } - } - - /* Set ks */ - if(argc >= 4) { - res = cstr_to_double(argv[3], &ks); - if(res != RES_OK || ks < 0) { - fprintf(stderr, "Invalid k-scattering value `%s'\n", argv[3]); - goto error; - } - } - - res = compute_4v_s(scene, nrealisations, ks); - if(res != RES_OK) { - fprintf(stderr, "Error in 4V/S integration\n"); - goto error; - } + res = s4vs_args_init(&args, argc, argv); + if(res != RES_OK) goto error; + res = import_obj(args.filename, &scn); + if(res != RES_OK) goto error; + res = compute_4v_s(scn, args.nrealisations, args.ks); + if(res != RES_OK) goto error; exit: - if(scene) S3D(scene_ref_put(scene)); + if(scn) S3D(scene_ref_put(scn)); if(mem_allocated_size() != 0) { fprintf(stderr, "Memory leaks: %lu Bytes\n", (unsigned long)mem_allocated_size()); diff --git a/src/s4vs_args.c b/src/s4vs_args.c @@ -0,0 +1,75 @@ +/* Copyright (C) 2015-2018, 2021, 2024 |Méso|Star> (contact@meso-star.com) + * + * 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/>. */ + +#define _POSIX_C_SOURCE 200809L /* getopt support */ + +#include "s4vs_args.h" + +#include <rsys/cstr.h> /* string conversions */ +#include <unistd.h> /* getopt */ + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +usage(FILE* stream, char* name) +{ + ASSERT(stream && name); + fprintf(stream, + "usage: %s [-n realisations_count] [-s scattering_coef] [obj_filename]\n", + name); +} + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +s4vs_args_init(struct s4vs_args* args, int argc, char** argv) +{ + int opt = 0; + res_T res = RES_OK; + ASSERT(args && argc && argv); + + *args = S4VS_ARGS_DEFAULT; + + while((opt = getopt(argc, argv, "n:s:")) != -1) { + switch(opt) { + case 'n': + res = cstr_to_ulong(optarg, &args->nrealisations); + if(res == RES_OK && args->nrealisations == 0) res = RES_BAD_ARG; + break; + case 's': + res = cstr_to_double(optarg, &args->ks); + if(res == RES_OK && args->ks < 0) res = RES_BAD_ARG; + break; + default: res = RES_BAD_ARG; break; + } + if(res != RES_OK) goto error; + } + if(optind < argc) args->filename = argv[optind]; + +exit: + return res; +error: + usage(stderr, argv[0]); + goto exit; +} + +void +s4vs_args_release(struct s4vs_args* args) +{ + /* Nothing to do */ + (void)args; /* Avoid "unused argument" warning */ +} diff --git a/src/s4vs_args.h b/src/s4vs_args.h @@ -0,0 +1,38 @@ +/* Copyright (C) 2015-2018, 2021, 2024 |Méso|Star> (contact@meso-star.com) + * + * 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 S4VS_ARGS_H +#define S4VS_ARGS_H + +#include <rsys/rsys.h> + +struct s4vs_args { + const char* filename; /* May be NULL <=> stdin */ + unsigned long nrealisations; + double ks; +}; +static const struct s4vs_args S4VS_ARGS_DEFAULT = {NULL, 10000, 0.0}; + +extern LOCAL_SYM res_T +s4vs_args_init + (struct s4vs_args* args, + int argc, + char** argv); + +extern LOCAL_SYM void +s4vs_args_release + (struct s4vs_args* args); + +#endif /* S4VS_ARGS_H */ diff --git a/src/s4vs_realization.h b/src/s4vs_realization.h @@ -19,6 +19,8 @@ #include <rsys/rsys.h> /* forward definition */ +struct s3d_hit; +struct s3d_scene_view; struct ssp_rng; struct s4vs_context {