star-enclosures-2d

Extract enclosures from 2D geometry
git clone git://git.meso-star.fr/star-enclosures-2d.git
Log | Files | Refs | README | LICENSE

test_senc2d_sample_enclosure.c (3919B)


      1 /* Copyright (C) 2018-2021, 2023, 2024 |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 "senc2d.h"
     17 #include "senc2d_sXd_helper.h"
     18 #include "test_senc2d_utils.h"
     19 
     20 #include <rsys/float2.h>
     21 #include <rsys/double2.h>
     22 
     23 #include <star/s2d.h>
     24 #include <star/ssp.h>
     25 
     26 int
     27 main(int argc, char** argv)
     28 {
     29   struct mem_allocator allocator;
     30   struct senc2d_device* dev = NULL;
     31   struct senc2d_scene* scn = NULL;
     32   struct senc2d_enclosure* enclosure = NULL;
     33   struct senc2d_enclosure_header header;
     34   struct s2d_device* s2d = NULL;
     35   struct s2d_scene* s2d_scn = NULL;
     36   struct s2d_scene_view* s2d_view = NULL;
     37   struct s2d_shape* s2d_shp = NULL;
     38   struct s2d_primitive prim;
     39   struct s2d_vertex_data vrtx_get;
     40   struct ssp_rng* rng;
     41   struct context ctx = CONTEXT_NULL__;
     42   int i;
     43   float st;
     44   (void)argc, (void)argv;
     45 
     46   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     47   OK(senc2d_device_create(NULL, &allocator, SENC2D_NTHREADS_DEFAULT, 1, &dev));
     48 
     49   /* A 2D square, but with a hole (incomplete).
     50    * 1 single enclosure including both sides of segments */
     51   ctx.positions = square_vertices;
     52   ctx.indices = box_indices;
     53   ctx.front_media = medium0;
     54   ctx.back_media = medium0;
     55 
     56   OK(senc2d_scene_create(dev,
     57     SENC2D_CONVENTION_NORMAL_FRONT | SENC2D_CONVENTION_NORMAL_INSIDE,
     58     nsegments - 1, get_indices, get_media, nvertices, get_position, &ctx,
     59     &scn));
     60 
     61   OK(senc2d_scene_get_enclosure(scn, 0, &enclosure));
     62   OK(senc2d_enclosure_get_header(enclosure, &header));
     63 
     64   /* Put enclosure in a 2D view... */
     65   vrtx_get.type = S2D_FLOAT2;
     66   vrtx_get.usage = S2D_POSITION;
     67   vrtx_get.get = senc2d_sXd_enclosure_get_position;
     68   S2D(device_create(NULL, &allocator, 0, &s2d));
     69   S2D(scene_create(s2d, &s2d_scn));
     70   S2D(shape_create_line_segments(s2d, &s2d_shp));
     71   S2D(line_segments_setup_indexed_vertices(s2d_shp, header.primitives_count,
     72     senc2d_sXd_enclosure_get_indices, header.vertices_count, &vrtx_get, 1,
     73     enclosure));
     74   S2D(scene_attach_shape(s2d_scn, s2d_shp));
     75   S2D(scene_view_create(s2d_scn, S2D_SAMPLE, &s2d_view));
     76 
     77   /* ... and sample it. */
     78   OK(ssp_rng_create(&allocator, SSP_RNG_THREEFRY, &rng));
     79   FOR_EACH(i, 0, 10000) {
     80     struct s2d_attrib attrib;
     81     int n, c;
     82     S2D(scene_view_sample(s2d_view, ssp_rng_canonical_float(rng),
     83       ssp_rng_canonical_float(rng), &prim, &st));
     84     S2D(primitive_get_attrib(&prim, S2D_POSITION, st, &attrib));
     85     c = 0;
     86     FOR_EACH(n, 0, 2)
     87       if(eq_eps(attrib.value[n], 0, FLT_EPSILON)
     88         || eq_eps(attrib.value[n], 1, FLT_EPSILON))
     89         c++;
     90     CHK(c == 1);
     91     S2D(primitive_get_attrib(&prim, S2D_GEOMETRY_NORMAL, st, &attrib));
     92     c = 0;
     93     FOR_EACH(n, 0, 2)
     94       if(eq_eps(attrib.value[n], -1, FLT_EPSILON)
     95         || eq_eps(attrib.value[n], 1, FLT_EPSILON))
     96         c++;
     97     CHK(c == 1);
     98     c = 0;
     99     FOR_EACH(n, 0, 2)
    100       if(eq_eps(attrib.value[n], 0, FLT_EPSILON))
    101         c++;
    102     CHK(c == 1);
    103   }
    104 
    105   SENC2D(enclosure_ref_put(enclosure));
    106   SENC2D(scene_ref_put(scn));
    107   SENC2D(device_ref_put(dev));
    108 
    109   SSP(rng_ref_put(rng));
    110 
    111   S2D(shape_ref_put(s2d_shp));
    112   S2D(scene_view_ref_put(s2d_view));
    113   S2D(device_ref_put(s2d));
    114   S2D(scene_ref_put(s2d_scn));
    115 
    116   check_memory_allocator(&allocator);
    117   mem_shutdown_proxy_allocator(&allocator);
    118   CHK(mem_allocated_size() == 0);
    119 
    120   return 0;
    121 }