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 763bf9ea47a5aa49c8519086fb3e15ea49cf670b
parent 8b0873bc61b3251303d55a9d21957221b8b617b8
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu, 14 Apr 2016 12:30:36 +0200

Prefix the 4V_S files and functions with s4vs

Diffstat:
Mcmake/CMakeLists.txt | 4++--
Dsrc/realization.c | 124-------------------------------------------------------------------------------
Dsrc/realization.h | 48------------------------------------------------
Msrc/s4vs.c | 6+++---
Asrc/s4vs_realization.c | 124+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/s4vs_realization.h | 48++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 177 insertions(+), 177 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -58,8 +58,8 @@ set(VERSION_MINOR 1) set(VERSION_PATCH 0) set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) -set(S4VS_FILES_SRC s4vs.c realization.c) -set(S4VS_FILES_INC realization.h) +set(S4VS_FILES_SRC s4vs.c s4vs_realization.c) +set(S4VS_FILES_INC s4vs_realization.h) set(S4VS_FILES_DOC COPYING.fr COPYING.en README.md) # Prepend each file in the `S4VS_FILES_<SRC|INC>' list by the absolute diff --git a/src/realization.c b/src/realization.c @@ -1,124 +0,0 @@ -/* Copyright (C) |Meso|Star> 2015-2016 (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 <rsys/float3.h> - -#include <star/s3d.h> -#include <star/ssp.h> -#include <star/smc.h> - -#include "realization.h" - -/* Maximum number of failures before an error occurs */ -#define MAX_FAILURES 10 - -void -realization(void* out_length, struct ssp_rng* rng, void* context) -{ - struct context* ctx = (struct context*)context; - struct s3d_attrib attrib; - struct s3d_primitive prim; - float normal[3], dir[4], pos[3], range[2], st[2]; - struct s3d_hit hit; - double length = 0; - double lambda = 0; - int nfailures = 0; - - do { /* Sample the scene surface to define the random walk starting point */ - float u, v, w; - - /* Sample a surface location, i.e. primitive ID and parametric coordinates */ - u = ssp_rng_canonical_float(rng); - v = ssp_rng_canonical_float(rng); - w = ssp_rng_canonical_float(rng); - S3D(scene_sample(ctx->scene, u, v, w, &prim, st)); - - /* retrieve the sampled geometric normal and position */ - S3D(primitive_get_attrib(&prim, S3D_GEOMETRY_NORMAL, st, &attrib)); - f3_normalize(normal, attrib.value); - S3D(primitive_get_attrib(&prim, S3D_POSITION, st, &attrib)); - f3_set(pos, attrib.value); - - /* Cosine weighted sampling of the hemisphere around the sampled normal */ - ssp_ran_hemisphere_cos(rng, normal, dir); - - /* Find the 1st hit from the sampled location along the sampled direction */ - range[0] = 1.e-6f; - range[1] = FLT_MAX; - do { - S3D(scene_trace_ray(ctx->scene, pos, dir, range, &hit)); - range[0] += 1.e-6f; - } while(S3D_PRIMITIVE_EQ(&hit.prim, &prim)); /* Handle self-hit */ - - /* No intersection <=> numerical imprecision or geometry leakage */ - } while(S3D_HIT_NONE(&hit) && ++nfailures < MAX_FAILURES); - - /* Too many ray-tracing failures => the geometry is inconsistent */ - if(nfailures >= MAX_FAILURES) goto error; - - /* Here the starting point and a propagation direction have been sampled */ - /* and we have determined the distance of the geometry in this direction */ - - while(1) { /* Here we go for the diffuse random walk */ - lambda = ssp_ran_exp(rng, ctx->ks); /* Sample a length according to ks */ - - if(lambda >= hit.distance) { - /* Lambda exceeds the geometry distance: stop the random walk */ - length += hit.distance; - break; - } - - /* lambda doesn't exceed the geometry distance */ - /* the random walk can continue after a diffusion */ - length += lambda; - f3_add(pos, pos, f3_mulf(dir, dir, (float)lambda)); - - do { - /* sample a new direction */ - ssp_ran_sphere_uniform(rng, dir); - range[0] = 0; - /* find the first intersection with the geometry */ - /* to refresh the geometry's distance */ - S3D(scene_trace_ray(ctx->scene, pos, dir, range, &hit)); - - /* No intersection <=> numerical imprecision or geometry leakage */ - } while(S3D_HIT_NONE(&hit) && ++nfailures < MAX_FAILURES); - - /* Too many ray-tracing failures => the geometry is inconsistent */ - if(nfailures >= MAX_FAILURES) goto error; - } - -exit: - ATOMIC_ADD(&ctx->nfailures, nfailures); /* Register the number of failures */ - SMC_DOUBLE(out_length) = length; - return; -error: - ctx->error = 1; /* Notify that an error occurs */ - length = 0; /* Reset the path length */ - goto exit; -} diff --git a/src/realization.h b/src/realization.h @@ -1,48 +0,0 @@ -/* Copyright (C) |Meso|Star> 2015-2016 (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. */ - -#ifndef REALIZATION_H -#define REALIZATION_H - -/* forward definition */ -struct ssp_rng; - -struct context { - struct s3d_sampler* sampler; - struct s3d_scene* scene; - double ks; - int nfailures; - int error; -}; - -/******************************************************************************* - * MC realization function - ******************************************************************************/ -extern void realization(void* length, struct ssp_rng* rng, void* context); - -#endif /* REALIZATION_H */ diff --git a/src/s4vs.c b/src/s4vs.c @@ -35,14 +35,14 @@ #include <star/s3daw.h> #include <star/smc.h> -#include "realization.h" +#include "s4vs_realization.h" static res_T compute_4v_s(struct s3d_scene* scene, const size_t max_steps, const double ks) { char buf[512]; struct time begin, end, elapsed; - struct context ctx; + struct s4vs_context ctx; float S, V, reference; struct smc_device* smc = NULL; struct smc_integrator integrator; @@ -81,7 +81,7 @@ compute_4v_s(struct s3d_scene* scene, const size_t max_steps, const double ks) /* Setup Star-MC */ SMC(device_create(NULL, NULL, SMC_NTHREADS_DEFAULT, NULL, &smc)); - integrator.integrand = &realization; /* Realization function */ + integrator.integrand = &s4vs_realization; /* Realization function */ integrator.type = &smc_double; /* Type of the Monte Carlo weight */ integrator.max_steps = max_steps; /* Realization count */ diff --git a/src/s4vs_realization.c b/src/s4vs_realization.c @@ -0,0 +1,124 @@ +/* Copyright (C) |Meso|Star> 2015-2016 (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 <rsys/float3.h> + +#include <star/s3d.h> +#include <star/ssp.h> +#include <star/smc.h> + +#include "s4vs_realization.h" + +/* Maximum number of failures before an error occurs */ +#define MAX_FAILURES 10 + +void +s4vs_realization(void* out_length, struct ssp_rng* rng, void* context) +{ + struct s4vs_context* ctx = (struct s4vs_context*)context; + struct s3d_attrib attrib; + struct s3d_primitive prim; + float normal[3], dir[4], pos[3], range[2], st[2]; + struct s3d_hit hit; + double length = 0; + double lambda = 0; + int nfailures = 0; + + do { /* Sample the scene surface to define the random walk starting point */ + float u, v, w; + + /* Sample a surface location, i.e. primitive ID and parametric coordinates */ + u = ssp_rng_canonical_float(rng); + v = ssp_rng_canonical_float(rng); + w = ssp_rng_canonical_float(rng); + S3D(scene_sample(ctx->scene, u, v, w, &prim, st)); + + /* retrieve the sampled geometric normal and position */ + S3D(primitive_get_attrib(&prim, S3D_GEOMETRY_NORMAL, st, &attrib)); + f3_normalize(normal, attrib.value); + S3D(primitive_get_attrib(&prim, S3D_POSITION, st, &attrib)); + f3_set(pos, attrib.value); + + /* Cosine weighted sampling of the hemisphere around the sampled normal */ + ssp_ran_hemisphere_cos(rng, normal, dir); + + /* Find the 1st hit from the sampled location along the sampled direction */ + range[0] = 1.e-6f; + range[1] = FLT_MAX; + do { + S3D(scene_trace_ray(ctx->scene, pos, dir, range, &hit)); + range[0] += 1.e-6f; + } while(S3D_PRIMITIVE_EQ(&hit.prim, &prim)); /* Handle self-hit */ + + /* No intersection <=> numerical imprecision or geometry leakage */ + } while(S3D_HIT_NONE(&hit) && ++nfailures < MAX_FAILURES); + + /* Too many ray-tracing failures => the geometry is inconsistent */ + if(nfailures >= MAX_FAILURES) goto error; + + /* Here the starting point and a propagation direction have been sampled */ + /* and we have determined the distance of the geometry in this direction */ + + while(1) { /* Here we go for the diffuse random walk */ + lambda = ssp_ran_exp(rng, ctx->ks); /* Sample a length according to ks */ + + if(lambda >= hit.distance) { + /* Lambda exceeds the geometry distance: stop the random walk */ + length += hit.distance; + break; + } + + /* lambda doesn't exceed the geometry distance */ + /* the random walk can continue after a diffusion */ + length += lambda; + f3_add(pos, pos, f3_mulf(dir, dir, (float)lambda)); + + do { + /* sample a new direction */ + ssp_ran_sphere_uniform(rng, dir); + range[0] = 0; + /* find the first intersection with the geometry */ + /* to refresh the geometry's distance */ + S3D(scene_trace_ray(ctx->scene, pos, dir, range, &hit)); + + /* No intersection <=> numerical imprecision or geometry leakage */ + } while(S3D_HIT_NONE(&hit) && ++nfailures < MAX_FAILURES); + + /* Too many ray-tracing failures => the geometry is inconsistent */ + if(nfailures >= MAX_FAILURES) goto error; + } + +exit: + ATOMIC_ADD(&ctx->nfailures, nfailures); /* Register the number of failures */ + SMC_DOUBLE(out_length) = length; + return; +error: + ctx->error = 1; /* Notify that an error occurs */ + length = 0; /* Reset the path length */ + goto exit; +} diff --git a/src/s4vs_realization.h b/src/s4vs_realization.h @@ -0,0 +1,48 @@ +/* Copyright (C) |Meso|Star> 2015-2016 (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. */ + +#ifndef S4V_S_REALIZATION_H +#define S4V_S_REALIZATION_H + +/* forward definition */ +struct ssp_rng; + +struct s4vs_context { + struct s3d_sampler* sampler; + struct s3d_scene* scene; + double ks; + int nfailures; + int error; +}; + +/******************************************************************************* + * MC realization function + ******************************************************************************/ +extern void s4vs_realization(void* length, struct ssp_rng* rng, void* context); + +#endif /* REALIZATION_H */