star-cad

Geometric operators for computer-aided design
git clone git://git.meso-star.fr/star-cad.git
Log | Files | Refs | README | LICENSE

test_lifetime.c (5062B)


      1 /* Copyright (C) 2022-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 "scad.h"
     17 #include "scad_geometry.h"
     18 #include "test_common.h"
     19 
     20 #include <rsys/rsys.h>
     21 #include <rsys/str.h>
     22 #include <rsys/math.h>
     23 #include <rsys/mem_allocator.h>
     24 #include <rsys/dynamic_array_double.h>
     25 
     26 #include <stdlib.h>
     27 #include <stdio.h>
     28 
     29 static void
     30 alive_and_well
     31   (struct scad_geometry* g,
     32    struct mem_allocator* allocator)
     33 {
     34   struct scad_geometry** geom_array = NULL;
     35   size_t i, c;
     36 
     37   ASSERT(g && allocator);
     38 
     39   OK(scad_geometry_explode(g, &geom_array, &c));
     40   OK(scad_geometries_set_name(geom_array, c, "alive_and_well", 0));
     41   OK(scad_geometry_ref_put(g));
     42   OK(scad_synchronize());
     43   for(i = 0; i < c; i++) {
     44     double dir1[3] = {0, 0, 1};
     45     struct scad_geometry* gg;
     46     OK(scad_geometry_extrude(geom_array[i], dir1, &gg));
     47     OK(scad_geometry_ref_put(geom_array[i]));
     48     OK(scad_geometry_ref_put(gg));
     49   }
     50   MEM_RM(allocator, geom_array);
     51 }
     52 
     53 int
     54 main(int argc, char* argv[])
     55 {
     56   res_T res = RES_OK;
     57   double p1[3] = {0, 0, 0};
     58   double diago[] = {1, 1, 1}, diago_[] = {.5, .5, -1}, base[] = {1, 1, 0};
     59   double dir1[3] = {0, 0, 1};
     60   struct scad_geometry* geom1 = NULL;
     61   struct scad_geometry* geom = NULL;
     62   struct scad_geometry* geoms[2];
     63   struct scad_geometry* out_geoms[2];
     64   struct mem_allocator allocator;
     65   struct scad_geometry** list = NULL;
     66   struct scad_geometry** list2 = NULL;
     67   size_t list_n, list_n2, center_n, i;
     68   double center[3], N[3];
     69   struct scad_options options = SCAD_DEFAULT_OPTIONS;
     70 
     71   (void)argc; (void)argv;
     72 
     73   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     74 
     75   OK(scad_initialize(NULL, &allocator, 3));
     76   options.Misc.LogRefCounting = SCAD_LOG_DIMTAGS_ALL | SCAD_LOG_GEOMETRY;
     77   OK(scad_set_options(&options));
     78 
     79   /* Check that 2D constituants of a deleted 3D object are alive and well */
     80   OK(scad_add_box(p1, diago, &geom));
     81   OK(scad_geometries_boundaries(&geom, 1, &list, &list_n));
     82   OK(scad_geometries_set_name(list, list_n, "boundary", 0));
     83   OK(scad_geometry_ref_put(geom));
     84   for(i = 0; i < list_n; i++) {
     85     alive_and_well(list[i], &allocator);
     86   }
     87   MEM_RM(&allocator, list); list = NULL;
     88 
     89   /* Check that a 3D derivative of a deleted 2D object is alive and well */
     90   OK(scad_add_rectangle(p1, base, &geom));
     91   OK(scad_geometry_extrude(geom, dir1, &geom1));
     92   OK(scad_geometry_ref_put(geom));
     93   OK(scad_geometries_boundaries(&geom1, 1, &list, &list_n));
     94   OK(scad_geometries_set_name(list, list_n, "boundary", 0));
     95   OK(scad_geometry_ref_put(geom1));
     96   for(i = 0; i < list_n; i++) {
     97     alive_and_well(list[i], &allocator);
     98   }
     99   MEM_RM(&allocator, list); list = NULL;
    100 
    101   /* Check that a 2D part of a deleted 3D object is alive and well */
    102   OK(scad_add_rectangle(p1, base, &geom));
    103   OK(scad_geometry_extrude(geom, dir1, &geom1));
    104   OK(scad_geometry_ref_put(geom1));
    105   alive_and_well(geom, &allocator);
    106 
    107   OK(scad_add_box(p1, diago, &geom1));
    108   OK(scad_geometries_boundaries(&geom1, 1, &list, &list_n));
    109   OK(scad_geometries_set_name(list, list_n, "bcavity", 0));
    110   for(i = 0; i < list_n; i++) {
    111     OK(scad_geometry_get_count(list[i], &center_n));
    112     ASSERT(center_n == 1);
    113     OK(scad_geometry_get_centerofmass(list[i], center));
    114     OK(scad_geometry_get_normal(list[i], center, N, &geom));
    115     OK(scad_geometry_ref_put(geom));
    116   }
    117   for(i = 0; i < list_n; i++) {
    118     OK(scad_geometry_ref_put(list[i]));
    119   }
    120   MEM_RM(&allocator, list); list = NULL;
    121   OK(scad_geometry_ref_put(geom1));
    122 
    123   /* Check that 2D constituants of a deleted 3D object are alive and well after
    124    * a partition */
    125   OK(scad_add_box(p1, diago, geoms+0));
    126   OK(scad_add_box(p1, diago_, geoms+1));
    127   OK(scad_geometries_boundaries(geoms+0, 1, &list, &list_n));
    128   OK(scad_geometries_boundaries(geoms+1, 1, &list2, &list_n2));
    129   OK(scad_geometries_partition(geoms, 2, 0, out_geoms));
    130   OK(scad_geometry_ref_put(geoms[0]));
    131   OK(scad_geometry_ref_put(geoms[1]));
    132   for(i = 0; i < list_n; i++) {
    133     alive_and_well(list[i], &allocator);
    134   }
    135   for(i = 0; i < list_n2; i++) {
    136     alive_and_well(list2[i], &allocator);
    137   }
    138   OK(scad_geometry_ref_put(out_geoms[0]));
    139   OK(scad_geometry_ref_put(out_geoms[1]));
    140   MEM_RM(&allocator, list); list = NULL;
    141   MEM_RM(&allocator, list2); list = NULL;
    142 
    143   OK(scad_finalize());
    144 
    145   check_memory_allocator(&allocator);
    146   mem_shutdown_proxy_allocator(&allocator);
    147   CHK(mem_allocated_size() == 0);
    148 
    149   return (res == RES_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
    150 }