use std::ops::Add;
use std::ops::AddAssign;
use num_traits::{Float, ToPrimitive};
use ::{CoordinateType, Point};
pub static COORD_PRECISION: f32 = 1e-1;
#[cfg_attr(feature = "use-serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Bbox<T>
where
T: CoordinateType,
{
pub xmin: T,
pub xmax: T,
pub ymin: T,
pub ymax: T,
}
#[cfg_attr(feature = "use-serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Extremes {
pub ymin: usize,
pub xmax: usize,
pub ymax: usize,
pub xmin: usize,
}
impl From<Vec<usize>> for Extremes {
fn from(original: Vec<usize>) -> Extremes {
Extremes {
ymin: original[0],
xmax: original[1],
ymax: original[2],
xmin: original[3],
}
}
}
#[cfg_attr(feature = "use-serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct ExtremePoint<T>
where
T: CoordinateType,
{
pub ymin: Point<T>,
pub xmax: Point<T>,
pub ymax: Point<T>,
pub xmin: Point<T>,
}
impl<T> Add for Bbox<T>
where
T: CoordinateType + ToPrimitive,
{
type Output = Bbox<T>;
fn add(self, rhs: Bbox<T>) -> Bbox<T> {
Bbox {
xmin: if self.xmin <= rhs.xmin {
self.xmin
} else {
rhs.xmin
},
xmax: if self.xmax >= rhs.xmax {
self.xmax
} else {
rhs.xmax
},
ymin: if self.ymin <= rhs.ymin {
self.ymin
} else {
rhs.ymin
},
ymax: if self.ymax >= rhs.ymax {
self.ymax
} else {
rhs.ymax
},
}
}
}
impl<T> AddAssign for Bbox<T>
where
T: CoordinateType + ToPrimitive,
{
fn add_assign(&mut self, rhs: Bbox<T>) {
self.xmin = if self.xmin <= rhs.xmin {
self.xmin
} else {
rhs.xmin
};
self.xmax = if self.xmax >= rhs.xmax {
self.xmax
} else {
rhs.xmax
};
self.ymin = if self.ymin <= rhs.ymin {
self.ymin
} else {
rhs.ymin
};
self.ymax = if self.ymax >= rhs.ymax {
self.ymax
} else {
rhs.ymax
};
}
}
#[cfg_attr(feature = "use-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Closest<F: Float> {
Intersection(Point<F>),
SinglePoint(Point<F>),
Indeterminate,
}
impl<F: Float> Closest<F> {
pub fn best_of_two(&self, other: &Self, p: &Point<F>) -> Self {
use algorithm::euclidean_distance::EuclideanDistance;
let left = match *self {
Closest::Indeterminate => return *other,
Closest::Intersection(_) => return *self,
Closest::SinglePoint(l) => l,
};
let right = match *other {
Closest::Indeterminate => return *self,
Closest::Intersection(_) => return *other,
Closest::SinglePoint(r) => r,
};
if left.euclidean_distance(p) <= right.euclidean_distance(p) {
*self
} else {
*other
}
}
}