GCC Code Coverage Report


Directory: ./
File: numerics/predicates.h
Date: 2026-11-30 06:00:29
Exec Total Coverage
Lines: 60 62 96.8%
Functions: 11 11 100.0%
Branches: 23 36 63.9%

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_NUMERICS_PREDICATES
41 #define GEOGRAM_NUMERICS_PREDICATES
42
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/numeric.h>
45 #include <geogram/basic/geometry.h>
46 #include <geogram/numerics/PCK.h>
47
48 /**
49 * \file geogram/numerics/predicates.h
50 * \brief Filtered exact predicates for restricted Voronoi diagrams.
51 */
52
53
54 namespace GEO {
55
56 /**
57 * \brief PCK (Predicate Construction Kit) implements a set of
58 * geometric predicates. PCK uses arithmetic filters (Meyer and Pion),
59 * expansion arithmetics (Shewchuk) and simulation of simplicity
60 * (Edelsbrunner).
61 */
62 namespace PCK {
63
64 /**
65 * \brief Mode for symbolic perturbations.
66 */
67 enum SOSMode { SOS_ADDRESS, SOS_LEXICO };
68
69 /**
70 * \brief Sets the current mode for handling symbolic perturbations
71 * (SOS for Simulation Of Simplicity).
72 * \param[in] m one of SOS_ADDRESS, SOS_LEXICO
73 * \details If SOS_ADDRESS mode is used, then points are supposed
74 * to be allocated in a fixed array, and the same point always
75 * designated by the same address. If SOS_LEXICO is used then points
76 * are sorted in lexicographic order for computing the symbolic
77 * perturbation. SOS_LEXICO works for points that are generated
78 * dynamically (with no fixed address).
79 */
80 [[deprecated("SOS mode is always lexico")]]
81 void GEOGRAM_API set_SOS_mode(SOSMode m);
82
83 /**
84 * \brief Gets the current mode for handling symbolic perturbations.
85 * \return one of SOS_ADDRESS, SOS_LEXICO
86 * \see set_SOS_mode()
87 */
88 [[deprecated("SOS mode is always lexico")]]
89 SOSMode GEOGRAM_API get_SOS_mode();
90
91 /**
92 * \brief Computes the side of a point (given directly)
93 * relative to a bisector.
94 * \details Computes the side of \f$ q0 \f$ relative to
95 * \f$ \Pi(p0,p1) \f$.
96 * Symbolic perturbation is applied whenever equality holds.
97 * \param[in] p0 , p1 extremities of the bisector
98 * \param[in] q0 point to be tested
99 * \param[in] DIM number of coordinates of the point
100 * \retval POSITIVE if d(p0,q0) < d(p1,q0)
101 * \retval NEGATIVE if d(p0,q0) > d(p1,q1)
102 * \retval perturb() if f(p0,q0) = d(p1,q1),
103 * where \c perturb() denotes a globally
104 * consistent perturbation, that returns either POSITIVE or NEGATIVE
105 * \note Only some specific dimensions are implemented (3,4,6 and 7)
106 */
107 Sign GEOGRAM_API side1_SOS(
108 const double* p0, const double* p1,
109 const double* q0,
110 coord_index_t DIM
111 );
112
113 /**
114 * \brief Computes the side of a point (given as the intersection
115 * between a segment and a bisector) relative to another bisector.
116 * \details Computes the side of \f$ q = \Pi(p0,p1) \cap [q0,q1] \f$
117 * relative to \f$ \Pi(p0,p2) \f$.
118 * Symbolic perturbation is applied whenever equality holds.
119 * \param[in] p0 first extremity of the bisectors
120 * \param[in] p1 second extremity of the first bisector
121 * (that defines the intersection q)
122 * \param[in] p2 second extremity of the second bisector
123 * (against which orientation is tested)
124 * \param[in] q0 , q1 extremities of the segment
125 * (that defines the intersection q)
126 * \retval POSITIVE if d(p0,q) < d(p2,q)
127 * \retval NEGATIVE if d(p0,q) > d(p2,q)
128 * \retval perturb() if d(p0,q) = d(p2,q),
129 * where \c perturb() denotes a globally
130 * consistent perturbation, that returns either POSITIVE or NEGATIVE
131 * \note Only some specific dimensions are implemented (3,4,6 and 7)
132 */
133 Sign GEOGRAM_API side2_SOS(
134 const double* p0, const double* p1, const double* p2,
135 const double* q0, const double* q1,
136 coord_index_t DIM
137 );
138
139 /**
140 * \brief Computes the side of a point (given as the intersection
141 * between a facet and two bisectors) relative to another bisector.
142 * \details Computes the side of
143 * \f$ q = \Pi(p0,p1) \cap Pi(p0,p2) \cap \Delta[q0,q1,q2] \f$
144 * relative to \f$ \Pi(p0,p3) \f$.
145 * Symbolic perturbation is applied whenever equality holds.
146 * \param[in] p0 first extremity of the bisectors
147 * \param[in] p1 second extremity of the first bisector
148 * (that defines the intersection q)
149 * \param[in] p2 second extremity of the second bisector
150 * (that defines the intersection q)
151 * \param[in] p3 second extremity of the third bisector
152 * (against which orientation is tested)
153 * \param[in] q0 , q1 , q2 vertices of the triangle
154 * (that defines the intersection q)
155 * \retval POSITIVE if d(p0,q) < d(p3,q)
156 * \retval NEGATIVE if d(p0,q) > d(p3,q)
157 * \retval perturb() if d(p0,q) = d(p3,q),
158 * where \c perturb() denotes a globally
159 * consistent perturbation, that returns either POSITIVE or NEGATIVE
160 * \note Only some specific dimensions are implemented (3,4,6 and 7)
161 */
162 Sign GEOGRAM_API side3_SOS(
163 const double* p0, const double* p1,
164 const double* p2, const double* p3,
165 const double* q0, const double* q1, const double* q2,
166 coord_index_t DIM
167 );
168
169 /**
170 * \brief Computes the side of a point (given as the intersection
171 * between a facet and two bisectors) relative to another bisector.
172 * \details Computes the side of
173 * \f$ q = \Pi(p0 h0,p1 h1) \cap Pi(p0 h0,p2 h2) \cap \Delta[q0, q1, q2] \f$
174 * relative to \f$ \Pi(p0 hp0,p3 hp3) \f$.
175 * Symbolic perturbation is applied whenever equality holds.
176 * \param[in] p0 first extremity of the bisectors
177 * \param[in] p1 second extremity of the first bisector
178 * (that defines the intersection q)
179 * \param[in] p2 second extremity of the second bisector
180 * (that defines the intersection q)
181 * \param[in] p3 second extremity of the third bisector
182 * (against which orientation is tested)
183 * \param h0 , h1 , h2 , h3 lifted coordinates of \p p0, \p p1, \p p2
184 * and \p p3
185 * \param[in] q0 , q1 , q2 vertices of the triangle
186 * (that defines the intersection q)
187 * \param[in] SOS if true, do the symbolic perturbation in the
188 * degenerate case
189 * \retval POSITIVE if d(p0 hp0,q) < d(p3 hp3, q)
190 * \retval NEGATIVE if d(p0 hp0,q) > d(p3 hp3, q)
191 * \retval perturb() if d(p0 hp0,q) = d(p3 hp3, q),
192 * where \c perturb() denotes a globally
193 * consistent perturbation, that returns either POSITIVE or NEGATIVE
194 */
195 Sign GEOGRAM_API side3_3dlifted_SOS(
196 const double* p0, const double* p1,
197 const double* p2, const double* p3,
198 double h0, double h1, double h2, double h3,
199 const double* q0, const double* q1, const double* q2,
200 bool SOS=true
201 );
202
203 /**
204 * \brief Computes the side of a point (given as the intersection
205 * between a tetrahedron and three bisectors) relative to
206 * another bisector.
207 * \details Computes the side of
208 * \f$ q = \Pi(p0,p1) \cap Pi(p0,p2) \cap Pi(p0,p3)
209 * \cap \Delta[q0,q1,q2,q3] \f$ relative to \f$ \Pi(p0,p4) \f$.
210 * Symbolic perturbation is applied whenever equality holds.
211 * \param[in] p0 first extremity of the bisectors
212 * \param[in] p1 second extremity of the first bisector
213 * (that defines the intersection q)
214 * \param[in] p2 second extremity of the second bisector
215 * (that defines the intersection q)
216 * \param[in] p3 second extremity of the third bisector
217 * (that defines the intersection q)
218 * \param[in] p4 second extremity of the fourth bisector
219 * (against which orientation is tested)
220 * \param[in] q0 , q1 , q2 , q3 vertices of the tetrahedron
221 * (that defines the intersection q)
222 * (that defines the intersection q)
223 * \retval POSITIVE if d(p0,q) < d(p4,q)
224 * \retval NEGATIVE if d(p0,q) > d(p4,q)
225 * \retval perturb() if d(p0,q) = d(p4,q),
226 * where \c perturb() denotes a globally
227 * consistent perturbation, that returns either POSITIVE or NEGATIVE
228 * \note Only some specific dimensions are implemented (3,4,6 and 7)
229 */
230 Sign GEOGRAM_API side4_SOS(
231 const double* p0,
232 const double* p1, const double* p2,
233 const double* p3, const double* p4,
234 const double* q0, const double* q1,
235 const double* q2, const double* q3,
236 coord_index_t DIM
237 );
238
239
240 /**
241 * \brief Computes the side of a point (given as the intersection
242 * between three bisectors) relative to another bisector.
243 * \details Computes the side of
244 * \f$ q = \Pi(p0,p1) \cap \Pi(p0,p2) \cap \Pi(p0,p3) \f$
245 * relative to \f$ Pi(p0,p4) \f$.
246 * This version does not apply symbolic perturbation when equality
247 * holds.
248 * side4_3d() is a special case of side4(), where the ambient and
249 * intrinsic dimensions coincide (therefore no embedding tetrahedron
250 * is needed).
251 * \param[in] p0 first extremity of the bisectors
252 * \param[in] p1 second extremity of the first bisector
253 * (that defines the intersection q)
254 * \param[in] p2 second extremity of the second bisector
255 * (that defines the intersection q)
256 * \param[in] p3 second extremity of the third bisector
257 * (that defines the intersection q)
258 * \param[in] p4 second extremity of the fourth bisector
259 * (against which orientation is tested)
260 * \retval POSITIVE if d(p0,q) < d(p4,q)
261 * \retval NEGATIVE if d(p0,q) > d(p4,q)
262 * \retval ZERO if d(p0,q) = d(p4,q),
263 */
264 Sign GEOGRAM_API side4_3d(
265 const double* p0,
266 const double* p1, const double* p2,
267 const double* p3, const double* p4
268 );
269
270 /**
271 * \brief Computes the side of a point (given as the intersection
272 * between three bisectors) relative to another bisector.
273 * \details Computes the side of
274 * \f$ q = \Pi(p0,p1) \cap \Pi(p0,p2) \cap \Pi(p0,p3) \f$
275 * relative to \f$ Pi(p0,p4) \f$.
276 * Symbolic perturbation is applied whenever equality holds.
277 * side4_3d() is a special case of side4(), where the ambient and
278 * intrinsic dimensions coincide (therefore no embedding tetrahedron
279 * is needed).
280 * \param[in] p0 first extremity of the bisectors
281 * \param[in] p1 second extremity of the first bisector
282 * (that defines the intersection q)
283 * \param[in] p2 second extremity of the second bisector
284 * (that defines the intersection q)
285 * \param[in] p3 second extremity of the third bisector
286 * (that defines the intersection q)
287 * \param[in] p4 second extremity of the fourth bisector
288 * (against which orientation is tested)
289 * \retval POSITIVE if d(p0,q) < d(p4,q)
290 * \retval NEGATIVE if d(p0,q) > d(p4,q)
291 * \retval perturb() if d(p0,q) = d(p4,q),
292 * where \c perturb() denotes a globally
293 * consistent perturbation, that returns either POSITIVE or NEGATIVE
294 */
295 Sign GEOGRAM_API side4_3d_SOS(
296 const double* p0, const double* p1,
297 const double* p2, const double* p3, const double* p4
298 );
299
300 /**
301 * \brief Tests whether a 3d point is inside the circumscribed
302 * sphere of a 3d tetrahedron.
303 * \param[in] p0 , p1 , p2 , p3 the four vertices of the tetrahedron
304 * \param[in] p4 the point
305 * \retval POSITIVE whenever \p p4 is inside the circumscribed sphere
306 * of the tetrahedron \p p0, \p p1, \p p2, \p p3
307 * \retval NEGATIVE whenever \p p4 is outside the circumscribed sphere
308 * of the tetrahedron \p p0, \p p1, \p p2, \p p3
309 * \retval perturb() if \p p4 is exactly on the circumscribed sphere
310 * of the tetrahedron \p p0, \p p1, \p p2, \p p3, where \c perturb()
311 * denotes a globally consistent perturbation, that returns
312 * either POSITIVE or NEGATIVE
313 * \pre orient_3d(p0,p1,p2,p3) > 0
314 */
315 Sign GEOGRAM_API in_sphere_3d_SOS(
316 const double* p0, const double* p1,
317 const double* p2, const double* p3,
318 const double* p4
319 );
320
321
322 /**
323 * \brief Tests whether a 2d point is inside the
324 * circumscribed circle of a 3d triangle.
325 * \param[in] p0 , p1 , p2 vertices of the triangle
326 * \param[in] p3 the point to be tested
327 * \retval POSITIVE whenever \p p3 is inside the circumscribed circle
328 * of the triangle \p p0, \p p1, \p p2
329 * \retval NEGATIVE whenever \p p2 is outside the circumscribed circle
330 * of the triangle \p p0, \p p1, \p p2
331 * \retval perturb() if \p p3 is exactly on the circumscribed circle
332 * of the triangle \p p0, \p p1, \p p2, where \c perturb()
333 * denotes a globally consistent perturbation, that returns
334 * either POSITIVE or NEGATIVE
335 * \pre \p p3 belongs to the plane yielded by \p p0, \p p1 and \p p2
336 */
337 Sign GEOGRAM_API in_circle_2d_SOS(
338 const double* p0, const double* p1, const double* p2,
339 const double* p3
340 );
341
342
343 /**
344 * \brief Tests whether a 3d point is inside the
345 * circumscribed circle of a 3d triangle.
346 * \param[in] p0 , p1 , p2 vertices of the triangle
347 * \param[in] p3 the point to be tested
348 * \retval POSITIVE whenever \p p3 is inside the circumscribed circle
349 * of the triangle \p p0, \p p1, \p p2
350 * \retval NEGATIVE whenever \p p2 is outside the circumscribed circle
351 * of the triangle \p p0, \p p1, \p p2
352 * \retval perturb() if \p p3 is exactly on the circumscribed circle
353 * of the triangle \p p0, \p p1, \p p2, where \c perturb()
354 * denotes a globally consistent perturbation, that returns
355 * either POSITIVE or NEGATIVE
356 * \pre \p p3 belongs to the plane yielded by \p p0, \p p1 and \p p2
357 */
358 Sign GEOGRAM_API in_circle_3d_SOS(
359 const double* p0, const double* p1, const double* p2,
360 const double* p3
361 );
362
363
364 /**
365 * \brief Tests whether a lifted 3d point is inside the
366 * circumscribed circle of a lifted 3d triangle.
367 * \param[in] p0 , p1 , p2 vertices of the triangle
368 * \param[in] p3 the point to be tested
369 * \param[in] h0 , h1 , h2 lifted coordinate of the triangle vertices
370 * \param[in] h3 lifted coordinate of the point to be tested
371 * \param[in] SOS if true, do the symbolic perturbation in the degenerate
372 * cases
373 * \retval POSITIVE whenever (\p p3, \p h3) is inside the
374 * circumscribed circle of the triangle (\p p0,\p h0) (\p p1,\p h1),
375 * (\p p2, \p h2)
376 * \retval NEGATIVE whenever (\p p3, \p h3) is outside the
377 * circumscribed circle
378 * of the triangle (\p p0,\p h0) (\p p1,\p h1), (\p p2, \p h2)
379 * \retval perturb() if (\p p3, \p h3) is exactly
380 * on the circumscribed circle
381 * of the triangle (\p p0,\p h0) (\p p1,\p h1), (\p p2, \p h2)
382 * where \c perturb() denotes a globally consistent perturbation,
383 * that returns either POSITIVE or NEGATIVE
384 * \pre (\p p3, \p h3) belongs to the hyperplane yielded by
385 * (\p p0, \p h0), (\p p1, \p h1) and (\p p2, \p h2)
386 */
387 Sign GEOGRAM_API in_circle_3dlifted_SOS(
388 const double* p0, const double* p1, const double* p2,
389 const double* p3,
390 double h0, double h1, double h2, double h3,
391 bool SOS=true
392 );
393
394 /**
395 * \brief Computes the orientation predicate in 2d.
396 * \details Computes the sign of the signed area of
397 * the triangle p0, p1, p2.
398 * \param[in] p0 , p1 , p2 vertices of the triangle
399 * \retval POSITIVE if the triangle is oriented counter-clockwise
400 * \retval ZERO if the triangle is flat
401 * \retval NEGATIVE if the triangle is oriented clockwise
402 */
403 Sign GEOGRAM_API orient_2d(
404 const double* p0, const double* p1, const double* p2
405 );
406
407
408 #ifndef GEOGRAM_PSM
409 /**
410 * \brief Computes the orientation predicate in 2d.
411 * \details Computes the sign of the signed area of
412 * the triangle p0, p1, p2.
413 * \param[in] p0 , p1 , p2 vertices of the triangle
414 * \retval POSITIVE if the triangle is oriented counter-clockwise
415 * \retval ZERO if the triangle is flat
416 * \retval NEGATIVE if the triangle is oriented clockwise
417 */
418 155150 inline Sign orient_2d(
419 const vec2& p0, const vec2& p1, const vec2& p2
420 ) {
421 155150 return orient_2d(p0.data(),p1.data(),p2.data());
422 }
423 #endif
424
425 /**
426 * \brief Computes the 3d orientation test with lifted points.
427 * \details Given three lifted points p0', p1', p2' in
428 * R^2, tests if the lifted point p3' in R^3 lies below or above
429 * the plane passing through the three points
430 * p0', p1', p2'.
431 * The first two coordinates and the
432 * third one are specified in separate arguments for each vertex.
433 * \param[in] p0 , p1 , p2 , p3 first 2 coordinates
434 * of the vertices of the 3-simplex
435 * \param[in] h0 , h1 , h2 , h3 heights of the vertices of
436 * the 3-simplex
437 * \retval POSITIVE if p3' lies below the plane
438 * \retval NEGATIVE if p3' lies above the plane
439 * \retval perturb() if p3' lies exactly on the hyperplane
440 * where \c perturb() denotes a globally
441 * consistent perturbation, that returns either POSITIVE or NEGATIVE
442 */
443 Sign GEOGRAM_API orient_2dlifted_SOS(
444 const double* p0, const double* p1,
445 const double* p2, const double* p3,
446 double h0, double h1, double h2, double h3
447 );
448
449
450 /**
451 * \brief Computes the orientation predicate in 3d.
452 * \details Computes the sign of the signed volume of
453 * the tetrahedron p0, p1, p2, p3.
454 * \param[in] p0 , p1 , p2 , p3 vertices of the tetrahedron
455 * \retval POSITIVE if the tetrahedron is oriented positively
456 * \retval ZERO if the tetrahedron is flat
457 * \retval NEGATIVE if the tetrahedron is oriented negatively
458 */
459 Sign GEOGRAM_API orient_3d(
460 const double* p0, const double* p1,
461 const double* p2, const double* p3
462 );
463
464 /**
465 * \brief Computes the orientation predicate in 3d.
466 * \details Computes the sign of the signed volume of
467 * the tetrahedron p0, p1, p2, p3.
468 * \param[in] p0 , p1 , p2 , p3 vertices of the tetrahedron
469 * \retval POSITIVE if the tetrahedron is oriented positively
470 * \retval NEGATIVE if the tetrahedron is oriented negatively
471 * \retval perturb() if the tetrahedron is flat,
472 * where \c perturb() denotes a globally
473 * consistent perturbation, that returns either POSITIVE or NEGATIVE
474 */
475 Sign GEOGRAM_API orient_3d_SOS(
476 const double* p0, const double* p1,
477 const double* p2, const double* p3
478 );
479
480 #ifndef GEOGRAM_PSM
481 /**
482 * \brief Computes the orientation predicate in 3d.
483 * \details Computes the sign of the signed volume of
484 * the tetrahedron p0, p1, p2, p3.
485 * \param[in] p0 , p1 , p2 , p3 vertices of the tetrahedron
486 * \retval POSITIVE if the tetrahedron is oriented positively
487 * \retval ZERO if the tetrahedron is flat
488 * \retval NEGATIVE if the tetrahedron is oriented negatively
489 */
490 15291512 inline Sign orient_3d(
491 const vec3& p0, const vec3& p1,
492 const vec3& p2, const vec3& p3
493 ) {
494 15291512 return orient_3d(p0.data(),p1.data(),p2.data(),p3.data());
495 }
496
497 /**
498 * \brief Computes the orientation predicate in 3d.
499 * \details Computes the sign of the signed volume of
500 * the tetrahedron p0, p1, p2, p3.
501 * \param[in] p0 , p1 , p2 , p3 vertices of the tetrahedron
502 * \retval POSITIVE if the tetrahedron is oriented positively
503 * \retval NEGATIVE if the tetrahedron is oriented negatively
504 * \retval perturb() if the tetrahedron is flat,
505 * where \c perturb() denotes a globally
506 * consistent perturbation, that returns either POSITIVE or NEGATIVE
507 */
508 6135523 inline Sign GEOGRAM_API orient_3d_SOS(
509 const vec3& p0, const vec3& p1,
510 const vec3& p2, const vec3& p3
511 ) {
512 6135523 return orient_3d_SOS(p0.data(),p1.data(),p2.data(),p3.data());
513 }
514
515 #endif
516
517 /**
518 * \brief Computes the 4d orientation test.
519 * \details Given four lifted points p0', p1', p2', and p3' in
520 * R^4, tests if the lifted point p4' in R^4 lies below or above
521 * the hyperplance passing through the four points
522 * p0', p1', p2', and p3'.
523 * This version does not apply symbolic perturbation.
524 * The first three coordinates and the
525 * fourth one are specified in separate arguments for each vertex.
526 * \param[in] p0 , p1 , p2 , p3 , p4 first 3 coordinates
527 * of the vertices of the 4-simplex
528 * \param[in] h0 , h1 , h2 , h3 , h4 heights of the vertices of
529 * the 4-simplex
530 * \retval POSITIVE if p4' lies below the hyperplane
531 * \retval NEGATIVE if p4' lies above the hyperplane
532 * \retval ZERO if p4' lies exactly on the hyperplane
533 */
534 Sign GEOGRAM_API orient_3dlifted(
535 const double* p0, const double* p1,
536 const double* p2, const double* p3, const double* p4,
537 double h0, double h1, double h2, double h3, double h4
538 );
539
540
541 /**
542 * \brief Computes the 4d orientation test with symbolic perturbation.
543 * \details Given four lifted points p0', p1', p2', and p3' in
544 * R^4, tests if the lifted point p4' in R^4 lies below or above
545 * the hyperplance passing through the four
546 * points p0', p1', p2', and p3'.
547 * Symbolic perturbation is applied whenever the 5 vertices are
548 * not linearly independent. The first three coordinates and the
549 * fourth one are specified in separate arguments for each vertex.
550 * \param[in] p0 , p1 , p2 , p3 , p4 first 3 coordinates
551 * of the vertices of the 4-simplex
552 * \param[in] h0 , h1 , h2 , h3 , h4 heights of the vertices of
553 * the 4-simplex
554 * \retval POSITIVE if p4' lies below the hyperplane
555 * \retval NEGATIVE if p4' lies above the hyperplane
556 * \retval perturb() if p4' lies exactly on the hyperplane
557 * where \c perturb() denotes a globally
558 * consistent perturbation, that returns either POSITIVE or NEGATIVE
559 */
560 Sign GEOGRAM_API orient_3dlifted_SOS(
561 const double* p0, const double* p1,
562 const double* p2, const double* p3, const double* p4,
563 double h0, double h1, double h2, double h3, double h4
564 );
565
566
567 /**
568 * \brief Computes the sign of the determinant of a 3x3
569 * matrix formed by three 3d points.
570 * \param[in] p0 , p1 , p2 the three points
571 * \return the sign of the determinant of the matrix.
572 */
573 Sign GEOGRAM_API det_3d(
574 const double* p0, const double* p1, const double* p2
575 );
576
577 #ifndef GEOGRAM_PSM
578 /**
579 * \brief Computes the sign of the determinant of a 3x3
580 * matrix formed by three 3d points.
581 * \param[in] p0 , p1 , p2 the three points
582 * \return the sign of the determinant of the matrix.
583 */
584 2124 inline Sign det_3d(
585 const vec3& p0, const vec3& p1, const vec3& p2
586 ) {
587 2124 return det_3d(p0.data(), p1.data(), p2.data());
588 }
589 #endif
590
591 /**
592 * \brief Computes the sign of the determinant of a 4x4
593 * matrix formed by four 4d points.
594 * \param[in] p0 , p1 , p2 , p3 the four points
595 * \return the sign of the determinant of the matrix.
596 */
597 Sign GEOGRAM_API det_4d(
598 const double* p0, const double* p1,
599 const double* p2, const double* p3
600 );
601
602 #ifndef GEOGRAM_PSM
603 /**
604 * \brief Computes the sign of the determinant of a 4x4
605 * matrix formed by four 4d points.
606 * \param[in] p0 , p1 , p2 , p3 the four points
607 * \return the sign of the determinant of the matrix.
608 */
609 4386 inline Sign det_4d(
610 const vec4& p0, const vec4& p1,
611 const vec4& p2, const vec4& p3
612 ) {
613 4386 return det_4d(p0.data(), p1.data(), p2.data(), p3.data());
614 }
615 #endif
616
617 /**
618 * \brief Computes the sign of the determinant of a
619 * 4x4 matrix formed by three 4d points and the
620 * difference of two 4d points.
621 * \param[in] p0 , p1 , p2 , p3 , p4 the four points
622 * \return the sign of the determinant of the matrix
623 * p0 p1 p2 p4-p3
624 */
625 Sign GEOGRAM_API det_compare_4d(
626 const double* p0, const double* p1,
627 const double* p2, const double* p3,
628 const double* p4
629 );
630
631 /**
632 * \brief Tests whether three points are aligned.
633 * \param[in] p0 , p1 , p2 the three points
634 * \retval true if the three points are aligned.
635 * \retval false otherwise.
636 * \details Function to be tested, use points_are_colinear_3d()
637 * instead.
638 */
639 bool GEOGRAM_API aligned_3d(
640 const double* p0, const double* p1, const double* p2
641 );
642
643 /**
644 * \brief Computes the sign of the dot product between two
645 * vectors.
646 * \param[in] p0 , p1 , p2 three 3d points.
647 * \return the sign of the dot product between the vectors
648 * p0p1 and p0p2.
649 */
650 Sign GEOGRAM_API dot_3d(
651 const double* p0, const double* p1, const double* p2
652 );
653
654 #ifndef GEOGRAM_PSM
655
656 /**
657 * \brief Tests whether three points are aligned.
658 * \param[in] p0 , p1 , p2 the three points
659 * \retval true if the three points are aligned.
660 * \retval false otherwise.
661 * \details Function to be tested, use points_are_colinear_3d()
662 * instead.
663 */
664 148390 inline bool aligned_3d(
665 const vec3& p0, const vec3& p1, const vec3& p2
666 ) {
667 148390 return aligned_3d(p0.data(), p1.data(), p2.data());
668 }
669
670 /**
671 * \brief Computes the sign of the dot product between two
672 * vectors.
673 * \param[in] p0 , p1 , p2 three 3d points.
674 * \return the sign of the dot product between the vectors
675 * p0p1 and p0p2.
676 */
677 inline Sign dot_3d(
678 const vec3& p0, const vec3& p1, const vec3& p2
679 ) {
680 return dot_3d(p0.data(), p1.data(), p2.data());
681 }
682 #endif
683
684 /**
685 * \brief Compares two dot products.
686 * \param[in] v0 , v1 , v2 three vectors.
687 * \return the sign of v0.v1 - v0.v2
688 */
689 Sign GEOGRAM_API dot_compare_3d(
690 const double* v0, const double* v1, const double* v2
691 );
692
693 /**
694 * \brief Tests whether two 2d points are identical.
695 * \param[in] p1 first point
696 * \param[in] p2 second point
697 * \retval true if \p p1 and \p p2 have exactly the same
698 * coordinates
699 * \retval false otherwise
700 */
701 bool points_are_identical_2d(
702 const double* p1,
703 const double* p2
704 );
705
706 /**
707 * \brief Tests whether two 3d points are identical.
708 * \param[in] p1 first point
709 * \param[in] p2 second point
710 * \retval true if \p p1 and \p p2 have exactly the same
711 * coordinates
712 * \retval false otherwise
713 */
714 bool GEOGRAM_API points_are_identical_3d(
715 const double* p1,
716 const double* p2
717 );
718
719 /**
720 * \brief Tests whether three 3d points are colinear.
721 * \param[in] p1 first point
722 * \param[in] p2 second point
723 * \param[in] p3 third point
724 * \retval true if \p p1, \p p2 and \p p3 are colinear
725 * \retbal false otherwise
726 */
727 bool GEOGRAM_API points_are_colinear_3d(
728 const double* p1,
729 const double* p2,
730 const double* p3
731 );
732
733 /**
734 * \brief Computes the (approximate) orientation predicate in 3d.
735 * \details Computes the sign of the (approximate) signed volume of
736 * the tetrahedron p0, p1, p2, p3.
737 * \param[in] p0 first vertex of the tetrahedron
738 * \param[in] p1 second vertex of the tetrahedron
739 * \param[in] p2 third vertex of the tetrahedron
740 * \param[in] p3 fourth vertex of the tetrahedron
741 * \retval POSITIVE if the tetrahedron is oriented positively
742 * \retval ZERO if the tetrahedron is flat
743 * \retval NEGATIVE if the tetrahedron is oriented negatively
744 */
745 231436 inline Sign orient_3d_inexact(
746 const double* p0, const double* p1,
747 const double* p2, const double* p3
748 ) {
749 231436 double a11 = p1[0] - p0[0] ;
750 231436 double a12 = p1[1] - p0[1] ;
751 231436 double a13 = p1[2] - p0[2] ;
752
753 231436 double a21 = p2[0] - p0[0] ;
754 231436 double a22 = p2[1] - p0[1] ;
755 231436 double a23 = p2[2] - p0[2] ;
756
757 231436 double a31 = p3[0] - p0[0] ;
758 231436 double a32 = p3[1] - p0[1] ;
759 231436 double a33 = p3[2] - p0[2] ;
760
761 231436 double Delta = det3x3(
762 a11,a12,a13,
763 a21,a22,a23,
764 a31,a32,a33
765 231436 );
766
767
1/2
✓ Branch 1 taken 231436 times.
✗ Branch 2 not taken.
462872 return geo_sgn(Delta);
768 }
769
770 /**
771 * \brief Displays some statistics about predicates,
772 * including the number of calls, the number of exact arithmetics
773 * calls, and the number of Simulation of Simplicity calls.
774 */
775 void GEOGRAM_API show_stats();
776
777 /**
778 * \brief Needs to be called before using any predicate.
779 */
780 void GEOGRAM_API initialize();
781
782 /**
783 * \brief Needs to be called at the end of the program.
784 */
785 void GEOGRAM_API terminate();
786 }
787 }
788
789 /**************************************************************************/
790
791 namespace GEO {
792
793
794 /**
795 * \brief Implementation of the perturbed orient_3d predicate
796 * \param[in] p0 , p1 , p2 , p3 the four points, as const references to T
797 * \tparam T point class
798 * \tparam SOS a class with:
799 * - constructor that takes the four points as const references to T
800 * - Sign orient_1d(v1, v2, axis)
801 * - Sign orient_2d(v1, v2, v3, axis1, axis2)
802 * where v1, v2, v3 are in {0,1,2,3} and axis, axis1, axis2 in {0,1,2}.
803 * The returned sign is multiplied by the parity of the order of the
804 * four points.
805 * \details This is the result of a discussion with Marc Alexa (01/2026),
806 * see also their article: A practical algorithm for weighted k-hulls,
807 * Look, Meyer, Alexa, SGP 2026
808 */
809 6136210 template<class T, class SOS> inline Sign orient_3d_SOS_impl(
810 const T& p0, const T& p1, const T& p2, const T& p3
811 ) {
812 6136210 constexpr coord_index_t X = 0, Y = 1, Z = 2;
813
1/2
✓ Branch 1 taken 6136210 times.
✗ Branch 2 not taken.
6136210 Sign s = ::GEO::PCK::orient_3d(p0, p1, p2, p3);
814
2/2
✓ Branch 0 taken 5564136 times.
✓ Branch 1 taken 572074 times.
6136210 if(s != ZERO) {
815 5564136 return s;
816 }
817
818 // The perturbed determinant is as follows:
819 // | x1+eps y1+eps^2 z1+eps^4 1 |
820 // | x2+eps^8 y2+eps^16 z2+eps^32 1 |
821 // | x3+eps^64 y3+eps^128 z3+eps^256 1 |
822 // | x4+eps^512 y4+eps^1024 z4+eps^2048 1 |
823 //
824 // By developping and sorting by exponents of eps
825 // one gets the perturbations. Did it with TinyCAS:
826 // https://github.com/BrunoLevy/Experiment/blob/main/algo/tiny_cas.h
827 //
828 // | a b 1 |
829 // - The minors | c d 1 | correspond to orient_2d((a,b), (c,d), (e,f))
830 // | e f 1 |
831 //
832 // - The other terms are just difference of coordinates (orient_1d)
833
834 // Static array that encodes all the terms of the expansion.
835 static const struct SOSInfo {
836 index_t dim; // 0: constant, 1: orient_1d, 2: orient_2d
837 index_t v1, v2, v3; // local indices of the two or three vertices
838 index_t ax1, ax2; // one or two projection axes
839 Sign sign; // sign of the term
840 } sosInfo[] = {
841 {2, 1, 2, 3, Y, Z, POSITIVE}, // eps
842 {2, 1, 2, 3, X, Z, NEGATIVE}, // eps^2
843 {2, 1, 2, 3, X, Y, POSITIVE}, // eps^4
844 {2, 0, 2, 3, Y, Z, NEGATIVE}, // eps^8
845 {1, 3, 2, NO_INDEX, Z, NO_INDEX, POSITIVE}, // eps^10
846 {1, 2, 3, NO_INDEX, Y, NO_INDEX, POSITIVE}, // eps^12
847 {2, 0, 2, 3, X, Z, POSITIVE}, // eps^16
848 // z2-z3 = -term in eps^10, already seen // eps^17
849 {1, 3, 2, NO_INDEX, X, NO_INDEX, POSITIVE}, // eps^20
850 {2, 0, 2, 3, X, Y, NEGATIVE}, // eps^32
851 // y3-y2 = -term in eps^12, already seen // eps^33
852 // x2-x3 = -term in eps^20, already seen // eps^34
853 {2, 0, 1, 3, Y, Z, POSITIVE}, // eps^64
854 {1, 1, 3, NO_INDEX, Z, NO_INDEX, POSITIVE}, // eps^66
855 {1, 3, 1, NO_INDEX, Y, NO_INDEX, POSITIVE}, // eps^68
856 {1, 3, 0, NO_INDEX, Z, NO_INDEX, POSITIVE}, // eps^80
857 {0,NO_INDEX,NO_INDEX,NO_INDEX,NO_INDEX,NO_INDEX, NEGATIVE} // eps^84
858 // There are more terms (up to eps^2184) but we do not need them,
859 // since we got a (constant) non-zero coefficient for eps^84
860 };
861
862
1/2
✓ Branch 1 taken 572074 times.
✗ Branch 2 not taken.
572074 SOS sos(p0, p1, p2, p3);
863
864 1291103 for(index_t k=0; ;++k) {
865 1291103 const SOSInfo& I = sosInfo[k];
866
3/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 78890 times.
✓ Branch 2 taken 1212157 times.
✗ Branch 3 not taken.
1291103 switch(I.dim) {
867 56 case 0: {
868 56 return I.sign;
869 } break;
870 78890 case 1: {
871
1/2
✓ Branch 1 taken 78890 times.
✗ Branch 2 not taken.
78890 s = sos.orient_1d(I.v1, I.v2, I.ax1);
872
2/2
✓ Branch 0 taken 32793 times.
✓ Branch 1 taken 46097 times.
78890 if(s != ZERO) {
873 32793 return Sign(I.sign*s);
874 }
875 46097 } break;
876 1212157 case 2: {
877
1/2
✓ Branch 1 taken 1212157 times.
✗ Branch 2 not taken.
1212157 s = sos.orient_2d(I.v1, I.v2, I.v3, I.ax1, I.ax2);
878
2/2
✓ Branch 0 taken 539225 times.
✓ Branch 1 taken 672932 times.
1212157 if(s != ZERO) {
879 539225 return Sign(I.sign*s);
880 }
881 672932 } break;
882 ✗ default:
883 ✗ geo_assert_not_reached;
884 }
885 }
886 geo_assert_not_reached;
887 }
888
889 namespace Permutation {
890 /**
891 * \brief Computes the parity of a permutation
892 * \param[in] orig an array of pointers, typically nD points
893 * \param[in] perm a permutation of \p orig
894 * \param[in] n size of \p orig and \p perm, 64 max
895 * \retval true if \p perm is an odd permutation of \p orig
896 * \retval false if \p perm is an even permutation of \p orig
897 * \pre \p n <= 64 and \p perm is a permutation of \p orig
898 * \details operates in O(n^2) (only use for small arrays)
899 */
900 572074 template <class T> inline bool permutation_is_odd(
901 const T** orig, const T** perm, index_t n
902 ) {
903
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 572074 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
572074 geo_debug_assert(n <= 64);
904 572074 Numeric::uint64 visited = 0;
905 572074 bool odd = false;
906
2/2
✓ Branch 0 taken 2288296 times.
✓ Branch 1 taken 572074 times.
2860370 for (index_t i = 0; i < n; ++i) {
907
2/2
✓ Branch 0 taken 1125544 times.
✓ Branch 1 taken 1162752 times.
2288296 if ((visited >> i) & 1) {
908 1125544 continue;
909 }
910 // Compute the length of the cycle starting from perm[i]
911 1162752 index_t len = 0;
912
2/2
✓ Branch 0 taken 2288296 times.
✓ Branch 1 taken 1162752 times.
3451048 for (index_t j = i; !((visited >> j) & 1); ) {
913 2288296 visited |= (Numeric::uint64(1) << j);
914 2288296 ++len;
915 2288296 j = index_t(std::find(orig, orig + n, perm[j]) - orig);
916 }
917 // even-length cycle contributes odd parity
918
2/2
✓ Branch 0 taken 377172 times.
✓ Branch 1 taken 785580 times.
1162752 if (len % 2 == 0) odd = !odd;
919 }
920 572074 return odd;
921 }
922 }
923 }
924
925 #endif
926