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_in_square.c (6440B)


      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_in_square. */
     18 
     19 #include "senc2d.h"
     20 #include "test_senc2d_utils.h"
     21 
     22 #include <rsys/double2.h>
     23 
     24 /*
     25             square_2                            square_3
     26 
     27                                        +-------------------------+
     28                                        |                         B
     29          +-------------+               |     +-------------+     |
     30       m1 |     m0      M            m1 |  m1 |      m0     M     |
     31          | +------+    |               | m0  | +------+    |     |
     32          | |  m1  S    |               |     | |  m1  S    |     |
     33          | | N <--|    |               |     | | N <--|    |     |
     34          | +------+    |               |     | +------+    |     |
     35          |        N <--|               |     |        N <--|     |
     36          +-------------+               |     +-------------+     |
     37                                        |                    N <--|
     38                                        +-------------------------+
     39  */
     40 
     41 /* Dump of star-geometry-2D 'square_in_square_2'. */
     42 static const unsigned square_in_square_2_vertices_count = 8;
     43 static const double square_in_square_2_vertices[16] =
     44 {
     45    0.1, 0,
     46    1, 0,
     47    0, 1.1,
     48    1, 1,
     49    -0.7, -1,
     50    2, -1,
     51    -1, 2.3,
     52    2, 2
     53 };
     54 static const unsigned square_in_square_2_segments_count = 8;
     55 static const unsigned square_in_square_2_segments[16] =
     56 {
     57    0, 2,
     58    2, 3,
     59    3, 1,
     60    1, 0,
     61    6, 4,
     62    7, 6,
     63    5, 7,
     64    4, 5
     65 };
     66 static const unsigned square_in_square_2_properties[24] =
     67 {
     68    0, 1, 0,
     69    0, 1, 0,
     70    0, 1, 0,
     71    0, 1, 0,
     72    0, 1, 0,
     73    0, 1, 0,
     74    0, 1, 0,
     75    0, 1, 0
     76 };
     77 /* Dump of star-geometry-2D 'square_in_square_3'. */
     78 static const unsigned square_in_square_3_vertices_count = 12;
     79 static const double square_in_square_3_vertices[24] =
     80 {
     81    0.1, 0,
     82    1, 0,
     83    0, 1.1,
     84    1, 1,
     85    -0.7, -1,
     86    2, -1,
     87    -1, 2.3,
     88    2, 2,
     89    -3, -4,
     90    6, -4,
     91    -4, 7,
     92    6, 6
     93 };
     94 static const unsigned square_in_square_3_segments_count = 12;
     95 static const unsigned square_in_square_3_segments[24] =
     96 {
     97    0, 2,
     98    2, 3,
     99    3, 1,
    100    1, 0,
    101    6, 4,
    102    7, 6,
    103    5, 7,
    104    4, 5,
    105    10, 8,
    106    11, 10,
    107    9, 11,
    108    8, 9
    109 };
    110 static const unsigned square_in_square_3_properties[36] =
    111 {
    112    0, 1, 0,
    113    0, 1, 0,
    114    0, 1, 0,
    115    0, 1, 0,
    116    0, 1, 0,
    117    0, 1, 0,
    118    0, 1, 0,
    119    0, 1, 0,
    120    0, 1, 0,
    121    0, 1, 0,
    122    0, 1, 0,
    123    0, 1, 0
    124 };
    125 
    126 int
    127 main(int argc, char** argv)
    128 {
    129   struct mem_allocator allocator;
    130   struct senc2d_device* dev = NULL;
    131   struct senc2d_scene* scn = NULL;
    132   struct context ctx = CONTEXT_NULL__;
    133   unsigned e, ecount, maxm, e2;
    134   (void)argc, (void)argv;
    135 
    136   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
    137   OK(senc2d_device_create(NULL, &allocator, SENC2D_NTHREADS_DEFAULT, 1, &dev));
    138 
    139   /* Create a scene with the first and second squares.
    140    * The enclosure in the small contains medium 1, the external enclosure
    141    * contains medium 1, the enclosure between the small and medium squares
    142    * contains medium 0. */
    143   ctx.positions = square_in_square_2_vertices;
    144   ctx.indices = square_in_square_2_segments;
    145   ctx.properties = square_in_square_2_properties;
    146   OK(senc2d_scene_create(dev,
    147     SENC2D_CONVENTION_NORMAL_FRONT | SENC2D_CONVENTION_NORMAL_INSIDE,
    148     square_in_square_2_segments_count, get_indices, get_media_from_properties,
    149     square_in_square_2_vertices_count, get_position, &ctx, &scn));
    150 
    151   OK(senc2d_scene_get_enclosure_count(scn, &ecount));
    152   CHK(ecount == 3);
    153 
    154   FOR_EACH(e, 0, ecount) {
    155     struct senc2d_enclosure* enclosure;
    156     struct senc2d_enclosure_header header;
    157     unsigned m;
    158     OK(senc2d_scene_get_enclosure(scn, e, &enclosure));
    159     OK(senc2d_enclosure_get_header(enclosure, &header));
    160     ASSERT(header.enclosed_media_count == 1);
    161     OK(senc2d_enclosure_get_medium(enclosure, 0, &m));
    162     ASSERT(m <= 1);
    163     ASSERT((m == 0) == (header.primitives_count == square_in_square_2_segments_count));
    164     OK(senc2d_enclosure_ref_put(enclosure));
    165   }
    166 
    167   OK(senc2d_scene_get_max_medium(scn, &maxm));
    168   CHK(maxm == 1);
    169   OK(senc2d_scene_ref_put(scn));
    170 
    171   /* Create a scene with the 3 squares, same 2 first squares as above.
    172    * The enclosure in the small square contains medium 1, the external enclosure
    173    * contains medium 1, the enclosure between the small and medium squares
    174    * contains medium 0 and the enclosure between the medium and big squares
    175    * contains both media 0 and 1. */
    176   ctx.positions = square_in_square_3_vertices;
    177   ctx.indices = square_in_square_3_segments;
    178   ctx.properties = square_in_square_3_properties;
    179   OK(senc2d_scene_create(dev,
    180     SENC2D_CONVENTION_NORMAL_FRONT | SENC2D_CONVENTION_NORMAL_INSIDE,
    181     square_in_square_3_segments_count, get_indices, get_media_from_properties,
    182     square_in_square_3_vertices_count, get_position, &ctx, &scn));
    183 
    184   OK(senc2d_scene_get_enclosure_count(scn, &ecount));
    185   CHK(ecount == 4);
    186 
    187   e2 = ecount;
    188   FOR_EACH(e, 0, ecount) {
    189     struct senc2d_enclosure* enclosure;
    190     struct senc2d_enclosure_header header;
    191     OK(senc2d_scene_get_enclosure(scn, e, &enclosure));
    192     OK(senc2d_enclosure_get_header(enclosure, &header));
    193     ASSERT(header.enclosed_media_count <= 2);
    194     if(header.enclosed_media_count == 2) {
    195       /* A single internal enclosure has 2 media */
    196       ASSERT(!header.is_infinite);
    197       ASSERT(e2 == ecount); (void)e2;
    198       e2 = e;
    199     }
    200     OK(senc2d_enclosure_ref_put(enclosure));
    201   }
    202 
    203   OK(senc2d_scene_get_max_medium(scn, &maxm));
    204   CHK(maxm == 1);
    205 
    206   OK(senc2d_scene_ref_put(scn));
    207   OK(senc2d_device_ref_put(dev));
    208 
    209   check_memory_allocator(&allocator);
    210   mem_shutdown_proxy_allocator(&allocator);
    211   CHK(mem_allocated_size() == 0);
    212   return 0;
    213 }