use std::ops::Index;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Coord2d {
pub x: f64,
pub y: f64,
}
impl Coord2d {
#[inline]
pub fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct CoordSeq(Vec<Coord2d>);
impl CoordSeq {
pub fn new(coords: Vec<Coord2d>) -> Self {
Self(coords)
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn as_slice(&self) -> &[Coord2d] {
&self.0
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = &Coord2d> {
self.0.iter()
}
}
impl Index<usize> for CoordSeq {
type Output = Coord2d;
#[inline]
fn index(&self, i: usize) -> &Self::Output {
&self.0[i]
}
}
pub(crate) type Bbox = (f64, f64, f64, f64);
pub(crate) fn bbox_of(cs: &[Coord2d]) -> Bbox {
let Some(first) = cs.first() else {
return (
f64::INFINITY,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NEG_INFINITY,
);
};
cs[1..]
.iter()
.fold((first.x, first.y, first.x, first.y), |e, c| {
(e.0.min(c.x), e.1.min(c.y), e.2.max(c.x), e.3.max(c.y))
})
}
pub(crate) fn bbox_fold(boxes: impl IntoIterator<Item = Bbox>) -> Option<Bbox> {
boxes
.into_iter()
.reduce(|a, b| (a.0.min(b.0), a.1.min(b.1), a.2.max(b.2), a.3.max(b.3)))
}
pub(super) fn check_coords(coords: &CoordSeq) -> Result<(), crate::GeometryError> {
for (i, c) in coords.iter().enumerate() {
if !c.x.is_finite() {
return Err(crate::GeometryError::NonFiniteCoordinate {
coord_index: i,
axis: 0,
});
}
if !c.y.is_finite() {
return Err(crate::GeometryError::NonFiniteCoordinate {
coord_index: i,
axis: 1,
});
}
}
for i in 0..coords.len().saturating_sub(1) {
if coords[i] == coords[i + 1] {
return Err(crate::GeometryError::ConsecutiveDuplicateVertices { index: i });
}
}
Ok(())
}
#[cfg(test)]
#[allow(
clippy::float_cmp,
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_possible_wrap,
clippy::range_plus_one,
clippy::no_effect_underscore_binding
)]
mod tests {
use super::*;
#[test]
fn coord2d_new_round_trips_through_accessors() {
let c = Coord2d::new(1.0, 2.0);
assert_eq!(c.x, 1.0);
assert_eq!(c.y, 2.0);
}
#[test]
fn coord_seq_as_slice_returns_expected_coordinates() {
let a = Coord2d::new(0.0, 0.0);
let b = Coord2d::new(1.0, 0.0);
let c = Coord2d::new(1.0, 1.0);
let seq = CoordSeq::new(vec![a, b, c]);
let sl = seq.as_slice();
assert_eq!(sl.len(), 3);
assert_eq!(sl[0], a);
assert_eq!(sl[1], b);
assert_eq!(sl[2], c);
}
#[test]
fn coord_seq_index_matches_as_slice() {
let a = Coord2d::new(3.0, 4.0);
let seq = CoordSeq::new(vec![a]);
assert_eq!(seq[0], a);
}
#[test]
fn coord_seq_len_and_is_empty() {
let empty = CoordSeq::new(vec![]);
assert!(empty.is_empty());
assert_eq!(empty.len(), 0);
let one = CoordSeq::new(vec![Coord2d::new(0.0, 0.0)]);
assert!(!one.is_empty());
assert_eq!(one.len(), 1);
}
#[test]
fn coord_seq_iter_yields_all_coordinates() {
let coords = vec![
Coord2d::new(1.0, 2.0),
Coord2d::new(3.0, 4.0),
Coord2d::new(5.0, 6.0),
];
let seq = CoordSeq::new(coords.clone());
let collected: Vec<&Coord2d> = seq.iter().collect();
assert_eq!(collected.len(), 3);
for (i, c) in collected.into_iter().enumerate() {
assert_eq!(*c, coords[i]);
}
}
}