#![allow(clippy::unwrap_used, clippy::float_cmp)]
use super::{segment_distance, simplify_chain, simplify_ring};
use crate::types::Coord2d;
fn c(x: f64, y: f64) -> Coord2d {
Coord2d::new(x, y)
}
#[test]
fn segment_distance_is_perpendicular_inside_and_endpoint_outside() {
let (a, b) = (c(0.0, 0.0), c(10.0, 0.0));
assert_eq!(segment_distance(c(5.0, 3.0), a, b), Some(3.0));
assert_eq!(segment_distance(c(5.0, 0.0), a, b), Some(0.0));
assert_eq!(segment_distance(c(-3.0, 4.0), a, b), Some(5.0));
assert_eq!(segment_distance(c(13.0, -4.0), a, b), Some(5.0));
assert_eq!(segment_distance(c(3.0, 4.0), a, a), Some(5.0));
assert_eq!(
segment_distance(c(0.0, 1e200), c(-1e200, 0.0), c(1e200, 0.0)),
None
);
}
#[test]
fn collinear_interior_vertices_go_at_any_positive_tolerance() {
let pts = [c(0.0, 0.0), c(1.0, 0.0), c(2.0, 0.0), c(3.0, 0.0)];
assert_eq!(simplify_chain(&pts, 1e-300), vec![c(0.0, 0.0), c(3.0, 0.0)]);
}
#[test]
fn the_farthest_vertex_is_kept_and_the_chain_split_there() {
let pts = [
c(0.0, 0.0),
c(1.0, 0.1),
c(2.0, 2.0),
c(3.0, 0.1),
c(4.0, 0.0),
];
assert_eq!(
simplify_chain(&pts, 0.7),
vec![c(0.0, 0.0), c(2.0, 2.0), c(4.0, 0.0)]
);
assert_eq!(simplify_chain(&pts, 0.6), pts.to_vec());
assert_eq!(simplify_chain(&pts, 10.0), vec![c(0.0, 0.0), c(4.0, 0.0)]);
}
#[test]
fn a_vertex_exactly_at_the_tolerance_is_dropped() {
let pts = [c(0.0, 0.0), c(1.0, 1.0), c(2.0, 0.0)];
assert_eq!(simplify_chain(&pts, 1.0), vec![c(0.0, 0.0), c(2.0, 0.0)]);
assert_eq!(simplify_chain(&pts, 0.999_999_999), pts.to_vec());
}
#[test]
fn ties_go_to_the_first_vertex_attaining_the_maximum() {
let pts = [c(0.0, 0.0), c(1.0, 1.0), c(2.0, 1.0), c(3.0, 0.0)];
assert_eq!(
simplify_chain(&pts, 0.4),
vec![c(0.0, 0.0), c(1.0, 1.0), c(2.0, 1.0), c(3.0, 0.0)]
);
assert_eq!(
simplify_chain(&pts, 0.5),
vec![c(0.0, 0.0), c(1.0, 1.0), c(3.0, 0.0)]
);
let pts = [
c(0.0, 0.0),
c(1.0, 1.0),
c(2.0, 1.0),
c(3.0, 1.0),
c(4.0, 0.0),
];
let got = simplify_chain(&pts, 0.5);
assert_eq!(
got,
vec![c(0.0, 0.0), c(1.0, 1.0), c(3.0, 1.0), c(4.0, 0.0)]
);
}
#[test]
fn a_ring_anchors_at_vertex_zero_and_its_antipode() {
let sq = [
c(0.0, 0.0),
c(1.0, 0.0),
c(2.0, 0.0),
c(2.0, 1.0),
c(2.0, 2.0),
c(1.0, 2.0),
c(0.0, 2.0),
c(0.0, 1.0),
];
let corners = vec![c(0.0, 0.0), c(2.0, 0.0), c(2.0, 2.0), c(0.0, 2.0)];
assert_eq!(simplify_ring(&sq, 0.1), corners);
let rotated: Vec<Coord2d> = sq[1..].iter().chain(&sq[..1]).copied().collect();
let got = simplify_ring(&rotated, 0.1);
assert_eq!(got.len(), 5);
assert_eq!(got[0], c(1.0, 0.0));
for corner in &corners {
assert!(got.contains(corner));
}
}
#[test]
fn the_closing_edge_is_simplified_like_any_other() {
let ring = [
c(1.0, 0.0),
c(2.0, 0.0),
c(2.0, 2.0),
c(0.0, 2.0),
c(0.0, 0.0),
c(0.5, 0.0),
];
let got = simplify_ring(&ring, 0.1);
assert!(!got.contains(&c(0.5, 0.0)));
assert!(got.contains(&c(0.0, 0.0)) && got.contains(&c(2.0, 0.0)));
}
#[test]
fn a_ring_can_collapse_to_two_vertices() {
let tri = [c(0.0, 0.0), c(1.0, 0.1), c(2.0, 0.0)];
assert_eq!(simplify_ring(&tri, 1.0), vec![c(0.0, 0.0), c(2.0, 0.0)]);
}
#[test]
fn a_long_monotone_chain_does_not_overflow_the_stack() {
let pts: Vec<Coord2d> = (0..200_000).map(f64::from).map(|t| c(t, t * t)).collect();
let got = simplify_chain(&pts, 1e-9);
assert_eq!(got.len(), pts.len());
}