use geo_types::{CoordNum, Rect, coord};
use geometry_coords::CoordinateScalar;
use geometry_tag::BoxTag;
use geometry_trait::{Box as BoxTrait, Geometry, IndexedAccess};
use crate::geo_coord::GeoCoord;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GeoRect<T: CoordNum> {
corners: [GeoCoord<T>; 2],
}
impl<T: CoordNum> GeoRect<T> {
#[inline]
#[must_use]
pub fn new(inner: Rect<T>) -> Self {
Self {
corners: [GeoCoord::new(inner.min()), GeoCoord::new(inner.max())],
}
}
#[inline]
#[must_use]
pub fn into_inner(self) -> Rect<T> {
Rect::new(self.corners[0].into_inner(), self.corners[1].into_inner())
}
}
impl<T: CoordinateScalar + CoordNum> Geometry for GeoRect<T> {
type Kind = BoxTag;
type Point = GeoCoord<T>;
}
impl<T: CoordinateScalar + CoordNum> IndexedAccess for GeoRect<T> {
#[inline]
fn get_indexed<const I: usize, const D: usize>(&self) -> T {
let c = self.corners[I].0;
if D == 0 { c.x } else { c.y }
}
#[inline]
fn set_indexed<const I: usize, const D: usize>(&mut self, value: T) {
let c = &mut self.corners[I].0;
if D == 0 {
*c = coord! { x: value, y: c.y };
} else {
*c = coord! { x: c.x, y: value };
}
}
}
impl<T: CoordinateScalar + CoordNum> BoxTrait for GeoRect<T> {}