solstice

Compute collected power and efficiencies of a solar plant
git clone git://git.meso-star.com/solstice.git
Log | Files | Refs | README | LICENSE

commit d32fb505a3452b1cffa2fba284c4435dce3d9bcf
parent 450a4545be809f1c2550bbdc0d0ce56de50563fe
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue, 12 Dec 2017 11:36:12 +0100

Merge branch 'feature_microfacet_pillbox' into develop

Diffstat:
Mcmake/parser/CMakeLists.txt | 1+
Mdoc/solstice-input.5.txt | 24+++++++++++++++++++-----
Msrc/parser/solparser_material.c | 39+++++++++++++++++++++++++++++++++++++--
Msrc/parser/solparser_material.h | 10+++++++++-
Msrc/parser/test_solparser4.c | 1+
Asrc/parser/test_solparser_mirror.c | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/parser/yaml/test_ko_0.yaml | 11+++++++++++
Msrc/parser/yaml/test_ok_1.yaml | 14++++++++++++++
Msrc/solstice_material.c | 22+++++++++++++++++++++-
9 files changed, 242 insertions(+), 9 deletions(-)

diff --git a/cmake/parser/CMakeLists.txt b/cmake/parser/CMakeLists.txt @@ -113,6 +113,7 @@ if(NOT NO_TEST) new_test(test_solparser8) new_test(test_solparser_normal_map) new_test(test_solparser_spectrum) + new_test(test_solparser_mirror) rcmake_copy_runtime_libraries(test_solparser) endif() diff --git a/doc/solstice-input.5.txt b/doc/solstice-input.5.txt @@ -186,9 +186,13 @@ _______ <mirror> ::= mirror: reflectivity: <mtl-data> # in [0, 1] - roughness: <mtl-data> # in [0, 1] + roughness: <mtl-data> + [ microfacet: <normal-distrib> ] # Default is BECKMANN [ <normal-map> ] +<normal-distrib> ::= BECKMANN + | PILLBOX + <virtual> ::= virtual: EMPTY-STRING <thin-dielectric> ::= thin_dielectric: @@ -331,7 +335,7 @@ shapes are: As the gaussian distribution is not truncated, the resulting sun vector can theoreticaly be oriented towards the sun, especially with a big, non-typical *std_dev* value. - + *buie*:: The *buie* distribution, as first discribed in [3]. Its single *csr* parameter is the ratio between the circumsolar irradiance and the sum of @@ -423,14 +427,24 @@ of the incoming direction. *mirror*:: Specular or glossy reflection whether the *roughness* parameter is 0 or not, -respectively. Glossy reflections are controlled by a microfacet BRDF with the -Beckmann normal distribution defined as: +respectively. Glossy reflections are controlled by a microfacet BRDF. The +microfacet normals are distributed with respect to the Beckmann or the Pillbox +distribution according to the *normal-distrib* attribute. ++ +Let m the *roughness* parameter in ]0, 1]. The Beckmann distribution is +defined as: + ....... D(wh) = exp(-tan^2(a) / m^2) / (PI * m^2 * cos^4(a)) ....... + -with a = arccos(wh.N) and m the *roughness* in ]0,1] of the interface. +with a = arccos(wh.N), while the pillbox distribution is defined as: ++ +....... + | 0; if |wh.N| >= m +D(wh) = | + | 1 / (PI * (1 - cos^2(m))); if |wh.N| < m +....... *thin-dielectric*:: The interface is assumed to be a thin slab of a dielectric material. The diff --git a/src/parser/solparser_material.c b/src/parser/solparser_material.c @@ -22,6 +22,38 @@ * Helper functions ******************************************************************************/ static res_T +parse_microfacet + (struct solparser* parser, + const yaml_node_t* microfacet, + enum solparser_microfacet_distribution* distrib) +{ + res_T res = RES_OK; + ASSERT(microfacet && distrib); + + if(microfacet->type != YAML_SCALAR_NODE) { + log_err(parser, microfacet, + "expect the name of a microfacet distribution.\n"); + res = RES_BAD_ARG; + goto error; + } + + if(!strcmp((char*)microfacet->data.scalar.value, "BECKMANN")) { + *distrib = SOLPARSER_MICROFACET_BECKMANN; + } else if(!strcmp((char*)microfacet->data.scalar.value, "PILLBOX")) { + *distrib = SOLPARSER_MICROFACET_PILLBOX; + } else { + log_err(parser, microfacet, "unknown microfacet distribution `%s'.\n", + microfacet->data.scalar.value); + res = RES_BAD_ARG; + goto error; + } +exit: + return res; +error: + goto exit; +} + +static res_T parse_material_dielectric (struct solparser* parser, yaml_document_t* doc, @@ -213,7 +245,7 @@ parse_material_mirror const yaml_node_t* mirror, struct solparser_material_mirror_id* out_imtl) { - enum { NORMAL_MAP, REFLECTIVITY, ROUGHNESS }; + enum { MICROFACET, NORMAL_MAP, REFLECTIVITY, ROUGHNESS }; struct solparser_material_mirror* mtl = NULL; size_t imtl = SIZE_MAX; int mask = 0; /* Register the parsed attributes */ @@ -259,7 +291,10 @@ parse_material_mirror } \ mask |= BIT(Flag); \ } (void)0 - if(!strcmp((char*)key->data.scalar.value, "normal_map")) { + if(!strcmp((char*)key->data.scalar.value, "microfacet")) { + SETUP_MASK(MICROFACET, "microfacet"); + res = parse_microfacet(parser, val, &mtl->ufacet_distrib); + } else if(!strcmp((char*)key->data.scalar.value, "normal_map")) { SETUP_MASK(NORMAL_MAP, "normal_map"); res = parse_image(parser, doc, val, &mtl->normal_map); } else if(!strcmp((char*)key->data.scalar.value, "reflectivity")) { diff --git a/src/parser/solparser_material.h b/src/parser/solparser_material.h @@ -28,12 +28,16 @@ enum solparser_material_type { SOLPARSER_MATERIAL_VIRTUAL }; +enum solparser_microfacet_distribution { + SOLPARSER_MICROFACET_BECKMANN, + SOLPARSER_MICROFACET_PILLBOX +}; + struct solparser_material_dielectric { struct solparser_medium_id medium_i; /* Medium the material "looks at" */ struct solparser_medium_id medium_t; /* Opposite medium */ struct solparser_image_id normal_map; }; - struct solparser_material_dielectric_id { size_t i; }; static INLINE void @@ -66,6 +70,7 @@ solparser_material_matte_init struct solparser_material_mirror { struct solparser_mtl_data roughness; /* In [0, 1] */ struct solparser_mtl_data reflectivity; /* In [0, 1] */ + enum solparser_microfacet_distribution ufacet_distrib; struct solparser_image_id normal_map; }; @@ -78,6 +83,9 @@ solparser_material_mirror_init { ASSERT(mirror); (void)allocator; + mirror->roughness.type = SOLPARSER_MTL_DATA_REAL; + mirror->roughness.value.real = 0; + mirror->ufacet_distrib = SOLPARSER_MICROFACET_BECKMANN; mirror->normal_map.i = SIZE_MAX; } diff --git a/src/parser/test_solparser4.c b/src/parser/test_solparser4.c @@ -163,6 +163,7 @@ main(int argc, char** argv) CHK(mirror->reflectivity.value.real == 0.2); CHK(mirror->roughness.type == SOLPARSER_MTL_DATA_REAL); CHK(mirror->roughness.value.real == 0.1); + CHK(mirror->ufacet_distrib == SOLPARSER_MICROFACET_BECKMANN); shape = solparser_get_shape(parser, obj->shape); CHK(shape->type == SOLPARSER_SHAPE_CUBOID); diff --git a/src/parser/test_solparser_mirror.c b/src/parser/test_solparser_mirror.c @@ -0,0 +1,129 @@ +/* Copyright (C) CNRS 2016-2017 + * + * 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 "solparser.h" +#include "test_solstice_utils.h" + +static void +check_object + (struct solparser* parser, + const struct solparser_object* obj, + const double reflectivity, + const double roughness, + const enum solparser_microfacet_distribution distrib) +{ + const struct solparser_shape* shape; + const struct solparser_shape_sphere* sphere; + const struct solparser_material_double_sided* mtl2; + const struct solparser_material* mtl; + const struct solparser_material_mirror* mirror; + + CHK(obj != NULL); + + shape = solparser_get_shape(parser, obj->shape); + CHK(shape->type == SOLPARSER_SHAPE_SPHERE); + sphere = solparser_get_shape_sphere(parser, shape->data.sphere); + CHK(sphere->radius == 1); + + mtl2 = solparser_get_material_double_sided(parser, obj->mtl2); + CHK(mtl2->front.i == mtl2->back.i); + mtl = solparser_get_material(parser, mtl2->front); + CHK(mtl->type == SOLPARSER_MATERIAL_MIRROR); + mirror = solparser_get_material_mirror(parser, mtl->data.mirror); + CHK(mirror->reflectivity.type == SOLPARSER_MTL_DATA_REAL); + CHK(mirror->reflectivity.value.real == reflectivity); + CHK(mirror->roughness.type == SOLPARSER_MTL_DATA_REAL); + CHK(mirror->roughness.value.real == roughness); + CHK(mirror->ufacet_distrib == distrib); +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct solparser* parser; + struct solparser_entity_iterator it, it_end; + struct solparser_entity_id entity_id; + const struct solparser_entity* entity; + const struct solparser_geometry* geom; + const struct solparser_object* obj[3]; + FILE* stream; + (void)argc, (void)argv; + + CHK((stream = tmpfile()) != NULL); + fprintf(stream, "- sun: {dni: 1}\n"); + fprintf(stream, "- material: &specular\n"); + fprintf(stream, " mirror:\n"); + fprintf(stream, " reflectivity: 1\n"); + fprintf(stream, " roughness: 0\n"); + fprintf(stream, "- material: &beckmann\n"); + fprintf(stream, " mirror:\n"); + fprintf(stream, " reflectivity: 0.5\n"); + fprintf(stream, " roughness: 0.5\n"); + fprintf(stream, " microfacet: BECKMANN\n"); + fprintf(stream, "- material: &pillbox\n"); + fprintf(stream, " mirror:\n"); + fprintf(stream, " reflectivity: 0.2\n"); + fprintf(stream, " roughness: 0.2\n"); + fprintf(stream, " microfacet: PILLBOX\n"); + fprintf(stream, "\n"); + fprintf(stream, "- entity:\n"); + fprintf(stream, " name: entity\n"); + fprintf(stream, " primary: 1\n"); + fprintf(stream, " geometry: \n"); + fprintf(stream, " - sphere: {radius: 1}\n"); + fprintf(stream, " material: *specular\n"); + fprintf(stream, " - sphere: {radius: 1}\n"); + fprintf(stream, " material: *beckmann\n"); + fprintf(stream, " - sphere: {radius: 1}\n"); + fprintf(stream, " material: *pillbox\n"); + rewind(stream); + + CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); + solparser_create(&allocator, &parser); + CHK(solparser_setup(parser, NULL, stream) == RES_OK); + CHK(solparser_load(parser) == RES_OK); + + solparser_entity_iterator_begin(parser, &it); + solparser_entity_iterator_end(parser, &it_end); + CHK(!solparser_entity_iterator_eq(&it, &it_end)); + + entity_id = solparser_entity_iterator_get(&it); + entity = solparser_get_entity(parser, entity_id); + CHK(!strcmp(str_cget(&entity->name), "entity")); + CHK(entity->primary == 1); + CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY); + + geom = solparser_get_geometry(parser, entity->data.geometry); + CHK(solparser_geometry_get_objects_count(geom) == 3); + + obj[0] = solparser_get_object(parser, solparser_geometry_get_object(geom,0)); + obj[1] = solparser_get_object(parser, solparser_geometry_get_object(geom,1)); + obj[2] = solparser_get_object(parser, solparser_geometry_get_object(geom,2)); + check_object(parser, obj[0], 1.0, 0.0, SOLPARSER_MICROFACET_BECKMANN); + check_object(parser, obj[1], 0.5, 0.5, SOLPARSER_MICROFACET_BECKMANN); + check_object(parser, obj[2], 0.2, 0.2, SOLPARSER_MICROFACET_PILLBOX); + + solparser_entity_iterator_next(&it); + CHK(solparser_entity_iterator_eq(&it, &it_end)); + CHK(solparser_load(parser) == RES_BAD_OP); + solparser_ref_put(parser); + + fclose(stream); + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} diff --git a/src/parser/yaml/test_ko_0.yaml b/src/parser/yaml/test_ko_0.yaml @@ -186,6 +186,7 @@ # mirror: # reflectivity: REAL # in [0, 1] # roughness: REAL # in [0, 1] +# [ microfacet: <BECKMANN|PILLBOX> ] # # missing mirror definition @@ -224,6 +225,16 @@ # 2x roughness - material: { mirror: { roughness: 1, roughness: 1 } } --- +# unknown microfacet +- material: { mirror: { roughness: 1, reflectivity: 1, microfacet: BECKBOX }} +--- +# 2x microfacet +- material: + mirror: + roughness: 1 + reflectivity: 1 + microfacet: BECKMANN + microfacet: PILLBOX # # <matte> ::= diff --git a/src/parser/yaml/test_ok_1.yaml b/src/parser/yaml/test_ok_1.yaml @@ -26,6 +26,7 @@ mirror: reflectivity: 1 roughness: 0 + microfacet: BECKMANN - geometry: &cylinder - material: *mirror @@ -56,3 +57,16 @@ geometry: - material: { mirror: { reflectivity: 0, roughness: 0.5 } } cylinder: { height: 1, radius: 1 } +--- +- sun: { dni: 1, spectrum: [{wavelength: 1, data: 1}] } +- entity: + name: entity + primary: 1 + geometry: + - cylinder: { height: 1, radius: 1 } + material: + mirror: + microfacet: PILLBOX + reflectivity: 0 + roughness: 0.5 + diff --git a/src/solstice_material.c b/src/solstice_material.c @@ -66,6 +66,23 @@ perturb_normal d3_normalize(normal, N); } +static enum ssol_microfacet_distribution +solparser_to_ssol_ufacet_distrib + (const enum solparser_microfacet_distribution distrib) +{ + enum ssol_microfacet_distribution ufacet_distrib; + switch(distrib) { + case SOLPARSER_MICROFACET_BECKMANN: + ufacet_distrib = SSOL_MICROFACET_BECKMANN; + break; + case SOLPARSER_MICROFACET_PILLBOX: + ufacet_distrib = SSOL_MICROFACET_PILLBOX; + break; + default: FATAL("Unreachable code.\n"); break; + } + return ufacet_distrib; +} + static void mtl_get_normal (struct ssol_device* dev, @@ -427,6 +444,7 @@ create_material_mirror struct ssol_material* mtl = NULL; struct ssol_param_buffer* pbuf = NULL; struct mirror_param* param; + enum ssol_microfacet_distribution ufacet_distrib; res_T res = RES_OK; ASSERT(solstice && mirror && out_mtl); @@ -470,7 +488,9 @@ create_material_mirror shader.reflectivity = mirror_get_reflectivity; shader.roughness = mirror_get_roughness; - SSOL(mirror_setup(mtl, &shader, SSOL_MICROFACET_BECKMANN)); + + ufacet_distrib = solparser_to_ssol_ufacet_distrib(mirror->ufacet_distrib); + SSOL(mirror_setup(mtl, &shader, ufacet_distrib)); SSOL(material_set_param_buffer(mtl, pbuf)); exit: