use super::{ChunkIterator, Intersection, Line2, PriorityDistance, SimpleAffine, WindowIterator};
use crate::format_float;
use std::{cmp::Ordering, fmt, fmt::Debug};
use vector_traits::{approx::*, prelude::*};
impl<T: GenericVector2> PartialEq for Line2<T> {
fn eq(&self, other: &Self) -> bool {
ulps_eq!(self.start.x(), other.start.x())
&& ulps_eq!(self.start.y(), other.start.y())
&& ulps_eq!(self.end.x(), other.end.x())
&& ulps_eq!(self.end.y(), other.end.y())
}
}
impl<T: GenericVector2> PartialOrd for PriorityDistance<T> {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: GenericVector2> Ord for PriorityDistance<T> {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.key.partial_cmp(&other.key).unwrap().reverse()
}
}
impl<T: GenericVector2> PartialEq for PriorityDistance<T> {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
ulps_eq!(self.key, other.key)
}
}
impl<T: GenericVector2> Eq for PriorityDistance<T> {}
impl<T: GenericVector2> From<[T::Scalar; 4]> for Line2<T> {
fn from(array: [T::Scalar; 4]) -> Self {
Line2 {
start: T::new_2d(array[0], array[1]),
end: T::new_2d(array[2], array[3]),
}
}
}
impl<T: GenericVector2> From<[[T::Scalar; 2]; 2]> for Line2<T> {
fn from(array: [[T::Scalar; 2]; 2]) -> Self {
Line2 {
start: T::new_2d(array[0][0], array[0][1]),
end: T::new_2d(array[1][0], array[1][1]),
}
}
}
impl<T: GenericVector2> Debug for Intersection<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Overlap(a) => Debug::fmt(&a, f),
Self::Intersection(a) => Debug::fmt(&a, f),
}
}
}
impl<T: GenericVector2> Default for SimpleAffine<T> {
#[inline]
fn default() -> Self {
Self {
a_offset: [T::Scalar::ZERO, T::Scalar::ZERO],
scale: [T::Scalar::ONE, T::Scalar::ONE],
b_offset: [T::Scalar::ZERO, T::Scalar::ZERO],
}
}
}
impl<T: GenericVector2> Iterator for WindowIterator<'_, T> {
type Item = Line2<T>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|window| Line2 {
start: window[0],
end: window[1],
})
}
}
impl<T: GenericVector2> Iterator for ChunkIterator<'_, T> {
type Item = Line2<T>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|window| Line2 {
start: window[0],
end: window[1],
})
}
}
impl<T: GenericVector2> ExactSizeIterator for WindowIterator<'_, T> {
fn len(&self) -> usize {
self.len()
}
}
impl<T: GenericVector2> Debug for Line2<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"({},{})-({},{})",
format_float(self.start.x()),
format_float(self.start.y()),
format_float(self.end.x()),
format_float(self.end.y()),
)
}
}
impl<T: GenericVector2> AbsDiffEq for Line2<T> {
type Epsilon = T::Scalar;
fn default_epsilon() -> Self::Epsilon {
T::Scalar::default_epsilon()
}
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
if T::Scalar::abs_diff_eq(&self.start.x(), &other.start.x(), epsilon)
&& T::Scalar::abs_diff_eq(&self.start.y(), &other.start.y(), epsilon)
{
T::Scalar::abs_diff_eq(&self.end.x(), &other.end.x(), epsilon)
&& T::Scalar::abs_diff_eq(&self.end.y(), &other.end.y(), epsilon)
} else {
T::Scalar::abs_diff_eq(&self.start.x(), &other.end.x(), epsilon)
&& T::Scalar::abs_diff_eq(&self.start.y(), &other.end.y(), epsilon)
&& T::Scalar::abs_diff_eq(&self.end.x(), &other.start.x(), epsilon)
&& T::Scalar::abs_diff_eq(&self.end.y(), &other.start.y(), epsilon)
}
}
}
impl<T: GenericVector2> UlpsEq for Line2<T> {
fn default_max_ulps() -> u32 {
T::Scalar::default_max_ulps()
}
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
if T::Scalar::ulps_eq(&self.start.x(), &other.start.x(), epsilon, max_ulps)
&& T::Scalar::ulps_eq(&self.start.y(), &other.start.y(), epsilon, max_ulps)
{
T::Scalar::ulps_eq(&self.end.x(), &other.end.x(), epsilon, max_ulps)
&& T::Scalar::ulps_eq(&self.end.y(), &other.end.y(), epsilon, max_ulps)
} else {
T::Scalar::ulps_eq(&self.start.x(), &other.end.x(), epsilon, max_ulps)
&& T::Scalar::ulps_eq(&self.start.y(), &other.end.y(), epsilon, max_ulps)
&& T::Scalar::ulps_eq(&self.end.x(), &other.start.x(), epsilon, max_ulps)
&& T::Scalar::ulps_eq(&self.end.y(), &other.start.y(), epsilon, max_ulps)
}
}
}