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_some_enclosures.c (3742B)


      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 #include "test_sg3d_utils2.h"
     19 
     20 #include <rsys/double3.h>
     21 
     22 #include <star/s3dut.h>
     23 
     24 #include <stdio.h>
     25 #include <limits.h>
     26 
     27 #define NB_CYL_X 4
     28 #define NB_CYL_Y 4
     29 #define NB_CYL_Z 4
     30 #define NB_CYL (NB_CYL_X * NB_CYL_Y * NB_CYL_Z)
     31 
     32 int
     33 main(int argc, char** argv)
     34 {
     35   struct mem_allocator allocator;
     36   struct sg3d_device* dev;
     37   struct sg3d_geometry* geom;
     38   struct sg3d_geometry_add_callbacks callbacks = SG3D_ADD_CALLBACKS_NULL__;
     39   unsigned cyl_trg_count, cyl_vrtx_count, count;
     40   int i, j, k;
     41   unsigned m_in, m_out, itf = 0;
     42   struct s3dut_context ctx = { {NULL,NULL,0,0}, CONTEXT_NULL__ };
     43   struct s3dut_mesh* cyl = NULL;
     44   (void)argc, (void)argv;
     45 
     46   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     47   OK(sg3d_device_create(NULL, &allocator, 1, &dev));
     48   OK(sg3d_geometry_create(dev, &geom));
     49   SG3D(device_ref_put(dev));
     50 
     51   callbacks.get_indices = get_s3dut_indices;
     52   callbacks.get_properties = get_s3dut_properties;
     53   callbacks.get_position = get_s3dut_position;
     54 
     55   ctx.ctx.positions = box_vertices;
     56   ctx.ctx.indices = cube_indices;
     57   ctx.ctx.front_media = &m_in;
     58   ctx.ctx.back_media = &m_out;
     59   ctx.ctx.intface = &itf;
     60 
     61   /* A 20 triangles 12 vertices cylinder template */
     62   S3DUT(create_cylinder(&allocator, 1, 1, 5, 1, &cyl));
     63   S3DUT(mesh_get_data(cyl, &ctx.data));
     64   ASSERT(ctx.data.nprimitives <= UINT_MAX);
     65   ASSERT(ctx.data.nvertices <= UINT_MAX);
     66   cyl_trg_count = (unsigned)ctx.data.nprimitives;
     67   cyl_vrtx_count = (unsigned)ctx.data.nvertices;
     68   OK(sg3d_geometry_reserve(geom, NB_CYL * cyl_vrtx_count, NB_CYL * cyl_trg_count, 0));
     69   FOR_EACH(i, 0, NB_CYL_X) {
     70     double center_x = 2 * (1 + NB_CYL_Z) * (i - NB_CYL_X / 2);
     71     FOR_EACH(j, 0, NB_CYL_Y) {
     72       double misalignment = 0;
     73       FOR_EACH(k, 0, NB_CYL_Z) {
     74         double center_y = 2 * (1 + NB_CYL_Z) * (j - NB_CYL_Y / 2);
     75         m_in = (unsigned)k;
     76         m_out = (unsigned)(k + 1);
     77         d3_splat(ctx.ctx.scale, k + 1);
     78 #ifdef MITIGATE_EMBREE_181
     79         /* Mitigate Embree issue #181
     80          * We cannot keep perfect alignment of cylinders
     81          * or some hits are missed in some raytracing tasks */
     82         misalignment = (k % 2) ? -0.01 : +0.01;
     83 #endif
     84         d3(ctx.ctx.offset, center_x + misalignment, center_y + misalignment, 0);
     85         OK(sg3d_geometry_add(geom, cyl_vrtx_count, cyl_trg_count, &callbacks, &ctx));
     86       }
     87     }
     88   }
     89   S3DUT(mesh_ref_put(cyl));
     90 
     91   OK(sg3d_geometry_get_unique_triangles_with_merge_conflict_count(geom, &count));
     92   CHK(count == 0);
     93   OK(sg3d_geometry_get_unique_triangles_with_unspecified_interface_count(geom, &count));
     94   CHK(count == 0);
     95   OK(sg3d_geometry_get_unique_triangles_with_unspecified_side_count(geom, &count));
     96   CHK(count == 0);
     97   OK(sg3d_geometry_dump_as_c_code(geom, stdout, "some_enclosures",
     98     SG3D_C_DUMP_CONST | SG3D_C_DUMP_STATIC));
     99 
    100   SG3D(geometry_ref_put(geom));
    101 
    102   check_memory_allocator(&allocator);
    103   mem_shutdown_proxy_allocator(&allocator);
    104   CHK(mem_allocated_size() == 0);
    105   return 0;
    106 }