star-geometry-3d

Clean and decorate 3D geometries
git clone git://git.meso-star.fr/star-geometry-3d.git
Log | Files | Refs | README | LICENSE

test_sg3d_unspecified_properties.c (5764B)


      1 /* Copyright (C) 2019, 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 #include "sg3d.h"
     17 #include "test_sg3d_utils.h"
     18 
     19 #include <stdio.h>
     20 
     21 int
     22 main(int argc, char** argv)
     23 {
     24   struct mem_allocator allocator;
     25   struct sg3d_device* dev;
     26   struct sg3d_geometry* geom;
     27   struct context ctx = CONTEXT_NULL__;
     28   struct sg3d_geometry_add_callbacks callbacks = SG3D_ADD_CALLBACKS_NULL__;
     29   unsigned property[12];
     30   unsigned i;
     31   const unsigned property_count = sizeof(property) / sizeof(*property);
     32   unsigned count;
     33   (void)argc, (void)argv;
     34 
     35   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     36   OK(sg3d_device_create(NULL, &allocator, 1, &dev));
     37   OK(sg3d_geometry_create(dev, &geom));
     38 
     39   FOR_EACH(i, 0, property_count) property[i] = SG3D_UNSPECIFIED_PROPERTY;
     40 
     41   callbacks.get_indices = get_indices;
     42   callbacks.get_position = get_position;
     43 
     44   /* A 3D cube.
     45    * 2 enclosures (inside, outside) sharing the same triangles,
     46    * but opposite sides */
     47   ctx.positions = box_vertices;
     48   ctx.indices = cube_indices;
     49 
     50   /* Add geometry with no properties */
     51   ctx.front_media = property;
     52   ctx.back_media = medium1;
     53   ctx.intface = property;
     54   OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx));
     55   OK(sg3d_geometry_get_unique_triangles_with_unspecified_side_count(geom, &count));
     56   CHK(count == ntriangles);
     57   OK(sg3d_geometry_get_added_triangles_count(geom, &count));
     58   CHK(count == ntriangles);
     59 
     60   /* Add same geometry with no properties on front/intface */
     61   callbacks.get_properties = get_properties;
     62   OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx));
     63   OK(sg3d_geometry_get_unique_triangles_with_unspecified_side_count(geom, &count));
     64   CHK(count == ntriangles);
     65   OK(sg3d_geometry_get_added_triangles_count(geom, &count));
     66   CHK(count == 2 * ntriangles);
     67 
     68   OK(sg3d_geometry_dump_as_c_code(geom, stdout, "front_unspecified",
     69     SG3D_C_DUMP_STATIC | SG3D_C_DUMP_CONST));
     70 
     71   /* Add same geometry, front/intface properties are defined for odd triangles */
     72   FOR_EACH(i, 0, sizeof(property) / sizeof(*property))
     73     property[i] = (i % 2) ? 0 : SG3D_UNSPECIFIED_PROPERTY;
     74   OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx));
     75   OK(sg3d_geometry_get_unique_triangles_with_unspecified_side_count(geom, &count));
     76   CHK(count == ntriangles / 2);
     77   OK(sg3d_geometry_get_unique_vertices_count(geom, &count));
     78   CHK(count == nvertices);
     79   OK(sg3d_geometry_get_added_triangles_count(geom, &count));
     80   CHK(count == 3 * ntriangles);
     81   OK(sg3d_geometry_get_unique_triangles_count(geom, &count));
     82   CHK(count == ntriangles);
     83   FOR_EACH(i, 0, count) {
     84     unsigned prop[SG3D_PROP_TYPES_COUNT__];
     85     OK(sg3d_geometry_get_unique_triangle_properties(geom, i, prop));
     86     CHK(prop[SG3D_FRONT] == ((i % 2) ? 0 : SG3D_UNSPECIFIED_PROPERTY)
     87       && prop[SG3D_BACK] == 1
     88       && prop[SG3D_INTFACE] == ((i % 2) ? 0 : SG3D_UNSPECIFIED_PROPERTY));
     89   }
     90 
     91   /* Same information again, using a reversed box */
     92   ctx.reverse_vrtx = 1;
     93   SWAP(const unsigned*, ctx.front_media, ctx.back_media);
     94   OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx));
     95   OK(sg3d_geometry_get_unique_triangles_with_unspecified_side_count(geom, &count));
     96   CHK(count == ntriangles / 2);
     97   OK(sg3d_geometry_get_added_triangles_count(geom, &count));
     98   CHK(count == 4 * ntriangles);
     99 
    100   OK(sg3d_geometry_dump_as_c_code(geom, stdout, "front_half_unspecified",
    101     SG3D_C_DUMP_STATIC | SG3D_C_DUMP_CONST));
    102 
    103   /* Define properties for remaining triangles, using reversed box */
    104   FOR_EACH(i, 0, sizeof(property) / sizeof(*property))
    105     property[i] = (i % 2) ? SG3D_UNSPECIFIED_PROPERTY : 0;
    106   OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx));
    107   OK(sg3d_geometry_get_unique_triangles_with_unspecified_side_count(geom, &count));
    108   CHK(count == 0);
    109   OK(sg3d_geometry_get_unique_vertices_count(geom, &count));
    110   CHK(count == nvertices);
    111   OK(sg3d_geometry_get_added_triangles_count(geom, &count));
    112   CHK(count == 5 * ntriangles);
    113   OK(sg3d_geometry_get_unique_triangles_count(geom, &count));
    114   CHK(count == ntriangles);
    115   FOR_EACH(i, 0, count) {
    116     unsigned prop[3];
    117     OK(sg3d_geometry_get_unique_triangle_properties(geom, i, prop));
    118     CHK(prop[SG3D_FRONT] == 0 && prop[SG3D_BACK] == 1
    119       && prop[SG3D_INTFACE] == 0);
    120   }
    121 
    122   OK(sg3d_geometry_dump_as_c_code(geom, stdout, "all_defined",
    123     SG3D_C_DUMP_STATIC | SG3D_C_DUMP_CONST));
    124 
    125   /* Define incoherent properties for some triangles */
    126   FOR_EACH(i, 0, sizeof(property) / sizeof(*property))
    127     property[i] = (i % 2);
    128   OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx));
    129   OK(sg3d_geometry_get_unique_triangles_with_merge_conflict_count(geom, &count));
    130   CHK(count == ntriangles / 2);
    131   OK(sg3d_geometry_get_unique_triangles_with_properties_conflict_count(geom, &count));
    132   CHK(count == 0);
    133   OK(sg3d_geometry_get_added_triangles_count(geom, &count));
    134   CHK(count == 6 * ntriangles);
    135 
    136   OK(sg3d_geometry_ref_put(geom));
    137   OK(sg3d_device_ref_put(dev));
    138 
    139   check_memory_allocator(&allocator);
    140   mem_shutdown_proxy_allocator(&allocator);
    141   CHK(mem_allocated_size() == 0);
    142   return 0;
    143 }