GCC Code Coverage Report


Directory: ./
File: mesh/triangle_intersection.cpp
Date: 2026-11-30 06:25:12
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 1125521 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 6753126 times.
✓ Branch 2 taken 1125521 times.
7878647 ) : result_(result) {
102 1125521 p_[0] = p0;
103 1125521 p_[1] = p1;
104 1125521 p_[2] = p2;
105 1125521 p_[3] = q0;
106 1125521 p_[4] = q1;
107 1125521 p_[5] = q2;
108 1125521 o3d_cached_ = 0;
109 1125521 has_non_degenerate_intersection_ = false;
110 1125521 p_index_[0] = p0_index;
111 1125521 p_index_[1] = p1_index;
112 1125521 p_index_[2] = p2_index;
113 1125521 p_index_[3] = q0_index;
114 1125521 p_index_[4] = q1_index;
115 1125521 p_index_[5] = q2_index;
116 1125521 has_global_indices_ = true;
117 1125521 flags_ = TRIANGLES_INTERSECTION_DEFAULT;
118 1125521 }
119
120 1125521 void set_flags(TriangleIsectFlags flags) {
121 1125521 flags_ = flags;
122 1125521 }
123
124 1125521 void compute() {
125 #ifdef TT_DEBUG
126 Logger::out("TT") << "Call compute()" << std::endl;
127 #endif
128
129
1/2
✓ Branch 0 taken 1125521 times.
✗ Branch 1 not taken.
1125521 if(result_ != nullptr) {
130 1125521 result_->resize(0);
131 }
132
133 1125521 bool test_degenerate = (
134 1125521 (flags_ & TRIANGLES_INTERSECTION_NO_DEGENERATE) == 0
135 );
136
137 1125521 bool test_bbox = (
138 1125521 (flags_ & TRIANGLES_INTERSECTION_NO_BBOX) == 0
139 );
140
141 // Test for degenerate triangles
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1125521 times.
1125521 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 1125521 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1125521 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 1125521 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1125521 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 1125521 times.
1125521 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 1125521 times.
✗ Branch 1 not taken.
1125521 if(has_global_indices_) {
180
181 1125521 index_t q_index[3] = {
182 NO_INDEX, NO_INDEX, NO_INDEX
183 };
184
185
2/2
✓ Branch 0 taken 3376563 times.
✓ Branch 1 taken 1125521 times.
4502084 for(index_t i=0; i<3; ++i) {
186
2/2
✓ Branch 0 taken 10129689 times.
✓ Branch 1 taken 3376563 times.
13506252 for(index_t j=0; j<3; ++j) {
187
2/2
✓ Branch 0 taken 1033334 times.
✓ Branch 1 taken 9096355 times.
10129689 if(p_index_[i+3] == p_index_[j]) {
188 1033334 q_index[i] = j;
189 }
190 }
191 }
192
193 // Early exit tests for configurations where 1 or 2 vertices
194 // are shared.
195 1125521 int nb_shared = (
196 1125521 (q_index[0] != NO_INDEX) +
197 1125521 (q_index[1] != NO_INDEX) +
198 1125521 (q_index[2] != NO_INDEX) ) ;
199
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1125521 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1125521 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 622254 times.
✓ Branch 1 taken 503267 times.
1125521 if(nb_shared == 1) {
204 622254 int shared_q =
205
2/2
✓ Branch 0 taken 206959 times.
✓ Branch 1 taken 415295 times.
622254 (q_index[1] != NO_INDEX) + (q_index[2] != NO_INDEX)*2;
206
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 622254 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
622254 geo_debug_assert(q_index[shared_q] != NO_INDEX);
207 622254 TriangleRegion q1 =
208 622254 TriangleRegion(T2_RGN_P0 + ((shared_q+1))%3);
209 622254 TriangleRegion q2 =
210 622254 TriangleRegion(T2_RGN_P0 + ((shared_q+2))%3);
211 TriangleRegion p1,p2,p3;
212
1/2
✓ Branch 1 taken 622254 times.
✗ Branch 2 not taken.
622254 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
213
1/2
✓ Branch 1 taken 622254 times.
✗ Branch 2 not taken.
622254 Sign o1 = orient3d(p1,p2,p3,q1);
214
1/2
✓ Branch 1 taken 622254 times.
✗ Branch 2 not taken.
622254 Sign o2 = orient3d(p1,p2,p3,q2);
215
2/2
✓ Branch 0 taken 492646 times.
✓ Branch 1 taken 129608 times.
622254 if(int(o1)*int(o2) > 0) {
216 492646 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 205540 times.
✓ Branch 1 taken 427335 times.
632875 if(nb_shared == 2) {
223 205540 int non_shared_q =
224
2/2
✓ Branch 0 taken 68603 times.
✓ Branch 1 taken 136937 times.
205540 (q_index[1] == NO_INDEX) + (q_index[2] == NO_INDEX)*2;
225
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 205540 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
205540 geo_debug_assert(q_index[non_shared_q] == NO_INDEX);
226 205540 TriangleRegion q = TriangleRegion(T2_RGN_P0 + non_shared_q);
227 TriangleRegion p1,p2,p3;
228
1/2
✓ Branch 1 taken 205540 times.
✗ Branch 2 not taken.
205540 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
229
3/4
✓ Branch 1 taken 205540 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 184637 times.
✓ Branch 4 taken 20903 times.
205540 if(orient3d(p1,p2,p3,q) != ZERO) {
230 184637 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 448238 times.
✗ Branch 2 not taken.
448238 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
242
1/2
✓ Branch 1 taken 448238 times.
✗ Branch 2 not taken.
448238 get_triangle_vertices(T2_RGN_T, q1,q2,q3);
243
1/2
✓ Branch 1 taken 448238 times.
✗ Branch 2 not taken.
448238 Sign o1 = orient3d(q1,q2,q3,p1);
244
1/2
✓ Branch 1 taken 448238 times.
✗ Branch 2 not taken.
448238 Sign o2 = orient3d(q1,q2,q3,p2);
245
1/2
✓ Branch 1 taken 448238 times.
✗ Branch 2 not taken.
448238 Sign o3 = orient3d(q1,q2,q3,p3);
246 448238 if(
247
2/2
✓ Branch 0 taken 149146 times.
✓ Branch 1 taken 299092 times.
448238 int(o1)*int(o2) == 1 &&
248
2/2
✓ Branch 0 taken 88523 times.
✓ Branch 1 taken 60623 times.
149146 int(o2)*int(o3) == 1 &&
249
1/2
✓ Branch 0 taken 88523 times.
✗ Branch 1 not taken.
88523 int(o3)*int(o1) == 1
250 ) {
251 88523 return;
252 }
253 }
254
255 359715 intersect_edge_triangle(T1_RGN_E0, T2_RGN_T);
256
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 359715 times.
359715 if(finished()) { return; }
257 359715 intersect_edge_triangle(T1_RGN_E1, T2_RGN_T);
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 359715 times.
359715 if(finished()) { return; }
259 359715 intersect_edge_triangle(T1_RGN_E2, T2_RGN_T);
260
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 359715 times.
359715 if(finished()) { return; }
261
262 359715 intersect_edge_triangle(T2_RGN_E0, T1_RGN_T);
263
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 359715 times.
359715 if(finished()) { return; }
264 359715 intersect_edge_triangle(T2_RGN_E1, T1_RGN_T);
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 359715 times.
359715 if(finished()) { return; }
266 359715 intersect_edge_triangle(T2_RGN_E2, T1_RGN_T);
267
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 359715 times.
359715 if(finished()) { return; }
268
269 // The same intersection can appear several times,
270 // remove the duplicates
271
1/2
✓ Branch 0 taken 359715 times.
✗ Branch 1 not taken.
359715 if(result_ != nullptr) {
272 359715 std::sort(result_->begin(), result_->end());
273 359715 auto p = std::unique(result_->begin(), result_->end());
274 359715 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 1125521 bool has_non_degenerate_intersection() const {
293 1125521 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 4359129 bool finished() const {
304 #ifdef TT_DEBUG
305 return false;
306 #endif
307
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4359129 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4359129 return (result_ == nullptr && has_non_degenerate_intersection_);
308 }
309
310 2158290 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 2158290 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2158290 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2158290 geo_debug_assert(region_dim(E) == 1);
318
2/8
✓ Branch 1 taken 2158290 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2158290 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2158290 geo_debug_assert(region_dim(T) == 2);
319
320 2158290 TriangleRegion R1 = E;
321 2158290 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 2158290 times.
✗ Branch 2 not taken.
2158290 get_triangle_vertices(T,p1,p2,p3);
328
1/2
✓ Branch 1 taken 2158290 times.
✗ Branch 2 not taken.
2158290 get_triangle_edges(T,e1,e2,e3);
329
1/2
✓ Branch 1 taken 2158290 times.
✗ Branch 2 not taken.
2158290 get_edge_vertices(E,q1,q2);
330
331
1/2
✓ Branch 1 taken 2158290 times.
✗ Branch 2 not taken.
2158290 Sign o1 = orient3d(p1,p2,p3,q1);
332
1/2
✓ Branch 1 taken 2158290 times.
✗ Branch 2 not taken.
2158290 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 402685 times.
✓ Branch 1 taken 1755605 times.
2158290 if(int(o1) * int(o2) == 1) {
338 774274 return;
339 }
340
341
4/4
✓ Branch 0 taken 1076660 times.
✓ Branch 1 taken 678945 times.
✓ Branch 2 taken 728073 times.
✓ Branch 3 taken 348587 times.
1755605 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 728073 times.
✗ Branch 2 not taken.
728073 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 728073 times.
✗ Branch 2 not taken.
728073 Sign a1 = orient2d(q1,p1,p2,nax);
352
1/2
✓ Branch 1 taken 728073 times.
✗ Branch 2 not taken.
728073 Sign a2 = orient2d(q1,p2,p3,nax);
353
1/2
✓ Branch 1 taken 728073 times.
✗ Branch 2 not taken.
728073 Sign a3 = orient2d(q1,p3,p1,nax);
354
355
1/2
✓ Branch 1 taken 728073 times.
✗ Branch 2 not taken.
728073 Sign b1 = orient2d(q2,p1,p2,nax);
356
1/2
✓ Branch 1 taken 728073 times.
✗ Branch 2 not taken.
728073 Sign b2 = orient2d(q2,p2,p3,nax);
357
1/2
✓ Branch 1 taken 728073 times.
✗ Branch 2 not taken.
728073 Sign b3 = orient2d(q2,p3,p1,nax);
358
359 728073 if(
360
2/2
✓ Branch 0 taken 145645 times.
✓ Branch 1 taken 582428 times.
728073 int(a1)*int(a2) > 0 &&
361
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 137335 times.
145645 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 728073 if(
369
2/2
✓ Branch 0 taken 144917 times.
✓ Branch 1 taken 583156 times.
728073 int(b1)*int(b2) > 0 &&
370
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 136607 times.
144917 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 728073 times.
✗ Branch 2 not taken.
728073 intersect_edge_edge_2d(E,e1,nax);
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 728073 times.
728073 if(finished()) { return; }
380
1/2
✓ Branch 1 taken 728073 times.
✗ Branch 2 not taken.
728073 intersect_edge_edge_2d(E,e2,nax);
381
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 728073 times.
728073 if(finished()) { return; }
382
1/2
✓ Branch 1 taken 728073 times.
✗ Branch 2 not taken.
728073 intersect_edge_edge_2d(E,e3,nax);
383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 728073 times.
728073 if(finished()) { return; }
384
385 728073 } 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 348587 times.
✓ Branch 1 taken 678945 times.
1027532 if(o1 == ZERO) {
393 348587 R1 = q1;
394
2/2
✓ Branch 0 taken 348587 times.
✓ Branch 1 taken 330358 times.
678945 } else if(o2 == ZERO) {
395 348587 R1 = q2;
396 }
397
398
1/2
✓ Branch 1 taken 1027532 times.
✗ Branch 2 not taken.
1027532 Sign oo1 = orient3d(p2,p3,q1,q2);
399
1/2
✓ Branch 1 taken 1027532 times.
✗ Branch 2 not taken.
1027532 Sign oo2 = orient3d(p3,p1,q1,q2);
400
401
2/2
✓ Branch 0 taken 371589 times.
✓ Branch 1 taken 655943 times.
1027532 if(int(oo1)*int(oo2) == -1) {
402 371589 return;
403 }
404
405
1/2
✓ Branch 1 taken 655943 times.
✗ Branch 2 not taken.
655943 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 655943 int nb_zeros = (oo1 == ZERO) + (oo2 == ZERO) + (oo3 == ZERO);
411
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 655943 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
655943 geo_debug_assert(nb_zeros != 3);
412
2/2
✓ Branch 0 taken 60488 times.
✓ Branch 1 taken 595455 times.
655943 if(nb_zeros == 1) {
413
2/2
✓ Branch 0 taken 30821 times.
✓ Branch 1 taken 29667 times.
60488 if(oo1 == ZERO) {
414 30821 R2 = e1;
415
2/2
✓ Branch 0 taken 25453 times.
✓ Branch 1 taken 4214 times.
29667 } else if(oo2 == ZERO) {
416 25453 R2 = e2;
417 } else {
418 4214 R2 = e3;
419 }
420
2/2
✓ Branch 0 taken 366193 times.
✓ Branch 1 taken 229262 times.
595455 } else if(nb_zeros == 2) {
421
2/2
✓ Branch 0 taken 114953 times.
✓ Branch 1 taken 251240 times.
366193 if(oo1 != ZERO) {
422 114953 R2 = p1;
423
2/2
✓ Branch 0 taken 121330 times.
✓ Branch 1 taken 129910 times.
251240 } else if(oo2 != ZERO) {
424 121330 R2 = p2;
425 } else {
426 129910 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 655943 bool outside =
439 1311886 (int(oo1) * int(oo2) == -1) ||
440
3/4
✓ Branch 0 taken 655943 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 439928 times.
✓ Branch 3 taken 216015 times.
1095871 (int(oo2) * int(oo3) == -1) ||
441
2/2
✓ Branch 0 taken 19941 times.
✓ Branch 1 taken 419987 times.
439928 (int(oo3) * int(oo1) == -1) ;
442
443
2/2
✓ Branch 0 taken 419987 times.
✓ Branch 1 taken 235956 times.
655943 if(!outside) {
444
1/2
✓ Branch 1 taken 419987 times.
✗ Branch 2 not taken.
419987 add_intersection(R1,R2);
445 }
446 }
447 }
448
449
450 2184219 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 2184219 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2184219 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2184219 geo_debug_assert(region_dim(E1) == 1);
458
2/8
✓ Branch 1 taken 2184219 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2184219 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2184219 geo_debug_assert(region_dim(E2) == 1);
459
460 2184219 TriangleRegion R1 = E1;
461 2184219 TriangleRegion R2 = E2;
462
463 TriangleRegion p1,p2;
464
1/2
✓ Branch 1 taken 2184219 times.
✗ Branch 2 not taken.
2184219 get_edge_vertices(E1,p1,p2);
465
466 TriangleRegion q1,q2;
467
1/2
✓ Branch 1 taken 2184219 times.
✗ Branch 2 not taken.
2184219 get_edge_vertices(E2,q1,q2);
468
469
1/2
✓ Branch 1 taken 2184219 times.
✗ Branch 2 not taken.
2184219 Sign a1 = orient2d(q1,q2,p1,nax);
470
1/2
✓ Branch 1 taken 2184219 times.
✗ Branch 2 not taken.
2184219 Sign a2 = orient2d(q1,q2,p2,nax);
471
472
4/4
✓ Branch 0 taken 376324 times.
✓ Branch 1 taken 1807895 times.
✓ Branch 2 taken 90134 times.
✓ Branch 3 taken 286190 times.
2184219 if(a1 == ZERO && a2 == ZERO) {
473 // Special case: 1D
474 // (the 2x2 edge extremities are aligned)
475
1/2
✓ Branch 1 taken 90134 times.
✗ Branch 2 not taken.
90134 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 286190 times.
✓ Branch 1 taken 1807895 times.
2094085 if(a1 == ZERO) {
480 286190 R1 = p1;
481
2/2
✓ Branch 0 taken 286288 times.
✓ Branch 1 taken 1521607 times.
1807895 } else if(a2 == ZERO) {
482 286288 R1 = p2;
483 }
484
485
1/2
✓ Branch 1 taken 2094085 times.
✗ Branch 2 not taken.
2094085 Sign b1 = orient2d(p1,p2,q1,nax);
486
1/2
✓ Branch 1 taken 2094085 times.
✗ Branch 2 not taken.
2094085 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 307500 times.
✓ Branch 1 taken 1786585 times.
2094085 if(b1 == ZERO) {
491 307500 R2 = q1;
492
2/2
✓ Branch 0 taken 307500 times.
✓ Branch 1 taken 1479085 times.
1786585 } else if(b2 == ZERO) {
493 307500 R2 = q2;
494 }
495
496
4/4
✓ Branch 0 taken 748448 times.
✓ Branch 1 taken 1345637 times.
✓ Branch 2 taken 596784 times.
✓ Branch 3 taken 151664 times.
2094085 if( int(a1)*int(a2) != 1 && int(b1)*int(b2) != 1) {
497
1/2
✓ Branch 1 taken 596784 times.
✗ Branch 2 not taken.
596784 add_intersection(R1,R2);
498 }
499 }
500 2184219 }
501
502 90134 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 90134 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 90134 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
90134 geo_debug_assert(region_dim(E1) == 1);
510
2/8
✓ Branch 1 taken 90134 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 90134 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
90134 geo_debug_assert(region_dim(E2) == 1);
511
512 TriangleRegion p1,p2;
513
1/2
✓ Branch 1 taken 90134 times.
✗ Branch 2 not taken.
90134 get_edge_vertices(E1,p1,p2);
514
515 TriangleRegion q1,q2;
516
1/2
✓ Branch 1 taken 90134 times.
✗ Branch 2 not taken.
90134 get_edge_vertices(E2,q1,q2);
517
518
1/2
✓ Branch 1 taken 90134 times.
✗ Branch 2 not taken.
90134 Sign d1 = dot3d(p1,q1,q2);
519
1/2
✓ Branch 1 taken 90134 times.
✗ Branch 2 not taken.
90134 Sign d2 = dot3d(p2,q1,q2);
520
1/2
✓ Branch 1 taken 90134 times.
✗ Branch 2 not taken.
90134 Sign d3 = dot3d(q1,p1,p2);
521
1/2
✓ Branch 1 taken 90134 times.
✗ Branch 2 not taken.
90134 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 62347 times.
✓ Branch 1 taken 27787 times.
✓ Branch 2 taken 52076 times.
✓ Branch 3 taken 10271 times.
✓ Branch 5 taken 52076 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10330 times.
✓ Branch 8 taken 41746 times.
✓ Branch 9 taken 10330 times.
✓ Branch 10 taken 79804 times.
90134 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 62349 times.
✓ Branch 1 taken 27785 times.
✓ Branch 2 taken 52077 times.
✓ Branch 3 taken 10272 times.
✓ Branch 5 taken 52077 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 52017 times.
✓ Branch 8 taken 60 times.
✓ Branch 9 taken 52017 times.
✓ Branch 10 taken 38117 times.
90134 if(d2 == ZERO && d3 == ZERO && points_are_identical(p2,q1)) {
533
1/2
✓ Branch 1 taken 52017 times.
✗ Branch 2 not taken.
52017 add_intersection(p2,q1);
534 }
535
536
9/10
✓ Branch 0 taken 62347 times.
✓ Branch 1 taken 27787 times.
✓ Branch 2 taken 52077 times.
✓ Branch 3 taken 10270 times.
✓ Branch 5 taken 52077 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 52017 times.
✓ Branch 8 taken 60 times.
✓ Branch 9 taken 52017 times.
✓ Branch 10 taken 38117 times.
90134 if(d1 == ZERO && d4 == ZERO && points_are_identical(p1,q2)) {
537
1/2
✓ Branch 1 taken 52017 times.
✗ Branch 2 not taken.
52017 add_intersection(p1,q2);
538 }
539
540
9/10
✓ Branch 0 taken 62349 times.
✓ Branch 1 taken 27785 times.
✓ Branch 2 taken 52078 times.
✓ Branch 3 taken 10271 times.
✓ Branch 5 taken 52078 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10332 times.
✓ Branch 8 taken 41746 times.
✓ Branch 9 taken 10332 times.
✓ Branch 10 taken 79802 times.
90134 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 89956 times.
90134 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 89956 times.
90134 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 89956 times.
90134 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 89956 times.
90134 if(d4 == NEGATIVE) {
560
1/2
✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
178 add_intersection(E1,q2);
561 }
562 90134 }
563
564 1158799 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 1037409 times.
✓ Branch 2 taken 121390 times.
✓ Branch 4 taken 31586 times.
✓ Branch 5 taken 1005823 times.
✓ Branch 6 taken 152976 times.
✓ Branch 7 taken 1005823 times.
1158799 if(region_dim(R1) >= 1 || region_dim(R2) >= 1) {
570 152976 has_non_degenerate_intersection_ = true;
571 }
572
2/2
✓ Branch 1 taken 556457 times.
✓ Branch 2 taken 602342 times.
1158799 if(is_in_T1(R1)) {
573
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 556457 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
556457 geo_debug_assert(!is_in_T1(R2));
574
1/2
✓ Branch 0 taken 556457 times.
✗ Branch 1 not taken.
556457 if(result_ != nullptr) {
575
2/4
✓ Branch 1 taken 556457 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 556457 times.
✗ Branch 5 not taken.
556457 result_->push_back(std::make_pair(R1,R2));
576 }
577 } else {
578
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 602342 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
602342 geo_debug_assert(is_in_T1(R2));
579
1/2
✓ Branch 0 taken 602342 times.
✗ Branch 1 not taken.
602342 if(result_ != nullptr) {
580
2/4
✓ Branch 1 taken 602342 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 602342 times.
✗ Branch 5 not taken.
602342 result_->push_back(std::make_pair(R2,R1));
581 }
582 }
583 1158799 }
584
585 9822349 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 9822349 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9822349 geo_debug_assert(region_dim(i) == 0);
591
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9822349 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9822349 geo_debug_assert(region_dim(j) == 0);
592
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9822349 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9822349 geo_debug_assert(region_dim(k) == 0);
593
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9822349 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9822349 geo_debug_assert(region_dim(l) == 0);
594
595 // Index for the cache (1 bit set for
596 // each vertex)
597
598 9822349 index_t o3d_idx =
599 9822349 (1u << index_t(i)) |
600 9822349 (1u << index_t(j)) |
601 9822349 (1u << index_t(k)) |
602 9822349 (1u << index_t(l)) ;
603
604
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 9822349 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
9822349 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 9822349 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 9822349 Numeric::uint64 o3d_mask = (Numeric::uint64(1u) << o3d_idx);
614
2/2
✓ Branch 0 taken 5422516 times.
✓ Branch 1 taken 4399833 times.
9822349 if((o3d_cached_ & o3d_mask) == 0) {
615
2/2
✓ Branch 0 taken 2180476 times.
✓ Branch 1 taken 3242040 times.
5422516 int o = flip ?
616 2180476 -int(PCK::orient_3d(p_[i],p_[j],p_[k],p_[l])) :
617 5422516 int(PCK::orient_3d(p_[i],p_[j],p_[k],p_[l])) ;
618 5422516 o3d_cache_[o3d_idx] = Numeric::int8(o);
619 5422516 o3d_cached_ |= o3d_mask;
620 }
621
622 // Get result from the cache
623
624
2/2
✓ Branch 0 taken 4745193 times.
✓ Branch 1 taken 5077156 times.
9822349 Sign result = flip ?
625 4745193 Sign(-o3d_cache_[o3d_idx]) :
626 5077156 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 9822349 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
9822349 geo_debug_assert(
631 result == PCK::orient_3d(p_[i], p_[j], p_[k], p_[l])
632 );
633
634 9822349 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 9822349 static bool odd_order(index_t i, index_t j, index_t k, index_t l) {
642 9822349 return (i > j) ^ (i > k) ^ (i > l) ^ (j > k) ^ (j > l) ^ (k > l);
643 }
644
645 12925046 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 12925046 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12925046 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
12925046 geo_debug_assert(region_dim(i) == 0);
652
2/8
✓ Branch 1 taken 12925046 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12925046 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
12925046 geo_debug_assert(region_dim(j) == 0);
653
2/8
✓ Branch 1 taken 12925046 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12925046 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
12925046 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 25850092 times.
✓ Branch 1 taken 12925046 times.
38775138 for(coord_index_t c = 0; c < 2; c++) {
658
1/2
✓ Branch 1 taken 25850092 times.
✗ Branch 2 not taken.
25850092 pi[c] = p_[i][index_t((normal_axis + 1 + c) % 3)];
659
1/2
✓ Branch 1 taken 25850092 times.
✗ Branch 2 not taken.
25850092 pj[c] = p_[j][index_t((normal_axis + 1 + c) % 3)];
660
1/2
✓ Branch 1 taken 25850092 times.
✗ Branch 2 not taken.
25850092 pk[c] = p_[k][index_t((normal_axis + 1 + c) % 3)];
661 }
662
1/2
✓ Branch 1 taken 12925046 times.
✗ Branch 2 not taken.
25850092 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 360536 Sign dot3d(
672 TriangleRegion i, TriangleRegion j, TriangleRegion k
673 ) const {
674
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 360536 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
360536 geo_debug_assert(region_dim(i) == 0);
675
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 360536 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
360536 geo_debug_assert(region_dim(j) == 0);
676
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 360536 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
360536 geo_debug_assert(region_dim(k) == 0);
677 360536 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 728073 coord_index_t normal_axis(
688 TriangleRegion v1, TriangleRegion v2, TriangleRegion v3
689 ) {
690
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 728073 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
728073 geo_debug_assert(region_dim(v1) == 0);
691
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 728073 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
728073 geo_debug_assert(region_dim(v2) == 0);
692
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 728073 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
728073 geo_debug_assert(region_dim(v3) == 0);
693
694 728073 const vec3& p1 = p_[v1];
695 728073 const vec3& p2 = p_[v2];
696 728073 const vec3& p3 = p_[v3];
697
698 728073 return PCK::triangle_normal_axis(p1,p2,p3);
699 }
700
701
702 208308 bool points_are_identical(TriangleRegion i, TriangleRegion j) {
703
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 208308 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
208308 geo_debug_assert(region_dim(i) == 0);
704
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 208308 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
208308 geo_debug_assert(region_dim(j) == 0);
705 208308 const vec3& p1 = p_[i];
706 208308 const vec3& p2 = p_[j];
707 return
708 208308 (p1[0] == p2[0]) &&
709
4/4
✓ Branch 0 taken 146036 times.
✓ Branch 1 taken 62272 times.
✓ Branch 4 taken 126616 times.
✓ Branch 5 taken 19420 times.
334924 (p1[1] == p2[1]) &&
710
2/2
✓ Branch 2 taken 124696 times.
✓ Branch 3 taken 1920 times.
334924 (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 2251042 index_t triangle_dim(
720 TriangleRegion i, TriangleRegion j, TriangleRegion k
721 ) {
722
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2251042 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2251042 geo_debug_assert(region_dim(i) == 0);
723
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2251042 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2251042 geo_debug_assert(region_dim(j) == 0);
724
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2251042 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2251042 geo_debug_assert(region_dim(k) == 0);
725
726 2251042 const vec3& p1 = p_[i];
727 2251042 const vec3& p2 = p_[j];
728 2251042 const vec3& p3 = p_[k];
729
730
1/2
✓ Branch 4 taken 2251042 times.
✗ Branch 5 not taken.
2251042 if(!PCK::aligned_3d(p1.data(), p2.data(), p3.data())) {
731 2251042 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 1125521 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 1125521 times.
✗ Branch 2 not taken.
1125521 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 1125521 times.
✗ Branch 2 not taken.
1125521 );
815 1125521 I.set_flags(flags);
816
1/2
✓ Branch 1 taken 1125521 times.
✗ Branch 2 not taken.
1125521 I.compute();
817 2251042 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 115777283 coord_index_t region_dim(TriangleRegion r) {
836
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 115777283 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
115777283 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 115777283 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 4414 times.
✓ Branch 1 taken 3718 times.
✓ Branch 2 taken 4721 times.
✓ Branch 3 taken 5968 times.
✓ Branch 4 taken 3666 times.
✓ Branch 5 taken 4003 times.
✓ Branch 6 taken 32386 times.
✓ Branch 7 taken 28779 times.
✓ Branch 8 taken 35689 times.
✓ Branch 9 taken 32838 times.
✓ Branch 10 taken 28609 times.
✓ Branch 11 taken 36311 times.
✓ Branch 12 taken 26059 times.
✓ Branch 13 taken 24371 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
271532 switch(R) {
846 4414 case T1_RGN_P0:
847 4414 result = T2_RGN_P0;
848 4414 break;
849 3718 case T1_RGN_P1:
850 3718 result = T2_RGN_P1;
851 3718 break;
852 4721 case T1_RGN_P2:
853 4721 result = T2_RGN_P2;
854 4721 break;
855 5968 case T2_RGN_P0:
856 5968 result = T1_RGN_P0;
857 5968 break;
858 3666 case T2_RGN_P1:
859 3666 result = T1_RGN_P1;
860 3666 break;
861 4003 case T2_RGN_P2:
862 4003 result = T1_RGN_P2;
863 4003 break;
864 32386 case T1_RGN_E0:
865 32386 result = T2_RGN_E0;
866 32386 break;
867 28779 case T1_RGN_E1:
868 28779 result = T2_RGN_E1;
869 28779 break;
870 35689 case T1_RGN_E2:
871 35689 result = T2_RGN_E2;
872 35689 break;
873 32838 case T2_RGN_E0:
874 32838 result = T1_RGN_E0;
875 32838 break;
876 28609 case T2_RGN_E1:
877 28609 result = T1_RGN_E1;
878 28609 break;
879 36311 case T2_RGN_E2:
880 36311 result = T1_RGN_E2;
881 36311 break;
882 26059 case T1_RGN_T:
883 26059 result = T2_RGN_T;
884 26059 break;
885 24371 case T2_RGN_T:
886 24371 result = T1_RGN_T;
887 24371 break;
888 ✗ case T_RGN_NB:
889 ✗ geo_assert_not_reached;
890 }
891 271532 return result;
892 }
893
894 3882560 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 3882560 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
3882560 geo_debug_assert(region_dim(T) == 2);
899
2/3
✓ Branch 0 taken 2355177 times.
✓ Branch 1 taken 1527383 times.
✗ Branch 2 not taken.
3882560 switch(T) {
900 2355177 case T1_RGN_T: {
901 2355177 p0 = T1_RGN_P0; p1 = T1_RGN_P1; p2 = T1_RGN_P2;
902 2355177 } break;
903 1527383 case T2_RGN_T: {
904 1527383 p0 = T2_RGN_P0; p1 = T2_RGN_P1; p2 = T2_RGN_P2;
905 1527383 } break;
906 ✗ default:
907 ✗ geo_assert_not_reached;
908 }
909 3882560 }
910
911 2158290 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 2158290 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2158290 geo_debug_assert(region_dim(T) == 2);
916
2/3
✓ Branch 0 taken 1079145 times.
✓ Branch 1 taken 1079145 times.
✗ Branch 2 not taken.
2158290 switch(T) {
917 1079145 case T1_RGN_T: {
918 1079145 e0 = T1_RGN_E0; e1 = T1_RGN_E1; e2 = T1_RGN_E2;
919 1079145 } break;
920 1079145 case T2_RGN_T: {
921 1079145 e0 = T2_RGN_E0; e1 = T2_RGN_E1; e2 = T2_RGN_E2;
922 1079145 } break;
923 ✗ default:
924 ✗ geo_assert_not_reached;
925 }
926 2158290 }
927
928 6773300 void get_edge_vertices(
929 TriangleRegion E, TriangleRegion& q0, TriangleRegion& q1
930 ) {
931
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 6773300 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
6773300 geo_debug_assert(region_dim(E) == 1);
932
6/7
✓ Branch 0 taken 1138735 times.
✓ Branch 1 taken 1126841 times.
✓ Branch 2 taken 1129315 times.
✓ Branch 3 taken 1126753 times.
✓ Branch 4 taken 1126368 times.
✓ Branch 5 taken 1125288 times.
✗ Branch 6 not taken.
6773300 switch(E) {
933 1138735 case T1_RGN_E0: {
934 1138735 q0 = T1_RGN_P1;
935 1138735 q1 = T1_RGN_P2;
936 1138735 } break;
937 1126841 case T1_RGN_E1: {
938 1126841 q0 = T1_RGN_P2;
939 1126841 q1 = T1_RGN_P0;
940 1126841 } break;
941 1129315 case T1_RGN_E2: {
942 1129315 q0 = T1_RGN_P0;
943 1129315 q1 = T1_RGN_P1;
944 1129315 } break;
945 1126753 case T2_RGN_E0: {
946 1126753 q0 = T2_RGN_P1;
947 1126753 q1 = T2_RGN_P2;
948 1126753 } break;
949 1126368 case T2_RGN_E1: {
950 1126368 q0 = T2_RGN_P2;
951 1126368 q1 = T2_RGN_P0;
952 1126368 } break;
953 1125288 case T2_RGN_E2: {
954 1125288 q0 = T2_RGN_P0;
955 1125288 q1 = T2_RGN_P1;
956 1125288 } break;
957 ✗ default:
958 ✗ geo_assert_not_reached;
959 }
960 6773300 }
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 126112 times.
✓ Branch 2 taken 80181 times.
206293 TriangleRegion R = is_in_T1(R1) ? T1_RGN_T : T2_RGN_T;
971
972
6/6
✓ Branch 1 taken 160492 times.
✓ Branch 2 taken 45801 times.
✓ Branch 4 taken 17152 times.
✓ Branch 5 taken 143340 times.
✓ Branch 6 taken 17152 times.
✓ Branch 7 taken 189141 times.
206293 if(region_dim(R1) == 1 && region_dim(R2) == 0) {
973 TriangleRegion v1,v2;
974
1/2
✓ Branch 1 taken 17152 times.
✗ Branch 2 not taken.
17152 get_edge_vertices(R1,v1,v2);
975
4/4
✓ Branch 0 taken 9482 times.
✓ Branch 1 taken 7670 times.
✓ Branch 2 taken 7577 times.
✓ Branch 3 taken 1905 times.
17152 if(R2 == v1 || R2 == v2) {
976 15247 R = R1;
977 }
978
6/6
✓ Branch 1 taken 155182 times.
✓ Branch 2 taken 33959 times.
✓ Branch 4 taken 14648 times.
✓ Branch 5 taken 140534 times.
✓ Branch 6 taken 14648 times.
✓ Branch 7 taken 174493 times.
189141 } else if(region_dim(R2) == 1 && region_dim(R1) == 0) {
979 TriangleRegion v1,v2;
980
1/2
✓ Branch 1 taken 14648 times.
✗ Branch 2 not taken.
14648 get_edge_vertices(R2,v1,v2);
981
4/4
✓ Branch 0 taken 8347 times.
✓ Branch 1 taken 6301 times.
✓ Branch 2 taken 6396 times.
✓ Branch 3 taken 1951 times.
14648 if(R1 == v1 || R1 == v2) {
982 12697 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 34504 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 34504 times.
✗ Branch 3 not taken.
44741 }
989 ) {
990 TriangleRegion v1,v2;
991
1/2
✓ Branch 1 taken 34504 times.
✗ Branch 2 not taken.
34504 get_edge_vertices(E,v1,v2);
992
8/8
✓ Branch 0 taken 6739 times.
✓ Branch 1 taken 27765 times.
✓ Branch 2 taken 2336 times.
✓ Branch 3 taken 4403 times.
✓ Branch 4 taken 8929 times.
✓ Branch 5 taken 21172 times.
✓ Branch 6 taken 5834 times.
✓ Branch 7 taken 3095 times.
34504 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