GCC Code Coverage Report


Directory: ./
File: mesh/triangle_intersection.cpp
Date: 2026-11-30 05:48:47
Exec Total Coverage
Lines: 306 359 85.2%
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 6753120 times.
✓ Branch 1 taken 1125520 times.
7878640 ) : result_(result) {
102 1125520 p_[0] = p0;
103 1125520 p_[1] = p1;
104 1125520 p_[2] = p2;
105 1125520 p_[3] = q0;
106 1125520 p_[4] = q1;
107 1125520 p_[5] = q2;
108 1125520 o3d_cached_ = 0;
109 1125520 has_non_degenerate_intersection_ = false;
110 1125520 p_index_[0] = p0_index;
111 1125520 p_index_[1] = p1_index;
112 1125520 p_index_[2] = p2_index;
113 1125520 p_index_[3] = q0_index;
114 1125520 p_index_[4] = q1_index;
115 1125520 p_index_[5] = q2_index;
116 1125520 has_global_indices_ = true;
117 flags_ = TRIANGLES_INTERSECTION_DEFAULT;
118 }
119
120 void set_flags(TriangleIsectFlags flags) {
121 1125520 flags_ = flags;
122 }
123
124 1125520 void compute() {
125 #ifdef TT_DEBUG
126 Logger::out("TT") << "Call compute()" << std::endl;
127 #endif
128
129
1/2
✓ Branch 0 taken 1125520 times.
✗ Branch 1 not taken.
1125520 if(result_ != nullptr) {
130 result_->resize(0);
131 }
132
133 bool test_degenerate = (
134 1125520 (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 1125520 times.
1125520 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 1125520 times.
1125520 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 1125520 times.
✗ Branch 1 not taken.
1125520 if(has_global_indices_) {
180
181 1125520 index_t q_index[3] = {
182 NO_INDEX, NO_INDEX, NO_INDEX
183 };
184
185
2/2
✓ Branch 0 taken 3376560 times.
✓ Branch 1 taken 1125520 times.
4502080 for(index_t i=0; i<3; ++i) {
186
2/2
✓ Branch 0 taken 10129680 times.
✓ Branch 1 taken 3376560 times.
13506240 for(index_t j=0; j<3; ++j) {
187
2/2
✓ Branch 0 taken 1033334 times.
✓ Branch 1 taken 9096346 times.
10129680 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 1125520 int nb_shared = (
196 1125520 (q_index[0] != NO_INDEX) +
197 1125520 (q_index[1] != NO_INDEX) +
198 1125520 (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 622254 times.
✓ Branch 1 taken 503266 times.
1125520 if(nb_shared == 1) {
204 622254 int shared_q =
205
2/2
✓ Branch 0 taken 415271 times.
✓ Branch 1 taken 206983 times.
622254 (q_index[1] != NO_INDEX) + (q_index[2] != NO_INDEX)*2;
206 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 622254 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
213 622254 Sign o1 = orient3d(p1,p2,p3,q1);
214 622254 Sign o2 = orient3d(p1,p2,p3,q2);
215
2/2
✓ Branch 0 taken 492651 times.
✓ Branch 1 taken 129603 times.
622254 if(int(o1)*int(o2) > 0) {
216 492651 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 427329 times.
632869 if(nb_shared == 2) {
223 205540 int non_shared_q =
224
2/2
✓ Branch 0 taken 136976 times.
✓ Branch 1 taken 68564 times.
205540 (q_index[1] == NO_INDEX) + (q_index[2] == NO_INDEX)*2;
225 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 205540 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
229
2/2
✓ Branch 1 taken 184636 times.
✓ Branch 2 taken 20904 times.
205540 if(orient3d(p1,p2,p3,q) != ZERO) {
230 184636 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 448233 get_triangle_vertices(T1_RGN_T, p1,p2,p3);
242 448233 get_triangle_vertices(T2_RGN_T, q1,q2,q3);
243 448233 Sign o1 = orient3d(q1,q2,q3,p1);
244 448233 Sign o2 = orient3d(q1,q2,q3,p2);
245 448233 Sign o3 = orient3d(q1,q2,q3,p3);
246 448233 if(
247
2/2
✓ Branch 0 taken 149148 times.
✓ Branch 1 taken 299085 times.
448233 int(o1)*int(o2) == 1 &&
248
2/2
✓ Branch 0 taken 88501 times.
✓ Branch 1 taken 60647 times.
149148 int(o2)*int(o3) == 1 &&
249
1/2
✓ Branch 0 taken 88501 times.
✗ Branch 1 not taken.
88501 int(o3)*int(o1) == 1
250 ) {
251 88501 return;
252 }
253 }
254
255 359732 intersect_edge_triangle(T1_RGN_E0, T2_RGN_T);
256 ✗ if(finished()) { return; }
257 359732 intersect_edge_triangle(T1_RGN_E1, T2_RGN_T);
258 ✗ if(finished()) { return; }
259 359732 intersect_edge_triangle(T1_RGN_E2, T2_RGN_T);
260 ✗ if(finished()) { return; }
261
262 359732 intersect_edge_triangle(T2_RGN_E0, T1_RGN_T);
263 ✗ if(finished()) { return; }
264 359732 intersect_edge_triangle(T2_RGN_E1, T1_RGN_T);
265 ✗ if(finished()) { return; }
266 359732 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 359732 times.
✗ Branch 1 not taken.
359732 if(result_ != nullptr) {
272 std::sort(result_->begin(), result_->end());
273 auto p = std::unique(result_->begin(), result_->end());
274 359732 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 1125520 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 728138 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 728138 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 728138 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 359732 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 359732 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 359732 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 359732 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 359732 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 359732 times.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
4359426 return (result_ == nullptr && has_non_degenerate_intersection_);
308 }
309
310 2158392 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 2158392 get_triangle_vertices(T,p1,p2,p3);
328 2158392 get_triangle_edges(T,e1,e2,e3);
329 2158392 get_edge_vertices(E,q1,q2);
330
331 2158392 Sign o1 = orient3d(p1,p2,p3,q1);
332 2158392 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 1755668 times.
✓ Branch 1 taken 402724 times.
2158392 if(int(o1) * int(o2) == 1) {
338 774534 return;
339 }
340
341
2/2
✓ Branch 0 taken 728138 times.
✓ Branch 1 taken 1027530 times.
1755668 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 1456276 index_t nax = normal_axis(p1,p2,p3);
347
348 // Test whether the extremities of the segment
349 // are in the triangle
350 {
351 728138 Sign a1 = orient2d(q1,p1,p2,nax);
352 728138 Sign a2 = orient2d(q1,p2,p3,nax);
353 728138 Sign a3 = orient2d(q1,p3,p1,nax);
354
355 728138 Sign b1 = orient2d(q2,p1,p2,nax);
356 728138 Sign b2 = orient2d(q2,p2,p3,nax);
357 728138 Sign b3 = orient2d(q2,p3,p1,nax);
358
359 728138 if(
360
2/2
✓ Branch 0 taken 145648 times.
✓ Branch 1 taken 582490 times.
728138 int(a1)*int(a2) > 0 &&
361
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 137338 times.
145648 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 728138 if(
369
2/2
✓ Branch 0 taken 144915 times.
✓ Branch 1 taken 583223 times.
728138 int(b1)*int(b2) > 0 &&
370
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 136605 times.
144915 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 728138 intersect_edge_edge_2d(E,e1,nax);
379 ✗ if(finished()) { return; }
380 728138 intersect_edge_edge_2d(E,e2,nax);
381 ✗ if(finished()) { return; }
382 728138 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 348608 times.
✓ Branch 1 taken 678922 times.
1027530 if(o1 == ZERO) {
393 348608 R1 = q1;
394
2/2
✓ Branch 0 taken 348608 times.
✓ Branch 1 taken 330314 times.
678922 } else if(o2 == ZERO) {
395 348608 R1 = q2;
396 }
397
398 1027530 Sign oo1 = orient3d(p2,p3,q1,q2);
399 1027530 Sign oo2 = orient3d(p3,p1,q1,q2);
400
401
2/2
✓ Branch 0 taken 655720 times.
✓ Branch 1 taken 371810 times.
1027530 if(int(oo1)*int(oo2) == -1) {
402 return;
403 }
404
405 655720 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 655720 int nb_zeros = (oo1 == ZERO) + (oo2 == ZERO) + (oo3 == ZERO);
411 geo_debug_assert(nb_zeros != 3);
412
2/2
✓ Branch 0 taken 60511 times.
✓ Branch 1 taken 595209 times.
655720 if(nb_zeros == 1) {
413
2/2
✓ Branch 0 taken 30857 times.
✓ Branch 1 taken 29654 times.
60511 if(oo1 == ZERO) {
414 30857 R2 = e1;
415
2/2
✓ Branch 0 taken 25434 times.
✓ Branch 1 taken 4220 times.
29654 } else if(oo2 == ZERO) {
416 25434 R2 = e2;
417 } else {
418 4220 R2 = e3;
419 }
420
2/2
✓ Branch 0 taken 366159 times.
✓ Branch 1 taken 229050 times.
595209 } else if(nb_zeros == 2) {
421
2/2
✓ Branch 0 taken 114870 times.
✓ Branch 1 taken 251289 times.
366159 if(oo1 != ZERO) {
422 114870 R2 = p1;
423
2/2
✓ Branch 0 taken 121385 times.
✓ Branch 1 taken 129904 times.
251289 } else if(oo2 != ZERO) {
424 121385 R2 = p2;
425 } else {
426 129904 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 655720 (int(oo1) * int(oo2) == -1) ||
440
2/2
✓ Branch 0 taken 439883 times.
✓ Branch 1 taken 215837 times.
655720 (int(oo2) * int(oo3) == -1) ||
441
2/2
✓ Branch 0 taken 419953 times.
✓ Branch 1 taken 19930 times.
439883 (int(oo3) * int(oo1) == -1) ;
442
443 if(!outside) {
444 419953 add_intersection(R1,R2);
445 }
446 }
447 }
448
449
450 2184414 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 2184414 get_edge_vertices(E1,p1,p2);
465
466 TriangleRegion q1,q2;
467 2184414 get_edge_vertices(E2,q1,q2);
468
469 2184414 Sign a1 = orient2d(q1,q2,p1,nax);
470 2184414 Sign a2 = orient2d(q1,q2,p2,nax);
471
472
2/2
✓ Branch 0 taken 90136 times.
✓ Branch 1 taken 2094278 times.
2184414 if(a1 == ZERO && a2 == ZERO) {
473 // Special case: 1D
474 // (the 2x2 edge extremities are aligned)
475 90136 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 286217 times.
✓ Branch 1 taken 1808061 times.
2094278 if(a1 == ZERO) {
480 286217 R1 = p1;
481
2/2
✓ Branch 0 taken 286297 times.
✓ Branch 1 taken 1521764 times.
1808061 } else if(a2 == ZERO) {
482 286297 R1 = p2;
483 }
484
485 2094278 Sign b1 = orient2d(p1,p2,q1,nax);
486 2094278 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 307520 times.
✓ Branch 1 taken 1786758 times.
2094278 if(b1 == ZERO) {
491 307520 R2 = q1;
492
2/2
✓ Branch 0 taken 307520 times.
✓ Branch 1 taken 1479238 times.
1786758 } else if(b2 == ZERO) {
493 307520 R2 = q2;
494 }
495
496
4/4
✓ Branch 0 taken 748509 times.
✓ Branch 1 taken 1345769 times.
✓ Branch 2 taken 596824 times.
✓ Branch 3 taken 151685 times.
2094278 if( int(a1)*int(a2) != 1 && int(b1)*int(b2) != 1) {
497 596824 add_intersection(R1,R2);
498 }
499 }
500 2184414 }
501
502 90136 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 90136 get_edge_vertices(E1,p1,p2);
514
515 TriangleRegion q1,q2;
516 90136 get_edge_vertices(E2,q1,q2);
517
518 90136 Sign d1 = dot3d(p1,q1,q2);
519 90136 Sign d2 = dot3d(p2,q1,q2);
520 90136 Sign d3 = dot3d(q1,p1,p2);
521 90136 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 52078 times.
✓ Branch 1 taken 38058 times.
90136 if(d1 == ZERO && d3 == ZERO && points_are_identical(p1,q1)) {
529 10332 add_intersection(p1,q1);
530 }
531
532
2/2
✓ Branch 0 taken 52079 times.
✓ Branch 1 taken 38057 times.
90136 if(d2 == ZERO && d3 == ZERO && points_are_identical(p2,q1)) {
533 52017 add_intersection(p2,q1);
534 }
535
536
2/2
✓ Branch 0 taken 52079 times.
✓ Branch 1 taken 38057 times.
90136 if(d1 == ZERO && d4 == ZERO && points_are_identical(p1,q2)) {
537 52017 add_intersection(p1,q2);
538 }
539
540
2/2
✓ Branch 0 taken 52080 times.
✓ Branch 1 taken 38056 times.
90136 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 89958 times.
90136 if(d1 == NEGATIVE) {
548 178 add_intersection(p1,E2);
549 }
550
551
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89958 times.
90136 if(d2 == NEGATIVE) {
552 178 add_intersection(p2,E2);
553 }
554
555
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89958 times.
90136 if(d3 == NEGATIVE) {
556 178 add_intersection(E1,q1);
557 }
558
559
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89958 times.
90136 if(d4 == NEGATIVE) {
560 178 add_intersection(E1,q2);
561 }
562 90136 }
563
564 1158809 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 1037419 times.
✓ Branch 2 taken 121390 times.
✓ Branch 4 taken 31586 times.
✓ Branch 5 taken 1005833 times.
1158809 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 556506 times.
✗ Branch 1 not taken.
556506 if(result_ != nullptr) {
575 556506 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 602303 times.
✗ Branch 1 not taken.
602303 if(result_ != nullptr) {
580 602303 result_->push_back(std::make_pair(R2,R1));
581 }
582 }
583 1158809 }
584
585 9822311 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 9822311 index_t o3d_idx =
599 9822311 (1u << index_t(i)) |
600 9822311 (1u << index_t(j)) |
601 9822311 (1u << index_t(k)) |
602 9822311 (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 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 9822311 Numeric::uint64 o3d_mask = (Numeric::uint64(1u) << o3d_idx);
614 9822311 if((o3d_cached_ & o3d_mask) == 0) {
615
2/2
✓ Branch 0 taken 2180493 times.
✓ Branch 1 taken 3241973 times.
5422466 int o = flip ?
616 2180493 -int(PCK::orient_3d(p_[i],p_[j],p_[k],p_[l])) :
617 5422466 int(PCK::orient_3d(p_[i],p_[j],p_[k],p_[l])) ;
618 5422466 o3d_cache_[o3d_idx] = Numeric::int8(o);
619 5422466 o3d_cached_ |= o3d_mask;
620 }
621
622 // Get result from the cache
623
624
2/2
✓ Branch 0 taken 4745264 times.
✓ Branch 1 taken 5077047 times.
9822311 Sign result = flip ?
625 4745264 Sign(-o3d_cache_[o3d_idx]) :
626 5077047 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 9822311 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 5422466 times.
✓ Branch 1 taken 4399845 times.
9822311 return (i > j) ^ (i > k) ^ (i > l) ^ (j > k) ^ (j > l) ^ (k > l);
643 }
644
645 12926212 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 25852424 times.
✓ Branch 1 taken 12926212 times.
38778636 for(coord_index_t c = 0; c < 2; c++) {
658 25852424 pi[c] = p_[i][index_t((normal_axis + 1 + c) % 3)];
659 25852424 pj[c] = p_[j][index_t((normal_axis + 1 + c) % 3)];
660 25852424 pk[c] = p_[k][index_t((normal_axis + 1 + c) % 3)];
661 }
662 12926212 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 90136 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 728138 const vec3& p1 = p_[v1];
695 728138 const vec3& p2 = p_[v2];
696 728138 const vec3& p3 = p_[v3];
697
698 728138 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 208316 const vec3& p1 = p_[i];
706 208316 const vec3& p2 = p_[j];
707 return
708 354368 (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 20982 times.
✓ Branch 9 taken 31096 times.
✓ Branch 10 taken 11288 times.
✓ Branch 11 taken 9694 times.
✓ Branch 12 taken 52043 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 52021 times.
✓ Branch 15 taken 22 times.
✓ Branch 16 taken 52043 times.
✓ Branch 17 taken 36 times.
✓ Branch 18 taken 52021 times.
✓ Branch 19 taken 22 times.
✓ Branch 20 taken 20984 times.
✓ Branch 21 taken 31096 times.
✓ Branch 22 taken 11290 times.
✓ Branch 23 taken 9694 times.
208316 (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 52017 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 52017 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 10334 times.
✓ Branch 11 taken 956 times.
126620 (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 1125520 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 1125520 I.compute();
817 1125520 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 5597522 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 5597522 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 4420 times.
✓ Branch 1 taken 3753 times.
✓ Branch 2 taken 4741 times.
✓ Branch 3 taken 5960 times.
✓ Branch 4 taken 3631 times.
✓ Branch 5 taken 3985 times.
✓ Branch 6 taken 32371 times.
✓ Branch 7 taken 28778 times.
✓ Branch 8 taken 35682 times.
✓ Branch 9 taken 32854 times.
✓ Branch 10 taken 28604 times.
✓ Branch 11 taken 36323 times.
✓ Branch 12 taken 26021 times.
✓ Branch 13 taken 24409 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
271532 switch(R) {
846 4420 case T1_RGN_P0:
847 result = T2_RGN_P0;
848 4420 break;
849 3753 case T1_RGN_P1:
850 result = T2_RGN_P1;
851 3753 break;
852 4741 case T1_RGN_P2:
853 result = T2_RGN_P2;
854 4741 break;
855 5960 case T2_RGN_P0:
856 result = T1_RGN_P0;
857 5960 break;
858 3631 case T2_RGN_P1:
859 result = T1_RGN_P1;
860 3631 break;
861 3985 case T2_RGN_P2:
862 result = T1_RGN_P2;
863 3985 break;
864 32371 case T1_RGN_E0:
865 result = T2_RGN_E0;
866 32371 break;
867 28778 case T1_RGN_E1:
868 result = T2_RGN_E1;
869 28778 break;
870 35682 case T1_RGN_E2:
871 result = T2_RGN_E2;
872 35682 break;
873 32854 case T2_RGN_E0:
874 result = T1_RGN_E0;
875 32854 break;
876 28604 case T2_RGN_E1:
877 result = T1_RGN_E1;
878 28604 break;
879 36323 case T2_RGN_E2:
880 result = T1_RGN_E2;
881 36323 break;
882 26021 case T1_RGN_T:
883 result = T2_RGN_T;
884 26021 break;
885 24409 case T2_RGN_T:
886 result = T1_RGN_T;
887 24409 break;
888 case T_RGN_NB:
889 ✗ geo_assert_not_reached;
890 }
891 271532 return result;
892 }
893
894 3882652 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 2355223 times.
✓ Branch 1 taken 1527429 times.
✗ Branch 2 not taken.
3882652 switch(T) {
900 2355223 case T1_RGN_T: {
901 2355223 p0 = T1_RGN_P0; p1 = T1_RGN_P1; p2 = T1_RGN_P2;
902 2355223 } break;
903 1527429 case T2_RGN_T: {
904 1527429 p0 = T2_RGN_P0; p1 = T2_RGN_P1; p2 = T2_RGN_P2;
905 1527429 } break;
906 default:
907 ✗ geo_assert_not_reached;
908 }
909 3882652 }
910
911 2158392 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 1079196 times.
✓ Branch 1 taken 1079196 times.
✗ Branch 2 not taken.
2158392 switch(T) {
917 1079196 case T1_RGN_T: {
918 1079196 e0 = T1_RGN_E0; e1 = T1_RGN_E1; e2 = T1_RGN_E2;
919 1079196 } break;
920 1079196 case T2_RGN_T: {
921 1079196 e0 = T2_RGN_E0; e1 = T2_RGN_E1; e2 = T2_RGN_E2;
922 1079196 } break;
923 default:
924 ✗ geo_assert_not_reached;
925 }
926 2158392 }
927
928 6773724 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 1138849 times.
✓ Branch 1 taken 1126943 times.
✓ Branch 2 taken 1129330 times.
✓ Branch 3 taken 1126837 times.
✓ Branch 4 taken 1126388 times.
✓ Branch 5 taken 1125377 times.
✗ Branch 6 not taken.
6773724 switch(E) {
933 1138849 case T1_RGN_E0: {
934 1138849 q0 = T1_RGN_P1;
935 1138849 q1 = T1_RGN_P2;
936 1138849 } break;
937 1126943 case T1_RGN_E1: {
938 1126943 q0 = T1_RGN_P2;
939 1126943 q1 = T1_RGN_P0;
940 1126943 } break;
941 1129330 case T1_RGN_E2: {
942 1129330 q0 = T1_RGN_P0;
943 1129330 q1 = T1_RGN_P1;
944 1129330 } break;
945 1126837 case T2_RGN_E0: {
946 1126837 q0 = T2_RGN_P1;
947 1126837 q1 = T2_RGN_P2;
948 1126837 } break;
949 1126388 case T2_RGN_E1: {
950 1126388 q0 = T2_RGN_P2;
951 1126388 q1 = T2_RGN_P0;
952 1126388 } break;
953 1125377 case T2_RGN_E2: {
954 1125377 q0 = T2_RGN_P0;
955 1125377 q1 = T2_RGN_P1;
956 1125377 } break;
957 default:
958 ✗ geo_assert_not_reached;
959 }
960 6773724 }
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 160526 times.
✓ Branch 2 taken 45767 times.
✓ Branch 4 taken 143357 times.
✓ Branch 5 taken 17169 times.
206293 if(region_dim(R1) == 1 && region_dim(R2) == 0) {
973 TriangleRegion v1,v2;
974 17169 get_edge_vertices(R1,v1,v2);
975
4/4
✓ Branch 0 taken 9492 times.
✓ Branch 1 taken 7677 times.
✓ Branch 2 taken 7583 times.
✓ Branch 3 taken 1909 times.
17169 if(R2 == v1 || R2 == v2) {
976 R = R1;
977 }
978
4/4
✓ Branch 1 taken 155148 times.
✓ Branch 2 taken 33976 times.
✓ Branch 4 taken 140517 times.
✓ Branch 5 taken 14631 times.
189124 } else if(region_dim(R2) == 1 && region_dim(R1) == 0) {
979 TriangleRegion v1,v2;
980 14631 get_edge_vertices(R2,v1,v2);
981
4/4
✓ Branch 0 taken 8337 times.
✓ Branch 1 taken 6294 times.
✓ Branch 2 taken 6390 times.
✓ Branch 3 taken 1947 times.
14631 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 24195 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 34432 times.
✗ Branch 1 not taken.
34432 }
989 ) {
990 TriangleRegion v1,v2;
991 34432 get_edge_vertices(E,v1,v2);
992
8/8
✓ Branch 0 taken 6742 times.
✓ Branch 1 taken 27690 times.
✓ Branch 2 taken 2336 times.
✓ Branch 3 taken 4406 times.
✓ Branch 4 taken 8929 times.
✓ Branch 5 taken 21097 times.
✓ Branch 6 taken 5831 times.
✓ Branch 7 taken 3098 times.
34432 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