GCC Code Coverage Report


Directory: ./
File: mesh/triangle_intersection.cpp
Date: 2026-11-30 06:00:29
Exec Total Coverage
Lines: 413 463 89.2%
Functions: 23 27 85.2%
Branches: 346 733 47.2%

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/triangle_intersection.h>
41 #include <geogram/numerics/predicates.h>
42 #include <geogram/numerics/exact_geometry.h>
43 #include <geogram/basic/string.h>
44 #include <geogram/basic/geometry.h>
45 #include <geogram/basic/argused.h>
46 #include <geogram/basic/logger.h>
47 #include <geogram/basic/algorithm.h>
48
49
50 #ifdef GEO_COMPILER_CLANG
51 #pragma GCC diagnostic ignored "-Wswitch-enum"
52 #endif
53
54 // Uncomment to activate debug messages
55 //#define TT_DEBUG
56
57 namespace {
58
59 using namespace GEO;
60
61 /**
62 * \brief Internal implementation class for
63 * triangles_intersections()
64 * \details Keeps the coordinates of the 2x3
65 * vertices of the triangles and a reference
66 * to the result symbolic information
67 */
68 class TriangleTriangleIntersection {
69 public:
70
71 ✗ TriangleTriangleIntersection(
72 const vec3& p0, const vec3& p1, const vec3& p2,
73 const vec3& q0, const vec3& q1, const vec3& q2,
74 TriangleIsects* result = nullptr
75 ✗ ) : result_(result) {
76 ✗ p_[0] = p0;
77 ✗ p_[1] = p1;
78 ✗ p_[2] = p2;
79 ✗ p_[3] = q0;
80 ✗ p_[4] = q1;
81 ✗ p_[5] = q2;
82 ✗ o3d_cached_ = 0;
83 ✗ has_non_degenerate_intersection_ = false;
84 ✗ for(index_t i=0; i<6; ++i) {
85 ✗ p_index_[i] = NO_INDEX;
86 }
87 ✗ has_global_indices_ = false;
88 ✗ flags_ = TRIANGLES_INTERSECTION_DEFAULT;
89 ✗ }
90
91 1099470 TriangleTriangleIntersection(
92 const vec3& p0, const vec3& p1, const vec3& p2,
93 const vec3& q0, const vec3& q1, const vec3& q2,
94 index_t p0_index,
95 index_t p1_index,
96 index_t p2_index,
97 index_t q0_index,
98 index_t q1_index,
99 index_t q2_index,
100 TriangleIsects* result = nullptr
101
2/2
✓ Branch 1 taken 6596820 times.
✓ Branch 2 taken 1099470 times.
7696290 ) : result_(result) {
102 1099470 p_[0] = p0;
103 1099470 p_[1] = p1;
104 1099470 p_[2] = p2;
105 1099470 p_[3] = q0;
106 1099470 p_[4] = q1;
107 1099470 p_[5] = q2;
108 1099470 o3d_cached_ = 0;
109 1099470 has_non_degenerate_intersection_ = false;
110 1099470 p_index_[0] = p0_index;
111 1099470 p_index_[1] = p1_index;
112 1099470 p_index_[2] = p2_index;
113 1099470 p_index_[3] = q0_index;
114 1099470 p_index_[4] = q1_index;
115 1099470 p_index_[5] = q2_index;
116 1099470 has_global_indices_ = true;
117 1099470 flags_ = TRIANGLES_INTERSECTION_DEFAULT;
118 1099470 }
119
120 1099470 void set_flags(TriangleIsectFlags flags) {
121 1099470 flags_ = flags;
122 1099470 }
123
124 1099470 void compute() {
125 #ifdef TT_DEBUG
126 Logger::out("TT") << "Call compute()" << std::endl;
127 #endif
128
129
1/2
✓ Branch 0 taken 1099470 times.
✗ Branch 1 not taken.
1099470 if(result_ != nullptr) {
130 1099470 result_->resize(0);
131 }
132
133 1099470 bool test_degenerate = (
134 1099470 (flags_ & TRIANGLES_INTERSECTION_NO_DEGENERATE) == 0
135 );
136
137 1099470 bool test_bbox = (
138 1099470 (flags_ & TRIANGLES_INTERSECTION_NO_BBOX) == 0
139 );
140
141 // Test for degenerate triangles
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1099470 times.
1099470 if(test_degenerate) {
143 ✗ if(
144 ✗ triangle_dim(T1_RGN_P0, T1_RGN_P1, T1_RGN_P2) != 2 ||
145 ✗ triangle_dim(T2_RGN_P0, T2_RGN_P1, T2_RGN_P2) != 2
146 ) {
147 /*
148 Logger::warn("PCK")
149 << "Tri tri intersect: degenerate triangle "
150 << std::endl;
151 */
152 ✗ return;
153 }
154 } else {
155 // If client code says there is no degenerate triangle,
156 // double check in debug mode.
157
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1099470 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1099470 geo_debug_assert(
158 triangle_dim(T1_RGN_P0, T1_RGN_P1, T1_RGN_P2) == 2
159 );
160
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1099470 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1099470 geo_debug_assert(
161 triangle_dim(T2_RGN_P0, T2_RGN_P1, T2_RGN_P2) == 2
162 );
163 }
164
165 // test for disjoint bounding boxes if not specified otherwise
166 // in TriangleIsectFlags (for instance, if caller already knows
167 // that bbox intersect, as when using an AABB tree or Zomorodian
168 // et.al's algorithm).
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1099470 times.
1099470 if(test_bbox) {
170 ✗ for(index_t c = 0; c < 3; ++c) {
171 ✗ double pmin = std::min({p_[0][c], p_[1][c], p_[2][c]});
172 ✗ double pmax = std::max({p_[0][c], p_[1][c], p_[2][c]});
173 ✗ double qmin = std::min({p_[3][c], p_[4][c], p_[5][c]});
174 ✗ double qmax = std::max({p_[3][c], p_[4][c], p_[5][c]});
175 ✗ if(pmax < qmin || qmax < pmin) return;
176 }
177 }
178
179
1/2
✓ Branch 0 taken 1099470 times.
✗ Branch 1 not taken.
1099470 if(has_global_indices_) {
180
181 1099470 index_t q_index[3] = {
182 NO_INDEX, NO_INDEX, NO_INDEX
183 };
184
185
2/2
✓ Branch 0 taken 3298410 times.
✓ Branch 1 taken 1099470 times.
4397880 for(index_t i=0; i<3; ++i) {
186
2/2
✓ Branch 0 taken 9895230 times.
✓ Branch 1 taken 3298410 times.
13193640 for(index_t j=0; j<3; ++j) {
187
2/2
✓ Branch 0 taken 1002669 times.
✓ Branch 1 taken 8892561 times.
9895230 if(p_index_[i+3] == p_index_[j]) {
188 1002669 q_index[i] = j;
189 }
190 }
191 }
192
193 // Early exit tests for configurations where 1 or 2 vertices
194 // are shared.
195 1099470 int nb_shared = (
196 1099470 (q_index[0] != NO_INDEX) +
197 1099470 (q_index[1] != NO_INDEX) +
198 1099470 (q_index[2] != NO_INDEX) ) ;
199
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1099470 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1099470 geo_debug_assert(nb_shared != 3);
200
201 // If there is a single shared vertex, early exit if non-shared
202 // edge [q1,q2] is strictly on one side of [p1,p2,p3]
203
2/2
✓ Branch 0 taken 603589 times.
✓ Branch 1 taken 495881 times.
1099470 if(nb_shared == 1) {
204 603589 int shared_q =
205
2/2
✓ Branch 0 taken 200270 times.
✓ Branch 1 taken 403319 times.
603589 (q_index[1] != NO_INDEX) + (q_index[2] != NO_INDEX)*2;
206
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 603589 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
603589 geo_debug_assert(q_index[shared_q] != NO_INDEX);
207 603589 TriangleRegion q1 =
208 603589 TriangleRegion(T2_RGN_P0 + ((shared_q+1))%3);
209 603589 TriangleRegion q2 =
210 603589 TriangleRegion(T2_RGN_P0 + ((shared_q+2))%3);
211 TriangleRegion p1,p2,p3;
212
1/2
✓ Branch 1 taken 603589 times.
✗ Branch 2 not taken.
603589 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
213
1/2
✓ Branch 1 taken 603589 times.
✗ Branch 2 not taken.
603589 Sign o1 = orient3d(p1,p2,p3,q1);
214
1/2
✓ Branch 1 taken 603589 times.
✗ Branch 2 not taken.
603589 Sign o2 = orient3d(p1,p2,p3,q2);
215
2/2
✓ Branch 0 taken 479593 times.
✓ Branch 1 taken 123996 times.
603589 if(int(o1)*int(o2) > 0) {
216 479593 return;
217 }
218 }
219
220 // If there are two shared vertices, early exit if non-shared
221 // vertex q is not on support plane of [p1,p2,p3]
222
2/2
✓ Branch 0 taken 199540 times.
✓ Branch 1 taken 420337 times.
619877 if(nb_shared == 2) {
223 199540 int non_shared_q =
224
2/2
✓ Branch 0 taken 66672 times.
✓ Branch 1 taken 132868 times.
199540 (q_index[1] == NO_INDEX) + (q_index[2] == NO_INDEX)*2;
225
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 199540 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
199540 geo_debug_assert(q_index[non_shared_q] == NO_INDEX);
226 199540 TriangleRegion q = TriangleRegion(T2_RGN_P0 + non_shared_q);
227 TriangleRegion p1,p2,p3;
228
1/2
✓ Branch 1 taken 199540 times.
✗ Branch 2 not taken.
199540 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
229
3/4
✓ Branch 1 taken 199540 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 178630 times.
✓ Branch 4 taken 20910 times.
199540 if(orient3d(p1,p2,p3,q) != ZERO) {
230 178630 return;
231 }
232 }
233 }
234
235 // If T1 is strictly on one side of the supporting
236 // plane of T2, then we are sure there is no intersection
237 // and we can stop there.
238 {
239 TriangleRegion p1,p2,p3;
240 TriangleRegion q1,q2,q3;
241
1/2
✓ Branch 1 taken 441247 times.
✗ Branch 2 not taken.
441247 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
242
1/2
✓ Branch 1 taken 441247 times.
✗ Branch 2 not taken.
441247 get_triangle_vertices(T2_RGN_T, q1,q2,q3);
243
1/2
✓ Branch 1 taken 441247 times.
✗ Branch 2 not taken.
441247 Sign o1 = orient3d(q1,q2,q3,p1);
244
1/2
✓ Branch 1 taken 441247 times.
✗ Branch 2 not taken.
441247 Sign o2 = orient3d(q1,q2,q3,p2);
245
1/2
✓ Branch 1 taken 441247 times.
✗ Branch 2 not taken.
441247 Sign o3 = orient3d(q1,q2,q3,p3);
246 441247 if(
247
2/2
✓ Branch 0 taken 147170 times.
✓ Branch 1 taken 294077 times.
441247 int(o1)*int(o2) == 1 &&
248
2/2
✓ Branch 0 taken 87889 times.
✓ Branch 1 taken 59281 times.
147170 int(o2)*int(o3) == 1 &&
249
1/2
✓ Branch 0 taken 87889 times.
✗ Branch 1 not taken.
87889 int(o3)*int(o1) == 1
250 ) {
251 87889 return;
252 }
253 }
254
255 353358 intersect_edge_triangle(T1_RGN_E0, T2_RGN_T);
256
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 353358 times.
353358 if(finished()) { return; }
257 353358 intersect_edge_triangle(T1_RGN_E1, T2_RGN_T);
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 353358 times.
353358 if(finished()) { return; }
259 353358 intersect_edge_triangle(T1_RGN_E2, T2_RGN_T);
260
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 353358 times.
353358 if(finished()) { return; }
261
262 353358 intersect_edge_triangle(T2_RGN_E0, T1_RGN_T);
263
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 353358 times.
353358 if(finished()) { return; }
264 353358 intersect_edge_triangle(T2_RGN_E1, T1_RGN_T);
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 353358 times.
353358 if(finished()) { return; }
266 353358 intersect_edge_triangle(T2_RGN_E2, T1_RGN_T);
267
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 353358 times.
353358 if(finished()) { return; }
268
269 // The same intersection can appear several times,
270 // remove the duplicates
271
1/2
✓ Branch 0 taken 353358 times.
✗ Branch 1 not taken.
353358 if(result_ != nullptr) {
272 353358 std::sort(result_->begin(), result_->end());
273 353358 auto p = std::unique(result_->begin(), result_->end());
274 353358 result_->resize(index_t(p - result_->begin()));
275 // sort_unique(*result_); // HERE
276 #ifdef TT_DEBUG
277 std::string message;
278 for(TriangleIsect I: *result_) {
279 message += (" " + String::to_string(I));
280 }
281 Logger::out("II") << "result: " << message << std::endl;
282 #endif
283 }
284 }
285
286 /**
287 * \brief Tests if there as a non-degenerate intersection
288 * \retval true if an intersection different from two colocated
289 * vertices was found.
290 * \retval false otherwise.
291 */
292 1099470 bool has_non_degenerate_intersection() const {
293 1099470 return has_non_degenerate_intersection_;
294 }
295
296 protected:
297
298 /**
299 * \brief Tests whether computation is finished
300 * \details If we just want to know whether there is an intersection
301 * then we can stop sooner.
302 */
303 4321563 bool finished() const {
304 #ifdef TT_DEBUG
305 return false;
306 #endif
307
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4321563 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4321563 return (result_ == nullptr && has_non_degenerate_intersection_);
308 }
309
310 2120148 void intersect_edge_triangle(TriangleRegion E, TriangleRegion T) {
311
312 #ifdef TT_DEBUG
313 Logger::out("TT") << std::endl;
314 Logger::out("TT") << " ET " << std::make_pair(E,T) << std::endl;
315 #endif
316
317
2/8
✓ Branch 1 taken 2120148 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2120148 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2120148 geo_debug_assert(region_dim(E) == 1);
318
2/8
✓ Branch 1 taken 2120148 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2120148 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2120148 geo_debug_assert(region_dim(T) == 2);
319
320 2120148 TriangleRegion R1 = E;
321 2120148 TriangleRegion R2 = T;
322
323 TriangleRegion p1,p2,p3;
324 TriangleRegion e1,e2,e3;
325 TriangleRegion q1,q2;
326
327
1/2
✓ Branch 1 taken 2120148 times.
✗ Branch 2 not taken.
2120148 get_triangle_vertices(T,p1,p2,p3);
328
1/2
✓ Branch 1 taken 2120148 times.
✗ Branch 2 not taken.
2120148 get_triangle_edges(T,e1,e2,e3);
329
1/2
✓ Branch 1 taken 2120148 times.
✗ Branch 2 not taken.
2120148 get_edge_vertices(E,q1,q2);
330
331
1/2
✓ Branch 1 taken 2120148 times.
✗ Branch 2 not taken.
2120148 Sign o1 = orient3d(p1,p2,p3,q1);
332
1/2
✓ Branch 1 taken 2120148 times.
✗ Branch 2 not taken.
2120148 Sign o2 = orient3d(p1,p2,p3,q2);
333
334 // If both extremities of the segment on same side of triangle
335 // plane, then we are sure there is no intersection and we can
336 // stop there.
337
2/2
✓ Branch 0 taken 397686 times.
✓ Branch 1 taken 1722462 times.
2120148 if(int(o1) * int(o2) == 1) {
338 761939 return;
339 }
340
341
4/4
✓ Branch 0 taken 1065625 times.
✓ Branch 1 taken 656837 times.
✓ Branch 2 taken 728265 times.
✓ Branch 3 taken 337360 times.
1722462 if(o1 == 0 && o2 == 0) {
342 #ifdef TT_DEBUG
343 Logger::out("TT") << " ET coplanar" << std::endl;
344 #endif
345 // Special case: triangle and segment are co-planar
346
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 index_t nax = normal_axis(p1,p2,p3);
347
348 // Test whether the extremities of the segment
349 // are in the triangle
350 {
351
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 Sign a1 = orient2d(q1,p1,p2,nax);
352
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 Sign a2 = orient2d(q1,p2,p3,nax);
353
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 Sign a3 = orient2d(q1,p3,p1,nax);
354
355
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 Sign b1 = orient2d(q2,p1,p2,nax);
356
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 Sign b2 = orient2d(q2,p2,p3,nax);
357
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 Sign b3 = orient2d(q2,p3,p1,nax);
358
359 728265 if(
360
2/2
✓ Branch 0 taken 145799 times.
✓ Branch 1 taken 582466 times.
728265 int(a1)*int(a2) > 0 &&
361
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 137489 times.
145799 int(a2)*int(a3) > 0 &&
362
1/2
✓ Branch 0 taken 8310 times.
✗ Branch 1 not taken.
8310 int(a3)*int(a1) > 0
363 ) {
364
1/2
✓ Branch 1 taken 8310 times.
✗ Branch 2 not taken.
8310 add_intersection(q1,T);
365
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8310 times.
8310 if(finished()) { return; }
366 }
367
368 728265 if(
369
2/2
✓ Branch 0 taken 145050 times.
✓ Branch 1 taken 583215 times.
728265 int(b1)*int(b2) > 0 &&
370
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 136740 times.
145050 int(b2)*int(b3) > 0 &&
371
1/2
✓ Branch 0 taken 8310 times.
✗ Branch 1 not taken.
8310 int(b3)*int(b1) > 0
372 ) {
373
1/2
✓ Branch 1 taken 8310 times.
✗ Branch 2 not taken.
8310 add_intersection(q2,T);
374
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8310 times.
8310 if(finished()) { return; }
375 }
376 }
377
378
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 intersect_edge_edge_2d(E,e1,nax);
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 728265 times.
728265 if(finished()) { return; }
380
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 intersect_edge_edge_2d(E,e2,nax);
381
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 728265 times.
728265 if(finished()) { return; }
382
1/2
✓ Branch 1 taken 728265 times.
✗ Branch 2 not taken.
728265 intersect_edge_edge_2d(E,e3,nax);
383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 728265 times.
728265 if(finished()) { return; }
384
385 728265 } else {
386
387
388 // Update symbolic information of segment
389 // if one of the segment vertices is on
390 // the triangle's supporting plane.
391
392
2/2
✓ Branch 0 taken 337360 times.
✓ Branch 1 taken 656837 times.
994197 if(o1 == ZERO) {
393 337360 R1 = q1;
394
2/2
✓ Branch 0 taken 337360 times.
✓ Branch 1 taken 319477 times.
656837 } else if(o2 == ZERO) {
395 337360 R1 = q2;
396 }
397
398
1/2
✓ Branch 1 taken 994197 times.
✗ Branch 2 not taken.
994197 Sign oo1 = orient3d(p2,p3,q1,q2);
399
1/2
✓ Branch 1 taken 994197 times.
✗ Branch 2 not taken.
994197 Sign oo2 = orient3d(p3,p1,q1,q2);
400
401
2/2
✓ Branch 0 taken 364253 times.
✓ Branch 1 taken 629944 times.
994197 if(int(oo1)*int(oo2) == -1) {
402 364253 return;
403 }
404
405
1/2
✓ Branch 1 taken 629944 times.
✗ Branch 2 not taken.
629944 Sign oo3 = orient3d(p1,p2,q1,q2);
406
407 // Update symbolic information of triangle
408 // if intersection is
409 // on a vertex or on an edge
410 629944 int nb_zeros = (oo1 == ZERO) + (oo2 == ZERO) + (oo3 == ZERO);
411
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 629944 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
629944 geo_debug_assert(nb_zeros != 3);
412
2/2
✓ Branch 0 taken 60465 times.
✓ Branch 1 taken 569479 times.
629944 if(nb_zeros == 1) {
413
2/2
✓ Branch 0 taken 30797 times.
✓ Branch 1 taken 29668 times.
60465 if(oo1 == ZERO) {
414 30797 R2 = e1;
415
2/2
✓ Branch 0 taken 25450 times.
✓ Branch 1 taken 4218 times.
29668 } else if(oo2 == ZERO) {
416 25450 R2 = e2;
417 } else {
418 4218 R2 = e3;
419 }
420
2/2
✓ Branch 0 taken 343650 times.
✓ Branch 1 taken 225829 times.
569479 } else if(nb_zeros == 2) {
421
2/2
✓ Branch 0 taken 107350 times.
✓ Branch 1 taken 236300 times.
343650 if(oo1 != ZERO) {
422 107350 R2 = p1;
423
2/2
✓ Branch 0 taken 114008 times.
✓ Branch 1 taken 122292 times.
236300 } else if(oo2 != ZERO) {
424 114008 R2 = p2;
425 } else {
426 122292 R2 = p3;
427 }
428 }
429
430 #ifdef TT_DEBUG
431 Logger::out("TT") << o1 << " " << o2
432 << " "
433 << oo1 << " " << oo2 << " " << oo3
434 << std::endl;
435 #endif
436 // Intersection is outside triangle if oo1, oo2 and oo3
437 // do not have the same sign (or zero)
438 629944 bool outside =
439 1259888 (int(oo1) * int(oo2) == -1) ||
440
3/4
✓ Branch 0 taken 629944 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 417384 times.
✓ Branch 3 taken 212560 times.
1047328 (int(oo2) * int(oo3) == -1) ||
441
2/2
✓ Branch 0 taken 19940 times.
✓ Branch 1 taken 397444 times.
417384 (int(oo3) * int(oo1) == -1) ;
442
443
2/2
✓ Branch 0 taken 397444 times.
✓ Branch 1 taken 232500 times.
629944 if(!outside) {
444
1/2
✓ Branch 1 taken 397444 times.
✗ Branch 2 not taken.
397444 add_intersection(R1,R2);
445 }
446 }
447 }
448
449
450 2184795 void intersect_edge_edge_2d(
451 TriangleRegion E1, TriangleRegion E2, index_t nax
452 ) {
453 #ifdef TT_DEBUG
454 Logger::out("TT") << " EE 2d " << std::make_pair(E1,E2)
455 << " axis: " << nax << std::endl;
456 #endif
457
2/8
✓ Branch 1 taken 2184795 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2184795 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2184795 geo_debug_assert(region_dim(E1) == 1);
458
2/8
✓ Branch 1 taken 2184795 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2184795 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2184795 geo_debug_assert(region_dim(E2) == 1);
459
460 2184795 TriangleRegion R1 = E1;
461 2184795 TriangleRegion R2 = E2;
462
463 TriangleRegion p1,p2;
464
1/2
✓ Branch 1 taken 2184795 times.
✗ Branch 2 not taken.
2184795 get_edge_vertices(E1,p1,p2);
465
466 TriangleRegion q1,q2;
467
1/2
✓ Branch 1 taken 2184795 times.
✗ Branch 2 not taken.
2184795 get_edge_vertices(E2,q1,q2);
468
469
1/2
✓ Branch 1 taken 2184795 times.
✗ Branch 2 not taken.
2184795 Sign a1 = orient2d(q1,q2,p1,nax);
470
1/2
✓ Branch 1 taken 2184795 times.
✗ Branch 2 not taken.
2184795 Sign a2 = orient2d(q1,q2,p2,nax);
471
472
4/4
✓ Branch 0 taken 376429 times.
✓ Branch 1 taken 1808366 times.
✓ Branch 2 taken 90148 times.
✓ Branch 3 taken 286281 times.
2184795 if(a1 == ZERO && a2 == ZERO) {
473 // Special case: 1D
474 // (the 2x2 edge extremities are aligned)
475
1/2
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
90148 intersect_edge_edge_1d(E1,E2);
476 } else {
477 // Update symbolic information if one of E1's vertices is
478 // on the supporting line of E2
479
2/2
✓ Branch 0 taken 286281 times.
✓ Branch 1 taken 1808366 times.
2094647 if(a1 == ZERO) {
480 286281 R1 = p1;
481
2/2
✓ Branch 0 taken 286468 times.
✓ Branch 1 taken 1521898 times.
1808366 } else if(a2 == ZERO) {
482 286468 R1 = p2;
483 }
484
485
1/2
✓ Branch 1 taken 2094647 times.
✗ Branch 2 not taken.
2094647 Sign b1 = orient2d(p1,p2,q1,nax);
486
1/2
✓ Branch 1 taken 2094647 times.
✗ Branch 2 not taken.
2094647 Sign b2 = orient2d(p1,p2,q2,nax);
487
488 // Update symbolic information if one of E2's vertices is
489 // on the supporting line of E1
490
2/2
✓ Branch 0 taken 307637 times.
✓ Branch 1 taken 1787010 times.
2094647 if(b1 == ZERO) {
491 307637 R2 = q1;
492
2/2
✓ Branch 0 taken 307637 times.
✓ Branch 1 taken 1479373 times.
1787010 } else if(b2 == ZERO) {
493 307637 R2 = q2;
494 }
495
496
4/4
✓ Branch 0 taken 748750 times.
✓ Branch 1 taken 1345897 times.
✓ Branch 2 taken 597058 times.
✓ Branch 3 taken 151692 times.
2094647 if( int(a1)*int(a2) != 1 && int(b1)*int(b2) != 1) {
497
1/2
✓ Branch 1 taken 597058 times.
✗ Branch 2 not taken.
597058 add_intersection(R1,R2);
498 }
499 }
500 2184795 }
501
502 90148 void intersect_edge_edge_1d(
503 TriangleRegion E1, TriangleRegion E2
504 ) {
505 #ifdef TT_DEBUG
506 Logger::out("TT") << " EE 1d " << std::make_pair(E1,E2)
507 << std::endl;
508 #endif
509
2/8
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 90148 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
90148 geo_debug_assert(region_dim(E1) == 1);
510
2/8
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 90148 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
90148 geo_debug_assert(region_dim(E2) == 1);
511
512 TriangleRegion p1,p2;
513
1/2
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
90148 get_edge_vertices(E1,p1,p2);
514
515 TriangleRegion q1,q2;
516
1/2
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
90148 get_edge_vertices(E2,q1,q2);
517
518
1/2
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
90148 Sign d1 = dot3d(p1,q1,q2);
519
1/2
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
90148 Sign d2 = dot3d(p2,q1,q2);
520
1/2
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
90148 Sign d3 = dot3d(q1,p1,p2);
521
1/2
✓ Branch 1 taken 90148 times.
✗ Branch 2 not taken.
90148 Sign d4 = dot3d(q2,p1,p2);
522
523 // Test for identical vertices
524 // (small optimization, each time there is an identical pair
525 // of points, the two dot3d predicates they participiate to
526 // should return ZERO, so we can filter)
527
528
9/10
✓ Branch 0 taken 62361 times.
✓ Branch 1 taken 27787 times.
✓ Branch 2 taken 52090 times.
✓ Branch 3 taken 10271 times.
✓ Branch 5 taken 52090 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10330 times.
✓ Branch 8 taken 41760 times.
✓ Branch 9 taken 10330 times.
✓ Branch 10 taken 79818 times.
90148 if(d1 == ZERO && d3 == ZERO && points_are_identical(p1,q1)) {
529
1/2
✓ Branch 1 taken 10330 times.
✗ Branch 2 not taken.
10330 add_intersection(p1,q1);
530 }
531
532
9/10
✓ Branch 0 taken 62363 times.
✓ Branch 1 taken 27785 times.
✓ Branch 2 taken 52091 times.
✓ Branch 3 taken 10272 times.
✓ Branch 5 taken 52091 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 52031 times.
✓ Branch 8 taken 60 times.
✓ Branch 9 taken 52031 times.
✓ Branch 10 taken 38117 times.
90148 if(d2 == ZERO && d3 == ZERO && points_are_identical(p2,q1)) {
533
1/2
✓ Branch 1 taken 52031 times.
✗ Branch 2 not taken.
52031 add_intersection(p2,q1);
534 }
535
536
9/10
✓ Branch 0 taken 62361 times.
✓ Branch 1 taken 27787 times.
✓ Branch 2 taken 52091 times.
✓ Branch 3 taken 10270 times.
✓ Branch 5 taken 52091 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 52031 times.
✓ Branch 8 taken 60 times.
✓ Branch 9 taken 52031 times.
✓ Branch 10 taken 38117 times.
90148 if(d1 == ZERO && d4 == ZERO && points_are_identical(p1,q2)) {
537
1/2
✓ Branch 1 taken 52031 times.
✗ Branch 2 not taken.
52031 add_intersection(p1,q2);
538 }
539
540
9/10
✓ Branch 0 taken 62363 times.
✓ Branch 1 taken 27785 times.
✓ Branch 2 taken 52092 times.
✓ Branch 3 taken 10271 times.
✓ Branch 5 taken 52092 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10332 times.
✓ Branch 8 taken 41760 times.
✓ Branch 9 taken 10332 times.
✓ Branch 10 taken 79816 times.
90148 if(d2 == ZERO && d4 == ZERO && points_are_identical(p2,q2)) {
541
1/2
✓ Branch 1 taken 10332 times.
✗ Branch 2 not taken.
10332 add_intersection(p2,q2);
542 }
543
544 // Test for point in segment:
545 // c is in segment [a,b] if (c-a).(c-b) < 0
546
547
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89970 times.
90148 if(d1 == NEGATIVE) {
548
1/2
✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
178 add_intersection(p1,E2);
549 }
550
551
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89970 times.
90148 if(d2 == NEGATIVE) {
552
1/2
✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
178 add_intersection(p2,E2);
553 }
554
555
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89970 times.
90148 if(d3 == NEGATIVE) {
556
1/2
✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
178 add_intersection(E1,q1);
557 }
558
559
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89970 times.
90148 if(d4 == NEGATIVE) {
560
1/2
✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
178 add_intersection(E1,q2);
561 }
562 90148 }
563
564 1136558 void add_intersection(TriangleRegion R1, TriangleRegion R2) {
565 #ifdef TT_DEBUG
566 Logger::out("TT") << " ==>I: " << std::make_pair(R1,R2)
567 << std::endl;
568 #endif
569
6/6
✓ Branch 1 taken 1015168 times.
✓ Branch 2 taken 121390 times.
✓ Branch 4 taken 31586 times.
✓ Branch 5 taken 983582 times.
✓ Branch 6 taken 152976 times.
✓ Branch 7 taken 983582 times.
1136558 if(region_dim(R1) >= 1 || region_dim(R2) >= 1) {
570 152976 has_non_degenerate_intersection_ = true;
571 }
572
2/2
✓ Branch 1 taken 545360 times.
✓ Branch 2 taken 591198 times.
1136558 if(is_in_T1(R1)) {
573
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 545360 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
545360 geo_debug_assert(!is_in_T1(R2));
574
1/2
✓ Branch 0 taken 545360 times.
✗ Branch 1 not taken.
545360 if(result_ != nullptr) {
575
2/4
✓ Branch 1 taken 545360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 545360 times.
✗ Branch 5 not taken.
545360 result_->push_back(std::make_pair(R1,R2));
576 }
577 } else {
578
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 591198 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
591198 geo_debug_assert(is_in_T1(R2));
579
1/2
✓ Branch 0 taken 591198 times.
✗ Branch 1 not taken.
591198 if(result_ != nullptr) {
580
2/4
✓ Branch 1 taken 591198 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 591198 times.
✗ Branch 5 not taken.
591198 result_->push_back(std::make_pair(R2,R1));
581 }
582 }
583 1136558 }
584
585 9589093 Sign orient3d(
586 TriangleRegion i, TriangleRegion j,
587 TriangleRegion k, TriangleRegion l
588 ) const {
589
590
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9589093 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9589093 geo_debug_assert(region_dim(i) == 0);
591
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9589093 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9589093 geo_debug_assert(region_dim(j) == 0);
592
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9589093 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9589093 geo_debug_assert(region_dim(k) == 0);
593
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9589093 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9589093 geo_debug_assert(region_dim(l) == 0);
594
595 // Index for the cache (1 bit set for
596 // each vertex)
597
598 9589093 index_t o3d_idx =
599 9589093 (1u << index_t(i)) |
600 9589093 (1u << index_t(j)) |
601 9589093 (1u << index_t(k)) |
602 9589093 (1u << index_t(l)) ;
603
604
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 9589093 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
9589093 geo_debug_assert(o3d_idx < 64);
605
606 // Result of the predicate should be flipped if the
607 // order of the arguments is permutted by an odd permutation
608
609 9589093 bool flip = odd_order(index_t(i),index_t(j),index_t(k),index_t(l));
610
611 // If cache not initialized, set cache value
612
613 9589093 Numeric::uint64 o3d_mask = (Numeric::uint64(1u) << o3d_idx);
614
2/2
✓ Branch 0 taken 5296148 times.
✓ Branch 1 taken 4292945 times.
9589093 if((o3d_cached_ & o3d_mask) == 0) {
615
2/2
✓ Branch 0 taken 2134978 times.
✓ Branch 1 taken 3161170 times.
5296148 int o = flip ?
616 2134978 -int(PCK::orient_3d(p_[i],p_[j],p_[k],p_[l])) :
617 5296148 int(PCK::orient_3d(p_[i],p_[j],p_[k],p_[l])) ;
618 5296148 o3d_cache_[o3d_idx] = Numeric::int8(o);
619 5296148 o3d_cached_ |= o3d_mask;
620 }
621
622 // Get result from the cache
623
624
2/2
✓ Branch 0 taken 4644114 times.
✓ Branch 1 taken 4944979 times.
9589093 Sign result = flip ?
625 4644114 Sign(-o3d_cache_[o3d_idx]) :
626 4944979 Sign( o3d_cache_[o3d_idx]) ;
627
628 // Sanity check: did our cache return the same result as
629 // directly calling the predicate ?
630
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9589093 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9589093 geo_debug_assert(
631 result == PCK::orient_3d(p_[i], p_[j], p_[k], p_[l])
632 );
633
634 9589093 return result;
635 }
636
637 /**
638 * \brief Tests the parity of the permutation of a list of
639 * four distinct indices with respect to the canonical order.
640 */
641 9589093 static bool odd_order(index_t i, index_t j, index_t k, index_t l) {
642 9589093 return (i > j) ^ (i > k) ^ (i > l) ^ (j > k) ^ (j > l) ^ (k > l);
643 }
644
645 12928474 Sign orient2d(
646 TriangleRegion i, TriangleRegion j, TriangleRegion k,
647 index_t normal_axis
648 ) const {
649 // Note: no cache for orient2d (tested, did not bring
650 // any performance gain).
651
2/8
✓ Branch 1 taken 12928474 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12928474 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
12928474 geo_debug_assert(region_dim(i) == 0);
652
2/8
✓ Branch 1 taken 12928474 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12928474 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
12928474 geo_debug_assert(region_dim(j) == 0);
653
2/8
✓ Branch 1 taken 12928474 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12928474 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
12928474 geo_debug_assert(region_dim(k) == 0);
654 double pi[2];
655 double pj[2];
656 double pk[2];
657
2/2
✓ Branch 0 taken 25856948 times.
✓ Branch 1 taken 12928474 times.
38785422 for(coord_index_t c = 0; c < 2; c++) {
658
1/2
✓ Branch 1 taken 25856948 times.
✗ Branch 2 not taken.
25856948 pi[c] = p_[i][index_t((normal_axis + 1 + c) % 3)];
659
1/2
✓ Branch 1 taken 25856948 times.
✗ Branch 2 not taken.
25856948 pj[c] = p_[j][index_t((normal_axis + 1 + c) % 3)];
660
1/2
✓ Branch 1 taken 25856948 times.
✗ Branch 2 not taken.
25856948 pk[c] = p_[k][index_t((normal_axis + 1 + c) % 3)];
661 }
662
1/2
✓ Branch 1 taken 12928474 times.
✗ Branch 2 not taken.
25856948 return PCK::orient_2d(pi,pj,pk);
663 }
664
665 /**
666 * \brief Computes the dot product between two vectors supported
667 * by three points.
668 * \param[in] i , j , k the three points
669 * \return sign(dot(pj-pi,pk-pi))
670 */
671 360592 Sign dot3d(
672 TriangleRegion i, TriangleRegion j, TriangleRegion k
673 ) const {
674
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 360592 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
360592 geo_debug_assert(region_dim(i) == 0);
675
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 360592 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
360592 geo_debug_assert(region_dim(j) == 0);
676
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 360592 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
360592 geo_debug_assert(region_dim(k) == 0);
677 360592 return PCK::dot_3d(p_[i].data(), p_[j].data(), p_[k].data());
678 }
679
680
681 /**
682 * \brief Computes the coordinate along which a triangle can be
683 * projected without introducting degeneracies.
684 * \param[in] v1 , v2 , v3 the three vertices of the triangle
685 * \return the coordinate to be used for 2d computations (0,1 or 2)
686 */
687 728265 coord_index_t normal_axis(
688 TriangleRegion v1, TriangleRegion v2, TriangleRegion v3
689 ) {
690
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 728265 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
728265 geo_debug_assert(region_dim(v1) == 0);
691
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 728265 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
728265 geo_debug_assert(region_dim(v2) == 0);
692
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 728265 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
728265 geo_debug_assert(region_dim(v3) == 0);
693
694 728265 const vec3& p1 = p_[v1];
695 728265 const vec3& p2 = p_[v2];
696 728265 const vec3& p3 = p_[v3];
697
698 728265 return PCK::triangle_normal_axis(p1,p2,p3);
699 }
700
701
702 208364 bool points_are_identical(TriangleRegion i, TriangleRegion j) {
703
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 208364 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
208364 geo_debug_assert(region_dim(i) == 0);
704
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 208364 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
208364 geo_debug_assert(region_dim(j) == 0);
705 208364 const vec3& p1 = p_[i];
706 208364 const vec3& p2 = p_[j];
707 return
708 208364 (p1[0] == p2[0]) &&
709
4/4
✓ Branch 0 taken 146088 times.
✓ Branch 1 taken 62276 times.
✓ Branch 4 taken 126644 times.
✓ Branch 5 taken 19444 times.
335008 (p1[1] == p2[1]) &&
710
2/2
✓ Branch 2 taken 124724 times.
✓ Branch 3 taken 1920 times.
335008 (p1[2] == p2[2]) ;
711 }
712
713 /**
714 * \brief Detects degenerate triangles
715 * \retval 0 if all the vertices of the triangle are the same point
716 * \retval 1 if the vertices of the triangle are co-linear
717 * \retval 2 otherwise
718 */
719 2198940 index_t triangle_dim(
720 TriangleRegion i, TriangleRegion j, TriangleRegion k
721 ) {
722
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2198940 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2198940 geo_debug_assert(region_dim(i) == 0);
723
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2198940 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2198940 geo_debug_assert(region_dim(j) == 0);
724
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2198940 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2198940 geo_debug_assert(region_dim(k) == 0);
725
726 2198940 const vec3& p1 = p_[i];
727 2198940 const vec3& p2 = p_[j];
728 2198940 const vec3& p3 = p_[k];
729
730
1/2
✓ Branch 4 taken 2198940 times.
✗ Branch 5 not taken.
2198940 if(!PCK::aligned_3d(p1.data(), p2.data(), p3.data())) {
731 2198940 return 2;
732 }
733 ✗ if(points_are_identical(i,j) && points_are_identical(j,k)) {
734 ✗ return 0;
735 }
736 ✗ return 1;
737 }
738
739
740 private:
741 vec3 p_[6];
742 TriangleIsects* result_;
743 bool has_non_degenerate_intersection_;
744 mutable Numeric::int8 o3d_cache_[64];
745 mutable Numeric::uint64 o3d_cached_; // 1 bit set per cached value
746 index_t p_index_[6];
747 bool has_global_indices_;
748 TriangleIsectFlags flags_;
749 };
750
751 }
752
753 /****************************************************************************/
754
755 namespace GEO {
756
757 ✗ std::string region_to_string(TriangleRegion rgn) {
758 ✗ const char* strs[T_RGN_NB] = {
759 "T1.P0",
760 "T1.P1",
761 "T1.P2",
762
763 "T2.P0",
764 "T2.P1",
765 "T2.P2",
766
767 "T1.E0",
768 "T1.E1",
769 "T1.E2",
770
771 "T2.E0",
772 "T2.E1",
773 "T2.E2",
774
775 "T1.T",
776 "T2.T"
777 };
778 ✗ geo_assert(int(rgn) < int(T_RGN_NB));
779 ✗ return strs[int(rgn)];
780 }
781
782 // This version returns the symbolic information.
783 ✗ bool triangles_intersections(
784 const vec3& p0, const vec3& p1, const vec3& p2,
785 const vec3& q0, const vec3& q1, const vec3& q2,
786 TriangleIsects& result, TriangleIsectFlags flags
787 ) {
788 ✗ result.resize(0);
789 TriangleTriangleIntersection I(
790 p0, p1, p2,
791 q0, q1, q2,
792 &result
793 ✗ );
794 ✗ I.set_flags(flags);
795 ✗ I.compute();
796 ✗ return I.has_non_degenerate_intersection();
797 }
798
799 // This version returns the symbolic information.
800 1099470 bool triangles_intersections(
801 const vec3& p0, const vec3& p1, const vec3& p2,
802 const vec3& q0, const vec3& q1, const vec3& q2,
803 index_t p0_index, index_t p1_index, index_t p2_index,
804 index_t q0_index, index_t q1_index, index_t q2_index,
805 TriangleIsects& result, TriangleIsectFlags flags
806 ) {
807
1/2
✓ Branch 1 taken 1099470 times.
✗ Branch 2 not taken.
1099470 result.resize(0);
808 TriangleTriangleIntersection I(
809 p0, p1, p2,
810 q0, q1, q2,
811 p0_index, p1_index, p2_index,
812 q0_index, q1_index, q2_index,
813 &result
814
1/2
✓ Branch 1 taken 1099470 times.
✗ Branch 2 not taken.
1099470 );
815 1099470 I.set_flags(flags);
816
1/2
✓ Branch 1 taken 1099470 times.
✗ Branch 2 not taken.
1099470 I.compute();
817 2198940 return I.has_non_degenerate_intersection();
818 }
819
820 // This version is just a predicate (returns true if
821 // there is a non-degenerate intersection, false
822 // otherwise).
823 ✗ bool triangles_intersections(
824 const vec3& p0, const vec3& p1, const vec3& p2,
825 const vec3& q0, const vec3& q1, const vec3& q2
826 ) {
827 TriangleTriangleIntersection I(
828 p0, p1, p2,
829 q0, q1, q2
830 ✗ );
831 ✗ I.compute();
832 ✗ return I.has_non_degenerate_intersection();
833 }
834
835 114427404 coord_index_t region_dim(TriangleRegion r) {
836
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 114427404 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
114427404 geo_debug_assert(index_t(r) < T_RGN_NB);
837 static coord_index_t trgl_rgn_dim[T_RGN_NB] = {
838 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2
839 };
840 114427404 return trgl_rgn_dim[index_t(r)];
841 }
842
843 271532 TriangleRegion swap_T1_T2(TriangleRegion R) {
844 271532 TriangleRegion result=T_RGN_NB;
845
14/16
✓ Branch 0 taken 4421 times.
✓ Branch 1 taken 3738 times.
✓ Branch 2 taken 4741 times.
✓ Branch 3 taken 5958 times.
✓ Branch 4 taken 3646 times.
✓ Branch 5 taken 3986 times.
✓ Branch 6 taken 32385 times.
✓ Branch 7 taken 28772 times.
✓ Branch 8 taken 35685 times.
✓ Branch 9 taken 32845 times.
✓ Branch 10 taken 28613 times.
✓ Branch 11 taken 36312 times.
✓ Branch 12 taken 26024 times.
✓ Branch 13 taken 24406 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
271532 switch(R) {
846 4421 case T1_RGN_P0:
847 4421 result = T2_RGN_P0;
848 4421 break;
849 3738 case T1_RGN_P1:
850 3738 result = T2_RGN_P1;
851 3738 break;
852 4741 case T1_RGN_P2:
853 4741 result = T2_RGN_P2;
854 4741 break;
855 5958 case T2_RGN_P0:
856 5958 result = T1_RGN_P0;
857 5958 break;
858 3646 case T2_RGN_P1:
859 3646 result = T1_RGN_P1;
860 3646 break;
861 3986 case T2_RGN_P2:
862 3986 result = T1_RGN_P2;
863 3986 break;
864 32385 case T1_RGN_E0:
865 32385 result = T2_RGN_E0;
866 32385 break;
867 28772 case T1_RGN_E1:
868 28772 result = T2_RGN_E1;
869 28772 break;
870 35685 case T1_RGN_E2:
871 35685 result = T2_RGN_E2;
872 35685 break;
873 32845 case T2_RGN_E0:
874 32845 result = T1_RGN_E0;
875 32845 break;
876 28613 case T2_RGN_E1:
877 28613 result = T1_RGN_E1;
878 28613 break;
879 36312 case T2_RGN_E2:
880 36312 result = T1_RGN_E2;
881 36312 break;
882 26024 case T1_RGN_T:
883 26024 result = T2_RGN_T;
884 26024 break;
885 24406 case T2_RGN_T:
886 24406 result = T1_RGN_T;
887 24406 break;
888 ✗ case T_RGN_NB:
889 ✗ geo_assert_not_reached;
890 }
891 271532 return result;
892 }
893
894 3805771 void get_triangle_vertices(
895 TriangleRegion T,
896 TriangleRegion& p0, TriangleRegion& p1, TriangleRegion& p2
897 ) {
898
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 3805771 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
3805771 geo_debug_assert(region_dim(T) == 2);
899
2/3
✓ Branch 0 taken 2304450 times.
✓ Branch 1 taken 1501321 times.
✗ Branch 2 not taken.
3805771 switch(T) {
900 2304450 case T1_RGN_T: {
901 2304450 p0 = T1_RGN_P0; p1 = T1_RGN_P1; p2 = T1_RGN_P2;
902 2304450 } break;
903 1501321 case T2_RGN_T: {
904 1501321 p0 = T2_RGN_P0; p1 = T2_RGN_P1; p2 = T2_RGN_P2;
905 1501321 } break;
906 ✗ default:
907 ✗ geo_assert_not_reached;
908 }
909 3805771 }
910
911 2120148 void get_triangle_edges(
912 TriangleRegion T,
913 TriangleRegion& e0, TriangleRegion& e1, TriangleRegion& e2
914 ) {
915
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2120148 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2120148 geo_debug_assert(region_dim(T) == 2);
916
2/3
✓ Branch 0 taken 1060074 times.
✓ Branch 1 taken 1060074 times.
✗ Branch 2 not taken.
2120148 switch(T) {
917 1060074 case T1_RGN_T: {
918 1060074 e0 = T1_RGN_E0; e1 = T1_RGN_E1; e2 = T1_RGN_E2;
919 1060074 } break;
920 1060074 case T2_RGN_T: {
921 1060074 e0 = T2_RGN_E0; e1 = T2_RGN_E1; e2 = T2_RGN_E2;
922 1060074 } break;
923 ✗ default:
924 ✗ geo_assert_not_reached;
925 }
926 2120148 }
927
928 6736287 void get_edge_vertices(
929 TriangleRegion E, TriangleRegion& q0, TriangleRegion& q1
930 ) {
931
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 6736287 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
6736287 geo_debug_assert(region_dim(E) == 1);
932
6/7
✓ Branch 0 taken 1132614 times.
✓ Branch 1 taken 1120732 times.
✓ Branch 2 taken 1123052 times.
✓ Branch 3 taken 1120392 times.
✓ Branch 4 taken 1120141 times.
✓ Branch 5 taken 1119356 times.
✗ Branch 6 not taken.
6736287 switch(E) {
933 1132614 case T1_RGN_E0: {
934 1132614 q0 = T1_RGN_P1;
935 1132614 q1 = T1_RGN_P2;
936 1132614 } break;
937 1120732 case T1_RGN_E1: {
938 1120732 q0 = T1_RGN_P2;
939 1120732 q1 = T1_RGN_P0;
940 1120732 } break;
941 1123052 case T1_RGN_E2: {
942 1123052 q0 = T1_RGN_P0;
943 1123052 q1 = T1_RGN_P1;
944 1123052 } break;
945 1120392 case T2_RGN_E0: {
946 1120392 q0 = T2_RGN_P1;
947 1120392 q1 = T2_RGN_P2;
948 1120392 } break;
949 1120141 case T2_RGN_E1: {
950 1120141 q0 = T2_RGN_P2;
951 1120141 q1 = T2_RGN_P0;
952 1120141 } break;
953 1119356 case T2_RGN_E2: {
954 1119356 q0 = T2_RGN_P0;
955 1119356 q1 = T2_RGN_P1;
956 1119356 } break;
957 ✗ default:
958 ✗ geo_assert_not_reached;
959 }
960 6736287 }
961
962 328516 TriangleRegion GEOGRAM_API regions_convex_hull(
963 TriangleRegion R1, TriangleRegion R2
964 ) {
965
1/6
✗ Branch 2 not taken.
✓ Branch 3 taken 328516 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
328516 geo_debug_assert(is_in_T1(R1) == is_in_T1(R2));
966
2/2
✓ Branch 0 taken 122223 times.
✓ Branch 1 taken 206293 times.
328516 if(R1 == R2) {
967 122223 return R1;
968 }
969
970
2/2
✓ Branch 1 taken 126131 times.
✓ Branch 2 taken 80162 times.
206293 TriangleRegion R = is_in_T1(R1) ? T1_RGN_T : T2_RGN_T;
971
972
6/6
✓ Branch 1 taken 160519 times.
✓ Branch 2 taken 45774 times.
✓ Branch 4 taken 17166 times.
✓ Branch 5 taken 143353 times.
✓ Branch 6 taken 17166 times.
✓ Branch 7 taken 189127 times.
206293 if(region_dim(R1) == 1 && region_dim(R2) == 0) {
973 TriangleRegion v1,v2;
974
1/2
✓ Branch 1 taken 17166 times.
✗ Branch 2 not taken.
17166 get_edge_vertices(R1,v1,v2);
975
4/4
✓ Branch 0 taken 9490 times.
✓ Branch 1 taken 7676 times.
✓ Branch 2 taken 7581 times.
✓ Branch 3 taken 1909 times.
17166 if(R2 == v1 || R2 == v2) {
976 15257 R = R1;
977 }
978
6/6
✓ Branch 1 taken 155155 times.
✓ Branch 2 taken 33972 times.
✓ Branch 4 taken 14634 times.
✓ Branch 5 taken 140521 times.
✓ Branch 6 taken 14634 times.
✓ Branch 7 taken 174493 times.
189127 } else if(region_dim(R2) == 1 && region_dim(R1) == 0) {
979 TriangleRegion v1,v2;
980
1/2
✓ Branch 1 taken 14634 times.
✗ Branch 2 not taken.
14634 get_edge_vertices(R2,v1,v2);
981
4/4
✓ Branch 0 taken 8339 times.
✓ Branch 1 taken 6295 times.
✓ Branch 2 taken 6392 times.
✓ Branch 3 taken 1947 times.
14634 if(R1 == v1 || R1 == v2) {
982 12687 R = R2;
983 }
984
6/6
✓ Branch 1 taken 10417 times.
✓ Branch 2 taken 164076 times.
✓ Branch 4 taken 10237 times.
✓ Branch 5 taken 180 times.
✓ Branch 6 taken 10237 times.
✓ Branch 7 taken 164256 times.
174493 } else if(region_dim(R1) == 0 && region_dim(R2) == 0) {
985 34453 for(TriangleRegion E: {
986 T1_RGN_E0, T1_RGN_E1, T1_RGN_E2,
987 T2_RGN_E0, T2_RGN_E1, T2_RGN_E2
988
1/2
✓ Branch 2 taken 34453 times.
✗ Branch 3 not taken.
44690 }
989 ) {
990 TriangleRegion v1,v2;
991
1/2
✓ Branch 1 taken 34453 times.
✗ Branch 2 not taken.
34453 get_edge_vertices(E,v1,v2);
992
8/8
✓ Branch 0 taken 6739 times.
✓ Branch 1 taken 27714 times.
✓ Branch 2 taken 2336 times.
✓ Branch 3 taken 4403 times.
✓ Branch 4 taken 8929 times.
✓ Branch 5 taken 21121 times.
✓ Branch 6 taken 5834 times.
✓ Branch 7 taken 3095 times.
34453 if((R1 == v1 && R2 == v2) || (R1 == v2 && R2 == v1)) {
993 10237 R = E;
994 10237 break;
995 }
996 }
997 }
998
999 206293 return R;
1000 }
1001
1002 }
1003