use geometry::Point;
use i_overlay::core::fill_rule::FillRule;
use i_overlay::core::overlay::{Overlay, ShapeType};
use i_overlay::core::overlay_rule::OverlayRule;
use i_overlay::i_float::int::point::IntPoint;
use mln_style::feature::{
Feature, GeometryCollection, GeometryCoordinates, GeometryTileFeature, convert_geometry, signed_area,
};
use mln_style::tile_id::CanonicalTileID;
use crate::tile::TileError;
#[must_use]
pub(crate) fn fixup_polygons(rings: &GeometryCollection) -> GeometryCollection {
let mut overlay = Overlay::new(rings.iter().map(Vec::len).sum());
for ring in rings.iter() {
overlay.add_path_iter(ring.iter().map(|p| IntPoint::new(i32::from(p.x), i32::from(p.y))), ShapeType::Subject);
}
let mut result = GeometryCollection::new();
for shape in overlay.overlay(OverlayRule::Subject, FillRule::EvenOdd) {
for contour in shape {
let mut ring: GeometryCoordinates = contour.iter().map(|p| Point::new(p.x as i16, p.y as i16)).collect();
if let Some(&first) = ring.first() {
ring.push(first);
}
result.push(ring);
}
}
result
}
pub fn limit_holes(polygon: &mut GeometryCollection, max_holes: u32) {
let max_holes = max_holes as usize;
if polygon.len() > 1 + max_holes {
polygon[1..].select_nth_unstable_by(max_holes, |a, b| signed_area(b).abs().total_cmp(&signed_area(a).abs()));
polygon.truncate(1 + max_holes);
}
}
pub(crate) fn convert_feature(
feature: &dyn GeometryTileFeature,
geometries: &GeometryCollection,
tile_id: &CanonicalTileID,
) -> Result<Feature<f64>, TileError> {
let mut converted = Feature::new(convert_geometry(feature.get_type(), geometries, tile_id)?);
converted.properties = feature.get_properties().into_owned();
converted.id = feature.get_id();
Ok(converted)
}
#[cfg(test)]
mod tests {
use mln_style::feature::classify_rings;
use super::*;
fn ring(points: &[(i16, i16)]) -> GeometryCoordinates {
points.iter().map(|&(x, y)| Point::new(x, y)).collect()
}
fn collection(rings: &[&[(i16, i16)]]) -> GeometryCollection {
GeometryCollection::from(rings.iter().map(|r| ring(r)).collect::<Vec<_>>())
}
fn test_signed_area(ring: &GeometryCoordinates) -> f64 {
signed_area(ring)
}
fn fixed_polygons(rings: &[&[(i16, i16)]]) -> Vec<(GeometryCoordinates, usize)> {
let fixed = fixup_polygons(&collection(rings));
let polygons = classify_rings(&fixed);
for polygon in &polygons {
for (i, ring) in polygon.iter().enumerate() {
assert_eq!(ring.first(), ring.last(), "{ring:?}");
assert_eq!(signed_area(ring) > 0.0, i == 0, "{ring:?}");
}
}
polygons.iter().map(|polygon| (polygon[0].clone(), polygon.len() - 1)).collect()
}
#[test]
fn fixup_polygons_rewinds_rings() {
let polygons = fixed_polygons(&[
&[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)],
&[(10, 10), (10, 20), (20, 20), (20, 10), (10, 10)],
&[(50, 0), (90, 0), (90, 40), (50, 40), (50, 0)],
]);
assert_eq!(polygons.len(), 2);
let mut holes: Vec<usize> = polygons.iter().map(|(_, holes)| *holes).collect();
holes.sort_unstable();
assert_eq!(holes, [0, 1]);
}
#[test]
fn fixup_polygons_splits_a_self_intersecting_ring() {
let polygons = fixed_polygons(&[&[(0, 0), (20, 20), (20, 0), (0, 20), (0, 0)]]);
assert_eq!(polygons.len(), 2);
for (outer, holes) in &polygons {
assert_eq!(*holes, 0);
assert_eq!(outer.len(), 4, "a closed triangle: {outer:?}");
assert_eq!(signed_area(outer), 200.0);
}
}
#[test]
fn limit_holes1() {
let mut polygon = collection(&[
&[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)],
&[(30, 30), (32, 30), (32, 32), (30, 30)],
&[(10, 10), (20, 10), (20, 20), (10, 10)],
]);
limit_holes(&mut polygon, 1);
assert_eq!(polygon.len(), 2);
assert_eq!(polygon[0][0].x, 0);
assert_eq!(polygon[1][0].x, 10);
}
#[test]
fn limit_holes2() {
let mut polygon = collection(&[
&[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)],
&[(10, 10), (20, 10), (20, 20), (10, 10)],
&[(30, 30), (32, 30), (32, 32), (30, 30)],
]);
limit_holes(&mut polygon, 1);
assert_eq!(polygon.len(), 2);
assert_eq!(polygon[0][0].x, 0);
assert_eq!(polygon[1][0].x, 10);
}
#[test]
fn limit_holes3() {
let mut polygon = collection(&[
&[
(7336, -248),
(7304, -248),
(7272, -168),
(7176, -200),
(7080, -136),
(7048, -56),
(7128, -8),
(7176, -56),
(7288, -56),
(7316, 0),
(6918, 0),
(6904, -40),
(6984, -72),
(6952, -88),
(6952, -168),
(6888, -88),
(6856, -88),
(6856, -8),
(6872, 0),
(6170, 0),
(6184, -40),
(6136, -72),
(6104, -56),
(6132, 0),
(6028, 0),
(6104, -152),
(6184, -200),
(6206, -256),
(6272, -256),
(6264, -248),
(6248, -120),
(6280, -136),
(6280, -232),
(6288, -256),
(6790, -256),
(6792, -248),
(6800, -256),
(7058, -256),
(7064, -248),
(7096, -256),
(7338, -256),
(7336, -248),
],
&[(6344, -104), (6264, -8), (6392, -72), (6360, -200), (6344, -104)],
&[(6744, -24), (6760, -72), (6728, -104), (6744, -24)],
&[(6616, -104), (6648, -88), (6632, -72), (6664, -56), (6664, -120), (6616, -104)],
]);
let original = polygon.clone();
assert_eq!(polygon.len(), 4);
assert_eq!(test_signed_area(&polygon[0]), 515_360.0); assert_eq!(test_signed_area(&polygon[1]), -12288.0); assert_eq!(test_signed_area(&polygon[2]), -2048.0); assert_eq!(test_signed_area(&polygon[3]), -3072.0);
limit_holes(&mut polygon, 2);
assert_eq!(polygon.len(), 3);
assert_eq!(original[0], polygon[0]);
assert!(polygon[1..].contains(&original[1]));
assert!(polygon[1..].contains(&original[3]));
}
}