#![allow(clippy::unwrap_used)]
use super::{ValidityIssue, validate, validate_line};
use crate::types::{Coord2d, CoordSeq, LinearRing, Polygon};
fn seq(pts: &[(f64, f64)]) -> CoordSeq {
CoordSeq::new(pts.iter().map(|&(x, y)| Coord2d::new(x, y)).collect())
}
fn ccw() -> CoordSeq {
seq(&[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)])
}
#[test]
fn a_valid_polygon_has_no_issues_and_its_constructor_agrees() {
let hole = seq(&[(2.0, 2.0), (2.0, 4.0), (4.0, 4.0), (4.0, 2.0)]);
let rings = [ccw(), hole.clone()];
assert!(validate(&rings).is_empty());
assert!(
Polygon::new(
LinearRing::new(ccw()).unwrap(),
vec![LinearRing::new(hole).unwrap()]
)
.is_ok()
);
assert!(validate(&[]).is_empty());
}
#[test]
fn the_closing_vertex_is_accepted_as_the_constructor_accepts_it() {
let closed = seq(&[
(0.0, 0.0),
(10.0, 0.0),
(10.0, 10.0),
(0.0, 10.0),
(0.0, 0.0),
]);
assert!(validate(core::slice::from_ref(&closed)).is_empty());
assert!(LinearRing::new(closed).is_ok());
let twice = seq(&[
(0.0, 0.0),
(10.0, 0.0),
(10.0, 10.0),
(0.0, 10.0),
(0.0, 0.0),
(0.0, 0.0),
]);
assert_eq!(
validate(core::slice::from_ref(&twice)),
vec![ValidityIssue::DuplicatePoint { ring: 0, index: 4 }]
);
assert!(matches!(
LinearRing::new(twice),
Err(crate::GeometryError::ConsecutiveDuplicateVertices { index: 4 })
));
}
#[test]
fn a_clockwise_exterior_and_a_counterclockwise_hole_are_orientation_issues() {
let cw = seq(&[(0.0, 0.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0)]);
assert_eq!(
validate(core::slice::from_ref(&cw)),
vec![ValidityIssue::RingOrientation { ring: 0 }]
);
let ccw_hole = seq(&[(2.0, 2.0), (4.0, 2.0), (4.0, 4.0), (2.0, 4.0)]);
assert_eq!(
validate(&[ccw(), ccw_hole]),
vec![ValidityIssue::RingOrientation { ring: 1 }]
);
assert_eq!(
validate(&[cw, seq(&[(2.0, 2.0), (4.0, 2.0), (4.0, 4.0), (2.0, 4.0)])]),
vec![
ValidityIssue::RingOrientation { ring: 0 },
ValidityIssue::RingOrientation { ring: 1 },
]
);
}
#[test]
fn a_bow_tie_reports_the_crossing_edges() {
let bow = seq(&[(0.0, 0.0), (10.0, 10.0), (10.0, 0.0), (0.0, 10.0)]);
let issues = validate(&[bow]);
assert_eq!(
issues,
vec![
ValidityIssue::DegenerateRing { ring: 0 },
ValidityIssue::SelfIntersection {
ring: 0,
edges: (0, 2)
},
]
);
}
#[test]
fn an_unbalanced_bow_tie_is_a_crossing_with_an_orientation_verdict() {
let bow = seq(&[(0.0, 0.0), (10.0, 10.0), (10.0, 0.0), (0.0, 4.0)]);
let issues = validate(&[bow]);
assert!(
issues.contains(&ValidityIssue::SelfIntersection {
ring: 0,
edges: (0, 2)
}),
"{issues:?}"
);
assert_eq!(issues.len(), 2, "{issues:?}");
}
#[test]
fn every_self_contact_is_reported_not_only_the_first() {
let comb = seq(&[
(0.0, 5.0),
(30.0, 5.0),
(30.0, 0.0),
(20.0, 10.0),
(10.0, 0.0),
(5.0, 10.0),
]);
let crossings: Vec<(usize, usize)> = validate(&[comb])
.into_iter()
.filter_map(|i| match i {
ValidityIssue::SelfIntersection { edges, .. } => Some(edges),
_ => None,
})
.collect();
assert_eq!(crossings, vec![(0, 2), (0, 3), (0, 4)]);
}
#[test]
fn a_pinch_and_a_t_touch_are_self_intersections_too() {
let pinch = seq(&[
(0.0, 0.0),
(10.0, 0.0),
(5.0, 5.0),
(10.0, 10.0),
(0.0, 10.0),
(5.0, 5.0),
]);
let issues = validate(&[pinch]);
assert!(
issues
.iter()
.any(|i| matches!(i, ValidityIssue::SelfIntersection { .. })),
"{issues:?}"
);
let t = seq(&[
(0.0, 0.0),
(10.0, 0.0),
(10.0, 10.0),
(5.0, 0.0),
(0.0, 10.0),
]);
let issues = validate(&[t]);
assert!(
issues
.iter()
.any(|i| matches!(i, ValidityIssue::SelfIntersection { .. })),
"{issues:?}"
);
}
#[test]
fn too_few_points_counts_distinct_vertices() {
assert_eq!(
validate(&[seq(&[(0.0, 0.0), (1.0, 1.0)])]),
vec![ValidityIssue::TooFewPoints {
ring: 0,
found: 2,
required: 3
}]
);
assert_eq!(
validate(&[seq(&[(0.0, 0.0)])]),
vec![ValidityIssue::TooFewPoints {
ring: 0,
found: 1,
required: 3
}]
);
assert_eq!(
validate(&[seq(&[])]),
vec![ValidityIssue::TooFewPoints {
ring: 0,
found: 0,
required: 3
}]
);
assert_eq!(
validate(&[seq(&[(0.0, 0.0), (0.0, 0.0), (1.0, 1.0)])]),
vec![
ValidityIssue::DuplicatePoint { ring: 0, index: 0 },
ValidityIssue::TooFewPoints {
ring: 0,
found: 2,
required: 3
},
]
);
}
#[test]
fn duplicates_are_reported_and_the_rest_is_judged_on_the_distinct_ring() {
let d = seq(&[
(0.0, 0.0),
(0.0, 0.0),
(10.0, 0.0),
(10.0, 10.0),
(0.0, 0.0),
]);
assert_eq!(
validate(&[d]),
vec![ValidityIssue::DuplicatePoint { ring: 0, index: 0 }]
);
let d = seq(&[
(0.0, 0.0),
(10.0, 10.0),
(10.0, 10.0),
(10.0, 0.0),
(0.0, 10.0),
]);
assert_eq!(
validate(&[d]),
vec![
ValidityIssue::DuplicatePoint { ring: 0, index: 1 },
ValidityIssue::DegenerateRing { ring: 0 },
ValidityIssue::SelfIntersection {
ring: 0,
edges: (0, 3)
},
]
);
}
#[test]
fn a_collinear_ring_is_degenerate_and_gets_no_orientation_verdict() {
let flat = seq(&[(0.0, 0.0), (5.0, 0.0), (10.0, 0.0)]);
assert_eq!(
validate(&[flat]),
vec![
ValidityIssue::DegenerateRing { ring: 0 },
ValidityIssue::SelfIntersection {
ring: 0,
edges: (1, 2)
},
ValidityIssue::SelfIntersection {
ring: 0,
edges: (2, 0)
},
]
);
let sliver = seq(&[(0.0, 0.0), (10.0, 1e-16), (20.0, 0.0)]);
assert_eq!(
validate(&[sliver]),
vec![ValidityIssue::RingOrientation { ring: 0 }]
);
}
#[test]
fn a_non_finite_coordinate_is_reported_per_vertex_and_nothing_else_is() {
let bad = seq(&[
(0.0, 0.0),
(f64::NAN, 0.0),
(10.0, f64::INFINITY),
(0.0, 10.0),
]);
assert_eq!(
validate(&[bad]),
vec![
ValidityIssue::NonFiniteCoordinate { ring: 0, index: 1 },
ValidityIssue::NonFiniteCoordinate { ring: 0, index: 2 },
]
);
}
#[test]
fn issues_name_their_ring_and_come_in_ring_order() {
let rings = [
seq(&[(0.0, 0.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0)]), seq(&[(2.0, 2.0), (2.0, 4.0), (4.0, 4.0), (4.0, 2.0)]), seq(&[(6.0, 6.0), (7.0, 7.0)]), ];
let issues = validate(&rings);
assert_eq!(
issues.iter().map(ValidityIssue::ring).collect::<Vec<_>>(),
vec![0, 2]
);
assert_eq!(
issues[1],
ValidityIssue::TooFewPoints {
ring: 2,
found: 2,
required: 3
}
);
}
#[test]
fn lines_need_two_distinct_vertices_and_no_repeats_and_may_cross() {
assert!(validate_line(&seq(&[(0.0, 0.0), (5.0, 5.0)])).is_empty());
assert_eq!(
validate_line(&seq(&[(0.0, 0.0)])),
vec![ValidityIssue::TooFewPoints {
ring: 0,
found: 1,
required: 2
}]
);
assert_eq!(
validate_line(&seq(&[(0.0, 0.0), (0.0, 0.0)])),
vec![
ValidityIssue::DuplicatePoint { ring: 0, index: 0 },
ValidityIssue::TooFewPoints {
ring: 0,
found: 1,
required: 2
},
]
);
assert!(validate_line(&seq(&[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)])).is_empty());
assert!(validate_line(&seq(&[(0.0, 0.0), (10.0, 10.0), (10.0, 0.0), (0.0, 10.0)])).is_empty());
assert_eq!(
validate_line(&seq(&[(0.0, f64::NAN), (1.0, 0.0)])),
vec![ValidityIssue::NonFiniteCoordinate { ring: 0, index: 0 }]
);
}