star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.fr/star-3d.git
Log | Files | Refs | README | LICENSE

commit fed683466778f5706b32676c12cfb7bf71365500
parent b5f75e6b4f96443ea32c2a14d1b643ea10e88fa2
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu, 18 Jan 2018 11:29:25 +0100

Test the spherical shapes

Diffstat:
Mcmake/CMakeLists.txt | 2++
Msrc/s3d_scene_view.c | 8+++++---
Asrc/test_s3d_sphere.c | 172+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_s3d_trace_ray_sphere.c | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 311 insertions(+), 3 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -147,7 +147,9 @@ if(NOT NO_TEST) new_test(test_s3d_scene) new_test(test_s3d_scene_view) new_test(test_s3d_shape) + new_test(test_s3d_sphere) new_test(test_s3d_trace_ray_instance) + new_test(test_s3d_trace_ray_sphere) new_test(test_s3d_seams) build_test(test_s3d_trace_ray) diff --git a/src/s3d_scene_view.c b/src/s3d_scene_view.c @@ -226,7 +226,7 @@ embree_geometry_register (struct s3d_scene_view* scnview, struct geometry* geom) { - ASSERT(scnview && geom && (geom->type==GEOM_MESH || geom->type==GEOM_INSTANCE)); + ASSERT(scnview && geom); /* Create the Embree geometry if it is not valid */ if(geom->irtc != RTC_INVALID_GEOMETRY_ID) { @@ -553,6 +553,7 @@ scene_view_register_sphere res = htable_geom_set(&scnview->cached_geoms, &shape_id, &geom); if(res != RES_OK) goto error; geom->name = shape->id.index; + geom->embree_outdated_mask |= EMBREE_USER_GEOMETRY; } /* Setup the sphere radius */ @@ -859,6 +860,7 @@ scene_view_compute_volume volume += flip ? -sphere_compute_volume(geom->data.sphere) : sphere_compute_volume(geom->data.sphere); + break; case GEOM_INSTANCE: volume += scene_view_compute_volume(geom->data.instance->scnview, flip); break; @@ -1389,8 +1391,8 @@ s3d_scene_view_get_primitive ASSERT(pgeom); geom = *pgeom; } - ASSERT(geom->type == GEOM_MESH); - ASSERT(i < mesh_get_ntris(geom->data.mesh)); + ASSERT(geom->type == GEOM_MESH || geom->type == GEOM_SPHERE); + ASSERT(geom->type != GEOM_MESH || i < mesh_get_ntris(geom->data.mesh)); prim->shape__ = geom; prim->geom_id = geom->name; prim->prim_id = (unsigned)i; diff --git a/src/test_s3d_sphere.c b/src/test_s3d_sphere.c @@ -0,0 +1,172 @@ +/* Copyright (C) |Meso|Star> 2015-2018 (contact@meso-star.com) + * + * This software is a computer program whose purpose is to describe a + * virtual 3D environment that can be ray-traced and sampled both robustly + * and efficiently. + * + * 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 "s3d.h" +#include "test_s3d_utils.h" + +#include <rsys/float2.h> +#include <rsys/float3.h> + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct s3d_primitive prim0; + struct s3d_primitive prim1; + struct s3d_hit hit; + struct s3d_device* dev; + struct s3d_scene* scn; + struct s3d_shape* sphere0; + struct s3d_shape* sphere1; + struct s3d_scene_view* view; + float center[3] = {0, 0, 0}; + float radius; + float org[3]; + float dir[3]; + float range[2]; + float N[3], P[3], tmp[3]; + float area; + float volume; + size_t nprims; + unsigned sphere0_id; + unsigned sphere1_id; + (void)argc, (void)argv; + + CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); + + CHK(s3d_device_create(NULL, &allocator, 0, &dev) == RES_OK); + CHK(s3d_scene_create(dev, &scn) == RES_OK); + + CHK(s3d_shape_create_sphere(NULL, NULL) == RES_BAD_ARG); + CHK(s3d_shape_create_sphere(dev, NULL) == RES_BAD_ARG); + CHK(s3d_shape_create_sphere(NULL, &sphere0) == RES_BAD_ARG); + CHK(s3d_shape_create_sphere(dev, &sphere0) == RES_OK); + + CHK(s3d_sphere_setup(NULL, NULL, -1) == RES_BAD_ARG); + CHK(s3d_sphere_setup(sphere0, NULL, -1) == RES_BAD_ARG); + CHK(s3d_sphere_setup(NULL, center, -1) == RES_BAD_ARG); + CHK(s3d_sphere_setup(sphere0, center, -1) == RES_BAD_ARG); + CHK(s3d_sphere_setup(NULL, NULL, 1) == RES_BAD_ARG); + CHK(s3d_sphere_setup(sphere0, NULL, 1) == RES_BAD_ARG); + CHK(s3d_sphere_setup(NULL, center, 1) == RES_BAD_ARG); + CHK(s3d_sphere_setup(sphere0, center, 1) == RES_OK); + CHK(s3d_sphere_setup(sphere0, center, 0) == RES_BAD_ARG); + CHK(s3d_shape_ref_put(sphere0) == RES_OK); + + CHK(s3d_shape_create_sphere(dev, &sphere0) == RES_OK); + CHK(s3d_scene_attach_shape(scn, sphere0) == RES_OK); + CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); + + f3(org, 0, 0, 100); + f3(dir, 0, 0, -1); + f2(range, 0, FLT_MAX); + + CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); + CHK(S3D_HIT_NONE(&hit)); + + radius = 2; + CHK(s3d_sphere_setup(sphere0, center, 2) == RES_OK); + CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); + CHK(S3D_HIT_NONE(&hit)); + CHK(s3d_scene_view_ref_put(view) == RES_OK); + CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); + CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); + CHK(!S3D_HIT_NONE(&hit)); + CHK(eq_epsf(hit.distance, 100 - radius, 1.e-3f)); + f3_normalize(N, hit.normal); + f3_add(P, org, f3_mulf(P, dir, hit.distance)); + CHK(f3_eq_eps(N, f3(tmp, 0, 0, 1), 1.e-3f)); + CHK(f3_eq_eps(P, f3(tmp, 0, 0, radius), 1.e-3f)); + + CHK(s3d_scene_view_compute_area(view, &area) == RES_OK); + CHK(eq_epsf(area, (float)(4*PI*radius*radius), 1.e-6f)); + CHK(s3d_scene_view_compute_volume(view, &volume) == RES_OK); + CHK(eq_epsf(volume, (float)(4.0/3.0*PI*radius*radius*radius), 1.e-6f)); + + CHK(s3d_shape_flip_surface(sphere0) == RES_OK); + CHK(s3d_scene_view_compute_area(view, &area) == RES_OK); + CHK(eq_epsf(area, (float)(4*PI*radius*radius), 1.e-6f)); + CHK(s3d_scene_view_compute_volume(view, &volume) == RES_OK); + CHK(eq_epsf(volume, (float)(4.0/3.0*PI*radius*radius*radius), 1.e-6f)); + + CHK(s3d_scene_view_ref_put(view) == RES_OK); + CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); + + CHK(s3d_scene_view_compute_area(view, &area) == RES_OK); + CHK(eq_epsf(area, (float)(4*PI*radius*radius), 1.e-6f)); + CHK(s3d_scene_view_compute_volume(view, &volume) == RES_OK); + CHK(eq_epsf(volume, (float)(-4.0/3.0*PI*radius*radius*radius), 1.e-6f)); + + CHK(s3d_shape_flip_surface(sphere0) == RES_OK); + CHK(s3d_scene_view_ref_put(view) == RES_OK); + + center[0] = 4; + CHK(s3d_shape_create_sphere(dev, &sphere1) == RES_OK); + CHK(s3d_sphere_setup(sphere1, center, radius) == RES_OK); + CHK(s3d_scene_attach_shape(scn, sphere1) == RES_OK); + CHK(s3d_scene_view_create(scn, S3D_GET_PRIMITIVE, &view) == RES_OK); + + CHK(s3d_scene_view_compute_area(view, &area) == RES_OK); + CHK(eq_epsf(area, (float)(2*4*PI*radius*radius), 1.e-6f)); + CHK(s3d_scene_view_compute_volume(view, &volume) == RES_OK); + CHK(eq_epsf(volume, (float)(2*4.0/3.0*PI*radius*radius*radius), 1.e-6f)); + + CHK(s3d_shape_get_id(sphere0, &sphere0_id) == RES_OK); + CHK(s3d_shape_get_id(sphere1, &sphere1_id) == RES_OK); + CHK(sphere0_id != sphere1_id); + + CHK(s3d_scene_view_primitives_count(view, &nprims) == RES_OK); + CHK(nprims == 2); + + CHK(s3d_scene_view_get_primitive(view, 0, &prim0) == RES_OK); + CHK(s3d_scene_view_get_primitive(view, 1, &prim1) == RES_OK); + CHK(prim0.prim_id == 0); + CHK(prim1.prim_id == 0); + CHK(prim0.geom_id == sphere0_id || prim0.geom_id == sphere1_id); + CHK(prim1.geom_id == sphere0_id || prim1.geom_id == sphere1_id); + CHK(prim0.geom_id != prim1.geom_id); + CHK(prim0.inst_id == S3D_INVALID_ID); + CHK(prim1.inst_id == S3D_INVALID_ID); + CHK(prim0.scene_prim_id == 0); + CHK(prim1.scene_prim_id == 1); + + CHK(s3d_shape_ref_put(sphere0) == RES_OK); + CHK(s3d_shape_ref_put(sphere1) == RES_OK); + CHK(s3d_scene_view_ref_put(view) == RES_OK); + CHK(s3d_scene_ref_put(scn) == RES_OK); + CHK(s3d_device_ref_put(dev) == RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} diff --git a/src/test_s3d_trace_ray_sphere.c b/src/test_s3d_trace_ray_sphere.c @@ -0,0 +1,132 @@ +/* Copyright (C) |Meso|Star> 2015-2018 (contact@meso-star.com) + * + * This software is a computer program whose purpose is to describe a + * virtual 3D environment that can be ray-traced and sampled both robustly + * and efficiently. + * + * 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 "s3d.h" +#include "test_s3d_camera.h" +#include "test_s3d_utils.h" + +#include <rsys/image.h> +#include <rsys/math.h> + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct image img; + struct camera cam; + struct s3d_device* dev; + struct s3d_scene* scn; + struct s3d_shape* shape; + struct s3d_scene_view* view; + const size_t img_sz[2] = {640, 480}; + const float radius = 2; + const float center[3] = {1.0, 1.0, 0}; + float pos[3] = {0, 0, 0}; + float tgt[3] = {0, 0, 0}; + float up[3] = {0, 1, 0}; + float proj_ratio; + size_t x, y; + int hit_something = 0; + (void)argc, (void)argv; + + CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); + image_init(&allocator, &img); + CHK(image_setup + (&img, img_sz[0], img_sz[1], img_sz[0]*3, IMAGE_RGB8, NULL) == RES_OK); + + CHK(s3d_device_create(NULL, &allocator, 0, &dev) == RES_OK); + CHK(s3d_scene_create(dev, &scn) == RES_OK); + CHK(s3d_shape_create_sphere(dev, &shape) == RES_OK); + CHK(s3d_sphere_setup(shape, center, radius) == RES_OK); + CHK(s3d_scene_attach_shape(scn, shape) == RES_OK); + CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); + + f3(pos, 0, 0, -10); + f3(tgt, 0, 0, 0); + f3(up, 0, 1, 0); + proj_ratio = (float)img_sz[0] / (float)img_sz[1]; + camera_init(&cam, pos, tgt, up, (float)PI*0.25f, proj_ratio); + + FOR_EACH(y, 0, img_sz[1]) { + float pixel[2]; + pixel[1] = (float)y / (float)img_sz[1]; + FOR_EACH(x, 0, img_sz[0]) { + const size_t ipix = (y*img_sz[0] + x)*3/*RGB*/; + struct s3d_hit hit; + const float range[2] = {0, FLT_MAX}; + float org[3]; + float dir[3]; + + pixel[0] = (float)x/(float)img_sz[0]; + camera_ray(&cam, pixel, org, dir); + CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); + if(S3D_HIT_NONE(&hit)) { + ((uint8_t*)img.pixels)[ipix+0] = 0; + ((uint8_t*)img.pixels)[ipix+1] = 0; + ((uint8_t*)img.pixels)[ipix+2] = 0; + } else { + float normal[3] = {0.f, 0.f, 0.f}; + float tmp[3]; + float len; + float dot; + f3_normalize(normal, hit.normal); + + f3_add(pos, org, f3_mulf(pos, dir, hit.distance)); + len = f3_len(f3_sub(pos, pos, center)); + CHK(eq_epsf(len, radius, 1.e-3f)); + CHK(f3_eq_eps(f3_mulf(tmp, normal, radius), pos, 1.e-3f)); + + dot = absf(f3_dot(normal, dir)); + ((uint8_t*)img.pixels)[ipix+0] = (uint8_t)(dot*255.f); + ((uint8_t*)img.pixels)[ipix+1] = (uint8_t)(dot*255.f); + ((uint8_t*)img.pixels)[ipix+2] = (uint8_t)(dot*255.f); + hit_something = 1; + } + } + } + CHK(hit_something == 1); + + /* Write image */ + CHK(image_write_ppm_stream(&img, 0, stdout) == RES_OK); + image_release(&img); + + CHK(s3d_device_ref_put(dev) == RES_OK); + CHK(s3d_scene_ref_put(scn) == RES_OK); + CHK(s3d_shape_ref_put(shape) == RES_OK); + CHK(s3d_scene_view_ref_put(view) == RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} +