GCC Code Coverage Report


Directory: ./
File: mesh/triangle_intersection.cpp
Date: 2026-11-30 05:45:57
Exec Total Coverage
Lines: 307 360 85.3%
Functions: 14 18 77.8%
Branches: 200 287 69.7%

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 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 0 taken 6596796 times.
✓ Branch 1 taken 1099466 times.
7696262 ) : result_(result) {
102 1099466 p_[0] = p0;
103 1099466 p_[1] = p1;
104 1099466 p_[2] = p2;
105 1099466 p_[3] = q0;
106 1099466 p_[4] = q1;
107 1099466 p_[5] = q2;
108 1099466 o3d_cached_ = 0;
109 1099466 has_non_degenerate_intersection_ = false;
110 1099466 p_index_[0] = p0_index;
111 1099466 p_index_[1] = p1_index;
112 1099466 p_index_[2] = p2_index;
113 1099466 p_index_[3] = q0_index;
114 1099466 p_index_[4] = q1_index;
115 1099466 p_index_[5] = q2_index;
116 1099466 has_global_indices_ = true;
117 flags_ = TRIANGLES_INTERSECTION_DEFAULT;
118 }
119
120 void set_flags(TriangleIsectFlags flags) {
121 1099466 flags_ = flags;
122 }
123
124 1099466 void compute() {
125 #ifdef TT_DEBUG
126 Logger::out("TT") << "Call compute()" << std::endl;
127 #endif
128
129
1/2
✓ Branch 0 taken 1099466 times.
✗ Branch 1 not taken.
1099466 if(result_ != nullptr) {
130 result_->resize(0);
131 }
132
133 bool test_degenerate = (
134 1099466 (flags_ & TRIANGLES_INTERSECTION_NO_DEGENERATE) == 0
135 );
136
137 bool test_bbox = (
138 (flags_ & TRIANGLES_INTERSECTION_NO_BBOX) == 0
139 );
140
141 // Test for degenerate triangles
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1099466 times.
1099466 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 geo_debug_assert(
158 triangle_dim(T1_RGN_P0, T1_RGN_P1, T1_RGN_P2) == 2
159 );
160 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 1099466 times.
1099466 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 1099466 times.
✗ Branch 1 not taken.
1099466 if(has_global_indices_) {
180
181 1099466 index_t q_index[3] = {
182 NO_INDEX, NO_INDEX, NO_INDEX
183 };
184
185
2/2
✓ Branch 0 taken 3298398 times.
✓ Branch 1 taken 1099466 times.
4397864 for(index_t i=0; i<3; ++i) {
186
2/2
✓ Branch 0 taken 9895194 times.
✓ Branch 1 taken 3298398 times.
13193592 for(index_t j=0; j<3; ++j) {
187
2/2
✓ Branch 0 taken 1002669 times.
✓ Branch 1 taken 8892525 times.
9895194 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 1099466 int nb_shared = (
196 1099466 (q_index[0] != NO_INDEX) +
197 1099466 (q_index[1] != NO_INDEX) +
198 1099466 (q_index[2] != NO_INDEX) ) ;
199 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 495877 times.
1099466 if(nb_shared == 1) {
204 603589 int shared_q =
205
2/2
✓ Branch 0 taken 403321 times.
✓ Branch 1 taken 200268 times.
603589 (q_index[1] != NO_INDEX) + (q_index[2] != NO_INDEX)*2;
206 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 603589 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
213 603589 Sign o1 = orient3d(p1,p2,p3,q1);
214 603589 Sign o2 = orient3d(p1,p2,p3,q2);
215
2/2
✓ Branch 0 taken 479659 times.
✓ Branch 1 taken 123930 times.
603589 if(int(o1)*int(o2) > 0) {
216 479659 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 420267 times.
619807 if(nb_shared == 2) {
223 199540 int non_shared_q =
224
2/2
✓ Branch 0 taken 132919 times.
✓ Branch 1 taken 66621 times.
199540 (q_index[1] == NO_INDEX) + (q_index[2] == NO_INDEX)*2;
225 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 199540 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
229
2/2
✓ Branch 1 taken 178634 times.
✓ Branch 2 taken 20906 times.
199540 if(orient3d(p1,p2,p3,q) != ZERO) {
230 178634 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 441173 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
242 441173 get_triangle_vertices(T2_RGN_T, q1,q2,q3);
243 441173 Sign o1 = orient3d(q1,q2,q3,p1);
244 441173 Sign o2 = orient3d(q1,q2,q3,p2);
245 441173 Sign o3 = orient3d(q1,q2,q3,p3);
246 441173 if(
247
2/2
✓ Branch 0 taken 147083 times.
✓ Branch 1 taken 294090 times.
441173 int(o1)*int(o2) == 1 &&
248
2/2
✓ Branch 0 taken 87873 times.
✓ Branch 1 taken 59210 times.
147083 int(o2)*int(o3) == 1 &&
249
1/2
✓ Branch 0 taken 87873 times.
✗ Branch 1 not taken.
87873 int(o3)*int(o1) == 1
250 ) {
251 87873 return;
252 }
253 }
254
255 353300 intersect_edge_triangle(T1_RGN_E0, T2_RGN_T);
256 ✗ if(finished()) { return; }
257 353300 intersect_edge_triangle(T1_RGN_E1, T2_RGN_T);
258 ✗ if(finished()) { return; }
259 353300 intersect_edge_triangle(T1_RGN_E2, T2_RGN_T);
260 ✗ if(finished()) { return; }
261
262 353300 intersect_edge_triangle(T2_RGN_E0, T1_RGN_T);
263 ✗ if(finished()) { return; }
264 353300 intersect_edge_triangle(T2_RGN_E1, T1_RGN_T);
265 ✗ if(finished()) { return; }
266 353300 intersect_edge_triangle(T2_RGN_E2, T1_RGN_T);
267 ✗ if(finished()) { return; }
268
269 // The same intersection can appear several times,
270 // remove the duplicates
271
1/2
✓ Branch 0 taken 353300 times.
✗ Branch 1 not taken.
353300 if(result_ != nullptr) {
272 std::sort(result_->begin(), result_->end());
273 auto p = std::unique(result_->begin(), result_->end());
274 353300 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 bool has_non_degenerate_intersection() const {
293 1099466 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 bool finished() const {
304 #ifdef TT_DEBUG
305 return false;
306 #endif
307
11/34
✗ Branch 0 not taken.
✓ Branch 1 taken 8310 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8310 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 728122 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 728122 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 728122 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 353300 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 353300 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 353300 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 353300 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 353300 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 353300 times.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
4320786 return (result_ == nullptr && has_non_degenerate_intersection_);
308 }
309
310 2119800 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 geo_debug_assert(region_dim(E) == 1);
318 geo_debug_assert(region_dim(T) == 2);
319
320 TriangleRegion R1 = E;
321 TriangleRegion R2 = T;
322
323 TriangleRegion p1,p2,p3;
324 TriangleRegion e1,e2,e3;
325 TriangleRegion q1,q2;
326
327 2119800 get_triangle_vertices(T,p1,p2,p3);
328 2119800 get_triangle_edges(T,e1,e2,e3);
329 2119800 get_edge_vertices(E,q1,q2);
330
331 2119800 Sign o1 = orient3d(p1,p2,p3,q1);
332 2119800 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 1722116 times.
✓ Branch 1 taken 397684 times.
2119800 if(int(o1) * int(o2) == 1) {
338 761854 return;
339 }
340
341
2/2
✓ Branch 0 taken 728122 times.
✓ Branch 1 taken 993994 times.
1722116 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 1456244 index_t nax = normal_axis(p1,p2,p3);
347
348 // Test whether the extremities of the segment
349 // are in the triangle
350 {
351 728122 Sign a1 = orient2d(q1,p1,p2,nax);
352 728122 Sign a2 = orient2d(q1,p2,p3,nax);
353 728122 Sign a3 = orient2d(q1,p3,p1,nax);
354
355 728122 Sign b1 = orient2d(q2,p1,p2,nax);
356 728122 Sign b2 = orient2d(q2,p2,p3,nax);
357 728122 Sign b3 = orient2d(q2,p3,p1,nax);
358
359 728122 if(
360
2/2
✓ Branch 0 taken 145717 times.
✓ Branch 1 taken 582405 times.
728122 int(a1)*int(a2) > 0 &&
361
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 137407 times.
145717 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 8310 add_intersection(q1,T);
365 ✗ if(finished()) { return; }
366 }
367
368 728122 if(
369
2/2
✓ Branch 0 taken 144981 times.
✓ Branch 1 taken 583141 times.
728122 int(b1)*int(b2) > 0 &&
370
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 136671 times.
144981 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 8310 add_intersection(q2,T);
374 ✗ if(finished()) { return; }
375 }
376 }
377
378 728122 intersect_edge_edge_2d(E,e1,nax);
379 ✗ if(finished()) { return; }
380 728122 intersect_edge_edge_2d(E,e2,nax);
381 ✗ if(finished()) { return; }
382 728122 intersect_edge_edge_2d(E,e3,nax);
383 ✗ if(finished()) { return; }
384
385 } 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 337239 times.
✓ Branch 1 taken 656755 times.
993994 if(o1 == ZERO) {
393 337239 R1 = q1;
394
2/2
✓ Branch 0 taken 337239 times.
✓ Branch 1 taken 319516 times.
656755 } else if(o2 == ZERO) {
395 337239 R1 = q2;
396 }
397
398 993994 Sign oo1 = orient3d(p2,p3,q1,q2);
399 993994 Sign oo2 = orient3d(p3,p1,q1,q2);
400
401
2/2
✓ Branch 0 taken 629824 times.
✓ Branch 1 taken 364170 times.
993994 if(int(oo1)*int(oo2) == -1) {
402 return;
403 }
404
405 629824 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 629824 int nb_zeros = (oo1 == ZERO) + (oo2 == ZERO) + (oo3 == ZERO);
411 geo_debug_assert(nb_zeros != 3);
412
2/2
✓ Branch 0 taken 60524 times.
✓ Branch 1 taken 569300 times.
629824 if(nb_zeros == 1) {
413
2/2
✓ Branch 0 taken 30877 times.
✓ Branch 1 taken 29647 times.
60524 if(oo1 == ZERO) {
414 30877 R2 = e1;
415
2/2
✓ Branch 0 taken 25429 times.
✓ Branch 1 taken 4218 times.
29647 } else if(oo2 == ZERO) {
416 25429 R2 = e2;
417 } else {
418 4218 R2 = e3;
419 }
420
2/2
✓ Branch 0 taken 343492 times.
✓ Branch 1 taken 225808 times.
569300 } else if(nb_zeros == 2) {
421
2/2
✓ Branch 0 taken 107282 times.
✓ Branch 1 taken 236210 times.
343492 if(oo1 != ZERO) {
422 107282 R2 = p1;
423
2/2
✓ Branch 0 taken 113928 times.
✓ Branch 1 taken 122282 times.
236210 } else if(oo2 != ZERO) {
424 113928 R2 = p2;
425 } else {
426 122282 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 bool outside =
439 629824 (int(oo1) * int(oo2) == -1) ||
440
2/2
✓ Branch 0 taken 417209 times.
✓ Branch 1 taken 212615 times.
629824 (int(oo2) * int(oo3) == -1) ||
441
2/2
✓ Branch 0 taken 397286 times.
✓ Branch 1 taken 19923 times.
417209 (int(oo3) * int(oo1) == -1) ;
442
443 if(!outside) {
444 397286 add_intersection(R1,R2);
445 }
446 }
447 }
448
449
450 2184366 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 geo_debug_assert(region_dim(E1) == 1);
458 geo_debug_assert(region_dim(E2) == 1);
459
460 TriangleRegion R1 = E1;
461 TriangleRegion R2 = E2;
462
463 TriangleRegion p1,p2;
464 2184366 get_edge_vertices(E1,p1,p2);
465
466 TriangleRegion q1,q2;
467 2184366 get_edge_vertices(E2,q1,q2);
468
469 2184366 Sign a1 = orient2d(q1,q2,p1,nax);
470 2184366 Sign a2 = orient2d(q1,q2,p2,nax);
471
472
2/2
✓ Branch 0 taken 90140 times.
✓ Branch 1 taken 2094226 times.
2184366 if(a1 == ZERO && a2 == ZERO) {
473 // Special case: 1D
474 // (the 2x2 edge extremities are aligned)
475 90140 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 1808036 times.
2094226 if(a1 == ZERO) {
480 286190 R1 = p1;
481
2/2
✓ Branch 0 taken 286298 times.
✓ Branch 1 taken 1521738 times.
1808036 } else if(a2 == ZERO) {
482 286298 R1 = p2;
483 }
484
485 2094226 Sign b1 = orient2d(p1,p2,q1,nax);
486 2094226 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 307507 times.
✓ Branch 1 taken 1786719 times.
2094226 if(b1 == ZERO) {
491 307507 R2 = q1;
492
2/2
✓ Branch 0 taken 307507 times.
✓ Branch 1 taken 1479212 times.
1786719 } else if(b2 == ZERO) {
493 307507 R2 = q2;
494 }
495
496
4/4
✓ Branch 0 taken 748477 times.
✓ Branch 1 taken 1345749 times.
✓ Branch 2 taken 596798 times.
✓ Branch 3 taken 151679 times.
2094226 if( int(a1)*int(a2) != 1 && int(b1)*int(b2) != 1) {
497 596798 add_intersection(R1,R2);
498 }
499 }
500 2184366 }
501
502 90140 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 geo_debug_assert(region_dim(E1) == 1);
510 geo_debug_assert(region_dim(E2) == 1);
511
512 TriangleRegion p1,p2;
513 90140 get_edge_vertices(E1,p1,p2);
514
515 TriangleRegion q1,q2;
516 90140 get_edge_vertices(E2,q1,q2);
517
518 90140 Sign d1 = dot3d(p1,q1,q2);
519 90140 Sign d2 = dot3d(p2,q1,q2);
520 90140 Sign d3 = dot3d(q1,p1,p2);
521 90140 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
2/2
✓ Branch 0 taken 52082 times.
✓ Branch 1 taken 38058 times.
90140 if(d1 == ZERO && d3 == ZERO && points_are_identical(p1,q1)) {
529 10332 add_intersection(p1,q1);
530 }
531
532
2/2
✓ Branch 0 taken 52083 times.
✓ Branch 1 taken 38057 times.
90140 if(d2 == ZERO && d3 == ZERO && points_are_identical(p2,q1)) {
533 52021 add_intersection(p2,q1);
534 }
535
536
2/2
✓ Branch 0 taken 52083 times.
✓ Branch 1 taken 38057 times.
90140 if(d1 == ZERO && d4 == ZERO && points_are_identical(p1,q2)) {
537 52021 add_intersection(p1,q2);
538 }
539
540
2/2
✓ Branch 0 taken 52084 times.
✓ Branch 1 taken 38056 times.
90140 if(d2 == ZERO && d4 == ZERO && points_are_identical(p2,q2)) {
541 10334 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 89962 times.
90140 if(d1 == NEGATIVE) {
548 178 add_intersection(p1,E2);
549 }
550
551
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89962 times.
90140 if(d2 == NEGATIVE) {
552 178 add_intersection(p2,E2);
553 }
554
555
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89962 times.
90140 if(d3 == NEGATIVE) {
556 178 add_intersection(E1,q1);
557 }
558
559
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89962 times.
90140 if(d4 == NEGATIVE) {
560 178 add_intersection(E1,q2);
561 }
562 90140 }
563
564 1136124 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
4/4
✓ Branch 1 taken 1014734 times.
✓ Branch 2 taken 121390 times.
✓ Branch 4 taken 31586 times.
✓ Branch 5 taken 983148 times.
1136124 if(region_dim(R1) >= 1 || region_dim(R2) >= 1) {
570 152976 has_non_degenerate_intersection_ = true;
571 }
572 if(is_in_T1(R1)) {
573 geo_debug_assert(!is_in_T1(R2));
574
1/2
✓ Branch 0 taken 545238 times.
✗ Branch 1 not taken.
545238 if(result_ != nullptr) {
575 545238 result_->push_back(std::make_pair(R1,R2));
576 }
577 } else {
578 geo_debug_assert(is_in_T1(R2));
579
1/2
✓ Branch 0 taken 590886 times.
✗ Branch 1 not taken.
590886 if(result_ != nullptr) {
580 590886 result_->push_back(std::make_pair(R2,R1));
581 }
582 }
583 1136124 }
584
585 9587649 Sign orient3d(
586 TriangleRegion i, TriangleRegion j,
587 TriangleRegion k, TriangleRegion l
588 ) const {
589
590 geo_debug_assert(region_dim(i) == 0);
591 geo_debug_assert(region_dim(j) == 0);
592 geo_debug_assert(region_dim(k) == 0);
593 geo_debug_assert(region_dim(l) == 0);
594
595 // Index for the cache (1 bit set for
596 // each vertex)
597
598 9587649 index_t o3d_idx =
599 9587649 (1u << index_t(i)) |
600 9587649 (1u << index_t(j)) |
601 9587649 (1u << index_t(k)) |
602 9587649 (1u << index_t(l)) ;
603
604 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 9587649 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 9587649 Numeric::uint64 o3d_mask = (Numeric::uint64(1u) << o3d_idx);
614 9587649 if((o3d_cached_ & o3d_mask) == 0) {
615
2/2
✓ Branch 0 taken 2134644 times.
✓ Branch 1 taken 3160937 times.
5295581 int o = flip ?
616 2134644 -int(PCK::orient_3d(p_[i],p_[j],p_[k],p_[l])) :
617 5295581 int(PCK::orient_3d(p_[i],p_[j],p_[k],p_[l])) ;
618 5295581 o3d_cache_[o3d_idx] = Numeric::int8(o);
619 5295581 o3d_cached_ |= o3d_mask;
620 }
621
622 // Get result from the cache
623
624
2/2
✓ Branch 0 taken 4643379 times.
✓ Branch 1 taken 4944270 times.
9587649 Sign result = flip ?
625 4643379 Sign(-o3d_cache_[o3d_idx]) :
626 4944270 Sign( o3d_cache_[o3d_idx]) ;
627
628 // Sanity check: did our cache return the same result as
629 // directly calling the predicate ?
630 geo_debug_assert(
631 result == PCK::orient_3d(p_[i], p_[j], p_[k], p_[l])
632 );
633
634 9587649 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 static bool odd_order(index_t i, index_t j, index_t k, index_t l) {
642
2/2
✓ Branch 0 taken 5295581 times.
✓ Branch 1 taken 4292068 times.
9587649 return (i > j) ^ (i > k) ^ (i > l) ^ (j > k) ^ (j > l) ^ (k > l);
643 }
644
645 12925916 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 geo_debug_assert(region_dim(i) == 0);
652 geo_debug_assert(region_dim(j) == 0);
653 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 25851832 times.
✓ Branch 1 taken 12925916 times.
38777748 for(coord_index_t c = 0; c < 2; c++) {
658 25851832 pi[c] = p_[i][index_t((normal_axis + 1 + c) % 3)];
659 25851832 pj[c] = p_[j][index_t((normal_axis + 1 + c) % 3)];
660 25851832 pk[c] = p_[k][index_t((normal_axis + 1 + c) % 3)];
661 }
662 12925916 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 Sign dot3d(
672 TriangleRegion i, TriangleRegion j, TriangleRegion k
673 ) const {
674 geo_debug_assert(region_dim(i) == 0);
675 geo_debug_assert(region_dim(j) == 0);
676 geo_debug_assert(region_dim(k) == 0);
677 90140 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 coord_index_t normal_axis(
688 TriangleRegion v1, TriangleRegion v2, TriangleRegion v3
689 ) {
690 geo_debug_assert(region_dim(v1) == 0);
691 geo_debug_assert(region_dim(v2) == 0);
692 geo_debug_assert(region_dim(v3) == 0);
693
694 728122 const vec3& p1 = p_[v1];
695 728122 const vec3& p2 = p_[v2];
696 728122 const vec3& p3 = p_[v3];
697
698 728122 return PCK::triangle_normal_axis(p1,p2,p3);
699 }
700
701
702 bool points_are_identical(TriangleRegion i, TriangleRegion j) {
703 geo_debug_assert(region_dim(i) == 0);
704 geo_debug_assert(region_dim(j) == 0);
705 208332 const vec3& p1 = p_[i];
706 208332 const vec3& p2 = p_[j];
707 return
708 354404 (p1[0] == p2[0]) &&
709
16/24
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 20988 times.
✓ Branch 9 taken 31094 times.
✓ Branch 10 taken 11288 times.
✓ Branch 11 taken 9700 times.
✓ Branch 12 taken 52047 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 52025 times.
✓ Branch 15 taken 22 times.
✓ Branch 16 taken 52047 times.
✓ Branch 17 taken 36 times.
✓ Branch 18 taken 52025 times.
✓ Branch 19 taken 22 times.
✓ Branch 20 taken 20990 times.
✓ Branch 21 taken 31094 times.
✓ Branch 22 taken 11290 times.
✓ Branch 23 taken 9700 times.
208332 (p1[1] == p2[1]) &&
710
8/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10332 times.
✓ Branch 5 taken 956 times.
✓ Branch 6 taken 52021 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 52021 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 10334 times.
✓ Branch 11 taken 956 times.
126628 (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 ✗ index_t triangle_dim(
720 TriangleRegion i, TriangleRegion j, TriangleRegion k
721 ) {
722 geo_debug_assert(region_dim(i) == 0);
723 geo_debug_assert(region_dim(j) == 0);
724 geo_debug_assert(region_dim(k) == 0);
725
726 ✗ const vec3& p1 = p_[i];
727 ✗ const vec3& p2 = p_[j];
728 ✗ const vec3& p3 = p_[k];
729
730 ✗ if(!PCK::aligned_3d(p1.data(), p2.data(), p3.data())) {
731 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 1099466 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 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 );
815 I.set_flags(flags);
816 1099466 I.compute();
817 1099466 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 5552184 coord_index_t region_dim(TriangleRegion r) {
836 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 5552184 return trgl_rgn_dim[index_t(r)];
841 }
842
843 271532 TriangleRegion swap_T1_T2(TriangleRegion R) {
844 TriangleRegion result=T_RGN_NB;
845
14/16
✓ Branch 0 taken 4435 times.
✓ Branch 1 taken 3759 times.
✓ Branch 2 taken 4744 times.
✓ Branch 3 taken 5945 times.
✓ Branch 4 taken 3625 times.
✓ Branch 5 taken 3982 times.
✓ Branch 6 taken 32396 times.
✓ Branch 7 taken 28775 times.
✓ Branch 8 taken 35690 times.
✓ Branch 9 taken 32831 times.
✓ Branch 10 taken 28607 times.
✓ Branch 11 taken 36313 times.
✓ Branch 12 taken 25967 times.
✓ Branch 13 taken 24463 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
271532 switch(R) {
846 4435 case T1_RGN_P0:
847 result = T2_RGN_P0;
848 4435 break;
849 3759 case T1_RGN_P1:
850 result = T2_RGN_P1;
851 3759 break;
852 4744 case T1_RGN_P2:
853 result = T2_RGN_P2;
854 4744 break;
855 5945 case T2_RGN_P0:
856 result = T1_RGN_P0;
857 5945 break;
858 3625 case T2_RGN_P1:
859 result = T1_RGN_P1;
860 3625 break;
861 3982 case T2_RGN_P2:
862 result = T1_RGN_P2;
863 3982 break;
864 32396 case T1_RGN_E0:
865 result = T2_RGN_E0;
866 32396 break;
867 28775 case T1_RGN_E1:
868 result = T2_RGN_E1;
869 28775 break;
870 35690 case T1_RGN_E2:
871 result = T2_RGN_E2;
872 35690 break;
873 32831 case T2_RGN_E0:
874 result = T1_RGN_E0;
875 32831 break;
876 28607 case T2_RGN_E1:
877 result = T1_RGN_E1;
878 28607 break;
879 36313 case T2_RGN_E2:
880 result = T1_RGN_E2;
881 36313 break;
882 25967 case T1_RGN_T:
883 result = T2_RGN_T;
884 25967 break;
885 24463 case T2_RGN_T:
886 result = T1_RGN_T;
887 24463 break;
888 case T_RGN_NB:
889 ✗ geo_assert_not_reached;
890 }
891 271532 return result;
892 }
893
894 3805275 void get_triangle_vertices(
895 TriangleRegion T,
896 TriangleRegion& p0, TriangleRegion& p1, TriangleRegion& p2
897 ) {
898 geo_debug_assert(region_dim(T) == 2);
899
2/3
✓ Branch 0 taken 2304202 times.
✓ Branch 1 taken 1501073 times.
✗ Branch 2 not taken.
3805275 switch(T) {
900 2304202 case T1_RGN_T: {
901 2304202 p0 = T1_RGN_P0; p1 = T1_RGN_P1; p2 = T1_RGN_P2;
902 2304202 } break;
903 1501073 case T2_RGN_T: {
904 1501073 p0 = T2_RGN_P0; p1 = T2_RGN_P1; p2 = T2_RGN_P2;
905 1501073 } break;
906 default:
907 ✗ geo_assert_not_reached;
908 }
909 3805275 }
910
911 2119800 void get_triangle_edges(
912 TriangleRegion T,
913 TriangleRegion& e0, TriangleRegion& e1, TriangleRegion& e2
914 ) {
915 geo_debug_assert(region_dim(T) == 2);
916
2/3
✓ Branch 0 taken 1059900 times.
✓ Branch 1 taken 1059900 times.
✗ Branch 2 not taken.
2119800 switch(T) {
917 1059900 case T1_RGN_T: {
918 1059900 e0 = T1_RGN_E0; e1 = T1_RGN_E1; e2 = T1_RGN_E2;
919 1059900 } break;
920 1059900 case T2_RGN_T: {
921 1059900 e0 = T2_RGN_E0; e1 = T2_RGN_E1; e2 = T2_RGN_E2;
922 1059900 } break;
923 default:
924 ✗ geo_assert_not_reached;
925 }
926 2119800 }
927
928 6735023 void get_edge_vertices(
929 TriangleRegion E, TriangleRegion& q0, TriangleRegion& q1
930 ) {
931 geo_debug_assert(region_dim(E) == 1);
932
6/7
✓ Branch 0 taken 1132443 times.
✓ Branch 1 taken 1120472 times.
✓ Branch 2 taken 1122867 times.
✓ Branch 3 taken 1120326 times.
✓ Branch 4 taken 1119931 times.
✓ Branch 5 taken 1118984 times.
✗ Branch 6 not taken.
6735023 switch(E) {
933 1132443 case T1_RGN_E0: {
934 1132443 q0 = T1_RGN_P1;
935 1132443 q1 = T1_RGN_P2;
936 1132443 } break;
937 1120472 case T1_RGN_E1: {
938 1120472 q0 = T1_RGN_P2;
939 1120472 q1 = T1_RGN_P0;
940 1120472 } break;
941 1122867 case T1_RGN_E2: {
942 1122867 q0 = T1_RGN_P0;
943 1122867 q1 = T1_RGN_P1;
944 1122867 } break;
945 1120326 case T2_RGN_E0: {
946 1120326 q0 = T2_RGN_P1;
947 1120326 q1 = T2_RGN_P2;
948 1120326 } break;
949 1119931 case T2_RGN_E1: {
950 1119931 q0 = T2_RGN_P2;
951 1119931 q1 = T2_RGN_P0;
952 1119931 } break;
953 1118984 case T2_RGN_E2: {
954 1118984 q0 = T2_RGN_P0;
955 1118984 q1 = T2_RGN_P1;
956 1118984 } break;
957 default:
958 ✗ geo_assert_not_reached;
959 }
960 6735023 }
961
962 328516 TriangleRegion GEOGRAM_API regions_convex_hull(
963 TriangleRegion R1, TriangleRegion R2
964 ) {
965 geo_debug_assert(is_in_T1(R1) == is_in_T1(R2));
966
2/2
✓ Branch 0 taken 206293 times.
✓ Branch 1 taken 122223 times.
328516 if(R1 == R2) {
967 return R1;
968 }
969
970 TriangleRegion R = is_in_T1(R1) ? T1_RGN_T : T2_RGN_T;
971
972
4/4
✓ Branch 1 taken 160545 times.
✓ Branch 2 taken 45748 times.
✓ Branch 4 taken 143364 times.
✓ Branch 5 taken 17181 times.
206293 if(region_dim(R1) == 1 && region_dim(R2) == 0) {
973 TriangleRegion v1,v2;
974 17181 get_edge_vertices(R1,v1,v2);
975
4/4
✓ Branch 0 taken 9498 times.
✓ Branch 1 taken 7683 times.
✓ Branch 2 taken 7587 times.
✓ Branch 3 taken 1911 times.
17181 if(R2 == v1 || R2 == v2) {
976 R = R1;
977 }
978
4/4
✓ Branch 1 taken 155129 times.
✓ Branch 2 taken 33983 times.
✓ Branch 4 taken 140510 times.
✓ Branch 5 taken 14619 times.
189112 } else if(region_dim(R2) == 1 && region_dim(R1) == 0) {
979 TriangleRegion v1,v2;
980 14619 get_edge_vertices(R2,v1,v2);
981
4/4
✓ Branch 0 taken 8331 times.
✓ Branch 1 taken 6288 times.
✓ Branch 2 taken 6386 times.
✓ Branch 3 taken 1945 times.
14619 if(R1 == v1 || R1 == v2) {
982 R = R2;
983 }
984
4/4
✓ Branch 1 taken 10419 times.
✓ Branch 2 taken 164074 times.
✓ Branch 4 taken 10237 times.
✓ Branch 5 taken 182 times.
174493 } else if(region_dim(R1) == 0 && region_dim(R2) == 0) {
985 24174 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 0 taken 34411 times.
✗ Branch 1 not taken.
34411 }
989 ) {
990 TriangleRegion v1,v2;
991 34411 get_edge_vertices(E,v1,v2);
992
8/8
✓ Branch 0 taken 6742 times.
✓ Branch 1 taken 27669 times.
✓ Branch 2 taken 2336 times.
✓ Branch 3 taken 4406 times.
✓ Branch 4 taken 8929 times.
✓ Branch 5 taken 21076 times.
✓ Branch 6 taken 5831 times.
✓ Branch 7 taken 3098 times.
34411 if((R1 == v1 && R2 == v2) || (R1 == v2 && R2 == v1)) {
993 R = E;
994 10237 break;
995 }
996 }
997 }
998
999 return R;
1000 }
1001
1002 }
1003