use crate::exact::kernel::{
RingPlacement, contact_witness, place_ring, ring_touch_points, same_point_set,
};
use crate::types::{Coord2d, LinearRing};
use super::ValidityIssue;
pub(super) struct Prepared {
pub(super) ring: LinearRing,
pub(super) origin: Vec<usize>,
}
impl Prepared {
fn edges(&self, other: &Prepared, (e, f): (usize, usize)) -> (usize, usize) {
(self.origin[e], other.origin[f])
}
}
pub(super) fn placement_into(rings: &[Option<Prepared>], issues: &mut Vec<ValidityIssue>) {
let Some((first, holes)) = rings.split_first() else {
return;
};
if let Some(ext) = first {
for (k, h) in holes.iter().enumerate() {
let ring = k + 1;
if let Some(h) = h
&& let Some(issue) = against_exterior(ring, h, ext)
{
issues.push(issue);
}
}
}
for (i, a) in rings.iter().enumerate().skip(1) {
let Some(a) = a else { continue };
for (j, b) in rings.iter().enumerate().skip(i + 1) {
let Some(b) = b else { continue };
if let Some(issue) = between_holes((i, a), (j, b)) {
issues.push(issue);
}
}
}
}
fn against_exterior(ring: usize, h: &Prepared, ext: &Prepared) -> Option<ValidityIssue> {
match place_ring(&h.ring, &ext.ring) {
RingPlacement::Inside => None,
RingPlacement::Outside => Some(ValidityIssue::HoleOutsideExterior { ring }),
RingPlacement::SharedEdge if same_point_set(&h.ring, &ext.ring) => {
Some(ValidityIssue::DuplicateRings { ring, other: 0 })
}
_ => Some(crossing(ring, h, 0, ext)),
}
}
fn between_holes((i, a): (usize, &Prepared), (j, b): (usize, &Prepared)) -> Option<ValidityIssue> {
let ab = place_ring(&a.ring, &b.ring);
let ba = place_ring(&b.ring, &a.ring);
let shared = ab == RingPlacement::SharedEdge || ba == RingPlacement::SharedEdge;
let crossing_pair = ab == RingPlacement::Crossing || ba == RingPlacement::Crossing;
if crossing_pair {
return Some(crossing(j, b, i, a));
}
if shared {
return Some(if same_point_set(&a.ring, &b.ring) {
ValidityIssue::DuplicateRings { ring: j, other: i }
} else {
crossing(j, b, i, a)
});
}
match (ab, ba) {
(RingPlacement::Outside, RingPlacement::Outside) => None,
(RingPlacement::Inside, _) => Some(ValidityIssue::NestedHoles { ring: i, inside: j }),
(_, RingPlacement::Inside) => Some(ValidityIssue::NestedHoles { ring: j, inside: i }),
_ => Some(crossing(j, b, i, a)),
}
}
fn crossing(ring: usize, r: &Prepared, other: usize, o: &Prepared) -> ValidityIssue {
let edges = contact_witness(&r.ring, &o.ring)
.or_else(|| contact_witness(&o.ring, &r.ring).map(|(f, e)| (e, f)))
.map_or((r.origin[0], o.origin[0]), |pair| r.edges(o, pair));
ValidityIssue::RingsCross { ring, other, edges }
}
pub(crate) fn disconnected_interior(rings: &[&LinearRing]) -> Option<(usize, usize)> {
let n = rings.len();
let boxes: Vec<Option<(f64, f64, f64, f64)>> = rings.iter().map(|r| ring_box(r)).collect();
let mut points: Vec<Coord2d> = Vec::new();
let mut parent: Vec<usize> = (0..n).collect();
let mut incidences: Vec<(usize, usize)> = Vec::new();
for r in 0..n {
for s in (r + 1)..n {
if !boxes_meet(boxes[r], boxes[s]) {
continue;
}
for p in ring_touch_points(rings[r], rings[s]) {
let id = if let Some(k) = points.iter().position(|&q| q == p) {
n + k
} else {
points.push(p);
parent.push(parent.len());
n + points.len() - 1
};
for ring in [r, s] {
if incidences.contains(&(ring, id)) {
continue;
}
incidences.push((ring, id));
let (x, y) = (find(&mut parent, ring), find(&mut parent, id));
if x == y {
return vertex_of(rings, points[id - n]);
}
parent[x] = y;
}
}
}
}
None
}
fn vertex_of(rings: &[&LinearRing], p: Coord2d) -> Option<(usize, usize)> {
rings.iter().enumerate().find_map(|(r, ring)| {
ring.coords()
.as_slice()
.iter()
.position(|&v| v == p)
.map(|k| (r, k))
})
}
fn find(parent: &mut [usize], mut x: usize) -> usize {
while parent[x] != x {
parent[x] = parent[parent[x]];
x = parent[x];
}
x
}
fn ring_box(r: &LinearRing) -> Option<(f64, f64, f64, f64)> {
r.envelope()
.ok()
.map(|e| (e.min().x, e.min().y, e.max().x, e.max().y))
}
fn boxes_meet(a: Option<(f64, f64, f64, f64)>, b: Option<(f64, f64, f64, f64)>) -> bool {
match (a, b) {
(Some(a), Some(b)) => a.0 <= b.2 && b.0 <= a.2 && a.1 <= b.3 && b.1 <= a.3,
_ => true,
}
}