pub(crate) mod kernel;
use crate::GeometryError;
use crate::topology::areal::AreaOperand;
use crate::topology::label::Location;
use crate::topology::line_analysis;
use crate::types::{Coord2d, LinearRing, Polygon};
pub use crate::topology::areal::Areal;
pub use crate::topology::line_analysis::{EventAt, SegmentEvent, rings};
pub use kernel::{RingPlacement, place_ring};
fn require_finite(coords: &[Coord2d]) -> Result<(), GeometryError> {
for (coord_index, c) in coords.iter().enumerate() {
if !c.x.is_finite() {
return Err(GeometryError::NonFiniteCoordinate {
coord_index,
axis: 0,
});
}
if !c.y.is_finite() {
return Err(GeometryError::NonFiniteCoordinate {
coord_index,
axis: 1,
});
}
}
Ok(())
}
pub fn on_segment(p: Coord2d, a: Coord2d, b: Coord2d) -> Result<bool, GeometryError> {
require_finite(&[p, a, b])?;
Ok(kernel::on_segment(p, a, b))
}
pub fn first_self_contact(ring: &LinearRing) -> Option<(usize, usize)> {
kernel::first_self_contact(ring.coords().as_slice())
}
pub fn locate(p: Coord2d, polygon: &Polygon) -> Result<Location, GeometryError> {
require_finite(&[p])?;
Ok(line_analysis::locate(p, polygon))
}
pub fn locate_in_areal(p: Coord2d, area: &Areal<'_>) -> Result<Location, GeometryError> {
require_finite(&[p])?;
Ok(area.locate(p))
}
pub fn segment_events(
p: Coord2d,
q: Coord2d,
area: &Areal<'_>,
) -> Result<Vec<SegmentEvent>, GeometryError> {
require_finite(&[p, q])?;
Ok(line_analysis::segment_events(p, q, area))
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::types::CoordSeq;
fn c(x: f64, y: f64) -> Coord2d {
Coord2d::new(x, y)
}
fn square() -> Polygon {
Polygon::new_simple(
LinearRing::new(CoordSeq::new(vec![
c(0.0, 0.0),
c(4.0, 0.0),
c(4.0, 4.0),
c(0.0, 4.0),
]))
.unwrap(),
)
.unwrap()
}
#[test]
fn checked_items_refuse_non_finite_coordinates_by_position() {
let ok = c(1.0, 0.0);
for bad in [c(f64::NAN, 0.0), c(0.0, f64::INFINITY)] {
let axis = u8::from(bad.x.is_finite());
for pos in 0..3 {
let mut args = [ok, c(0.0, 0.0), c(4.0, 0.0)];
args[pos] = bad;
assert_eq!(
on_segment(args[0], args[1], args[2]),
Err(GeometryError::NonFiniteCoordinate {
coord_index: pos,
axis
}),
"on_segment, position {pos}"
);
}
let sq = square();
let area = Areal::new(&sq);
let at = |coord_index| GeometryError::NonFiniteCoordinate { coord_index, axis };
assert_eq!(locate(bad, &sq), Err(at(0)));
assert_eq!(locate_in_areal(bad, &area), Err(at(0)));
assert_eq!(segment_events(bad, ok, &area).err(), Some(at(0)));
assert_eq!(segment_events(ok, bad, &area).err(), Some(at(1)));
}
}
#[test]
fn checked_items_agree_with_the_kernel_on_finite_input() {
let sq = square();
let area = Areal::new(&sq);
assert_eq!(on_segment(c(2.0, 0.0), c(0.0, 0.0), c(4.0, 0.0)), Ok(true));
assert_eq!(on_segment(c(2.0, 1.0), c(0.0, 0.0), c(4.0, 0.0)), Ok(false));
for (p, want) in [
(c(2.0, 2.0), Location::Interior),
(c(4.0, 1.0), Location::Boundary),
(c(0.0, 0.0), Location::Boundary),
(c(5.0, 2.0), Location::Exterior),
] {
assert_eq!(locate(p, &sq), Ok(want), "{p:?}");
assert_eq!(locate_in_areal(p, &area), Ok(want), "{p:?}");
assert_eq!(locate(p, &sq).unwrap(), line_analysis::locate(p, &sq));
}
let ev = segment_events(c(2.0, 2.0), c(6.0, 2.0), &area).unwrap();
assert_eq!(ev.len(), 2);
assert_eq!(ev[0].at, EventAt::Point(c(2.0, 2.0)));
assert_eq!(ev[0].ahead, Location::Interior);
assert_eq!(
ev[1].at,
EventAt::Crossing {
a: c(4.0, 0.0),
b: c(4.0, 4.0)
}
);
assert_eq!(ev[1].ahead, Location::Exterior);
assert_eq!(first_self_contact(sq.exterior()), None);
assert_eq!(rings(&sq).count(), 1);
}
}