use geometry_cs::CoordinateSystem;
use geometry_model::Point2D;
use geometry_trait::Point;
use crate::bounds::Bounds;
pub trait Indexable {
fn bounds(&self) -> Bounds;
}
impl<Cs: CoordinateSystem> Indexable for Point2D<f64, Cs> {
fn bounds(&self) -> Bounds {
Bounds::point([self.get::<0>(), self.get::<1>()])
}
}
impl Indexable for Bounds {
fn bounds(&self) -> Bounds {
*self
}
}
impl<T> Indexable for (Bounds, T) {
fn bounds(&self) -> Bounds {
self.0
}
}
#[cfg(test)]
#[allow(clippy::float_cmp, reason = "exact integer-valued coordinate literals")]
mod tests {
use super::Indexable;
use crate::bounds::Bounds;
use geometry_cs::Cartesian;
use geometry_model::Point2D;
#[test]
fn point_bounds_is_degenerate() {
let p = Point2D::<f64, Cartesian>::new(1.0, 2.0);
let b = p.bounds();
assert_eq!(b.min, [1.0, 2.0]);
assert_eq!(b.max, [1.0, 2.0]);
}
#[test]
fn box_is_its_own_bounds() {
let b = Bounds::new([0.0, 0.0], [5.0, 5.0]);
assert_eq!(b.bounds(), b);
}
#[test]
fn payload_pair_indexes_by_box() {
let value = (Bounds::new([1.0, 1.0], [2.0, 2.0]), "label");
assert_eq!(value.bounds(), Bounds::new([1.0, 1.0], [2.0, 2.0]));
}
}