use crate::exact::kernel::{first_self_contact, on_segment, segments_properly_cross};
use crate::predicates::{Borrowed, Relatable, borrowed};
use crate::types::{Coord2d, Geometry, LineString, MultiLineString, MultiPoint, Polygon};
#[must_use]
pub fn is_simple(g: &impl Relatable) -> bool {
match borrowed(g) {
Borrowed::Point(_) => true,
Borrowed::MultiPoint(m) => multi_point_is_simple(m),
Borrowed::LineString(l) => line_is_simple(l),
Borrowed::MultiLineString(m) => multi_line_is_simple(m),
Borrowed::Polygon(p) => polygon_is_simple(p),
Borrowed::MultiPolygon(m) => m.polygons().iter().all(polygon_is_simple),
Borrowed::GeometryCollection(c) => c.leaves().all(leaf_is_simple),
}
}
fn leaf_is_simple(g: &Geometry) -> bool {
match g {
Geometry::Point(_) => true,
Geometry::MultiPoint(m) => multi_point_is_simple(m),
Geometry::LineString(l) => line_is_simple(l),
Geometry::MultiLineString(m) => multi_line_is_simple(m),
Geometry::Polygon(p) => polygon_is_simple(p),
Geometry::MultiPolygon(m) => m.polygons().iter().all(polygon_is_simple),
Geometry::GeometryCollection(c) => c.leaves().all(leaf_is_simple),
}
}
fn multi_point_is_simple(m: &MultiPoint) -> bool {
let pts: Vec<Coord2d> = m.points().iter().map(|p| p.coord()).collect();
pts.iter()
.enumerate()
.all(|(i, p)| !pts[i + 1..].contains(p))
}
fn polygon_is_simple(p: &Polygon) -> bool {
core::iter::once(p.exterior())
.chain(p.holes())
.all(|r| first_self_contact(r.coords().as_slice()).is_none())
}
fn is_closed(v: &[Coord2d]) -> bool {
v.len() > 2 && v.first() == v.last()
}
fn line_is_simple(l: &LineString) -> bool {
let v = l.coords().as_slice();
let m = v.len() - 1; let closed = is_closed(v);
for i in 0..m {
let (a, b) = (v[i], v[i + 1]);
if i + 1 < m && (on_segment(v[i + 2], a, b) || on_segment(a, b, v[i + 2])) {
return false;
}
for j in (i + 2)..m {
let (c, d) = (v[j], v[j + 1]);
if closed && i == 0 && j == m - 1 {
if on_segment(b, c, d) || on_segment(c, a, b) {
return false;
}
continue;
}
if segments_touch(a, b, c, d) {
return false;
}
}
}
true
}
fn segments_touch(a: Coord2d, b: Coord2d, c: Coord2d, d: Coord2d) -> bool {
segments_properly_cross(a, b, c, d)
|| on_segment(a, c, d)
|| on_segment(b, c, d)
|| on_segment(c, a, b)
|| on_segment(d, a, b)
}
fn multi_line_is_simple(m: &MultiLineString) -> bool {
let lines = m.line_strings();
if !lines.iter().all(line_is_simple) {
return false;
}
let boxes: Vec<(f64, f64, f64, f64)> = lines.iter().map(line_box).collect();
for i in 0..lines.len() {
for j in (i + 1)..lines.len() {
let (p, q) = (boxes[i], boxes[j]);
if p.0 <= q.2
&& q.0 <= p.2
&& p.1 <= q.3
&& q.1 <= p.3
&& !meet_only_at_boundaries(&lines[i], &lines[j])
{
return false;
}
}
}
true
}
fn meet_only_at_boundaries(x: &LineString, y: &LineString) -> bool {
let (u, w) = (x.coords().as_slice(), y.coords().as_slice());
let boundary = |v: &[Coord2d], p: Coord2d| -> bool {
!is_closed(v) && (v.first() == Some(&p) || v.last() == Some(&p))
};
for e in u.windows(2) {
let (a, b) = (e[0], e[1]);
for f in w.windows(2) {
let (c, d) = (f[0], f[1]);
if segments_properly_cross(a, b, c, d) {
return false;
}
let mut common: Vec<Coord2d> = Vec::new();
for (p, s, t) in [(a, c, d), (b, c, d), (c, a, b), (d, a, b)] {
if on_segment(p, s, t) && !common.contains(&p) {
common.push(p);
}
}
if common.len() >= 2 {
return false;
}
if let Some(&p) = common.first()
&& !(boundary(u, p) && boundary(w, p))
{
return false;
}
}
}
true
}
fn line_box(l: &LineString) -> (f64, f64, f64, f64) {
l.bbox()
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::float_cmp)]
mod tests {
use super::*;
use crate::types::{CoordSeq, GeometryCollection, LinearRing, MultiPolygon, Point};
fn line(pts: &[(f64, f64)]) -> LineString {
LineString::new(CoordSeq::new(
pts.iter().map(|&(x, y)| Coord2d::new(x, y)).collect(),
))
.unwrap()
}
fn ring(pts: &[(f64, f64)]) -> LinearRing {
LinearRing::new(CoordSeq::new(
pts.iter().map(|&(x, y)| Coord2d::new(x, y)).collect(),
))
.unwrap()
}
fn pt(x: f64, y: f64) -> Point {
Point::new(Coord2d::new(x, y)).unwrap()
}
#[test]
fn points_and_multipoints() {
assert!(is_simple(&pt(1.0, 2.0)));
assert!(is_simple(&MultiPoint::empty()));
assert!(is_simple(&MultiPoint::new(vec![
pt(0.0, 0.0),
pt(1.0, 0.0)
])));
assert!(!is_simple(&MultiPoint::new(vec![
pt(0.0, 0.0),
pt(1.0, 0.0),
pt(0.0, 0.0)
])));
assert!(!is_simple(&MultiPoint::new(vec![
pt(0.0, 1.0),
pt(-0.0, 1.0)
])));
}
#[test]
fn lines_open_and_closed() {
assert!(is_simple(&line(&[(0.0, 0.0), (1.0, 1.0), (2.0, 0.0)])));
assert!(is_simple(&line(&[
(0.0, 0.0),
(2.0, 0.0),
(2.0, 2.0),
(0.0, 2.0),
(0.0, 0.0)
])));
assert!(!is_simple(&line(&[
(0.0, 0.0),
(2.0, 2.0),
(2.0, 0.0),
(0.0, 2.0)
])));
assert!(!is_simple(&line(&[
(0.0, 0.0),
(1.0, 1.0),
(2.0, 0.0),
(2.0, 2.0),
(1.0, 1.0),
(0.0, 2.0)
])));
assert!(!is_simple(&line(&[
(0.0, 0.0),
(0.0, 4.0),
(2.0, 4.0),
(2.0, 2.0),
(0.0, 2.0)
])));
assert!(!is_simple(&line(&[(0.0, 0.0), (2.0, 0.0), (1.0, 0.0)])));
assert!(!is_simple(&line(&[
(0.0, 0.0),
(2.0, 0.0),
(1.0, 1.0),
(2.0, 2.0),
(0.0, 2.0),
(1.0, 1.0),
(0.0, 0.0)
])));
assert!(!is_simple(&line(&[
(0.0, 0.0),
(2.0, 0.0),
(2.0, 2.0),
(1.0, 0.0),
(0.0, 0.0)
])));
assert!(is_simple(&line(&[(0.0, 0.0), (1.0, 0.0), (2.0, 0.0)])));
}
#[test]
fn multilines_under_the_mod_2_rule() {
let a = line(&[(0.0, 0.0), (2.0, 0.0)]);
let b = line(&[(2.0, 0.0), (4.0, 1.0)]);
assert!(is_simple(&MultiLineString::new(vec![a.clone(), b.clone()])));
let c = line(&[(2.0, 0.0), (2.0, -3.0)]);
assert!(is_simple(&MultiLineString::new(vec![
a.clone(),
b.clone(),
c
])));
let t = line(&[(1.0, 0.0), (1.0, 2.0)]);
assert!(!is_simple(&MultiLineString::new(vec![a.clone(), t])));
let x = line(&[(1.0, -1.0), (1.0, 1.0)]);
assert!(!is_simple(&MultiLineString::new(vec![a.clone(), x])));
let o = line(&[(1.0, 0.0), (3.0, 0.0)]);
assert!(!is_simple(&MultiLineString::new(vec![a.clone(), o])));
let closed = line(&[(2.0, 0.0), (3.0, 1.0), (3.0, -1.0), (2.0, 0.0)]);
assert!(!is_simple(&MultiLineString::new(vec![a.clone(), closed])));
let bow = line(&[(10.0, 0.0), (12.0, 2.0), (12.0, 0.0), (10.0, 2.0)]);
assert!(!is_simple(&MultiLineString::new(vec![a, bow])));
assert!(is_simple(&MultiLineString::empty()));
}
#[test]
fn polygons_by_their_rings() {
let square =
Polygon::new_simple(ring(&[(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (0.0, 4.0)])).unwrap();
assert!(is_simple(&square));
let pinch = Polygon::new_simple(ring(&[
(0.0, 0.0),
(4.0, 0.0),
(2.0, 2.0),
(4.0, 4.0),
(0.0, 4.0),
(2.0, 2.0),
]))
.unwrap();
assert!(!is_simple(&pinch));
let touching = Polygon::new(
ring(&[(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (0.0, 4.0)]),
vec![ring(&[(0.0, 2.0), (2.0, 3.0), (2.0, 1.0)])],
)
.unwrap();
assert!(is_simple(&touching));
assert!(!is_simple(&MultiPolygon::new(vec![pinch.clone()]).unwrap()));
assert!(is_simple(&MultiPolygon::new(vec![square.clone()]).unwrap()));
let gc = GeometryCollection::new(vec![
Geometry::Polygon(square),
Geometry::LineString(line(&[(0.0, 0.0), (9.0, 9.0)])),
Geometry::Point(pt(1.0, 1.0)),
])
.unwrap();
assert!(is_simple(&gc));
let nested = GeometryCollection::new(vec![
Geometry::GeometryCollection(gc),
Geometry::Polygon(pinch),
])
.unwrap();
assert!(!is_simple(&nested));
assert!(is_simple(&GeometryCollection::empty()));
}
}