use core::cmp::Ordering;
use arithmetica::{RationalCoord2d, Sign, orient2d, orient2d_rational, rational_compare_along};
use crate::exact::kernel::{edge_crosses_ray, on_segment};
use crate::topology::areal::{AreaOperand, Window};
use crate::topology::label::Location;
use crate::types::Coord2d;
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub enum ExactPoint<'a> {
Stored(Coord2d),
Rational(&'a RationalCoord2d),
}
const AXIS_X: ([f64; 2], [f64; 2]) = ([0.0, 0.0], [1.0, 0.0]);
const AXIS_Y: ([f64; 2], [f64; 2]) = ([0.0, 0.0], [0.0, 1.0]);
fn cmp_coord(r: &RationalCoord2d, axis: ([f64; 2], [f64; 2]), stored: Coord2d) -> Option<Ordering> {
let s = RationalCoord2d::from_f64([stored.x, stored.y]).ok()?;
rational_compare_along(axis.0, axis.1, r, &s).ok()
}
impl ExactPoint<'_> {
pub(crate) fn window(&self) -> Window {
match self {
ExactPoint::Stored(c) => (*c, *c),
ExactPoint::Rational(r) => {
let [bx, by] = r.f64_bracket();
(Coord2d::new(bx.lo, by.lo), Coord2d::new(bx.hi, by.hi))
}
}
}
fn on_segment(&self, a: Coord2d, b: Coord2d) -> bool {
match self {
ExactPoint::Stored(p) => on_segment(*p, a, b),
ExactPoint::Rational(r) => {
let collinear =
orient2d_rational([a.x, a.y], [b.x, b.y], r).ok() == Some(Sign::Zero);
if !collinear {
return false;
}
let within = |axis, lo: Coord2d, hi: Coord2d| {
cmp_coord(r, axis, lo) != Some(Ordering::Less)
&& cmp_coord(r, axis, hi) != Some(Ordering::Greater)
};
within(
AXIS_X,
Coord2d::new(a.x.min(b.x), 0.0),
Coord2d::new(a.x.max(b.x), 0.0),
) && within(
AXIS_Y,
Coord2d::new(0.0, a.y.min(b.y)),
Coord2d::new(0.0, a.y.max(b.y)),
)
}
}
}
fn edge_crosses_ray(&self, a: Coord2d, b: Coord2d) -> bool {
match self {
ExactPoint::Stored(p) => edge_crosses_ray(a, b, *p),
ExactPoint::Rational(r) => {
let above = |c: Coord2d| cmp_coord(r, AXIS_Y, c) == Some(Ordering::Less);
if above(a) == above(b) {
return false;
}
let s = orient2d_rational([a.x, a.y], [b.x, b.y], r).ok();
if b.y > a.y {
s == Some(Sign::Positive)
} else {
s == Some(Sign::Negative)
}
}
}
}
#[allow(clippy::float_cmp)]
fn upper_half(&self, w: Coord2d) -> bool {
match self {
ExactPoint::Stored(x) => w.y > x.y || (w.y == x.y && w.x > x.x),
ExactPoint::Rational(r) => match cmp_coord(r, AXIS_Y, w) {
Some(Ordering::Less) => true,
Some(Ordering::Equal) => cmp_coord(r, AXIS_X, w) == Some(Ordering::Less),
_ => false,
},
}
}
fn cmp_angle(&self, u: Coord2d, v: Coord2d) -> Ordering {
match (self.upper_half(u), self.upper_half(v)) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => {
let s = match self {
ExactPoint::Stored(x) => orient2d([x.x, x.y], [u.x, u.y], [v.x, v.y]).ok(),
ExactPoint::Rational(r) => orient2d_rational([u.x, u.y], [v.x, v.y], r).ok(),
};
match s {
Some(Sign::Positive) => Ordering::Less,
Some(Sign::Negative) => Ordering::Greater,
_ => Ordering::Equal,
}
}
}
}
fn outside_ray_reach(&self, env: (f64, f64, f64, f64)) -> bool {
match self {
ExactPoint::Stored(p) => p.x > env.2 || p.y < env.1 || p.y > env.3,
ExactPoint::Rational(r) => {
cmp_coord(r, AXIS_X, Coord2d::new(env.2, 0.0)) == Some(Ordering::Greater)
|| cmp_coord(r, AXIS_Y, Coord2d::new(0.0, env.1)) == Some(Ordering::Less)
|| cmp_coord(r, AXIS_Y, Coord2d::new(0.0, env.3)) == Some(Ordering::Greater)
}
}
}
}
#[derive(Clone, Copy)]
struct Ray {
to: Coord2d,
is_out: bool,
}
fn incident_rays<A: AreaOperand + ?Sized>(x: &ExactPoint<'_>, area: &A) -> Vec<Ray> {
let mut out = Vec::new();
let stored_eq = |c: Coord2d| match x {
ExactPoint::Stored(p) => *p == c,
ExactPoint::Rational(r) => RationalCoord2d::from_f64([c.x, c.y])
.ok()
.is_some_and(|s| arithmetica::rational_coords_equal(r, &s)),
};
area.for_edges_near(&x.window(), &mut |a, b| {
if stored_eq(a) {
out.push(Ray {
to: b,
is_out: true,
});
} else if stored_eq(b) {
out.push(Ray {
to: a,
is_out: false,
});
} else if x.on_segment(a, b) {
out.push(Ray {
to: b,
is_out: true,
});
out.push(Ray {
to: a,
is_out: false,
});
}
true
});
out
}
pub(crate) fn locate<A: AreaOperand + ?Sized>(x: &ExactPoint<'_>, area: &A) -> Location {
let mut on = false;
area.for_edges_near(&x.window(), &mut |a, b| {
on = x.on_segment(a, b);
!on
});
if on {
return Location::Boundary;
}
let Some(env) = area.envelope() else {
return Location::Exterior;
};
if x.outside_ray_reach(env) {
return Location::Exterior;
}
let (lo, hi) = x.window();
let ray: Window = (lo, Coord2d::new(env.2, hi.y));
let mut parity: Vec<(usize, usize)> = Vec::new();
area.for_edges_with_ref_near(&ray, &mut |part, ring, a, b| {
if x.edge_crosses_ray(a, b) {
parity.push((part, ring));
}
true
});
parity.sort_unstable();
let mut odd: Vec<(usize, usize)> = Vec::new();
let mut i = 0;
while i < parity.len() {
let mut j = i;
while j < parity.len() && parity[j] == parity[i] {
j += 1;
}
if (j - i) % 2 == 1 {
odd.push(parity[i]);
}
i = j;
}
let inside = odd
.iter()
.filter(|(_, ring)| *ring == 0)
.any(|&(part, _)| !odd.iter().any(|&(q, ring)| q == part && ring > 0));
if inside {
Location::Interior
} else {
Location::Exterior
}
}
pub(crate) fn location_left_of<A: AreaOperand + ?Sized>(
x: &ExactPoint<'_>,
toward: Coord2d,
area: &A,
) -> Location {
let rays = incident_rays(x, area);
if rays.is_empty() {
return locate(x, area);
}
if let Some(r) = rays
.iter()
.find(|r| x.cmp_angle(r.to, toward) == Ordering::Equal)
{
return if r.is_out {
Location::Interior
} else {
Location::Exterior
};
}
let mut below: Option<&Ray> = None;
let mut greatest: Option<&Ray> = None;
for r in &rays {
if x.cmp_angle(r.to, toward) == Ordering::Less
&& below.is_none_or(|b| x.cmp_angle(b.to, r.to) == Ordering::Less)
{
below = Some(r);
}
if greatest.is_none_or(|g| x.cmp_angle(g.to, r.to) == Ordering::Less) {
greatest = Some(r);
}
}
match below.or(greatest) {
Some(r) if r.is_out => Location::Interior,
_ => Location::Exterior,
}
}