test_sync.c (5189B)
1 /* Copyright (C) 2022-2025 |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/double3.h> 24 #include <rsys/mem_allocator.h> 25 #include <rsys/dynamic_array_double.h> 26 27 #include <stdlib.h> 28 #include <stdio.h> 29 30 static void 31 get_position 32 (const size_t ivert, double pos[2], void* data) 33 { 34 double* coord = data; 35 ASSERT(pos && coord); 36 pos[0] = coord[2*ivert]; 37 pos[1] = coord[1+2*ivert]; 38 } 39 40 int 41 main(int argc, char* argv[]) 42 { 43 res_T res = RES_OK; 44 const double p1[3] = {0, 0, 0}; 45 const double p2[3] = {0.25, 0.25, 0.8}; 46 const double d1[3] = {1, 1, 1}; 47 double coord[] = {0, 1.6, 0.5, 0.9, 0.8, 0.6}; 48 struct scad_geometry* geom1 = NULL; 49 struct scad_geometry* geom2 = NULL; 50 struct scad_geometry* geom = NULL; 51 struct scad_geometry* poly = NULL; 52 struct mem_allocator allocator; 53 struct darray_double trg; 54 struct scad_options options = SCAD_DEFAULT_OPTIONS; 55 size_t c; 56 57 (void)argc; (void)argv; 58 59 OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator)); 60 61 OK(scad_initialize(NULL, &allocator, 3)); 62 options.Mesh.MeshSizeExtendFromBoundary = 0; 63 options.Mesh.MeshSizeFromPoints = 0; 64 options.Mesh.Algorithm = SCAD_INITIAL_MESH_ONLY; 65 OK(scad_set_options(&options)); 66 67 OK(scad_add_box(p1, d1, &geom)); 68 OK(scad_scene_mesh()); 69 darray_double_init(&allocator, &trg); 70 OK(scad_stl_get_data(geom, &trg)); 71 CHK(12 * 9 == darray_double_size_get(&trg)); 72 darray_double_release(&trg); 73 OK(scad_geometry_ref_put(geom)); 74 75 OK(scad_add_box(p1, d1, &geom1)); 76 OK(scad_add_box(p2, d1, &geom2)); 77 OK(scad_geometries_cut(&geom1, 1, &geom2, 1, &geom)); 78 OK(scad_geometry_ref_put(geom1)); 79 OK(scad_geometry_ref_put(geom2)); 80 OK(scad_scene_mesh()); 81 darray_double_init(&allocator, &trg); 82 OK(scad_stl_get_data(geom, &trg)); 83 CHK(24 * 9 == darray_double_size_get(&trg)); 84 darray_double_release(&trg); 85 OK(scad_geometry_ref_put(geom)); 86 87 OK(scad_add_polygon(get_position, coord, 0, 3, &poly)); 88 OK(scad_geometry_extrude(poly, d1, &geom)); 89 OK(scad_geometry_ref_put(poly)); 90 OK(scad_scene_mesh()); 91 darray_double_init(&allocator, &trg); 92 OK(scad_stl_get_data(geom, &trg)); 93 CHK(8 * 9 == darray_double_size_get(&trg)); 94 darray_double_release(&trg); 95 OK(scad_geometry_ref_put(geom)); 96 97 OK(scad_add_box(p1, d1, &geom1)); 98 OK(scad_geometry_translate(geom1, d1, &geom)); 99 OK(scad_geometry_ref_put(geom1)); 100 OK(scad_scene_mesh()); 101 darray_double_init(&allocator, &trg); 102 OK(scad_stl_get_data(geom, &trg)); 103 CHK(12 * 9 == darray_double_size_get(&trg)); 104 darray_double_release(&trg); 105 OK(scad_geometry_ref_put(geom)); 106 107 OK(scad_add_box(p1, d1, &geom)); 108 OK(scad_geometry_set_visibility(geom, 0, 1)); 109 OK(scad_scene_mesh()); 110 darray_double_init(&allocator, &trg); 111 OK(scad_stl_get_data(geom, &trg)); 112 CHK(0 == darray_double_size_get(&trg)); 113 darray_double_release(&trg); 114 OK(scad_geometry_ref_put(geom)); 115 116 OK(scad_add_box(p1, d1, &geom)); 117 OK(scad_scene_mesh()); 118 OK(scad_geometries_clear_mesh(&geom, 1)); 119 darray_double_init(&allocator, &trg); 120 OK(scad_stl_get_data(geom, &trg)); 121 CHK(0 == darray_double_size_get(&trg)); 122 darray_double_release(&trg); 123 OK(scad_geometry_ref_put(geom)); 124 125 options.Mesh.Algorithm = SCAD_MESHADAPT; 126 options.Mesh.MeshSizeFromPoints = 1; 127 options.Mesh.MeshSizeExtendFromBoundary = 1; 128 OK(scad_set_options(&options)); 129 130 OK(scad_add_box(p1, d1, &geom)); 131 OK(scad_scene_mesh()); 132 darray_double_init(&allocator, &trg); 133 OK(scad_stl_get_data(geom, &trg)); 134 c = darray_double_size_get(&trg); 135 darray_double_clear(&trg); 136 137 OK(scad_geometries_clear_mesh(&geom, 1)); 138 OK(scad_stl_get_data(geom, &trg)); 139 CHK(0 == darray_double_size_get(&trg)); 140 141 OK(scad_geometries_set_mesh_size_modifier(&geom, 1, SCAD_SIZE_FACTOR, 0.25)); 142 OK(scad_scene_mesh()); 143 OK(scad_stl_get_data(geom, &trg)); 144 CHK(c < darray_double_size_get(&trg)); 145 darray_double_release(&trg); 146 OK(scad_geometry_ref_put(geom)); 147 148 options.Mesh.MeshSizeExtendFromBoundary = 0; 149 options.Mesh.MeshSizeFromPoints = 0; 150 options.Mesh.Algorithm = SCAD_INITIAL_MESH_ONLY; 151 OK(scad_set_options(&options)); 152 153 #if 0 154 set mesh size cb : besoin de sync ? 155 #endif 156 157 options.Misc.LogRefCounting = SCAD_LOG_DIMTAGS_ONLY_UNDELETED; 158 OK(scad_set_options(&options)); 159 160 OK(scad_finalize()); 161 162 check_memory_allocator(&allocator); 163 mem_shutdown_proxy_allocator(&allocator); 164 CHK(mem_allocated_size() == 0); 165 166 return (res == RES_OK) ? EXIT_SUCCESS : EXIT_FAILURE; 167 }