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_inconsistant_square.c (5106B)


      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 "test_senc2d_utils.h"
     18 
     19 #include <rsys/double2.h>
     20 
     21 /* The following array lists the indices toward the 2D vertices of each
     22  * segment.
     23  *                     Y
     24  *    2----3         |
     25  *    |      |         0----X
     26  *    |      |
     27  *    0----1 */
     28 /* Segment #0 order is not consistant with other segments */
     29 static unsigned
     30 inconsistant_box_indices[4/*#segments*/ * 2/*#indices per segment*/] = {
     31   2, 0,
     32   2, 3,
     33   3, 1,
     34   1, 0
     35 };
     36 static const unsigned inconsistant_box_nsegments
     37 = sizeof(inconsistant_box_indices) / (2 * sizeof(*inconsistant_box_indices));
     38 
     39 /* Media definitions reflect segment #0 inconsistancy */
     40 static const unsigned
     41 inconsistant_medium_front[4] = { 1, 0, 0, 0 };
     42 static const unsigned
     43 inconsistant_medium_back[4] = { 0, 1, 1, 1 };
     44 static const double
     45 inconsistant_square_vertices[4/*#vertices*/ * 2/*#coords per vertex*/] = {
     46   10.0, 10.0,
     47   11.0, 10.0,
     48   10.0, 11.0,
     49   11.0, 11.0
     50 };
     51 
     52 static void
     53 test(const int convention)
     54 {
     55   struct mem_allocator allocator;
     56   struct senc2d_device* dev = NULL;
     57   struct senc2d_scene* scn = NULL;
     58   struct senc2d_enclosure* enclosure;
     59   struct senc2d_enclosure_header header;
     60   int conv;
     61   int conv_front, conv_in;
     62   struct context ctx = CONTEXT_NULL__;
     63   unsigned i, e, ecount;
     64 
     65   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     66   OK(senc2d_device_create(NULL, &allocator, SENC2D_NTHREADS_DEFAULT, 1, &dev));
     67 
     68   /* A 2D square.
     69    * 2 enclosures (inside, outside) sharing the same segments,
     70    * but opposite sides.
     71    * What differs in this test is that segment #0 vertices are given
     72    * in the opposite rotation order. */
     73   ctx.positions = inconsistant_square_vertices;
     74   ctx.indices = inconsistant_box_indices;
     75   ctx.front_media = inconsistant_medium_front;
     76   ctx.back_media = inconsistant_medium_back;
     77 
     78   OK(senc2d_scene_create(dev, convention, inconsistant_box_nsegments,
     79     get_indices, get_media, nvertices, get_position, &ctx, &scn));
     80 
     81   OK(senc2d_scene_get_enclosure_count(scn, &ecount));
     82   CHK(ecount == 2);
     83 
     84   OK(senc2d_scene_get_convention(scn, &conv));
     85   CHK(conv == convention);
     86   conv_front = (conv & SENC2D_CONVENTION_NORMAL_FRONT) != 0;
     87   conv_in = (conv & SENC2D_CONVENTION_NORMAL_INSIDE) != 0;
     88 
     89   FOR_EACH(e, 0, ecount) {
     90     unsigned medium, expected_external_medium, expected_medium;
     91     char name[128];
     92     enum senc2d_side side, expected_side;
     93     unsigned gid;
     94     (void)name;
     95     OK(senc2d_scene_get_enclosure(scn, e, &enclosure));
     96     OK(senc2d_enclosure_get_header(enclosure, &header));
     97     CHK(header.enclosed_media_count == 1);
     98     CHK(header.area == 4);
     99     CHK(header.volume == (header.is_infinite ? -1 : 1));
    100     OK(senc2d_enclosure_get_medium(enclosure, 0, &medium));
    101     /* If NORMAL_FRONT the external enclosure's medium should be 0,
    102      * 1 if NORMAL_BACK */
    103     expected_external_medium = conv_front ? 0 : 1;
    104     expected_medium = (header.is_infinite ?
    105       expected_external_medium : !expected_external_medium);
    106     CHK(medium == expected_medium);
    107 
    108 #ifdef DUMP_ENCLOSURES
    109     sprintf(name, "test_inconsistant_square_%s_%s_%u.obj",
    110       conv_front ? "front" : "back", conv_in ? "in" : "out", e);
    111     dump_enclosure(scn, e, name);
    112 #endif
    113 
    114     FOR_EACH(i, 0, header.primitives_count) {
    115       int same, reversed, fst_reversed;
    116       fst_reversed = ((e == 0) == conv_in);
    117       expected_side = (inconsistant_medium_front[i] == expected_medium)
    118         ? SENC2D_FRONT : SENC2D_BACK;
    119       cmp_seg(i, enclosure,
    120         inconsistant_box_indices + (2 * i), inconsistant_square_vertices,
    121         &same, &reversed);
    122       /* Should be made of the same segments */
    123       CHK(same);
    124       CHK(i ? reversed != fst_reversed : reversed == fst_reversed);
    125       OK(senc2d_enclosure_get_segment_id(enclosure, i, &gid, &side));
    126       CHK(side == expected_side);
    127     }
    128     SENC2D(enclosure_ref_put(enclosure));
    129   }
    130 
    131   SENC2D(scene_ref_put(scn));
    132   SENC2D(device_ref_put(dev));
    133 
    134   check_memory_allocator(&allocator);
    135   mem_shutdown_proxy_allocator(&allocator);
    136   CHK(mem_allocated_size() == 0);
    137 }
    138 
    139 int main(int argc, char** argv)
    140 {
    141   (void)argc, (void)argv;
    142 
    143   test(SENC2D_CONVENTION_NORMAL_FRONT | SENC2D_CONVENTION_NORMAL_INSIDE);
    144   test(SENC2D_CONVENTION_NORMAL_BACK | SENC2D_CONVENTION_NORMAL_INSIDE);
    145   test(SENC2D_CONVENTION_NORMAL_FRONT | SENC2D_CONVENTION_NORMAL_OUTSIDE);
    146   test(SENC2D_CONVENTION_NORMAL_BACK | SENC2D_CONVENTION_NORMAL_OUTSIDE);
    147 
    148   return 0;
    149 }