use std::f64::consts::PI;
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::constants::EXTENT;
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)
}
#[must_use]
pub fn is_boundary_edge(a: Point<i16>, b: Point<i16>) -> bool {
let outside = |v: i16| v < 0 || i32::from(v) > EXTENT;
(a.x == b.x && outside(a.x)) || (a.y == b.y && outside(a.y))
}
#[must_use]
pub fn is_entirely_outside(ring: &[Point<i16>]) -> bool {
let extent = |v: i16| i32::from(v);
ring.iter().all(|p| p.x < 0)
|| ring.iter().all(|p| extent(p.x) > EXTENT)
|| ring.iter().all(|p| p.y < 0)
|| ring.iter().all(|p| extent(p.y) > EXTENT)
}
#[must_use]
pub fn round_polygon_corners(polygon: &GeometryCollection, corner_distance: f64) -> GeometryCollection {
GeometryCollection::from(polygon.iter().map(|ring| round_ring(ring, corner_distance)).collect::<Vec<_>>())
}
fn round_ring(ring: &GeometryCoordinates, corner_distance: f64) -> GeometryCoordinates {
let closed = ring.len() > 1 && ring.first() == ring.last();
let vertex_count = if closed { ring.len() - 1 } else { ring.len() };
if vertex_count < 3 {
return ring.clone();
}
let mut rounded = Vec::with_capacity(vertex_count * 4);
let mut previous_is_boundary = is_boundary_edge(ring[vertex_count - 1], ring[0]);
for i in 0..vertex_count {
let previous = ring[(i + vertex_count - 1) % vertex_count];
let corner = ring[i];
let next = ring[(i + 1) % vertex_count];
let next_is_boundary = is_boundary_edge(corner, next);
if previous_is_boundary || next_is_boundary {
rounded.push(Point::new(f64::from(corner.x), f64::from(corner.y)));
} else {
append_round_corner(&mut rounded, previous, corner, next, corner_distance);
}
previous_is_boundary = next_is_boundary;
}
let snap = |v: f64| (v + 0.5).floor() as i16;
let mut snapped = GeometryCoordinates::with_capacity(rounded.len() + 1);
for p in rounded {
let p = Point::new(snap(p.x), snap(p.y));
if snapped.last() != Some(&p) {
snapped.push(p);
}
}
while snapped.len() > 1 && snapped.first() == snapped.last() {
snapped.pop();
}
if snapped.len() < 3 {
return ring.clone();
}
if closed {
snapped.push(snapped[0]);
}
snapped
}
fn append_round_corner(
ring: &mut Vec<Point<f64>>,
previous: Point<i16>,
corner: Point<i16>,
next: Point<i16>,
corner_distance: f64,
) {
const MAX_EDGE_FRACTION: f64 = 0.2;
let c = Point::new(f64::from(corner.x), f64::from(corner.y));
let towards = |p: Point<i16>| Point::new(f64::from(p.x) - c.x, f64::from(p.y) - c.y);
let (a, b) = (towards(previous), towards(next));
let (len_a, len_b) = (a.x.hypot(a.y), b.x.hypot(b.y));
if len_a == 0.0 || len_b == 0.0 {
ring.push(c);
return;
}
let ua = Point::new(a.x / len_a, a.y / len_a);
let ub = Point::new(b.x / len_b, b.y / len_b);
let dot = ua.x * ub.x + ua.y * ub.y;
if dot.abs() > mln_style::math::deg2rad(5.0).cos() {
ring.push(c);
return;
}
let r = corner_distance.min(len_a * MAX_EDGE_FRACTION).min(len_b * MAX_EDGE_FRACTION);
let tangent_a = Point::new(c.x + ua.x * r, c.y + ua.y * r);
let tangent_b = Point::new(c.x + ub.x * r, c.y + ub.y * r);
let bisector = Point::new(ua.x + ub.x, ua.y + ub.y);
let to_center = r / f64::midpoint(1.0, dot).sqrt() / bisector.x.hypot(bisector.y);
let center = Point::new(c.x + bisector.x * to_center, c.y + bisector.y * to_center);
let from = Point::new(tangent_a.x - center.x, tangent_a.y - center.y);
let to = Point::new(tangent_b.x - center.x, tangent_b.y - center.y);
let sweep = (from.x * to.y - from.y * to.x).atan2(from.x * to.x + from.y * to.y);
let steps = ((sweep.abs() / (PI / 6.0) - 1e-6).ceil() as i32).max(2);
for step in 0..=steps {
let (sin, cos) = (sweep * f64::from(step) / f64::from(steps)).sin_cos();
ring.push(Point::new(center.x + cos * from.x - sin * from.y, center.y + sin * from.x + cos * from.y));
}
}
#[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]));
}
fn points(ring: &GeometryCoordinates) -> Vec<(i16, i16)> {
ring.iter().map(|p| (p.x, p.y)).collect()
}
#[test]
fn round_polygon_corners_square() {
let polygon = collection(&[&[(0, 0), (100, 0), (100, 100), (0, 100), (0, 0)]]);
let rounded = round_polygon_corners(&polygon, 10.0);
assert_eq!(
points(&rounded[0]),
[
(0, 10),
(1, 5),
(5, 1),
(10, 0),
(90, 0),
(95, 1),
(99, 5),
(100, 10),
(100, 90),
(99, 95),
(95, 99),
(90, 100),
(10, 100),
(5, 99),
(1, 95),
(0, 90),
(0, 10),
]
);
}
#[test]
fn round_polygon_corners_clamps_to_a_fifth_of_the_shorter_edge() {
let polygon = collection(&[&[(0, 0), (100, 0), (100, 50), (0, 50), (0, 0)]]);
let rounded = round_polygon_corners(&polygon, 1000.0);
assert_eq!(points(&rounded[0])[..5], [(0, 10), (1, 5), (5, 1), (10, 0), (90, 0)]);
}
#[test]
fn round_polygon_corners_keeps_corners_on_clip_lines_sharp() {
let polygon = collection(&[&[(-64, 0), (100, 0), (100, 100), (-64, 100), (-64, 0)]]);
let rounded = round_polygon_corners(&polygon, 10.0);
let rounded = points(&rounded[0]);
assert_eq!(rounded.len(), 2 * 4 + 2 + 1);
assert_eq!(rounded[0], (-64, 0));
assert!(rounded.contains(&(-64, 100)));
assert!(!rounded.contains(&(100, 0)));
}
#[test]
fn round_polygon_corners_drops_vertices_that_snap_together() {
let polygon = collection(&[&[(0, 0), (100, 0), (100, 100), (0, 100), (0, 0)]]);
assert_eq!(round_polygon_corners(&polygon, 0.4), polygon);
}
#[test]
fn round_polygon_corners_rounds_every_ring() {
let polygon = collection(&[
&[(0, 0), (100, 0), (100, 100), (0, 100), (0, 0)],
&[(25, 25), (25, 75), (75, 75), (75, 25), (25, 25)],
]);
let rounded = round_polygon_corners(&polygon, 5.0);
assert_eq!(rounded.len(), 2);
assert_eq!(rounded[0].len(), 17);
assert_eq!(rounded[1].len(), 17);
assert_eq!(rounded[1].first(), rounded[1].last());
assert_eq!(points(&rounded[1])[0], (30, 25));
}
#[test]
fn round_polygon_corners_keeps_straight_and_reversing_vertices() {
let polygon = collection(&[&[(0, 0), (50, 0), (200, 1), (0, 2), (0, 0)]]);
let rounded = points(&round_polygon_corners(&polygon, 10.0)[0]);
assert!(rounded.contains(&(50, 0)));
assert!(rounded.contains(&(200, 1)));
}
#[test]
fn is_entirely_outside_needs_every_vertex_past_one_edge() {
assert!(is_entirely_outside(&ring(&[(8200, 0), (9000, 100), (8193, 9000)])));
assert!(is_entirely_outside(&ring(&[(0, -5), (100, -1), (50, -2000)])));
assert!(!is_entirely_outside(&ring(&[(-10, 100), (100, -10), (-10, -10)])));
assert!(!is_entirely_outside(&ring(&[(8192, 0), (9000, 100), (9000, 9000)])));
}
#[test]
fn round_polygon_corners_keeps_degenerate_rings() {
let polygon = collection(&[&[], &[(1, 1)], &[(1, 1), (1, 1)], &[(0, 0), (9, 0), (0, 0)]]);
assert_eq!(round_polygon_corners(&polygon, 10.0), polygon);
}
}