star-cad

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

test_export2.c (3715B)


      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/math.h>
     22 #include <rsys/dynamic_array_double.h>
     23 #include <rsys/mem_allocator.h>
     24 
     25 #include <stdlib.h>
     26 
     27 /*
     28  * +-------------------+
     29  * |                   |
     30  * |  +-------------+  |
     31  * |  |             |  |
     32  * |  |  +-------+  |  |
     33  * |  |  |       |  |  |
     34  * |  |  |       |  |  |
     35  * |  |  |       |  |  |
     36  * |  |  +-------+  |  |
     37  * |  |             |  |
     38  * |  +-------------+  |
     39  * |                   |
     40  * +-------------------+
     41  */
     42 
     43 int
     44 main(int argc, char* argv[])
     45 {
     46   res_T res = RES_OK;
     47   double p1[3] = {0, 0, 0};
     48   double p2[3] = {2, 2, 2};
     49   double p3[3] = {4, 4, 4};
     50   double d1[3] = {10, 10, 10};
     51   double d2[3] = {6, 6, 6};
     52   double d3[3] = {2, 2, 2};
     53   struct darray_double array;
     54   struct scad_geometry* cube1 = NULL;
     55   struct scad_geometry* cube2 = NULL;
     56   struct scad_geometry* cube3 = NULL;
     57   struct mem_allocator allocator;
     58   struct scad_options options = SCAD_DEFAULT_OPTIONS;
     59 
     60   (void)argc; (void)argv;
     61 
     62   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     63   darray_double_init(&allocator, &array);
     64   OK(scad_initialize(NULL, &allocator, 3));
     65   options.Misc.LogRefCounting = SCAD_LOG_DIMTAGS_ALL | SCAD_LOG_GEOMETRY;
     66   OK(scad_set_options(&options));
     67 
     68   OK(scad_add_box(p1, d1, &cube1));
     69   OK(scad_geometry_set_name(cube1, "cube1"));
     70   OK(scad_add_box(p2, d2, &cube2));
     71   OK(scad_geometry_set_name(cube2, "cube2"));
     72   OK(scad_add_box(p3, d3, &cube3));
     73   OK(scad_geometry_set_name(cube3, "cube3"));
     74 
     75   OK(scad_scene_mesh());
     76 
     77   /* Check that all three cubes can be exported whith forced normals */
     78   OK(scad_stl_export(cube1, NULL, SCAD_FORCE_NORMALS_OUTWARD, 0));
     79   OK(scad_stl_export(cube2, NULL, SCAD_FORCE_NORMALS_OUTWARD, 0));
     80   OK(scad_stl_export(cube3, "bin_cube3", SCAD_FORCE_NORMALS_OUTWARD, 1));
     81 
     82   /* Check that 2 cubes as a single model can be exported whith forced normals */
     83   OK(scad_stl_get_data(cube1, &array));
     84   OK(scad_stl_get_data(cube2, &array));
     85   OK(scad_stl_data_write(&array, "2cubes.stl", SCAD_FORCE_NORMALS_OUTWARD, 0));
     86 
     87   /* Check that with 3 cubes as a single model, the model cannot be exported
     88    * whith forced normals... */
     89   OK(scad_stl_get_data(cube3, &array));
     90   BAD(scad_stl_data_write(&array, "3cubes.stl", SCAD_FORCE_NORMALS_OUTWARD, 0));
     91   /* ...but can be exported anyway without forcing normals... */
     92   OK(scad_stl_data_write(&array, "3cubes.stl", SCAD_KEEP_NORMALS_UNCHANGED, 0));
     93   /* ...and can still be exported if some triangles are duplicated */
     94   OK(scad_stl_get_data(cube1, &array));
     95   OK(scad_stl_data_write(&array, "3cubesd.stl", SCAD_KEEP_NORMALS_UNCHANGED, 0));
     96 
     97   OK(scad_geometry_ref_put(cube1));
     98   OK(scad_geometry_ref_put(cube2));
     99   OK(scad_geometry_ref_put(cube3));
    100   OK(scad_finalize());
    101 
    102   darray_double_release(&array);
    103   check_memory_allocator(&allocator);
    104   mem_shutdown_proxy_allocator(&allocator);
    105   CHK(mem_allocated_size() == 0);
    106 
    107   return (res == RES_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
    108 }