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_invalid_scenes.c (4992B)


      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_on_cube. */
     18 
     19 #define _POSIX_C_SOURCE 200112L /* snprintf */
     20 
     21 #include "senc3d.h"
     22 #include "test_senc3d_utils.h"
     23 
     24 #include <rsys/double3.h>
     25 
     26 #include <stdio.h>
     27 
     28 /*
     29   2 overlapping cubes with faces splitted on opposite diagonals
     30   (cannot merge triangles)
     31  */
     32 
     33 /* Dump of star-geometry-3d 'invalid_1'. */
     34 static const unsigned invalid_vertices_count = 8;
     35 static const double invalid_vertices[24] =
     36 {
     37    0, 0, 0,
     38    1, 0, 0,
     39    0, 1, 0,
     40    1, 1, 0,
     41    0, 0, 1,
     42    1, 0, 1,
     43    0, 1, 1,
     44    1, 1, 1
     45 };
     46 static const unsigned invalid_triangles_count = 24;
     47 static const unsigned invalid_triangles[72] =
     48 {
     49    0, 2, 1,
     50    1, 2, 3,
     51    0, 4, 2,
     52    2, 4, 6,
     53    4, 5, 6,
     54    6, 5, 7,
     55    3, 7, 1,
     56    1, 7, 5,
     57    2, 6, 3,
     58    3, 6, 7,
     59    0, 1, 4,
     60    4, 1, 5,
     61    0, 3, 1,
     62    0, 2, 3,
     63    0, 6, 2,
     64    0, 4, 6,
     65    4, 5, 7,
     66    6, 4, 7,
     67    3, 7, 5,
     68    1, 3, 5,
     69    2, 6, 7,
     70    3, 2, 7,
     71    0, 1, 5,
     72    4, 0, 5
     73 };
     74 static const unsigned invalid_properties[72] =
     75 {
     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    0, 1, 0
    100 };
    101 
    102 static const unsigned degenerated1_triangles_count = 1;
    103 static const unsigned degenerated1_vertices_count = 3;
    104 static const unsigned degenerated1[3] = { 0, 0, 0 };
    105 
    106 static const unsigned degenerated2_triangles_count = 1;
    107 static const unsigned degenerated2_vertices_count = 3;
    108 static const unsigned degenerated2[3] = { 0, 1, 2 };
    109 
    110 static const double degenerated_vertices[9]
    111 = { 0, 0, 0,  1, 0, 0,  2, 0, 0 };
    112 static const unsigned degenerated_properties[9]
    113 = { 0, 0, 0,  1, 0, 0,  2, 0, 0 };
    114 
    115 int
    116 main(int argc, char** argv)
    117 {
    118   struct mem_allocator allocator;
    119   struct senc3d_device* dev = NULL;
    120   struct senc3d_scene* scn = NULL;
    121   struct context ctx = CONTEXT_NULL__;
    122   unsigned count, tcount, t, e;
    123   struct senc3d_enclosure* enc;
    124   (void)argc, (void)argv;
    125 
    126   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
    127   OK(senc3d_device_create(NULL, &allocator, SENC3D_NTHREADS_DEFAULT, 1, &dev));
    128 
    129   /* Degenerated triangle: duplicated vertex */
    130   ctx.positions = degenerated_vertices;
    131   ctx.indices = degenerated1;
    132   ctx.properties = degenerated_properties;
    133   BA(senc3d_scene_create(dev,
    134     SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE,
    135     degenerated1_triangles_count, get_indices, get_media_from_properties,
    136     degenerated1_vertices_count, get_position, &ctx, &scn));
    137 
    138   /* Degenerated triangles: flat triangle */
    139   ctx.positions = degenerated_vertices;
    140   ctx.indices = degenerated2;
    141   ctx.properties = degenerated_properties;
    142   BA(senc3d_scene_create(dev,
    143     SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE,
    144     degenerated2_triangles_count, get_indices, get_media_from_properties,
    145     degenerated2_vertices_count, get_position, &ctx, &scn));
    146 
    147   /* Degenerated scene: overlapping triangles */
    148   ctx.positions = invalid_vertices;
    149   ctx.indices = invalid_triangles;
    150   ctx.properties = invalid_properties;
    151   OK(senc3d_scene_create(dev,
    152     SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE,
    153     invalid_triangles_count, get_indices, get_media_from_properties,
    154     invalid_vertices_count, get_position, &ctx, &scn));
    155 
    156   OK(senc3d_scene_get_triangles_count(scn, &tcount));
    157   FOR_EACH(t, 0, tcount) {
    158     unsigned ids[2];
    159     BO(senc3d_scene_get_triangle_enclosures(scn, t, ids));
    160   }
    161   BO(senc3d_scene_get_enclosure_count(scn, &count));
    162   BO(senc3d_scene_get_enclosure(scn, 0, &enc));
    163 
    164   OK(senc3d_scene_get_overlapping_triangles_count(scn, &count));
    165   FOR_EACH(e, 0, count) {
    166     OK(senc3d_scene_get_overlapping_triangle(scn, e, &t));
    167     ASSERT(t < tcount);
    168   }
    169   CHK(count == invalid_triangles_count);
    170   OK(senc3d_scene_get_overlapping_triangle(scn, 0, &t));
    171   BA(senc3d_scene_get_overlapping_triangle(scn, invalid_triangles_count, &t));
    172 
    173   OK(senc3d_scene_ref_put(scn));
    174   OK(senc3d_device_ref_put(dev));
    175 
    176   check_memory_allocator(&allocator);
    177   mem_shutdown_proxy_allocator(&allocator);
    178   CHK(mem_allocated_size() == 0);
    179   return 0;
    180 }