star-cad

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

test_tolerance.c (2830B)


      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 #define _POSIX_C_SOURCE 200112L
     17 
     18 #include "scad.h"
     19 #include "scad_geometry.h"
     20 #include "test_common.h"
     21 
     22 #include <rsys/rsys.h>
     23 #include <rsys/math.h>
     24 #include <rsys/mem_allocator.h>
     25 #include <rsys/double3.h>
     26 
     27 #include <stdlib.h>
     28 #include <stdio.h>
     29 
     30 /*
     31  * +-------+              +-------+            +------+
     32  * |       | +----+       |       +----+       |     +----+
     33  * |       | |    |       |       |    |       |     ||   |
     34  * |       | +----+       |       +----+       |     +----+
     35  * +-------+              +-------+            +------+
     36  *    x = 1.1                  x = 1             x = 0.9
     37  */
     38 
     39 static int
     40 tolerance
     41   (double x,
     42    double tol,
     43    size_t expected_count,
     44    struct mem_allocator* allocator)
     45 {
     46   double p1[3] = {0, 0, 0};
     47   double d[3] = {1, 1, 1};
     48   double p2[3];
     49   struct scad_geometry* geoms[2];
     50   struct scad_geometry* out_geom;
     51   size_t c;
     52 
     53   ASSERT(tol > 0 && (expected_count == 1 || expected_count == 2));
     54 
     55   OK(scad_initialize(NULL, allocator, 3));
     56 
     57   /* set position for cube #2 */
     58   d3(p2, x, 0, 0);
     59 
     60   OK(scad_add_box("cube1", p1, d, geoms+0));
     61   OK(scad_add_box("cube2", p2, d, geoms+1));
     62 
     63   /* Try to fuse the 2 cubes and count resulting connex components */
     64   OK(scad_fuse_geometries("fused", geoms, 1, geoms+1, 1, &out_geom));
     65   OK(scad_geometry_get_count(out_geom, &c));
     66 
     67   OK(scad_finalize());
     68 
     69   return (c == expected_count) ? RES_OK : RES_BAD_ARG;
     70 }
     71 
     72 int
     73 main(int argc, char* argv[])
     74 {
     75   struct mem_allocator allocator;
     76   res_T res = RES_OK;
     77 
     78   (void)argc; (void)argv;
     79 
     80   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     81 
     82   /* With contacting geometries */
     83   OK(tolerance(1, 0.01, 1, &allocator));
     84   OK(tolerance(1, 0.1, 1, &allocator));
     85 
     86   /* With overlapping geometries */
     87   OK(tolerance(0.9, 0.01, 1, &allocator));
     88   OK(tolerance(0.9, 0.1, 1, &allocator));
     89 
     90   /* With distant geometries */
     91   OK(tolerance(1.1, 0.01, 2, &allocator));
     92   OK(tolerance(1.1, 0.1, 1, &allocator));
     93 
     94   check_memory_allocator(&allocator);
     95   mem_shutdown_proxy_allocator(&allocator);
     96   CHK(mem_allocated_size() == 0);
     97 
     98   return (res == RES_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
     99 }