star-sf

Set of surface and volume scattering functions
git clone git://git.meso-star.fr/star-sf.git
Log | Files | Refs | README | LICENSE

commit c434ba7c1e31b931360e64772f952dde6a283652
parent cc9ac6210658e7263edf4e5e1bae3edbafe56dd6
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sat, 10 Sep 2016 14:30:40 +0200

Rename the fresnel terms

Diffstat:
Mcmake/CMakeLists.txt | 4++--
Msrc/ssf.h | 12++++++------
Dsrc/ssf_fresnel_dielectric.c | 104-------------------------------------------------------------------------------
Asrc/ssf_fresnel_dielectric_dielectric.c | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/test_ssf_fresnel_dielectric.c | 60------------------------------------------------------------
Asrc/test_ssf_fresnel_dielectric_dielectric.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 175 insertions(+), 172 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -52,7 +52,7 @@ set(SSF_FILES_SRC ssf_bsdf.c ssf_bxdf.c ssf_fresnel.c - ssf_fresnel_dielectric.c + ssf_fresnel_dielectric_dielectric.c ssf_fresnel_no_op.c ssf_specular_reflection.c) rcmake_prepend_path(SSF_FILES_SRC ${SSF_SOURCE_DIR}) @@ -92,7 +92,7 @@ if(NOT NO_TEST) new_test(test_ssf_bsdf) new_test(test_ssf_bxdf) new_test(test_ssf_fresnel) - new_test(test_ssf_fresnel_dielectric) + new_test(test_ssf_fresnel_dielectric_dielectric) new_test(test_ssf_fresnel_no_op) new_test(test_ssf_specular_reflection) endif() diff --git a/src/ssf.h b/src/ssf.h @@ -82,10 +82,10 @@ SSF_API const struct ssf_bxdf_type ssf_specular_reflection; /* Fresnel term for perfect reflection */ SSF_API const struct ssf_fresnel_type ssf_fresnel_no_op; -/* Fresnel term of a dielectric surface */ -SSF_API const struct ssf_fresnel_type ssf_fresnel_dielectric; -/* Fresnel term of a metalic surface */ -SSF_API const struct ssf_fresnel_type ssf_fresnel_conductor; +/* Fresnel term between 2 dielectric mediums */ +SSF_API const struct ssf_fresnel_type ssf_fresnel_dielectric_dielectric; +/* Fresnel term between a dielectric and a conductor */ +SSF_API const struct ssf_fresnel_type ssf_fresnel_dielectric_conductor; /******************************************************************************* * BSDF API - Bidirectionnal Scattering Distribution Function. Describes the @@ -155,7 +155,7 @@ ssf_fresnel_get_data void** data); SSF_API res_T -ssf_fresnel_dielectric_setup +ssf_fresnel_dielectric_dielectric_setup (struct ssf_fresnel* fresnel, /* Refraction id of the medium the incoming ray travels in */ const double eta_i, @@ -163,7 +163,7 @@ ssf_fresnel_dielectric_setup const double eta_t); SSF_API res_T -ssf_fresnel_conductor_setup +ssf_fresnel_dielectric_conductor_setup (struct ssf_fresnel* fresnel, const double eta_i, /* Refraction id of the dielectric medium */ const double eta_t, /* Real part of the refraction id of the conductor */ diff --git a/src/ssf_fresnel_dielectric.c b/src/ssf_fresnel_dielectric.c @@ -1,104 +0,0 @@ -/* Copyright (C) |Meso|Star> 2016 (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/>. */ - -#include "ssf.h" -#include "ssf_fresnel_c.h" -#include <rsys/math.h> -#include <math.h> - -struct fresnel_dielectric { - double eta_i; - double eta_t; -}; - -/******************************************************************************* - * Private functions - ******************************************************************************/ -static res_T -fresnel_dielectric_init(struct mem_allocator* allocator, void* fresnel) -{ - struct fresnel_dielectric* fd = fresnel; - (void)allocator; - ASSERT(fresnel); - fd->eta_i = 0.0; - fd->eta_t = 0.0; - return RES_OK; -} - -static void -fresnel_dielectric_release(void* fresnel) -{ - (void)fresnel; -} - -static double -fresnel_dielectric_eval(void* fresnel, const double cos_theta_i) -{ - const struct fresnel_dielectric* fd = fresnel; - double sin_theta_i; - double sin_theta_t; - double cos_theta_t; - double Rp; /* Parallel */ - double Rs; /* Orthogonal */ - ASSERT(fresnel && cos_theta_i >= 0); - - /* Use Snell's low to retrieve cos_theta_t: - * eta_i * sin_theta_i = eta_t * sin_theta_t */ - sin_theta_i = sqrt(MMAX(0.0, 1.0 - cos_theta_i*cos_theta_i)); - sin_theta_t = fd->eta_i * sin_theta_i / fd->eta_t; - if(sin_theta_t >= 1) return 1.0; /* Full reflection */ - cos_theta_t = sqrt(1.0 - sin_theta_t*sin_theta_t); - - /* Compute the reflectance for the light polarized with its electric field - * parallel to the plane of incidence */ - Rp = (fd->eta_i*cos_theta_t - fd->eta_t*cos_theta_i) - / (fd->eta_i*cos_theta_t + fd->eta_t*cos_theta_i); - Rp = fabs(Rp); - Rp = Rp * Rp; - - /* Compute the reflectance for the light polarized with its electric field - * perpendicular to the plane of incidence */ - Rs = (fd->eta_i*cos_theta_i - fd->eta_t*cos_theta_t) - / (fd->eta_i*cos_theta_i + fd->eta_t*cos_theta_t); - Rs = fabs(Rs); - Rs = Rs * Rs; - - return 0.5 * (Rp + Rs); -} - -/******************************************************************************* - * Exported symbols - ******************************************************************************/ -const struct ssf_fresnel_type ssf_fresnel_dielectric = { - fresnel_dielectric_init, - fresnel_dielectric_release, - fresnel_dielectric_eval, - sizeof(struct fresnel_dielectric), - ALIGNOF(struct fresnel_dielectric) -}; - -res_T -ssf_fresnel_dielectric_setup - (struct ssf_fresnel* fresnel, const double eta_i, const double eta_t) -{ - struct fresnel_dielectric* fd; - if(!fresnel || !FRESNEL_TYPE_EQ(&fresnel->type, &ssf_fresnel_dielectric)) - return RES_BAD_ARG; - fd = fresnel->data; - fd->eta_i = eta_i; - fd->eta_t = eta_t; - return RES_OK; -} - diff --git a/src/ssf_fresnel_dielectric_dielectric.c b/src/ssf_fresnel_dielectric_dielectric.c @@ -0,0 +1,107 @@ +/* Copyright (C) |Meso|Star> 2016 (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/>. */ + +#include "ssf.h" +#include "ssf_fresnel_c.h" +#include <rsys/math.h> +#include <math.h> + +struct fresnel_dielectric_dielectric { + double eta_i; + double eta_t; +}; + +/******************************************************************************* + * Private functions + ******************************************************************************/ +static res_T +fresnel_dielectric_dielectric_init + (struct mem_allocator* allocator, void* fresnel) +{ + struct fresnel_dielectric_dielectric* fd = fresnel; + (void)allocator; + ASSERT(fresnel); + fd->eta_i = 0.0; + fd->eta_t = 0.0; + return RES_OK; +} + +static void +fresnel_dielectric_dielectric_release(void* fresnel) +{ + (void)fresnel; +} + +static double +fresnel_dielectric_dielectric_eval(void* fresnel, const double cos_theta_i) +{ + const struct fresnel_dielectric_dielectric* fd = fresnel; + double sin_theta_i; + double sin_theta_t; + double cos_theta_t; + double Rp; /* Parallel */ + double Rs; /* Orthogonal */ + ASSERT(fresnel && cos_theta_i >= 0); + + /* Use Snell's low to retrieve cos_theta_t: + * eta_i * sin_theta_i = eta_t * sin_theta_t */ + sin_theta_i = sqrt(MMAX(0.0, 1.0 - cos_theta_i*cos_theta_i)); + sin_theta_t = fd->eta_i * sin_theta_i / fd->eta_t; + if(sin_theta_t >= 1) return 1.0; /* Full reflection */ + cos_theta_t = sqrt(1.0 - sin_theta_t*sin_theta_t); + + /* Compute the reflectance for the light polarized with its electric field + * parallel to the plane of incidence */ + Rp = (fd->eta_i*cos_theta_t - fd->eta_t*cos_theta_i) + / (fd->eta_i*cos_theta_t + fd->eta_t*cos_theta_i); + Rp = fabs(Rp); + Rp = Rp * Rp; + + /* Compute the reflectance for the light polarized with its electric field + * perpendicular to the plane of incidence */ + Rs = (fd->eta_i*cos_theta_i - fd->eta_t*cos_theta_t) + / (fd->eta_i*cos_theta_i + fd->eta_t*cos_theta_t); + Rs = fabs(Rs); + Rs = Rs * Rs; + + return 0.5 * (Rp + Rs); +} + +/******************************************************************************* + * Exported symbols + ******************************************************************************/ +const struct ssf_fresnel_type ssf_fresnel_dielectric_dielectric = { + fresnel_dielectric_dielectric_init, + fresnel_dielectric_dielectric_release, + fresnel_dielectric_dielectric_eval, + sizeof(struct fresnel_dielectric_dielectric), + ALIGNOF(struct fresnel_dielectric_dielectric) +}; + +res_T +ssf_fresnel_dielectric_dielectric_setup + (struct ssf_fresnel* fresnel, const double eta_i, const double eta_t) +{ + struct fresnel_dielectric_dielectric* fd; + if(!fresnel + || !FRESNEL_TYPE_EQ(&fresnel->type, &ssf_fresnel_dielectric_dielectric)) { + return RES_BAD_ARG; + } + fd = fresnel->data; + fd->eta_i = eta_i; + fd->eta_t = eta_t; + return RES_OK; +} + diff --git a/src/test_ssf_fresnel_dielectric.c b/src/test_ssf_fresnel_dielectric.c @@ -1,60 +0,0 @@ -/* Copyright (C) |Meso|Star> 2016 (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/>. */ - -#include "ssf.h" -#include "test_ssf_utils.h" - -#include <rsys/math.h> - -int -main(int argc, char** argv) -{ - const size_t nsteps = 100; - const double step = (PI/2.0) / (double)nsteps; - size_t i; - struct mem_allocator allocator; - struct ssf_fresnel* fresnel; - struct ssf_fresnel* dummy; - double val; - (void)argc, (void)argv; - - mem_init_proxy_allocator(&allocator, &mem_default_allocator); - CHECK(ssf_fresnel_create(&allocator, &fresnel_dummy, &dummy), RES_OK); - CHECK(ssf_fresnel_create - (&allocator, &ssf_fresnel_dielectric, &fresnel), RES_OK); - - CHECK(ssf_fresnel_dielectric_setup(NULL, -1.0, -1.0), RES_BAD_ARG); - CHECK(ssf_fresnel_dielectric_setup(fresnel, -1.0, -1.0), RES_OK); - CHECK(ssf_fresnel_dielectric_setup(dummy, -1.0, -1.0), RES_BAD_ARG); - CHECK(ssf_fresnel_dielectric_setup(fresnel, 1.000277, 1.5), RES_OK); - - CHECK(ssf_fresnel_eval(fresnel, 1, &val), RES_OK); - CHECK(eq_eps(val, 0.04, 1.e-4), 1); - - FOR_EACH(i, 0, nsteps+1) { - const double theta_i = MMIN((double)i*step, PI/2); - const double cos_theta_i = cos(theta_i); - CHECK(ssf_fresnel_eval(fresnel, cos_theta_i, &val), RES_OK); - printf("%g %g\n", theta_i, val); - } - - CHECK(ssf_fresnel_ref_put(fresnel), RES_OK); - CHECK(ssf_fresnel_ref_put(dummy), RES_OK); - - check_memory_allocator(&allocator); - mem_shutdown_proxy_allocator(&allocator); - CHECK(mem_allocated_size(), 0); - return 0; -} diff --git a/src/test_ssf_fresnel_dielectric_dielectric.c b/src/test_ssf_fresnel_dielectric_dielectric.c @@ -0,0 +1,60 @@ +/* Copyright (C) |Meso|Star> 2016 (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/>. */ + +#include "ssf.h" +#include "test_ssf_utils.h" + +#include <rsys/math.h> + +int +main(int argc, char** argv) +{ + const size_t nsteps = 100; + const double step = (PI/2.0) / (double)nsteps; + size_t i; + struct mem_allocator allocator; + struct ssf_fresnel* fresnel; + struct ssf_fresnel* dummy; + double val; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + CHECK(ssf_fresnel_create(&allocator, &fresnel_dummy, &dummy), RES_OK); + CHECK(ssf_fresnel_create + (&allocator, &ssf_fresnel_dielectric_dielectric, &fresnel), RES_OK); + + CHECK(ssf_fresnel_dielectric_dielectric_setup(NULL, -1.0, -1.0), RES_BAD_ARG); + CHECK(ssf_fresnel_dielectric_dielectric_setup(fresnel, -1.0, -1.0), RES_OK); + CHECK(ssf_fresnel_dielectric_dielectric_setup(dummy,-1.0, -1.0), RES_BAD_ARG); + CHECK(ssf_fresnel_dielectric_dielectric_setup(fresnel,1.000277, 1.5), RES_OK); + + CHECK(ssf_fresnel_eval(fresnel, 1, &val), RES_OK); + CHECK(eq_eps(val, 0.04, 1.e-4), 1); + + FOR_EACH(i, 0, nsteps+1) { + const double theta_i = MMIN((double)i*step, PI/2); + const double cos_theta_i = cos(theta_i); + CHECK(ssf_fresnel_eval(fresnel, cos_theta_i, &val), RES_OK); + printf("%g %g\n", theta_i, val); + } + + CHECK(ssf_fresnel_ref_put(fresnel), RES_OK); + CHECK(ssf_fresnel_ref_put(dummy), RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + return 0; +}