use num_traits::NumCast;
use crate::shape::Point;
use crate::base::Vec2;
use crate::traits::{BoundingBox, Cast, DType, MapPoint, TryBoundingBox};
pub use crate::base::Angle;
pub use crate::base::{Contains, Side};
use super::Rect;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Edge<T> {
pub start: Point<T>,
pub end: Point<T>,
}
impl<T: Copy> Edge<T> {
pub fn new(start: impl Into<Point<T>>, end: impl Into<Point<T>>) -> Self {
Self {
start: start.into(),
end: end.into(),
}
}
pub fn reversed(&self) -> Self {
Self {
start: self.end,
end: self.start,
}
}
}
impl<T: PartialEq> Edge<T> {
pub fn is_degenerate(&self) -> bool {
self.start == self.end
}
pub fn is_rectilinear(&self) -> bool {
!self.is_degenerate() && (self.start.x == self.end.x || self.start.y == self.end.y)
}
pub fn is_horizontal(&self) -> bool {
!self.is_degenerate() && self.start.y == self.end.y
}
pub fn is_vertical(&self) -> bool {
!self.is_degenerate() && self.start.x == self.end.x
}
}
impl<T: DType> Edge<T> {
pub fn vec2(&self) -> Vec2<T> {
self.end - self.start
}
pub fn side_of(&self, point: Point<T>) -> Side {
assert!(!self.is_degenerate());
let a = self.vec2();
let b = point - self.start;
if a.x.is_zero() {
let side = if point.x < self.start.x {
Side::Left
} else if point.x > self.start.x {
Side::Right
} else {
Side::Center
};
let side = if self.start.y < self.end.y {
side
} else {
side.other()
};
return side;
} else if a.y.is_zero() {
let side = if point.y < self.start.y {
Side::Right
} else if point.y > self.start.y {
Side::Left
} else {
Side::Center
};
let side = if self.start.x < self.end.x {
side
} else {
side.other()
};
return side;
}
let area = b.cross_prod(a);
if area.is_zero() {
Side::Center
} else if area < T::zero() {
Side::Left
} else {
Side::Right
}
}
pub fn contains_point(&self, point: Point<T>) -> Contains {
if self.start == point || self.end == point {
Contains::OnBounds
} else if self.is_degenerate() {
Contains::No
} else {
let a = self.start - point;
let b = self.end - point;
if a.cross_prod(b).is_zero() && a.dot(b) <= T::zero() {
Contains::WithinBounds
} else {
Contains::No
}
}
}
pub fn line_contains_point(&self, point: Point<T>) -> bool {
if self.is_degenerate() {
self.start == point
} else {
let l = self.vec2();
let b = point - self.start;
l.cross_prod(b).is_zero()
}
}
pub fn is_parallel(&self, other: &Self) -> bool {
if self.is_degenerate() || other.is_degenerate() {
false
} else {
let a = self.vec2();
let b = other.vec2();
if a.x.is_zero() {
b.x.is_zero()
} else if a.y.is_zero() {
b.y.is_zero()
} else if b.x.is_zero() || b.y.is_zero() {
false
} else {
a.cross_prod(b).is_zero()
}
}
}
pub fn is_collinear(&self, other: &Self) -> bool {
if self.is_degenerate() || other.is_degenerate() {
false
} else {
let v = self.vec2();
let a = other.start - self.start;
let b = other.end - self.start;
if self.start.x == self.end.x {
other.start.x == self.start.x && other.end.x == self.start.x
} else if self.start.y == self.end.y {
other.start.y == self.start.y && other.end.y == self.start.y
} else if other.start.x == other.end.x {
self.start.x == other.start.x && self.end.x == other.start.x
} else if other.start.y == other.end.y {
self.start.y == other.start.y && self.end.y == other.start.y
} else {
v.cross_prod(a).is_zero() && v.cross_prod(b).is_zero()
}
}
}
pub fn is_coincident(&self, other: &Self) -> bool {
let self_start_in_other = other.contains_point(self.start).inclusive_bounds();
let self_end_in_other = other.contains_point(self.end).inclusive_bounds();
let other_start_in_self = self.contains_point(other.start).inclusive_bounds();
let other_end_in_self = self.contains_point(other.end).inclusive_bounds();
let share_more_than_one_point = self.end != other.start
&& self.start != other.end
&& (self_start_in_other || other_start_in_self)
&& (other_end_in_self || self_end_in_other);
debug_assert!(if share_more_than_one_point {
self.is_parallel(other)
} else {
true
});
let oriented_the_same_way = self.vec2().dot(other.vec2()) > T::zero();
share_more_than_one_point && oriented_the_same_way
}
pub fn is_parallel_approx(&self, other: &Self, epsilon_squared: T) -> bool {
if self.is_degenerate() || other.is_degenerate() {
false
} else {
let d1 = self.vec2();
let d2 = other.vec2();
let len1_sqr = d1.norm2_squared();
let len2_sqr = d2.norm2_squared();
let cross = d1.cross_prod(d2);
let cross_sqr = cross * cross;
cross_sqr <= len1_sqr * len2_sqr * epsilon_squared
}
}
pub fn is_collinear_approx(&self, other: &Self, epsilon_squared: T) -> bool {
if self.is_degenerate() || other.is_degenerate() {
false
} else {
let d1 = self.vec2();
let d2 = other.vec2();
let len1_sqr = d1.norm2_squared();
let len2_sqr = d2.norm2_squared();
let cross = d1.cross_prod(d2);
let cross_sqr = cross * cross;
let approx_parallel = cross_sqr <= len1_sqr * len2_sqr * epsilon_squared;
if approx_parallel {
let e = other.start - self.start;
let len_e_sqrt = e.norm2_squared();
let cross = e.cross_prod(d1);
let cross_sqr = cross * cross;
cross_sqr <= len1_sqr * len_e_sqrt * epsilon_squared
} else {
false
}
}
}
pub fn lines_intersect_approx(&self, other: &Self, epsilon_squared: T) -> bool {
!self.is_parallel_approx(other, epsilon_squared)
|| self.is_collinear_approx(other, epsilon_squared)
}
pub fn crossed_by_line(&self, other: &Self) -> Contains {
let side1 = other.side_of(self.start);
if side1 == Side::Center {
Contains::OnBounds
} else {
let side2 = other.side_of(self.end);
if side2 == Side::Center {
Contains::OnBounds
} else if side1 == side2 {
Contains::No
} else {
Contains::WithinBounds
}
}
}
pub fn lines_intersect(&self, other: &Self) -> bool {
!self.is_parallel(other) || self.is_collinear(other)
}
}
impl<T: Copy> From<Edge<T>> for (Point<T>, Point<T>) {
fn from(e: Edge<T>) -> Self {
(e.start, e.end)
}
}
impl<T: Copy> From<&Edge<T>> for (Point<T>, Point<T>) {
fn from(e: &Edge<T>) -> Self {
(e.start, e.end)
}
}
impl<T: DType> From<(Point<T>, Point<T>)> for Edge<T> {
fn from(points: (Point<T>, Point<T>)) -> Self {
Edge::new(points.0, points.1)
}
}
impl<T: DType> From<[Point<T>; 2]> for Edge<T> {
fn from(points: [Point<T>; 2]) -> Self {
Edge::new(points[0], points[1])
}
}
impl<T: Copy> MapPoint<T> for Edge<T> {
fn transform<F>(&self, transformate: F) -> Self
where
F: Fn(Point<T>) -> Point<T>
{
Self {
start: transformate(self.start),
end: transformate(self.end)
}
}
}
impl<T: Copy + PartialOrd> BoundingBox<T> for Edge<T> {
fn bounding_box(&self) -> Rect<T> {
Rect::new(self.start, self.end)
}
}
impl<T: Copy + PartialOrd> TryBoundingBox<T> for Edge<T> {
fn try_bounding_box(&self) -> Option<Rect<T>> {
Some(self.bounding_box())
}
}
impl<Src: Copy + NumCast, Dst: Copy + NumCast> Cast<Src, Dst> for Edge<Src> {
type Output = Edge<Dst>;
fn try_cast(&self) -> Option<Self::Output> {
match (self.start.try_cast(), self.end.try_cast()) {
(Some(s), Some(e)) => Some(Edge::new(s, e)),
_ => None,
}
}
}