star-2d

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

test_s2d_sample.c (6994B)


      1 /* Copyright (C) 2016-2021, 2023 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "s2d.h"
     17 #include "test_s2d_utils.h"
     18 
     19 #include <rsys/float2.h>
     20 
     21 #define NSAMPS 4096
     22 
     23 int
     24 main(int argc, char** argv)
     25 {
     26   struct mem_allocator allocator;
     27   struct s2d_vertex_data vdata;
     28   struct s2d_device* dev;
     29   struct s2d_scene* scn;
     30   struct s2d_scene_view* scnview;
     31   struct s2d_shape* shape;
     32   struct s2d_primitive prim;
     33   float s;
     34   unsigned square_id;
     35   int i;
     36   (void)argc, (void)argv;
     37 
     38   mem_init_proxy_allocator(&allocator, &mem_default_allocator);
     39 
     40   CHK(s2d_device_create(NULL, &allocator, 1, &dev) == RES_OK);
     41   CHK(s2d_scene_create(dev, &scn) == RES_OK);
     42   CHK(s2d_shape_create_line_segments(dev, &shape) == RES_OK);
     43 
     44   CHK(s2d_shape_get_id(shape, &square_id) == RES_OK);
     45   CHK(s2d_scene_attach_shape(scn, shape) == RES_OK);
     46   CHK(s2d_scene_view_create(scn, S2D_SAMPLE, &scnview) == RES_OK);
     47 
     48   CHK(s2d_scene_view_sample(NULL, 1, 1, NULL, NULL) == RES_BAD_ARG);
     49   CHK(s2d_scene_view_sample(scnview, 1, 1, NULL, NULL) == RES_BAD_ARG);
     50   CHK(s2d_scene_view_sample(NULL, 0, 1, NULL, NULL) == RES_BAD_ARG);
     51   CHK(s2d_scene_view_sample(scnview, 0, 1, NULL, NULL) == RES_BAD_ARG);
     52   CHK(s2d_scene_view_sample(NULL, 1, 0, NULL, NULL) == RES_BAD_ARG);
     53   CHK(s2d_scene_view_sample(scnview, 1, 0, NULL, NULL) == RES_BAD_ARG);
     54   CHK(s2d_scene_view_sample(NULL, 0, 0, NULL, NULL) == RES_BAD_ARG);
     55   CHK(s2d_scene_view_sample(scnview, 0, 0, NULL, NULL) == RES_BAD_ARG);
     56   CHK(s2d_scene_view_sample(NULL, 1, 1, &prim, NULL) == RES_BAD_ARG);
     57   CHK(s2d_scene_view_sample(scnview, 1, 1, &prim, NULL) == RES_BAD_ARG);
     58   CHK(s2d_scene_view_sample(NULL, 0, 1, &prim, NULL) == RES_BAD_ARG);
     59   CHK(s2d_scene_view_sample(scnview, 0, 1, &prim, NULL) == RES_BAD_ARG);
     60   CHK(s2d_scene_view_sample(NULL, 1, 0, &prim, NULL) == RES_BAD_ARG);
     61   CHK(s2d_scene_view_sample(scnview, 1, 0, &prim, NULL) == RES_BAD_ARG);
     62   CHK(s2d_scene_view_sample(NULL, 0, 0, &prim, NULL) == RES_BAD_ARG);
     63   CHK(s2d_scene_view_sample(scnview, 0, 0, &prim, NULL) == RES_BAD_ARG);
     64   CHK(s2d_scene_view_sample(NULL, 1, 1, NULL, &s) == RES_BAD_ARG);
     65   CHK(s2d_scene_view_sample(scnview, 1, 1, NULL, &s) == RES_BAD_ARG);
     66   CHK(s2d_scene_view_sample(NULL, 0, 1, NULL,&s) == RES_BAD_ARG);
     67   CHK(s2d_scene_view_sample(scnview, 0, 1, NULL, &s) == RES_BAD_ARG);
     68   CHK(s2d_scene_view_sample(NULL, 1, 0, NULL, &s) == RES_BAD_ARG);
     69   CHK(s2d_scene_view_sample(scnview, 1, 0, NULL, &s) == RES_BAD_ARG);
     70   CHK(s2d_scene_view_sample(NULL, 0, 0, NULL, &s) == RES_BAD_ARG);
     71   CHK(s2d_scene_view_sample(scnview, 0, 0, NULL, &s) == RES_BAD_ARG);
     72   CHK(s2d_scene_view_sample(NULL, 1, 1, &prim, &s) == RES_BAD_ARG);
     73   CHK(s2d_scene_view_sample(scnview, 1, 1, &prim, &s) == RES_BAD_ARG);
     74   CHK(s2d_scene_view_sample(NULL, 0, 1, &prim, &s) == RES_BAD_ARG);
     75   CHK(s2d_scene_view_sample(scnview, 0, 1, &prim, &s) == RES_BAD_ARG);
     76   CHK(s2d_scene_view_sample(NULL, 1, 0, &prim, &s) == RES_BAD_ARG);
     77   CHK(s2d_scene_view_sample(scnview, 1, 0, &prim, &s) == RES_BAD_ARG);
     78   CHK(s2d_scene_view_sample(NULL, 0, 0, &prim, &s) == RES_BAD_ARG);
     79   CHK(s2d_scene_view_sample(scnview, 0, 0, &prim, &s) == RES_OK);
     80   CHK(S2D_PRIMITIVE_EQ(&prim, &S2D_PRIMITIVE_NULL) == 1);
     81 
     82   vdata.usage = S2D_POSITION;
     83   vdata.type = S2D_FLOAT2;
     84   vdata.get = line_segments_get_position;
     85   CHK(s2d_line_segments_setup_indexed_vertices
     86     (shape, square_nsegs, line_segments_get_ids, square_nverts, &vdata, 1,
     87      (void*)&square_desc) == RES_OK);
     88 
     89   CHK(s2d_scene_view_sample(scnview, 0, 0, &prim, &s) == RES_OK);
     90   CHK(S2D_PRIMITIVE_EQ(&prim, &S2D_PRIMITIVE_NULL) == 1);
     91 
     92   CHK(s2d_scene_view_ref_put(scnview) == RES_OK);
     93 
     94   CHK(s2d_scene_view_create(scn, S2D_SAMPLE, &scnview) == RES_OK);
     95   CHK(s2d_scene_view_sample(scnview, 0, 0, &prim, &s) == RES_OK);
     96   CHK(S2D_PRIMITIVE_EQ(&prim, &S2D_PRIMITIVE_NULL) == 0);
     97 
     98   CHK(prim.prim_id < 4);
     99   CHK(prim.geom_id == square_id);
    100   CHK(s == 0);
    101 
    102   /* Should take effect int the next created scene view */
    103   CHK(s2d_shape_flip_contour(shape) == RES_OK);
    104 
    105   FOR_EACH(i, 0, NSAMPS) {
    106     struct s2d_attrib attr_position;
    107     struct s2d_attrib attr_normal;
    108     const float u = rand_canonic();
    109     const float v = rand_canonic();
    110     float N[2], P[2], tmp[2];
    111 
    112     CHK(s2d_scene_view_sample(scnview, u, v, &prim, &s) == RES_OK);
    113     CHK(S2D_PRIMITIVE_EQ(&prim, &S2D_PRIMITIVE_NULL) == 0);
    114     CHK(prim.prim_id < 4);
    115     CHK(prim.geom_id == square_id);
    116 
    117     CHK(s2d_primitive_get_attrib
    118       (&prim, S2D_POSITION, s, &attr_position) == RES_OK);
    119     CHK(s2d_primitive_get_attrib
    120       (&prim, S2D_GEOMETRY_NORMAL, s, &attr_normal) == RES_OK);
    121 
    122     f2_normalize(attr_normal.value, attr_normal.value);
    123 
    124     switch(prim.prim_id) {
    125       case 0: f2(P, -1.f*s + (1-s)*1.f, -1.f); f2(N, 0.f, 1.f); break;
    126       case 1: f2(P, -1.f, 1.f*s + (1-s)*-1.f); f2(N, 1.f, 0.f); break;
    127       case 2: f2(P, 1.f*s + (1-s)* -1.f, 1.f); f2(N, 0.f,-1.f); break;
    128       case 3: f2(P, 1.f, -1.f*s + (1-s)* 1.f); f2(N,-1.f, 0.f); break;
    129       default: CHK(0 == 1); break; /* Invalid primitive id */
    130     }
    131     f2_add(P, P, f2_splat(tmp, 10));
    132     CHK(f2_eq_eps(P, attr_position.value, 1.e-6f));
    133     CHK(f2_eq_eps(N, attr_normal.value, 1.e-6f));
    134   }
    135 
    136   CHK(s2d_scene_view_ref_put(scnview) == RES_OK);
    137 
    138   CHK(s2d_scene_view_create(scn, S2D_SAMPLE, &scnview) == RES_OK);
    139   FOR_EACH(i, 0, NSAMPS) {
    140     struct s2d_attrib attr;
    141     const float u = rand_canonic();
    142     const float v = rand_canonic();
    143     float N[2];
    144 
    145     CHK(s2d_scene_view_sample(scnview, u, v, &prim, &s) == RES_OK);
    146     CHK(S2D_PRIMITIVE_EQ(&prim, &S2D_PRIMITIVE_NULL) == 0);
    147     CHK(prim.prim_id < 4);
    148     CHK(prim.geom_id == square_id);
    149 
    150     CHK(s2d_primitive_get_attrib(&prim, S2D_GEOMETRY_NORMAL, s, &attr) == RES_OK);
    151     f2_normalize(attr.value, attr.value);
    152 
    153     switch(prim.prim_id) {
    154       case 0: f2(N, 0.f,-1.f); break;
    155       case 1: f2(N,-1.f, 0.f); break;
    156       case 2: f2(N, 0.f, 1.f); break;
    157       case 3: f2(N, 1.f, 0.f); break;
    158       default: CHK(0 == 1); break; /* Invalid primitive id */
    159     }
    160 
    161     CHK(f2_eq_eps(N, attr.value, 1.e-6f) == 1);
    162   }
    163 
    164   CHK(s2d_scene_view_ref_put(scnview) == RES_OK);
    165 
    166   CHK(s2d_device_ref_put(dev) == RES_OK);
    167   CHK(s2d_scene_ref_put(scn) == RES_OK);
    168   CHK(s2d_shape_ref_put(shape) == RES_OK);
    169 
    170   check_memory_allocator(&allocator);
    171   mem_shutdown_proxy_allocator(&allocator);
    172   CHK(mem_allocated_size() == 0);
    173   return 0;
    174 }
    175