GCC Code Coverage Report


Directory: ./
File: mesh/mesh_CSG_builder.cpp
Date: 2026-11-30 05:48:47
Exec Total Coverage
Lines: 600 863 69.5%
Functions: 59 96 61.5%
Branches: 561 1418 39.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2023 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #include <geogram/mesh/mesh_CSG_builder.h>
41 #include <geogram/mesh/mesh_surface_intersection.h>
42 #include <geogram/mesh/mesh_io.h>
43 #include <geogram/mesh/mesh_repair.h>
44 #include <geogram/mesh/mesh_fill_holes.h>
45 #include <geogram/mesh/mesh_convex_hull.h>
46 #include <geogram/mesh/mesh_minkowski.h>
47 #include <geogram/delaunay/parallel_delaunay_3d.h>
48 #include <geogram/delaunay/CDT_2d.h>
49 #include <geogram/image/image.h>
50 #include <geogram/image/image_library.h>
51 #include <geogram/basic/line_stream.h>
52 #include <geogram/basic/command_line.h>
53
54 namespace {
55 using namespace GEO;
56
57 /**
58 * \brief Tests whether the meshes in a scope may have intersections
59 * \param[in] scope a scope
60 * \retval true if there may be some intersections between the meshes
61 * \retval false if there is for sure no intersection between the meshes
62 */
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 bool may_have_intersections(const CSGScope& scope) {
64 39 std::vector<Box3d> box(scope.size());
65
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 39 times.
277 for(index_t i=0; i<scope.size(); ++i) {
66
1/2
✓ Branch 1 taken 199 times.
✗ Branch 2 not taken.
199 box[i] = CSGBuilder::get_bbox(scope[i]);
67 }
68
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 16 times.
102 for(index_t i=0; i<scope.size(); ++i) {
69
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 63 times.
193 for(index_t j=i+1; j<scope.size(); ++j) {
70
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 23 times.
260 if(bboxes_overlap(box[i], box[j])) {
71 return true;
72 }
73 }
74 }
75 return false;
76 }
77 }
78
79
80 namespace GEO {
81
82
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 AbstractCSGBuilder::AbstractCSGBuilder() {
83
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 reset_defaults();
84
85 22 verbose_ = false;
86 22 detailed_verbose_ = false;
87 22 warnings_ = false;
88
89 #define DECLARE_OBJECT(obj) \
90 object_funcs_[#obj] = [this](const ArgList& args) { \
91 this->add_##obj(args); \
92 }
93
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
44 DECLARE_OBJECT(square);
94
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
46 DECLARE_OBJECT(circle);
95
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
69 DECLARE_OBJECT(cube);
96
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
58 DECLARE_OBJECT(sphere);
97
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
81 DECLARE_OBJECT(cylinder);
98
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
23 DECLARE_OBJECT(polyhedron);
99
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
55 DECLARE_OBJECT(polygon);
100
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
46 DECLARE_OBJECT(import);
101
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
24 DECLARE_OBJECT(surface);
102
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 DECLARE_OBJECT(text);
103
104 #define DECLARE_INSTRUCTION(instr) \
105 instruction_funcs_[#instr] = [this](const ArgList& args) { \
106 this->eval_##instr(args); \
107 }
108
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
276 DECLARE_INSTRUCTION(multmatrix);
109
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 DECLARE_INSTRUCTION(translate);
110
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 DECLARE_INSTRUCTION(rotate);
111
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 DECLARE_INSTRUCTION(scale);
112
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 DECLARE_INSTRUCTION(resize);
113
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
40 DECLARE_INSTRUCTION(intersection);
114
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
50 DECLARE_INSTRUCTION(difference);
115
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
130 DECLARE_INSTRUCTION(group);
116
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
23 DECLARE_INSTRUCTION(color);
117
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
23 DECLARE_INSTRUCTION(hull);
118
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
46 DECLARE_INSTRUCTION(linear_extrude);
119
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
25 DECLARE_INSTRUCTION(rotate_extrude);
120
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 DECLARE_INSTRUCTION(projection);
121
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 DECLARE_INSTRUCTION(minkowski);
122
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
35 DECLARE_INSTRUCTION(union);
123
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
26 DECLARE_INSTRUCTION(render);
124 22 }
125
126 44 AbstractCSGBuilder::~AbstractCSGBuilder() {
127 44 }
128
129 724 void AbstractCSGBuilder::reset_defaults() {
130 724 fa_ = DEFAULT_FA;
131 724 fs_ = DEFAULT_FS;
132 724 fn_ = DEFAULT_FN;
133 724 }
134
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 void AbstractCSGBuilder::add_object(
136 const std::string& object, const ArgList& args
137 ) {
138 auto object_func = object_funcs_.find(object);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if(object_func == object_funcs_.end()) {
140 ✗ error(object+ ": no such object");
141 }
142 object_func->second(args);
143 248 }
144
145 454 void AbstractCSGBuilder::begin_instruction() {
146 454 }
147
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 454 times.
454 void AbstractCSGBuilder::end_instruction(
149 const std::string& instruction, const ArgList& args
150 ) {
151 auto instruction_func = instruction_funcs_.find(instruction);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 454 times.
454 if(instruction_func == instruction_funcs_.end()) {
153 ✗ error(instruction+ ": no such instruction");
154 }
155 instruction_func->second(args);
156 454 }
157
158 ✗ void AbstractCSGBuilder::add_square(const ArgList& args) {
159 geo_argused(args);
160 ✗ error("not implemented");
161 }
162
163 ✗ void AbstractCSGBuilder::add_circle(const ArgList& args) {
164 geo_argused(args);
165 ✗ error("not implemented");
166 }
167
168 ✗ void AbstractCSGBuilder::add_cube(const ArgList& args) {
169 geo_argused(args);
170 ✗ error("not implemented");
171 }
172
173 ✗ void AbstractCSGBuilder::add_sphere(const ArgList& args) {
174 geo_argused(args);
175 ✗ error("not implemented");
176 }
177
178 ✗ void AbstractCSGBuilder::add_cylinder(const ArgList& args) {
179 geo_argused(args);
180 ✗ error("not implemented");
181 }
182
183 ✗ void AbstractCSGBuilder::add_polyhedron(const ArgList& args) {
184 geo_argused(args);
185 ✗ error("not implemented");
186 }
187
188 ✗ void AbstractCSGBuilder::add_polygon(const ArgList& args) {
189 geo_argused(args);
190 ✗ error("not implemented");
191 }
192
193 ✗ void AbstractCSGBuilder::add_import(const ArgList& args) {
194 geo_argused(args);
195 ✗ error("not implemented");
196 }
197
198 ✗ void AbstractCSGBuilder::add_surface(const ArgList& args) {
199 geo_argused(args);
200 ✗ error("not implemented");
201 }
202
203 ✗ void AbstractCSGBuilder::add_text(const ArgList& args) {
204 geo_argused(args);
205 ✗ error("not implemented");
206 }
207
208 ✗ void AbstractCSGBuilder::eval_multmatrix(const ArgList& args) {
209 geo_argused(args);
210 ✗ error("not implemented");
211 }
212
213 ✗ void AbstractCSGBuilder::eval_translate(const ArgList& args) {
214 geo_argused(args);
215 ✗ error("not implemented");
216 }
217
218 ✗ void AbstractCSGBuilder::eval_rotate(const ArgList& args) {
219 geo_argused(args);
220 ✗ error("not implemented");
221 }
222
223 ✗ void AbstractCSGBuilder::eval_scale(const ArgList& args) {
224 geo_argused(args);
225 ✗ error("not implemented");
226 }
227
228 ✗ void AbstractCSGBuilder::eval_resize(const ArgList& args) {
229 geo_argused(args);
230 ✗ error("not implemented");
231 }
232
233 ✗ void AbstractCSGBuilder::eval_union(const ArgList& args) {
234 geo_argused(args);
235 ✗ error("not implemented");
236 }
237
238 ✗ void AbstractCSGBuilder::eval_intersection(const ArgList& args) {
239 geo_argused(args);
240 ✗ error("not implemented");
241 }
242
243 ✗ void AbstractCSGBuilder::eval_difference(const ArgList& args) {
244 geo_argused(args);
245 ✗ error("not implemented");
246 }
247
248 ✗ void AbstractCSGBuilder::eval_group(const ArgList& args) {
249 geo_argused(args);
250 ✗ error("not implemented");
251 }
252
253 ✗ void AbstractCSGBuilder::eval_color(const ArgList& args) {
254 geo_argused(args);
255 ✗ error("not implemented");
256 }
257
258 ✗ void AbstractCSGBuilder::eval_hull(const ArgList& args) {
259 geo_argused(args);
260 ✗ error("not implemented");
261 }
262
263 ✗ void AbstractCSGBuilder::eval_linear_extrude(const ArgList& args) {
264 geo_argused(args);
265 ✗ error("not implemented");
266 }
267
268 ✗ void AbstractCSGBuilder::eval_rotate_extrude(const ArgList& args) {
269 geo_argused(args);
270 ✗ error("not implemented");
271 }
272
273 ✗ void AbstractCSGBuilder::eval_projection(const ArgList& args) {
274 geo_argused(args);
275 ✗ error("not implemented");
276 }
277
278 ✗ void AbstractCSGBuilder::eval_minkowski(const ArgList& args) {
279 geo_argused(args);
280 ✗ error("not implemented");
281 }
282
283 ✗ void AbstractCSGBuilder::eval_render(const ArgList& args) {
284 geo_argused(args);
285 ✗ error("not implemented");
286 }
287
288 /************************************************************************/
289
290
1/2
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 CSGBuilder::CSGBuilder() {
291
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 reset_file_path();
292 22 STL_epsilon_ = 1e-6;
293 22 max_arity_ = 32;
294 22 simplify_coplanar_facets_ = true;
295 22 coplanar_angle_tolerance_ = 0.0;
296 22 delaunay_ = true;
297 22 detect_intersecting_neighbors_ = true;
298 22 fast_union_ = false;
299
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 noop_ = false;
300
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 empty_mesh_ = std::make_shared<Mesh>();
301
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 empty_mesh_->vertices.set_dimension(2);
302 22 }
303
304 44 CSGBuilder::~CSGBuilder() {
305 44 }
306
307 /************************************************************/
308
309 22 std::shared_ptr<Mesh> CSGBuilder::square(vec2 size, bool center) {
310
311 double x1 = 0.0;
312 double y1 = 0.0;
313 double x2 = size.x;
314 double y2 = size.y;
315
316
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 13 times.
22 if(center) {
317 9 x1 -= size.x/2.0;
318 9 x2 -= size.x/2.0;
319 9 y1 -= size.y/2.0;
320 9 y2 -= size.y/2.0;
321 }
322
323
324
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
22 if(size.x <= 0.0 || size.y <= 0.0) {
325 ✗ if(warnings_) {
326 ✗ Logger::warn("CSG")
327 << "square with negative size (returning empty shape)"
328 << std::endl;
329 }
330 return empty_mesh_;
331 }
332
333 std::shared_ptr<Mesh> M = std::make_shared<Mesh>();
334
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 M->vertices.set_dimension(2);
335
336
2/4
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
22 M->vertices.create_vertex(vec2(x1,y1));
337
2/4
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
22 M->vertices.create_vertex(vec2(x2,y1));
338
2/4
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
22 M->vertices.create_vertex(vec2(x1,y2));
339
2/6
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
22 M->vertices.create_vertex(vec2(x2,y2));
340
341
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 M->facets.create_triangle(0,1,3);
342
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 M->facets.create_triangle(0,3,2);
343
344
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 M->edges.create_edge(0,1);
345
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 M->edges.create_edge(1,3);
346
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 M->edges.create_edge(3,2);
347
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 M->edges.create_edge(2,0);
348
349
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 finalize_mesh(M);
350
351 return M;
352 }
353
354 119 std::shared_ptr<Mesh> CSGBuilder::circle(double r, index_t nu) {
355
356
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 95 times.
119 if(nu == 0) {
357 24 nu = index_t(GEOCSG::get_fragments_from_r(r, fn_, fs_, fa_));
358 }
359 nu = std::max(nu, index_t(3));
360
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119 times.
119 if(r <= 0.0) {
362 ✗ if(warnings_) {
363 ✗ Logger::warn("CSG")
364 << "circle with negative radius (returning empty shape)"
365 << std::endl;
366 }
367 return empty_mesh_;
368 }
369
370 std::shared_ptr<Mesh> M = std::make_shared<Mesh>();
371
1/2
✓ Branch 1 taken 119 times.
✗ Branch 2 not taken.
119 M->vertices.set_dimension(2);
372
373
2/2
✓ Branch 0 taken 2916 times.
✓ Branch 1 taken 119 times.
3035 for(index_t u=0; u<nu; ++u) {
374 2916 double theta = double(u)*2.0*M_PI/double(nu);
375 2916 double ctheta = cos(theta);
376 2916 double stheta = sin(theta);
377 2916 double x = ctheta*r;
378
1/2
✓ Branch 1 taken 2916 times.
✗ Branch 2 not taken.
2916 double y = stheta*r;
379
1/4
✓ Branch 1 taken 2916 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2916 M->vertices.create_vertex(vec2(x,y));
380 }
381
382 // zigzag triangulation
383 {
384 index_t low = 0;
385 119 index_t high = nu-1;
386 for(;;) {
387
2/2
✓ Branch 0 taken 1367 times.
✓ Branch 1 taken 63 times.
1430 if(low+1==high) {
388 break;
389 }
390
1/2
✓ Branch 1 taken 1367 times.
✗ Branch 2 not taken.
1367 M->facets.create_triangle(low, low+1, high);
391 ++low;
392
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1311 times.
1367 if(high-1==low) {
393 break;
394 }
395
1/2
✓ Branch 1 taken 1311 times.
✗ Branch 2 not taken.
1311 M->facets.create_triangle(low, high-1, high);
396 --high;
397 }
398 }
399
400
2/2
✓ Branch 0 taken 2916 times.
✓ Branch 1 taken 119 times.
3035 for(index_t u=0; u<nu; ++u) {
401
1/2
✓ Branch 1 taken 2916 times.
✗ Branch 2 not taken.
2916 M->edges.create_edge(u, (u+1)%nu);
402 }
403
404
1/2
✓ Branch 1 taken 119 times.
✗ Branch 2 not taken.
119 finalize_mesh(M);
405
406 return M;
407 }
408
409 51 std::shared_ptr<Mesh> CSGBuilder::cube(vec3 size, bool center) {
410
411 double x1 = 0.0;
412 double y1 = 0.0;
413 double z1 = 0.0;
414 double x2 = size.x;
415 double y2 = size.y;
416 double z2 = size.z;
417
418
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 4 times.
51 if(center) {
419 47 x1 -= size.x/2.0;
420 47 x2 -= size.x/2.0;
421 47 y1 -= size.y/2.0;
422 47 y2 -= size.y/2.0;
423 47 z1 -= size.z/2.0;
424 47 z2 -= size.z/2.0;
425 }
426
427
3/6
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 51 times.
51 if(size.x <= 0.0 || size.y <= 0.0 || size.z <= 0.0) {
428 ✗ if(warnings_) {
429 ✗ Logger::warn("CSG")
430 << "cube with negative size (returning empty shape)"
431 << std::endl;
432 }
433 return empty_mesh_;
434 }
435
436 std::shared_ptr<Mesh> M = std::make_shared<Mesh>();
437
438
2/4
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
51 M->vertices.create_vertex(vec3(x1,y1,z1));
439
2/4
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
51 M->vertices.create_vertex(vec3(x2,y1,z1));
440
2/4
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
51 M->vertices.create_vertex(vec3(x1,y2,z1));
441
2/4
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
51 M->vertices.create_vertex(vec3(x2,y2,z1));
442
2/4
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
51 M->vertices.create_vertex(vec3(x1,y1,z2));
443
2/4
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
51 M->vertices.create_vertex(vec3(x2,y1,z2));
444
2/4
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
51 M->vertices.create_vertex(vec3(x1,y2,z2));
445
2/6
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
51 M->vertices.create_vertex(vec3(x2,y2,z2));
446
447
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(7,3,6);
448
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(6,3,2);
449
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(7,5,3);
450
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(3,5,1);
451
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(3,1,2);
452
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(2,1,0);
453
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(1,5,0);
454
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(0,5,4);
455
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(2,0,6);
456
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(6,0,4);
457
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(6,4,7);
458
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.create_triangle(7,4,5);
459
460
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 M->facets.connect();
461
462
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 finalize_mesh(M);
463
464 return M;
465 }
466
467 36 std::shared_ptr<Mesh> CSGBuilder::sphere(double r) {
468 36 index_t nu = index_t(GEOCSG::get_fragments_from_r(r,fn_,fs_,fa_));
469 36 index_t nv = nu / 2;
470
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
36 if(nu >= 5 && (nu & 1) != 0) {
471 ✗ ++nv;
472 }
473
474 nu = std::max(nu, index_t(3));
475 36 nv = std::max(nv, index_t(2));
476
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if(r <= 0.0) {
478 ✗ if(warnings_) {
479 ✗ Logger::warn("CSG")
480 << "sphere with negative radius (returning empty shape)"
481 << std::endl;
482 }
483 return empty_mesh_;
484 }
485
486 36 std::shared_ptr<Mesh> result = circle(1.0, nu);
487
488
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 GEOCSG::sweep(
489 result, nv,
490
1/4
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
36 [&](index_t u, index_t v)->vec3 {
491 16200 double phi = (double(v) + 0.5)*M_PI/double(nv) - M_PI/2.0;
492 16200 double cphi = cos(phi);
493 16200 double sphi = sin(phi);
494 16200 double ctheta = result->vertices.point(u).x;
495 16200 double stheta = result->vertices.point(u).y;
496 return vec3(
497 16200 r*ctheta*cphi,
498 16200 r*stheta*cphi,
499 16200 r*sphi
500 16200 );
501 }
502 );
503
504
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 result->facets.connect();
505
506
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 finalize_mesh(result);
507 return result;
508 }
509
510 59 std::shared_ptr<Mesh> CSGBuilder::cylinder(
511 double h, double r1, double r2, bool center
512 ) {
513 index_t nu = index_t(
514 59 GEOCSG::get_fragments_from_r(std::max(r1,r2),fn_,fs_,fa_)
515 59 );
516
517 59 double r[2] = { r1, r2 };
518
519 double z[2] = {
520
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 8 times.
59 center ? -h/2.0 : 0.0,
521 center ? h/2.0 : h
522 59 };
523
524
2/4
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 59 times.
59 if(r1 < 0.0 || r2 < 0.0) {
525 ✗ if(warnings_) {
526 ✗ Logger::warn("CSG")
527 << "cylinder with negative radius (returning empty shape)"
528 << std::endl;
529 }
530 return empty_mesh_;
531 }
532
533 // If first side's capping is a pole, then flip sides
534 // (sweep() supposes that the pole is always on the second side).
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if(r[0] == 0.0) {
536 std::swap(r[0],r[1]);
537 std::swap(z[0],z[1]);
538 }
539
540 59 std::shared_ptr<Mesh> result = circle(1.0, nu);
541
1/2
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
59 GEOCSG::sweep(
542 result, 2,
543
1/4
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
59 [&](index_t u, index_t v)->vec3 {
544 2561 vec3 p = result->vertices.point(u);
545 return vec3(
546 2561 r[v]*p.x, r[v]*p.y, z[v]
547 );
548 },
549
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 1 times.
59 (r[1] == 0.0) ? GEOCSG::SWEEP_POLE : GEOCSG::SWEEP_CAP
550 );
551
552
1/2
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
59 result->facets.connect();
553
554
1/2
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
59 finalize_mesh(result);
555 return result;
556 }
557
558
559 24 std::shared_ptr<Mesh> CSGBuilder::import(
560 const std::filesystem::path& filename, const std::string& layer,
561 index_t timestamp, vec2 origin, vec2 scale
562 ) {
563 24 std::shared_ptr<Mesh> result;
564
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 std::filesystem::path full_filename = filename;
565
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
24 if(!find_file(full_filename)) {
566 ✗ Logger::err("CSG") << filename << ": file not found"
567 << std::endl;
568 result = empty_mesh_;
569 ✗ return result;
570 }
571
572 24 if(
573
7/12
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 19 times.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 19 times.
✓ Branch 12 taken 5 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
77 full_filename.extension() == ".dxf" ||
574
4/10
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 19 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
29 full_filename.extension() == ".DXF"
575 ) {
576
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
38 result = import_with_openSCAD(full_filename, layer, timestamp);
577 } else {
578 5 result = std::make_shared<Mesh>();
579
3/8
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
20 mesh_load(full_filename.string(),*result);
580 }
581
582 24 if(
583
7/12
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 5 times.
✓ Branch 12 taken 19 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
91 full_filename.extension() == ".stl" ||
584
4/10
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 19 times.
✓ Branch 8 taken 5 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
43 full_filename.extension() == ".STL"
585 ) {
586 MeshRepairMode mode = MESH_REPAIR_DEFAULT;
587
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if(!verbose_) {
588 mode = MeshRepairMode(mode | MESH_REPAIR_QUIET);
589 }
590
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 mesh_repair(*result, mode, STL_epsilon_);
591 }
592
593
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 19 times.
24 if(result->vertices.dimension() == 3) {
594 bool z_all_zero = true;
595
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 for(const vec3& p : result->vertices.points()) {
596
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if(p.z != 0.0) {
597 z_all_zero = false;
598 break;
599 }
600 }
601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if(z_all_zero) {
602 ✗ result->vertices.set_dimension(2);
603 }
604 }
605
606
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 19 times.
24 if(result->vertices.dimension() == 3) {
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if(!result->facets.are_simplices()) {
608 ✗ tessellate_facets(*result, 3);
609 }
610 }
611 // Apply origin and scale, triangulate
612
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 5 times.
24 if(result->vertices.dimension() == 2) {
613
2/2
✓ Branch 0 taken 694 times.
✓ Branch 1 taken 19 times.
713 for(index_t v: result->vertices) {
614 vec2& p = result->vertices.point<2>(v);
615 694 p = vec2(
616 694 (p.x - origin.x) * scale.x,
617 694 (p.y - origin.y) * scale.y
618 );
619 }
620
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 result->facets.compute_borders();
621
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 triangulate(result);
622 }
623
624
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 finalize_mesh(result);
625 return result;
626 24 }
627
628 2 std::shared_ptr<Mesh> CSGBuilder::surface(
629 const std::filesystem::path& filename, bool center, bool invert
630 ) {
631 2 return surface_with_OpenSCAD(filename, center, invert);
632 }
633
634 ✗ std::shared_ptr<Mesh> CSGBuilder::text(
635 const std::string& text,
636 double size,
637 const std::string& font,
638 const std::string& halign,
639 const std::string& valign,
640 double spacing,
641 const std::string& direction,
642 const std::string& language,
643 const std::string& script
644 ) {
645 return text_with_OpenSCAD(
646 text, size, font, halign, valign, spacing,
647 direction, language, script
648 ✗ );
649 }
650
651 ✗ std::shared_ptr<Mesh> CSGBuilder::text_with_OpenSCAD(
652 const std::string& text,
653 double size,
654 const std::string& font,
655 const std::string& halign,
656 const std::string& valign,
657 double spacing,
658 const std::string& direction,
659 const std::string& language,
660 const std::string& script
661 ) {
662 ✗ GEOCSG::ArgList args;
663 ✗ args.add_arg("text", text);
664 ✗ args.add_arg("size", size);
665 ✗ args.add_arg("font", font);
666 ✗ args.add_arg("halign", halign);
667 ✗ args.add_arg("valign", valign);
668 ✗ args.add_arg("spacing", spacing);
669 ✗ args.add_arg("direction", direction);
670 ✗ args.add_arg("language", language);
671 ✗ args.add_arg("script", script);
672 bool TWO_D=true;
673 std::shared_ptr<Mesh> result = GEOCSG::call_OpenSCAD(
674 ✗ current_path(), "text", args, TWO_D
675 ✗ );
676 ✗ result->facets.compute_borders();
677 ✗ finalize_mesh(result);
678 ✗ return result;
679 }
680
681 2 std::shared_ptr<Mesh> CSGBuilder::surface_with_OpenSCAD(
682 const std::filesystem::path& filename, bool center, bool invert
683 ) {
684 2 std::shared_ptr<Mesh> result;
685
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 std::filesystem::path full_filename = filename;
686
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if(!find_file(full_filename)) {
687 ✗ Logger::err("CSG") << filename << ": file not found"
688 << std::endl;
689 result = empty_mesh_;
690 ✗ return result;
691 }
692
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 GEOCSG::ArgList args;
693
3/6
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
4 args.add_arg("file", full_filename);
694
3/6
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
4 args.add_arg("center", center);
695
3/6
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
4 args.add_arg("invert", invert);
696
3/6
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
2 result = GEOCSG::call_OpenSCAD(current_path(), "surface", args);
697
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 finalize_mesh(result);
698 return result;
699 2 }
700
701 258 std::shared_ptr<Mesh> CSGBuilder::multmatrix(
702 const mat4& M, const CSGScope& scope
703 ) {
704 258 std::shared_ptr<Mesh> result = group(scope);
705
706
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 60 times.
258 if(result->vertices.dimension() == 3) {
707
2/2
✓ Branch 0 taken 60123 times.
✓ Branch 1 taken 198 times.
60321 for(index_t v: result->vertices) {
708 60123 vec4 p = M*vec4(result->vertices.point<3>(v), 1.0);
709 60123 result->vertices.point<3>(v) = (1.0 / p.w) * vec3(p.x, p.y, p.z);
710 }
711
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 } else if(result->vertices.dimension() == 2) {
712
2/2
✓ Branch 0 taken 502 times.
✓ Branch 1 taken 60 times.
562 for(index_t v: result->vertices) {
713 502 vec4 p = M*vec4(result->vertices.point<2>(v), 0.0, 1.0);
714 502 result->vertices.point<2>(v) = (1.0 / p.w) * vec2(p.x, p.y);
715 }
716 } else {
717 ✗ geo_assert_not_reached;
718 }
719
720 // Preserve normals orientations if transform is left-handed
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258 times.
258 if(det(M) < 0.0) {
722 ✗ for(index_t e: result->edges) {
723 ✗ result->edges.flip(e);
724 }
725 ✗ for(index_t t: result->facets) {
726 ✗ result->facets.flip(t);
727 }
728 }
729
1/2
✓ Branch 1 taken 258 times.
✗ Branch 2 not taken.
258 finalize_mesh(result);
730 258 return result;
731 }
732
733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 431 times.
431 std::shared_ptr<Mesh> CSGBuilder::union_instr(const CSGScope& scope) {
734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 431 times.
431 if(scope.size() == 0) {
735 return empty_mesh_;
736 }
737
738
2/2
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 39 times.
431 if(scope.size() == 1) {
739 return scope[0];
740 }
741
742 // Boolean operations can handle no more than max_arity_ operands.
743 // For a union with more than max_arity_ operands, split it into two.
744 39 bool split = (scope.size() > max_arity_);
745
3/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 28 times.
78 if(fast_union_ && scope[0]->vertices.dimension() == 3) {
746 split = false; // do not split if fast_union_ is set and op is 3D
747 }
748
749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if(split) {
750 CSGScope scope1;
751 CSGScope scope2;
752 ✗ index_t n1 = index_t(scope.size()/2);
753 ✗ for(index_t i=0; i<scope.size(); ++i) {
754 ✗ if(i < n1) {
755 ✗ scope1.emplace_back(scope[i]);
756 } else {
757 ✗ scope2.emplace_back(scope[i]);
758 }
759 }
760 CSGScope scope3;
761 ✗ scope3.emplace_back(union_instr(scope1));
762 ✗ scope3.emplace_back(union_instr(scope2));
763 ✗ return union_instr(scope3);
764 }
765
766 39 std::shared_ptr<Mesh> result = append(scope);
767
3/4
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
✓ Branch 4 taken 16 times.
39 if(may_have_intersections(scope)) {
768
2/6
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
46 do_CSG(result, "union");
769 }
770
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 finalize_mesh(result);
771 return result;
772 }
773
774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 std::shared_ptr<Mesh> CSGBuilder::intersection(const CSGScope& scope) {
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if(scope.size() == 0) {
776 return empty_mesh_;
777 }
778
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if(scope.size() == 1) {
780 return scope[0];
781 }
782
783 // Boolean operations can handle no more than max_arity_ operands.
784 // For a intersection with more than max_arity_ operands,
785 // split it into two.
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if(scope.size() > max_arity_) {
787 CSGScope scope1;
788 CSGScope scope2;
789 ✗ index_t n1 = index_t(scope.size()/2);
790 ✗ for(index_t i=0; i<scope.size(); ++i) {
791 ✗ if(i < n1) {
792 ✗ scope1.emplace_back(scope[i]);
793 } else {
794 ✗ scope2.emplace_back(scope[i]);
795 }
796 }
797
798 CSGScope scope3;
799 ✗ scope3.emplace_back(intersection(scope1));
800 ✗ scope3.emplace_back(intersection(scope2));
801 ✗ return intersection(scope3);
802 }
803
804 18 std::shared_ptr<Mesh> result = append(scope);
805
2/6
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
18 do_CSG(result, "intersection");
806
1/2
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
18 finalize_mesh(result);
807 return result;
808 }
809
810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 std::shared_ptr<Mesh> CSGBuilder::difference(const CSGScope& scope) {
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if(scope.size() == 0) {
812 return empty_mesh_;
813 }
814
815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if(scope.size() == 1) {
816 return scope[0];
817 }
818
819 bool no_difference = true;
820
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 for(index_t i=1; i<scope.size(); ++i) {
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if(scope[i]->vertices.nb() != 0) {
822 no_difference = false;
823 break;
824 }
825 }
826
827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if(no_difference) {
828 return scope[0];
829 }
830
831 // Boolean operations can handle no more than max_arity_ operands.
832 // For a difference with more than max_arity_ operands, split it
833 // (by calling union_instr() that in turn splits the list if need be).
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if(scope.size() > max_arity_) {
835 CSGScope scope2;
836 ✗ for(index_t i=1; i<scope.size(); ++i) {
837 ✗ scope2.emplace_back(scope[i]);
838 }
839 CSGScope scope3;
840 ✗ scope3.emplace_back(scope[0]);
841 ✗ scope3.emplace_back(union_instr(scope2));
842 ✗ return difference(scope3);
843 }
844
845 30 std::shared_ptr<Mesh> result = append(scope);
846 // construct the expression x0-x1-x2...-xn
847
1/4
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
30 std::string expr = "x0";
848
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 30 times.
98 for(index_t i=1; i<scope.size(); ++i) {
849
1/4
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
76 expr += "-x" + String::to_string(i);
850 }
851
852
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 do_CSG(result, expr);
853
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 finalize_mesh(result);
854 return result;
855 }
856
857 371 std::shared_ptr<Mesh> CSGBuilder::group(const CSGScope& scope) {
858 371 return union_instr(scope);
859 }
860
861 1 std::shared_ptr<Mesh> CSGBuilder::color(vec4 color, const CSGScope& scope) {
862 geo_argused(color);
863 1 std::shared_ptr<Mesh> result = group(scope);
864
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 finalize_mesh(result);
865 1 return result;
866 }
867
868 1 std::shared_ptr<Mesh> CSGBuilder::hull(const CSGScope& scope) {
869 1 std::shared_ptr<Mesh> result = append(scope);
870
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 result->edges.clear();
871
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 result->facets.clear();
872 // Particular case: no vertex in scope (yes, this happens !)
873
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(result->vertices.nb() == 0) {
874 return result;
875 }
876
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(result->vertices.dimension() == 3) {
877
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 compute_convex_hull_3d(*result);
878 } else {
879 ✗ compute_convex_hull_2d(*result);
880 ✗ triangulate(result);
881 }
882
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 finalize_mesh(result);
883 return result;
884 }
885
886 24 std::shared_ptr<Mesh> CSGBuilder::linear_extrude(
887 const CSGScope& scope, double height, bool center, vec2 scale,
888 index_t slices, double twist
889 ) {
890 24 std::shared_ptr<Mesh> result = union_instr(scope);
891
892
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 10 times.
24 double z1 = center ? -height/2.0 : 0.0;
893 24 double z2 = center ? height/2.0 : height;
894
895
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if(slices == 0) {
896
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 slices = index_t(
897
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
48 GEOCSG::get_linear_extrusion_slices(
898 result, height, scale, twist, fn_, fs_, fa_
899 )
900 );
901 }
902
903 24 index_t nv = slices+1;
904
905
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 GEOCSG::sweep(
906 result, nv,
907
1/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
24 [&](index_t u, index_t v)->vec3 {
908
2/2
✓ Branch 0 taken 4599 times.
✓ Branch 1 taken 1618 times.
6217 vec2 ref = result->vertices.point<2>(u);
909 6217 double t = double(v) / double(nv-1);
910
911 6217 double s = 1.0 - t;
912 6217 double z = s*z1 + t*z2;
913
2/2
✓ Branch 0 taken 4599 times.
✓ Branch 1 taken 1618 times.
6217 vec2 sz = s*vec2(1.0, 1.0) + t*scale;
914
915 6217 double x = ref.x * sz.x;
916 6217 double y = ref.y * sz.y;
917
918
2/2
✓ Branch 0 taken 4599 times.
✓ Branch 1 taken 1618 times.
6217 if(twist != 0.0) {
919 4599 double alpha = twist*t*M_PI/180.0;
920 4599 double ca = cos(alpha);
921 4599 double sa = sin(alpha);
922 4599 double x2 = ca*x+sa*y;
923 4599 double y2 = -sa*x+ca*y;
924 x = x2;
925 y = y2;
926 }
927
928 6217 return vec3(x,y,z);
929 },
930
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 ((scale.x == 0.0 && scale.y == 0.0)
931 ? GEOCSG::SWEEP_POLE : GEOCSG::SWEEP_CAP)
932 );
933
934
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 result->facets.connect();
935
936
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 finalize_mesh(result);
937 24 return result;
938 }
939
940 3 std::shared_ptr<Mesh> CSGBuilder::rotate_extrude(
941 const CSGScope& scope, double angle
942 ) {
943 3 std::shared_ptr<Mesh> result = union_instr(scope);
944
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 result->vertices.set_dimension(2);
945
946
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(angle == 360.0) {
947
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 result->facets.clear();
948
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 result->vertices.remove_isolated();
949 }
950
951 // Remove edges that are co-linear with rotation axis.
952 // (as well as vertices dangling on rotation axis).
953 {
954 index_t nb_edges_to_remove = 0;
955
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 vector<index_t> remove_edges(result->edges.nb(),0);
956
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 3 times.
67 for(index_t e: result->edges) {
957 index_t v1 = result->edges.vertex(e,0);
958 index_t v2 = result->edges.vertex(e,1);
959 if(
960
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 55 times.
64 (result->vertices.point<2>(v1).x == 0.0) &&
961
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 (result->vertices.point<2>(v2).x == 0.0)
962 ) {
963 6 remove_edges[e] = 1;
964 6 ++nb_edges_to_remove;
965 }
966 }
967
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(nb_edges_to_remove != 0) {
968
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 result->edges.delete_elements(remove_edges);
969
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 result->vertices.remove_isolated();
970 }
971 }
972
973 3 double R = 0.0;
974
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 3 times.
64 for(index_t v: result->vertices) {
975 61 R = std::max(R, ::fabs(result->vertices.point<2>(v).x));
976 }
977 index_t slices = index_t(
978
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 GEOCSG::get_fragments_from_r_and_twist(R,angle,fn_,fs_,fa_)
979 3 );
980 3 index_t nv = slices+1;
981
982
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 GEOCSG::sweep(
983
1/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
3 result, nv, [&](index_t u, index_t v)->vec3 {
984 1830 vec2 ref = result->vertices.point<2>(u);
985 1830 double t = double(v) / double(nv-1);
986 1830 double alpha = t * 2.0 * M_PI * angle / 360.0;
987 return vec3(
988 1830 ref.x * cos(alpha),
989 1830 ref.x * sin(alpha),
990 ref.y
991 1830 );
992 },
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 (angle == 360.0) ? GEOCSG::SWEEP_PERIODIC : GEOCSG::SWEEP_CAP
994 );
995
996 // there may be duplicated points around the poles
997
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 mesh_repair(
998 *result, MeshRepairMode(MESH_REPAIR_DEFAULT | MESH_REPAIR_QUIET)
999 );
1000
1001 // Note: we flip when angle is *positive* because X,Y,rotate dir
1002 // is not a direct basis.
1003
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(angle > 0) {
1004
2/2
✓ Branch 0 taken 3300 times.
✓ Branch 1 taken 3 times.
3303 for(index_t t: result->facets) {
1005
1/2
✓ Branch 1 taken 3300 times.
✗ Branch 2 not taken.
3300 result->facets.flip(t);
1006 }
1007 }
1008
1009
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 result->facets.connect();
1010
1011
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 finalize_mesh(result);
1012 3 return result;
1013 }
1014
1015 ✗ std::shared_ptr<Mesh> CSGBuilder::projection(
1016 const CSGScope& scope, bool cut
1017 ) {
1018 ✗ std::shared_ptr<Mesh> result = append(scope);
1019 ✗ if(result->vertices.dimension() != 3) {
1020 ✗ Logger::err("CSG") << "projection(): input mesh is not of dimenion 3"
1021 << std::endl;
1022 ✗ result->clear();
1023 return result;
1024 }
1025 ✗ if(cut) {
1026 // Cut mode: intersection between object and z=0 plane.
1027 // We reuse the intersection() function as follows:
1028 // Compute intersection with (enlarged) half bbox and
1029 // keep z=0 facets only.
1030 ✗ auto [pmin, pmax] = get_bbox_bounds(result);
1031 vec3 T(
1032 ✗ pmin.x - (pmax.x - pmin.x),
1033 ✗ pmin.y - (pmax.y - pmin.y),
1034 0.0
1035 );
1036 ✗ std::shared_ptr<Mesh> C = cube(3.0*(pmax-pmin), false);
1037 ✗ for(index_t v: C->vertices) {
1038 C->vertices.point<3>(v) += T;
1039 }
1040 CSGScope scope2;
1041 ✗ scope2.push_back(result);
1042 ✗ scope2.push_back(C);
1043 ✗ result = difference(scope2);
1044 ✗ GEOCSG::keep_z0_only(result);
1045 ✗ result->facets.compute_borders();
1046 ✗ triangulate(result);
1047 } else {
1048 // Union of all triangles projected in 2D. We project only
1049 // half of them (since we have a closed shape). We select
1050 // the orientation with the smallest number of triangles.
1051 ✗ result->edges.clear();
1052 vector<GEO::Sign> sign(result->facets.nb());
1053 index_t nb_negative = 0;
1054 index_t nb_positive = 0;
1055 ✗ for(index_t t: result->facets) {
1056 index_t v1 = result->facets.vertex(t,0);
1057 index_t v2 = result->facets.vertex(t,1);
1058 index_t v3 = result->facets.vertex(t,2);
1059 ✗ vec2 p1 = result->vertices.point<2>(v1);
1060 ✗ vec2 p2 = result->vertices.point<2>(v2);
1061 ✗ vec2 p3 = result->vertices.point<2>(v3);
1062 ✗ sign[t] = GEO::PCK::orient_2d(p1,p2,p3);
1063 ✗ nb_negative += (sign[t] == GEO::NEGATIVE);
1064 ✗ nb_positive += (sign[t] == GEO::POSITIVE);
1065 }
1066 GEO::Sign keep_sign =
1067 ✗ (nb_positive > nb_negative) ? GEO::NEGATIVE : GEO::POSITIVE;
1068
1069 Attribute<index_t> e_operand_bit(
1070 ✗ result->edges.attributes(),"operand_bit"
1071 );
1072 ✗ for(index_t t: result->facets) {
1073 ✗ if(sign[t] == keep_sign) {
1074 index_t v1 = result->facets.vertex(t,0);
1075 index_t v2 = result->facets.vertex(t,1);
1076 index_t v3 = result->facets.vertex(t,2);
1077 ✗ index_t e1 = result->edges.create_edge(v1,v2);
1078 ✗ index_t e2 = result->edges.create_edge(v2,v3);
1079 ✗ index_t e3 = result->edges.create_edge(v3,v1);
1080 // We are using the "cnstr_operand_bits_is_operand_id"
1081 // of the "union" operation in CDT2d. It makes it
1082 // possible to compute a union operation with more than
1083 // 32 operands (and here we got one operand per input
1084 // triangle !).
1085 ✗ e_operand_bit[e1] = t;
1086 ✗ e_operand_bit[e2] = t;
1087 ✗ e_operand_bit[e3] = t;
1088 }
1089 }
1090 ✗ result->vertices.set_dimension(2);
1091 ✗ triangulate(result,"union_cnstr_operand_bits_is_operand_id");
1092 ✗ result->facets.compute_borders();
1093 ✗ triangulate(result);
1094 }
1095 ✗ finalize_mesh(result);
1096 return result;
1097 }
1098
1099 ✗ std::shared_ptr<Mesh> CSGBuilder::minkowski(const CSGScope& scope) {
1100 ✗ if(scope.size() == 0) {
1101 return empty_mesh_;
1102 }
1103
1104 ✗ if(scope.size() == 1) {
1105 return scope[0];
1106 }
1107
1108 ✗ if(scope.size() != 2) {
1109 ✗ error("Minkowski: more than 2 operands");
1110 }
1111
1112 if(
1113 ✗ scope[0]->vertices.dimension() == 2 &&
1114 scope[1]->vertices.dimension() == 2
1115 ) {
1116 std::shared_ptr<Mesh> result = std::make_shared<Mesh>();
1117 ✗ compute_minkowski_sum_2d(*result, *scope[0], *scope[1]);
1118 ✗ triangulate(result);
1119 return result;
1120 }
1121
1122 if(
1123 ✗ scope[0]->vertices.dimension() == 3 &&
1124 scope[1]->vertices.dimension() == 3
1125 ) {
1126 std::shared_ptr<Mesh> result = std::make_shared<Mesh>();
1127 ✗ compute_minkowski_sum_3d(*result, *scope[0], *scope[1]);
1128 // TODO: simplify coplanar facets
1129 return result;
1130 }
1131
1132 ✗ error("Minkowski: invalid operand dimensions");
1133 }
1134
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 std::shared_ptr<Mesh> CSGBuilder::append(const CSGScope& scope) {
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if(scope.size() == 0) {
1137 return empty_mesh_;
1138 }
1139
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if(scope.size() == 1) {
1141 return scope[0];
1142 }
1143
1144
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
88 if(!fast_union_ && scope.size() > max_arity_) {
1145 ✗ Logger::warn("CSG") << "Scope with more than "
1146 ✗ << max_arity_
1147 << " children"
1148 << std::endl;
1149 }
1150
1151 std::shared_ptr<Mesh> a = std::make_shared<Mesh>();
1152 88 index_t dim = 2;
1153 index_t nb_v = 0;
1154 index_t nb_e = 0;
1155 index_t nb_f = 0;
1156
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 88 times.
403 for(const std::shared_ptr<Mesh>& b: scope) {
1157 315 dim = std::max(dim, b->vertices.dimension());
1158 315 nb_v += b->vertices.nb();
1159 315 nb_e += b->edges.nb();
1160 315 nb_f += b->facets.nb();
1161 }
1162
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 a->vertices.set_dimension(dim);
1163 a->vertices.create_vertices(nb_v);
1164
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 a->edges.create_edges(nb_e);
1165
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 a->facets.create_triangles(nb_f);
1166
1167 index_t v_ofs = 0;
1168 index_t e_ofs = 0;
1169 index_t f_ofs = 0;
1170
1171 Attribute<index_t> f_operand_bit;
1172 Attribute<index_t> e_operand_bit;
1173
1174
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 29 times.
88 if(a->vertices.dimension() == 3) {
1175
2/4
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 59 times.
✗ Branch 5 not taken.
118 f_operand_bit.bind(a->facets.attributes(), "operand_bit");
1176 }
1177
1178
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 59 times.
88 if(a->vertices.dimension() == 2) {
1179
2/6
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
58 e_operand_bit.bind(a->edges.attributes(), "operand_bit");
1180 }
1181
1182 index_t operand_bit = index_t(1);
1183
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 88 times.
403 for(const std::shared_ptr<Mesh>& b : scope) {
1184
2/2
✓ Branch 0 taken 60734 times.
✓ Branch 1 taken 315 times.
61049 for(index_t v: b->vertices) {
1185
2/2
✓ Branch 0 taken 180888 times.
✓ Branch 1 taken 60734 times.
422510 for(index_t coord=0; coord<a->vertices.dimension(); ++coord) {
1186 180888 a->vertices.point_ptr(v + v_ofs)[coord] =
1187
1/2
✓ Branch 0 taken 180888 times.
✗ Branch 1 not taken.
180888 (coord < b->vertices.dimension() ?
1188 180888 b->vertices.point_ptr(v)[coord] : 0.0);
1189 }
1190 }
1191
1192
2/2
✓ Branch 0 taken 118980 times.
✓ Branch 1 taken 315 times.
119295 for(index_t f: b->facets) {
1193 geo_debug_assert(b->facets.nb_vertices(f) == 3);
1194 index_t v1 = b->facets.vertex(f,0);
1195 index_t v2 = b->facets.vertex(f,1);
1196 index_t v3 = b->facets.vertex(f,2);
1197
1/2
✓ Branch 0 taken 118980 times.
✗ Branch 1 not taken.
118980 a->facets.set_vertex(f + f_ofs, 0, v1 + v_ofs);
1198
1/2
✓ Branch 0 taken 118980 times.
✗ Branch 1 not taken.
118980 a->facets.set_vertex(f + f_ofs, 1, v2 + v_ofs);
1199
1/2
✓ Branch 0 taken 118980 times.
✗ Branch 1 not taken.
118980 a->facets.set_vertex(f + f_ofs, 2, v3 + v_ofs);
1200 index_t f1 = b->facets.adjacent(f,0);
1201 index_t f2 = b->facets.adjacent(f,1);
1202 index_t f3 = b->facets.adjacent(f,2);
1203
2/2
✓ Branch 0 taken 118216 times.
✓ Branch 1 taken 764 times.
118980 if(f1 != NO_INDEX) {
1204 118216 f1 += f_ofs;
1205 }
1206
2/2
✓ Branch 0 taken 118165 times.
✓ Branch 1 taken 815 times.
118980 if(f2 != NO_INDEX) {
1207 118165 f2 += f_ofs;
1208 }
1209
2/2
✓ Branch 0 taken 118251 times.
✓ Branch 1 taken 729 times.
118980 if(f3 != NO_INDEX) {
1210 118251 f3 += f_ofs;
1211 }
1212 a->facets.set_adjacent(f + f_ofs, 0, f1);
1213 a->facets.set_adjacent(f + f_ofs, 1, f2);
1214 a->facets.set_adjacent(f + f_ofs, 2, f3);
1215 if(f_operand_bit.is_bound()) {
1216 117880 f_operand_bit[f + f_ofs] = operand_bit;
1217 }
1218 }
1219
1220
2/2
✓ Branch 0 taken 1314 times.
✓ Branch 1 taken 315 times.
1629 for(index_t e: b->edges) {
1221 index_t v1 = b->edges.vertex(e,0);
1222 index_t v2 = b->edges.vertex(e,1);
1223
1/2
✓ Branch 0 taken 1314 times.
✗ Branch 1 not taken.
1314 a->edges.set_vertex(e + e_ofs, 0, v1 + v_ofs);
1224
1/2
✓ Branch 0 taken 1314 times.
✗ Branch 1 not taken.
1314 a->edges.set_vertex(e + e_ofs, 1, v2 + v_ofs);
1225 if(e_operand_bit.is_bound()) {
1226 1314 e_operand_bit[e + e_ofs] = operand_bit;
1227 }
1228 }
1229
1230 315 v_ofs += b->vertices.nb();
1231 315 e_ofs += b->edges.nb();
1232 315 f_ofs += b->facets.nb();
1233 315 operand_bit = operand_bit << 1;
1234 }
1235 return a;
1236 }
1237
1238 26 bool CSGBuilder::find_file(std::filesystem::path& filename) {
1239
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 for(const std::filesystem::path& path: file_path_) {
1240 52 std::filesystem::path current_path = path / filename;
1241
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 if(std::filesystem::is_regular_file(current_path)) {
1242
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 filename = current_path;
1243 return true;
1244 }
1245 52 }
1246 return false;
1247 }
1248
1249 19 std::shared_ptr<Mesh> CSGBuilder::import_with_openSCAD(
1250 const std::filesystem::path& filepath, const std::string& layer,
1251 index_t timestamp
1252 ) {
1253 19 GEOCSG::ArgList args;
1254
3/6
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 19 times.
✗ Branch 8 not taken.
38 args.add_arg("file", filepath);
1255
3/6
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 19 times.
✗ Branch 8 not taken.
38 args.add_arg("layer", layer);
1256
3/6
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 19 times.
✗ Branch 8 not taken.
38 args.add_arg("timestamp", int(timestamp));
1257 bool TWO_D = true;
1258 std::shared_ptr<Mesh> result = GEOCSG::call_OpenSCAD(
1259
2/4
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
38 current_path(), "import", args, TWO_D
1260
2/4
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 );
1261
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 result->facets.compute_borders();
1262
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 finalize_mesh(result);
1263 19 return result;
1264 }
1265
1266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 void CSGBuilder::do_CSG(
1267 std::shared_ptr<Mesh>& mesh, const std::string& boolean_expr
1268 ) {
1269
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 44 times.
71 if(mesh->vertices.dimension() == 2) {
1270 27 triangulate(mesh, boolean_expr); // has all intersections inside
1271 27 mesh->facets.clear(); // now keep edge borders only
1272 27 mesh->vertices.remove_isolated(); // then remove internal vertices
1273 27 triangulate(mesh); // re-triangulate border edges
1274 } else {
1275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if(noop_) {
1276 ✗ return;
1277 }
1278 44 MeshSurfaceIntersection I(*mesh);
1279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 I.set_verbose(verbose_);
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 I.set_fine_verbose(detailed_verbose_);
1281
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 I.set_delaunay(delaunay_);
1282
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 I.set_detect_intersecting_neighbors(detect_intersecting_neighbors_);
1283
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 I.intersect();
1284
4/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 28 times.
44 if(fast_union_ && boolean_expr == "union") {
1285
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 I.remove_internal_shells();
1286 } else {
1287
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 I.classify(boolean_expr);
1288 }
1289
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if(simplify_coplanar_facets_) {
1290
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 I.simplify_coplanar_facets(coplanar_angle_tolerance_);
1291 }
1292 44 }
1293 }
1294
1295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 void CSGBuilder::triangulate(
1296 std::shared_ptr<Mesh>& mesh, const std::string& boolean_expr
1297 ) {
1298
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
106 geo_assert(mesh->vertices.dimension() == 2);
1299
1300 // Keep edges only
1301 106 mesh->facets.clear();
1302 106 mesh->vertices.remove_isolated();
1303
1304
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 bool has_operand_bit = Attribute<index_t>::is_defined(
1305 106 mesh->edges.attributes(), "operand_bit"
1306 );
1307
1308
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
106 geo_assert(has_operand_bit);
1309
1310 Attribute<index_t> e_operand_bit(
1311 106 mesh->edges.attributes(), "operand_bit"
1312 );
1313
1314 #ifdef GEO_DEBUG
1315 {
1316 bool all_zero = true;
1317 for(index_t e: mesh->edges) {
1318 if(e_operand_bit[e] != 0 && e_operand_bit[e] != NO_INDEX) {
1319 all_zero = false;
1320 }
1321 }
1322 geo_debug_assert(!all_zero);
1323 }
1324 #endif
1325
1326
2/4
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
106 auto [pmin, pmax] = get_bbox_bounds(mesh);
1327 vec3 D = pmax - pmin;
1328
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 double d = std::max(std::max(D.x,D.y)*10.0, 1.0);
1329 D = vec3(d,d,0.0);
1330 pmin -= D;
1331 pmax += D;
1332
1333
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 GEO::ExactCDT2d CDT;
1334
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 CDT.create_enclosing_rectangle(pmin.x, pmin.y, pmax.x, pmax.y);
1335
1336 // In case there are duplicated vertices, keep track of indexing
1337 vector<index_t> vertex_id(mesh->vertices.nb());
1338
2/2
✓ Branch 0 taken 3127 times.
✓ Branch 1 taken 106 times.
3233 for(index_t v: mesh->vertices) {
1339 3127 vec2 p = mesh->vertices.point<2>(v);
1340
1/2
✓ Branch 1 taken 3127 times.
✗ Branch 2 not taken.
3127 vertex_id[v] = CDT.insert(
1341 6254 GEO::ExactCDT2d::ExactPoint(p.x, p.y, 1.0), v
1342 );
1343 }
1344
1345 // Memorize current number of vertices to detect vertices
1346 // coming from constraint intersections
1347 index_t nv0 = CDT.nv();
1348
1349 // Insert constraints
1350
2/2
✓ Branch 0 taken 3127 times.
✓ Branch 1 taken 106 times.
3233 for(index_t e: mesh->edges) {
1351 index_t v1 = mesh->edges.vertex(e,0);
1352 index_t v2 = mesh->edges.vertex(e,1);
1353
1/2
✓ Branch 1 taken 3127 times.
✗ Branch 2 not taken.
3127 CDT.insert_constraint(
1354 vertex_id[v1], vertex_id[v2], e_operand_bit[e]
1355 );
1356 }
1357
1358
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 CDT.classify_triangles(boolean_expr);
1359
1360 // Create vertices coming from constraint intersections
1361
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 106 times.
187 for(index_t v=nv0; v<CDT.nv(); ++v) {
1362
1/2
✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
81 GEO::vec2 p = GEO::PCK::approximate(CDT.point(v));
1363
1/2
✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
81 CDT.set_vertex_id(
1364 v,
1365
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
81 mesh->vertices.create_vertex(vec2(p.x, p.y))
1366 );
1367 }
1368
1369 // Create triangles in target mesh
1370
2/2
✓ Branch 0 taken 2739 times.
✓ Branch 1 taken 106 times.
5584 for(index_t t=0; t<CDT.nT(); ++t) {
1371
1/2
✓ Branch 1 taken 2739 times.
✗ Branch 2 not taken.
2739 mesh->facets.create_triangle(
1372 CDT.vertex_id(CDT.Tv(t,0)),
1373 CDT.vertex_id(CDT.Tv(t,1)),
1374 CDT.vertex_id(CDT.Tv(t,2))
1375 );
1376 }
1377
1378
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 mesh->facets.connect();
1379
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 mesh->facets.compute_borders();
1380 212 }
1381
1382 79 void CSGBuilder::triangulate(std::shared_ptr<Mesh>& mesh) {
1383 Attribute<index_t> e_operand_bit(
1384 158 mesh->edges.attributes(),"operand_bit"
1385 );
1386
2/2
✓ Branch 0 taken 1837 times.
✓ Branch 1 taken 79 times.
1916 for(index_t e: mesh->edges) {
1387 1837 e_operand_bit[e] = index_t(1);
1388 }
1389
3/8
✓ Branch 1 taken 79 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 79 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 79 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
158 triangulate(mesh, "union");
1390 79 }
1391
1392 740 void CSGBuilder::finalize_mesh(std::shared_ptr<Mesh>& M) {
1393 geo_argused(M);
1394 740 }
1395
1396 305 Box3d CSGBuilder::get_bbox(const std::shared_ptr<Mesh>& M) {
1397 Box3d result;
1398
2/2
✓ Branch 0 taken 915 times.
✓ Branch 1 taken 305 times.
1220 for(index_t c=0; c<3; ++c) {
1399 915 result.xyz_min[c] = Numeric::max_float64();
1400 915 result.xyz_max[c] = -Numeric::max_float64();
1401 }
1402
2/2
✓ Branch 0 taken 35778 times.
✓ Branch 1 taken 305 times.
36083 for(index_t v: M->vertices) {
1403 const double* pp = M->vertices.point_ptr(v);
1404 vec3 p(
1405
2/2
✓ Branch 0 taken 32058 times.
✓ Branch 1 taken 3720 times.
35778 pp[0], pp[1], (M->vertices.dimension() == 3) ? pp[2] : 0.0
1406 );
1407
2/2
✓ Branch 0 taken 107334 times.
✓ Branch 1 taken 35778 times.
143112 for(index_t c=0; c<3; ++c) {
1408
2/2
✓ Branch 0 taken 5083 times.
✓ Branch 1 taken 102251 times.
107334 result.xyz_min[c] = std::min(result.xyz_min[c], p[c]);
1409
2/2
✓ Branch 0 taken 4227 times.
✓ Branch 1 taken 103107 times.
111561 result.xyz_max[c] = std::max(result.xyz_max[c], p[c]);
1410 }
1411 }
1412 305 return result;
1413 }
1414
1415 /***************** AbstractCSGBuilder API implementation ***********/
1416
1417 248 void CSGBuilder::add_object(
1418 const std::string& object, const ArgList& args
1419 ) {
1420 248 AbstractCSGBuilder::add_object(object,args);
1421 geo_debug_assert(result_ != nullptr);
1422
1/2
✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
248 top_scope().push_back(result_);
1423 result_ = nullptr;
1424 248 }
1425
1426 454 void CSGBuilder::begin_instruction() {
1427 454 AbstractCSGBuilder::begin_instruction();
1428 push_scope();
1429 454 }
1430
1431 454 void CSGBuilder::end_instruction(
1432 const std::string& instruction, const ArgList& args
1433 ) {
1434 454 AbstractCSGBuilder::end_instruction(instruction, args);
1435 pop_scope();
1436 geo_debug_assert(result_ != nullptr);
1437
1/2
✓ Branch 0 taken 454 times.
✗ Branch 1 not taken.
454 top_scope().push_back(result_);
1438 result_ = nullptr;
1439 454 }
1440
1441 22 void CSGBuilder::add_square(const ArgList& args) {
1442 vec2 size{1.0, 1.0};
1443 // polymorphic size arg (painful...)
1444
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
44 if(args.has_arg("size",0,Value::NUMBER)) {
1445 double s = 1.0;
1446 ✗ s = args.get_arg("size",s,0);
1447 ✗ size = {s,s};
1448 } else {
1449
1/2
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
44 size = args.get_arg("size",size,0);
1450 }
1451
1/2
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 bool center = args.get_arg("center", false, 1);
1452 22 result_ = square(size,center);
1453 22 }
1454
1455 24 void CSGBuilder::add_circle(const ArgList& args) {
1456 double r = 1.0;
1457
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 r = args.get_arg("r",r,0);
1458 24 result_ = circle(r);
1459 24 }
1460
1461 47 void CSGBuilder::add_cube(const ArgList& args) {
1462 vec3 size{1.0, 1.0, 1.0};
1463 // polymorphic size arg (painful...)
1464
2/4
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 47 times.
94 if(args.has_arg("size",0,Value::NUMBER)) {
1465 double s = 1.0;
1466 ✗ s = args.get_arg("size",s,0);
1467 ✗ size = {s,s,s};
1468 } else {
1469
1/2
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
94 size = args.get_arg("size",size,0);
1470 }
1471 bool center = false;
1472
1/2
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 center = args.get_arg("center", center, 1);
1473 47 result_ = cube(size,center);
1474 47 }
1475
1476 36 void CSGBuilder::add_sphere(const ArgList& args) {
1477
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 double r = args.get_arg("r", 1.0, 0);
1478 36 result_ = sphere(r);
1479 36 }
1480
1481 59 void CSGBuilder::add_cylinder(const ArgList& args) {
1482
1/2
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
59 double h = args.get_arg("h", 1.0, 0);
1483 double r1 = 1.0;
1484 double r2 = 1.0;
1485 59 if(
1486
4/8
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 59 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 59 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 59 times.
✗ Branch 9 not taken.
118 args.has_arg("r1", 0, Value::NUMBER) &&
1487
3/8
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 59 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 59 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
118 args.has_arg("r2", 1, Value::NUMBER)
1488 ) {
1489
1/2
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
59 r1 = args.get_arg("r1", r1, 1);
1490
1/2
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
118 r2 = args.get_arg("r2", r1, 2);
1491 } else {
1492 ✗ r1 = args.get_arg("r", r1, 1);
1493 r2 = r1;
1494 }
1495
1/2
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
59 bool center = args.get_arg("center", false, 3);
1496 59 result_ = cylinder(h,r1,r2,center);
1497 59 }
1498
1499 1 void CSGBuilder::add_polyhedron(const ArgList& args) {
1500 1 result_ = std::make_shared<Mesh>();
1501
8/18
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
3 if(!args.has_arg("points") || !args.has_arg("faces")) {
1502 ✗ error("polyhedron: missing points or facets");
1503 }
1504
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 const Value& points = args.get_arg_value("points");
1505
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 const Value& faces = args.get_arg_value("faces");
1506
1507
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if(points.type != Value::ARRAY2D || faces.type != Value::ARRAY2D) {
1508 ✗ error("polyhedron: wrong type (expected array)");
1509 }
1510
1511 result_->vertices.create_vertices(points.array_val.size());
1512
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for(index_t v=0; v<points.array_val.size(); ++v) {
1513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if(points.array_val[v].size() != 3) {
1514 ✗ error("polyhedron: wrong vertex size (expected 3d)");
1515 }
1516 5 result_->vertices.point(v) = {
1517 points.array_val[v][0],
1518 points.array_val[v][1],
1519 points.array_val[v][2]
1520 10 };
1521 }
1522
1523 vector<index_t> facet;
1524
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
12 for(index_t f=0; f<faces.array_val.size(); ++f) {
1525
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5 times.
21 for(index_t lv=0; lv < faces.array_val[f].size(); ++lv) {
1526 16 double v = faces.array_val[f][lv];
1527
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if(v < 0.0 || v >= double(result_->vertices.nb())) {
1528 ✗ error(
1529 ✗ String::format(
1530 "polyhedron: invalid vertex index %d (max is %d)",
1531 ✗ int(v), int(result_->vertices.nb())-1
1532 )
1533 );
1534 }
1535
1/4
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
16 facet.push_back(index_t(v));
1536 }
1537
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
5 index_t new_f = result_->facets.create_polygon(facet.size());
1538
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5 times.
21 for(index_t lv=0; lv < facet.size(); ++lv) {
1539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 result_->facets.set_vertex(new_f, lv, facet[lv]);
1540 }
1541
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 facet.resize(0);
1542 }
1543
1544
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 tessellate_facets(*result_,3);
1545
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 result_->facets.connect();
1546
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 finalize_mesh(result_);
1547 1 }
1548
1549 33 void CSGBuilder::add_polygon(const ArgList& args) {
1550 33 result_ = std::make_shared<Mesh>();
1551 33 result_->vertices.set_dimension(2);
1552
1553
8/18
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 33 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 33 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 33 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 33 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 33 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
99 if(!args.has_arg("points") || !args.has_arg("paths")) {
1554 ✗ error("polygon: missing points or paths");
1555 }
1556
1557
1/2
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 const Value& points = args.get_arg_value("points");
1558
1559
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 if(points.type == Value::ARRAY1D && points.array_val.size() == 0) {
1560 // Special case, empty array, happens with BOSL2 scripts
1561 return;
1562 }
1563
1564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if(points.type != Value::ARRAY2D) {
1565 ✗ error("polygon: wrong points type (expected array)");
1566 }
1567
1568 result_->vertices.create_vertices(points.array_val.size());
1569
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 33 times.
275 for(index_t v=0; v<points.array_val.size(); ++v) {
1570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 209 times.
209 if(points.array_val[v].size() != 2) {
1571 ✗ error("polyhedron: wrong vertex size (expected 2d)");
1572 }
1573 209 result_->vertices.point<2>(v) = {
1574 points.array_val[v][0],
1575 points.array_val[v][1]
1576 418 };
1577 }
1578
1579
1/2
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 const Value& paths = args.get_arg_value("paths");
1580
1581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if(paths.type == Value::ARRAY2D ) {
1582 ✗ for(const auto& P : paths.array_val) {
1583 ✗ for(double v: P) {
1584 ✗ if(v < 0.0 || v >= double(result_->vertices.nb())) {
1585 ✗ error(
1586 ✗ String::format(
1587 "polygon: invalid vertex index %d (max is %d)",
1588 ✗ int(v), int(result_->vertices.nb())-1
1589 )
1590 );
1591 }
1592 }
1593 ✗ for(index_t lv1=0; lv1 < P.size(); ++lv1) {
1594 ✗ index_t lv2 = (lv1+1)%P.size();
1595 ✗ index_t v1 = index_t(P[lv1]);
1596 ✗ index_t v2 = index_t(P[lv2]);
1597 // some files do [0,1,2], some others [0,1,2,0], so we need
1598 // to test here for null edges.
1599 ✗ if(v1 != v2) {
1600 ✗ result_->edges.create_edge(v1,v2);
1601 }
1602 }
1603 }
1604
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 } else if(paths.type == Value::NONE) {
1605
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 33 times.
242 for(index_t v1=0; v1 < points.array_val.size(); ++v1) {
1606 209 index_t v2 = (v1+1)%points.array_val.size();
1607 209 result_->edges.create_edge(v1,v2);
1608 }
1609 } else {
1610 ✗ error("polygon: wrong path type (expected array or undef)");
1611 }
1612 33 triangulate(result_);
1613 33 finalize_mesh(result_);
1614 }
1615
1616 24 void CSGBuilder::add_import(const ArgList& args) {
1617
3/8
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 24 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 24 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
48 std::string filename = args.get_arg("file", std::string(""));
1618
4/12
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 24 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 24 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
48 std::string layer = args.get_arg("layer", std::string(""));
1619
4/8
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 24 times.
✗ Branch 10 not taken.
48 index_t timestamp = index_t(args.get_arg("timestamp", 0));
1620
3/6
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 24 times.
✗ Branch 8 not taken.
48 vec2 origin = args.get_arg("origin", vec2(0.0, 0.0));
1621
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
24 vec2 scale = args.get_arg("scale", vec2(1.0, 1.0));
1622
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 result_ = import(
1623
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
48 filename,layer,timestamp,origin,scale
1624 );
1625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if(result_ == nullptr) {
1626 ✗ error((filename + ": could not load").c_str());
1627 }
1628 24 }
1629
1630 2 void CSGBuilder::add_surface(const ArgList& args) {
1631
3/8
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
4 std::string filename = args.get_arg("file", std::string(""));
1632
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 bool center = args.get_arg("center", false);
1633
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 bool invert = args.get_arg("invert", false);
1634
3/8
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
4 result_ = surface(filename, center, invert);
1635 2 }
1636
1637 ✗ void CSGBuilder::add_text(const ArgList& args) {
1638 ✗ std::string text = args.get_arg("text", "");
1639 ✗ if(text == "") {
1640 ✗ text = args.get_arg("arg_0", "");
1641 }
1642 ✗ if(text == "") {
1643 ✗ text = args.get_arg("t", "");
1644 }
1645 ✗ double size = args.get_arg("size", 10.0);
1646 ✗ std::string font = args.get_arg("font", "");
1647 ✗ std::string halign = args.get_arg("halign", "left");
1648 ✗ std::string valign = args.get_arg("valign", "baseline");
1649 ✗ double spacing = args.get_arg("spacing", 1.0);
1650 ✗ std::string direction = args.get_arg("direction", "ltr");
1651 ✗ std::string language = args.get_arg("language", "en");
1652 ✗ std::string script = args.get_arg("script", "latin");
1653 ✗ result_ = this->text(
1654 text, size, font, halign, valign,
1655 spacing, direction, language, script
1656 );
1657 ✗ }
1658
1659 254 void CSGBuilder::eval_multmatrix(const ArgList& args) {
1660 mat4 xform;
1661 xform.load_identity();
1662
1/2
✓ Branch 2 taken 254 times.
✗ Branch 3 not taken.
254 xform = args.get_arg("m", xform, 0);
1663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 254 times.
254 result_ = multmatrix(xform, top_scope());
1664 254 }
1665
1666 ✗ void CSGBuilder::eval_translate(const ArgList& args) {
1667 ✗ vec3 T{0.0, 0.0, 0.0};
1668 ✗ T = args.get_arg("v", T, 0);
1669 ✗ mat4 xform = translation_matrix(T);
1670 ✗ result_ = multmatrix(xform, top_scope());
1671 ✗ }
1672
1673 ✗ void CSGBuilder::eval_rotate(const ArgList& args) {
1674 double a(0.0);
1675 ✗ vec3 v{0.0, 0.0, 0.0};
1676 ✗ a = args.get_arg("a",a,0);
1677 ✗ v = args.get_arg("v",v,1);
1678 mat4 xform;
1679 ✗ if(a == 0.0) {
1680 // no angle specified, v is [rx, ry, rz]
1681 ✗ xform = rotation_matrix(v);
1682 } else {
1683 // v is rotation axis
1684 ✗ xform = rotation_matrix(a, v);
1685 }
1686 ✗ result_ = multmatrix(xform, top_scope());
1687 ✗ }
1688
1689 ✗ void CSGBuilder::eval_scale(const ArgList& args) {
1690 vec3 s{1.0, 1.0, 1.0};
1691 ✗ s = args.get_arg("v",s,0);
1692 ✗ mat4 xform = scaling_matrix(s.x, s.y, s.z);
1693 ✗ result_ = multmatrix(xform, top_scope());
1694 ✗ }
1695
1696 ✗ void CSGBuilder::eval_resize(const ArgList& args) {
1697 vec3 newsize(1.0, 1.0, 1.0);
1698 vec3 autosize(0.0, 0.0, 0.0);
1699 ✗ newsize = args.get_arg("newsize",newsize,0);
1700 ✗ autosize = args.get_arg("autosize",autosize,1);
1701
1702 ✗ result_ = union_instr(top_scope());
1703 vec3 scaling(1.0, 1.0, 1.0);
1704 double default_scaling = 1.0;
1705 ✗ Box3d B = get_bbox(result_);
1706
1707 ✗ for(index_t coord=0; coord<3; ++coord) {
1708 ✗ if(newsize[coord] != 0) {
1709 ✗ scaling[coord] = newsize[coord] / (
1710 ✗ B.xyz_max[coord] - B.xyz_min[coord]
1711 );
1712 default_scaling = scaling[coord];
1713 }
1714 }
1715
1716 ✗ for(index_t coord=0; coord<3; ++coord) {
1717 ✗ if(newsize[coord] == 0.0) {
1718 ✗ if(autosize[coord] == 1.0) {
1719 ✗ scaling[coord] = default_scaling;
1720 }
1721 }
1722 }
1723 ✗ for(index_t v: result_->vertices) {
1724 ✗ if(result_->vertices.dimension() == 3) {
1725 ✗ result_->vertices.point<3>(v).x *= scaling.x;
1726 ✗ result_->vertices.point<3>(v).y *= scaling.y;
1727 ✗ result_->vertices.point<3>(v).z *= scaling.z;
1728 } else {
1729 ✗ result_->vertices.point<2>(v).x *= scaling.x;
1730 ✗ result_->vertices.point<2>(v).y *= scaling.y;
1731 }
1732 }
1733 ✗ }
1734
1735 13 void CSGBuilder::eval_union(const ArgList& args) {
1736 geo_argused(args);
1737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 result_ = union_instr(top_scope());
1738 13 }
1739
1740 18 void CSGBuilder::eval_intersection(const ArgList& args) {
1741 geo_argused(args);
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 result_ = intersection(top_scope());
1743 18 }
1744
1745 28 void CSGBuilder::eval_difference(const ArgList& args) {
1746 geo_argused(args);
1747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 result_ = difference(top_scope());
1748 28 }
1749
1750 112 void CSGBuilder::eval_group(const ArgList& args) {
1751 geo_argused(args);
1752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 result_ = group(top_scope());
1753 112 }
1754
1755 1 void CSGBuilder::eval_color(const ArgList& args) {
1756 vec4 C(1.0, 1.0, 1.0, 1.0);
1757
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 C = args.get_arg("arg_0",C);
1758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 result_ = color(C,top_scope());
1759 1 }
1760
1761 1 void CSGBuilder::eval_hull(const ArgList& args) {
1762 geo_argused(args);
1763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 result_ = hull(top_scope());
1764 1 }
1765
1766 24 void CSGBuilder::eval_linear_extrude(const ArgList& args) {
1767
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 double height = args.get_arg("height", 1.0);
1768
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
48 bool center = args.get_arg("center", false);
1769
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 vec2 scale = args.get_arg("scale", vec2(1.0, 1.0));
1770
2/4
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
24 index_t slices = index_t(args.get_arg("slices",0));
1771
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 double twist = args.get_arg("twist",0.0);
1772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 result_ = linear_extrude(
1773 top_scope(), height, center, scale, slices, twist
1774 );
1775 24 }
1776
1777 3 void CSGBuilder::eval_rotate_extrude(const ArgList& args) {
1778
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 double angle = args.get_arg("angle", 360.0);
1779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 result_ = rotate_extrude(top_scope(),angle);
1780 3 }
1781
1782 ✗ void CSGBuilder::eval_projection(const ArgList& args) {
1783 ✗ bool cut = args.get_arg("cut", false);
1784 ✗ result_ = projection(top_scope(),cut);
1785 ✗ }
1786
1787 ✗ void CSGBuilder::eval_minkowski(const ArgList& args) {
1788 geo_argused(args);
1789 ✗ result_ = minkowski(top_scope());
1790 ✗ }
1791
1792 4 void CSGBuilder::eval_render(const ArgList& args) {
1793 4 eval_group(args);
1794 4 }
1795
1796 }
1797