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_many_enclosures.c (3920B)


      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 is similar to test_senc3d_some_enclosures, but involves 64*64*64
     17  * cylinders instead of 4*4*4, thus making it impossible to define the geometry
     18  * through static arrays. */
     19 
     20 #define NB_CYL_X 32
     21 #define NB_CYL_Y 32
     22 #define NB_CYL_Z 16
     23 #define NB_CYL (NB_CYL_X * NB_CYL_Y * NB_CYL_Z)
     24 
     25 #include "senc3d.h"
     26 #include "test_senc3d_utils.h"
     27 #include "test_senc3d_utils2.h"
     28 
     29 #include <star/s3dut.h>
     30 #include <rsys/clock_time.h>
     31 #include <rsys/double3.h>
     32 
     33 #include <stdio.h>
     34 #include <limits.h>
     35 
     36 int
     37 main(int argc, char** argv)
     38 {
     39   struct mem_allocator allocator;
     40   struct senc3d_device* dev = NULL;
     41   struct senc3d_scene* scn = NULL;
     42   struct s3dut_mesh* cyl = NULL;
     43   struct s3dut_context ctx = { {NULL,NULL,0,0}, CONTEXT_NULL__ };
     44   unsigned count;
     45   unsigned cyl_trg_count, cyl_vrtx_count, e;
     46   char dump[64];
     47   struct time t0, t1;
     48   (void)argc, (void)argv;
     49 
     50   OK(mem_init_regular_allocator(&allocator));
     51   OK(senc3d_device_create(NULL, &allocator, SENC3D_NTHREADS_DEFAULT, 1, &dev));
     52 
     53   /* A 20 triangles 12 vertices cylinder template */
     54   S3DUT(create_cylinder(&allocator, 1, 1, 3, 1, &cyl));
     55   S3DUT(mesh_get_data(cyl, &ctx.data));
     56   ASSERT(ctx.data.nprimitives < UINT_MAX);
     57   ASSERT(ctx.data.nvertices < UINT_MAX);
     58   cyl_trg_count = (unsigned)ctx.data.nprimitives;
     59   cyl_vrtx_count = (unsigned)ctx.data.nvertices;
     60 
     61   /* Create the scene with N_CYL cylinders.
     62    * There are NB_CYL_X * NB_CYL_Y imbrications of NB_CYL_Z cylinders each.
     63    * Each imbrication is located on a grid.
     64    * The get_s3du_xxx getters have to retrieve the cylinder from the
     65    * primitive and vertice indexes. */
     66   time_current(&t0);
     67   OK(senc3d_scene_create(dev,
     68     SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE,
     69     NB_CYL * cyl_trg_count, get_s3dut_indices, get_s3dut_media,
     70     NB_CYL * cyl_vrtx_count, get_s3dut_position, &ctx, &scn));
     71   time_sub(&t0, time_current(&t1), &t0);
     72   time_dump(&t0, TIME_MSEC | TIME_SEC | TIME_MIN, NULL, dump, sizeof(dump));
     73   printf("Scene created in: %s\n", dump);
     74   S3DUT(mesh_ref_put(cyl));
     75 
     76   OK(senc3d_scene_get_vertices_count(scn, &count));
     77   CHK(count == NB_CYL * cyl_vrtx_count);
     78   OK(senc3d_scene_get_triangles_count(scn, &count));
     79   CHK(count == NB_CYL * cyl_trg_count);
     80 
     81   OK(senc3d_scene_get_enclosure_count(scn, &count));
     82   CHK(count == 1 + NB_CYL);
     83   FOR_EACH(e, 0, count) {
     84     struct senc3d_enclosure* enclosure;
     85     struct senc3d_enclosure_header header;
     86     unsigned m;
     87     OK(senc3d_scene_get_enclosure(scn, e, &enclosure));
     88     OK(senc3d_enclosure_get_header(enclosure, &header));
     89     CHK(header.enclosed_media_count == 1);
     90     OK(senc3d_enclosure_get_medium(enclosure, 0, &m));
     91     CHK(header.primitives_count ==
     92       (header.is_infinite /* Outermost enclosure: NB_CYL_X*NB_CYL_Y cylinders */
     93         ? NB_CYL_X * NB_CYL_Y * cyl_trg_count
     94         : (m == 0
     95           ? cyl_trg_count /* Innermost enclosures: 1 cylinder */
     96           : 2 * cyl_trg_count))); /* Other enclosures: 2 cylinders */
     97     OK(senc3d_enclosure_ref_put(enclosure));
     98   }
     99 
    100   OK(senc3d_scene_ref_put(scn));
    101   OK(senc3d_device_ref_put(dev));
    102 
    103   check_memory_allocator(&allocator);
    104   mem_shutdown_regular_allocator(&allocator);
    105   CHK(mem_allocated_size() == 0);
    106   return 0;
    107 }