GCC Code Coverage Report


Directory: ./
File: mesh/mesh_surface_intersection.cpp
Date: 2026-11-30 05:48:47
Exec Total Coverage
Lines: 761 894 85.1%
Functions: 44 49 89.8%
Branches: 729 1400 52.1%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 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_surface_intersection.h>
41 #include <geogram/mesh/mesh_surface_intersection_internal.h>
42 #include <geogram/mesh/mesh_AABB.h>
43 #include <geogram/mesh/mesh_repair.h>
44 #include <geogram/mesh/mesh_fill_holes.h>
45 #include <geogram/mesh/mesh_geometry.h>
46 #include <geogram/mesh/mesh_topology.h>
47 #include <geogram/mesh/mesh_io.h>
48 #include <geogram/mesh/boxes_intersections.h>
49 #include <geogram/mesh/index.h>
50 #include <geogram/delaunay/CDT_2d.h>
51 #include <geogram/numerics/predicates.h>
52 #include <geogram/numerics/expansion_nt.h>
53 #include <geogram/basic/stopwatch.h>
54 #include <geogram/basic/permutation.h>
55 #include <geogram/basic/boolean_expression.h>
56 #include <geogram/basic/debug_stream.h>
57 #include <geogram/basic/algorithm.h>
58
59 #include <sstream>
60 #include <stack>
61 #include <bitset>
62
63 #ifdef GEO_COMPILER_CLANG
64 // I'm using long long
65 #pragma GCC diagnostic ignored "-Wc++98-compat-pedantic"
66 #endif
67
68 // If defined, displays status messages and saves files whenever some
69 // error conditions are met.
70 // #define MESH_SURFACE_INTERSECTION_DEBUG
71
72 namespace {
73 using namespace GEO;
74 /**
75 * \brief Removes all facets that have their three vertices aligned
76 */
77 587 void remove_degenerate_triangles(Mesh& M) {
78 vector<index_t> remove_f(M.facets.nb());
79
1/4
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
587 parallel_for(0, M.facets.nb(), [&M, &remove_f](index_t f) {
80
1/2
✓ Branch 0 taken 136884 times.
✗ Branch 1 not taken.
136884 const vec3& p1 = M.facets.point(f,0);
81 const vec3& p2 = M.facets.point(f,1);
82 const vec3& p3 = M.facets.point(f,2);
83 136884 remove_f[f] = PCK::aligned_3d(p1,p2,p3);
84 136884 });
85
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 M.facets.delete_elements(remove_f);
86 587 }
87
88 /**
89 * \brief Enumerates the connected components in a facet attribute
90 * \param[in] M a reference to the mesh
91 * \param[in] attribute the name of the facet attribute
92 * \return the number of found connected components
93 */
94 1183 index_t get_surface_connected_components(
95 Mesh& M, const std::string& attribute = "chart"
96 ) {
97 Attribute<index_t> chart(M.facets.attributes(), attribute);
98
1/2
✓ Branch 1 taken 1183 times.
✗ Branch 2 not taken.
2366 return GEO::get_connected_components(M, chart);
99 }
100
101 /**
102 * \brief Computes the intersection between two mesh triangular facets
103 * \details This function is just a wrapper around triangles_intersections()
104 * for Mesh facets.
105 * \param[in] M the mesh
106 * \param[in] f1 , f2 the two facets
107 * \param[out] I a vector of triangle intersections
108 * \retval true if there was an intersection
109 * \retval false otherwise
110 */
111
1/2
✓ Branch 0 taken 1125520 times.
✗ Branch 1 not taken.
1125520 bool mesh_facets_intersect(
112 Mesh& M, index_t f1, index_t f2, TriangleIsects& I
113 ) {
114 geo_debug_assert(M.facets.nb_vertices(f1) == 3);
115 geo_debug_assert(M.facets.nb_vertices(f2) == 3);
116 index_t v1 = M.facets.vertex(f1,0);
117 index_t v2 = M.facets.vertex(f1,1);
118 index_t v3 = M.facets.vertex(f1,2);
119 index_t w1 = M.facets.vertex(f2,0);
120 index_t w2 = M.facets.vertex(f2,1);
121 index_t w3 = M.facets.vertex(f2,2);
122 1125520 return triangles_intersections(
123 M.vertices.point(v1),
124 M.vertices.point(v2),
125 M.vertices.point(v3),
126 M.vertices.point(w1),
127 M.vertices.point(w2),
128 M.vertices.point(w3),
129 v1,v2,v3,
130 w1,w2,w3,
131 I,
132 TriangleIsectFlags(
133 TRIANGLES_INTERSECTION_NO_DEGENERATE | // removed in pre-proc
134 TRIANGLES_INTERSECTION_NO_BBOX // we use an AABB
135 )
136 1125520 );
137 }
138
139 /**
140 * \brief Tests whether a segment intersects a triangle
141 * \param[in] q1 , q2 the two extremities of the segment
142 * \param[in] p1 , p2 , p3 the three verties of the triangle
143 * \retval true if the segment has an intersection with the
144 * interior of the triangle
145 * \retval false otherwise
146 * \details Degenerate configurations (segment passing through vertex,
147 * edge, or co-planar with triangle) are symbolically perturbed.
148 */
149 76168 template <class POINT> bool segment_triangle_intersection_SOS(
150 const POINT& q1, const POINT& q2,
151 const POINT& p1, const POINT& p2, const POINT& p3
152 ) {
153 576 Sign o1 = PCK::orient_3d_SOS(q1,p1,p2,p3);
154 576 Sign o2 = PCK::orient_3d_SOS(q2,p1,p2,p3);
155
156 // There is no intersection if q1 and q2 are on the
157 // same side of the supporting plane of (p1,p2,p3)
158
2/2
✓ Branch 0 taken 30294 times.
✓ Branch 1 taken 7790 times.
76168 if(o1 == o2) {
159 return false;
160 }
161
162 // There is an intersection if the three tetrahedra
163 // formed by [q1,q2] and the three edges of the triangle
164 // have the same orientation
165 96 Sign s1 = PCK::orient_3d_SOS(q1,q2,p1,p2);
166 96 Sign s2 = PCK::orient_3d_SOS(q1,q2,p2,p3);
167
2/2
✓ Branch 0 taken 5082 times.
✓ Branch 1 taken 2708 times.
15580 if(s1*s2 < 0) {
168 return false;
169 }
170 30 Sign s3 = PCK::orient_3d_SOS(q1,q2,p3,p1);
171
3/4
✓ Branch 0 taken 2397 times.
✓ Branch 1 taken 311 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 311 times.
5416 return(s2*s3 > 0 && s3*s1 > 0);
172 }
173
174 /**
175 * \brief Computes the barycenter of a triangle
176 * \param[in] p1 , p2 , p3 the exact coordinates of the three vertices of
177 * the triangle in homogeneous form
178 * \return the exact barycenter of the triangle in homogenous form
179 */
180 3 exact::vec3h triangle_barycenter(
181 const exact::vec3h& p1, const exact::vec3h& p2, const exact::vec3h& p3
182 ) {
183 3 exact::vec3h p = p1;
184
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(p2.w == p.w) {
185
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 p.x += p2.x;
186
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 p.y += p2.y;
187
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 p.z += p2.z;
188 } else {
189 ✗ p.x = p2.w*p.x + p.w*p2.x;
190 ✗ p.y = p2.w*p.y + p.w*p2.y;
191 ✗ p.z = p2.w*p.z + p.w*p2.z;
192 ✗ p.w *= p2.w;
193 }
194
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(p3.w == p.w) {
195
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 p.x += p3.x;
196
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 p.y += p3.y;
197
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 p.z += p3.z;
198 } else {
199 ✗ p.x = p3.w*p.x + p.w*p3.x;
200 ✗ p.y = p3.w*p.y + p.w*p3.y;
201 ✗ p.z = p3.w*p.z + p.w*p3.z;
202 ✗ p.w *= p3.w;
203 }
204
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 p.w *= exact::vec3h::value_type(3.0);
205 3 return p;
206 ✗ }
207 }
208
209
210 namespace GEO {
211
212 587 MeshSurfaceIntersection::MeshSurfaceIntersection(Mesh& M) :
213 lock_(GEOGRAM_SPINLOCK_INIT),
214 587 mesh_(M),
215
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
1174 vertex_to_exact_point_(M.vertices.attributes(), "exact_point"),
216 587 dry_run_(false),
217 halfedges_(*this),
218 radial_bundles_(*this),
219 587 radial_polylines_(*this)
220 {
221
2/2
✓ Branch 0 taken 72132 times.
✓ Branch 1 taken 587 times.
72719 for(index_t v: mesh_.vertices) {
222 72132 vertex_to_exact_point_[v] = nullptr;
223 }
224 587 verbose_ = false;
225 587 fine_verbose_ = false;
226 587 delaunay_ = true;
227 587 detect_intersecting_neighbors_ = true;
228 587 use_radial_sort_ = true;
229 587 monster_threshold_ = NO_INDEX;
230 587 has_operand_bits_ = false;
231 587 skeleton_ = nullptr;
232 587 skeleton_trim_fins_ = false;
233 587 interpolate_attributes_ = false;
234 587 }
235
236 587 MeshSurfaceIntersection::~MeshSurfaceIntersection() {
237 587 vertex_to_exact_point_.destroy();
238 1174 }
239
240 ✗ void MeshSurfaceIntersection::remove_external_shell() {
241 vector<index_t> remove_f;
242 ✗ mark_external_shell(remove_f);
243 ✗ mesh_.facets.delete_elements(remove_f);
244 ✗ mesh_.facets.connect();
245 ✗ }
246
247 28 void MeshSurfaceIntersection::remove_internal_shells() {
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if(mesh_.facets.nb() == 0) {
249 ✗ return;
250 }
251 vector<index_t> remove_f;
252
1/2
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
28 mark_external_shell(remove_f);
253
2/2
✓ Branch 0 taken 225074 times.
✓ Branch 1 taken 28 times.
225102 for(index_t& i: remove_f) {
254 225074 i=1-i;
255 }
256
1/2
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
28 mesh_.facets.delete_elements(remove_f);
257 }
258
259 ✗ void MeshSurfaceIntersection::remove_fins() {
260 // TODO
261 ✗ }
262
263 587 void MeshSurfaceIntersection::intersect_prologue() {
264
1/2
✓ Branch 2 taken 587 times.
✗ Branch 3 not taken.
587 Stopwatch W("Prologue", verbose_);
265
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 582 times.
587 if(!mesh_.facets.are_simplices()) {
266
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 tessellate_facets(mesh_,3);
267 }
268
269 Attribute<index_t> operand_bit;
270
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 operand_bit.bind_if_is_defined(
271
4/6
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 587 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 572 times.
✓ Branch 7 taken 15 times.
1174 mesh_.facets.attributes(), "operand_bit"
272 );
273
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 15 times.
587 has_operand_bits_ = operand_bit.is_bound();
274 if(!operand_bit.is_bound()) {
275 // TODO: not good, there might be more than 32 components
276 // (and why do we need to do that BTW?)
277
2/4
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
15 get_surface_connected_components(mesh_,"operand_bit");
278
2/6
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
15 operand_bit.bind(mesh_.facets.attributes(), "operand_bit");
279
2/2
✓ Branch 0 taken 54108 times.
✓ Branch 1 taken 15 times.
54123 for(index_t f: mesh_.facets) {
280 54108 operand_bit[f] = (index_t(1) << operand_bit[f]) ;
281 }
282 }
283
284
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 remove_degenerate_triangles(mesh_);
285
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 mesh_colocate_vertices_no_check(mesh_);
286
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 mesh_remove_bad_facets_no_check(mesh_);
287
288 // Set symbolic perturbation mode to lexicographic order
289 // on point coordinates instead of point indices only,
290 // Needed to get compatible triangulations on coplanar faces
291 // (example, cubes that touch on a facet).
292
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 SOS_bkp_ = PCK::get_SOS_mode();
293
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 PCK::set_SOS_mode(PCK::SOS_LEXICO);
294 587 }
295
296 587 void MeshSurfaceIntersection::intersect_get_intersections(
297 vector<IsectInfo>& intersections
298 ) {
299
2/4
✓ Branch 2 taken 587 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 587 times.
✗ Branch 6 not taken.
1174 Stopwatch Wtot("Find isects", verbose_);
300 {
301 vector<std::pair<index_t, index_t> > FF;
302 1125520 auto report_BB = [this,&FF](index_t f1, index_t f2) {
303 // Optionally skip facet pairs that share a vertex or an edge
304 if(
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1125520 times.
1125520 !detect_intersecting_neighbors_ && (
306 ✗ (mesh_.facets.find_adjacent(f1,f2)!=NO_INDEX) ||
307 ✗ (mesh_.facets.find_common_vertex(f1,f2)!=NO_INDEX)
308 )
309 ) {
310 return;
311 }
312 1125520 FF.push_back(std::make_pair(f1,f2));
313 };
314
315 // For now, keeping parallel AABB construction and evaluation,
316 // ZE seems to be slower, see stats in:
317 // https://github.com/BrunoLevy/geogram/issues/362
318 static constexpr bool use_ZE = false; // Zoromodian-Edelsbrunner
319 static constexpr bool ZE_groups = false;
320 static constexpr bool ZE_groups_auto_isect = true;
321
322 // Implementation using AABB
323 if(!use_ZE) {
324
3/6
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 587 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 587 times.
✗ Branch 8 not taken.
587 Stopwatch* W = new Stopwatch("AABB build", verbose_);
325
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 MeshFacetsAABB AABB(mesh_, AABB_INDIRECT);
326 587 delete W;
327 // Get candidate pairs of intersecting facets
328
4/8
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 587 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 587 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 587 times.
✗ Branch 11 not taken.
1174 W = new Stopwatch("AABB box-box", verbose_);
329
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 AABB.compute_facet_bbox_intersections(report_BB);
330 587 delete W;
331 }
332
333 // Alternative implementation using Zoromodian-Edelsbrunner
334 if(use_ZE) {
335 Stopwatch W("AABB box-box", verbose_);
336 vector<Box3d> boxes(mesh_.facets.nb());
337 parallel_for(
338 0, mesh_.facets.nb(), [&](index_t f) {
339 vec3 p0 = mesh_.facets.point(f,0);
340 vec3 p1 = mesh_.facets.point(f,1);
341 vec3 p2 = mesh_.facets.point(f,2);
342 boxes[f].xyz_min[0] = std::min(std::min(p0.x,p1.x),p2.x);
343 boxes[f].xyz_min[1] = std::min(std::min(p0.y,p1.y),p2.y);
344 boxes[f].xyz_min[2] = std::min(std::min(p0.z,p1.z),p2.z);
345 boxes[f].xyz_max[0] = std::max(std::max(p0.x,p1.x),p2.x);
346 boxes[f].xyz_max[1] = std::max(std::max(p0.y,p1.y),p2.y);
347 boxes[f].xyz_max[2] = std::max(std::max(p0.z,p1.z),p2.z);
348 }
349 );
350
351
352 index_t nb_groups = 0;
353 vector<index_t> indices;
354 vector<index_t> group_ptr;
355 bool has_facets_in_several_groups = false;
356
357 auto has_one_bit = [](index_t x)->bool {
358 return ((x != 0) && ((x & (x - 1))) == 0);
359 };
360
361 // Compute groups
362 if(ZE_groups && has_operand_bits_) {
363 Attribute<index_t> operand_bit;
364 operand_bit.bind_if_is_defined(
365 mesh_.facets.attributes(), "operand_bit"
366 );
367 if(operand_bit.is_bound()) {
368 vector<index_t> group_size(32,0);
369 for(index_t f: mesh_.facets) {
370 for(index_t g=0; g<32; ++g) {
371 if(operand_bit[f] == (1u << g)) {
372 ++group_size[g];
373 } else if (!has_one_bit(operand_bit[f])) {
374 has_facets_in_several_groups = true;
375 break;
376 }
377 }
378 }
379 // If some facets belong to several groups (this
380 // can happen when some input facets are exactly
381 // co-planar), then we do not use group-based
382 // computation (it would be possible, but it
383 // would require to split the set of facets in
384 // several groups according to the groups they
385 // belong to). It is also possible to consider
386 // all the facets in multiple groups as a single
387 // group, but doing so is slow for high-arity
388 // operators (example: dragon_bas.scad).
389 if(!has_facets_in_several_groups) {
390 for(index_t g=0; g<32; ++g) {
391 if(group_size[g] != 0) {
392 nb_groups = std::max(nb_groups,g);
393 }
394 }
395 ++nb_groups;
396 group_ptr.resize(nb_groups+1);
397 group_ptr[0] = 0;
398 for(index_t g=0; g<nb_groups; ++g) {
399 group_ptr[g+1] = group_ptr[g] + group_size[g];
400 }
401 indices.resize(group_ptr[nb_groups]);
402 vector<index_t> group_insert = group_ptr;
403 for(index_t f: mesh_.facets) {
404 for(index_t g=0; g<32; ++g) {
405 if(operand_bit[f] == (1u << g)) {
406 indices[group_insert[g]] = f;
407 ++group_insert[g];
408 }
409 }
410 }
411 }
412 }
413 }
414
415 if(nb_groups == 0) {
416 boxes_intersections(boxes, report_BB);
417 } else {
418 for(index_t g1 = 0; g1 < nb_groups; ++g1) {
419 for(index_t g2 = 0; g2 < nb_groups; ++g2) {
420 if(ZE_groups_auto_isect || g1 != g2) {
421 index_t b1 = group_ptr[g1];
422 index_t e1 = group_ptr[g1+1];
423 index_t b2 = group_ptr[g2];
424 index_t e2 = group_ptr[g2+1];
425 boxes_intersections_hybrid_impl(
426 boxes.data(),
427 indices.data()+b1, indices.data()+e1,
428 boxes.data(),
429 indices.data()+b2, indices.data()+e2,
430 report_BB
431 );
432 }
433 }
434 }
435 }
436 }
437
438 // Compute facet-facet intersections in parallel
439
3/6
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 587 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 587 times.
✗ Branch 8 not taken.
587 Stopwatch* W = new Stopwatch("AABB tri-tri", verbose_);
440
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 Process::spinlock lock = GEOGRAM_SPINLOCK_INIT;
441
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 parallel_for(
442
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
1125520 0, FF.size(), [&](index_t i) {
443 TriangleIsects I;
444 1125520 index_t f1 = FF[i].first;
445 1125520 index_t f2 = FF[i].second;
446
2/2
✓ Branch 1 taken 1088718 times.
✓ Branch 2 taken 36802 times.
1125520 if(!mesh_facets_intersect(mesh_,f1, f2, I)) {
447 1088718 return;
448 }
449
450 36802 Process::acquire_spinlock(lock);
451
452
2/2
✓ Branch 0 taken 10907 times.
✓ Branch 1 taken 25895 times.
36802 if(I.size() > 2) {
453 // Coplanar intersection: to generate the edges,
454 // test validity of all possible pairs of vertices.
455
2/2
✓ Branch 0 taken 41988 times.
✓ Branch 1 taken 10907 times.
52895 for(index_t i1=0; i1< I.size(); ++i1) {
456
2/2
✓ Branch 0 taken 61612 times.
✓ Branch 1 taken 41988 times.
103600 for(index_t i2=0; i2<i1; ++i2) {
457 IsectInfo II = {
458 f1, f2,
459 61612 I[i1].first, I[i1].second,
460 61612 I[i2].first, I[i2].second
461 61612 };
462
463 // Valid edges are the ones where both
464 // extremities are on the same edge
465 // of f1 or on the same edge of f2
466 // (note: it is a *combinatorial*
467 // convex hull).
468 61612 TriangleRegion AB1=regions_convex_hull(
469 II.A_rgn_f1,II.B_rgn_f1
470 );
471
472 61612 TriangleRegion AB2=regions_convex_hull(
473 II.A_rgn_f2, II.B_rgn_f2
474 );
475
476
4/4
✓ Branch 1 taken 40278 times.
✓ Branch 2 taken 21334 times.
✓ Branch 4 taken 20654 times.
✓ Branch 5 taken 19624 times.
61612 if(region_dim(AB1)==1 || region_dim(AB2)==1) {
477
2/2
✓ Branch 0 taken 41895 times.
✓ Branch 1 taken 93 times.
41988 intersections.push_back(II);
478 41988 II.flip();
479
2/2
✓ Branch 0 taken 41983 times.
✓ Branch 1 taken 5 times.
41988 intersections.push_back(II);
480 }
481 }
482 }
483 } else {
484 // Intersection is either a segment
485 // or a vertex of f2.
486 25895 TriangleRegion A_rgn_f1 = I[0].first;
487 25895 TriangleRegion A_rgn_f2 = I[0].second;
488
489 TriangleRegion B_rgn_f1 = A_rgn_f1;
490 TriangleRegion B_rgn_f2 = A_rgn_f2;
491
492
2/2
✓ Branch 0 taken 21592 times.
✓ Branch 1 taken 4303 times.
25895 if(I.size() == 2) {
493 21592 B_rgn_f1 = I[1].first;
494 21592 B_rgn_f2 = I[1].second;
495 }
496
497 IsectInfo II = {
498 f1, f2,
499 A_rgn_f1, A_rgn_f2,
500 B_rgn_f1, B_rgn_f2
501 25895 };
502
2/2
✓ Branch 0 taken 25467 times.
✓ Branch 1 taken 428 times.
25895 intersections.push_back(II);
503 25895 II.flip();
504
2/2
✓ Branch 0 taken 25844 times.
✓ Branch 1 taken 51 times.
25895 intersections.push_back(II);
505 }
506 36802 Process::release_spinlock(lock);
507 }
508 );
509 587 delete W;
510 }
511 587 }
512
513 587 void MeshSurfaceIntersection::intersect_remesh_intersections(
514 vector<IsectInfo>& intersections
515 ) {
516
1/2
✓ Branch 2 taken 587 times.
✗ Branch 3 not taken.
587 Stopwatch W("CDT",verbose_);
517
518 // Keep track of original facet ids: they will be copied to the subfacets
519 // whenever a facet is split.
520 // NOTE: of course, facet ids are no longer valids once coplanar facets
521 // are merged.
522
2/4
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 587 times.
✗ Branch 5 not taken.
587 original_facet_id_.bind(mesh_.facets.attributes(), "original_facet_id");
523
2/2
✓ Branch 0 taken 136848 times.
✓ Branch 1 taken 587 times.
137435 for(index_t f: mesh_.facets) {
524 136848 original_facet_id_[f] = f;
525 }
526
527 // We need to copy the initial mesh, because MeshInTriangle needs
528 // to access it in parallel threads, and without a copy, the internal
529 // arrays of the mesh can be modified whenever there is a
530 // reallocation. Without copying, we would need to insert many
531 // locks (each time the mesh is accessed).
532
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 mesh_copy_.copy(mesh_);
533
534 {
535 // Sort intersections by f1, so that all intersections between f1
536 // and another facet appear as a contiguous sequence.
537 587 GEO::sort(
538
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 intersections.begin(), intersections.end(),
539 587 [](const IsectInfo& a, const IsectInfo& b) -> bool {
540
14/32
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 99491 times.
✓ Branch 17 taken 135665 times.
✓ Branch 18 taken 381804 times.
✓ Branch 19 taken 397693 times.
✓ Branch 20 taken 373564 times.
✓ Branch 21 taken 397693 times.
✓ Branch 22 taken 3208 times.
✓ Branch 23 taken 9392 times.
✓ Branch 24 taken 912 times.
✓ Branch 25 taken 2296 times.
✓ Branch 26 taken 1908 times.
✓ Branch 27 taken 7484 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✓ Branch 30 taken 45 times.
✓ Branch 31 taken 795 times.
1799350 return (a.f1 < b.f1);
541 }
542 );
543
544 // Now iterate on all intersections, and identify
545 // the [b,e[ intervals that correspond to the same f1 facet.
546 // Get starting indices of intersections in same facet.
547 vector<index_t> start;
548 {
549 587 index_t b=0;
550
2/2
✓ Branch 0 taken 15506 times.
✓ Branch 1 taken 587 times.
16680 while(b < intersections.size()) {
551 start.push_back(b);
552 index_t e = b;
553 while(
554
2/2
✓ Branch 0 taken 151216 times.
✓ Branch 1 taken 56 times.
151272 e < intersections.size() &&
555
2/2
✓ Branch 0 taken 135766 times.
✓ Branch 1 taken 15450 times.
151216 intersections[e].f1 == intersections[b].f1
556 ) {
557 135766 ++e;
558 }
559 15506 b = e;
560 }
561
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 start.push_back(intersections.size());
562 }
563
564 // Display intersection stats
565
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 567 times.
587 if(verbose_) {
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 index_t nb_intersections = intersections.size()/2;
567 20 index_t nb_intersected_triangles = (start.size()-1)/2;
568 20 index_t max_intersections_in_triangle = 0;
569
2/2
✓ Branch 0 taken 4527 times.
✓ Branch 1 taken 20 times.
4547 for(index_t i=0; i+1<start.size(); ++i) {
570 4527 max_intersections_in_triangle = std::max(
571 max_intersections_in_triangle,
572 4527 index_t(start[i+1]-start[i])
573 );
574 }
575
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
20 Logger::out("CDT") << "Intersections: "
576 << nb_intersections << std::endl;
577
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
20 Logger::out("CDT") << "Intersected triangles: "
578 << nb_intersected_triangles
579 << std::endl;
580
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
40 Logger::out("CDT") << "Max intersections in triangle: "
581
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 << max_intersections_in_triangle
582 << std::endl;
583 }
584
585
586 #define TRIANGULATE_IN_PARALLEL
587
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 index_t f_done = 0;
589
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 index_t f_tot = (start.size()-1);
590
591 #ifdef TRIANGULATE_IN_PARALLEL
592
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 parallel_for_slice(
593
1/4
✓ Branch 0 taken 587 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
587 0,start.size()-1, [&](index_t k1, index_t k2) {
594 #else
595 index_t k1 = 0;
596 index_t k2 = start.size()-1;
597 #endif
598
599 755 MeshInTriangle MIT(*this);
600
1/2
✓ Branch 1 taken 755 times.
✗ Branch 2 not taken.
755 MIT.set_delaunay(delaunay_);
601
1/2
✓ Branch 1 taken 755 times.
✗ Branch 2 not taken.
755 MIT.set_dry_run(dry_run_);
602
603
1/2
✓ Branch 1 taken 755 times.
✗ Branch 2 not taken.
755 index_t tid = Thread::current_id();
604
605
2/2
✓ Branch 0 taken 15506 times.
✓ Branch 1 taken 755 times.
16261 for(index_t k=k1; k<k2; ++k) {
606
2/2
✓ Branch 0 taken 4527 times.
✓ Branch 1 taken 10979 times.
15506 index_t b = start[k];
607 15506 index_t e = start[k+1];
608
609
2/2
✓ Branch 0 taken 4527 times.
✓ Branch 1 taken 10979 times.
15506 if(fine_verbose_) {
610 4527 ++f_done;
611
2/4
✓ Branch 1 taken 4527 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4527 times.
✗ Branch 5 not taken.
9054 Logger::out("Isect")
612
1/4
✓ Branch 0 taken 4527 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4527 << String::format(
613 "[%2d] %5d/%5d %6d:%3d",
614
1/2
✓ Branch 1 taken 4527 times.
✗ Branch 2 not taken.
4527 int(tid), int(f_done), int(f_tot),
615
1/2
✓ Branch 1 taken 4527 times.
✗ Branch 2 not taken.
4527 int(intersections[b].f1), int(e-b)
616 )
617 << std::endl;
618 }
619
620
1/2
✓ Branch 1 taken 15506 times.
✗ Branch 2 not taken.
15506 MIT.begin_facet(intersections[b].f1);
621
2/2
✓ Branch 0 taken 135766 times.
✓ Branch 1 taken 15506 times.
151272 for(index_t i=b; i<e; ++i) {
622
2/2
✓ Branch 0 taken 54379 times.
✓ Branch 1 taken 81387 times.
135766 const IsectInfo& II = intersections[i];
623
624 // Each IsectInfo is either an individual vertex
625 // or a segment with two vertices.
626 // Each vertex is represented combinatorially.
627 // The MeshInTriangle knows how to compute the
628 // geometry from the combinatorial information.
629
630 if(II.is_point()) {
631 8606 MIT.add_vertex(
632
1/2
✓ Branch 1 taken 8606 times.
✗ Branch 2 not taken.
8606 II.f2,
633 II.A_rgn_f1, II.A_rgn_f2
634 );
635 } else {
636 127160 MIT.add_edge(
637 127160 II.f2,
638 127160 II.A_rgn_f1, II.A_rgn_f2,
639
1/2
✓ Branch 1 taken 127160 times.
✗ Branch 2 not taken.
127160 II.B_rgn_f1, II.B_rgn_f2
640 );
641 }
642 }
643
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15506 times.
15506 if(e-b >= monster_threshold_) {
645 ✗ index_t f = intersections[b].f1;
646 ✗ MIT.save_constraints(
647 ✗ "constraints_"+String::to_string(f)+".geogram"
648 );
649 }
650
651 // Inserts constraints
652 // and creates new vertices in shared mesh
653
1/2
✓ Branch 1 taken 15506 times.
✗ Branch 2 not taken.
15506 MIT.commit();
654
655 // For debugging, optionally save "monsters"
656 // (that is, triangles that have a huge number
657 // of intersections).
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15506 times.
15506 if(e-b >= monster_threshold_) {
659 ✗ index_t f = intersections[b].f1;
660 ✗ MIT.save(
661 ✗ "triangulation_"+String::to_string(f)+".geogram"
662 );
663 //MIT.save_constraints(
664 // "constraints_"+String::to_string(f)+".geogram"
665 //);
666 std::ofstream out(
667 ✗ "facet_"+String::to_string(f)+".obj"
668 ✗ );
669 ✗ for(index_t v: mesh_copy_.facets.vertices(f)) {
670 ✗ vec3 p = mesh_copy_.vertices.point(v);
671 ✗ out << "v " << p << std::endl;
672 }
673 ✗ out << "f ";
674 for(
675 index_t lv=0;
676 ✗ lv<mesh_copy_.facets.nb_vertices(f); ++lv
677 ) {
678 ✗ out << lv+1 << " ";
679 }
680 out << std::endl;
681 ✗ }
682
683 // Clear it so that it is clean for next triangle.
684
1/2
✓ Branch 1 taken 15506 times.
✗ Branch 2 not taken.
15506 MIT.clear();
685 }
686
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 703 times.
755 if(fine_verbose_) {
687
2/4
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
104 Logger::out("Isect")
688
2/6
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
104 << String::format("[%2d] done",int(tid))
689 << std::endl;
690 }
691 #ifdef TRIANGULATE_IN_PARALLEL
692 755 });
693 #endif
694 }
695 587 }
696
697 587 void MeshSurfaceIntersection::intersect_epilogue(
698 const vector<IsectInfo>& intersections
699 ) {
700
1/2
✓ Branch 2 taken 587 times.
✗ Branch 3 not taken.
587 Stopwatch W_epilogue("Epilogue", verbose_);
701 // Vertices coming from intersections may land exactly
702 // on an existing vertex (see #111)
703 {
704
2/4
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 587 times.
✗ Branch 5 not taken.
587 Stopwatch W("I on v", verbose_);
705
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 vector<index_t> v2v(mesh_.vertices.nb());
706
2/2
✓ Branch 0 taken 95737 times.
✓ Branch 1 taken 587 times.
96324 for(index_t v : mesh_.vertices) {
707 95737 v2v[v] = v;
708 }
709
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 parallel_for(
710 0, mesh_.vertices.nb(),
711 ✗ [&](index_t v) {
712 // If the vertex is an original vertex, not
713 // coming from an intersection, check whether
714 // it also exists as an intersection
715
2/2
✓ Branch 0 taken 70840 times.
✓ Branch 1 taken 24897 times.
95737 if(vertex_to_exact_point_[v] == nullptr) {
716 70840 const vec3& xyz = mesh_.vertices.point(v);
717 70840 ExactPoint K(xyz[0], xyz[1], xyz[2], 1.0);
718 auto it = exact_point_to_vertex_.find(K);
719
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 70835 times.
70840 if(it != exact_point_to_vertex_.end()) {
720 5 v2v[v] = it->second;
721 }
722 70840 }
723 95737 }
724 );
725
2/2
✓ Branch 0 taken 804720 times.
✓ Branch 1 taken 587 times.
805307 for(index_t c : mesh_.facet_corners) {
726 804720 index_t v = v2v[mesh_.facet_corners.vertex(c)];
727 mesh_.facet_corners.set_vertex(c, v);
728 }
729 587 }
730
731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if(interpolate_attributes_) {
732 ✗ Stopwatch W("Attrib", verbose_);
733 Attribute<index_t> original_facet_id(
734 ✗ mesh_.facets.attributes(), "original_facet_id"
735 );
736
737 ✗ for(index_t c: mesh_.facet_corners) {
738 ✗ index_t f = c/3;
739 ✗ index_t f0 = original_facet_id[f];
740 // If this is an original facet, there is nothing to do !
741 ✗ if(f == f0) {
742 ✗ continue;
743 }
744
745 ✗ index_t v = mesh_.facet_corners.vertex(c);
746 index_t v1 = mesh_.facets.vertex(f0,0);
747 index_t v2 = mesh_.facets.vertex(f0,1);
748 index_t v3 = mesh_.facets.vertex(f0,2);
749
750 index_t c1 = mesh_.facets.corner(f0,0);
751 index_t c2 = mesh_.facets.corner(f0,1);
752 index_t c3 = mesh_.facets.corner(f0,2);
753
754 ✗ if(v == v1) {
755 ✗ mesh_.facet_corners.attributes().copy_item(c,c1);
756 ✗ continue;
757 }
758
759 ✗ if(v == v2) {
760 ✗ mesh_.facet_corners.attributes().copy_item(c,c2);
761 ✗ continue;
762 }
763
764 ✗ if(v == v3) {
765 ✗ mesh_.facet_corners.attributes().copy_item(c,c3);
766 ✗ continue;
767 }
768
769 ✗ vec3 p1=mesh_.vertices.point(v1);
770 ✗ vec3 p2=mesh_.vertices.point(v2);
771 ✗ vec3 p3=mesh_.vertices.point(v3);
772 ✗ vec3 p =mesh_.vertices.point(v);
773
774 double a = Geom::triangle_area(p1,p2,p3);
775 double a1 = Geom::triangle_area(p ,p2,p3);
776 double a2 = Geom::triangle_area(p1,p ,p3);
777 double a3 = Geom::triangle_area(p1,p2,p );
778
779 ✗ mesh_.facet_corners.attributes().zero_item(c);
780 ✗ mesh_.facet_corners.attributes().madd_item(c, a1/a, c1);
781 ✗ mesh_.facet_corners.attributes().madd_item(c, a2/a, c2);
782 ✗ mesh_.facet_corners.attributes().madd_item(c, a3/a, c3);
783 }
784 ✗ }
785
786
787 // Remove original facets that have intersections.
788 {
789
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 vector<index_t> has_intersections(mesh_.facets.nb(), 0);
790
2/2
✓ Branch 0 taken 135766 times.
✓ Branch 1 taken 587 times.
136353 for(const IsectInfo& II: intersections) {
791 135766 has_intersections[II.f1] = 1;
792 135766 has_intersections[II.f2] = 1;
793 }
794
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 mesh_.facets.delete_elements(has_intersections);
795 }
796
797 // There can be duplicated facets coming from
798 // tesselated co-planar facets.
799 // Note: this updates operand_bit attribute
800
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 mesh_remove_bad_facets_no_check(mesh_);
801
802 #ifdef MESH_SURFACE_INTERSECTION_DEBUG
803 std::cerr << "Sanity check: verify that there is no degenerate triangle"
804 << std::endl;
805 // Sanity check: do we have facets with their three vertices
806 // aligned ? Normally cannot happen since we have eliminated
807 // them during intersection, but who knows ?
808 // Actually this happens sometimes...
809 {
810 Attribute<bool> selected(mesh_.facets.attributes(), "selection");
811 for(index_t t: mesh_.facets) {
812 if(PCK::aligned_3d(
813 exact_vertex(mesh_.facets.vertex(t,0)),
814 exact_vertex(mesh_.facets.vertex(t,1)),
815 exact_vertex(mesh_.facets.vertex(t,2))
816 )) {
817 selected[t] = true;
818 std::cerr << "FACET HAS 3 ALIGNED VERTICES" << std::endl;
819 } else {
820 selected[t] = false;
821 }
822
823 }
824 }
825 std::cerr << "There is no degenerate triangle" << std::endl;
826 #endif
827
828
2/2
✓ Branch 0 taken 584 times.
✓ Branch 1 taken 3 times.
587 if(use_radial_sort_) {
829
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 build_Weiler_model();
830 }
831
832
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 PCK::set_SOS_mode(SOS_bkp_);
833 587 }
834
835 587 void MeshSurfaceIntersection::intersect() {
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if(mesh_.facets.nb() == 0) {
837 ✗ return;
838 }
839
1/2
✓ Branch 2 taken 587 times.
✗ Branch 3 not taken.
587 Stopwatch W("Intersect", verbose_);
840
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 intersect_prologue();
841 vector<IsectInfo> intersections;
842
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 intersect_get_intersections(intersections);
843
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 intersect_remesh_intersections(intersections);
844
1/2
✓ Branch 1 taken 587 times.
✗ Branch 2 not taken.
587 intersect_epilogue(intersections);
845 587 }
846
847
2/2
✓ Branch 0 taken 150871 times.
✓ Branch 1 taken 728987 times.
879858 MeshSurfaceIntersection::ExactPoint MeshSurfaceIntersection::exact_vertex(
848 index_t v
849 ) const {
850 geo_debug_assert(v < mesh_.vertices.nb());
851 879858 const ExactPoint* p = vertex_to_exact_point_[v];
852
2/2
✓ Branch 0 taken 150871 times.
✓ Branch 1 taken 728987 times.
879858 if(p != nullptr) {
853 150871 return *p;
854 }
855 728987 const vec3& xyz = mesh_.vertices.point(v);
856 728987 return ExactPoint(xyz[0], xyz[1], xyz[2], 1.0);
857 }
858
859 91237 index_t MeshSurfaceIntersection::find_or_create_exact_vertex(
860 const ExactPoint& p
861 ) {
862 std::map<ExactPoint,index_t,ExactPointCompare>::iterator it;
863 bool inserted;
864
1/2
✓ Branch 2 taken 91237 times.
✗ Branch 3 not taken.
91237 std::tie(it, inserted) = exact_point_to_vertex_.insert(
865 91237 std::make_pair(p,NO_INDEX)
866 );
867
2/2
✓ Branch 0 taken 66340 times.
✓ Branch 1 taken 24897 times.
91237 if(!inserted) {
868 66340 return it->second;
869 }
870 24897 vec3 p_inexact = PCK::approximate(p);
871 24897 index_t v = mesh_.vertices.create_vertex(p_inexact.data());
872 24897 it->second = v;
873 24897 vertex_to_exact_point_[v] = &(it->first);
874 24897 return v;
875 }
876
877 /************************ Radial sort ***********************************/
878
879 3820 void MeshSurfaceIntersection::RadialSort::init(index_t h_ref) {
880 3820 degenerate_ = false;
881 3820 h_ref_ = NO_INDEX; // so that normal() computes N_ref_
882 3820 N_ref_ = normal(h_ref);
883 3820 h_ref_ = h_ref;
884 3820 }
885
886 24526 bool MeshSurfaceIntersection::RadialSort::operator()(
887 index_t h1, index_t h2
888 ) const {
889
890 // To order facets around radial edges, we use two different things:
891 //
892 // 1) a 2D coordinate system. For each halfedge h, this gives
893 // two signs. The 2D coordinate system is defined by:
894 // - a reference halfedge h_ref passed to init(h_ref)
895 // - u_sign (su): location of opposite vertex relative to facet
896 // incident to h_ref: h_orient(h_ref_, h)
897 // - v_sign (sv): dot product between the normals to the facets
898 // incident to h and incident to h_ref: h_refNorient(h)
899 // u_sign and v_sign determine 4 quadrants (in fact 8 regions if one
900 // counts when they vanish). If two halfedges land in two different
901 // regions then their order is determined. The order of the regions
902 // is stored in su_sv_to_linear_index[3][3].
903 //
904 // 2) now if two halfedges h1,h2 land in the same region, their order is
905 // given by orient3d(p1,p2,q1,q2) = horient(h1,h2) where p1,p2 are
906 // the vertices of the radial edge and q1 (resp q2) the opposite
907 // vertices of h1 (resp h2).
908
909 24526 Sign su1 = h_orient(h_ref_, h1);
910 24526 Sign su2 = h_orient(h_ref_, h2);
911
912 // Optimization: quick exit, h1 appears first if it is the
913 // only one in positive u half.
914
2/2
✓ Branch 0 taken 2088 times.
✓ Branch 1 taken 22438 times.
24526 if(su1 * su2 < 0) {
915 2088 return (su1 > 0);
916 }
917
918 22438 Sign sv1 = h_refNorient(h1);
919 22438 Sign sv2 = h_refNorient(h2);
920
921 // Map (su,sv) sign pair into a region linear index ("pseudo-angle")
922 //
923 // su
924 // ^
925 // | + 2
926 // | 3 | 1
927 // | 0 4---+---0
928 // | 5 | 7
929 // | - 6
930 // |
931 // | - 0 +
932 // *--------------> sv
933 static int su_sv_to_linear_index[3][3] = {
934 // -, 0, + <--------- su = h_orient(h_ref, h)
935 {5, 4, 3}, // - <--- sv = h_Norient(h)
936 {6,-1, 2}, // 0
937 {7, 0, 1} // +
938 };
939
940 22438 int theta1 = su_sv_to_linear_index[sv1+1][su1+1];
941 22438 int theta2 = su_sv_to_linear_index[sv2+1][su2+1];
942
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22438 times.
22438 if(theta1 == -1 || theta2 == -1) {
944 ✗ report_problem("Triangle with both zero orient and zero Norient");
945 ✗ return false;
946 }
947
948
2/2
✓ Branch 0 taken 22436 times.
✓ Branch 1 taken 2 times.
22438 if(theta1 != theta2) { // Different thetas: then we know the order
949 22436 return (theta2 > theta1);
950 }
951
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if((theta1 & 1) == 0) { // Same thetas, = to 0,2,4,6 (should not happen)
953 ✗ report_problem("Both triangles in same reference half-plane");
954 ✗ return false;
955 }
956
957 // Same thetas, both = to 1,3,5 or 7, measure relative orientation
958 2 Sign o_12 = h_orient(h1,h2);
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(o_12 == ZERO) {
960 ✗ report_problem("Both triangles in same half-plane");
961 ✗ return false;
962 }
963 2 return o_12 > 0;
964 }
965
966 49054 Sign MeshSurfaceIntersection::RadialSort::h_orient(
967 index_t h1, index_t h2
968 ) const {
969
970 // Normally we compute -orient3d(p1,p2,q1,q2) where
971 // p1,p2 denote the radial edge and q1 (resp q2) the opposite
972 // vertex to h1 (resp h2).
973 // Replacing a facet of a tetrahedron with a co-planar triangle
974 // of same orientation does not change signed volume, so instead
975 // of the facet adjacent to h1, we use the facet of the original
976 // mesh (that contains the facet adjacent to h1), that has simpler
977 // coordinates.
978
979
2/2
✓ Branch 0 taken 32422 times.
✓ Branch 1 taken 16632 times.
49054 if(h1 == h2) {
980 return ZERO;
981 }
982
1/2
✓ Branch 0 taken 32422 times.
✗ Branch 1 not taken.
32422 index_t f1 = I_.halfedges_.facet(h1);
983 index_t f2 = I_.halfedges_.facet(h2);
984 index_t w1 = I_.halfedges_.vertex(h1,2);
985 index_t w2 = I_.halfedges_.vertex(h2,2);
986
987 // Optimization (using original points as often as possible)
988 // If w1 is an original vertex, use w1 and original facet of h2
989 // (there is no minus sign, because args are swapped, we have h2 first)
990
2/2
✓ Branch 0 taken 5057 times.
✓ Branch 1 taken 27365 times.
32422 if(I_.is_original_vertex(w1)) {
991 5057 vec3 p0 = mesh_.vertices.point(w1);
992 5057 auto [q0, q1, q2] = I_.get_initial_facet_vertices(f2);
993 return Sign(PCK::orient_3d(q0,q1,q2,p0));
994 }
995
996 // Optimization (using original points as often as possible)
997 // If w2 is an original vertex, use w2 and original facet of h1
998
2/2
✓ Branch 0 taken 3654 times.
✓ Branch 1 taken 23711 times.
27365 if(I_.is_original_vertex(w2)) {
999 3654 vec3 q0 = mesh_.vertices.point(w2);
1000 3654 auto [p0, p1, p2] = I_.get_initial_facet_vertices(f1);
1001 3654 return Sign(-PCK::orient_3d(p0,p1,p2,q0));
1002 }
1003
1004 // General case: use w2 (it's an intersection point) and original
1005 // facet of h1 converted to exact points.
1006 23711 auto [p0, p1, p2] = I_.get_initial_facet_vertices(f1);
1007
1008 23711 ExactPoint pp0(p0.x, p0.y, p0.z, 1.0);
1009 23711 ExactPoint pp1(p1.x, p1.y, p1.z, 1.0);
1010 23711 ExactPoint pp2(p2.x, p2.y, p2.z, 1.0);
1011
1/2
✓ Branch 1 taken 23711 times.
✗ Branch 2 not taken.
23711 const ExactPoint& q2 = I_.exact_vertex(w2);
1012
1013
1/2
✓ Branch 1 taken 23711 times.
✗ Branch 2 not taken.
23711 return Sign(-PCK::orient_3d(pp0,pp1,pp2,q2));
1014
1015 23711 }
1016
1017 44876 Sign MeshSurfaceIntersection::RadialSort::h_refNorient(index_t h2) const {
1018
2/2
✓ Branch 0 taken 28244 times.
✓ Branch 1 taken 16632 times.
44876 if(h2 == h_ref_) {
1019 return POSITIVE;
1020 }
1021
3/4
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 28211 times.
✓ Branch 3 taken 33 times.
✗ Branch 4 not taken.
28244 static PCK::PredicateStats stats("h_refNorient");
1022 stats.log_invoke();
1023 28244 exact::vec3 N2 = normal(h2);
1024
1/2
✓ Branch 1 taken 28244 times.
✗ Branch 2 not taken.
28244 return dot(N_ref_,N2).sign();
1025 28244 }
1026
1027 32064 exact::vec3 MeshSurfaceIntersection::RadialSort::normal(index_t h) const {
1028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32064 times.
32064 if(h == h_ref_) {
1029 ✗ return N_ref_;
1030 }
1031 32064 auto [p1, p2, p3] = I_.get_initial_facet_vertices(
1032 I_.halfedges_.facet(h)
1033 32064 );
1034 exact::vec3 N = cross(
1035 64128 make_vec3<exact::vec3>(p1,p2),
1036
1/2
✓ Branch 3 taken 32064 times.
✗ Branch 4 not taken.
64128 make_vec3<exact::vec3>(p1,p3)
1037
1/2
✓ Branch 1 taken 32064 times.
✗ Branch 2 not taken.
32064 );
1038 Numeric::optimize_number_representation(N);
1039 return N;
1040 32064 }
1041
1042 ✗ void MeshSurfaceIntersection::RadialSort::report_problem(
1043 const char* message
1044 ) const {
1045 ✗ degenerate_ = true;
1046 ✗ Logger::err("RadialSort") << message << std::endl;
1047 ✗ }
1048
1049 /*****************************************************************/
1050
1051 28 void MeshSurfaceIntersection::mark_external_shell(
1052 vector<index_t>& on_external_shell
1053 ) {
1054 // Chart attribute corresponds to volumetric regions
1055
1/2
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Attribute<index_t> chart(mesh_.facets.attributes(), "chart");
1056
1057 // Get nb charts
1058 28 index_t nb_charts = 0;
1059
2/2
✓ Branch 0 taken 225074 times.
✓ Branch 1 taken 28 times.
225102 for(index_t f: mesh_.facets) {
1060 225074 nb_charts = std::max(nb_charts, chart[f]+1);
1061 }
1062
1063 // Get connected components by traversing both alpha2 and alpha3 links,
1064 // and orient facets coherently
1065 index_t nb_components = 0;
1066 vector<index_t> component(mesh_.facets.nb(), NO_INDEX);
1067 {
1068
4/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 225043 times.
✓ Branch 2 taken 225074 times.
✓ Branch 3 taken 28 times.
225102 for(index_t f:mesh_.facets) {
1069
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 225043 times.
225074 if(component[f] == NO_INDEX) {
1070 std::stack<index_t> S;
1071
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 component[f] = nb_components;
1072 S.push(f);
1073
2/2
✓ Branch 0 taken 225074 times.
✓ Branch 1 taken 31 times.
225105 while(!S.empty()) {
1074
2/2
✓ Branch 0 taken 223356 times.
✓ Branch 1 taken 1718 times.
225074 index_t f1 = S.top();
1075 S.pop();
1076
1077 {
1078
2/2
✓ Branch 0 taken 72433 times.
✓ Branch 1 taken 152641 times.
225074 index_t f2 = halfedges_.facet_alpha3(f1);
1079
2/2
✓ Branch 0 taken 72433 times.
✓ Branch 1 taken 152641 times.
225074 if(component[f2] == NO_INDEX) {
1080
2/2
✓ Branch 0 taken 71880 times.
✓ Branch 1 taken 553 times.
72433 component[f2]=component[f1];
1081 S.push(f2);
1082 }
1083 }
1084
1085
2/2
✓ Branch 0 taken 675222 times.
✓ Branch 1 taken 225074 times.
900296 for(index_t le1=0; le1<3; ++le1) {
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 675222 times.
675222 index_t f2 = mesh_.facets.adjacent(f1,le1);
1087
3/4
✓ Branch 0 taken 675222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 152610 times.
✓ Branch 3 taken 522612 times.
675222 if(f2!=NO_INDEX && component[f2]==NO_INDEX) {
1088 #ifdef GEO_DEBUG
1089 index_t le2 = mesh_.facets.find_adjacent(f2,f1);
1090 geo_debug_assert(
1091 mesh_.facets.vertex(f1,le1) !=
1092 mesh_.facets.vertex(f2,le2)
1093 );
1094 #endif
1095
2/2
✓ Branch 0 taken 151445 times.
✓ Branch 1 taken 1165 times.
152610 component[f2] = component[f1];
1096 S.push(f2);
1097 }
1098 }
1099 }
1100 }
1101 225074 ++nb_components;
1102 }
1103 }
1104
1105 // Compute the volume enclosed by each chart
1106
1107
1/4
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
28 vector<double> chart_volume(nb_charts,0.0);
1108
2/2
✓ Branch 0 taken 225074 times.
✓ Branch 1 taken 28 times.
225102 for(index_t f: mesh_.facets) {
1109 225074 vec3 p1 = mesh_.facets.point(f,0);
1110 225074 vec3 p2 = mesh_.facets.point(f,1);
1111 225074 vec3 p3 = mesh_.facets.point(f,2);
1112 225074 chart_volume[chart[f]] += dot(p1,cross(p2,p3)) / 6.0;
1113 }
1114
1115
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 28 times.
1152 for(index_t c=0; c<chart_volume.size(); ++c) {
1116 1124 chart_volume[c] = ::fabs(chart_volume[c]);
1117 }
1118
1119 // For each component, find the chart that encloses the largest
1120 // volume (it is the external boundary of the component)
1121
1122
2/6
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
28 vector<double> max_chart_volume_in_component(nb_components, 0.0);
1123 vector<index_t> chart_with_max_volume_in_component(
1124 nb_components, NO_INDEX
1125 );
1126
1127
2/2
✓ Branch 0 taken 225074 times.
✓ Branch 1 taken 28 times.
225102 for(index_t f: mesh_.facets) {
1128
2/2
✓ Branch 0 taken 108704 times.
✓ Branch 1 taken 116370 times.
225074 double V = chart_volume[chart[f]];
1129
2/2
✓ Branch 0 taken 108704 times.
✓ Branch 1 taken 116370 times.
225074 if( V >= max_chart_volume_in_component[component[f]]) {
1130 108704 max_chart_volume_in_component[component[f]] = V;
1131 108704 chart_with_max_volume_in_component[component[f]] = chart[f];
1132 }
1133 }
1134
1135
1/2
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
28 on_external_shell.resize(mesh_.facets.nb());
1136
2/2
✓ Branch 0 taken 225074 times.
✓ Branch 1 taken 28 times.
225102 for(index_t f: mesh_.facets) {
1137 225074 on_external_shell[f] = (
1138 225074 chart[f] == chart_with_max_volume_in_component[component[f]]
1139 );
1140 }
1141 28 }
1142
1143 /*************************************************************************/
1144
1145 584 void MeshSurfaceIntersection::build_Weiler_model() {
1146
1147
1/2
✓ Branch 2 taken 584 times.
✗ Branch 3 not taken.
584 Stopwatch Wweiler("Weiler",verbose_);
1148
1149 Attribute<bool> corner_is_on_border(
1150
2/4
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 584 times.
✗ Branch 5 not taken.
584 mesh_.facet_corners.attributes(), "is_on_border"
1151 );
1152
2/2
✓ Branch 0 taken 656643 times.
✓ Branch 1 taken 584 times.
657227 for(index_t c: mesh_.facet_corners) {
1153 corner_is_on_border[c] = false;
1154 }
1155
1156
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 halfedges_.initialize();
1157
1158 // Step 1: Duplicate all surfaces and create alpha3 links
1159 {
1160
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 index_t nf = mesh_.facets.nb();
1161
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 mesh_.facets.create_triangles(nf);
1162
2/2
✓ Branch 0 taken 218881 times.
✓ Branch 1 taken 584 times.
219465 for(index_t f1=0; f1<nf; ++f1) {
1163 218881 index_t f2 = f1+nf;
1164
1/2
✓ Branch 0 taken 218881 times.
✗ Branch 1 not taken.
218881 mesh_.facets.set_vertex(f2,0,mesh_.facets.vertex(f1,2));
1165 mesh_.facets.set_vertex(f2,1,mesh_.facets.vertex(f1,1));
1166 mesh_.facets.set_vertex(f2,2,mesh_.facets.vertex(f1,0));
1167
1168 // Copy attributes
1169
1/2
✓ Branch 1 taken 218881 times.
✗ Branch 2 not taken.
218881 mesh_.facets.attributes().copy_item(f2,f1);
1170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218881 times.
218881 if(interpolate_attributes_) {
1171 ✗ mesh_.facet_corners.attributes().copy_item(3*f2, 3*f1+2);
1172 ✗ mesh_.facet_corners.attributes().copy_item(3*f2+1,3*f1+1);
1173 ✗ mesh_.facet_corners.attributes().copy_item(3*f2+2,3*f1 );
1174 }
1175
1176 // Sew halfedges (need to be done *after* copy attributes,
1177 // since alpha3 is stored as an .. **attribute** !!!
1178 218881 halfedges_.sew3(3*f1, 3*f2+1);
1179 218881 halfedges_.sew3(3*f1+1,3*f2 );
1180 218881 halfedges_.sew3(3*f1+2,3*f2+2);
1181 }
1182 }
1183
1184 // Memorize flipped status for facets before classification
1185 // (that changes total number of facets). Used by get_initial_facet()
1186
2/4
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 584 times.
✗ Branch 5 not taken.
584 f_is_flipped_.bind(mesh_.facets.attributes(), "flipped");
1187
2/2
✓ Branch 0 taken 437762 times.
✓ Branch 1 taken 584 times.
438346 for(index_t f: mesh_.facets) {
1188 437762 f_is_flipped_[f] = (f >= mesh_.facets.nb() / 2);
1189 }
1190
1191 // Step 2: Clear all facet-facet links
1192
2/2
✓ Branch 0 taken 1313286 times.
✓ Branch 1 taken 584 times.
1313870 for(index_t c: mesh_.facet_corners) {
1193 mesh_.facet_corners.set_adjacent_facet(c,NO_INDEX);
1194 }
1195
1196 // Step 3: Compute halfedges bundles and radial polylines
1197 {
1198
2/4
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 584 times.
✗ Branch 5 not taken.
584 Stopwatch W("Bundles",verbose_);
1199
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 radial_bundles_.initialize();
1200 584 }
1201
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 radial_polylines_.initialize();
1202
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 584 times.
584 if(skeleton_ != nullptr) {
1204 ✗ geo_assert(!dry_run_);
1205 ✗ radial_polylines_.get_skeleton(*skeleton_, skeleton_trim_fins_);
1206 }
1207
1208 // Step 4: Connect manifold edges
1209
2/2
✓ Branch 0 taken 618424 times.
✓ Branch 1 taken 584 times.
619008 for(index_t bndl: radial_bundles_) {
1210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 618424 times.
618424 if(radial_bundles_.nb_halfedges(bndl) == 1) {
1211 // If there is only one halfedge in the radial bundle,
1212 // this is the original surface border, then we
1213 // create the "hem" by connecting the halfedge on the
1214 // border to its mate in the duplicated facet
1215 //
1216 // Note: if an object has a tiny hole, it has the effect
1217 // of creating a single shell instead of two shells with
1218 // opposite orientation, it may be a problem sometimes...
1219 index_t h = radial_bundles_.halfedge(bndl,0);
1220 halfedges_.sew2(h,halfedges_.alpha3(h));
1221 corner_is_on_border[h] = true;
1222 corner_is_on_border[halfedges_.alpha3(h)] = true;
1223
2/2
✓ Branch 0 taken 574750 times.
✓ Branch 1 taken 43674 times.
618424 } else if(radial_bundles_.nb_halfedges(bndl) == 2) {
1224 // If there are two halfedges in the radial bundle,
1225 // then it is a standard manifold edge
1226 index_t h1 = radial_bundles_.halfedge(bndl,0);
1227 index_t h2 = radial_bundles_.halfedge(bndl,1);
1228 halfedges_.sew2(h1,halfedges_.alpha3(h2));
1229 }
1230 }
1231
1232 // Step 5: get charts
1233 // After this step, charts are interconnected triangles with coherent
1234 // orientation bordered by non-manifold radial edges
1235 index_t nb_charts = 0;
1236 {
1237
2/4
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 584 times.
✗ Branch 5 not taken.
584 Stopwatch W("Charts",verbose_);
1238
2/4
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 584 times.
✗ Branch 5 not taken.
584 nb_charts = get_surface_connected_components(mesh_, "chart");
1239 584 }
1240
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 564 times.
584 if(verbose_) {
1241
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
40 Logger::out("Weiler")
1242 << "Found " << nb_charts << " charts" << std::endl;
1243 }
1244
1245 // I had before an assertion check that the number of charts is even
1246 // but I removed it:
1247 // the number of charts is not necessarily even, since there
1248 // can be "fins" (see e.g. "saturn" example that creates 5 charts).
1249
1250 // Step 6: Radial sort
1251 {
1252
2/4
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 584 times.
✗ Branch 5 not taken.
584 Stopwatch("Radial sort", verbose_);
1253
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 radial_polylines_.radial_sort();
1254 }
1255
1256 // Step 7: Create alpha2 links
1257
2/2
✓ Branch 0 taken 3820 times.
✓ Branch 1 taken 584 times.
4404 for(index_t P: radial_polylines_) {
1258
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21837 times.
✓ Branch 2 taken 21837 times.
✓ Branch 3 taken 3820 times.
25657 for(index_t bndl: radial_polylines_.bundles(P)) {
1259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21837 times.
21837 if(!radial_bundles_.is_sorted(bndl)) {
1260 ✗ continue;
1261 }
1262 index_t N = radial_bundles_.nb_halfedges(bndl);
1263
2/2
✓ Branch 0 taken 81893 times.
✓ Branch 1 taken 21837 times.
103730 for(index_t i=0; i<N; ++i) {
1264
2/2
✓ Branch 0 taken 60056 times.
✓ Branch 1 taken 21837 times.
81893 index_t i_next = (i == N-1) ? 0 : i+1;
1265
2/2
✓ Branch 0 taken 60056 times.
✓ Branch 1 taken 21837 times.
81893 index_t i_prev = (i == 0 ) ? N-1 : i-1;
1266 index_t h = radial_bundles_.halfedge(bndl,i);
1267 index_t h_next = radial_bundles_.halfedge(bndl,i_next);
1268 index_t h_prev = radial_bundles_.halfedge(bndl,i_prev);
1269 halfedges_.sew2(h,halfedges_.alpha3(h_next));
1270 halfedges_.sew2(h_prev,halfedges_.alpha3(h));
1271 }
1272 }
1273 }
1274
1275 // Step 8: Identify regions
1276 {
1277
2/8
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 584 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
584 Stopwatch W("Regions",verbose_);
1278
2/4
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 584 times.
✗ Branch 5 not taken.
584 index_t nb_regions = get_surface_connected_components(mesh_,"chart");
1279
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 564 times.
584 if(verbose_) {
1280
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
40 Logger::out("Weiler")
1281 << "Found " << nb_regions << " regions" << std::endl;
1282 }
1283 584 }
1284
1285 584 }
1286
1287
1288 /***********************************************************************/
1289
1290 584 void MeshSurfaceIntersection::RadialBundles::initialize() {
1291 // Sorted vector of halfedges
1292 // In this step, we only insert the ones such that v2 < v1
1293 // For the ones such that v1 > v2, we can deduce the information
1294 // from the first half.
1295 // Note: some of them come from the original halfedges and some of
1296 // them from the ones we just created by duplicating the faces.
1297 584 H_.reserve(mesh_.facet_corners.nb());
1298 584 H_.resize(0);
1299
2/2
✓ Branch 0 taken 1313286 times.
✓ Branch 1 taken 584 times.
1313870 for(index_t h: mesh_.facet_corners) {
1300
3/4
✓ Branch 0 taken 1313286 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 656643 times.
✓ Branch 3 taken 656643 times.
2626572 if(I_.halfedges_.vertex(h,0) < I_.halfedges_.vertex(h,1)) {
1301 H_.push_back(h);
1302 }
1303 }
1304
1305
1306 // Step 2: Lexicographic sort of the H array, so that "bundles" will be
1307 // contiguous. By "bundle", I mean the set of halfedges having the same
1308 // extremities in the same order.
1309 584 GEO::sort(
1310 584 H_.begin(), H_.end(),
1311 584 [&](index_t h1, index_t h2)->bool {
1312 geo_debug_assert(h1 < mesh_.facet_corners.nb());
1313 geo_debug_assert(h2 < mesh_.facet_corners.nb());
1314
1/2
✓ Branch 0 taken 10308728 times.
✗ Branch 1 not taken.
10308728 index_t v10 = I_.halfedges_.vertex(h1,0);
1315 index_t v20 = I_.halfedges_.vertex(h2,0);
1316
2/2
✓ Branch 0 taken 4834647 times.
✓ Branch 1 taken 5474081 times.
10308728 if(v10 < v20) {
1317 return true;
1318 }
1319
2/2
✓ Branch 0 taken 1759186 times.
✓ Branch 1 taken 3075461 times.
4834647 if(v10 > v20) {
1320 return false;
1321 }
1322 1759186 return (I_.halfedges_.vertex(h1,1) < I_.halfedges_.vertex(h2,1));
1323 }
1324 );
1325
1326 // Step 3: find "bundles" of halfedges in the sorted array. Two halfedges
1327 // are in the same bundle if they connect the same pair of vertices (in
1328 // the same order).
1329 // Halfedges of bundle B are H[b],H[b+1]...H[e-1]
1330 // where b = bndl_start[B] and e = bndl_start[B+1]
1331 // (there are bndl_start.size()-1 bundles)
1332 584 bndl_start_.resize(0);
1333
2/2
✓ Branch 0 taken 309212 times.
✓ Branch 1 taken 584 times.
619592 for(index_t b=0, e=0; b<H_.size(); b=e) {
1334 bndl_start_.push_back(b);
1335
1/2
✓ Branch 0 taken 309212 times.
✗ Branch 1 not taken.
309212 index_t v0 = I_.halfedges_.vertex(H_[b],0);
1336 index_t v1 = I_.halfedges_.vertex(H_[b],1);
1337 309212 e = b+1;
1338 309212 while(
1339
2/2
✓ Branch 0 taken 569458 times.
✓ Branch 1 taken 86601 times.
656059 e < H_.size() &&
1340
5/6
✓ Branch 0 taken 656059 times.
✓ Branch 1 taken 584 times.
✓ Branch 2 taken 656059 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 347431 times.
✓ Branch 5 taken 222027 times.
1226101 I_.halfedges_.vertex(H_[e],0) == v0 &&
1341 I_.halfedges_.vertex(H_[e],1) == v1
1342 ) {
1343 347431 ++e;
1344 }
1345 }
1346
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 584 times.
584 bndl_start_.push_back(H_.size());
1347
1348 // Step 4: construct second half of the sorted halfedges array from
1349 // the first half. Remember, at this point, H contains a mixture of
1350 // original halfedges and new ones, but they are all such that v1 < v2.
1351 // We now store all the bundles such that v1 > v2 (by "mirroring" the
1352 // initial ones, that is, traversing alpha3).
1353 {
1354 index_t bndl_start_size = bndl_start_.size();
1355
2/2
✓ Branch 0 taken 309212 times.
✓ Branch 1 taken 584 times.
309796 for(index_t bndl=0; bndl+1<bndl_start_size; ++bndl) {
1356 309212 index_t b = bndl_start_[bndl];
1357 309212 index_t e = bndl_start_[bndl+1];
1358
2/2
✓ Branch 0 taken 656643 times.
✓ Branch 1 taken 309212 times.
965855 for(index_t i=b; i<e; ++i) {
1359 656643 H_.push_back(I_.halfedges_.alpha3(H_[i]));
1360 }
1361 309212 bndl_start_.push_back(H_.size());
1362 }
1363 }
1364
1365 // Step 5: chain bundles around vertices
1366 584 v_first_bndl_.assign(mesh_.vertices.nb(), NO_INDEX);
1367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 584 times.
584 bndl_next_around_v_.assign(bndl_start_.size()-1, NO_INDEX);
1368
2/2
✓ Branch 0 taken 618424 times.
✓ Branch 1 taken 584 times.
619592 for(index_t bndl = 0; bndl < nb(); ++bndl) {
1369 // Skip regular bundles (that are inside charts),
1370 // we only need chaining along non-manifold radial polylines
1371
2/2
✓ Branch 0 taken 574750 times.
✓ Branch 1 taken 43674 times.
618424 if(nb_halfedges(bndl) == 2) {
1372 574750 continue;
1373 }
1374 43674 index_t v1 = vertex(bndl,0);
1375 43674 bndl_next_around_v_[bndl] = v_first_bndl_[v1];
1376 43674 v_first_bndl_[v1] = bndl;
1377 }
1378
1379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 584 times.
584 bndl_is_sorted_.assign(nb(), false);
1380
1381 if(!facet_chart_.is_bound()) {
1382
1/2
✓ Branch 2 taken 584 times.
✗ Branch 3 not taken.
1168 facet_chart_.bind(mesh_.facets.attributes(), "chart");
1383 }
1384
1385
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 564 times.
584 if(I_.verbose_) {
1386
1/2
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
40 Logger::out("Weiler") << "Found " << nb() << " bundles"
1387 << std::endl;
1388 }
1389 584 }
1390
1391 21837 void MeshSurfaceIntersection::RadialBundles::get_sorted_incident_charts(
1392 index_t bndl, vector<ChartPos>& chart_pos
1393 ) {
1394 21837 chart_pos.resize(0);
1395
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 81893 times.
✓ Branch 2 taken 81893 times.
✓ Branch 3 taken 21837 times.
103730 for(index_t h: halfedges(bndl)) {
1396 chart_pos.push_back(
1397 81893 std::make_pair(
1398 facet_chart_[I_.halfedges_.facet(h)], chart_pos.size()
1399 )
1400 );
1401 }
1402 21837 std::sort(
1403 chart_pos.begin(), chart_pos.end(),
1404 [](const ChartPos& a, const ChartPos& b)->bool {
1405
4/22
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 21818 times.
✓ Branch 13 taken 38238 times.
✓ Branch 14 taken 19436 times.
✓ Branch 15 taken 38238 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
117730 return a.first < b.first;
1406 }
1407 );
1408 21837 }
1409
1410 584 void MeshSurfaceIntersection::RadialPolylines::initialize() {
1411 584 B_.resize(0);
1412 584 polyline_start_.resize(0);
1413 584 polyline_start_.push_back(0);
1414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 584 times.
584 vector<bool> bndl_visited(I_.radial_bundles_.nb(),false);
1415
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 584 times.
✓ Branch 2 taken 618424 times.
✓ Branch 3 taken 584 times.
619592 for(index_t bndl: I_.radial_bundles_) {
1416
1417 // Bundle already visited, or regular internal edgge
1418
4/4
✓ Branch 0 taken 578570 times.
✓ Branch 1 taken 39854 times.
✓ Branch 2 taken 574750 times.
✓ Branch 3 taken 3820 times.
618424 if(bndl_visited[bndl] || I_.radial_bundles_.nb_halfedges(bndl)==2) {
1419 614604 continue;
1420 }
1421
1422 // Find first bundle of the poly line:
1423 // Traverse bundles on border backwards until a
1424 // non-manifold vertex is reached or until we have
1425 // looped to our starting point.
1426 index_t bndl_first = bndl;
1427 for(;;) {
1428 index_t bndl_p =
1429
1/2
✓ Branch 1 taken 14659 times.
✗ Branch 2 not taken.
14659 I_.radial_bundles_.prev_along_polyline(bndl_first);
1430
2/2
✓ Branch 0 taken 10839 times.
✓ Branch 1 taken 3820 times.
14659 if(bndl_p == NO_INDEX || bndl_p == bndl) {
1431 break;
1432 }
1433 bndl_first = bndl_p;
1434 }
1435
1436 // Traverse polyline by traversing bundles on border forward
1437 // until a non-manifold vertex is reached or until we have looped
1438 // to our starting point.
1439 3820 index_t bndl_cur = bndl_first;
1440 for(;;) {
1441 B_.push_back(bndl_cur);
1442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21837 times.
21837 bndl_visited[bndl_cur] = true;
1443
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21837 times.
✓ Branch 3 taken 21837 times.
✗ Branch 4 not taken.
43674 bndl_visited[I_.radial_bundles_.opposite(bndl_cur)] = true;
1444
1/2
✓ Branch 1 taken 21837 times.
✗ Branch 2 not taken.
21837 index_t bndl_n=I_.radial_bundles_.next_along_polyline(bndl_cur);
1445
2/2
✓ Branch 0 taken 18017 times.
✓ Branch 1 taken 3820 times.
21837 if(bndl_n == NO_INDEX || bndl_n == bndl_first) {
1446 break;
1447 }
1448 18017 bndl_cur = bndl_n;
1449 18017 }
1450
1/2
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
3820 polyline_start_.push_back(B_.size());
1451 }
1452
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 564 times.
584 if(I_.verbose_) {
1453
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
40 Logger::out("Weiler") << "Found " <<nb()<< " polylines"
1454 << std::endl;
1455 }
1456 584 }
1457
1458 ✗ void MeshSurfaceIntersection::RadialPolylines::get_skeleton(
1459 Mesh& skeleton, bool trim_fins
1460 ) {
1461 ✗ skeleton.clear();
1462 ✗ skeleton.vertices.set_dimension(3);
1463 Attribute<bool> new_v_selection(
1464 ✗ skeleton.vertices.attributes(), "selection"
1465 );
1466 Attribute<bool> v_selection(
1467 ✗ mesh_.vertices.attributes(), "selection"
1468 );
1469 ✗ vector<index_t> v_id(mesh_.vertices.nb(), NO_INDEX);
1470 ✗ for(index_t bndl: B_) {
1471 ✗ for(index_t lv=0; lv<2; ++lv) {
1472 ✗ index_t v = I_.radial_bundles_.vertex(bndl,lv);
1473 ✗ if(v_id[v] == NO_INDEX) {
1474 ✗ v_id[v] = skeleton.vertices.create_vertex(
1475 ✗ mesh_.vertices.point(v)
1476 );
1477 ✗ if(I_.radial_bundles_.nb_bundles_around_vertex(v) != 2) {
1478 v_selection[v] = true;
1479 ✗ new_v_selection[v_id[v]] = true;
1480 }
1481 }
1482 }
1483 }
1484 ✗ for(index_t polyline: *this) {
1485 ✗ for(index_t bndl: bundles(polyline)) {
1486 ✗ if(trim_fins && I_.radial_bundles_.nb_halfedges(bndl) < 3) {
1487 ✗ continue;
1488 }
1489 ✗ index_t v1 = I_.radial_bundles_.vertex(bndl,0);
1490 ✗ index_t v2 = I_.radial_bundles_.vertex(bndl,1);
1491 geo_debug_assert(v_id[v1] != NO_INDEX);
1492 geo_debug_assert(v_id[v2] != NO_INDEX);
1493 ✗ skeleton.edges.create_edge(v_id[v1], v_id[v2]);
1494 }
1495 }
1496 ✗ skeleton.vertices.remove_isolated();
1497 ✗ }
1498
1499 584 void MeshSurfaceIntersection::RadialPolylines::radial_sort() {
1500
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 564 times.
584 if(I_.verbose_) {
1501
1/2
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
40 Logger::out("Radial sort")
1502 << "Nb radial polylines:" << nb() << std::endl;
1503 }
1504
1/2
✓ Branch 2 taken 584 times.
✗ Branch 3 not taken.
584 Stopwatch W("Radial sort",I_.verbose_);
1505
1506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 584 times.
584 index_t nb_sorted = 0;
1507
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 index_t nb_to_sort = nb();
1508
1509 // For each polyline in parallel
1510
1/2
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
584 parallel_for_slice(
1511 0, nb(),
1512 1300 [&](index_t b, index_t e) {
1513 716 index_t tid = Thread::current_id();
1514 716 RadialSort RS(I_);
1515
1516 vector<RadialBundles::ChartPos> ref_chart_to_radial_id;
1517 vector<RadialBundles::ChartPos> cur_chart_to_radial_id;
1518 vector<index_t> bndl_h;
1519
1520 // P: current polyline
1521
2/2
✓ Branch 0 taken 3820 times.
✓ Branch 1 taken 716 times.
4536 for(index_t P = b; P < e; ++P) {
1522
1523 index_t bndl_ref = NO_INDEX; // reference bundle
1524 index_t N = NO_INDEX; // nb halfedges in ref bundle
1525
1526
1/2
✓ Branch 0 taken 3820 times.
✗ Branch 1 not taken.
3820 for(index_t bndl: bundles(P)) {
1527
1/2
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
3820 bool OK = I_.radial_bundles_.radial_sort(bndl, RS);
1528
1/2
✓ Branch 0 taken 3820 times.
✗ Branch 1 not taken.
3820 if(OK) {
1529
1/2
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
3820 I_.radial_bundles_.get_sorted_incident_charts(
1530 bndl, ref_chart_to_radial_id
1531 );
1532 for(
1533 index_t i=0;
1534
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 13909 times.
✓ Branch 2 taken 10089 times.
✓ Branch 3 taken 3820 times.
13909 i+1<ref_chart_to_radial_id.size(); ++i
1535 ) {
1536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10089 times.
10089 OK = OK && (
1537 10089 ref_chart_to_radial_id[i].first !=
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10089 times.
10089 ref_chart_to_radial_id[i+1].first
1539 );
1540 }
1541 }
1542
1/2
✓ Branch 0 taken 3820 times.
✗ Branch 1 not taken.
3820 if(OK) {
1543 bndl_ref = bndl;
1544 3820 N = I_.radial_bundles_.nb_halfedges(bndl);
1545 3820 break;
1546 }
1547 }
1548
1549 // Copy order to all other bundles in polyline
1550
2/2
✓ Branch 0 taken 21837 times.
✓ Branch 1 taken 3820 times.
25657 for(index_t bndl: bundles(P)) {
1551
2/2
✓ Branch 0 taken 3820 times.
✓ Branch 1 taken 18017 times.
21837 if(bndl == bndl_ref) {
1552 3820 continue;
1553 }
1554
1555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18017 times.
18017 if(I_.radial_bundles_.nb_halfedges(bndl)==1) {
1556 ✗ continue;
1557 }
1558
1559 // Necessary condition: reference bndl should be OK
1560 // and current bndl should have same nbr of halfedges
1561
2/4
✓ Branch 0 taken 18017 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18017 times.
✗ Branch 3 not taken.
18017 bool OK = (bndl_ref != NO_INDEX) && (
1562 I_.radial_bundles_.nb_halfedges(bndl)==N
1563 );
1564
1565 // Now check that charts around current bndl are the
1566 // same as charts around reference bndl. It can happen
1567 // that they differ even if N matches (example21.csg)
1568 if(OK) {
1569
1/2
✓ Branch 1 taken 18017 times.
✗ Branch 2 not taken.
18017 I_.radial_bundles_.get_sorted_incident_charts(
1570 bndl, cur_chart_to_radial_id
1571 );
1572
2/2
✓ Branch 0 taken 67984 times.
✓ Branch 1 taken 18017 times.
86001 for(index_t i=0; i<N; ++i) {
1573
1/2
✓ Branch 0 taken 67984 times.
✗ Branch 1 not taken.
67984 OK = OK && (
1574 67984 cur_chart_to_radial_id[i].first ==
1575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67984 times.
67984 ref_chart_to_radial_id[i].first
1576 );
1577 }
1578 }
1579
1580
1/2
✓ Branch 0 taken 18017 times.
✗ Branch 1 not taken.
18017 if(OK) {
1581 // Here ref_bnfl has a valid order, and bndl has
1582 // the same surrounding charts as ref_bndl,
1583 // so we can reuse the radial order computed
1584 // in ref_bndl
1585
1/2
✓ Branch 1 taken 18017 times.
✗ Branch 2 not taken.
18017 bndl_h.assign(N, NO_INDEX);
1586
2/2
✓ Branch 0 taken 67984 times.
✓ Branch 1 taken 18017 times.
86001 for(index_t i=0; i<N; ++i) {
1587 // This halfedge ...
1588 67984 index_t h = I_.radial_bundles_.halfedge(
1589 67984 bndl, cur_chart_to_radial_id[i].second
1590 );
1591 // ... goes here.
1592 67984 bndl_h[ref_chart_to_radial_id[i].second] = h;
1593 }
1594 18017 I_.radial_bundles_.set_sorted_halfedges(bndl,bndl_h);
1595 } else {
1596 // Else compute the radial sort geometrically
1597 ✗ OK = I_.radial_bundles_.radial_sort(bndl, RS);
1598 // May return !OK (if not using geogram_plus) when it
1599 // cannot sort (example_022.csg and example_024.csg)
1600 ✗ if(!OK) {
1601 std::cerr << std::endl
1602 << "FATAL ERROR: "
1603 << "Did not manage to sort a bundle"
1604 << " in Polyline of length "
1605 ✗ << nb_bundles(P)
1606 << std::endl;
1607 std::cerr
1608 << "(if you reached this point, you may"
1609 << " need geogramplus, contact TESSAEL)"
1610 << std::endl;
1611 ✗ geo_assert_not_reached;
1612 }
1613 }
1614 }
1615
2/2
✓ Branch 0 taken 1500 times.
✓ Branch 1 taken 2320 times.
3820 if(I_.fine_verbose_) {
1616 1500 ++nb_sorted;
1617
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1488 times.
1500 if(!(nb_sorted%100)) {
1618
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
24 Logger::out("Radial sort")
1619
1/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12 << String::format(
1620 "[%2d] %6d/%6d",
1621
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 int(tid),int(nb_sorted),int(nb_to_sort)
1622 )
1623 << std::endl;
1624 }
1625 }
1626 }
1627
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 667 times.
716 if(I_.fine_verbose_) {
1628
2/6
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
98 Logger::out("Radial sort")
1629
2/6
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
98 << String::format("[%2d] done",int(tid))
1630 << std::endl;
1631 }
1632 716 }
1633 );
1634 584 }
1635
1636 /***********************************************************************/
1637 }
1638
1639
1640 namespace {
1641 using namespace GEO;
1642
1643 /**
1644 * \brief Gets the position of the leftmost
1645 * bit set in a 32 bits integer
1646 * \param[in] x the integer
1647 * \return the position of the leftmost bit
1648 * set, or NO_INDEX if the specified integer
1649 * is zero.
1650 */
1651 inline index_t leftmost_bit_set(index_t x) {
1652 index_t result = NO_INDEX;
1653
2/2
✓ Branch 0 taken 17792 times.
✓ Branch 1 taken 556 times.
18348 for(index_t i=0; i<32; ++i) {
1654
2/2
✓ Branch 0 taken 1119 times.
✓ Branch 1 taken 16673 times.
17792 if((x&1) != 0) {
1655 result = i;
1656 }
1657 17792 x = x >> 1;
1658 }
1659 return result;
1660 }
1661 }
1662
1663 /***************************************************/
1664
1665 namespace GEO {
1666
1667 556 void MeshSurfaceIntersection::classify(const std::string& expr) {
1668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 556 times.
556 if(mesh_.facets.nb() == 0) {
1669 ✗ return;
1670 }
1671
1/2
✓ Branch 2 taken 556 times.
✗ Branch 3 not taken.
556 Stopwatch W("Classify", verbose_);
1672
1673 // intersect_epilogue() restored the caller's SOS mode, so the ray
1674 // casts below would break exact ties by point address (differs
1675 // between platforms/allocators). Use the lexicographic mode, as
1676 // during intersect(), and restore the caller's mode on exit.
1677 // It is necessary, because whenever component classification cannot
1678 // be done using initial vertices, one needs to create exact points
1679 // stored in temporaries, see compute_component_inclusion_bits_exact()
1680 struct SOSModeGuard {
1681
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 SOSModeGuard() : bkp(PCK::get_SOS_mode()) {
1682
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 PCK::set_SOS_mode(PCK::SOS_LEXICO);
1683 556 }
1684 556 ~SOSModeGuard() { PCK::set_SOS_mode(bkp); }
1685 PCK::SOSMode bkp;
1686 } sos_mode_guard;
1687
1688 // Takes as input a Weiler model, with duplicated interfaces,
1689 // operand bit (that indices for each triangle the set of operands
1690 // it corresponds to), volumetric alpha3 links and correct facet
1691 // adjacency links. It computes the operand_inclusion_bits attribute,
1692 // that indicates for each triangle the set of operands that contains
1693 // it, then evalues the boolean expression \p expr on all facets,
1694 // and keeps the facets on the boundary of the region where \p expr
1695 // evaluates to true.
1696
1697 // Chart attribute corresponds to volumetric regions
1698
2/4
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 556 times.
✗ Branch 5 not taken.
556 Attribute<index_t> chart(mesh_.facets.attributes(), "chart");
1699
1700 // For each facet, bit n set if facet belongs to the boundary
1701 // of operand n. There can be several bit sets if two operands
1702 // are tangent (and share facets).
1703 // For each facet pair (f, g=halfedges_.facet_alpha3(f)),
1704 // we have operand_bit[f] = operand_bit[g]
1705
2/6
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 556 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
556 Attribute<index_t> operand_bit(mesh_.facets.attributes(), "operand_bit");
1706
1707 // For each facet, bit n set if facet is inside operand n.
1708 // For each facet pair (f, g=halfedges_.facet_alpha3(f)),
1709 // we have nth-bit(operand_bit[f]) != nth-bit(operand_bit[g]),
1710 // that is, among a facet pair (f,g) on the boundary of operand n,
1711 // one of f,g is considered to be "outside" the operand, and the
1712 // other one is considered to be "inside".
1713 // The one that is considered to be "outside" is the one that belongs
1714 // to the chart that encloses the largest volume.
1715 Attribute<index_t> operand_inclusion_bits(
1716
2/6
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 556 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
556 mesh_.facets.attributes(), "operand_inclusion_bits"
1717 );
1718
1719 // Get nb charts and nb operands
1720 556 index_t nb_charts = 0;
1721 index_t nb_operands = 0;
1722
2/2
✓ Branch 0 taken 212688 times.
✓ Branch 1 taken 556 times.
213244 for(index_t f: mesh_.facets) {
1723 212688 nb_charts = std::max(nb_charts, chart[f]+1);
1724 212688 nb_operands = nb_operands | operand_bit[f];
1725 }
1726
1727
1/2
✓ Branch 0 taken 556 times.
✗ Branch 1 not taken.
556 if(nb_operands != 0) {
1728 556 nb_operands = leftmost_bit_set(nb_operands) + 1;
1729 }
1730
1731
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 550 times.
556 if(verbose_) {
1732
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
12 Logger::out("Weiler") << "nb operands=" << nb_operands << std::endl;
1733 }
1734
1735 // Get connected components, obtained by traversing all facet
1736 // adjacency links and volumetric alpha3 links
1737
1738 index_t nb_components = 0;
1739
1740 Attribute<index_t> facet_component(
1741
2/6
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 556 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
556 mesh_.facets.attributes(), "component"
1742 );
1743
1744
2/2
✓ Branch 0 taken 212688 times.
✓ Branch 1 taken 556 times.
213244 for(index_t f: mesh_.facets) {
1745 212688 facet_component[f] = NO_INDEX;
1746 }
1747 // TODO: Switch to vector when I will not need to visualize anymore
1748 // vector<index_t> facet_component(mesh_.facets.nb(), NO_INDEX);
1749
1750 // one vertex per component, can be non-manifold
1751 vector<index_t> component_origin;
1752
1753 // symbolic constant for vertex shared by several components
1754 static constexpr index_t NON_MANIFOLD = index_t(-2);
1755
1756 // one vertex per component or NON_MANIFOLD if component has no manifold
1757 // vertex (that is, only vertices shared with other components)
1758 vector<index_t> component_vertex;
1759
1760 // ith bit is 1 if component is inside ith operand
1761 vector<index_t> component_inclusion_bits;
1762
1763
1764 // one facet per component
1765 vector<index_t> component_facet;
1766
1767 // get the connected components obtained by traversing both alpha2
1768 // and alpha3 links
1769 {
1770
4/4
✓ Branch 0 taken 1565 times.
✓ Branch 1 taken 211123 times.
✓ Branch 2 taken 212688 times.
✓ Branch 3 taken 556 times.
213244 for(index_t f:mesh_.facets) {
1771
2/2
✓ Branch 0 taken 1565 times.
✓ Branch 1 taken 211123 times.
212688 if(facet_component[f] == NO_INDEX) {
1772
1773
2/4
✓ Branch 0 taken 1565 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1565 times.
✗ Branch 4 not taken.
3130 component_origin.push_back(mesh_.facets.vertex(f,0));
1774
2/4
✓ Branch 1 taken 1565 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1565 times.
✗ Branch 5 not taken.
1565 component_inclusion_bits.push_back(0);
1775
1776 std::stack<index_t> S;
1777
1/2
✓ Branch 0 taken 1565 times.
✗ Branch 1 not taken.
1565 facet_component[f] = nb_components;
1778 S.push(f);
1779
2/2
✓ Branch 0 taken 212688 times.
✓ Branch 1 taken 1565 times.
214253 while(!S.empty()) {
1780
2/2
✓ Branch 0 taken 211332 times.
✓ Branch 1 taken 1356 times.
212688 index_t f1 = S.top();
1781 S.pop();
1782 {
1783
2/2
✓ Branch 0 taken 67896 times.
✓ Branch 1 taken 144792 times.
212688 index_t f2 = halfedges_.facet_alpha3(f1);
1784 geo_debug_assert(f2 != NO_INDEX);
1785
2/2
✓ Branch 0 taken 67896 times.
✓ Branch 1 taken 144792 times.
212688 if(facet_component[f2] == NO_INDEX) {
1786
2/2
✓ Branch 0 taken 67454 times.
✓ Branch 1 taken 442 times.
67896 facet_component[f2]=facet_component[f1];
1787 S.push(f2);
1788 }
1789 }
1790
1791
2/2
✓ Branch 0 taken 638064 times.
✓ Branch 1 taken 212688 times.
850752 for(index_t le1=0; le1<3; ++le1) {
1792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 638064 times.
638064 index_t f2 = mesh_.facets.adjacent(f1,le1);
1793 if(
1794
1/2
✓ Branch 0 taken 638064 times.
✗ Branch 1 not taken.
638064 f2 != NO_INDEX &&
1795
2/2
✓ Branch 0 taken 143227 times.
✓ Branch 1 taken 494837 times.
638064 facet_component[f2] == NO_INDEX
1796 ) {
1797 #ifdef GEO_DEBUG
1798 index_t le2 = mesh_.facets.find_adjacent(f2,f1);
1799 geo_debug_assert(
1800 mesh_.facets.vertex(f1,le1) !=
1801 mesh_.facets.vertex(f2,le2)
1802 );
1803 #endif
1804
2/2
✓ Branch 0 taken 142313 times.
✓ Branch 1 taken 914 times.
143227 facet_component[f2] = facet_component[f1];
1805 S.push(f2);
1806 }
1807 }
1808 }
1809 1565 ++nb_components;
1810 }
1811 }
1812
1813
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 component_vertex.assign(nb_components, NON_MANIFOLD);
1814 component_facet.assign(nb_components, NO_INDEX);
1815
1816
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 vector<index_t> vertex_component(mesh_.vertices.nb(), NO_INDEX);
1817
1818
2/2
✓ Branch 0 taken 212688 times.
✓ Branch 1 taken 556 times.
213244 for(index_t f: mesh_.facets) {
1819 212688 index_t comp = facet_component[f];
1820 212688 component_facet[comp] = f;
1821
2/2
✓ Branch 0 taken 638064 times.
✓ Branch 1 taken 212688 times.
850752 for(index_t v: mesh_.facets.vertices(f)) {
1822
2/2
✓ Branch 0 taken 46596 times.
✓ Branch 1 taken 591468 times.
638064 if(vertex_component[v] == NO_INDEX) {
1823 46596 vertex_component[v] = comp;
1824
2/2
✓ Branch 0 taken 13311 times.
✓ Branch 1 taken 578157 times.
591468 } else if(vertex_component[v] != comp) {
1825 13311 vertex_component[v] = NON_MANIFOLD;
1826 }
1827 }
1828 }
1829
1830 // Pass 1: prefer original vertices
1831
2/2
✓ Branch 0 taken 46596 times.
✓ Branch 1 taken 556 times.
47152 for(index_t v:mesh_.vertices) {
1832 46596 index_t comp = vertex_component[v];
1833
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 46596 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
46596 geo_assert(comp != NO_INDEX);
1834 if(
1835
4/4
✓ Branch 0 taken 45609 times.
✓ Branch 1 taken 987 times.
✓ Branch 2 taken 34005 times.
✓ Branch 3 taken 11604 times.
46596 comp != NON_MANIFOLD &&
1836 is_original_vertex(v)
1837 ) {
1838 34005 component_vertex[comp] = v;
1839 }
1840 }
1841
1842 // Pass 2: treat non-manifold vertices
1843
2/2
✓ Branch 0 taken 46596 times.
✓ Branch 1 taken 556 times.
47152 for(index_t v:mesh_.vertices) {
1844 46596 index_t comp = vertex_component[v];
1845
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 46596 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
46596 geo_assert(comp != NO_INDEX);
1846 if(
1847
2/2
✓ Branch 0 taken 45609 times.
✓ Branch 1 taken 987 times.
46596 comp != NON_MANIFOLD &&
1848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45609 times.
45609 vertex_component[component_vertex[comp]] == NON_MANIFOLD
1849 ) {
1850 ✗ component_vertex[comp] = v;
1851 }
1852 }
1853
1854
2/2
✓ Branch 0 taken 1565 times.
✓ Branch 1 taken 556 times.
2121 for(index_t v: component_vertex) {
1855
1/10
✗ Branch 0 not taken.
✓ Branch 1 taken 1565 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
1565 geo_assert(v != NO_INDEX);
1856 }
1857 }
1858
1859 // Compute the volume enclosed by each chart
1860
1861
1/4
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
556 vector<double> chart_volume(nb_charts,0.0);
1862
2/2
✓ Branch 0 taken 212688 times.
✓ Branch 1 taken 556 times.
213244 for(index_t f: mesh_.facets) {
1863 // Reuse the representative vertex recorded during component discovery
1864 // as a common local origin for all charts in that component. Absolute
1865 // coordinates can corrupt the volume sign of small translated shells.
1866
1/2
✓ Branch 0 taken 212688 times.
✗ Branch 1 not taken.
212688 const vec3& origin = mesh_.vertices.point(
1867
1/2
✓ Branch 0 taken 212688 times.
✗ Branch 1 not taken.
212688 component_origin[facet_component[f]]
1868 );
1869 vec3 p1 = mesh_.facets.point(f,0) - origin;
1870 vec3 p2 = mesh_.facets.point(f,1) - origin;
1871 vec3 p3 = mesh_.facets.point(f,2) - origin;
1872 212688 chart_volume[chart[f]] += dot(p1,cross(p2,p3)) / 6.0;
1873 }
1874
1875 // For each component, find the chart that encloses the largest
1876 // volume (it is the external boundary of the component)
1877
1878
2/6
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 556 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
556 vector<double> max_chart_volume_in_component(nb_components, 0.0);
1879 vector<index_t> chart_with_max_volume_in_component(
1880 nb_components, NO_INDEX
1881 );
1882
1883
2/2
✓ Branch 0 taken 212688 times.
✓ Branch 1 taken 556 times.
213244 for(index_t f: mesh_.facets) {
1884
2/2
✓ Branch 0 taken 112937 times.
✓ Branch 1 taken 99751 times.
212688 double V = chart_volume[chart[f]];
1885 212688 if( ::fabs(V) >=
1886
2/2
✓ Branch 0 taken 112937 times.
✓ Branch 1 taken 99751 times.
212688 ::fabs(max_chart_volume_in_component[facet_component[f]])
1887 ) {
1888 112937 max_chart_volume_in_component[facet_component[f]] = V;
1889 112937 chart_with_max_volume_in_component[facet_component[f]] =
1890 chart[f];
1891 }
1892 }
1893
1894
1895 // If there is more than one component, one needs to check
1896 // whether some components "float" inside other ones.
1897 // To do that, we determine the component inclusion bits
1898 // by launching a ray from a vertex of the component, and
1899 // checking parity of the number of intersections for each operand.
1900
1901
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 25 times.
556 if(nb_components > 1) {
1902
1903 // Copy facet_component[] from mesh_ to mesh_copy_,
1904 // following original_facet_id_ links.
1905 // Note: when some input facet are overlapping and co-planar,
1906 // some original facets may become orphan (facet_component_copy_
1907 // remains NO_INDEX for those facets).
1908 // They are ignored in raytracing. This may be a problem in very
1909 // specific configurations (to be investigated).
1910 // TODO: find a way of keeping original facet <-> intersection facet
1911 // relations even when facets are merged.
1912 {
1913 Attribute<index_t> facet_component_copy(
1914
1/2
✓ Branch 1 taken 531 times.
✗ Branch 2 not taken.
1062 mesh_copy_.facets.attributes(), "component"
1915 );
1916
2/2
✓ Branch 0 taken 19178 times.
✓ Branch 1 taken 531 times.
19709 for(index_t f: mesh_copy_.facets) {
1917 19178 facet_component_copy[f] = NO_INDEX;
1918 }
1919
2/2
✓ Branch 0 taken 40532 times.
✓ Branch 1 taken 531 times.
41063 for(index_t f: mesh_.facets) {
1920 40532 index_t original_f = original_facet_id_[f];
1921 40532 index_t component = facet_component[f];
1922 40532 facet_component_copy[original_f] = component;
1923 }
1924 }
1925
1926
1927
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 525 times.
531 if(verbose_) {
1928
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
12 Logger::out("Weiler") << "Classifying " << nb_components
1929 << " components using ray tracing"
1930 << std::endl;
1931 }
1932
1/2
✓ Branch 1 taken 531 times.
✗ Branch 2 not taken.
531 parallel_for(
1933 2071 0, nb_components, [&](index_t component) {
1934
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1537 times.
1540 index_t v = component_vertex[component];
1935
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1537 times.
1540 if(v == NON_MANIFOLD) {
1936 // If component has only non-manifold vertices (shared
1937 // with other components), for classifying the component
1938 // we start raytracing from the barycenter of the first
1939 // facet of the component.
1940
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 index_t f = component_facet[component];
1941 ExactPoint p = triangle_barycenter(
1942
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
9 exact_vertex(mesh_.facets.vertex(f,0)),
1943
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
9 exact_vertex(mesh_.facets.vertex(f,1)),
1944
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
9 exact_vertex(mesh_.facets.vertex(f,2))
1945
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 );
1946 Numeric::optimize_number_representation(p);
1947 3 component_inclusion_bits[component] =
1948
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 compute_component_inclusion_bits_exact(
1949 component, p
1950 );
1951 3 } else {
1952 1537 component_inclusion_bits[component] =
1953 1537 compute_component_inclusion_bits(component, v);
1954 }
1955 1540 }
1956 );
1957
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 525 times.
531 if(verbose_) {
1958
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
12 Logger::out("Weiler") << "Done." << std::endl;
1959 }
1960 }
1961
1962 // Compute operand inclusion bits for each facet,
1963 // by propagating component's inclusion bits
1964 // from component's external shell
1965
1966 {
1967
2/4
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 556 times.
✗ Branch 5 not taken.
556 vector<index_t> visited(mesh_.facets.nb(), false);
1968 std::stack<index_t> S;
1969
1970
4/4
✓ Branch 0 taken 68152 times.
✓ Branch 1 taken 144536 times.
✓ Branch 2 taken 212688 times.
✓ Branch 3 taken 556 times.
213244 for(index_t f: mesh_.facets) {
1971 212688 if(chart[f] ==
1972
2/2
✓ Branch 0 taken 68152 times.
✓ Branch 1 taken 144536 times.
212688 chart_with_max_volume_in_component[facet_component[f]]
1973 ) {
1974 68152 visited[f] = 1;
1975
2/2
✓ Branch 0 taken 67777 times.
✓ Branch 1 taken 375 times.
68152 operand_inclusion_bits[f] =
1976
2/2
✓ Branch 0 taken 67777 times.
✓ Branch 1 taken 375 times.
68152 component_inclusion_bits[facet_component[f]];
1977 S.push(f);
1978 }
1979 }
1980
1981
2/2
✓ Branch 0 taken 212688 times.
✓ Branch 1 taken 556 times.
213244 while(!S.empty()) {
1982
2/2
✓ Branch 0 taken 211335 times.
✓ Branch 1 taken 1353 times.
212688 index_t f1 = S.top();
1983 S.pop();
1984 {
1985
2/2
✓ Branch 0 taken 25266 times.
✓ Branch 1 taken 187422 times.
212688 index_t f2 = halfedges_.facet_alpha3(f1);
1986
2/2
✓ Branch 0 taken 25266 times.
✓ Branch 1 taken 187422 times.
212688 if(f2 != NO_INDEX && !visited[f2]) {
1987
2/2
✓ Branch 0 taken 25076 times.
✓ Branch 1 taken 190 times.
25266 visited[f2] = true;
1988 S.push(f2);
1989 25266 operand_inclusion_bits[f2] =
1990 25266 operand_inclusion_bits[f1] ^ operand_bit[f1];
1991 }
1992 }
1993
2/2
✓ Branch 0 taken 638064 times.
✓ Branch 1 taken 212688 times.
850752 for(index_t le=0; le<3; ++le) {
1994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 638064 times.
638064 index_t f2 = mesh_.facets.adjacent(f1,le);
1995
3/4
✓ Branch 0 taken 638064 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 119270 times.
✓ Branch 3 taken 518794 times.
638064 if(f2 != NO_INDEX && !visited[f2]) {
1996
2/2
✓ Branch 0 taken 118482 times.
✓ Branch 1 taken 788 times.
119270 visited[f2] = true;
1997 S.push(f2);
1998 119270 operand_inclusion_bits[f2] = operand_inclusion_bits[f1];
1999 }
2000 }
2001 }
2002 }
2003
2004 // Classify facets based on ther operand inclusion bits and on the
2005 // boolean expression
2006
2007
1/4
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
556 vector<index_t> classify_facet(mesh_.facets.nb(), 0);
2008
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 547 times.
556 if(expr == "intersection") {
2009 // If operation is an intersection, return the neighbors of
2010 // the facets that have all their operand inclusion bit sets.
2011 9 index_t all_bits_set = (1u << nb_operands)-1u;
2012
2/2
✓ Branch 0 taken 81288 times.
✓ Branch 1 taken 9 times.
81297 for(index_t f: mesh_.facets) {
2013 bool flipped =
2014
1/2
✓ Branch 0 taken 81288 times.
✗ Branch 1 not taken.
81288 (max_chart_volume_in_component[facet_component[f]] < 0.0);
2015
1/2
✓ Branch 0 taken 81288 times.
✗ Branch 1 not taken.
81288 if(flipped) {
2016 81288 classify_facet[f] =
2017 81288 (operand_inclusion_bits[f] == all_bits_set);
2018 } else {
2019 ✗ classify_facet[f] =
2020 (
2021 ✗ operand_inclusion_bits[halfedges_.facet_alpha3(f)] ==
2022 all_bits_set
2023 );
2024 }
2025 }
2026 } else {
2027
2028
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 6 taken 547 times.
✗ Branch 7 not taken.
1094 BooleanExpression E(expr == "union" ? "*" : expr);
2029
2030 // For a general operation, return the facets f for which the
2031 // expression evaluates to false on f and to true on the neighbors
2032 // of f. Rember: what we want to compute is the *boundary* of the
2033 // region defined by the boolean expression, that is, the facets
2034 // for which the result of the boolean expression changes when they
2035 // are traversed by alpha3.
2036
2037 131400 auto classify = [&](index_t f)->bool {
2038 bool flipped =
2039
2/2
✓ Branch 0 taken 113592 times.
✓ Branch 1 taken 17808 times.
131400 (max_chart_volume_in_component[facet_component[f]]<0.0);
2040 131400 index_t f_in_sets = operand_inclusion_bits[f];
2041 index_t g_in_sets = operand_inclusion_bits[
2042
2/2
✓ Branch 0 taken 113592 times.
✓ Branch 1 taken 17808 times.
131400 halfedges_.facet_alpha3(f)
2043 131400 ];
2044 return flipped
2045
5/6
✓ Branch 0 taken 113592 times.
✓ Branch 1 taken 17808 times.
✓ Branch 3 taken 28198 times.
✓ Branch 4 taken 85394 times.
✓ Branch 6 taken 28198 times.
✗ Branch 7 not taken.
131400 ? (E(f_in_sets) && !E(g_in_sets))
2046
3/4
✓ Branch 1 taken 4488 times.
✓ Branch 2 taken 13320 times.
✓ Branch 4 taken 4488 times.
✗ Branch 5 not taken.
17808 : (E(g_in_sets) && !E(f_in_sets));
2047 547 };
2048
2049 try {
2050
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 539 times.
547 if(mesh_.facets.nb() > 1024) {
2051
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 parallel_for(
2052 8 0, mesh_.facets.nb(), [&](index_t f) {
2053 88700 classify_facet[f] = classify(f);
2054 }
2055 );
2056 } else {
2057
2/2
✓ Branch 0 taken 42700 times.
✓ Branch 1 taken 539 times.
43239 for(index_t f: mesh_.facets) {
2058
1/2
✓ Branch 1 taken 42700 times.
✗ Branch 2 not taken.
42700 classify_facet[f] = classify(f);
2059 }
2060 }
2061 ✗ } catch(...) {
2062 ✗ }
2063 }
2064
2065 // invert classification (we mark the facets that
2066 // we want to delete)
2067
2/2
✓ Branch 0 taken 212688 times.
✓ Branch 1 taken 556 times.
213244 for(index_t f: mesh_.facets) {
2068 212688 classify_facet[f] = 1u - classify_facet[f];
2069 }
2070
2071
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 mesh_.facets.delete_elements(classify_facet);
2072 //remove_fins();
2073
1/2
✓ Branch 1 taken 556 times.
✗ Branch 2 not taken.
556 mesh_.facets.connect();
2074
2075
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 550 times.
556 if(verbose_) {
2076
2/6
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
12 Logger::out("Weiler") << "Facets classified" << std::endl;
2077 }
2078 556 }
2079
2080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1537 times.
1537 index_t MeshSurfaceIntersection::compute_component_inclusion_bits(
2081 index_t component, index_t v
2082 ) {
2083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1537 times.
1537 if(!is_original_vertex(v)) {
2084 ✗ return compute_component_inclusion_bits_exact(
2085 ✗ component, exact_vertex(v)
2086 );
2087 }
2088
2089 Attribute<index_t> operand_bit_copy(
2090 1537 mesh_copy_.facets.attributes(), "operand_bit"
2091 );
2092
2093 Attribute<index_t> facet_component_copy(
2094
1/4
✓ Branch 1 taken 1537 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1537 mesh_copy_.facets.attributes(), "component"
2095 );
2096
2097 index_t component_inclusion_bits = 0;
2098
2099 1537 vec3 q1 = mesh_.vertices.point(v);
2100 3074 vec3 q2{q1.x, q1.y, q1.z + 1e6};
2101
2102
2/2
✓ Branch 0 taken 56938 times.
✓ Branch 1 taken 1537 times.
58475 for(index_t f: mesh_copy_.facets) {
2103
2104 // TODO: understand when it can happen (probably when merging
2105 // overlapping facets, one of the merged facets is no longer seen
2106 // by the mesh_ -> mesh_copy_ original_facet relation).
2107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56938 times.
56938 if(facet_component_copy[f] == NO_INDEX) {
2108 ✗ continue;
2109 }
2110
2111
2/2
✓ Branch 0 taken 19142 times.
✓ Branch 1 taken 37796 times.
56938 if(facet_component_copy[f] == component) {
2112 19142 continue;
2113 }
2114
2115
1/2
✓ Branch 0 taken 37796 times.
✗ Branch 1 not taken.
37796 index_t facet_operand_bits = operand_bit_copy[f];
2116 const vec3& p1 = mesh_copy_.facets.point(f,0);
2117 const vec3& p2 = mesh_copy_.facets.point(f,1);
2118 const vec3& p3 = mesh_copy_.facets.point(f,2);
2119
3/4
✓ Branch 1 taken 37796 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37485 times.
✓ Branch 4 taken 311 times.
37796 if(segment_triangle_intersection_SOS(q1,q2,p1,p2,p3)) {
2120 311 component_inclusion_bits ^= facet_operand_bits;
2121 }
2122 }
2123
2124
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1507 times.
1537 if(verbose_) {
2125
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 std::bitset<sizeof(index_t)*8> B(component_inclusion_bits);
2126
2/6
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
60 Logger::out("Weiler") << " component " << component
2127
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 << ":" << B << std::endl;
2128 }
2129
2130 return component_inclusion_bits;
2131 }
2132
2133 3 index_t MeshSurfaceIntersection::compute_component_inclusion_bits_exact(
2134 index_t component, const ExactPoint& q1
2135 ) {
2136 Attribute<index_t> operand_bit_copy(
2137 3 mesh_copy_.facets.attributes(), "operand_bit"
2138 );
2139
2140 Attribute<index_t> facet_component_copy(
2141
1/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
3 mesh_copy_.facets.attributes(), "component"
2142 );
2143
2144 index_t component_inclusion_bits = 0;
2145
2146 3 ExactPoint q2 = q1;
2147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if(q2.w.is_one()) {
2148 ✗ q2.z += exact::scalar(1e6);
2149 } else {
2150
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 q2.z += (q2.w * exact::scalar(1e6));
2151 }
2152
2153
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 3 times.
327 for(index_t f: mesh_copy_.facets) {
2154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
324 if(facet_component_copy[f] == NO_INDEX) {
2155 36 continue;
2156 }
2157
2158
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 288 times.
324 if(facet_component_copy[f] == component) {
2159 36 continue;
2160 }
2161
2162
1/2
✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
288 index_t facet_operand_bits = operand_bit_copy[f];
2163 288 ExactPoint p1(mesh_copy_.facets.point(f,0));
2164 288 ExactPoint p2(mesh_copy_.facets.point(f,1));
2165 288 ExactPoint p3(mesh_copy_.facets.point(f,2));
2166
2/4
✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 288 times.
288 if(segment_triangle_intersection_SOS(q1,q2,p1,p2,p3)) {
2167 ✗ component_inclusion_bits ^= facet_operand_bits;
2168 }
2169 288 }
2170
2171
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(verbose_) {
2172
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 std::bitset<sizeof(index_t)*8> B(component_inclusion_bits);
2173
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 Logger::out("Weiler") << " component " << component
2174
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 << ":" << B << std::endl;
2175 }
2176
2177 3 return component_inclusion_bits;
2178 3 }
2179
2180 /*************************************************************************/
2181
2182 575 void MeshSurfaceIntersection::simplify_coplanar_facets(
2183 double angle_tolerance
2184 ) {
2185
2/2
✓ Branch 0 taken 445 times.
✓ Branch 1 taken 130 times.
575 if(mesh_.facets.nb() == 0) {
2186 130 return;
2187 }
2188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445 times.
445 if(interpolate_attributes_) {
2189 ✗ Logger::warn("Intersect")
2190 << "Cannot simplify coplanar facets with interpolated attributes"
2191 << std::endl;
2192 ✗ return;
2193 }
2194
2195
1/2
✓ Branch 2 taken 445 times.
✗ Branch 3 not taken.
445 Stopwatch W("Coplanar",verbose_);
2196
2/4
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 445 times.
✗ Branch 5 not taken.
890 Attribute<index_t> facet_group(mesh_.facets.attributes(), "group");
2197 vector<index_t> group_facet; // one facet per group
2198
2/2
✓ Branch 0 taken 132482 times.
✓ Branch 1 taken 445 times.
132927 for(index_t f: mesh_.facets) {
2199 132482 facet_group[f] = NO_INDEX;
2200 }
2201
2/6
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 445 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
445 Attribute<bool> keep_vertex(mesh_.vertices.attributes(), "keep");
2202
2/2
✓ Branch 0 taken 67384 times.
✓ Branch 1 taken 445 times.
67829 for(index_t v: mesh_.vertices) {
2203 keep_vertex[v] = false;
2204 }
2205 445 index_t current_group = 0;
2206 {
2207 // clear attributes -------------v
2208
1/2
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
445 CoplanarFacets coplanar(*this, true, angle_tolerance);
2209
4/4
✓ Branch 0 taken 77581 times.
✓ Branch 1 taken 54901 times.
✓ Branch 2 taken 132482 times.
✓ Branch 3 taken 445 times.
132927 for(index_t f: mesh_.facets) {
2210
2/2
✓ Branch 0 taken 77581 times.
✓ Branch 1 taken 54901 times.
132482 if(facet_group[f] == NO_INDEX) {
2211
1/2
✓ Branch 1 taken 77581 times.
✗ Branch 2 not taken.
77581 coplanar.get(f, current_group); // This sets facet_group_[f]
2212
1/2
✓ Branch 1 taken 77581 times.
✗ Branch 2 not taken.
77581 coplanar.mark_vertices_to_keep();
2213 group_facet.push_back(f);
2214 77581 ++current_group;
2215 }
2216 }
2217 445 }
2218
2219
1/4
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
445 vector<index_t> remove_f(mesh_.facets.nb(), 0);
2220 445 index_t nb_groups = current_group;
2221 geo_debug_assert(nb_groups == group_facet.size());
2222
2223 // Avoid to have reallocations in parallel with access by
2224 // preallocating facets (we are going to create a maximum
2225 // nb of facets that corresponds to the actual nb of facets)
2226
1/2
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
445 mesh_.facets.reserve(mesh_.facets.nb());
2227
2228 // Triangulate coplanar facet groups in parallel
2229
1/2
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
445 Process::spinlock lock = GEOGRAM_SPINLOCK_INIT;
2230
1/2
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
445 parallel_for_slice(
2231 2225 0, nb_groups, [&](index_t b, index_t e) {
2232 // do not clear attributes -----v
2233 1780 CoplanarFacets coplanar(*this,false,angle_tolerance);
2234
2235
2/2
✓ Branch 0 taken 77581 times.
✓ Branch 1 taken 1780 times.
79361 for(index_t group=b; group<e; ++group) {
2236
1/2
✓ Branch 1 taken 77581 times.
✗ Branch 2 not taken.
77581 coplanar.get(group_facet[group],group);
2237
2238
2/2
✓ Branch 0 taken 65020 times.
✓ Branch 1 taken 12561 times.
77581 if(coplanar.nb_facets() < 2) {
2239 65020 continue;
2240 }
2241
2242
1/2
✓ Branch 1 taken 12561 times.
✗ Branch 2 not taken.
12561 coplanar.triangulate();
2243
2244 bool OK = true;
2245
2/2
✓ Branch 0 taken 36532 times.
✓ Branch 1 taken 12561 times.
49093 for(index_t t=0; t<coplanar.CDT.nT(); ++t) {
2246 // If one of v1,v2,v3 is NO_INDEX,
2247 // it means that v1,v2 or v3 was one of the four
2248 // vertices of the external quad.
2249 // It means that there was probably an
2250 // inside/outside classification error.
2251
2252 index_t v1=coplanar.CDT.vertex_id(coplanar.CDT.Tv(t,0));
2253 index_t v2=coplanar.CDT.vertex_id(coplanar.CDT.Tv(t,1));
2254 index_t v3=coplanar.CDT.vertex_id(coplanar.CDT.Tv(t,2));
2255 36532 OK = OK && (v1 != NO_INDEX);
2256 36532 OK = OK && (v2 != NO_INDEX);
2257 36532 OK = OK && (v3 != NO_INDEX);
2258 }
2259
2260
1/2
✓ Branch 0 taken 12561 times.
✗ Branch 1 not taken.
12561 if(OK) {
2261 12561 coplanar.mark_facets(remove_f);
2262 12561 Process::acquire_spinlock(lock);
2263
2/2
✓ Branch 0 taken 36532 times.
✓ Branch 1 taken 12561 times.
85625 for(index_t t=0; t<coplanar.CDT.nT(); ++t) {
2264 index_t v1=
2265 coplanar.CDT.vertex_id(coplanar.CDT.Tv(t,0));
2266 index_t v2=
2267 coplanar.CDT.vertex_id(coplanar.CDT.Tv(t,1));
2268 index_t v3=
2269 coplanar.CDT.vertex_id(coplanar.CDT.Tv(t,2));
2270 index_t new_f =
2271
1/2
✓ Branch 1 taken 36532 times.
✗ Branch 2 not taken.
36532 mesh_.facets.create_triangle(v1,v2,v3);
2272 36532 facet_group[new_f] = current_group;
2273 }
2274 12561 Process::release_spinlock(lock);
2275 }
2276 }
2277 1780 }
2278 );
2279
2280 // Delete temporary attributes
2281
1/2
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
445 facet_group.destroy();
2282
1/2
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
445 keep_vertex.destroy();
2283
2284
1/4
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
445 remove_f.resize(mesh_.facets.nb(),0);
2285
1/2
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
445 mesh_.facets.delete_elements(remove_f);
2286
1/2
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
445 mesh_.facets.connect();
2287 445 }
2288
2289
2290
2291 }
2292
2293 /******************************************************************************/
2294
2295 namespace {
2296 using namespace GEO;
2297
2298 1036 void copy_operand(Mesh& result, const Mesh& operand, index_t operand_id) {
2299 Attribute<index_t> operand_bit(
2300
1/2
✓ Branch 2 taken 1036 times.
✗ Branch 3 not taken.
2072 result.facets.attributes(), "operand_bit"
2301 );
2302 index_t v_ofs = result.vertices.create_vertices(operand.vertices.nb());
2303
2/2
✓ Branch 0 taken 12040 times.
✓ Branch 1 taken 1036 times.
13076 for(index_t v: operand.vertices) {
2304 12040 result.vertices.point(v + v_ofs) = operand.vertices.point(v);
2305 }
2306
2/2
✓ Branch 0 taken 18060 times.
✓ Branch 1 taken 1036 times.
19096 for(index_t f1: operand.facets) {
2307 index_t N = operand.facets.nb_vertices(f1);
2308
1/2
✓ Branch 1 taken 18060 times.
✗ Branch 2 not taken.
18060 index_t f2 = result.facets.create_polygon(N);
2309
2/2
✓ Branch 0 taken 54180 times.
✓ Branch 1 taken 18060 times.
72240 for(index_t lv=0; lv<N; ++lv) {
2310
1/2
✓ Branch 0 taken 54180 times.
✗ Branch 1 not taken.
54180 result.facets.set_vertex(
2311 f2,lv,operand.facets.vertex(f1,lv) + v_ofs
2312 );
2313 }
2314 18060 operand_bit[f2] = index_t(1) << operand_id;
2315 }
2316 1036 }
2317 }
2318
2319 namespace GEO {
2320
2321 518 void mesh_boolean_operation(
2322 Mesh& result, const Mesh& A, const Mesh& B,
2323 const std::string& operation,
2324 MeshBooleanOperationFlags flags
2325 ) {
2326 bool verbose = ((flags & MESH_BOOL_OPS_VERBOSE) != 0);
2327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if(&result == &A) {
2328 Attribute<index_t> operand_bit(
2329 ✗ result.facets.attributes(), "operand_bit"
2330 );
2331 ✗ for(index_t f: A.facets) {
2332 ✗ operand_bit[f] = index_t(1);
2333 }
2334 ✗ copy_operand(result,B,1);
2335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 } else if(&result == &B) {
2336 ✗ mesh_boolean_operation(
2337 ✗ result, B, A, (operation=="A-B") ? "B-A" : operation, verbose
2338 );
2339 ✗ return;
2340 } else {
2341 518 result.clear();
2342 518 result.vertices.set_dimension(3);
2343 518 copy_operand(result,A,0);
2344 518 copy_operand(result,B,1);
2345 }
2346 518 MeshSurfaceIntersection I(result);
2347 I.set_radial_sort(true);
2348 I.set_verbose(verbose);
2349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if((flags & MESH_BOOL_OPS_ATTRIBS) != 0) {
2350 I.set_interpolate_attributes(true);
2351 }
2352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if((flags & MESH_BOOL_OPS_NO_CHECK_NEIGHBORS) != 0) {
2353 I.set_detect_intersecting_neighbors(false);
2354 }
2355
1/2
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
518 I.intersect();
2356
1/2
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
518 I.classify(operation);
2357
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518 if(
2358
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518 (flags & MESH_BOOL_OPS_ATTRIBS) == 0 &&
2359 (flags & MESH_BOOL_OPS_NO_SIMPLIFY) == 0
2360 ) {
2361
1/2
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
518 I.simplify_coplanar_facets();
2362 }
2363 518 }
2364
2365 1 void mesh_remove_intersections(Mesh& M, index_t max_iter, bool verbose) {
2366 // TODO: same as tet_meshing() (compute union) ?
2367
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for(index_t k=0; k<max_iter; ++k) {
2368 3 MeshSurfaceIntersection I(M);
2369 I.set_radial_sort(false);
2370 I.set_verbose(verbose);
2371
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 I.intersect();
2372
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 mesh_repair(M);
2373 3 }
2374 1 }
2375
2376 ✗ bool mesh_facets_have_intersection(Mesh& M, index_t f1, index_t f2) {
2377 ✗ for(auto [ p1, p2, p3] : M.facets.triangle_points(f1)) {
2378 ✗ for(auto [ q1, q2, q3] : M.facets.triangle_points(f2)) {
2379 ✗ if(triangles_intersections(p1,p2,p3,q1,q2,q3)) {
2380 ✗ return true;
2381 }
2382 }
2383 }
2384 ✗ return false;
2385 }
2386 }
2387