star-schiff

Library for estimating radiative properties
git clone git://git.meso-star.com/star-schiff.git
Log | Files | Refs | README | LICENSE

commit cb59b68ec96f35bae58e60379c51767150b3e65c
parent 596a6964a6c51bdcc422b48351bc8e1ff841a020
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon,  5 Oct 2015 15:30:41 +0200

Begin the implementation of the Algae Integrator

Implementation draft that is going to be used to debug the sampling of
Algae geometries.

Diffstat:
Mcmake/CMakeLists.txt | 4++--
Msrc/salgae.h | 5++++-
Msrc/salgae_device.c | 31+++++++++++++++++++++++--------
Msrc/salgae_device.h | 9+++++++++
Asrc/salgae_estimator.c | 205+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 243 insertions(+), 11 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -37,7 +37,7 @@ option(NO_TEST "Disable the test" OFF) # Check dependencies ################################################################################ find_package(RCMake 0.2.1 REQUIRED) -find_package(RSys 0.2.1 REQUIRED) +find_package(RSys 0.3 REQUIRED) find_package(StarSP 0.2 REQUIRED) find_package(StarMC 0.2 REQUIRED) find_package(Star3D 0.2 REQUIRED) @@ -59,7 +59,7 @@ set(VERSION_MINOR 3) set(VERSION_PATCH 0) set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) -set(SALGAE_FILES_SRC salgae_device.c) +set(SALGAE_FILES_SRC salgae_device.c salgae_estimator.c) set(SALGAE_FILES_INC_API salgae.h) set(SALGAE_FILES_INC salgae_device.h) set(SALGAE_FILES_DOC COPYING.en COPYING.fr README.md) diff --git a/src/salgae.h b/src/salgae.h @@ -68,8 +68,10 @@ struct salgae_material { void* material; /* Opaque user defined representation of the material */ }; +static const struct salgae_material SALGAE_NULL_MATERIAL = { NULL, NULL }; + struct salgae_integrator_desc { - void (*sample_geometry) /* Sample a geometry i.e. a shape and its material */ + res_T (*sample_geometry) /* Sample a geometry i.e. a shape and its material */ (struct ssp_rng* rng, struct salgae_material* mtl, /* Sampled material */ struct s3d_shape* shape, /* Sampled shape */ @@ -114,6 +116,7 @@ salgae_device_ref_put SALGAE_API res_T salgae_integrate (struct salgae_device* dev, + struct ssp_rng* rng, struct salgae_integrator_desc* desc, const double wavelength, /* Wavelength to integrate */ const size_t steps_count, /* #realisations */ diff --git a/src/salgae_device.c b/src/salgae_device.c @@ -34,6 +34,8 @@ #include <star/s3d.h> +#include <stdarg.h> + /******************************************************************************* * Helper function ******************************************************************************/ @@ -48,6 +50,24 @@ device_release(ref_T* ref) } /******************************************************************************* + * Local functions + ******************************************************************************/ +void +log_error(struct salgae_device* dev, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(dev && msg); + if(dev->verbose) { + res_T res; (void)res; + va_start(vargs_list, msg); + res = logger_vprint(dev->logger, LOG_ERROR, msg, vargs_list); + ASSERT(res == RES_OK); + va_end(vargs_list); + } +} + + +/******************************************************************************* * API functions ******************************************************************************/ res_T @@ -73,10 +93,7 @@ salgae_device_create dev = MEM_CALLOC(mem_allocator, 1, sizeof(struct salgae_device)); if(!dev) { - if(verbose) { - logger_print(logger, LOG_ERROR, - "Couldn't allocate the Star-Algae device.\n"); - } + log_error(dev, "Couldn't allocate the Star-Algae device.\n"); res = RES_MEM_ERR; goto error; } @@ -91,10 +108,8 @@ salgae_device_create } else { res = s3d_device_create(dev->logger, dev->allocator, verbose, &dev->s3d); if(res != RES_OK) { - if(verbose) { - logger_print(logger, LOG_ERROR, - "Couldn't create the internal Star-3D device of Star-Algea.\n"); - } + log_error + (dev, "Couldn't create the internal Star-3D device of Star-Algea.\n"); goto error; } } diff --git a/src/salgae_device.h b/src/salgae_device.h @@ -44,5 +44,14 @@ struct salgae_device { ref_T ref; }; +extern LOCAL_SYM void +log_error + (struct salgae_device* dev, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; #endif /* SALGAE_DEVICE_H */ diff --git a/src/salgae_estimator.c b/src/salgae_estimator.c @@ -0,0 +1,205 @@ +/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com) + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. */ + +#include "salgae.h" +#include "salgae_device.h" + +#include <rsys/logger.h> +#include <rsys/mem_allocator.h> + +#include <star/s3d.h> +#include <star/ssp.h> + +#define NDIRS 10 + +struct salgae_estimator { + size_t nsteps; + double wavelength; + + struct salgae_device* dev; + ref_T ref; +}; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static res_T +radiative_path_length + (struct salgae_device* dev, + struct ssp_rng* rng, + struct salgae_integrator_desc* desc) +{ + struct s3d_shape* shape = NULL; + struct s3d_scene* scene = NULL; + struct salgae_material material = SALGAE_NULL_MATERIAL; + size_t i; + res_T res = RES_OK; + ASSERT(dev && rng && desc); + + res = s3d_shape_create_mesh(dev->s3d, &shape); + if(res != RES_OK) { + log_error(dev, "Couldn't create the Star-3D Algae shape.\n"); + goto error; + } + res = s3d_scene_create(dev->s3d, &scene); + if(res != RES_OK) { + log_error(dev, "Couldn't create the Star-3D Algae scene.\n"); + goto error; + } + res = s3d_scene_attach_shape(scene, shape); + if(res != RES_OK) { + log_error(dev, + "Couldn't attach the Star-3D Algae shape to the Star-3D Algae scene.\n"); + goto error; + } + + res = desc->sample_geometry(rng, &material, shape, desc->sampler_context); + if(res != RES_OK) { + log_error(dev, "Error in sampling an Algea geometry.\n"); + goto error; + } + + S3D(scene_begin_session(scene, S3D_TRACE)); + FOR_EACH(i, 0, NDIRS) { + float dir[4]; + ssp_ran_sphere_uniform(rng, dir); + /* TODO */ + } + S3D(scene_end_session(scene)); + +exit: + if(scene) S3D(scene_ref_put(scene)); + if(shape) S3D(shape_ref_put(shape)); + return res; +error: + goto exit; +} + +static char +check_desc(struct salgae_integrator_desc* desc) +{ + ASSERT(desc); + return desc->sample_geometry + && desc->scattering_angles_count; +} + +static void +estimator_release(ref_T* ref) +{ + struct salgae_estimator* estimator; + ASSERT(ref); + estimator = CONTAINER_OF(ref, struct salgae_estimator, ref); + MEM_RM(estimator->dev->allocator, estimator); +} + +static res_T +estimator_create + (struct salgae_device* dev, struct salgae_estimator** out_estimator) +{ + struct salgae_estimator* estimator = NULL; + res_T res = RES_OK; + ASSERT(dev && out_estimator); + + estimator = MEM_CALLOC(dev->allocator, 1, sizeof(struct salgae_estimator)); + if(!estimator) { + log_error + (dev, "Not enough memory: couldn't allocate the Star-Algea estimator.\n"); + res = RES_MEM_ERR; + goto error; + } + ref_init(&estimator->ref); + SALGAE(device_ref_get(dev)); + estimator->dev = dev; + +exit: + *out_estimator = estimator; + return res; +error: + if(estimator) { + SALGAE(estimator_ref_put(estimator)); + estimator = NULL; + } + goto exit; +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +salgae_integrate + (struct salgae_device* dev, + struct ssp_rng* rng, + struct salgae_integrator_desc* desc, + const double wavelength, /* Wavelength to integrate */ + const size_t steps_count, /* #realisations */ + struct salgae_estimator** out_estimator) +{ + struct salgae_estimator* estimator = NULL; + res_T res = RES_OK; + + if(!dev || !rng || !desc || !check_desc(desc) || wavelength < 0.0 + || !steps_count || !out_estimator) { + res = RES_BAD_ARG; + goto error; + } + + res = estimator_create(dev, &estimator); + if(res != RES_OK) goto error; + estimator->nsteps = steps_count; + estimator->wavelength = wavelength; + + res = radiative_path_length(dev, rng, desc); + if(res != RES_OK) goto error; + +exit: + if(out_estimator) *out_estimator = estimator; + return res; +error: + if(estimator) { + SALGAE(estimator_ref_put(estimator)); + estimator = NULL; + } + goto exit; +} + +res_T +salgae_estimator_ref_get(struct salgae_estimator* estimator) +{ + if(!estimator) return RES_BAD_ARG; + ref_get(&estimator->ref); + return RES_OK; +} + +res_T +salgae_estimator_ref_put(struct salgae_estimator* estimator) +{ + if(!estimator) return RES_BAD_ARG; + ref_put(&estimator->ref, estimator_release); + return RES_OK; +} +