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 68b1b1401648160f86964af8c435833164eb198a
parent 95987dd2f27fb613bd9ca9cb40f473e7179705e0
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 27 Jun 2016 15:41:30 +0200

Test the s2d_primitive API

Diffstat:
Mcmake/CMakeLists.txt | 1+
Asrc/test_s2d_primitive.c | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 146 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -127,6 +127,7 @@ if(NOT NO_TEST) endfunction() new_test(test_s2d_device) + new_test(test_s2d_primitive) new_test(test_s2d_sample) new_test(test_s2d_scene) new_test(test_s2d_shape) diff --git a/src/test_s2d_primitive.c b/src/test_s2d_primitive.c @@ -0,0 +1,145 @@ +/* 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> + +#define NSAMPS 4096 + +static float +rand_canonic(void) +{ + int r; + while((r = rand()) == RAND_MAX); + return (float)r / (float)RAND_MAX; +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct s2d_attrib attr; + struct s2d_primitive prim; + struct s2d_vertex_data vdata; + struct s2d_device* dev; + struct s2d_scene* scn; + struct s2d_shape* shape; + unsigned box_id; + float tmp[2]; + float length; + float s; + int i; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + CHECK(s2d_device_create(NULL, &allocator, 1, &dev), RES_OK); + CHECK(s2d_scene_create(dev, &scn), RES_OK); + CHECK(s2d_shape_create_line_segments(dev, &shape), RES_OK); + CHECK(s2d_shape_get_id(shape, &box_id), RES_OK); + CHECK(s2d_scene_attach_shape(scn, shape), RES_OK); + + vdata.usage = S2D_POSITION; + vdata.type = S2D_FLOAT2; + vdata.get = box_get_position; + CHECK(s2d_line_segments_setup_indexed_vertices + (shape, box_nsegs, box_get_ids, box_nverts, &vdata, 1, (void*)&box_desc), + RES_OK); + + CHECK(s2d_scene_begin_session(scn, S2D_SAMPLE), RES_OK); + CHECK(s2d_scene_sample(scn, 0, 0, &prim, &s), RES_OK); + CHECK(s2d_scene_end_session(scn), RES_OK); + + #define GET_ATTRIB s2d_primitive_get_attrib + CHECK(GET_ATTRIB(NULL, S2D_ATTRIBS_COUNT__, 2, NULL), RES_BAD_ARG); + CHECK(GET_ATTRIB(&prim, S2D_ATTRIBS_COUNT__, 2, NULL), RES_BAD_ARG); + CHECK(GET_ATTRIB(NULL, S2D_ATTRIBS_COUNT__, s, NULL), RES_BAD_ARG); + CHECK(GET_ATTRIB(&prim, S2D_ATTRIBS_COUNT__, s, NULL), RES_BAD_ARG); + CHECK(GET_ATTRIB(NULL, S2D_ATTRIBS_COUNT__, 2, &attr), RES_BAD_ARG); + CHECK(GET_ATTRIB(&prim, S2D_ATTRIBS_COUNT__, 2, &attr), RES_BAD_ARG); + CHECK(GET_ATTRIB(NULL, S2D_ATTRIBS_COUNT__, s, &attr), RES_BAD_ARG); + CHECK(GET_ATTRIB(&prim, S2D_ATTRIBS_COUNT__, s, &attr), RES_BAD_ARG); + CHECK(GET_ATTRIB(NULL, S2D_POSITION, 2, NULL), RES_BAD_ARG); + CHECK(GET_ATTRIB(&prim, S2D_POSITION, 2, NULL), RES_BAD_ARG); + CHECK(GET_ATTRIB(NULL, S2D_POSITION, s, NULL), RES_BAD_ARG); + CHECK(GET_ATTRIB(&prim, S2D_POSITION, s, NULL), RES_BAD_ARG); + CHECK(GET_ATTRIB(NULL, S2D_POSITION, 2, &attr), RES_BAD_ARG); + CHECK(GET_ATTRIB(&prim, S2D_POSITION, 2, &attr), RES_BAD_ARG); + CHECK(GET_ATTRIB(NULL, S2D_POSITION, s, &attr), RES_BAD_ARG); + CHECK(GET_ATTRIB(&prim, S2D_POSITION, s, &attr), RES_OK); + CHECK(attr.type, S2D_FLOAT2); + CHECK(attr.usage, S2D_POSITION); + + CHECK(GET_ATTRIB(&prim, S2D_GEOMETRY_NORMAL, s, &attr), RES_OK); + f2_normalize(attr.value, attr.value); + switch(prim.prim_id) { + case 0: CHECK(f2_eq_eps(attr.value, f2(tmp, 0.f, 1.f), 1.e-6f), 1); break; + case 1: CHECK(f2_eq_eps(attr.value, f2(tmp,-1.f, 0.f), 1.e-6f), 1); break; + case 2: CHECK(f2_eq_eps(attr.value, f2(tmp, 0.f,-1.f), 1.e-6f), 1); break; + case 3: CHECK(f2_eq_eps(attr.value, f2(tmp, 1.f, 0.f), 1.e-6f), 1); break; + default: CHECK(0, 1); /* Invalid primitive id */ + } + + CHECK(GET_ATTRIB(&S2D_PRIMITIVE_NULL, S2D_GEOMETRY_NORMAL, s, &attr), + RES_BAD_ARG); + #undef GET_ATTRIB + + CHECK(s2d_primitive_compute_length(NULL, NULL), RES_BAD_ARG); + CHECK(s2d_primitive_compute_length(&prim, NULL), RES_BAD_ARG); + CHECK(s2d_primitive_compute_length(NULL, &length), RES_BAD_ARG); + CHECK(s2d_primitive_compute_length(&prim, &length), RES_OK); + CHECK(eq_epsf(length, 2, 1.e-6f), 1); + + CHECK(s2d_primitive_sample(NULL, 2.f, NULL), RES_BAD_ARG); + CHECK(s2d_primitive_sample(&prim, 2.f, NULL), RES_BAD_ARG); + CHECK(s2d_primitive_sample(NULL, 0.5f, NULL), RES_BAD_ARG); + CHECK(s2d_primitive_sample(&prim, 0.5f, NULL), RES_BAD_ARG); + CHECK(s2d_primitive_sample(NULL, 2.f, &s), RES_BAD_ARG); + CHECK(s2d_primitive_sample(&prim, 2.f, &s), RES_BAD_ARG); + CHECK(s2d_primitive_sample(NULL, 0.5f, &s), RES_BAD_ARG); + CHECK(s2d_primitive_sample(&prim, 0.5f, &s), RES_OK); + CHECK(eq_epsf(s, 0.5f, 1.e-6f), 1); + + FOR_EACH(i, 0, NSAMPS) { + CHECK(s2d_primitive_sample(&prim, rand_canonic(), &s), RES_OK); + CHECK(s >= 0.f, 1); + CHECK(s <= 1.f, 1); + } + + CHECK(s2d_device_ref_put(dev), RES_OK); + CHECK(s2d_scene_ref_put(scn), RES_OK); + CHECK(s2d_shape_ref_put(shape), RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + return 0; +} +