GCC Code Coverage Report


Directory: ./
File: mesh/triangle_intersection.h
Date: 2026-11-30 05:48:47
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 1 1 100.0%
Branches: 5 12 41.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 #ifndef GEOGRAM_MESH_TRIANGLE_INTERSECTION
41 #define GEOGRAM_MESH_TRIANGLE_INTERSECTION
42
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/geometry.h>
45 #include <geogram/basic/memory.h>
46 #include <utility>
47
48 /**
49 * \file geogram/mesh/triangle_intersection.h
50 * \brief Symbolic computation of triangle-triangle intersection
51 */
52
53 namespace GEO {
54
55 /**
56 * \brief Encodes the location of a point within a triangle.
57 * \details A point can be located in 6 different regions, that
58 * correspond to the three vertices, three edges and interior
59 * of a triangle.
60 * - RGN_P0, RGN_P1, RGN_P2 when point is exactly on a vertex
61 * - RGN_E0, RGN_E1, RGN_E2 when point is on an edge
62 * - RGN_T when point is on the interior of the triangle
63 */
64 enum TriangleRegion {
65 T1_RGN_P0 = 0,
66 T1_RGN_P1 = 1,
67 T1_RGN_P2 = 2,
68
69 T2_RGN_P0 = 3,
70 T2_RGN_P1 = 4,
71 T2_RGN_P2 = 5,
72
73 T1_RGN_E0 = 6,
74 T1_RGN_E1 = 7,
75 T1_RGN_E2 = 8,
76
77 T2_RGN_E0 = 9,
78 T2_RGN_E1 = 10,
79 T2_RGN_E2 = 11,
80
81 T1_RGN_T = 12,
82 T2_RGN_T = 13,
83
84 T_RGN_NB = 14
85 };
86
87
88 /**
89 * \brief Tests whether a region belongs to triangle T1
90 * \retval true if region belongs to T1
91 * \retval false if region belongs to T2
92 */
93 inline bool is_in_T1(TriangleRegion R) {
94 return (R == T1_RGN_P0) ||
95 (R == T1_RGN_P1) ||
96 (R == T1_RGN_P2) ||
97 (R == T1_RGN_E0) ||
98 (R == T1_RGN_E1) ||
99 (R == T1_RGN_E2) ||
100 (R == T1_RGN_T ) ;
101 }
102
103 /**
104 * \brief Replaces T1 with T2 or T2 with T1 in a region code
105 * \param[in] R a region
106 * \return the region in T2 corresponding to a region in T1,
107 * or the region in T1 corresponding to a region in T2.
108 */
109 TriangleRegion GEOGRAM_API swap_T1_T2(TriangleRegion R);
110
111 /**
112 * \brief Encodes the symbolic representation of a triangle intersection,
113 * as a pair of TriangleRegion.
114 */
115 typedef std::pair<TriangleRegion, TriangleRegion> TriangleIsect;
116
117
118 /**
119 * \brief A small vector of TriangleIsect stored in an array with static
120 * size.
121 * \details Used to return the symbolic information of triangle-triangle
122 * intersections. It is interesting to have a specialized class that avoids
123 * dynamic allocations for cases where triangle-triangle intersection is
124 * called in concurrent threads.
125 */
126 class TriangleIsects {
127 public:
128
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
1125520 TriangleIsects() : size_(0) {
129 }
130
131 typedef TriangleIsect* iterator;
132 typedef const TriangleIsect* const_iterator;
133
134 iterator begin() {
135 359732 return data_;
136 }
137
138 iterator end() {
139 359732 return begin()+size_;
140 }
141
142 const_iterator begin() const {
143 return data_;
144 }
145
146 const_iterator end() const {
147 return begin()+size_;
148 }
149
150 1158809 void push_back(const TriangleIsect& I) {
151
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1158809 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1158809 geo_assert(size_ < capacity_);
152 data_[size_] = I;
153 1158809 ++size_;
154 1158809 }
155
156 void clear() {
157 size_ = 0;
158 }
159
160 index_t size() const {
161
4/4
✓ Branch 0 taken 10907 times.
✓ Branch 1 taken 25895 times.
✓ Branch 2 taken 41988 times.
✓ Branch 3 taken 10907 times.
78790 return size_;
162 }
163
164 void resize(index_t new_size) {
165 geo_debug_assert(new_size <= capacity_);
166 1125520 size_ = new_size;
167 1485252 }
168
169 TriangleIsect& operator[](index_t i) {
170 geo_debug_assert(i<size());
171 return data_[i];
172 }
173
174 const TriangleIsect& operator[](index_t i) const {
175 geo_debug_assert(i<size());
176 return data_[i];
177 }
178
179 private:
180 /**
181 * \brief The maximum capacity used internally to store the
182 * symbolic vertices of a triangle-triangle
183 * intersection. Note that in the transient state of triangle-triangle
184 * intersection there can be more than 6 vertices. The duplicated
185 * vertices are eliminated at the end of triangle-triangle
186 * intersection,using sort-uniq.
187 */
188 static constexpr int capacity_ = 20;
189 index_t size_;
190 TriangleIsect data_[capacity_];
191 };
192
193 enum TriangleIsectFlags {
194 TRIANGLES_INTERSECTION_DEFAULT = 0,
195 TRIANGLES_INTERSECTION_NO_DEGENERATE = 1,
196 TRIANGLES_INTERSECTION_NO_BBOX = 2
197 };
198
199
200 /**
201 * \brief Triangle-triangle intersection with symbolic information
202 * \details The input triangles are supposed to be non-degenerate
203 * (their three vertices are supposed to be distinct and not co-linear).
204 * For now, when intersection is surfacic (overlapping pair
205 * of co-planar triangles), the vertices of the intersection are
206 * not sorted. One can order them by computing their convex hull.
207 * \param[in] p0 , p1 , p2 first triangle
208 * \param[in] q0 , q1 , q2 second triangle
209 * \param[out] result the intersection in symbolic
210 * form, as TriangleRegion pairs. There can be
211 * between 0 and 6 intersection pairs in the result.
212 * \retval true if there is a non-degenerate intersection
213 * \retval false otherwise. Degenerate intersection cases are:
214 * - one vertex in common
215 * - two vertices (an edge) in common
216 * - or duplicated triangles.
217 */
218 bool GEOGRAM_API triangles_intersections(
219 const vec3& p0, const vec3& p1, const vec3& p2,
220 const vec3& q0, const vec3& q1, const vec3& q2,
221 TriangleIsects& result,
222 TriangleIsectFlags flags = TRIANGLES_INTERSECTION_DEFAULT
223 );
224
225
226 [[deprecated("use TriangleIsects instead of vector<TriangleIsect>")]]
227 inline bool triangles_intersections(
228 const vec3& p0, const vec3& p1, const vec3& p2,
229 const vec3& q0, const vec3& q1, const vec3& q2,
230 vector<TriangleIsect>& result
231 ) {
232 TriangleIsects result_;
233 bool r = triangles_intersections(p0,p1,p2,q0,q1,q2,result_);
234 result.clear();
235 for(TriangleIsect I : result_) {
236 result.push_back(I);
237 }
238 return r;
239 }
240
241
242 /**
243 * \brief Triangle-triangle intersection with symbolic information
244 * \details The input triangles are supposed to be non-degenerate
245 * (their three vertices are supposed to be distinct and not co-linear).
246 * For now, when intersection is surfacic (overlapping pair
247 * of co-planar triangles), the vertices of the intersection are
248 * not sorted. One can order them by computing their convex hull.
249 * \param[in] p0 , p1 , p2 first triangle
250 * \param[in] q0 , q1 , q2 second triangle
251 * \param[in] p0_index , p1_index , p2_index global indices of p0 , p1 , p2
252 * \param[in] q0_index , q1_index , q2_index global indices of q0 , q1 , q2
253 * indicating whether q0 (resp q1, q2) correspond to p0 , p1 , p2
254 * \param[out] result the intersection in symbolic
255 * form, as TriangleRegion pairs. There can be
256 * between 0 and 6 intersection pairs in the result.
257 * \retval true if there is a non-degenerate intersection
258 * \retval false otherwise. Degenerate intersection cases are:
259 * - one vertex in common
260 * - two vertices (an edge) in common
261 * - or duplicated triangles.
262 */
263 bool GEOGRAM_API triangles_intersections(
264 const vec3& p0, const vec3& p1, const vec3& p2,
265 const vec3& q0, const vec3& q1, const vec3& q2,
266 index_t p0_index,
267 index_t p1_index,
268 index_t p2_index,
269 index_t q0_index,
270 index_t q1_index,
271 index_t q2_index,
272 TriangleIsects& result,
273 TriangleIsectFlags flags = TRIANGLES_INTERSECTION_DEFAULT
274 );
275
276
277 /**
278 * \brief Triangle-triangle intersection predicate
279 * \param[in] p0 , p1 , p2 first triangle
280 * \param[in] q0 , q1 , q2 second triangle
281 * \retval true if there is a non-degenerate intersection
282 * \retval false otherwise. Degenerate intersection cases are:
283 * - one vertex in common
284 * - two vertices (an edge) in common
285 * - or duplicated triangles.
286 */
287 bool triangles_intersections(
288 const vec3& p0, const vec3& p1, const vec3& p2,
289 const vec3& q0, const vec3& q1, const vec3& q2
290 );
291
292 /**
293 * \brief Converts a triangle region code to a string.
294 * \param[in] rgn the triangle region code.
295 * \return the string representation of \p rgn.
296 */
297 std::string GEOGRAM_API region_to_string(TriangleRegion rgn);
298
299 /**
300 * \brief Gets the dimension of a triangle region
301 * \param[in] r a triangle region
302 * \retval 0 for vertices
303 * \retval 1 for edges
304 * \retval 2 for the interior
305 */
306 coord_index_t GEOGRAM_API region_dim(TriangleRegion r);
307
308 /**
309 * \brief Gets the vertices of a triangle
310 * \param[in] T one of T1_RGN_T, T2_RGN_T
311 * \param[out] p0 , p1 , p2 the region codes of the
312 * three vertices of the triangle
313 */
314 void GEOGRAM_API get_triangle_vertices(
315 TriangleRegion T,
316 TriangleRegion& p0, TriangleRegion& p1, TriangleRegion& p2
317 );
318
319 /**
320 * \brief Gets the edges of a triangle
321 * \param[in] T one of T1_RGN_T, T2_RGN_T
322 * \param[out] e0 , e1 , e2 the region codes of the
323 * three edges of the triangle
324 */
325 void GEOGRAM_API get_triangle_edges(
326 TriangleRegion T,
327 TriangleRegion& e0, TriangleRegion& e1, TriangleRegion& e2
328 );
329
330 /**
331 * \brief Gets the vertices of an edge
332 * \param[in] E the region code of an edge
333 * \param[out] q0 , q1 the region codes of the two vertices
334 * of the edge
335 */
336 void GEOGRAM_API get_edge_vertices(
337 TriangleRegion E, TriangleRegion& q0, TriangleRegion& q1
338 );
339
340
341 /**
342 * \brief Computes the convex hull of two regions
343 * \details The function is purely combinatorial.
344 * - The convex hull of twice the same region is
345 * that region
346 * - the convex hull of an edge and a vertex this edge
347 * is incident to is that edge
348 * - the convex hull of two vertices is the edge incident
349 * to the vertices
350 * - in all other cases, the convex hull is the triangle
351 * \pre \p R1 and \p R2 are in the same triangle (they
352 * both start with T1_ or they both start with T2_).
353 * \param[in] R1 , R2 the two regions
354 * \return The convex hull of R1 and R2
355 */
356 TriangleRegion GEOGRAM_API regions_convex_hull(
357 TriangleRegion R1, TriangleRegion R2
358 );
359
360 /**
361 * \brief Prints a triangle intersection element to a stream.
362 * \details Used for debugging purposes.
363 * \param[in] out the stream.
364 * \param[in] I the intersection element to be printed.
365 */
366 inline std::ostream& operator<<(
367 std::ostream& out, const TriangleIsect& I
368 ) {
369 return (
370 out << "("
371 << region_to_string(I.first) << ","
372 << region_to_string(I.second)
373 << ")"
374 );
375 }
376
377 /**
378 * \brief Prints the result of a triangle intersection to a stream.
379 * \details Used for debugging purposes.
380 * \param[in] out the stream.
381 * \param[in] II the intersections to be printed.
382 */
383 inline std::ostream& operator<<(
384 std::ostream& out, TriangleIsects& II
385 ) {
386 for(const TriangleIsect& I : II) {
387 out << I << " ";
388 }
389 return out;
390 }
391
392 [[deprecated("use TriangleIsects instead of vector<TriangleIsect>")]]
393 inline std::ostream& operator<<(
394 std::ostream& out, const vector<TriangleIsect>& II
395 ) {
396 for(const TriangleIsect& I : II) {
397 out << I << " ";
398 }
399 return out;
400 }
401
402
403 }
404
405 #endif
406