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_unspecified_medium.c (9237B)


      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_unspecified_properties.
     18  * This test is similar to test_senc2d_many_enclosures that creates a huge
     19  * geometry by program. */
     20 
     21 #include "senc2d.h"
     22 #include "senc2d_sXd_helper.h"
     23 #include "test_senc2d_utils.h"
     24 
     25 #include <rsys/double2.h>
     26 
     27 #define SG2_UNSPECIFIED_PROPERTY UINT_MAX
     28 
     29 /* Dump of star-geometry-2D 'front_unspecified'. */
     30 static const unsigned front_unspecified_vertices_count = 4;
     31 static const double front_unspecified_vertices[8] =
     32 {
     33    0.1, 0,
     34    1, 0,
     35    0, 1.1,
     36    1, 1
     37 };
     38 static const unsigned front_unspecified_segments_count = 4;
     39 static const unsigned front_unspecified_segments[8] =
     40 {
     41    0, 2,
     42    2, 3,
     43    3, 1,
     44    1, 0
     45 };
     46 static const unsigned front_unspecified_properties[12] =
     47 {
     48    SG2_UNSPECIFIED_PROPERTY, 1, SG2_UNSPECIFIED_PROPERTY,
     49    SG2_UNSPECIFIED_PROPERTY, 1, SG2_UNSPECIFIED_PROPERTY,
     50    SG2_UNSPECIFIED_PROPERTY, 1, SG2_UNSPECIFIED_PROPERTY,
     51    SG2_UNSPECIFIED_PROPERTY, 1, SG2_UNSPECIFIED_PROPERTY
     52 };
     53 /* Dump of star-geometry-2D 'front_half_unspecified'. */
     54 static const unsigned front_half_unspecified_vertices_count = 4;
     55 static const double front_half_unspecified_vertices[8] =
     56 {
     57    0.1, 0,
     58    1, 0,
     59    0, 1.1,
     60    1, 1
     61 };
     62 static const unsigned front_half_unspecified_segments_count = 4;
     63 static const unsigned front_half_unspecified_segments[8] =
     64 {
     65    0, 2,
     66    2, 3,
     67    3, 1,
     68    1, 0
     69 };
     70 static const unsigned front_half_unspecified_properties[12] =
     71 {
     72    SG2_UNSPECIFIED_PROPERTY, 1, SG2_UNSPECIFIED_PROPERTY,
     73    0, 1, 0,
     74    SG2_UNSPECIFIED_PROPERTY, 1, SG2_UNSPECIFIED_PROPERTY,
     75    0, 1, 0
     76 };
     77 /* Dump of star-geometry-2D 'all_defined'. */
     78 static const unsigned all_defined_vertices_count = 4;
     79 static const double all_defined_vertices[8] =
     80 {
     81    0.1, 0,
     82    1, 0,
     83    0, 1.1,
     84    1, 1
     85 };
     86 static const unsigned all_defined_segments_count = 4;
     87 static const unsigned all_defined_segments[8] =
     88 {
     89    0, 2,
     90    2, 3,
     91    3, 1,
     92    1, 0
     93 };
     94 static const unsigned all_defined_properties[12] =
     95 {
     96    0, 1, 0,
     97    0, 1, 0,
     98    0, 1, 0,
     99    0, 1, 0
    100 };
    101 
    102 static void
    103 test(const int convention)
    104 {
    105   struct mem_allocator allocator;
    106   struct senc2d_device* dev = NULL;
    107   struct senc2d_scene* scn = NULL;
    108   struct senc2d_enclosure* enclosure;
    109   struct senc2d_enclosure_header header;
    110   unsigned medium, expected_external_medium, expected_internal_medium;
    111   unsigned gid;
    112   enum senc2d_side side;
    113   struct context ctx = CONTEXT_NULL__;
    114   unsigned i, s, ecount;
    115   const int conv_front = (convention & SENC2D_CONVENTION_NORMAL_FRONT) != 0;
    116 
    117   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
    118   OK(senc2d_device_create(NULL, &allocator, SENC2D_NTHREADS_DEFAULT, 1, &dev));
    119   
    120   /* Geometry with no media information on both sides */
    121   ctx.positions = front_unspecified_vertices;
    122   ctx.indices = front_unspecified_segments;
    123   ctx.properties = front_unspecified_properties;
    124   OK(senc2d_scene_create(dev, convention, front_unspecified_segments_count,
    125     get_indices, get_media_from_properties, front_unspecified_vertices_count,
    126     get_position, &ctx, &scn));
    127 
    128   OK(senc2d_scene_get_enclosure_count(scn, &ecount));
    129   CHK(ecount == 2);
    130 
    131   FOR_EACH(i, 0, ecount) {
    132     struct senc2d_enclosure* ee;
    133     struct senc2d_enclosure_header hh;
    134     unsigned cc;
    135     OK(senc2d_scene_get_enclosure(scn, i, &enclosure));
    136     OK(senc2d_enclosure_get_header(enclosure, &header));
    137 
    138     CHK(header.enclosure_id == i);
    139     CHK(header.enclosed_media_count == 1);
    140 
    141     OK(senc2d_enclosure_get_medium(enclosure, 0, &medium));
    142     /* Geometrical normals point outside the square in input segments:
    143      * if convention is front, front medium (undef) is outside,
    144      * that is medium 0's enclosure is infinite */
    145     expected_external_medium = conv_front ? SENC2D_UNSPECIFIED_MEDIUM : 1;
    146     expected_internal_medium = conv_front ? 1 : SENC2D_UNSPECIFIED_MEDIUM;
    147 
    148     CHK(medium == (header.is_infinite
    149       ? expected_external_medium : expected_internal_medium));
    150     CHK(header.primitives_count == nsegments);
    151     CHK(header.unique_primitives_count == nsegments);
    152     CHK(header.vertices_count == nvertices);
    153     CHK(header.is_infinite == (i == 0));
    154 
    155     OK(senc2d_scene_get_enclosure_count_by_medium(scn, medium, &cc));
    156     CHK(cc == 1);
    157     OK(senc2d_scene_get_enclosure_by_medium(scn, medium, 0, &ee));
    158     OK(senc2d_enclosure_get_header(ee, &hh));
    159     CHK(header.enclosure_id == hh.enclosure_id);
    160     OK(senc2d_enclosure_ref_put(ee));
    161 
    162     FOR_EACH(s, 0, header.primitives_count) {
    163       unsigned ind[2];
    164       OK(senc2d_enclosure_get_segment_id(enclosure, s, &gid, &side));
    165       CHK(gid == s);
    166       CHK(side == (medium == 1) ? SENC2D_BACK : SENC2D_FRONT);
    167       OK(senc2d_enclosure_get_segment(enclosure, s, ind));
    168     }
    169     OK(senc2d_enclosure_ref_put(enclosure));
    170   }
    171   OK(senc2d_scene_ref_put(scn));
    172 
    173   /* Same geometry, front media are defined for odd segments */
    174   ctx.positions = front_half_unspecified_vertices;
    175   ctx.indices = front_half_unspecified_segments;
    176   ctx.properties = front_half_unspecified_properties;
    177   OK(senc2d_scene_create(dev, convention, front_half_unspecified_segments_count,
    178     get_indices, get_media_from_properties, front_half_unspecified_vertices_count,
    179     get_position, &ctx, &scn));
    180 
    181   OK(senc2d_scene_get_enclosure_count(scn, &ecount));
    182   CHK(ecount == 2);
    183 
    184   FOR_EACH(i, 0, ecount) {
    185     unsigned expected_external_media_count, expected_internal_media_count,
    186       expected_media_count;
    187     OK(senc2d_scene_get_enclosure(scn, i, &enclosure));
    188     OK(senc2d_enclosure_get_header(enclosure, &header));
    189 
    190     CHK(header.enclosure_id == i);
    191 
    192     OK(senc2d_enclosure_get_medium(enclosure, 0, &medium));
    193     /* Geometrical normals point outside the square in input segments:
    194      * if convention is front, front medium is outside and the enclosure
    195      * contains 2 media */
    196     expected_external_media_count = conv_front ? 2 : 1;
    197     expected_internal_media_count = conv_front ? 1 : 2;
    198     expected_media_count = header.is_infinite
    199       ? expected_external_media_count : expected_internal_media_count;
    200     CHK(header.enclosed_media_count == expected_media_count);
    201     OK(senc2d_enclosure_ref_put(enclosure));
    202   }
    203   OK(senc2d_scene_ref_put(scn));
    204 
    205   /* Same geometry, all media are defined */
    206   ctx.positions = all_defined_vertices;
    207   ctx.indices = all_defined_segments;
    208   ctx.properties = all_defined_properties;
    209   OK(senc2d_scene_create(dev, convention, all_defined_segments_count,
    210     get_indices, get_media_from_properties, all_defined_vertices_count,
    211     get_position, &ctx, &scn));
    212 
    213   OK(senc2d_scene_get_enclosure_count(scn, &ecount));
    214   CHK(ecount == 2);
    215 
    216   FOR_EACH(i, 0, ecount) {
    217     struct senc2d_enclosure* ee;
    218     struct senc2d_enclosure_header hh;
    219     unsigned cc;
    220     OK(senc2d_scene_get_enclosure(scn, i, &enclosure));
    221     OK(senc2d_enclosure_get_header(enclosure, &header));
    222 
    223     CHK(header.enclosure_id == i);
    224     CHK(header.enclosed_media_count == 1);
    225     OK(senc2d_enclosure_get_medium(enclosure, 0, &medium));
    226     /* Geometrical normals point outside the square in input segments:
    227      * if convention is front, front medium (0) is outside,
    228      * that is medium 0's enclosure is infinite */
    229     CHK(conv_front == ((medium == 0) == header.is_infinite));
    230     CHK(header.primitives_count == nsegments);
    231     CHK(header.unique_primitives_count == nsegments);
    232     CHK(header.vertices_count == nvertices);
    233     CHK(header.is_infinite == (i == 0));
    234 
    235     OK(senc2d_scene_get_enclosure_count_by_medium(scn, medium, &cc));
    236     CHK(cc == 1);
    237     OK(senc2d_scene_get_enclosure_by_medium(scn, medium, 0, &ee));
    238     OK(senc2d_enclosure_get_header(ee, &hh));
    239     CHK(header.enclosure_id == hh.enclosure_id);
    240     OK(senc2d_enclosure_ref_put(ee));
    241 
    242     FOR_EACH(s, 0, header.primitives_count) {
    243       OK(senc2d_enclosure_get_segment_id(enclosure, s, &gid, &side));
    244       CHK(gid == s);
    245       CHK(side == (medium == 1) ? SENC2D_BACK : SENC2D_FRONT);
    246     }
    247     OK(senc2d_enclosure_ref_put(enclosure));
    248   }
    249 
    250   SENC2D(scene_ref_put(scn));
    251   SENC2D(device_ref_put(dev));
    252 
    253   check_memory_allocator(&allocator);
    254   mem_shutdown_proxy_allocator(&allocator);
    255   CHK(mem_allocated_size() == 0);
    256 }
    257 
    258 int
    259 main(int argc, char** argv)
    260 {
    261   (void)argc, (void)argv;
    262   test(SENC2D_CONVENTION_NORMAL_FRONT | SENC2D_CONVENTION_NORMAL_INSIDE);
    263   test(SENC2D_CONVENTION_NORMAL_BACK | SENC2D_CONVENTION_NORMAL_INSIDE);
    264   test(SENC2D_CONVENTION_NORMAL_FRONT | SENC2D_CONVENTION_NORMAL_OUTSIDE);
    265   test(SENC2D_CONVENTION_NORMAL_BACK | SENC2D_CONVENTION_NORMAL_OUTSIDE);
    266   return 0;
    267 }