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_square_on_square.c (4334B)


      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 /* This test has been created using the sg2_geometry_dump_as_C_code feature
     17  * of star-geometry-2D. It uses output from test_sg2_square_on_square. */
     18 
     19 #define _POSIX_C_SOURCE 200112L /* snprintf */
     20 
     21 #include "senc2d.h"
     22 #include "test_senc2d_utils.h"
     23 
     24 #include <rsys/double2.h>
     25 
     26 #include <stdio.h>
     27 
     28 /*
     29          +-----------------------+
     30          |                       3
     31          |     +------+          |
     32       m2 | m1  | m0   2          |
     33          |     |      |--> N     |
     34          |     +------+          |
     35          |     | m0   1          |
     36          |     |      |--> N     |
     37          |     +------+          |
     38          |--> N                  |
     39          +-----------------------+
     40  */
     41 
     42 /* Dump of star-geometry-2D 'square_on_square'. */
     43 static const unsigned square_on_square_vertices_count = 10;
     44 static const double square_on_square_vertices[20] =
     45 {
     46    1, 1,
     47    2, 1,
     48    1, 2,
     49    2, 2,
     50    1, 3,
     51    2, 3,
     52    0.4, 0,
     53    4, 0,
     54    0, 4.4,
     55    4, 4
     56 };
     57 static const unsigned square_on_square_segments_count = 11;
     58 static const unsigned square_on_square_segments[22] =
     59 {
     60    0, 2,
     61    2, 3,
     62    3, 1,
     63    1, 0,
     64    2, 4,
     65    4, 5,
     66    5, 3,
     67    8, 6,
     68    9, 8,
     69    7, 9,
     70    6, 7
     71 };
     72 static const unsigned square_on_square_properties[33] =
     73 {
     74    1, 0, 0,
     75    0, 0, 0,
     76    1, 0, 0,
     77    1, 0, 0,
     78    1, 0, 0,
     79    1, 0, 0,
     80    1, 0, 0,
     81    1, 2, 0,
     82    1, 2, 0,
     83    1, 2, 0,
     84    1, 2, 0
     85 };
     86 
     87 int
     88 main(int argc, char** argv)
     89 {
     90   struct mem_allocator allocator;
     91   struct senc2d_device* dev = NULL;
     92   struct senc2d_scene* scn = NULL;
     93   struct context ctx = CONTEXT_NULL__;
     94   unsigned count, e;
     95   (void)argc, (void)argv;
     96 
     97   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     98   OK(senc2d_device_create(NULL, &allocator, SENC2D_NTHREADS_DEFAULT, 1, &dev));
     99 
    100   /* Create a scene with the squares.
    101    * The enclosures in the small squares contain medium 0, the external enclosure
    102    * contains medium 2, the enclosure between the small and big squares
    103    * contains medium 1. */
    104   ctx.positions = square_on_square_vertices;
    105   ctx.indices = square_on_square_segments;
    106   ctx.properties = square_on_square_properties;
    107   OK(senc2d_scene_create(dev,
    108     SENC2D_CONVENTION_NORMAL_FRONT | SENC2D_CONVENTION_NORMAL_INSIDE,
    109     square_on_square_segments_count, get_indices, get_media_from_properties,
    110     square_on_square_vertices_count, get_position, &ctx, &scn));
    111 
    112   OK(senc2d_scene_get_enclosure_count(scn, &count));
    113   CHK(count == 4);
    114 
    115   OK(senc2d_scene_get_vertices_count(scn, &count));
    116   CHK(count == square_on_square_vertices_count);
    117 
    118   OK(senc2d_scene_get_segments_count(scn, &count));
    119   CHK(count == square_on_square_segments_count);
    120 
    121   OK(senc2d_scene_get_enclosure_count(scn, &count));
    122   CHK(count == 4);
    123   FOR_EACH(e, 0, count) {
    124     struct senc2d_enclosure* enclosure;
    125     struct senc2d_enclosure_header header;
    126     unsigned m;
    127     char name[128]; (void)name;
    128     OK(senc2d_scene_get_enclosure(scn, e, &enclosure));
    129     OK(senc2d_enclosure_get_header(enclosure, &header));
    130     CHK(header.enclosed_media_count == 1);
    131     OK(senc2d_enclosure_get_medium(enclosure, 0, &m));
    132     if(header.is_infinite) ASSERT(m == 2); /* External */
    133     else if(header.primitives_count == 4) ASSERT(m == 0); /* Internal */
    134     else  ASSERT(m == 1); /* In between */
    135     OK(senc2d_enclosure_ref_put(enclosure));
    136 #ifdef DUMP_ENCLOSURES
    137     snprintf(name, sizeof(name), "test_square_on_square_%u.obj", e);
    138     dump_enclosure(scn, e, name);
    139 #endif
    140   }
    141 
    142   OK(senc2d_scene_ref_put(scn));
    143   OK(senc2d_device_ref_put(dev));
    144 
    145   check_memory_allocator(&allocator);
    146   mem_shutdown_proxy_allocator(&allocator);
    147   CHK(mem_allocated_size() == 0);
    148   return 0;
    149 }