star-gf

Compute Gebhart factors
git clone git://git.meso-star.fr/star-gf.git
Log | Files | Refs | README | LICENSE

commit 2e9b98a63d2d7eaab762a53cd6d2e8e5d278e117
parent 4fa5bf5faa979dcd2e7b83d32274eddacc0ab8a3
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri, 11 Sep 2015 13:49:04 +0200

Update the SGF API

Use the new S3D API to retrieve informations on the scene primitives
rather than relying on additional data provided by the user of the SGF
library.

Diffstat:
Msrc/sgf.c | 72+++++++++++++++++++++++++++++++++++++++++-------------------------------
Msrc/sgf.h | 10+---------
Msrc/sgf_accum.h | 6+++++-
Msrc/test_sgf_cube.c | 5+----
Msrc/test_sgf_tetrahedron.c | 7++-----
Msrc/test_sgf_utils.h | 20--------------------
6 files changed, 50 insertions(+), 70 deletions(-)

diff --git a/src/sgf.c b/src/sgf.c @@ -42,10 +42,7 @@ static INLINE char check_context(struct sgf_context* ctx) { ASSERT(ctx); - return ctx->get_triangle - && ctx->get_material_property - && ctx->primitives_count - && ctx->primitive_id < ctx->primitives_count + return ctx->get_material_property && ctx->scene && ctx->logger; } @@ -60,6 +57,9 @@ gebhart_radiative_path { struct s3d_hit hit; struct sgf_accum* gebfac; /* Gebhart factor */ + struct s3d_primitive prim; + struct s3d_attrib attrib; + float st[2] = {0.f, 0.f}; double transmissivity = 1.0; double proba_reflec_spec; double emissivity; @@ -67,8 +67,8 @@ gebhart_radiative_path double specularity; const double trans_min = 1.e-8; size_t iprim, iprim_from; - float vec0[3], vec1[3]; - float triangle[3][3]; + size_t nprims; + float vec0[3]; float normal[3]; float pos[3]; float dir[4]; @@ -81,16 +81,19 @@ gebhart_radiative_path if(emissivity <= 0.0) return RES_OK; - /* Compute the geometric normal of the triangle */ - ctx->get_triangle(ctx->geometry, ctx->primitive_id, triangle); - f3_sub(vec0, triangle[1], triangle[0]); - f3_sub(vec1, triangle[2], triangle[0]); - f3_cross(normal, vec1, vec0); - f3_normalize(normal, normal); + S3D(scene_primitives_count(ctx->scene, &nprims)); + S3D(scene_get_primitive(ctx->scene, (unsigned)ctx->primitive_id, &prim)); - /* Uniformly sample the triangle to define the starting position of the + /* Compute the geometric normal of the primitive */ + S3D(primitive_get_attrib(&prim, S3D_GEOMETRY_NORMAL, st, &attrib)); + f3_normalize(normal, attrib.value); + + /* Uniformly sample the primitive to define the starting position of the * radiative path */ - ssp_ran_triangle_uniform(rng, triangle[0], triangle[1], triangle[2], pos); + S3D(primitive_sample + (&prim, (float)ssp_rng_canonical(rng), (float)ssp_rng_canonical(rng), st)); + S3D(primitive_get_attrib(&prim, S3D_POSITION, st, &attrib)); + f3_set(pos, attrib.value); /* Cosine weighted sampling of the starting radiative path direction around * `normal' */ @@ -122,18 +125,6 @@ gebhart_radiative_path f3_mulf(vec0, dir, hit.distance); f3_add(pos, vec0, pos); iprim = hit.prim.prim_id; - ASSERT(iprim < ctx->primitives_count); - - /* FIXME Why ? */ - ctx->get_triangle(ctx->geometry, iprim, triangle); - ssp_ran_triangle_uniform(rng, triangle[0], triangle[1], triangle[2], pos); -#ifndef NDEBUG - { - /*Ax + By + Cz + D*/ - const float dist = f3_dot(pos, normal) - f3_dot(normal, triangle[0]); - ASSERT(eq_eps(dist, 0, 1.e-6f)); - } -#endif /* Fetch material property */ emissivity = ctx->get_material_property @@ -174,13 +165,12 @@ gebhart_radiative_path f3_add(dir, dir, vec0); f3_normalize(dir, dir); } - ctx->get_triangle(ctx->geometry, iprim, triangle); } -#if 1 && !defined(NDEBUG) +#if !defined(NDEBUG) { /* Ensure the energy conservation property */ double sum_radiative_flux = 0.0; gebfac = result; - FOR_EACH(iprim, 0, ctx->primitives_count) { + FOR_EACH(iprim, 0, nprims) { sum_radiative_flux += gebfac[ispectral_band].radiative_flux; gebfac += ctx->spectral_bands_count; } @@ -238,6 +228,7 @@ sgf_integrate struct smc_accumulator* accum = NULL; struct smc_accumulator_status status; size_t i; + size_t nprims; int mask; res_T res = RES_OK; @@ -254,11 +245,30 @@ sgf_integrate if((mask & S3D_TRACE) == 0) { if(context->verbose) { logger_print(context->logger, LOG_ERROR, - "No active trace session on the Star-3D scene\n"); + "No active \"trace\" session on the Star-3D scene\n"); } res = RES_BAD_OP; goto error; } + if((mask & S3D_GET_PRIMITIVE) == 0) { + if(context->verbose) { + logger_print(context->logger, LOG_ERROR, + "No active \"get primitive\" session on the Star-3D scene\n"); + } + res = RES_BAD_OP; + goto error; + } + + S3D(scene_primitives_count(context->scene, &nprims)); + if(context->primitive_id >= nprims) { + if(context->verbose) { + logger_print(context->logger, LOG_ERROR, + "Invalid primitive index `%lu'\n", + (unsigned long)context->primitive_id); + } + res = RES_BAD_ARG; + goto error; + } res = smc_integrate(smc, &integrator, context, &accum); if(res != RES_OK) { @@ -277,7 +287,7 @@ sgf_integrate goto error; } - FOR_EACH(i, 0, context->primitives_count) { + FOR_EACH(i, 0, nprims) { accbuf[i] = darray_gfacc_cdata_get((struct darray_gfacc*)status.value)[i]; } exit: diff --git a/src/sgf.h b/src/sgf.h @@ -52,13 +52,6 @@ enum sgf_material_property { /* Descriptor of a gebhart factor integration */ struct sgf_context { - /* Retrieve the triangle from the client side memory */ - void (*get_triangle) - (void* geometry, /* Client side geometry */ - const size_t primitive_id, /* Triangle identifier */ - float triangle[3/*#vertices*/][3/*#coords*/]); /* Output data */ - void* geometry; /* Client side geometry */ - /* Retrieve material properties from client side memory */ double (*get_material_property) (void* material, /* Client side material */ @@ -67,7 +60,6 @@ struct sgf_context { const size_t spectral_band_id); /* Spectral band identifier */ void* material; /* Client side material */ - size_t primitives_count; /* Total number of primitives */ size_t primitive_id; /* Triangle id on which the integration is performed */ size_t spectral_bands_count; /* Total number of spectral bands */ @@ -77,7 +69,7 @@ struct sgf_context { }; static const struct sgf_context SGF_CONTEXT_NULL = { - NULL, NULL, NULL, NULL, 0, 0, 0, NULL, NULL, 0 + NULL, NULL, 0, 0, NULL, NULL, 0 }; /* Integrated value */ diff --git a/src/sgf_accum.h b/src/sgf_accum.h @@ -32,6 +32,7 @@ #include <rsys/dynamic_array.h> #include <rsys/logger.h> +#include <star/s3d.h> #include <star/smc.h> #define DARRAY_DATA struct sgf_accum @@ -43,14 +44,17 @@ accum_buffer_create(struct mem_allocator* allocator, void* context) { struct sgf_context* ctx = context; struct darray_gfacc* buf = NULL; + size_t nprims; res_T res = RES_OK; ASSERT(allocator && ctx); buf = MEM_ALLOC(allocator, sizeof(struct darray_gfacc)); if(!buf) goto error; + S3D(scene_primitives_count(ctx->scene, &nprims)); + darray_gfacc_init(allocator, buf); - res = darray_gfacc_resize(buf, ctx->primitives_count); + res = darray_gfacc_resize(buf, nprims); if(res != RES_OK) goto error; exit: diff --git a/src/test_sgf_cube.c b/src/test_sgf_cube.c @@ -130,11 +130,8 @@ main(int argc, char** argv) CHECK(s3d_mesh_setup_indexed_vertices(shape, (unsigned)nprims, get_ids, (unsigned)nvertices, attribs, &mesh), RES_OK); - ctx.get_triangle = get_triangle; - ctx.geometry = &mesh; ctx.get_material_property = get_material_property; ctx.material = &mtr; - ctx.primitives_count = nprims; ctx.spectral_bands_count = 1; ctx.scene = scn; ctx.logger = LOGGER_DEFAULT; @@ -159,7 +156,7 @@ main(int argc, char** argv) CHECK(sgf_integrate(NULL, &ctx, NSTEPS, gfacc), RES_BAD_ARG); CHECK(sgf_integrate(smc, &ctx, NSTEPS, gfacc), RES_BAD_OP); - CHECK(s3d_scene_begin_session(scn, S3D_TRACE), RES_OK); + CHECK(s3d_scene_begin_session(scn, S3D_TRACE|S3D_GET_PRIMITIVE), RES_OK); FOR_EACH(iprim, 0, nprims) { struct sgf_accum* row = gfacc + iprim*nprims; ctx.primitive_id = iprim; diff --git a/src/test_sgf_tetrahedron.c b/src/test_sgf_tetrahedron.c @@ -35,7 +35,7 @@ #include <star/s3d.h> #include <star/smc.h> -#define NSTEPS 100000L +#define NSTEPS 10000L static const float vertices[] = { 0.57735026918962576450f, 0.f, 0.f, @@ -97,11 +97,8 @@ main(int argc, char** argv) CHECK(s3d_mesh_setup_indexed_vertices(shape, (unsigned)nprims, get_ids, (unsigned)nvertices, attribs, &mesh), RES_OK); - ctx.get_triangle = get_triangle; - ctx.geometry = &mesh; ctx.get_material_property = get_material_property; ctx.material = &mtr; - ctx.primitives_count = nprims; ctx.spectral_bands_count = 1; ctx.scene = scn; ctx.logger = LOGGER_DEFAULT; @@ -109,7 +106,7 @@ main(int argc, char** argv) gfacc = sa_add(gfacc, nprims*nprims); - CHECK(s3d_scene_begin_session(scn, S3D_TRACE), RES_OK); + CHECK(s3d_scene_begin_session(scn, S3D_TRACE|S3D_GET_PRIMITIVE), RES_OK); FOR_EACH(iprim, 0, nprims) { struct sgf_accum* row = gfacc + iprim*nprims; ctx.primitive_id = iprim; diff --git a/src/test_sgf_utils.h b/src/test_sgf_utils.h @@ -70,26 +70,6 @@ get_pos(const unsigned ivert, float pos[3], void* data) pos[2] = mesh->vertices[i + 2]; } -static void -get_triangle(void* geometry, const size_t itri, float triangle[3][3]) -{ - struct triangle_mesh* mesh = geometry; - unsigned ids[3]; - int ivert; - NCHECK(mesh, NULL); - NCHECK(triangle, NULL); - CHECK(itri < mesh->ntriangles, 1); - get_ids((unsigned)itri, ids, mesh); - FOR_EACH(ivert, 0, 3) { - float pos[3]; - int icoord; - get_pos(ids[ivert], pos, mesh); - FOR_EACH(icoord, 0, 3) { - triangle[ivert][icoord] = pos[icoord]; - } - } -} - static double get_material_property (void* material,