star-cad

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

test_periodic.c (3485B)


      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 
     18 #include "test_common.h"
     19 
     20 #include <rsys/rsys.h>
     21 #include <rsys/math.h>
     22 #include <rsys/double3.h>
     23 #include <rsys/mem_allocator.h>
     24 
     25 #define XR 1.5
     26 #define L 1
     27 
     28 int
     29 main(int argc, char* argv[])
     30 {
     31   struct mem_allocator allocator;
     32   struct scad_geometry *cyl1 = NULL, *cyl2 = NULL, *cyl = NULL;
     33   struct scad_geometry** faces = NULL;
     34   struct scad_geometry *internal = NULL, *external = NULL, *lat[2] = { NULL, NULL};
     35   double p1[3] = { 0,0,0 }, p2[3], d2[3] = { L,0,0};
     36   double r1 = 1, r2 = r1 * XR, len;
     37   double cyl_affine[16] = { 1, 0, 0, 0,  0, XR, 0, 0,  0, 0, XR, 0,  0, 0, 0, 1 };
     38   size_t i, facesn;
     39   struct scad_options options = SCAD_DEFAULT_OPTIONS;
     40 
     41   (void)argc; (void)argv;
     42 
     43   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     44 
     45   OK(scad_initialize(NULL, &allocator, 2));
     46   options.Misc.LogRefCounting = SCAD_LOG_DIMTAGS_ALL | SCAD_LOG_GEOMETRY;
     47   OK(scad_set_options(&options));
     48 
     49   r2 = r1 * XR;
     50   OK(scad_add_cylinder(p1, d2, r1, 2*PI, &cyl1));
     51   OK(scad_add_cylinder(p1, d2, r2, 2*PI, &cyl2));
     52   OK(scad_geometries_cut(&cyl2, 1, &cyl1, 1, &cyl));
     53   OK(scad_geometry_ref_put(cyl1));
     54   OK(scad_geometry_ref_put(cyl2));
     55   OK(scad_geometries_boundaries(&cyl, 1, &faces, &facesn));
     56   ASSERT(facesn == 4);
     57   d3_add(p2, p1, d2);
     58   len = d3_len(d2);
     59   for(i = 0; i < facesn; i++) {
     60     struct scad_geometry* f = faces[i];
     61     double center[3], m;
     62     OK(scad_geometry_get_centerofmass(f, center));
     63     if(fabs(center[0] - p1[0]) < FLT_EPSILON) {
     64       ASSERT(lat[0] == NULL);
     65       lat[0] = f;
     66       OK(scad_geometry_set_name(f, "left_side"));
     67       continue;
     68     }
     69     if(fabs(center[0] - p2[0]) < FLT_EPSILON) {
     70       ASSERT(lat[1] == NULL);
     71       lat[1] = f;
     72       OK(scad_geometry_set_name(f, "right_side"));
     73       continue;
     74     }
     75     scad_geometry_get_mass(f, &m);
     76     if(fabs(m - len*2*PI*r1) < FLT_EPSILON) {
     77       ASSERT(internal == NULL);
     78       internal = f;
     79       OK(scad_geometry_set_name(f, "internal"));
     80       continue;
     81     }
     82     if(fabs(m - len*2*PI*r2) < FLT_EPSILON) {
     83       ASSERT(external == NULL);
     84       external = f;
     85       OK(scad_geometry_set_name(f, "external"));
     86       continue;
     87     }
     88   }
     89   ASSERT(lat[0] && lat[1] && internal && external);
     90   OK(scad_geometries_set_periodic(&internal, 1, &external, 1, cyl_affine));
     91   OK(scad_geometries_set_mesh_algorithm(lat, 1, SCAD_INITIAL_MESH_ONLY));
     92   for(i = 0; i < facesn; i++) {
     93     OK(scad_geometry_ref_put(faces[i]));
     94   }
     95   MEM_RM(&allocator, faces);
     96   OK(scad_scene_mesh());
     97   OK(scad_stl_export(cyl, "periodic", SCAD_FORCE_NORMALS_OUTWARD, 1));
     98   OK(scad_geometry_ref_put(cyl));
     99   OK(scad_finalize());
    100 #undef XR
    101 
    102   check_memory_allocator(&allocator);
    103   mem_shutdown_proxy_allocator(&allocator);
    104   CHK(mem_allocated_size() == 0);
    105 
    106   return EXIT_SUCCESS;
    107 }