star-2d

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

commit f99e90503db6a45a10180c3bcb1b1c987ea657ec
parent 6db0f1960518ec690b80fe61289f3ced4a0db55e
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 29 Jun 2016 10:58:22 +0200

Add a test that push further the scene session checks

Diffstat:
Mcmake/CMakeLists.txt | 1+
Asrc/test_s2d_session_advanced.c | 217+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 218 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -130,6 +130,7 @@ if(NOT NO_TEST) new_test(test_s2d_primitive) new_test(test_s2d_sample) new_test(test_s2d_scene) + new_test(test_s2d_session_advanced) new_test(test_s2d_shape) new_test(test_s2d_trace_ray) endif(NOT NO_TEST) diff --git a/src/test_s2d_session_advanced.c b/src/test_s2d_session_advanced.c @@ -0,0 +1,217 @@ +/* Copyright (C) |Meso|Star> 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 "s2d.h" +#include "test_s2d_utils.h" + +#include <rsys/float2.h> +#include <rsys/stretchy_array.h> + +#include <string.h> + +static struct s2d_shape* +create_circle_shape + (struct s2d_device* dev, + const float radius, + const float center[2], + const unsigned nsteps) +{ + float* positions = NULL; + unsigned* indices = NULL; + struct s2d_shape* shape; + struct s2d_vertex_data vdata; + struct line_segments_desc desc = { NULL, NULL }; + const double step = 2.0*PI/(double)nsteps; + unsigned i; + + CHECK(s2d_shape_create_line_segments(dev, &shape), RES_OK); + + CHECK(nsteps > 4, 1); + NCHECK(center, NULL); + NCHECK(sa_add(positions, nsteps*2/*#coords per vertex*/), NULL); + NCHECK(sa_add(indices, nsteps*2/*#ids per segment*/), NULL); + + FOR_EACH(i, 0, nsteps) { + const double theta = i*step; + const double x = radius*cos(theta) + center[0]; + const double y = radius*sin(theta) + center[1]; + positions[i*2 + 0] = (float)x; + positions[i*2 + 1] = (float)y; + } + + FOR_EACH(i, 0, nsteps) { + indices[i*2 + 0] = i; + indices[i*2 + 1] = (i+1) % nsteps; + } + + desc.vertices = positions; + desc.indices = indices; + + vdata.type = S2D_FLOAT2; + vdata.usage = S2D_POSITION; + vdata.get = line_segments_get_position; + + CHECK(s2d_line_segments_setup_indexed_vertices + (shape, nsteps, line_segments_get_ids, nsteps, &vdata, 1, (void*)&desc), + RES_OK); + + CHECK(s2d_shape_flip_contour(shape), RES_OK); + + sa_release(positions); + sa_release(indices); + return shape; +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct s2d_device* dev; + struct s2d_scene* scn; + struct s2d_shape* shape; + struct s2d_primitive prim; + unsigned shape_nsteps[3] = { 16, 8, 5 }; + unsigned shape_ids[3]; + int* shape_prims[3] ={ NULL, NULL, NULL }; + int* scene_prims = NULL; + size_t i; + size_t nprims; + float tmp[2]; + int ishape; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + CHECK(s2d_device_create(NULL, &allocator, 0, &dev), RES_OK); + CHECK(s2d_scene_create(dev, &scn), RES_OK); + + NCHECK(sa_add(shape_prims[0], shape_nsteps[0]), NULL); + NCHECK(sa_add(shape_prims[1], shape_nsteps[1]), NULL); + NCHECK(sa_add(shape_prims[2], shape_nsteps[2]), NULL); + NCHECK(sa_add + (scene_prims, shape_nsteps[0]+shape_nsteps[1]+shape_nsteps[2]), NULL); + + shape = create_circle_shape(dev, 1.f, f2(tmp, 0.f, 0.f), shape_nsteps[0]); + CHECK(s2d_shape_get_id(shape, &shape_ids[0]), RES_OK); + NCHECK(shape_ids[0], S2D_INVALID_ID); + CHECK(s2d_scene_attach_shape(scn, shape), RES_OK); + CHECK(s2d_shape_ref_put(shape), RES_OK); + + shape = create_circle_shape(dev, 0.5f, f2(tmp, 2.f, 0.5f), shape_nsteps[1]); + CHECK(s2d_shape_get_id(shape, &shape_ids[1]), RES_OK); + NCHECK(shape_ids[1], S2D_INVALID_ID); + NCHECK(shape_ids[1], shape_ids[0]); + CHECK(s2d_scene_attach_shape(scn, shape), RES_OK); + CHECK(s2d_shape_ref_put(shape), RES_OK); + + shape = create_circle_shape(dev, 0.25f, f2(tmp, 1.5f, -0.25f), shape_nsteps[2]); + CHECK(s2d_shape_get_id(shape, &shape_ids[2]), RES_OK); + NCHECK(shape_ids[2], S2D_INVALID_ID); + NCHECK(shape_ids[2], shape_ids[0]); + NCHECK(shape_ids[2], shape_ids[1]); + CHECK(s2d_scene_attach_shape(scn, shape), RES_OK); + CHECK(s2d_shape_ref_put(shape), RES_OK); + + CHECK(s2d_scene_begin_session + (scn, S2D_SAMPLE|S2D_GET_PRIMITIVE|S2D_TRACE), RES_OK); + + /* Test sampling */ + memset(shape_prims[0], 0, sa_size(shape_prims[0])*sizeof(shape_prims[0][0])); + memset(shape_prims[1], 0, sa_size(shape_prims[1])*sizeof(shape_prims[1][0])); + memset(shape_prims[2], 0, sa_size(shape_prims[2])*sizeof(shape_prims[2][0])); + memset(scene_prims, 0, sa_size(scene_prims)*sizeof(scene_prims[0])); + FOR_EACH(i, 0, 1024) { + struct s2d_attrib attr; + float s; + CHECK(s2d_scene_sample(scn, rand_canonic(), rand_canonic(), &prim, &s), RES_OK); + CHECK(s2d_primitive_get_attrib(&prim, S2D_POSITION, s, &attr), RES_OK); + CHECK(attr.type, S2D_FLOAT2); + + FOR_EACH(ishape, 0, 3) if(prim.geom_id == shape_ids[ishape]) break; + NCHECK(ishape, 3); + + /* Mark the shape primitive as sampled */ + CHECK(prim.prim_id < shape_nsteps[ishape], 1); + shape_prims[ishape][prim.prim_id] = 1; + + /* Mark the scene primitive as sampled */ + CHECK(prim.scene_prim_id < sa_size(scene_prims), 1); + scene_prims[prim.scene_prim_id] = 1; + } + + /* Check that all primitives were sampled */ + FOR_EACH(i, 0, sa_size(shape_prims[0])) CHECK(shape_prims[0][i], 1); + FOR_EACH(i, 0, sa_size(shape_prims[1])) CHECK(shape_prims[1][i], 1); + FOR_EACH(i, 0, sa_size(shape_prims[2])) CHECK(shape_prims[2][i], 1); + FOR_EACH(i, 0, sa_size(scene_prims)) CHECK(scene_prims[i], 1); + + /* Check iteration */ + memset(shape_prims[0], 0, sa_size(shape_prims[0])*sizeof(shape_prims[0][0])); + memset(shape_prims[1], 0, sa_size(shape_prims[1])*sizeof(shape_prims[1][0])); + memset(shape_prims[2], 0, sa_size(shape_prims[2])*sizeof(shape_prims[2][0])); + memset(scene_prims, 0, sa_size(scene_prims)*sizeof(scene_prims[0])); + CHECK(s2d_scene_primitives_count(scn, &nprims), RES_OK); + CHECK(sa_size(scene_prims), nprims); + FOR_EACH(i, 0, nprims) { + + CHECK(s2d_scene_get_primitive(scn, (unsigned)i, &prim), RES_OK); + FOR_EACH(ishape, 0, 3) if(prim.geom_id == shape_ids[ishape]) break; + NCHECK(ishape, 3); + + /* Mark the shape primitive as visited */ + CHECK(prim.prim_id < shape_nsteps[ishape], 1); + shape_prims[ishape][prim.prim_id] = 1; + + /* Mark the scene primitive as visited */ + CHECK(prim.scene_prim_id < sa_size(scene_prims), 1); + scene_prims[prim.scene_prim_id] = 1; + } + + /* Check that all primitives were visited */ + FOR_EACH(i, 0, sa_size(shape_prims[0])) CHECK(shape_prims[0][i], 1); + FOR_EACH(i, 0, sa_size(shape_prims[1])) CHECK(shape_prims[1][i], 1); + FOR_EACH(i, 0, sa_size(shape_prims[2])) CHECK(shape_prims[2][i], 1); + FOR_EACH(i, 0, sa_size(scene_prims)) CHECK(scene_prims[i], 1); + + CHECK(s2d_scene_end_session(scn), RES_OK); + + CHECK(s2d_scene_ref_put(scn), RES_OK); + CHECK(s2d_device_ref_put(dev), RES_OK); + + sa_release(scene_prims); + sa_release(shape_prims[0]); + sa_release(shape_prims[1]); + sa_release(shape_prims[2]); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + + return 0; +} +