use crate::error::GeometryError;
use crate::types::Coord2d;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Envelope {
min_x: f64,
min_y: f64,
max_x: f64,
max_y: f64,
}
impl Envelope {
pub fn new(min: Coord2d, max: Coord2d) -> Result<Self, GeometryError> {
for (index, c) in [(0usize, min), (1, max)] {
if !c.x.is_finite() {
return Err(GeometryError::NonFiniteCoordinate {
coord_index: index,
axis: 0,
});
}
if !c.y.is_finite() {
return Err(GeometryError::NonFiniteCoordinate {
coord_index: index,
axis: 1,
});
}
}
if min.x > max.x || min.y > max.y {
return Err(GeometryError::InvertedEnvelope);
}
Ok(Self {
min_x: min.x,
min_y: min.y,
max_x: max.x,
max_y: max.y,
})
}
pub fn from_points<I>(points: I) -> Result<Option<Envelope>, GeometryError>
where
I: IntoIterator<Item = Coord2d>,
{
let mut bounds: Option<(f64, f64, f64, f64)> = None;
for (coord_index, c) in points.into_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,
});
}
bounds = Some(match bounds {
None => (c.x, c.y, c.x, c.y),
Some((min_x, min_y, max_x, max_y)) => (
if c.x < min_x { c.x } else { min_x },
if c.y < min_y { c.y } else { min_y },
if c.x > max_x { c.x } else { max_x },
if c.y > max_y { c.y } else { max_y },
),
});
}
Ok(bounds.map(|(min_x, min_y, max_x, max_y)| Self {
min_x,
min_y,
max_x,
max_y,
}))
}
#[allow(clippy::float_cmp)] pub fn is_point(&self) -> bool {
self.min_x == self.max_x && self.min_y == self.max_y
}
pub fn min(&self) -> Coord2d {
Coord2d::new(self.min_x, self.min_y)
}
pub fn max(&self) -> Coord2d {
Coord2d::new(self.max_x, self.max_y)
}
pub fn width(&self) -> f64 {
self.max_x - self.min_x
}
pub fn height(&self) -> f64 {
self.max_y - self.min_y
}
pub fn contains_coord(&self, c: Coord2d) -> bool {
c.x >= self.min_x && c.x <= self.max_x && c.y >= self.min_y && c.y <= self.max_y
}
pub fn contains_envelope(&self, other: &Envelope) -> bool {
other.min_x >= self.min_x
&& other.max_x <= self.max_x
&& other.min_y >= self.min_y
&& other.max_y <= self.max_y
}
pub fn intersects(&self, other: &Envelope) -> bool {
self.min_x <= other.max_x
&& other.min_x <= self.max_x
&& self.min_y <= other.max_y
&& other.min_y <= self.max_y
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[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::*;
fn c(x: f64, y: f64) -> Coord2d {
Coord2d::new(x, y)
}
fn env(a: f64, b: f64, x: f64, y: f64) -> Envelope {
Envelope::new(c(a, b), c(x, y)).unwrap()
}
#[test]
fn accessors_return_the_constructed_corners() {
let e = env(-1.0, -2.0, 3.0, 4.0);
assert_eq!(e.min(), c(-1.0, -2.0));
assert_eq!(e.max(), c(3.0, 4.0));
assert_eq!(e.width(), 4.0);
assert_eq!(e.height(), 6.0);
}
#[test]
fn a_zero_width_envelope_is_valid() {
let e = env(1.0, 0.0, 1.0, 5.0);
assert_eq!(e.width(), 0.0);
assert_eq!(e.height(), 5.0);
assert!(e.contains_coord(c(1.0, 2.5)));
}
#[test]
fn a_point_sized_envelope_is_valid() {
let e = env(2.0, 3.0, 2.0, 3.0);
assert_eq!(e.width(), 0.0);
assert_eq!(e.height(), 0.0);
assert!(e.contains_coord(c(2.0, 3.0)));
assert!(!e.contains_coord(c(2.0, 3.000_000_1)));
}
#[test]
fn inverted_bounds_are_rejected_on_each_axis() {
assert_eq!(
Envelope::new(c(4.0, 0.0), c(0.0, 2.0)).unwrap_err(),
GeometryError::InvertedEnvelope
);
assert_eq!(
Envelope::new(c(0.0, 4.0), c(2.0, 0.0)).unwrap_err(),
GeometryError::InvertedEnvelope
);
}
#[test]
fn non_finite_bounds_are_rejected_with_their_position() {
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
assert_eq!(
Envelope::new(c(bad, 0.0), c(1.0, 1.0)).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 0
}
);
assert_eq!(
Envelope::new(c(0.0, bad), c(1.0, 1.0)).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 1
}
);
assert_eq!(
Envelope::new(c(0.0, 0.0), c(bad, 1.0)).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 1,
axis: 0
}
);
assert_eq!(
Envelope::new(c(0.0, 0.0), c(1.0, bad)).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 1,
axis: 1
}
);
}
}
#[test]
fn non_finite_is_reported_even_when_bounds_look_inverted() {
assert_eq!(
Envelope::new(c(f64::NAN, 5.0), c(0.0, 0.0)).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 0
}
);
}
#[test]
fn containment_is_closed_on_every_edge() {
let e = env(0.0, 0.0, 2.0, 2.0);
for p in [
c(0.0, 0.0),
c(2.0, 2.0),
c(0.0, 2.0),
c(2.0, 0.0),
c(1.0, 0.0),
c(0.0, 1.0),
] {
assert!(e.contains_coord(p), "{p:?} is on the boundary");
}
assert!(!e.contains_coord(c(-0.000_000_1, 1.0)));
assert!(!e.contains_coord(c(1.0, 2.000_000_1)));
}
#[test]
fn intersection_is_closed_so_touching_counts() {
let a = env(0.0, 0.0, 2.0, 2.0);
assert!(a.intersects(&env(1.0, 1.0, 3.0, 3.0)), "overlapping");
assert!(a.intersects(&env(2.0, 0.0, 4.0, 2.0)), "edge contact");
assert!(a.intersects(&env(2.0, 2.0, 4.0, 4.0)), "corner contact");
assert!(a.intersects(&env(0.5, 0.5, 1.5, 1.5)), "contained");
assert!(!a.intersects(&env(2.5, 0.0, 4.0, 2.0)), "separated in x");
assert!(!a.intersects(&env(0.0, 2.5, 2.0, 4.0)), "separated in y");
}
#[test]
fn from_points_of_nothing_is_none() {
assert_eq!(Envelope::from_points(std::iter::empty()), Ok(None));
}
#[test]
fn from_points_of_one_point_is_that_point() {
let e = Envelope::from_points([c(2.0, 3.0)]).unwrap().unwrap();
assert_eq!(e, env(2.0, 3.0, 2.0, 3.0));
assert!(e.is_point());
assert_eq!(e.width(), 0.0);
}
#[test]
fn from_points_takes_the_extremes_in_any_order() {
let pts = [c(3.0, -1.0), c(-2.0, 7.0), c(0.5, 0.5), c(3.0, 7.0)];
let e = Envelope::from_points(pts).unwrap().unwrap();
assert_eq!(e, env(-2.0, -1.0, 3.0, 7.0));
assert!(!e.is_point());
for v in [e.min().x, e.min().y, e.max().x, e.max().y] {
assert!(
pts.iter()
.any(|p| p.x.to_bits() == v.to_bits() || p.y.to_bits() == v.to_bits())
);
}
}
#[test]
fn from_points_keeps_negative_zero_as_the_input_bit_pattern() {
let e = Envelope::from_points([c(-0.0, 1.0), c(0.0, 2.0)])
.unwrap()
.unwrap();
assert_eq!(e.min().x.to_bits(), (-0.0_f64).to_bits());
assert_eq!(e.max().x.to_bits(), (-0.0_f64).to_bits());
let e = Envelope::from_points([c(0.0, 1.0), c(-0.0, 2.0)])
.unwrap()
.unwrap();
assert_eq!(e.min().x.to_bits(), 0.0_f64.to_bits());
assert_eq!(e.max().x.to_bits(), 0.0_f64.to_bits());
}
#[test]
fn from_points_reports_the_first_non_finite_value_by_index_and_axis() {
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
assert_eq!(
Envelope::from_points([c(0.0, 0.0), c(1.0, bad), c(bad, bad)]).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 1,
axis: 1
}
);
assert_eq!(
Envelope::from_points([c(bad, 0.0)]).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 0
}
);
}
}
#[test]
fn is_point_is_false_for_a_line_and_a_box() {
assert!(!env(1.0, 0.0, 1.0, 5.0).is_point());
assert!(!env(0.0, 2.0, 5.0, 2.0).is_point());
assert!(!env(0.0, 0.0, 1.0, 1.0).is_point());
assert!(env(-4.5, 9.0, -4.5, 9.0).is_point());
}
#[test]
fn intersection_is_symmetric() {
let pairs = [
(env(0.0, 0.0, 2.0, 2.0), env(1.0, 1.0, 3.0, 3.0)),
(env(0.0, 0.0, 2.0, 2.0), env(5.0, 5.0, 6.0, 6.0)),
(env(0.0, 0.0, 2.0, 2.0), env(2.0, 2.0, 3.0, 3.0)),
];
for (a, b) in pairs {
assert_eq!(a.intersects(&b), b.intersects(&a));
}
}
}