star-cad

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

test_normals.c (3178B)


      1 /* Copyright (C) 2022 |Meso|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 int
     30 main(int argc, char* argv[])
     31 {
     32   res_T res = RES_OK;
     33   double p1[3] = {0, 0, 0};
     34   double s1[3] = {1, 1, 0};
     35   double c[3] = {0.5, 0.5, 0};
     36   double scale[3] = {0.5, 0.5, 0.5};
     37   double d1[3] = {0, 0, 1};
     38   double d1_[3] = {0, 0, -1};
     39   double m1, m2;
     40   struct scad_geometry* base = NULL;
     41   struct scad_geometry* sbase = NULL;
     42   struct scad_geometry* geom1 = NULL;
     43   struct scad_geometry* geom2 = NULL;
     44   struct scad_geometry* geoms[2];
     45   struct scad_geometry* part[2];
     46   struct mem_allocator allocator;
     47   struct scad_options options = SCAD_DEFAULT_OPTIONS;
     48   struct darray_double trg;
     49 
     50   (void)argc; (void)argv;
     51 
     52   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     53   darray_double_init(&allocator, &trg);
     54   OK(scad_initialize(NULL, &allocator, 3));
     55   OK(scad_set_options(NULL));
     56   OK(scad_set_options(&options));
     57 
     58   OK(scad_add_rectangle("base", p1, s1, &base));
     59   OK(scad_geometry_extrude(base, "top", d1, &geom1));
     60   OK(scad_geometry_get_mass(geom1, &m1));
     61 
     62   OK(scad_geometry_dilate(base, c, scale, "small_base", &sbase));
     63   OK(scad_geometry_extrude(sbase, "bottom", d1_, &geom2));
     64   OK(scad_geometry_get_mass(geom2, &m2));
     65 
     66   geoms[0] = geom1;
     67   geoms[1] = geom2;
     68   OK(scad_synchronize());
     69   OK(scad_geometries_partition(geoms, 2, 0, part));
     70   OK(scad_synchronize());
     71   OK(scad_scene_mesh());
     72   OK(scad_synchronize());
     73 
     74   CHK(eq_eps(m1, 1, 1e-10));
     75   CHK(eq_eps(m2, 0.25, 1e-10));
     76 
     77   OK(scad_stl_export(part[0], "/tmp/geom1", SCAD_FORCE_NORMALS_OUTWARD, 0));
     78   OK(scad_stl_export(part[1], "/tmp/geom2", SCAD_FORCE_NORMALS_OUTWARD, 0));
     79 
     80   OK(scad_stl_get_data(geom2, &trg));
     81   OK(scad_stl_data_write(&trg, "/tmp/test", 0, 0));
     82   OK(scad_stl_data_write(&trg, "/tmp/test", 1, 1));
     83 
     84   OK(scad_stl_export(geom2, "/tmp/test", SCAD_FORCE_NORMALS_OUTWARD, 0));
     85   OK(scad_stl_export(geom2, "/tmp/test", SCAD_FORCE_NORMALS_OUTWARD, 1));
     86   OK(scad_stl_export(geom1, NULL, SCAD_FORCE_NORMALS_OUTWARD, 0));
     87   OK(scad_stl_export(geom1, NULL, SCAD_FORCE_NORMALS_OUTWARD, 1));
     88 
     89   OK(scad_finalize());
     90   darray_double_release(&trg);
     91 
     92   check_memory_allocator(&allocator);
     93   mem_shutdown_proxy_allocator(&allocator);
     94   CHK(mem_allocated_size() == 0);
     95 
     96   return (res == RES_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
     97 }