#[cfg(any(feature = "approx", test))]
use approx::{AbsDiffEq, RelativeEq};
use crate::{CoordNum, Coordinate, Line, Point, Triangle};
use std::iter::FromIterator;
use std::ops::{Index, IndexMut};
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LineString<T>(pub Vec<Coordinate<T>>)
where
T: CoordNum;
#[derive(Debug)]
pub struct PointsIter<'a, T: CoordNum + 'a>(::std::slice::Iter<'a, Coordinate<T>>);
impl<'a, T: CoordNum> Iterator for PointsIter<'a, T> {
type Item = Point<T>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|c| Point(*c))
}
}
impl<'a, T: CoordNum> DoubleEndedIterator for PointsIter<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back().map(|c| Point(*c))
}
}
impl<T: CoordNum> LineString<T> {
pub fn points_iter(&self) -> PointsIter<T> {
PointsIter(self.0.iter())
}
pub fn into_points(self) -> Vec<Point<T>> {
self.0.into_iter().map(Point).collect()
}
pub fn lines<'a>(&'a self) -> impl ExactSizeIterator + Iterator<Item = Line<T>> + 'a {
self.0.windows(2).map(|w| {
unsafe { Line::new(*w.get_unchecked(0), *w.get_unchecked(1)) }
})
}
pub fn triangles<'a>(&'a self) -> impl ExactSizeIterator + Iterator<Item = Triangle<T>> + 'a {
self.0.windows(3).map(|w| {
unsafe {
Triangle(
*w.get_unchecked(0),
*w.get_unchecked(1),
*w.get_unchecked(2),
)
}
})
}
pub fn close(&mut self) {
if !self.is_closed() {
debug_assert!(!self.0.is_empty());
self.0.push(self.0[0]);
}
}
#[deprecated(note = "Use geo::algorithm::coords_iter::CoordsIter::coords_count instead")]
pub fn num_coords(&self) -> usize {
self.0.len()
}
pub fn is_closed(&self) -> bool {
self.0.first() == self.0.last()
}
}
impl<T: CoordNum, IC: Into<Coordinate<T>>> From<Vec<IC>> for LineString<T> {
fn from(v: Vec<IC>) -> Self {
LineString(v.into_iter().map(|c| c.into()).collect())
}
}
impl<T: CoordNum, IC: Into<Coordinate<T>>> FromIterator<IC> for LineString<T> {
fn from_iter<I: IntoIterator<Item = IC>>(iter: I) -> Self {
LineString(iter.into_iter().map(|c| c.into()).collect())
}
}
impl<T: CoordNum> IntoIterator for LineString<T> {
type Item = Coordinate<T>;
type IntoIter = ::std::vec::IntoIter<Coordinate<T>>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a, T: CoordNum> IntoIterator for &'a mut LineString<T> {
type Item = &'a mut Coordinate<T>;
type IntoIter = ::std::slice::IterMut<'a, Coordinate<T>>;
fn into_iter(self) -> ::std::slice::IterMut<'a, Coordinate<T>> {
self.0.iter_mut()
}
}
impl<T: CoordNum> Index<usize> for LineString<T> {
type Output = Coordinate<T>;
fn index(&self, index: usize) -> &Coordinate<T> {
self.0.index(index)
}
}
impl<T: CoordNum> IndexMut<usize> for LineString<T> {
fn index_mut(&mut self, index: usize) -> &mut Coordinate<T> {
self.0.index_mut(index)
}
}
#[cfg(any(feature = "approx", test))]
impl<T> RelativeEq for LineString<T>
where
T: AbsDiffEq<Epsilon = T> + CoordNum + RelativeEq,
{
#[inline]
fn default_max_relative() -> Self::Epsilon {
T::default_max_relative()
}
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
if self.0.len() != other.0.len() {
return false;
}
let points_zipper = self.points_iter().zip(other.points_iter());
for (lhs, rhs) in points_zipper {
if lhs.relative_ne(&rhs, epsilon, max_relative) {
return false;
}
}
true
}
}
#[cfg(any(feature = "approx", test))]
impl<T: AbsDiffEq<Epsilon = T> + CoordNum> AbsDiffEq for LineString<T> {
type Epsilon = T;
#[inline]
fn default_epsilon() -> Self::Epsilon {
T::default_epsilon()
}
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
if self.0.len() != other.0.len() {
return false;
}
let mut points_zipper = self.points_iter().zip(other.points_iter());
points_zipper.all(|(lhs, rhs)| lhs.abs_diff_eq(&rhs, epsilon))
}
}
#[cfg(feature = "rstar")]
impl<T> ::rstar::RTreeObject for LineString<T>
where
T: ::num_traits::Float + ::rstar::RTreeNum,
{
type Envelope = ::rstar::AABB<Point<T>>;
fn envelope(&self) -> Self::Envelope {
use num_traits::Bounded;
let bounding_rect = crate::private_utils::line_string_bounding_rect(self);
match bounding_rect {
None => ::rstar::AABB::from_corners(
Point::new(Bounded::min_value(), Bounded::min_value()),
Point::new(Bounded::max_value(), Bounded::max_value()),
),
Some(b) => ::rstar::AABB::from_corners(
Point::new(b.min().x, b.min().y),
Point::new(b.max().x, b.max().y),
),
}
}
}
#[cfg(feature = "rstar")]
impl<T> ::rstar::PointDistance for LineString<T>
where
T: ::num_traits::Float + ::rstar::RTreeNum,
{
fn distance_2(&self, point: &Point<T>) -> T {
let d = crate::private_utils::point_line_string_euclidean_distance(*point, self);
if d == T::zero() {
d
} else {
d.powi(2)
}
}
}
#[cfg(test)]
mod test {
use super::*;
use approx::AbsDiffEq;
#[test]
fn test_abs_diff_eq() {
let delta = 1e-6;
let coords = vec![(0., 0.), (5., 0.), (10., 10.)];
let ls: LineString<f32> = coords.into_iter().collect();
let coords_x = vec![(0., 0.), (5. + delta, 0.), (10., 10.)];
let ls_x: LineString<f32> = coords_x.into_iter().collect();
assert!(ls.abs_diff_eq(&ls_x, 1e-2));
assert!(ls.abs_diff_ne(&ls_x, 1e-12));
let coords_y = vec![(0., 0.), (5., 0. + delta), (10., 10.)];
let ls_y: LineString<f32> = coords_y.into_iter().collect();
assert!(ls.abs_diff_eq(&ls_y, 1e-2));
assert!(ls.abs_diff_ne(&ls_y, 1e-12));
let coords_x = vec![(0., 0.), (5., 0.)];
let ls_under: LineString<f32> = coords_x.into_iter().collect();
assert!(ls.abs_diff_ne(&ls_under, 1.));
let coords_x = vec![(0., 0.), (5., 0.), (10., 10.), (10., 100.)];
let ls_oversized: LineString<f32> = coords_x.into_iter().collect();
assert!(ls.abs_diff_ne(&ls_oversized, 1.));
}
#[test]
fn test_relative_eq() {
let delta = 1e-6;
let coords = vec![(0., 0.), (5., 0.), (10., 10.)];
let ls: LineString<f32> = coords.into_iter().collect();
let coords_x = vec![(0., 0.), (5. + delta, 0.), (10., 10.)];
let ls_x: LineString<f32> = coords_x.into_iter().collect();
assert!(ls.relative_eq(&ls_x, 1e-2, 1e-2));
assert!(ls.relative_ne(&ls_x, 1e-12, 1e-12));
let coords_y = vec![(0., 0.), (5., 0. + delta), (10., 10.)];
let ls_y: LineString<f32> = coords_y.into_iter().collect();
assert!(ls.relative_eq(&ls_y, 1e-2, 1e-2));
assert!(ls.relative_ne(&ls_y, 1e-12, 1e-12));
let coords_x = vec![(0., 0.), (5., 0.)];
let ls_under: LineString<f32> = coords_x.into_iter().collect();
assert!(ls.relative_ne(&ls_under, 1., 1.));
let coords_x = vec![(0., 0.), (5., 0.), (10., 10.), (10., 100.)];
let ls_oversized: LineString<f32> = coords_x.into_iter().collect();
assert!(ls.relative_ne(&ls_oversized, 1., 1.));
}
}