star-enclosures-3d

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

test_senc3d_cube_behind_cube.c (7652B)


      1 /* Copyright (C) 2018-2020, 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 sg3_geometry_dump_as_C_code feature
     17  * of star-geometry. It uses output from test_sg3_cube_behind_cube. */
     18 
     19 #include "senc3d.h"
     20 #include "test_senc3d_utils.h"
     21 
     22 #include <rsys/double3.h>
     23 
     24 /* Dump of star-geometry 'cube_behind_cube_2'. */
     25 static const unsigned cube_behind_cube_2_vertices_count = 16;
     26 static const double cube_behind_cube_2_vertices[48] =
     27 {
     28  0.1, 0, 0,
     29  1, 0, 0,
     30  0, 1, 0,
     31  1, 1, 0,
     32  0, 0, 1.1,
     33  1, 0, 1,
     34  0, 1, 1,
     35  1, 1.1, 1,
     36  -1.5, -2, 20,
     37  3, -2, 20,
     38  -2, 3, 20,
     39  3, 3, 20,
     40  -2, -2, 25.5,
     41  3, -2, 25,
     42  -2, 3, 25,
     43  3, 3.5, 25
     44 };
     45 static const unsigned cube_behind_cube_2_triangles_count = 24;
     46 static const unsigned cube_behind_cube_2_triangles[72] =
     47 {
     48  0, 2, 1,
     49  1, 2, 3,
     50  0, 4, 2,
     51  2, 4, 6,
     52  4, 5, 6,
     53  6, 5, 7,
     54  3, 7, 1,
     55  1, 7, 5,
     56  2, 6, 3,
     57  3, 6, 7,
     58  0, 1, 4,
     59  4, 1, 5,
     60  8, 10, 9,
     61  9, 10, 11,
     62  8, 12, 10,
     63  10, 12, 14,
     64  12, 13, 14,
     65  14, 13, 15,
     66  11, 15, 9,
     67  9, 15, 13,
     68  10, 14, 11,
     69  11, 14, 15,
     70  8, 9, 12,
     71  12, 9, 13
     72 };
     73 static const unsigned cube_behind_cube_2_properties[72] =
     74 {
     75  0, 1, 0,
     76  0, 1, 0,
     77  0, 1, 0,
     78  0, 1, 0,
     79  0, 1, 0,
     80  0, 1, 0,
     81  0, 1, 0,
     82  0, 1, 0,
     83  0, 1, 0,
     84  0, 1, 0,
     85  0, 1, 0,
     86  0, 1, 0,
     87  0, 1, 0,
     88  0, 1, 0,
     89  0, 1, 0,
     90  0, 1, 0,
     91  0, 1, 0,
     92  0, 1, 0,
     93  0, 1, 0,
     94  0, 1, 0,
     95  0, 1, 0,
     96  0, 1, 0,
     97  0, 1, 0,
     98  0, 1, 0
     99 };
    100 /* Dump of star-geometry 'cube_behind_cube_3'. */
    101 static const unsigned cube_behind_cube_3_vertices_count = 24;
    102 static const double cube_behind_cube_3_vertices[72] =
    103 {
    104  0.1, 0, 0,
    105  1, 0, 0,
    106  0, 1, 0,
    107  1, 1, 0,
    108  0, 0, 1.1,
    109  1, 0, 1,
    110  0, 1, 1,
    111  1, 1.1, 1,
    112  -1.5, -2, 20,
    113  3, -2, 20,
    114  -2, 3, 20,
    115  3, 3, 20,
    116  -2, -2, 25.5,
    117  3, -2, 25,
    118  -2, 3, 25,
    119  3, 3.5, 25,
    120  -2.3, -3, 30,
    121  4, -3, 30,
    122  -3, 4, 30,
    123  4, 4, 30,
    124  -3, -3, 37.7,
    125  4, -3, 37,
    126  -3, 4, 37,
    127  4, 4.7, 37
    128 };
    129 static const unsigned cube_behind_cube_3_triangles_count = 36;
    130 static const unsigned cube_behind_cube_3_triangles[108] =
    131 {
    132  0, 2, 1,
    133  1, 2, 3,
    134  0, 4, 2,
    135  2, 4, 6,
    136  4, 5, 6,
    137  6, 5, 7,
    138  3, 7, 1,
    139  1, 7, 5,
    140  2, 6, 3,
    141  3, 6, 7,
    142  0, 1, 4,
    143  4, 1, 5,
    144  8, 10, 9,
    145  9, 10, 11,
    146  8, 12, 10,
    147  10, 12, 14,
    148  12, 13, 14,
    149  14, 13, 15,
    150  11, 15, 9,
    151  9, 15, 13,
    152  10, 14, 11,
    153  11, 14, 15,
    154  8, 9, 12,
    155  12, 9, 13,
    156  16, 18, 17,
    157  17, 18, 19,
    158  16, 20, 18,
    159  18, 20, 22,
    160  20, 21, 22,
    161  22, 21, 23,
    162  19, 23, 17,
    163  17, 23, 21,
    164  18, 22, 19,
    165  19, 22, 23,
    166  16, 17, 20,
    167  20, 17, 21
    168 };
    169 static const unsigned cube_behind_cube_3_properties[108] =
    170 {
    171  0, 1, 0,
    172  0, 1, 0,
    173  0, 1, 0,
    174  0, 1, 0,
    175  0, 1, 0,
    176  0, 1, 0,
    177  0, 1, 0,
    178  0, 1, 0,
    179  0, 1, 0,
    180  0, 1, 0,
    181  0, 1, 0,
    182  0, 1, 0,
    183  0, 1, 0,
    184  0, 1, 0,
    185  0, 1, 0,
    186  0, 1, 0,
    187  0, 1, 0,
    188  0, 1, 0,
    189  0, 1, 0,
    190  0, 1, 0,
    191  0, 1, 0,
    192  0, 1, 0,
    193  0, 1, 0,
    194  0, 1, 0,
    195  1, 0, 0,
    196  1, 0, 0,
    197  1, 0, 0,
    198  1, 0, 0,
    199  1, 0, 0,
    200  1, 0, 0,
    201  1, 0, 0,
    202  1, 0, 0,
    203  1, 0, 0,
    204  1, 0, 0,
    205  1, 0, 0,
    206  1, 0, 0
    207 };
    208 
    209 /*
    210               cube_2                           cube_3
    211 
    212                                        +-----------------------+
    213                                        |                       3
    214                                        |                       |
    215                                     m1 | m0                    |
    216                                        |                       |
    217                                        |                       |
    218                                        |                       |
    219                                        |                       |--> N
    220                                        |                       |
    221                                        |                       |
    222                                        +-----------------------+
    223            +------------+                   +------------+
    224            |            2                   |            2
    225         m0 | m1         |                m0 | m1         |
    226            |            |                   |            |
    227            |            |--> N              |            |--> N
    228            |            |                   |            |
    229            +------------+                   +------------+
    230               +-----+                          +-----+
    231            m0 | m1  1                       m0 | m1  1
    232               |     |--> N                     |     |--> N
    233               +-----+                          +-----+
    234  */
    235 
    236 int
    237 main(int argc, char** argv)
    238 {
    239   struct mem_allocator allocator;
    240   struct senc3d_device* dev = NULL;
    241   struct senc3d_scene* scn = NULL;
    242   struct context ctx = CONTEXT_NULL__;
    243   unsigned i, ecount, maxm;
    244   (void)argc, (void)argv;
    245 
    246   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
    247   OK(senc3d_device_create(NULL, &allocator, SENC3D_NTHREADS_DEFAULT, 1, &dev));
    248 
    249   /* Create a scene with the first and second cubes.
    250    * Both cubes have medium 1 inside and medium 0 outside,
    251    * the second cube is +Z from the first cube and is big enough
    252    * to prevent rays from the first cube to miss it. */
    253   ctx.positions = cube_behind_cube_2_vertices;
    254   ctx.indices = cube_behind_cube_2_triangles;
    255   ctx.properties = cube_behind_cube_2_properties;
    256   OK(senc3d_scene_create(dev,
    257     SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE,
    258     cube_behind_cube_2_triangles_count, get_indices, get_media_from_properties,
    259     cube_behind_cube_2_vertices_count, get_position, &ctx, &scn));
    260 
    261   OK(senc3d_scene_get_enclosure_count(scn, &ecount));
    262   CHK(ecount == 3);
    263 
    264   FOR_EACH(i, 0, ecount) {
    265     struct senc3d_enclosure* enclosure;
    266     struct senc3d_enclosure_header header;
    267     OK(senc3d_scene_get_enclosure(scn, i, &enclosure));
    268     OK(senc3d_enclosure_get_header(enclosure, &header));
    269     CHK(header.enclosed_media_count == 1);
    270     OK(senc3d_enclosure_ref_put(enclosure));
    271   }
    272 
    273   OK(senc3d_scene_get_max_medium(scn, &maxm));
    274   CHK(maxm == 1);
    275   OK(senc3d_scene_ref_put(scn));
    276 
    277   /* Create a scene with the 3 cubes, same 2 first cubes as above
    278    * The third cube has medium 0 inside and medium 1 outside and is further
    279    * in +Z and bigger */
    280   ctx.positions = cube_behind_cube_3_vertices;
    281   ctx.indices = cube_behind_cube_3_triangles;
    282   ctx.properties = cube_behind_cube_3_properties;
    283   OK(senc3d_scene_create(dev,
    284     SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE,
    285     cube_behind_cube_3_triangles_count, get_indices, get_media_from_properties,
    286     cube_behind_cube_3_vertices_count, get_position, &ctx, &scn));
    287 
    288   OK(senc3d_scene_get_enclosure_count(scn, &ecount));
    289   CHK(ecount == 4);
    290 
    291   FOR_EACH(i, 0, ecount) {
    292     struct senc3d_enclosure* enclosure;
    293     struct senc3d_enclosure_header header;
    294     OK(senc3d_scene_get_enclosure(scn, i, &enclosure));
    295     OK(senc3d_enclosure_get_header(enclosure, &header));
    296     /* Inside enclosures contain 1 single media */
    297     CHK(header.enclosed_media_count == (header.is_infinite ? 2u : 1u));
    298     OK(senc3d_enclosure_ref_put(enclosure));
    299   }
    300 
    301   OK(senc3d_scene_get_max_medium(scn, &maxm));
    302   CHK(maxm == 1);
    303 
    304   OK(senc3d_scene_ref_put(scn));
    305   OK(senc3d_device_ref_put(dev));
    306 
    307   check_memory_allocator(&allocator);
    308   mem_shutdown_proxy_allocator(&allocator);
    309   CHK(mem_allocated_size() == 0);
    310   return 0;
    311 }