geometry_overlay/predicate/orientation.rs
1//! OVL1.T1 — the orientation (side) predicate.
2//!
3//! Given three points `p`, `q`, `r`, decide whether `r` lies to the
4//! left of, to the right of, or on the directed line `p → q`. This is
5//! the signed area of the triangle `(p, q, r)`, reduced to its sign.
6//!
7//! Mirrors `boost::geometry::strategy::side::side_by_triangle`
8//! (`boost/geometry/strategy/cartesian/side_by_triangle.hpp`). Boost's
9//! `side_value` computes the same signed area
10//! `(qx - px)(ry - py) - (qy - py)(rx - px)`; its result sign is the
11//! side, with `+1` = left, `-1` = right, `0` = collinear — the
12//! convention the spherical side test spells out explicitly
13//! (`test/strategies/spherical_side.cpp:55-56`: `side == 1 ? 'L' :
14//! side == -1 ? 'R'`).
15//!
16//! # Robustness
17//!
18//! The sign is computed on the raw input coordinates (no rescale) by the
19//! adaptive expansion arithmetic in
20//! [`geometry_coords::precise_math::orient2d`]. This mirrors Boost's robust
21//! side strategy and produces the exact sign for finite `f32`/`f64` inputs.
22//! Boost's
23//! `side_by_triangle` additionally treats any coincident pair among the
24//! three points as collinear
25//! (`side_by_triangle.hpp:159-164`); this predicate does the same,
26//! because a zero-length base line has no well-defined side.
27
28use geometry_coords::{CoordinateScalar, precise_math};
29use geometry_trait::Point;
30
31/// The three possible outcomes of the [`orientation_2d`] side test.
32///
33/// Mirrors the `+1 / 0 / -1` return of Boost's `side_by_triangle`
34/// (`boost/geometry/strategy/cartesian/side_by_triangle.hpp`), named
35/// so call sites read as topology rather than as integers.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37pub enum Sign {
38 /// `r` lies to the **left** of the directed line `p → q`
39 /// (counter-clockwise turn). Boost's `+1`, the `'L'` case in
40 /// `test/strategies/spherical_side.cpp`.
41 Positive,
42 /// `r` lies to the **right** of the directed line `p → q`
43 /// (clockwise turn). Boost's `-1`, the `'R'` case.
44 Negative,
45 /// `p`, `q`, `r` are **collinear** (or two of them coincide).
46 /// Boost's `0`, the `'|'` case.
47 Collinear,
48}
49
50/// Sign of the signed area of the triangle `(p, q, r)` — i.e. which
51/// side of the directed line `p → q` the point `r` lies on.
52///
53/// Returns [`Sign::Positive`] for a left turn (counter-clockwise),
54/// [`Sign::Negative`] for a right turn (clockwise), and
55/// [`Sign::Collinear`] when the three points are collinear or any two
56/// coincide.
57///
58/// Mirrors `side_by_triangle::apply`
59/// (`boost/geometry/strategy/cartesian/side_by_triangle.hpp:144-147`),
60/// computing `(qx - px)(ry - py) - (qy - py)(rx - px)` and taking its
61/// sign. Cartesian only.
62///
63/// # Examples
64///
65/// ```
66/// use geometry_cs::Cartesian;
67/// use geometry_model::Point2D;
68/// use geometry_overlay::predicate::orientation::{orientation_2d, Sign};
69///
70/// type P = Point2D<f64, Cartesian>;
71/// let p = P::new(0.0, 0.0);
72/// let q = P::new(1.0, 0.0);
73///
74/// // A point above the x-axis is to the left of p → q.
75/// assert_eq!(orientation_2d(&p, &q, &P::new(0.5, 1.0)), Sign::Positive);
76/// // Below is to the right.
77/// assert_eq!(orientation_2d(&p, &q, &P::new(0.5, -1.0)), Sign::Negative);
78/// // On the axis is collinear.
79/// assert_eq!(orientation_2d(&p, &q, &P::new(2.0, 0.0)), Sign::Collinear);
80/// ```
81#[must_use]
82pub fn orientation_2d<P>(p: &P, q: &P, r: &P) -> Sign
83where
84 P: Point,
85 P::Scalar: CoordinateScalar + Into<f64>,
86{
87 let px = p.get::<0>();
88 let py = p.get::<1>();
89 let qx = q.get::<0>();
90 let qy = q.get::<1>();
91 let rx = r.get::<0>();
92 let ry = r.get::<1>();
93
94 // Signed area of (p, q, r). Boost's `side_by_triangle::side_value`
95 // computes the identical determinant
96 // (`side_by_triangle.hpp` `side_value`).
97 let area = precise_math::orient2d(
98 [px.into(), py.into()],
99 [qx.into(), qy.into()],
100 [rx.into(), ry.into()],
101 );
102
103 if area > 0.0 {
104 Sign::Positive
105 } else if area < 0.0 {
106 Sign::Negative
107 } else {
108 Sign::Collinear
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 //! Reproduces the left / right / collinear convention asserted in
115 //! `test/strategies/spherical_side.cpp:55-56` (`1 = 'L'`,
116 //! `-1 = 'R'`, else collinear), on the Cartesian predicate.
117
118 use super::{Sign, orientation_2d};
119 use geometry_cs::Cartesian;
120 use geometry_model::Point2D;
121
122 type P = Point2D<f64, Cartesian>;
123
124 #[test]
125 fn left_right_collinear_unit_segment() {
126 let p = P::new(0.0, 0.0);
127 let q = P::new(1.0, 0.0);
128 assert_eq!(orientation_2d(&p, &q, &P::new(0.5, 1.0)), Sign::Positive);
129 assert_eq!(orientation_2d(&p, &q, &P::new(0.5, -1.0)), Sign::Negative);
130 assert_eq!(orientation_2d(&p, &q, &P::new(0.5, 0.0)), Sign::Collinear);
131 }
132
133 #[test]
134 fn sign_flips_with_base_direction() {
135 // Reversing the directed base line flips left ↔ right — the
136 // signed area negates. `side_by_triangle` has the same
137 // antisymmetry.
138 let a = P::new(0.0, 0.0);
139 let b = P::new(4.0, 4.0);
140 let c = P::new(4.0, 0.0);
141 assert_eq!(orientation_2d(&a, &b, &c), Sign::Negative);
142 assert_eq!(orientation_2d(&b, &a, &c), Sign::Positive);
143 }
144
145 #[test]
146 fn coincident_points_are_collinear() {
147 // Boost returns 0 whenever two of the three points coincide
148 // (`side_by_triangle.hpp:159-164`) — a zero-length base line
149 // has no side.
150 let p = P::new(2.0, 3.0);
151 let r = P::new(9.0, 9.0);
152 assert_eq!(orientation_2d(&p, &p, &r), Sign::Collinear);
153 assert_eq!(orientation_2d(&p, &r, &p), Sign::Collinear);
154 assert_eq!(orientation_2d(&r, &p, &p), Sign::Collinear);
155 }
156
157 #[test]
158 fn diagonal_line_sides() {
159 // Line y = x, direction (0,0) → (2,2).
160 let p = P::new(0.0, 0.0);
161 let q = P::new(2.0, 2.0);
162 // (0,2) is above the line → left.
163 assert_eq!(orientation_2d(&p, &q, &P::new(0.0, 2.0)), Sign::Positive);
164 // (2,0) is below the line → right.
165 assert_eq!(orientation_2d(&p, &q, &P::new(2.0, 0.0)), Sign::Negative);
166 // (5,5) is on the line → collinear.
167 assert_eq!(orientation_2d(&p, &q, &P::new(5.0, 5.0)), Sign::Collinear);
168 }
169}