From 23201759497b6dddcd1415b55da49c705d12d5e3 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:00:56 -0600 Subject: [PATCH 01/12] feat(analysis): Repair Geometry takes a rule for what is inside: Shells Minus Holes or Even-Odd (#2003) Repair Geometry's rebuild by area now takes a rule. Shells Minus Holes, the default, is today's rebuild, byte for byte. Even-Odd reads each ring's own area the same way (a bowtie keeps both triangles, a ring traced twice counts once), then keeps what lies inside an odd number of rings, whatever their roles: where two parts overlap is left out, a ring listed as a part inside another is a hole, and what of a hole lies outside its exterior ring is a part. It suits data whose ring roles may be wrong, such as a hole stored as a part of its own. Exact duplicate rings of one role (from any vertex, either direction) are left out first, since a pair would cancel out, and the tool says how many. Features rebuilt under Even-Odd have even_odd in repairs, and one notice says how many there were. Valid polygons, and those fixed without moving a coordinate, come out the same under either rule. --- crates/analysis/src/repair.rs | 222 ++++++++++++++++--- crates/analysis/src/tools.rs | 8 + crates/analysis/src/tools/repair_geometry.rs | 56 ++++- crates/analysis/tests/repair_geometry.rs | 161 +++++++++++++- docs/user-guide/analysis.md | 22 ++ 5 files changed, 431 insertions(+), 38 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index a921ac3db3..2d93b82059 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -109,6 +109,9 @@ pub(crate) struct Repaired { /// When the overlay rebuilt it, how far its coordinates may have moved, in their units (degrees, /// as coordinates are stored). pub moved: Option, + /// Under [`Rule::EvenOdd`], exact duplicate rings of one role dropped before the rings were + /// combined, since a pair would cancel out. + pub duplicates: usize, } /// What [`repair`] made of one geometry. @@ -121,17 +124,55 @@ pub(crate) enum Outcome { Kept(Kept), } -/// Repairs one geometry, spending at most [`validity::MAX_WORK`] on each check and on the -/// overlay, and asking `has_room` whether the browser can give the overlay's memory before it runs -/// (see `web_has_room` in the tools). -pub(crate) fn repair(g: Option<&Geometry>, has_room: &dyn Fn(u64) -> bool) -> Outcome { - repair_with(g, validity::MAX_WORK, &|_| true, has_room) +/// How the rebuild by area decides what is inside a polygon whose rings cross, overlap or sit in +/// the wrong place (#2003). Both give a valid geometry; they differ in what the rings are taken to +/// mean, and only for such polygons: one valid as it is, or fixed without moving a coordinate, is +/// written the same either way. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) enum Rule { + /// Each part is its exterior ring's area less the union of its holes', and the feature is the + /// union of its parts: the rings' roles as OGC gives them (GEOS's "structure"). Parts that + /// overlap merge, a hole outside its exterior ring disappears, and a ring listed as a part of + /// its own is filled even when it lies inside another part. + #[default] + ShellsMinusHoles, + /// Each ring's own area as [`Rule::ShellsMinusHoles`] reads it (its loops by winding, so a + /// ring traced twice or a bowtie keeps its area), then the rings combined by parity whatever + /// their roles: what lies inside an odd number of rings is in (GEOS's "linework" gives the same + /// area). Where two parts overlap is left out, a ring listed as a part inside another is a + /// hole, and what of a hole lies outside its exterior ring is a part. Exact duplicate rings of + /// one role are dropped first, since a pair would cancel out ([`Repaired::duplicates`]). + EvenOdd, } -/// [`repair`] with a work `budget`, asking `allow` before each reservation of a buffer whether to +impl Rule { + /// The rule a tool parameter names, as its choices spell it. + pub(crate) fn named(name: &str) -> Option { + match name { + "Shells Minus Holes" => Some(Rule::ShellsMinusHoles), + "Even-Odd" => Some(Rule::EvenOdd), + _ => None, + } + } +} + +/// Repairs one geometry under `rule`, spending at most [`validity::MAX_WORK`] on each check and on +/// the overlay, and asking `has_room` whether the browser can give the overlay's memory before it +/// runs (see `web_has_room` in the tools). +pub(crate) fn repair_by(g: Option<&Geometry>, rule: Rule, has_room: &dyn Fn(u64) -> bool) -> Outcome { + repair_by_with(g, rule, validity::MAX_WORK, &|_| true, has_room) +} + +/// [`repair_by`] with a work `budget`, asking `allow` before each reservation of a buffer whether to /// try it, as [`validity::check_with`] does: tests refuse some to stand in for running out of /// memory. -pub(crate) fn repair_with(g: Option<&Geometry>, budget: u64, allow: &dyn Fn(usize) -> bool, has_room: &dyn Fn(u64) -> bool) -> Outcome { +pub(crate) fn repair_by_with( + g: Option<&Geometry>, + rule: Rule, + budget: u64, + allow: &dyn Fn(usize) -> bool, + has_room: &dyn Fn(u64) -> bool, +) -> Outcome { let first = validity::check_with(g, budget, allow); if first.problems.is_empty() { return Outcome::Valid; @@ -148,11 +189,13 @@ pub(crate) fn repair_with(g: Option<&Geometry>, budget: u64, allow: &dyn Fn(usiz Geometry::Line(parts) => match lines(parts, room) { Err(limit) => Outcome::Kept(kept_for(limit)), Ok((parts, _)) if parts.is_empty() => Outcome::Gone(Gone::TooFewPoints), - Ok((parts, dropped)) => { - verified(Repaired { geometry: Geometry::Line(parts), codes: first.codes(), dropped, narrow: 0, moved: None }, budget, allow) - } + Ok((parts, dropped)) => verified( + Repaired { geometry: Geometry::Line(parts), codes: first.codes(), dropped, narrow: 0, moved: None, duplicates: 0 }, + budget, + allow, + ), }, - Geometry::Polygon(polys) => polygon(polys, &first, budget, allow, has_room), + Geometry::Polygon(polys) => polygon(polys, &first, rule, budget, allow, has_room), // A point's only problems, a missing or non-finite coordinate, are handled above. Geometry::Point(_) | Geometry::MultiPoint(_) => Outcome::Kept(Kept::Failed), } @@ -264,6 +307,7 @@ fn rings(polys: &[Vec>], room: Room) -> Result<(Vec>>, fn polygon( polys: &[Vec>], first: &Checked, + rule: Rule, budget: u64, allow: &dyn Fn(usize) -> bool, has_room: &dyn Fn(u64) -> bool, @@ -297,7 +341,7 @@ fn polygon( } codes.sort_unstable(); if second.problems.is_empty() { - return Outcome::Repaired(Repaired { geometry: fixed, codes, dropped, narrow: 0, moved: None }); + return Outcome::Repaired(Repaired { geometry: fixed, codes, dropped, narrow: 0, moved: None, duplicates: 0 }); } let Geometry::Polygon(parts) = &fixed else { return Outcome::Kept(Kept::Failed) }; if gone(second).is_some() { @@ -322,8 +366,11 @@ fn polygon( Ok(c) => c, Err(limit) => return Outcome::Kept(kept_for(limit)), }; - let rebuilt = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| by_area(parts, &crossing, room))); - let Rebuilt { polys, no_area, narrow, found } = match rebuilt { + let rebuilt = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| match rule { + Rule::ShellsMinusHoles => by_area(parts, &crossing, room), + Rule::EvenOdd => by_parity(parts, &crossing, room), + })); + let Rebuilt { polys, no_area, narrow, found, duplicates } = match rebuilt { Ok(Ok(rebuilt)) => rebuilt, Ok(Err(kept)) => return Outcome::Kept(kept), Err(_) => return Outcome::Kept(Kept::Failed), @@ -338,7 +385,11 @@ fn polygon( } codes.sort_unstable(); let moved = Some(step * GRID_STEPS_MOVED); - verified(Repaired { geometry: Geometry::Polygon(polys), codes, dropped: dropped.saturating_add(no_area), narrow, moved }, budget, allow) + verified( + Repaired { geometry: Geometry::Polygon(polys), codes, dropped: dropped.saturating_add(no_area), narrow, moved, duplicates }, + budget, + allow, + ) } /// The step of the integer grid i_overlay rounds the points `pts` to: it takes half the larger @@ -512,6 +563,119 @@ struct Rebuilt { /// Holes and parts dropped as narrower than a step of the overlay's grid in any direction. narrow: usize, found: Found, + /// Exact duplicate rings of one role dropped first ([`Rule::EvenOdd`] only). + duplicates: usize, +} + +/// The area `ring` stands for, as both rules read one ring: as it is when simple (it isn't in +/// `crossing`, the rings that cross, touch or run back over themselves, by part and ring, sorted), +/// otherwise its loops by non-zero winding, so both lobes of a bowtie count and a ring traced twice +/// counts once. +fn ring_area(ring: &[Coord], at: (usize, usize), crossing: &[(usize, usize)], room: Room) -> Result, Kept> { + let ring = geo::LineString(room.collect(ring.len(), ring.iter().map(|c| geo::Coord { x: c.x, y: c.y })).map_err(kept_for)?); + let poly = geo::Polygon::new(ring, vec![]); + if crossing.binary_search(&at).is_err() { + // A simple ring: its area as it is, wound as the overlay expects. + return Ok(geo::MultiPolygon::new(vec![poly.orient(Direction::Default)])); + } + on_grid(std::iter::once(poly.exterior()))?; + Ok(poly.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::NonZero)) +} + +/// The rebuild under [`Rule::EvenOdd`] of `parts` (closed rings, each with 3 distinct points or +/// more; `crossing` as in [`ring_area`]): exact duplicate rings of one role dropped +/// ([`distinct_rings`]), each ring's own area ([`ring_area`]), pieces narrower than a step of the +/// overlay's grid dropped and counted as [`by_area`] drops them, then the pieces combined by +/// parity in one overlay. No codes are found beyond the checks': the rings' roles, which they +/// would be about, don't decide the result. +fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) -> Result { + let (keep, duplicates) = distinct_rings(parts, room).map_err(kept_for)?; + let mut pieces: Vec> = Vec::new(); + let mut no_area = 0usize; + let mut narrow = 0usize; + for (p, r) in keep { + let Some(ring) = parts.get(p).and_then(|rings| rings.get(r)) else { continue }; + let area = ring_area(ring, (p, r), crossing, room)?; + if area.0.is_empty() { + no_area += 1; + } + for piece in area.0 { + room.grow(&mut pieces).map_err(kept_for)?; + pieces.push(piece); + } + } + if pieces.len() > 1 { + // Pieces narrower than a step of the grid the combining overlay rounds to are dropped first. + let step = on_grid(rings_of(&pieces))?; + pieces = wider(pieces, step, &mut narrow, room)?; + } + let pieces = geo::MultiPolygon::new(pieces); + let combined = if pieces.0.len() > 1 { + on_grid(rings_of(&pieces.0))?; + pieces.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::EvenOdd) + } else { + pieces + }; + Ok(Rebuilt { polys: polygons(&combined, room).map_err(kept_for)?, no_area, narrow, found: Found::new(), duplicates }) +} + +/// The rings of `parts` to combine under [`Rule::EvenOdd`], by part and ring in their order, and +/// how many were left out as exact duplicates of an earlier ring of the same role: an exterior ring +/// of another exterior ring, a hole of another hole (of any part). The same ring is the same points +/// in the same cyclic order, from any vertex and in either direction. A ring isn't compared with one +/// of the other role: a hole equal to its exterior ring leaves its part no area, as it does under +/// [`Rule::ShellsMinusHoles`]. Found by sorting each ring's normal form, in buffers reserved +/// fallibly. +fn distinct_rings(parts: &[Vec>], room: Room) -> Result<(Vec<(usize, usize)>, usize), Limit> { + let n = parts.iter().map(Vec::len).fold(0usize, usize::saturating_add); + // (whether a hole, the ring's normal form, part, ring) + let mut all: Vec<(bool, Vec<(u64, u64)>, usize, usize)> = room.vec(n)?; + for (p, rings) in parts.iter().enumerate() { + for (r, ring) in rings.iter().enumerate() { + all.push((r > 0, normal_form(ring, room)?, p, r)); + } + } + all.sort_unstable_by(|a, b| (a.0, &a.1, a.2, a.3).cmp(&(b.0, &b.1, b.2, b.3))); + let mut keep: Vec<(usize, usize)> = room.vec(all.len())?; + let mut duplicates = 0usize; + for (i, (hole, form, p, r)) in all.iter().enumerate() { + // Sorted, so a duplicate follows the first of its kind, which has the lowest part and ring. + if i > 0 && all.get(i - 1).is_some_and(|prev| prev.0 == *hole && prev.1 == *form) { + duplicates += 1; + } else { + keep.push((*p, *r)); + } + } + keep.sort_unstable(); + Ok((keep, duplicates)) +} + +/// A closed ring's points as bits, without the closing repeat, from its least vertex (by x, then +/// y) and in whichever direction reads less: the same for the same ring whatever vertex it starts +/// at and whichever way it is wound. A vertex the ring passes more than once is tried as a start +/// each time. +fn normal_form(ring: &[Coord], room: Room) -> Result, Limit> { + let pts = match ring.split_last() { + Some((last, rest)) if Some(last) == rest.first() => rest, + _ => ring, + }; + let bits = |c: &Coord| (c.x.to_bits(), c.y.to_bits()); + let n = pts.len(); + let least = pts + .iter() + .map(|c| (c.x, c.y)) + .fold((f64::INFINITY, f64::INFINITY), |m, c| if c.0.total_cmp(&m.0).then(c.1.total_cmp(&m.1)).is_lt() { c } else { m }); + let mut best: Option> = None; + for (start, _) in pts.iter().enumerate().filter(|(_, c)| (c.x, c.y) == least) { + for forward in [true, false] { + let at = |k: usize| if forward { (start + k) % n } else { (start + n - k % n) % n }; + let form = room.collect(n, (0..n).filter_map(|k| pts.get(at(k)).map(bits)))?; + if best.as_ref().is_none_or(|b| form < *b) { + best = Some(form); + } + } + } + Ok(best.unwrap_or_default()) } /// The rebuild by area (see the module doc) of `parts`, closed rings each with 3 distinct points @@ -525,16 +689,7 @@ struct Rebuilt { /// ones are dropped first, so their vanishing isn't taken for a merge). fn by_area(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) -> Result { let empty = geo::MultiPolygon::::new(vec![]); - let area = |ring: &Vec, at: (usize, usize)| -> Result, Kept> { - let ring = geo::LineString(room.collect(ring.len(), ring.iter().map(|c| geo::Coord { x: c.x, y: c.y })).map_err(kept_for)?); - let poly = geo::Polygon::new(ring, vec![]); - if crossing.binary_search(&at).is_err() { - // A simple ring: its area as it is, wound as the overlay expects. - return Ok(geo::MultiPolygon::new(vec![poly.orient(Direction::Default)])); - } - on_grid(std::iter::once(poly.exterior()))?; - Ok(poly.boolean_op_with_fill_rule(&empty, OpType::Union, FillRule::NonZero)) - }; + let area = |ring: &Vec, at: (usize, usize)| ring_area(ring, at, crossing, room); let mut all: Vec> = Vec::new(); let mut no_area = 0usize; let mut narrow = 0usize; @@ -611,7 +766,7 @@ fn by_area(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) - } else { all }; - Ok(Rebuilt { polys: polygons(&merged, room).map_err(kept_for)?, no_area, narrow, found }) + Ok(Rebuilt { polys: polygons(&merged, room).map_err(kept_for)?, no_area, narrow, found, duplicates: 0 }) } /// The overlay's polygons as rings, copied fallibly. @@ -635,6 +790,16 @@ fn polygons(m: &geo::MultiPolygon, room: Room) -> Result mod tests { use super::*; + /// [`repair_by`] under the default rule. + fn repair(g: Option<&Geometry>, has_room: &dyn Fn(u64) -> bool) -> Outcome { + repair_by(g, Rule::default(), has_room) + } + + /// [`repair_by_with`] under the default rule. + fn repair_with(g: Option<&Geometry>, budget: u64, allow: &dyn Fn(usize) -> bool, has_room: &dyn Fn(u64) -> bool) -> Outcome { + repair_by_with(g, Rule::default(), budget, allow, has_room) + } + type Ring<'a> = &'a [(f64, f64)]; /// Every reservation tried. @@ -910,9 +1075,10 @@ mod tests { #[test] fn a_repair_that_is_not_valid_is_not_kept() { - let bad = Repaired { geometry: poly(&[BOWTIE]), codes: vec![Code::SelfIntersection], dropped: 0, narrow: 0, moved: None }; + let bad = + Repaired { geometry: poly(&[BOWTIE]), codes: vec![Code::SelfIntersection], dropped: 0, narrow: 0, moved: None, duplicates: 0 }; assert_eq!(verified(bad, validity::MAX_WORK, &|_| true), Outcome::Kept(Kept::Failed)); - let busy = Repaired { geometry: poly(&[BOWTIE]), codes: vec![], dropped: 0, narrow: 0, moved: None }; + let busy = Repaired { geometry: poly(&[BOWTIE]), codes: vec![], dropped: 0, narrow: 0, moved: None, duplicates: 0 }; assert_eq!(verified(busy, 0, &|_| true), Outcome::Kept(Kept::Work)); } } diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index e0f3eadc28..36692ee353 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -100,6 +100,14 @@ pub static TOOLS: &[ToolSpec] = &[ summary: "Writes every feature with its geometry made valid where it can be: rings closed, repeated vertices and degenerate parts dropped, self-intersections, overlapping parts and holes outside their shell resolved. Features with a missing or non-finite coordinate anywhere, even in a hole, or left with no usable points or, for polygons, no area once repaired, are written without geometry. Other features too complex to check, or whose repair fails, are written unchanged. Says what was repaired in each.", params: &[ P { name: "input", label: "Input Features", kind: K::VectorLayer, required: true, default: "", help: "" }, + P { + name: "rule", + label: "Rule", + kind: K::Choice(&["Shells Minus Holes", "Even-Odd"]), + required: false, + default: "\"Shells Minus Holes\"", + help: "What is inside a polygon whose rings cross, overlap or sit in the wrong place. Shells Minus Holes: each part is its outer ring less its holes, and overlapping parts merge. Even-Odd: what lies inside an odd number of rings, so where two parts overlap is left out and a ring listed as a part inside another is a hole. Valid polygons are written the same either way.", + }, P { name: "output", label: "Output Name", kind: K::Text, required: false, default: "", help: "" }, ], run: repair_geometry::run, diff --git a/crates/analysis/src/tools/repair_geometry.rs b/crates/analysis/src/tools/repair_geometry.rs index e6b5a2f363..ae37d16aee 100644 --- a/crates/analysis/src/tools/repair_geometry.rs +++ b/crates/analysis/src/tools/repair_geometry.rs @@ -3,7 +3,7 @@ //! repaired. use super::check_geometry::{features, joined, row_bytes}; use super::*; -use crate::repair::{self, Gone, Kept, Outcome}; +use crate::repair::{self, Gone, Kept, Outcome, Rule}; use crate::validity::Code; const TOOL: &str = "Repair Geometry"; @@ -12,9 +12,22 @@ const TOOL: &str = "Repair Geometry"; /// valid ones as stored, repaired ones with their new geometry, those that can't be repaired /// without geometry, and those left as they were (too complex, or the repair failed) as stored. pub(super) fn run(inp: &dyn Inputs, p: &Json) -> Result { - run_with_room(inp, p, &web_has_room, |g| repair::repair(g, &web_has_room)) + let rule = rule_of(p)?; + run_with_room(inp, p, &web_has_room, |g| repair::repair_by(g, rule, &web_has_room)) } +/// The rule `p` names (#2003), Shells Minus Holes when it names none. +fn rule_of(p: &Json) -> Result { + match p.get("rule") { + None | Some(Json::Null) => Ok(Rule::default()), + Some(v) => v.as_str().and_then(Rule::named).ok_or_else(|| format!("{TOOL}: rule is \"Shells Minus Holes\" or \"Even-Odd\"")), + } +} + +/// What a feature rebuilt under the Even-Odd rule has in its repairs field, after the codes: data, +/// never translated. +const EVEN_ODD: &str = "even_odd"; + /// A feature's geometry in the output. enum Written { /// As stored. @@ -58,6 +71,11 @@ struct Tally { moved: f64, /// Features with more attribute values than the layer has fields. extra: usize, + /// Under the Even-Odd rule: features it rebuilt, and duplicate rings left out first, from how + /// many features. + even_odd: usize, + duplicates: usize, + duplicate_features: usize, } impl Tally { @@ -74,6 +92,7 @@ fn run_with_room( has_room: &dyn Fn(u64) -> bool, repair: impl Fn(Option<&Geometry>) -> Outcome, ) -> Result { + let rule = rule_of(p)?; let ds = input(inp, p, "input")?; if ds.geometry_type == GeometryType::None { return Err(format!("{TOOL} needs a layer with geometry: '{}' is a table", ds.name)); @@ -122,9 +141,15 @@ fn run_with_room( tally.overlaid += 1; tally.moved = tally.moved.max(moved); } - // A hole or part with no area on the overlay's grid is said after the codes. + // The Even-Odd rule decides only what the overlay rebuilds: said for those features. + let even_odd = rule == Rule::EvenOdd && r.moved.is_some(); + tally.even_odd += usize::from(even_odd); + tally.duplicates += r.duplicates; + tally.duplicate_features += usize::from(r.duplicates > 0); + // A hole or part with no area on the overlay's grid is said after the codes, then the + // rule when it isn't the default. let narrow = (r.narrow > 0).then_some(Gone::NoArea.as_str()); - let names: Vec<&str> = r.codes.iter().map(|c| c.as_str()).chain(narrow).collect(); + let names: Vec<&str> = r.codes.iter().map(|c| c.as_str()).chain(narrow).chain(even_odd.then_some(EVEN_ODD)).collect(); (joined(&names, ", ").map_err(no_memory)?, Written::Repaired(r.geometry)) } Outcome::Gone(why) => { @@ -289,6 +314,21 @@ fn notices(t: &Tally, field: &str) -> Vec { rounded_up(t.moved) )); } + if t.even_odd > 0 { + messages.push(format!( + "{TOOL}: {} {} rebuilt by the Even-Odd rule, with {EVEN_ODD} in {field}: where their rings overlap is left out, and a ring inside another is a hole, whatever its role", + features(t.even_odd), + was(t.even_odd) + )); + } + if t.duplicates > 0 { + messages.push(format!( + "{TOOL}: {} duplicate {} left out of {} before the Even-Odd rule, which would have canceled each pair out", + t.duplicates, + if t.duplicates == 1 { "ring was" } else { "rings were" }, + features(t.duplicate_features) + )); + } if t.extra > 0 { messages.push(format!( "{TOOL}: {} had more attribute values than the layer has fields; the extra values aren't in the output", @@ -364,7 +404,7 @@ mod tests { asked.set(need); true }, - |g| repair::repair(g, &|_| true), + |g| repair::repair_by(g, Rule::default(), &|_| true), ) .unwrap(); let Output::Layer(out) = &r.output else { panic!("a layer") }; @@ -377,7 +417,7 @@ mod tests { let first = point_bytes(&inp.0.features[0].attrs, 2) + "ring_not_closed".len() as u64 + ALLOCATION_OVERHEAD + 2 * geometry; let second = point_bytes(&inp.0.features[1].attrs, 2) + "null_geometry".len() as u64 + ALLOCATION_OVERHEAD; assert_eq!(asked.get(), first + second); - let e = run_with_room(&inp, &p, &|_| false, |g| repair::repair(g, &|_| true)).err().unwrap(); + let e = run_with_room(&inp, &p, &|_| false, |g| repair::repair_by(g, Rule::default(), &|_| true)).err().unwrap(); assert_eq!(e, "Repair Geometry: the 2 features would need about 3 MiB, more than the browser can give now: select fewer features"); } @@ -420,7 +460,7 @@ mod tests { let r = run_with_room(&inp, &p, &|_| true, |g| { repaired.set(repaired.get() + 1); control.cancel(); - repair::repair(g, &|_| true) + repair::repair_by(g, Rule::default(), &|_| true) }); assert_eq!(r.err().as_deref(), Some(crate::control::CANCELLED)); assert_eq!(repaired.get(), 1, "stopped before the second feature"); @@ -434,7 +474,7 @@ mod tests { control.cancel(); true }, - |g| repair::repair(g, &|_| true), + |g| repair::repair_by(g, Rule::default(), &|_| true), ); assert_eq!(r.err().as_deref(), Some(crate::control::CANCELLED), "stopped in the copies"); } diff --git a/crates/analysis/tests/repair_geometry.rs b/crates/analysis/tests/repair_geometry.rs index 75045de0e4..bcd15a8bc8 100644 --- a/crates/analysis/tests/repair_geometry.rs +++ b/crates/analysis/tests/repair_geometry.rs @@ -1039,8 +1039,10 @@ fn windings(g: &Geometry) -> Vec<(String, Geometry)> { /// each, as merging the parts does here. The manual gives no areas: the expected ones are this /// tool's own (geo's overlay), pinned so that a change shows, and checked against what any union /// of the parts must cover: at least the largest part, less than all of them added up. -#[test] -fn the_postgis_manual_examples_merge_into_one_polygon() { +/// The PostGIS manual's two ST_MakeValid examples of overlapping parts +/// (https://postgis.net/docs/ST_MakeValid.html), each ring a part of its own, with the area this +/// tool gives them merged (see [`the_postgis_manual_examples_merge_into_one_polygon`]). +fn postgis_examples() -> Vec<(Vec>, f64)> { let ring = |s: &str| -> Vec<(f64, f64)> { s.split(',') .map(|p| match p.split_whitespace().map(str::parse::).collect::>()[..] { @@ -1073,6 +1075,12 @@ fn the_postgis_manual_examples_merge_into_one_polygon() { 19872.857, ), ]; + examples.into() +} + +#[test] +fn the_postgis_manual_examples_merge_into_one_polygon() { + let examples = postgis_examples(); for (rings, want) in examples { // Each ring a part of its own, as the manual's MULTIPOLYGONs have them. let owned: Vec> = rings.iter().map(|r| vec![r.as_slice()]).collect(); @@ -1089,3 +1097,152 @@ fn the_postgis_manual_examples_merge_into_one_polygon() { assert!((got - want).abs() <= want * 1e-6, "area {got} not {want}"); } } + +/// The output and repairs of one polygon feature holding `g`, repaired under `rule` (#2003), and +/// the run's messages. +fn one_by(rule: &str, g: Geometry) -> (Option, String, Vec) { + let mut l = layer(GeometryType::Polygon); + add(&mut l, Some(g), "x"); + let r = rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": rule})).unwrap(); + valid(dataset(&r)); + let (g, repairs) = rows(&r).remove(0); + (g, repairs, r.messages.clone()) +} + +/// The hole count of each part of a polygon geometry, sorted, and its area. +fn shape(g: &Geometry) -> (Vec, f64) { + let Geometry::Polygon(parts) = g else { panic!("a polygon: {g:?}") }; + let mut holes: Vec = parts.iter().map(|rings| rings.len() - 1).collect(); + holes.sort_unstable(); + (holes, area(g)) +} + +/// The shapes where Even-Odd gives another polygon than the default, with what it gives: each +/// part's hole count, the area, and the repairs. +fn rule_cases() -> Vec<(&'static str, Geometry, Vec, f64, &'static str)> { + let twice: Ring = &[(0., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]; + let small: Ring = &[(20., 0.), (22., 0.), (22., 2.), (20., 2.), (20., 0.)]; + let arms = multi(&[&[&[(0., 4.), (10., 4.), (10., 6.), (0., 6.), (0., 4.)]], &[&[(4., 0.), (6., 0.), (6., 10.), (4., 10.), (4., 0.)]]]); + vec![ + // Where two parts overlap is left out: two L shapes touching at two corners; four arms. + ("two overlapping parts", multi(&[&[SQUARE], &[OVERLAPPING]]), vec![0, 0], 150.0, "parts_overlap, even_odd"), + ("plus sign", arms, vec![0, 0, 0, 0], 32.0, "parts_overlap, even_odd"), + // What of a hole lies outside its exterior ring is a part. + ("hole outside its shell", poly(&[SQUARE, OUTSIDE]), vec![0, 0], 100.5, "hole_outside_shell, even_odd"), + ( + "hole crossing its shell", + poly(&[SQUARE, &[(8., 4.), (8., 6.), (12., 6.), (12., 4.), (8., 4.)]]), + vec![0, 0], + 100.0, + "hole_outside_shell, even_odd", + ), + // A ring listed as a part inside another is a hole, whichever way it is wound. + ( + "hole listed as a part", + multi(&[&[SQUARE], &[&[(2., 2.), (2., 4.), (4., 4.), (4., 2.), (2., 2.)]]]), + vec![1], + 96.0, + "parts_overlap, even_odd", + ), + ( + "part inside another, wound the same way", + multi(&[&[SQUARE], &[&[(2., 2.), (8., 2.), (8., 8.), (2., 8.), (2., 2.)]]]), + vec![1], + 64.0, + "parts_overlap, even_odd", + ), + // Duplicates are left out first rather than cancel each other out. + ("the same part twice", multi(&[&[SQUARE], &[SQUARE]]), vec![0], 100.0, "parts_overlap, even_odd"), + ( + "the same part twice, from another vertex", + multi(&[&[SQUARE], &[&[(10., 0.), (10., 10.), (0., 10.), (0., 0.), (10., 0.)]]]), + vec![0], + 100.0, + "parts_overlap, even_odd", + ), + // A ring's own area is read as the default reads it: traced twice, it counts once. + ("a ring traced twice", poly(&[twice]), vec![0], 100.0, "self_intersection, even_odd"), + ("a ring traced twice beside a part", multi(&[&[twice], &[small]]), vec![0, 0], 104.0, "self_intersection, even_odd"), + ] +} + +/// Shells Minus Holes, named, is the default exactly: the same output, coordinate for coordinate, +/// with the same repairs and notices, on every shape the default rebuilds and every shape the two +/// rules differ on. +#[test] +fn shells_minus_holes_named_is_the_default() { + let shapes = rebuilt().into_iter().map(|(_, g, _)| g).chain(rule_cases().into_iter().map(|(_, g, ..)| g)); + for g in shapes { + let mut l = layer(GeometryType::Polygon); + add(&mut l, Some(g.clone()), "x"); + let default = run(&l); + let named = rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": "Shells Minus Holes"})).unwrap(); + assert_eq!(dataset(&named).features[0].geometry, dataset(&default).features[0].geometry, "{g:?}"); + assert_eq!(rows(&named), rows(&default), "{g:?}"); + assert_eq!(named.messages, default.messages, "{g:?}"); + } +} + +/// Even-Odd (#2003): what lies inside an odd number of rings, each ring's own area first, in every +/// winding. A hole listed as a part stays a hole; overlaps are left out; duplicates are left out +/// first and said; a ring that runs back over itself keeps its area. The feature's repairs say the +/// rule, and one notice says how many features it rebuilt. +#[test] +fn even_odd_keeps_what_lies_inside_an_odd_number_of_rings() { + for (name, g, holes, want, code) in rule_cases() { + for (winding, g) in windings(&g) { + let (got, repairs, messages) = one_by("Even-Odd", g); + let got = got.unwrap_or_else(|| panic!("{name}, {winding}: no geometry ({repairs})")); + let (got_holes, got_area) = shape(&got); + assert_eq!(got_holes, holes, "{name}, {winding}: {got:?}"); + assert!((got_area - want).abs() < 1e-6, "{name}, {winding}: area {got_area}"); + assert_eq!(repairs, code, "{name}, {winding}"); + assert!(messages.iter().any(|m| m.contains("1 feature was rebuilt by the Even-Odd rule")), "{name}: {messages:?}"); + let dup = messages.iter().any(|m| m.contains("1 duplicate ring was left out")); + assert_eq!(dup, name.starts_with("the same part twice"), "{name}, {winding}: {messages:?}"); + } + } + // The hole listed as a part, exactly: the square with a hole where the ring was. + let (got, ..) = one_by("Even-Odd", multi(&[&[SQUARE], &[&[(2., 2.), (2., 4.), (4., 4.), (4., 2.), (2., 2.)]]])); + assert_eq!(got.as_ref().map(norm), Some(norm(&poly(&[SQUARE, &[(2., 2.), (2., 4.), (4., 4.), (4., 2.), (2., 2.)]])))); + // As under the default: a hole equal to its exterior ring leaves no area (they aren't of one + // role, so they aren't duplicates), and so does a ring with all its points on one line. + for g in [poly(&[SQUARE, SQUARE]), poly(&[&[(0., 0.), (5., 5.), (10., 10.), (0., 0.)]])] { + let (got, repairs, _) = one_by("Even-Odd", g.clone()); + assert_eq!((got, repairs.as_str()), (None, "no_area"), "{g:?}"); + } + // A feature valid as it is, or fixed without moving a coordinate, comes out the same under + // either rule; one the overlay rebuilds (the bowtie) says the rule. + for g in [poly(&[SQUARE, HOLE]), poly(&[&[(0., 0.), (10., 0.), (10., 10.), (0., 10.)]]), poly(&[BOWTIE])] { + let (got, repairs, messages) = one_by("Even-Odd", g.clone()); + let (want, default_repairs) = one(GeometryType::Polygon, Some(g.clone())); + assert_eq!(got.as_ref().map(norm), want.as_ref().map(norm), "{g:?}"); + let rebuilt = default_repairs.contains("self_intersection"); + assert_eq!(repairs.ends_with("even_odd"), rebuilt, "{g:?}: {repairs}"); + assert_eq!(messages.iter().any(|m| m.contains("Even-Odd")), rebuilt, "{g:?}"); + } +} + +/// The PostGIS manual's two examples under Even-Odd: 4 and 14 polygons, the counts the manual +/// shows for its "linework" method. +#[test] +fn even_odd_gives_the_postgis_manual_linework_counts() { + for ((rings, _), want) in postgis_examples().into_iter().zip([4, 14]) { + let owned: Vec> = rings.iter().map(|r| vec![r.as_slice()]).collect(); + let parts: Vec<&[Ring]> = owned.iter().map(Vec::as_slice).collect(); + let (got, repairs, _) = one_by("Even-Odd", multi(&parts)); + let Some(Geometry::Polygon(polys)) = got else { panic!("{repairs}") }; + assert_eq!(polys.len(), want, "{repairs}"); + } +} + +/// A rule the tool doesn't know is refused with its choices, not read as the default; any letter +/// case of a choice is the choice. +#[test] +fn an_unknown_rule_is_refused() { + let mut l = layer(GeometryType::Polygon); + add(&mut l, Some(poly(&[BOWTIE])), "x"); + let e = rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": "Odd-Even"})).unwrap_err(); + assert!(e.contains("Shells Minus Holes") && e.contains("Even-Odd"), "{e}"); + assert!(rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": "even-odd"})).is_ok()); +} diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index a0fc941e12..515f24e60a 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1268,6 +1268,28 @@ How each problem is repaired: - Lines may cross themselves, so only their repeated vertices and their parts with fewer than 2 distinct points are repaired. Points have nothing to repair but non-finite coordinates. +**Rule** (`rule`) chooses what the rebuild takes to be inside, when rings cross, overlap or sit in +the wrong place. A valid polygon, or one fixed without moving a coordinate, comes out the same +either way. + +- **Shells Minus Holes** (the default) is the rebuild above: rings keep the roles the data gives + them. +- **Even-Odd** reads each ring's own area the same way, so a bowtie keeps both triangles and a ring + traced twice counts once, then keeps what lies inside an odd number of rings, whatever their + roles. This matches the "linework" method of make-valid tools. Where two parts overlap is left + out, a ring listed as a part inside another is a hole, and what of a hole lies outside its + exterior ring is a part. It suits data whose ring roles may be wrong, such as a hole stored as a + part of its own: Shells Minus Holes fills it, Even-Odd keeps it a hole. Exact duplicate rings of + one role, an exterior of another exterior or a hole of another hole, are left out first, since + each pair would cancel out, and the tool says how many. Features rebuilt under Even-Odd have + `even_odd` in `repairs` after the codes. + +| Shape | Shells Minus Holes | Even-Odd | +| --- | --- | --- | +| Two overlapping squares | one merged polygon | two pieces, the overlap left out | +| A square with a ring listed as a part inside it | the square, filled | the square with a hole | +| A hole partly outside its square | the square with a notch | the notch, and the part outside as its own piece | + When a ring of a part crosses itself, Check Geometry doesn't check that part against its other rings or the other parts; Repair Geometry checks the feature again after the fixes that move no coordinate, and adds `hole_outside_shell`, `rings_intersect` and `parts_overlap` when the rebuild From 6bd2d072c5d4b9554a91122a3c4d70a7435d9aac Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:24:06 -0600 Subject: [PATCH 02/12] fix(analysis): Even-Odd finds duplicate rings in linear time and says what it did (#2003) A ring's normal form now starts at its least rotation by Booth's algorithm (K. S. Booth, Lexicographically least circular substrings, IPL 10(4-5), 1980), linear however often the ring passes one vertex: trying each pass as a start was quadratic, outside the work bound and Cancel. -0.0 and 0.0 read as the same point. Under Even-Odd the default's "merged" and "taken out" notices aren't given, the Even-Odd notice says what it keeps, and parts_overlap is said when the combining overlay left overlapping area out. The rule is read once and handed to the repair; the choices are one list; Even-Odd's memory is in the browser's estimate. --- crates/analysis/src/repair.rs | 145 +++++++++++++++---- crates/analysis/src/tools.rs | 2 +- crates/analysis/src/tools/repair_geometry.rs | 35 ++--- crates/analysis/tests/repair_geometry.rs | 20 +++ 4 files changed, 158 insertions(+), 44 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 2d93b82059..0d511dc849 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -29,6 +29,7 @@ //! of the browser before it runs. Natively a panic is caught, and the feature is left as it was. use crate::validity::{self, At, Checked, Code, Limit, Problem, Room}; +use geo::Area; use geo::algorithm::bool_ops::{BooleanOps, FillRule, OpType}; use geo::orient::{Direction, Orient}; use rustgis_data::{Coord, Geometry}; @@ -145,17 +146,25 @@ pub(crate) enum Rule { EvenOdd, } +/// The rules as Repair Geometry's `rule` parameter offers them, in [`Rule`]'s order: its choices +/// and [`Rule::named`] read this one list. +pub(crate) const RULES: [&str; 2] = ["Shells Minus Holes", "Even-Odd"]; + impl Rule { - /// The rule a tool parameter names, as its choices spell it. + /// The rule a tool parameter names, as [`RULES`] spells it. pub(crate) fn named(name: &str) -> Option { - match name { - "Shells Minus Holes" => Some(Rule::ShellsMinusHoles), - "Even-Odd" => Some(Rule::EvenOdd), - _ => None, + match RULES.iter().position(|r| *r == name)? { + 0 => Some(Rule::ShellsMinusHoles), + _ => Some(Rule::EvenOdd), } } } +/// The memory, per vertex, that [`Rule::EvenOdd`] holds besides the overlay's +/// ([`OVERLAY_BYTES`]): each ring's normal form, and while it is made, its two readings +/// ([`distinct_rings`]). +const EVEN_ODD_BYTES: u64 = 48; + /// Repairs one geometry under `rule`, spending at most [`validity::MAX_WORK`] on each check and on /// the overlay, and asking `has_room` whether the browser can give the overlay's memory before it /// runs (see `web_has_room` in the tools). @@ -353,7 +362,8 @@ fn polygon( if size.saturating_mul(OVERLAY_COST) > budget { return Outcome::Kept(Kept::Work); } - if !has_room(size.saturating_mul(OVERLAY_BYTES)) { + let per_vertex = if rule == Rule::EvenOdd { OVERLAY_BYTES + EVEN_ODD_BYTES } else { OVERLAY_BYTES }; + if !has_room(size.saturating_mul(per_vertex)) { return Outcome::Kept(Kept::Memory); } let Some(step) = grid_step(parts.iter().flatten().flatten().map(|c| (c.x, c.y))) else { return Outcome::Kept(Kept::Failed) }; @@ -605,18 +615,25 @@ fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) } } if pieces.len() > 1 { - // Pieces narrower than a step of the grid the combining overlay rounds to are dropped first. + // Pieces narrower than a step of the grid the combining overlay rounds to are dropped first + // (finding the step also tells the overlay can run on them). let step = on_grid(rings_of(&pieces))?; pieces = wider(pieces, step, &mut narrow, room)?; } let pieces = geo::MultiPolygon::new(pieces); + let mut found = Found::new(); let combined = if pieces.0.len() > 1 { - on_grid(rings_of(&pieces.0))?; - pieces.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::EvenOdd) + let combined = pieces.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::EvenOdd); + // Area left out where pieces overlapped: said as the checks would have said it (they don't + // relate a part with a ring crossing itself), in a tenth of a grid step's worth of area. + if parts.len() > 1 && combined.unsigned_area() < pieces.unsigned_area() * (1.0 - 1e-9) { + found.push(Code::PartsOverlap); + } + combined } else { pieces }; - Ok(Rebuilt { polys: polygons(&combined, room).map_err(kept_for)?, no_area, narrow, found: Found::new(), duplicates }) + Ok(Rebuilt { polys: polygons(&combined, room).map_err(kept_for)?, no_area, narrow, found, duplicates }) } /// The rings of `parts` to combine under [`Rule::EvenOdd`], by part and ring in their order, and @@ -650,34 +667,66 @@ fn distinct_rings(parts: &[Vec>], room: Room) -> Result<(Vec<(usize, Ok((keep, duplicates)) } -/// A closed ring's points as bits, without the closing repeat, from its least vertex (by x, then -/// y) and in whichever direction reads less: the same for the same ring whatever vertex it starts -/// at and whichever way it is wound. A vertex the ring passes more than once is tried as a start -/// each time. +/// A closed ring's points as bits, without the closing repeat: its least rotation (as a sequence +/// of points) in whichever direction reads less, the same for the same ring whatever vertex it +/// starts at and whichever way it is wound. In time and memory linear in the ring, however often +/// it passes one vertex ([`least_rotation`]). fn normal_form(ring: &[Coord], room: Room) -> Result, Limit> { let pts = match ring.split_last() { Some((last, rest)) if Some(last) == rest.first() => rest, _ => ring, }; - let bits = |c: &Coord| (c.x.to_bits(), c.y.to_bits()); let n = pts.len(); - let least = pts - .iter() - .map(|c| (c.x, c.y)) - .fold((f64::INFINITY, f64::INFINITY), |m, c| if c.0.total_cmp(&m.0).then(c.1.total_cmp(&m.1)).is_lt() { c } else { m }); + // Plus 0.0 makes -0.0 the same as 0.0: the same point, which writers and reprojection give either way. + let forward = room.collect(n, pts.iter().map(|c| ((c.x + 0.0).to_bits(), (c.y + 0.0).to_bits())))?; + let backward = room.collect(n, forward.iter().rev().copied())?; let mut best: Option> = None; - for (start, _) in pts.iter().enumerate().filter(|(_, c)| (c.x, c.y) == least) { - for forward in [true, false] { - let at = |k: usize| if forward { (start + k) % n } else { (start + n - k % n) % n }; - let form = room.collect(n, (0..n).filter_map(|k| pts.get(at(k)).map(bits)))?; - if best.as_ref().is_none_or(|b| form < *b) { - best = Some(form); - } + for seq in [forward, backward] { + let start = least_rotation(&seq, room)?; + let form = room.collect(n, seq.iter().skip(start).chain(seq.iter().take(start)).copied())?; + if best.as_ref().is_none_or(|b| form < *b) { + best = Some(form); } } Ok(best.unwrap_or_default()) } +/// Where the lexicographically least rotation of `s` starts, by Booth's algorithm in linear time +/// (K. S. Booth, "Lexicographically least circular substrings", Information Processing Letters +/// 10(4–5), 1980, 240–242): a failure function over `s` read twice round, as Knuth–Morris–Pratt +/// matching uses one. 0 for an empty `s`. +fn least_rotation(s: &[(u64, u64)], room: Room) -> Result { + let n = s.len(); + let len = n.saturating_mul(2); + // f[j]: the failure function's value at j, one more than the index it points to (0 for none), + // so it stays unsigned. + let mut f: Vec = room.collect(len, std::iter::repeat_n(0, len))?; + let at = |i: usize| s.get(i % n.max(1)).copied().unwrap_or_default(); + let mut k = 0usize; + for j in 1..len { + let sj = at(j); + let mut i = f.get(j.saturating_sub(k + 1)).copied().unwrap_or(0); + while i != 0 && sj != at(k + i) { + if sj < at(k + i) { + k = j.saturating_sub(i); + } + i = f.get(i.saturating_sub(1)).copied().unwrap_or(0); + } + if sj != at(k + i) { + // i == 0 here: no proper prefix matches. + if sj < at(k) { + k = j; + } + if let Some(v) = f.get_mut(j.saturating_sub(k)) { + *v = 0; + } + } else if let Some(v) = f.get_mut(j.saturating_sub(k)) { + *v = i + 1; + } + } + Ok(k % n.max(1)) +} + /// The rebuild by area (see the module doc) of `parts`, closed rings each with 3 distinct points /// or more, of which those in `crossing` (part, ring; sorted) cross, touch or run back over /// themselves. The rings of the result, how many rings and parts were left without area, how many @@ -800,6 +849,50 @@ mod tests { repair_by_with(g, Rule::default(), budget, allow, has_room) } + /// Booth's least rotation agrees with trying every rotation, on every sequence of up to 8 + /// symbols from 3, repeats and all. + #[test] + fn least_rotation_is_the_least_of_all_rotations() { + let room = Room(&|_| true); + for n in 1..=8usize { + for code in 0..3usize.pow(n as u32) { + let s: Vec<(u64, u64)> = (0..n).map(|k| ((code / 3usize.pow(k as u32) % 3) as u64, 0)).collect(); + let rotation = |start: usize| -> Vec<(u64, u64)> { s[start..].iter().chain(&s[..start]).copied().collect() }; + let least = (0..n).map(rotation).min().unwrap(); + assert_eq!(rotation(least_rotation(&s, room).unwrap()), least, "{s:?}"); + } + } + assert_eq!(least_rotation(&[], room).unwrap(), 0); + } + + /// The same ring has one normal form from any vertex, in either direction, and a different ring + /// another; a ring passing one vertex 100,000 times takes linear time, where trying each pass as + /// a start would take about 10^10 steps. + #[test] + fn a_ring_has_one_normal_form() { + let room = Room(&|_| true); + let square: Vec = ring(SQUARE); + let want = normal_form(&square, room).unwrap(); + let open = &square[..square.len() - 1]; + for start in 0..open.len() { + for reversed in [false, true] { + let mut r: Vec = open[start..].iter().chain(&open[..start]).copied().collect(); + if reversed { + r.reverse(); + } + r.push(r[0]); + assert_eq!(normal_form(&r, room).unwrap(), want, "from {start}, reversed {reversed}"); + } + } + assert_ne!(normal_form(&ring(&[(0., 0.), (10., 0.), (10., 11.), (0., 10.), (0., 0.)]), room).unwrap(), want); + let mut star = vec![Coord::new(0., 0.)]; + for k in 0..100_000 { + let a = f64::from(k) * 1e-5; + star.extend([Coord::new(a.cos(), a.sin()), Coord::new(0., 0.)]); + } + assert_eq!(normal_form(&star, room).unwrap().len(), star.len() - 1); + } + type Ring<'a> = &'a [(f64, f64)]; /// Every reservation tried. diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 36692ee353..4f53ee158e 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -103,7 +103,7 @@ pub static TOOLS: &[ToolSpec] = &[ P { name: "rule", label: "Rule", - kind: K::Choice(&["Shells Minus Holes", "Even-Odd"]), + kind: K::Choice(&crate::repair::RULES), required: false, default: "\"Shells Minus Holes\"", help: "What is inside a polygon whose rings cross, overlap or sit in the wrong place. Shells Minus Holes: each part is its outer ring less its holes, and overlapping parts merge. Even-Odd: what lies inside an odd number of rings, so where two parts overlap is left out and a ring listed as a part inside another is a hole. Valid polygons are written the same either way.", diff --git a/crates/analysis/src/tools/repair_geometry.rs b/crates/analysis/src/tools/repair_geometry.rs index ae37d16aee..f04f82b73b 100644 --- a/crates/analysis/src/tools/repair_geometry.rs +++ b/crates/analysis/src/tools/repair_geometry.rs @@ -12,8 +12,7 @@ const TOOL: &str = "Repair Geometry"; /// valid ones as stored, repaired ones with their new geometry, those that can't be repaired /// without geometry, and those left as they were (too complex, or the repair failed) as stored. pub(super) fn run(inp: &dyn Inputs, p: &Json) -> Result { - let rule = rule_of(p)?; - run_with_room(inp, p, &web_has_room, |g| repair::repair_by(g, rule, &web_has_room)) + run_with_room(inp, p, &web_has_room, |g, rule| repair::repair_by(g, rule, &web_has_room)) } /// The rule `p` names (#2003), Shells Minus Holes when it names none. @@ -90,7 +89,7 @@ fn run_with_room( inp: &dyn Inputs, p: &Json, has_room: &dyn Fn(u64) -> bool, - repair: impl Fn(Option<&Geometry>) -> Outcome, + repair: impl Fn(Option<&Geometry>, Rule) -> Outcome, ) -> Result { let rule = rule_of(p)?; let ds = input(inp, p, "input")?; @@ -124,7 +123,7 @@ fn run_with_room( crate::control::reached(i); let Some(f) = ds.features.get(*row) else { continue }; tally.extra += usize::from(f.attrs.len() > width); - let (repairs, geometry) = match repair(f.geometry.as_ref()) { + let (repairs, geometry) = match repair(f.geometry.as_ref(), rule) { Outcome::Valid => (String::new(), Written::Same), Outcome::Repaired(r) => { tally.repaired += 1; @@ -220,7 +219,7 @@ fn run_with_room( } out.features = list.into(); - let mut messages = notices(&tally, &field); + let mut messages = notices(&tally, &field, rule); messages.extend(notes); Ok(ToolResult { output: Output::Layer(out), messages }) } @@ -229,7 +228,7 @@ fn run_with_room( /// and per kind of change beyond the codes: pieces dropped (with too few points or no area, or /// narrower than a step of the overlay's grid in any direction), holes taken out, parts merged or /// split, coordinates moved. `field` is the output's repairs field. -fn notices(t: &Tally, field: &str) -> Vec { +fn notices(t: &Tally, field: &str, rule: Rule) -> Vec { let mut messages = vec![summary(t)]; let is = |n: usize| if n == 1 { "is" } else { "are" }; let was = |n: usize| if n == 1 { "was" } else { "were" }; @@ -287,15 +286,17 @@ fn notices(t: &Tally, field: &str) -> Vec { Gone::NoArea.as_str() )); } + // What Shells Minus Holes does with a hole outside its ring and with overlapping parts; Even-Odd + // does otherwise, and its own notice says what. let holes = t.count(Code::HoleOutsideShell); - if holes > 0 { + if holes > 0 && rule == Rule::ShellsMinusHoles { messages.push(format!( "{TOOL}: holes not inside their exterior ring were taken out of {}; what of a hole is inside its exterior ring stays a hole", features(holes) )); } let merged = t.count(Code::PartsOverlap); - if merged > 0 { + if merged > 0 && rule == Rule::ShellsMinusHoles { messages.push(format!("{TOOL}: the parts that overlapped or touched along a line were merged in {}", features(merged))); } let split = t.count(Code::DisconnectedInterior); @@ -316,7 +317,7 @@ fn notices(t: &Tally, field: &str) -> Vec { } if t.even_odd > 0 { messages.push(format!( - "{TOOL}: {} {} rebuilt by the Even-Odd rule, with {EVEN_ODD} in {field}: where their rings overlap is left out, and a ring inside another is a hole, whatever its role", + "{TOOL}: {} {} rebuilt by the Even-Odd rule, with {EVEN_ODD} in {field}: each keeps what lies inside an odd number of its rings, whatever their roles, so where rings overlap is left out, a ring inside another is a hole, and what of a hole lies outside its exterior ring is a part", features(t.even_odd), was(t.even_odd) )); @@ -404,7 +405,7 @@ mod tests { asked.set(need); true }, - |g| repair::repair_by(g, Rule::default(), &|_| true), + |g, rule| repair::repair_by(g, rule, &|_| true), ) .unwrap(); let Output::Layer(out) = &r.output else { panic!("a layer") }; @@ -417,7 +418,7 @@ mod tests { let first = point_bytes(&inp.0.features[0].attrs, 2) + "ring_not_closed".len() as u64 + ALLOCATION_OVERHEAD + 2 * geometry; let second = point_bytes(&inp.0.features[1].attrs, 2) + "null_geometry".len() as u64 + ALLOCATION_OVERHEAD; assert_eq!(asked.get(), first + second); - let e = run_with_room(&inp, &p, &|_| false, |g| repair::repair_by(g, Rule::default(), &|_| true)).err().unwrap(); + let e = run_with_room(&inp, &p, &|_| false, |g, rule| repair::repair_by(g, rule, &|_| true)).err().unwrap(); assert_eq!(e, "Repair Geometry: the 2 features would need about 3 MiB, more than the browser can give now: select fewer features"); } @@ -427,7 +428,7 @@ mod tests { fn features_left_as_they_were_are_counted_by_reason() { let t = Tally { processed: 9, failed: 1, ..Tally::default() }; assert_eq!( - notices(&t, "repairs"), + notices(&t, "repairs", Rule::default()), [ "Repair Geometry: repaired 0 of 9 features", "Repair Geometry: 1 feature couldn't be repaired, because geo's geometry operations failed on it, and is written unchanged, with repair_failed in repairs", @@ -435,14 +436,14 @@ mod tests { ); let t = Tally { processed: 9, work: 2, memory: 3, failed: 2, ..Tally::default() }; assert_eq!( - notices(&t, "repairs_2")[1..], + notices(&t, "repairs_2", Rule::default())[1..], [ "Repair Geometry: 2 features were too complex to repair within the work limit and are written unchanged, with too_complex in repairs_2", "Repair Geometry: 3 features couldn't be repaired, because there wasn't enough memory to repair them, and are written unchanged, with too_complex in repairs_2", "Repair Geometry: 2 features couldn't be repaired, because geo's geometry operations failed on them, and are written unchanged, with repair_failed in repairs_2", ] ); - assert_eq!(notices(&Tally::default(), "repairs"), ["Repair Geometry: no features to repair"]); + assert_eq!(notices(&Tally::default(), "repairs", Rule::default()), ["Repair Geometry: no features to repair"]); } /// A cancel stops the repairs at the next feature, and the copies at the next row. @@ -457,10 +458,10 @@ mod tests { let control = crate::control::Control::new(); let _installed = crate::control::install(Some(control.clone())); let repaired = std::cell::Cell::new(0); - let r = run_with_room(&inp, &p, &|_| true, |g| { + let r = run_with_room(&inp, &p, &|_| true, |g, rule| { repaired.set(repaired.get() + 1); control.cancel(); - repair::repair_by(g, Rule::default(), &|_| true) + repair::repair_by(g, rule, &|_| true) }); assert_eq!(r.err().as_deref(), Some(crate::control::CANCELLED)); assert_eq!(repaired.get(), 1, "stopped before the second feature"); @@ -474,7 +475,7 @@ mod tests { control.cancel(); true }, - |g| repair::repair_by(g, Rule::default(), &|_| true), + |g, rule| repair::repair_by(g, rule, &|_| true), ); assert_eq!(r.err().as_deref(), Some(crate::control::CANCELLED), "stopped in the copies"); } diff --git a/crates/analysis/tests/repair_geometry.rs b/crates/analysis/tests/repair_geometry.rs index bcd15a8bc8..9081ce8167 100644 --- a/crates/analysis/tests/repair_geometry.rs +++ b/crates/analysis/tests/repair_geometry.rs @@ -1246,3 +1246,23 @@ fn an_unknown_rule_is_refused() { assert!(e.contains("Shells Minus Holes") && e.contains("Even-Odd"), "{e}"); assert!(rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": "even-odd"})).is_ok()); } + +/// Under Even-Odd the notices say what Even-Odd did: not the default's "merged" or "taken out"; +/// parts that overlap are said even when a part's ring crosses itself (which keeps the checks from +/// relating it); and a duplicate written with -0.0 where the other has 0.0 is the same ring. +#[test] +fn even_odd_says_what_it_did() { + let (_, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[OVERLAPPING]])); + assert_eq!(repairs, "parts_overlap, even_odd"); + assert!(!messages.iter().any(|m| m.contains("were merged") || m.contains("were taken out")), "{messages:?}"); + // A bowtie part with a square overlapping one lobe: the overlap is left out and said. + let lobes: Ring = &[(0., 0.), (4., 4.), (4., 0.), (0., 4.), (0., 0.)]; + let (got, repairs, _) = one_by("Even-Odd", multi(&[&[lobes], &[&[(3., 1.), (6., 1.), (6., 3.), (3., 3.), (3., 1.)]]])); + assert!(repairs.contains("parts_overlap") && repairs.contains("self_intersection"), "{repairs}"); + assert!(got.is_some(), "{repairs}"); + // -0.0 and 0.0: one ring, left out once as a duplicate rather than canceled. + let neg: Ring = &[(-0.0, 0.), (10., 0.), (10., 10.), (-0.0, 10.), (-0.0, 0.)]; + let (got, _, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[neg]])); + assert_eq!(got.map(|g| area(&g)), Some(100.0)); + assert!(messages.iter().any(|m| m.contains("1 duplicate ring was left out")), "{messages:?}"); +} From 7b7855cf677d35a0cd561271ff3fba6212b7c358 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:49:02 -0600 Subject: [PATCH 03/12] fix(analysis): Even-Odd tells overlapping parts by their exterior rings, and holds less (#2003) parts_overlap under Even-Odd now compares only parts' exterior rings, with rounding's share of area (the rings' length times a grid step) allowed, so a part's own hole or parts sharing an edge aren't taken for an overlap; a part left out as another's duplicate is one. Only rings of one role and one length get a normal form, so a polygon whose rings can have no duplicate costs none, and Even-Odd's memory estimate covers what finding duplicates holds at most. The overlay's grid is checked on the pieces it combines. A rule named without a rule to run doesn't compile. The Assistant's geoprocessing skill names the rule, and the guide's table says what Even-Odd gives a hole partly outside its square. --- crates/analysis/src/repair.rs | 126 +++++++++++++----- crates/analysis/src/tools/repair_geometry.rs | 5 +- crates/analysis/tests/repair_geometry.rs | 10 ++ .../assistant/skills/geoprocessing/SKILL.md | 5 +- docs/user-guide/analysis.md | 2 +- 5 files changed, 108 insertions(+), 40 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 0d511dc849..09b09a5085 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -146,24 +146,26 @@ pub(crate) enum Rule { EvenOdd, } -/// The rules as Repair Geometry's `rule` parameter offers them, in [`Rule`]'s order: its choices -/// and [`Rule::named`] read this one list. +/// The rules as Repair Geometry's `rule` parameter offers them: its choices and [`Rule::named`] +/// read this one list, and [`RULE_OF`] gives the rule of each, in the same order. pub(crate) const RULES: [&str; 2] = ["Shells Minus Holes", "Even-Odd"]; +/// The rule each of [`RULES`] names. +const RULE_OF: [Rule; 2] = [Rule::ShellsMinusHoles, Rule::EvenOdd]; +// A rule named without a rule to run, or the other way round, doesn't compile. +const _: () = assert!(RULES.len() == RULE_OF.len()); impl Rule { /// The rule a tool parameter names, as [`RULES`] spells it. pub(crate) fn named(name: &str) -> Option { - match RULES.iter().position(|r| *r == name)? { - 0 => Some(Rule::ShellsMinusHoles), - _ => Some(Rule::EvenOdd), - } + RULES.iter().position(|r| *r == name).and_then(|i| RULE_OF.get(i).copied()) } } /// The memory, per vertex, that [`Rule::EvenOdd`] holds besides the overlay's -/// ([`OVERLAY_BYTES`]): each ring's normal form, and while it is made, its two readings -/// ([`distinct_rings`]). -const EVEN_ODD_BYTES: u64 = 48; +/// ([`OVERLAY_BYTES`]), at most, while [`distinct_rings`] finds duplicates: each ring's normal form +/// kept (16), and while one is made its two readings (32), Booth's failure table (16), a rotation +/// (16) and the least so far (16). +const EVEN_ODD_BYTES: u64 = 96; /// Repairs one geometry under `rule`, spending at most [`validity::MAX_WORK`] on each check and on /// the overlay, and asking `has_room` whether the browser can give the overlay's memory before it @@ -596,11 +598,14 @@ fn ring_area(ring: &[Coord], at: (usize, usize), crossing: &[(usize, usize)], ro /// more; `crossing` as in [`ring_area`]): exact duplicate rings of one role dropped /// ([`distinct_rings`]), each ring's own area ([`ring_area`]), pieces narrower than a step of the /// overlay's grid dropped and counted as [`by_area`] drops them, then the pieces combined by -/// parity in one overlay. No codes are found beyond the checks': the rings' roles, which they -/// would be about, don't decide the result. +/// parity in one overlay. Beyond the checks' codes it finds one: [`Code::PartsOverlap`] when parts' +/// exterior rings overlap (the checks don't relate a part with a ring crossing itself), told by +/// [`parts_overlap`]. fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) -> Result { - let (keep, duplicates) = distinct_rings(parts, room).map_err(kept_for)?; + let Distinct { keep, duplicates, exterior_duplicates } = distinct_rings(parts, room).map_err(kept_for)?; let mut pieces: Vec> = Vec::new(); + // The exterior rings' own areas, kept apart to tell whether parts overlap. + let mut exteriors: Vec> = Vec::new(); let mut no_area = 0usize; let mut narrow = 0usize; for (p, r) in keep { @@ -610,10 +615,19 @@ fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) no_area += 1; } for piece in area.0 { + if r == 0 && parts.len() > 1 { + room.grow(&mut exteriors).map_err(kept_for)?; + exteriors.push(piece.clone()); + } room.grow(&mut pieces).map_err(kept_for)?; pieces.push(piece); } } + let mut found = Found::new(); + // A part left out as another's duplicate overlapped it all. + if exterior_duplicates > 0 || parts_overlap(&exteriors)? { + found.push(Code::PartsOverlap); + } if pieces.len() > 1 { // Pieces narrower than a step of the grid the combining overlay rounds to are dropped first // (finding the step also tells the overlay can run on them). @@ -621,50 +635,88 @@ fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) pieces = wider(pieces, step, &mut narrow, room)?; } let pieces = geo::MultiPolygon::new(pieces); - let mut found = Found::new(); let combined = if pieces.0.len() > 1 { - let combined = pieces.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::EvenOdd); - // Area left out where pieces overlapped: said as the checks would have said it (they don't - // relate a part with a ring crossing itself), in a tenth of a grid step's worth of area. - if parts.len() > 1 && combined.unsigned_area() < pieces.unsigned_area() * (1.0 - 1e-9) { - found.push(Code::PartsOverlap); - } - combined + // The pieces left must be on a grid the overlay can represent too: in the browser a panic + // in it can't be caught. + on_grid(rings_of(&pieces.0))?; + pieces.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::EvenOdd) } else { pieces }; Ok(Rebuilt { polys: polygons(&combined, room).map_err(kept_for)?, no_area, narrow, found, duplicates }) } -/// The rings of `parts` to combine under [`Rule::EvenOdd`], by part and ring in their order, and -/// how many were left out as exact duplicates of an earlier ring of the same role: an exterior ring -/// of another exterior ring, a hole of another hole (of any part). The same ring is the same points -/// in the same cyclic order, from any vertex and in either direction. A ring isn't compared with one -/// of the other role: a hole equal to its exterior ring leaves its part no area, as it does under -/// [`Rule::ShellsMinusHoles`]. Found by sorting each ring's normal form, in buffers reserved -/// fallibly. -fn distinct_rings(parts: &[Vec>], room: Room) -> Result<(Vec<(usize, usize)>, usize), Limit> { +/// Whether the areas of parts' exterior rings overlap, by an area more than rounding to the +/// overlay's grid could take: combined by parity, overlaps drop out, and rounding moves each edge +/// by at most half a step on each axis, so it changes the area by less than the rings' length +/// times a step. Holes play no part, so a hole in its own part isn't taken for an overlap. +fn parts_overlap(exteriors: &[geo::Polygon]) -> Result { + if exteriors.len() < 2 { + return Ok(false); + } + let step = on_grid(rings_of(exteriors))?; + let each: f64 = exteriors.iter().map(Area::unsigned_area).sum(); + let combined = geo::MultiPolygon::new(exteriors.to_vec()).boolean_op_with_fill_rule( + &geo::MultiPolygon::new(vec![]), + OpType::Union, + FillRule::EvenOdd, + ); + let length: f64 = rings_of(exteriors).map(|r| r.lines().map(|l| (l.end.x - l.start.x).hypot(l.end.y - l.start.y)).sum::()).sum(); + Ok(each - combined.unsigned_area() > length * step) +} + +/// What [`distinct_rings`] keeps of a polygon's rings. +struct Distinct { + /// The rings to combine, by part and ring, in their order. + keep: Vec<(usize, usize)>, + /// How many were left out as duplicates, and how many of those were exterior rings. + duplicates: usize, + exterior_duplicates: usize, +} + +/// The rings of `parts` to combine under [`Rule::EvenOdd`], leaving out exact duplicates of an +/// earlier ring of the same role: an exterior ring of another exterior ring, a hole of another +/// hole (of any part). The same ring is the same points in the same cyclic order, from any vertex +/// and in either direction ([`normal_form`]). A ring isn't compared with one of the other role: a +/// hole equal to its exterior ring leaves its part no area, as it does under +/// [`Rule::ShellsMinusHoles`]. Only rings of one role and one length are compared, so a polygon +/// whose rings can have no duplicate costs no normal form. In buffers reserved fallibly. +fn distinct_rings(parts: &[Vec>], room: Room) -> Result { let n = parts.iter().map(Vec::len).fold(0usize, usize::saturating_add); - // (whether a hole, the ring's normal form, part, ring) - let mut all: Vec<(bool, Vec<(u64, u64)>, usize, usize)> = room.vec(n)?; + // (whether a hole, its length, part, ring), sorted so rings that may be the same are together. + let mut all: Vec<(bool, usize, usize, usize)> = room.vec(n)?; for (p, rings) in parts.iter().enumerate() { for (r, ring) in rings.iter().enumerate() { - all.push((r > 0, normal_form(ring, room)?, p, r)); + all.push((r > 0, ring.len(), p, r)); + } + } + all.sort_unstable(); + let mut keep: Vec<(usize, usize)> = room.vec(n)?; + // (whether a hole, the normal form, part, ring) of the rings that share a role and a length. + let mut keyed: Vec<(bool, Vec<(u64, u64)>, usize, usize)> = Vec::new(); + for group in all.chunk_by(|a, b| (a.0, a.1) == (b.0, b.1)) { + for &(hole, _, p, r) in group { + if group.len() == 1 { + keep.push((p, r)); + } else if let Some(ring) = parts.get(p).and_then(|rings| rings.get(r)) { + room.grow(&mut keyed)?; + keyed.push((hole, normal_form(ring, room)?, p, r)); + } } } - all.sort_unstable_by(|a, b| (a.0, &a.1, a.2, a.3).cmp(&(b.0, &b.1, b.2, b.3))); - let mut keep: Vec<(usize, usize)> = room.vec(all.len())?; - let mut duplicates = 0usize; - for (i, (hole, form, p, r)) in all.iter().enumerate() { + keyed.sort_unstable(); + let (mut duplicates, mut exterior_duplicates) = (0usize, 0usize); + for (i, (hole, form, p, r)) in keyed.iter().enumerate() { // Sorted, so a duplicate follows the first of its kind, which has the lowest part and ring. - if i > 0 && all.get(i - 1).is_some_and(|prev| prev.0 == *hole && prev.1 == *form) { + if i > 0 && keyed.get(i - 1).is_some_and(|prev| prev.0 == *hole && prev.1 == *form) { duplicates += 1; + exterior_duplicates += usize::from(!hole); } else { keep.push((*p, *r)); } } keep.sort_unstable(); - Ok((keep, duplicates)) + Ok(Distinct { keep, duplicates, exterior_duplicates }) } /// A closed ring's points as bits, without the closing repeat: its least rotation (as a sequence diff --git a/crates/analysis/src/tools/repair_geometry.rs b/crates/analysis/src/tools/repair_geometry.rs index f04f82b73b..0d7f928f7c 100644 --- a/crates/analysis/src/tools/repair_geometry.rs +++ b/crates/analysis/src/tools/repair_geometry.rs @@ -19,7 +19,10 @@ pub(super) fn run(inp: &dyn Inputs, p: &Json) -> Result { fn rule_of(p: &Json) -> Result { match p.get("rule") { None | Some(Json::Null) => Ok(Rule::default()), - Some(v) => v.as_str().and_then(Rule::named).ok_or_else(|| format!("{TOOL}: rule is \"Shells Minus Holes\" or \"Even-Odd\"")), + Some(v) => v + .as_str() + .and_then(Rule::named) + .ok_or_else(|| format!("{TOOL}: rule is one of {}", repair::RULES.map(|r| format!("\"{r}\"")).join(", "))), } } diff --git a/crates/analysis/tests/repair_geometry.rs b/crates/analysis/tests/repair_geometry.rs index 9081ce8167..3b1bd42765 100644 --- a/crates/analysis/tests/repair_geometry.rs +++ b/crates/analysis/tests/repair_geometry.rs @@ -1260,6 +1260,16 @@ fn even_odd_says_what_it_did() { let (got, repairs, _) = one_by("Even-Odd", multi(&[&[lobes], &[&[(3., 1.), (6., 1.), (6., 3.), (3., 3.), (3., 1.)]]])); assert!(repairs.contains("parts_overlap") && repairs.contains("self_intersection"), "{repairs}"); assert!(got.is_some(), "{repairs}"); + // A part's own hole is no overlap: a square with a hole, and a bowtie part well apart, which + // sends the feature to the overlay. + let far: Ring = &[(20., 0.), (22., 2.), (22., 0.), (20., 2.), (20., 0.)]; + let (_, repairs, _) = one_by("Even-Odd", multi(&[&[SQUARE, HOLE], &[far]])); + assert_eq!(repairs, "self_intersection, even_odd"); + // Parts sharing an edge, rounded to the grid, aren't taken for overlapping either (they touch + // along a line, which the checks say as such). + let side: Ring = &[(10., 0.), (20., 0.), (20., 10.), (10., 10.), (10., 0.)]; + let (_, repairs, _) = one_by("Even-Odd", multi(&[&[SQUARE], &[side], &[far]])); + assert!(repairs.contains("even_odd"), "{repairs}"); // -0.0 and 0.0: one ring, left out once as a duplicate rather than canceled. let neg: Ring = &[(-0.0, 0.), (10., 0.), (10., 10.), (-0.0, 10.), (-0.0, 0.)]; let (got, _, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[neg]])); diff --git a/crates/assistant/skills/geoprocessing/SKILL.md b/crates/assistant/skills/geoprocessing/SKILL.md index 8f3886a362..dfda3f2190 100644 --- a/crates/assistant/skills/geoprocessing/SKILL.md +++ b/crates/assistant/skills/geoprocessing/SKILL.md @@ -31,7 +31,10 @@ Based on the RustGIS user guide, Analysis › Geoprocessing tools repaired; features with a missing or non-finite coordinate, even in a hole, or left with no usable points or, for polygons, no area once repaired are written without geometry; valid points and lines are kept; other features too complex to check, or whose repair fails, - are written unchanged) + are written unchanged; optional `rule`: "Shells Minus Holes", the default, merges + overlapping parts and fills a ring listed as a part, or "Even-Odd", which keeps what lies + inside an odd number of rings, so overlaps are left out and a hole stored as a part stays a + hole) - selection: `selectByLocation` (`input`, `select`, `distance`, `invert`), `selectByAttributes` (`input`, `where`) - statistics: `countPoints` (Summarize Within: `polygons`, `points`, `statistics`: summary diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 515f24e60a..9d169bc7ef 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1288,7 +1288,7 @@ either way. | --- | --- | --- | | Two overlapping squares | one merged polygon | two pieces, the overlap left out | | A square with a ring listed as a part inside it | the square, filled | the square with a hole | -| A hole partly outside its square | the square with a notch | the notch, and the part outside as its own piece | +| A hole partly outside its square | the square with a notch | the square with a notch, and the part outside as its own piece | When a ring of a part crosses itself, Check Geometry doesn't check that part against its other rings or the other parts; Repair Geometry checks the feature again after the fixes that move no From 2234f120f5a6700881d499596cb4835b9239a581 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:42:05 -0600 Subject: [PATCH 04/12] fix(analysis): Even-Odd combines each part first, then the parts (#2003) Even-Odd now builds as Shells Minus Holes does, part by part and then the parts together, with parity in place of shells minus holes. A hole two parts both have stays out of both (leaving one out as a duplicate filled it); only a hole repeated in its own part, or a part repeated whole, is left out as a duplicate. A part whose exterior ring has no area is dropped with its holes. A part with a ring crossing itself gets the same hole_outside_shell and rings_intersect checks as under the default, and parts_overlap comes from the parts' combination, so an island in another part's hole isn't taken for an overlap. Parts joined along a line are said. The rules are one table of names and rules. --- crates/analysis/src/repair.rs | 301 ++++++++++++------- crates/analysis/src/tools/repair_geometry.rs | 6 + crates/analysis/tests/repair_geometry.rs | 35 +++ docs/user-guide/analysis.md | 6 +- 4 files changed, 233 insertions(+), 115 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 09b09a5085..409ca1d574 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -29,9 +29,9 @@ //! of the browser before it runs. Natively a panic is caught, and the feature is left as it was. use crate::validity::{self, At, Checked, Code, Limit, Problem, Room}; -use geo::Area; use geo::algorithm::bool_ops::{BooleanOps, FillRule, OpType}; use geo::orient::{Direction, Orient}; +use geo::{Area, Euclidean, Length}; use rustgis_data::{Coord, Geometry}; /// Work per vertex the overlay takes in or makes, against the same bound as a check @@ -146,18 +146,15 @@ pub(crate) enum Rule { EvenOdd, } -/// The rules as Repair Geometry's `rule` parameter offers them: its choices and [`Rule::named`] -/// read this one list, and [`RULE_OF`] gives the rule of each, in the same order. -pub(crate) const RULES: [&str; 2] = ["Shells Minus Holes", "Even-Odd"]; -/// The rule each of [`RULES`] names. -const RULE_OF: [Rule; 2] = [Rule::ShellsMinusHoles, Rule::EvenOdd]; -// A rule named without a rule to run, or the other way round, doesn't compile. -const _: () = assert!(RULES.len() == RULE_OF.len()); +/// Each rule by the name Repair Geometry's `rule` parameter gives it, in the order it offers them. +const NAMED: [(&str, Rule); 2] = [("Shells Minus Holes", Rule::ShellsMinusHoles), ("Even-Odd", Rule::EvenOdd)]; +/// The names of [`NAMED`], as the parameter's choices. +pub(crate) const RULES: [&str; 2] = [NAMED[0].0, NAMED[1].0]; impl Rule { - /// The rule a tool parameter names, as [`RULES`] spells it. + /// The rule a tool parameter names, as [`NAMED`] spells it. pub(crate) fn named(name: &str) -> Option { - RULES.iter().position(|r| *r == name).and_then(|i| RULE_OF.get(i).copied()) + NAMED.iter().find(|(n, _)| *n == name).map(|(_, rule)| *rule) } } @@ -595,128 +592,219 @@ fn ring_area(ring: &[Coord], at: (usize, usize), crossing: &[(usize, usize)], ro } /// The rebuild under [`Rule::EvenOdd`] of `parts` (closed rings, each with 3 distinct points or -/// more; `crossing` as in [`ring_area`]): exact duplicate rings of one role dropped -/// ([`distinct_rings`]), each ring's own area ([`ring_area`]), pieces narrower than a step of the -/// overlay's grid dropped and counted as [`by_area`] drops them, then the pieces combined by -/// parity in one overlay. Beyond the checks' codes it finds one: [`Code::PartsOverlap`] when parts' -/// exterior rings overlap (the checks don't relate a part with a ring crossing itself), told by -/// [`parts_overlap`]. +/// more; `crossing` as in [`ring_area`]), built as [`by_area`] builds its own, part by part and then +/// the parts together, with parity in place of shells minus holes: +/// - exact duplicates left out first ([`distinct_rings`]): a hole repeated in its part, and a part +/// repeated whole, which parity would otherwise cancel out; +/// - each ring's own area ([`ring_area`]); a part whose exterior ring has none is left out with its +/// holes, as [`by_area`] leaves it; +/// - a part's holes narrower than a step of the overlay's grid dropped, then the part's rings +/// combined by parity, so its holes' overlaps come back and a hole partly outside its ring adds +/// that part as area; +/// - the parts, the narrow ones dropped, combined by parity, so where parts overlap is left out and +/// parts meeting along a line are joined. +/// +/// Besides the checks' codes it finds those the checks can't tell of a part with a ring crossing +/// itself ([`unrelated_hole_codes`]), and [`Code::PartsOverlap`] when a part was another's +/// duplicate, or when the parts' combination makes fewer polygons than it was given (parts joined) +/// or less area than they had, by more than rounding to the grid could take: it moves each edge by +/// at most half a step on each axis, so changes the area by less than the rings' length times a +/// step. fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) -> Result { - let Distinct { keep, duplicates, exterior_duplicates } = distinct_rings(parts, room).map_err(kept_for)?; - let mut pieces: Vec> = Vec::new(); - // The exterior rings' own areas, kept apart to tell whether parts overlap. - let mut exteriors: Vec> = Vec::new(); + let empty = geo::MultiPolygon::::new(vec![]); + let Distinct { keep, duplicates, duplicate_parts } = distinct_rings(parts, room).map_err(kept_for)?; + let mut all: Vec> = Vec::new(); let mut no_area = 0usize; let mut narrow = 0usize; - for (p, r) in keep { - let Some(ring) = parts.get(p).and_then(|rings| rings.get(r)) else { continue }; - let area = ring_area(ring, (p, r), crossing, room)?; - if area.0.is_empty() { + let mut found = Found::new(); + if duplicate_parts > 0 { + found.push(Code::PartsOverlap); + } + // `keep` is by part, then ring, and keeps every exterior ring of a part it keeps. + for kept in keep.chunk_by(|a, b| a.0 == b.0) { + let Some(&(p, 0)) = kept.first() else { continue }; + let Some(rings) = parts.get(p) else { continue }; + let Some(shell) = rings.first() else { continue }; + let shell = ring_area(shell, (p, 0), crossing, room)?; + if shell.0.is_empty() { + // Its holes go with it. no_area += 1; + continue; } - for piece in area.0 { - if r == 0 && parts.len() > 1 { - room.grow(&mut exteriors).map_err(kept_for)?; - exteriors.push(piece.clone()); + let mut cut: Vec> = Vec::new(); + for &(_, r) in kept.get(1..).unwrap_or_default() { + let Some(hole) = rings.get(r) else { continue }; + let hole = ring_area(hole, (p, r), crossing, room)?; + if hole.0.is_empty() { + no_area += 1; + } + for piece in hole.0 { + room.grow(&mut cut).map_err(kept_for)?; + cut.push(piece); } - room.grow(&mut pieces).map_err(kept_for)?; - pieces.push(piece); + } + if !cut.is_empty() { + let step = on_grid(rings_of(&shell.0).chain(rings_of(&cut)))?; + cut = wider(cut, step, &mut narrow, room)?; + } + let part = if cut.is_empty() { + shell + } else { + let cut = geo::MultiPolygon::new(cut); + let related = (0..rings.len()).all(|r| crossing.binary_search(&(p, r)).is_err()); + if !related { + unrelated_hole_codes(&shell, &cut, &mut found); + } + let mut pieces: Vec> = room.vec(shell.0.len().saturating_add(cut.0.len())).map_err(kept_for)?; + pieces.extend(shell.0); + pieces.extend(cut.0); + geo::MultiPolygon::new(pieces).boolean_op_with_fill_rule(&empty, OpType::Union, FillRule::EvenOdd) + }; + if part.0.is_empty() { + no_area += 1; + } + for piece in part.0 { + room.grow(&mut all).map_err(kept_for)?; + all.push(piece); } } - let mut found = Found::new(); - // A part left out as another's duplicate overlapped it all. - if exterior_duplicates > 0 || parts_overlap(&exteriors)? { - found.push(Code::PartsOverlap); - } - if pieces.len() > 1 { - // Pieces narrower than a step of the grid the combining overlay rounds to are dropped first - // (finding the step also tells the overlay can run on them). - let step = on_grid(rings_of(&pieces))?; - pieces = wider(pieces, step, &mut narrow, room)?; - } - let pieces = geo::MultiPolygon::new(pieces); - let combined = if pieces.0.len() > 1 { - // The pieces left must be on a grid the overlay can represent too: in the browser a panic - // in it can't be caught. - on_grid(rings_of(&pieces.0))?; - pieces.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::EvenOdd) + let combined = if parts.len() > 1 && all.len() > 1 { + // Parts narrower than a step of the grid, in any direction, are dropped first; finding the + // step checks the grid can take every vertex, which holds for those left. + let step = on_grid(rings_of(&all))?; + let all = wider(all, step, &mut narrow, room)?; + let given = all.len(); + let each: f64 = all.iter().map(Area::unsigned_area).sum(); + let length: f64 = rings_of(&all).map(|r| Euclidean.length(r)).sum(); + let combined = geo::MultiPolygon::new(all).boolean_op_with_fill_rule(&empty, OpType::Union, FillRule::EvenOdd); + if combined.0.len() < given || each - combined.unsigned_area() > length * step { + found.push(Code::PartsOverlap); + } + combined } else { - pieces + geo::MultiPolygon::new(all) }; Ok(Rebuilt { polys: polygons(&combined, room).map_err(kept_for)?, no_area, narrow, found, duplicates }) } -/// Whether the areas of parts' exterior rings overlap, by an area more than rounding to the -/// overlay's grid could take: combined by parity, overlaps drop out, and rounding moves each edge -/// by at most half a step on each axis, so it changes the area by less than the rings' length -/// times a step. Holes play no part, so a hole in its own part isn't taken for an overlap. -fn parts_overlap(exteriors: &[geo::Polygon]) -> Result { - if exteriors.len() < 2 { - return Ok(false); +/// What the checks don't tell of the holes of a part with a ring crossing itself, which they +/// can't relate to the rest: [`Code::HoleOutsideShell`] when some of `cut`, its holes' areas, lies +/// outside `shell`, its exterior ring's, and [`Code::RingsIntersect`] when holes overlap or touch +/// along a line, which their union tells by making fewer polygons than it was given. +fn unrelated_hole_codes(shell: &geo::MultiPolygon, cut: &geo::MultiPolygon, found: &mut Found) { + if !found.contains(&Code::HoleOutsideShell) && !cut.boolean_op_with_fill_rule(shell, OpType::Difference, FillRule::NonZero).0.is_empty() + { + found.push(Code::HoleOutsideShell); + } + if cut.0.len() > 1 + && !found.contains(&Code::RingsIntersect) + && on_grid(rings_of(&cut.0)).is_ok() + && cut.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::NonZero).0.len() < cut.0.len() + { + found.push(Code::RingsIntersect); } - let step = on_grid(rings_of(exteriors))?; - let each: f64 = exteriors.iter().map(Area::unsigned_area).sum(); - let combined = geo::MultiPolygon::new(exteriors.to_vec()).boolean_op_with_fill_rule( - &geo::MultiPolygon::new(vec![]), - OpType::Union, - FillRule::EvenOdd, - ); - let length: f64 = rings_of(exteriors).map(|r| r.lines().map(|l| (l.end.x - l.start.x).hypot(l.end.y - l.start.y)).sum::()).sum(); - Ok(each - combined.unsigned_area() > length * step) } /// What [`distinct_rings`] keeps of a polygon's rings. struct Distinct { - /// The rings to combine, by part and ring, in their order. + /// The rings to combine, by part and then ring. keep: Vec<(usize, usize)>, - /// How many were left out as duplicates, and how many of those were exterior rings. + /// How many rings were left out as duplicates, and how many parts. duplicates: usize, - exterior_duplicates: usize, + duplicate_parts: usize, } -/// The rings of `parts` to combine under [`Rule::EvenOdd`], leaving out exact duplicates of an -/// earlier ring of the same role: an exterior ring of another exterior ring, a hole of another -/// hole (of any part). The same ring is the same points in the same cyclic order, from any vertex -/// and in either direction ([`normal_form`]). A ring isn't compared with one of the other role: a -/// hole equal to its exterior ring leaves its part no area, as it does under -/// [`Rule::ShellsMinusHoles`]. Only rings of one role and one length are compared, so a polygon -/// whose rings can have no duplicate costs no normal form. In buffers reserved fallibly. +/// The rings of `parts` to combine under [`Rule::EvenOdd`], without exact duplicates whose +/// canceling out no one means: a hole repeated in its own part, and a part repeated whole (its +/// exterior ring and its holes, after the first). A hole repeated in another part is kept: each of +/// the parts has it, and leaving one out would fill it. The same ring is the same points in the +/// same cyclic order, from any vertex and in either direction ([`normal_form`]); only rings, and +/// parts, of the same size are compared, so a polygon that can have no duplicate costs no normal +/// form. In buffers reserved fallibly. fn distinct_rings(parts: &[Vec>], room: Room) -> Result { let n = parts.iter().map(Vec::len).fold(0usize, usize::saturating_add); - // (whether a hole, its length, part, ring), sorted so rings that may be the same are together. - let mut all: Vec<(bool, usize, usize, usize)> = room.vec(n)?; + let mut keep: Vec<(usize, usize)> = room.vec(n)?; + let mut duplicates = 0usize; for (p, rings) in parts.iter().enumerate() { - for (r, ring) in rings.iter().enumerate() { - all.push((r > 0, ring.len(), p, r)); + let Some((_, rest)) = rings.split_first() else { continue }; + keep.push((p, 0)); + let mut holes = room.collect(rest.len(), rest.iter().enumerate().map(|(h, ring)| (h + 1, ring.as_slice())))?; + let (kept, left_out) = first_of_each(&mut holes, room)?; + duplicates += left_out; + keep.extend(kept.into_iter().map(|r| (p, r))); + } + // Parts that may be the same: the same number of rings kept, of the same number of points. + let mut sizes: Vec<(usize, usize, usize)> = room.vec(parts.len())?; + for kept in keep.chunk_by(|a, b| a.0 == b.0) { + let Some(&(p, _)) = kept.first() else { continue }; + let points = kept.iter().filter_map(|&(p, r)| parts.get(p)?.get(r)).map(Vec::len).fold(0usize, usize::saturating_add); + sizes.push((kept.len(), points, p)); + } + sizes.sort_unstable(); + let mut repeated: Vec = Vec::new(); + let mut keyed: Vec<(Vec>, usize)> = Vec::new(); + for group in sizes.chunk_by(|a, b| (a.0, a.1) == (b.0, b.1)) { + if group.len() < 2 { + continue; } - } - all.sort_unstable(); - let mut keep: Vec<(usize, usize)> = room.vec(n)?; - // (whether a hole, the normal form, part, ring) of the rings that share a role and a length. - let mut keyed: Vec<(bool, Vec<(u64, u64)>, usize, usize)> = Vec::new(); - for group in all.chunk_by(|a, b| (a.0, a.1) == (b.0, b.1)) { - for &(hole, _, p, r) in group { - if group.len() == 1 { - keep.push((p, r)); - } else if let Some(ring) = parts.get(p).and_then(|rings| rings.get(r)) { - room.grow(&mut keyed)?; - keyed.push((hole, normal_form(ring, room)?, p, r)); + keyed.clear(); + for &(count, _, p) in group { + let from = keep.partition_point(|k| k.0 < p); + let mut forms = room.vec(count)?; + for &(p, r) in keep.get(from..from.saturating_add(count)).unwrap_or_default() { + let Some(ring) = parts.get(p).and_then(|rings| rings.get(r)) else { continue }; + forms.push(normal_form(ring, room)?); + } + // The exterior ring first, then the holes in any order. + if let Some((_, holes)) = forms.split_first_mut() { + holes.sort_unstable(); + } + room.grow(&mut keyed)?; + keyed.push((forms, p)); + } + keyed.sort_unstable(); + for pair in keyed.windows(2) { + if let [first, then] = pair + && first.0 == then.0 + { + room.grow(&mut repeated)?; + repeated.push(then.1); } } } + repeated.sort_unstable(); + let before = keep.len(); + keep.retain(|(p, _)| repeated.binary_search(p).is_err()); + Ok(Distinct { duplicates: duplicates + (before - keep.len()), duplicate_parts: repeated.len(), keep }) +} + +/// Of `rings` (an id and the ring), the ids of those that aren't an exact duplicate of another with +/// a lower id, in order, and how many are. Only rings of one length get a normal form. +fn first_of_each(rings: &mut [(usize, &[Coord])], room: Room) -> Result<(Vec, usize), Limit> { + rings.sort_unstable_by_key(|(id, ring)| (ring.len(), *id)); + let mut kept: Vec = room.vec(rings.len())?; + let mut keyed: Vec<(Vec<(u64, u64)>, usize)> = Vec::new(); + for group in rings.chunk_by(|a, b| a.1.len() == b.1.len()) { + if let [(id, _)] = group { + kept.push(*id); + continue; + } + for &(id, ring) in group { + room.grow(&mut keyed)?; + keyed.push((normal_form(ring, room)?, id)); + } + } keyed.sort_unstable(); - let (mut duplicates, mut exterior_duplicates) = (0usize, 0usize); - for (i, (hole, form, p, r)) in keyed.iter().enumerate() { - // Sorted, so a duplicate follows the first of its kind, which has the lowest part and ring. - if i > 0 && keyed.get(i - 1).is_some_and(|prev| prev.0 == *hole && prev.1 == *form) { + let mut duplicates = 0usize; + for (i, (form, id)) in keyed.iter().enumerate() { + if i > 0 && keyed.get(i - 1).is_some_and(|prev| prev.0 == *form) { duplicates += 1; - exterior_duplicates += usize::from(!hole); } else { - keep.push((*p, *r)); + kept.push(*id); } } - keep.sort_unstable(); - Ok(Distinct { keep, duplicates, exterior_duplicates }) + kept.sort_unstable(); + Ok((kept, duplicates)) } /// A closed ring's points as bits, without the closing repeat: its least rotation (as a sequence @@ -826,21 +914,8 @@ fn by_area(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) - // The holes' areas, each wound the same way, make their union under non-zero winding. let cut = geo::MultiPolygon::new(cut); let related = (0..rings.len()).all(|r| crossing.binary_search(&(p, r)).is_err()); - if !related - && !found.contains(&Code::HoleOutsideShell) - && !cut.boolean_op_with_fill_rule(&shell, OpType::Difference, FillRule::NonZero).0.is_empty() - { - found.push(Code::HoleOutsideShell); - } - // Holes that overlap or touch along a line merge, which their union tells by making - // fewer polygons than it was given, as the parts' union does below. - if !related - && cut.0.len() > 1 - && !found.contains(&Code::RingsIntersect) - && on_grid(rings_of(&cut.0)).is_ok() - && cut.boolean_op_with_fill_rule(&empty, OpType::Union, FillRule::NonZero).0.len() < cut.0.len() - { - found.push(Code::RingsIntersect); + if !related { + unrelated_hole_codes(&shell, &cut, &mut found); } shell.boolean_op_with_fill_rule(&cut, OpType::Difference, FillRule::NonZero) }; diff --git a/crates/analysis/src/tools/repair_geometry.rs b/crates/analysis/src/tools/repair_geometry.rs index 0d7f928f7c..427b679034 100644 --- a/crates/analysis/src/tools/repair_geometry.rs +++ b/crates/analysis/src/tools/repair_geometry.rs @@ -302,6 +302,12 @@ fn notices(t: &Tally, field: &str, rule: Rule) -> Vec { if merged > 0 && rule == Rule::ShellsMinusHoles { messages.push(format!("{TOOL}: the parts that overlapped or touched along a line were merged in {}", features(merged))); } + if merged > 0 && rule == Rule::EvenOdd { + messages.push(format!( + "{TOOL}: the parts that overlapped or touched along a line in {} were combined by the Even-Odd rule: where they overlapped is left out, and parts meeting along a line were joined into one", + features(merged) + )); + } let split = t.count(Code::DisconnectedInterior); if split > 0 { messages.push(format!( diff --git a/crates/analysis/tests/repair_geometry.rs b/crates/analysis/tests/repair_geometry.rs index 3b1bd42765..4470faf2be 100644 --- a/crates/analysis/tests/repair_geometry.rs +++ b/crates/analysis/tests/repair_geometry.rs @@ -1276,3 +1276,38 @@ fn even_odd_says_what_it_did() { assert_eq!(got.map(|g| area(&g)), Some(100.0)); assert!(messages.iter().any(|m| m.contains("1 duplicate ring was left out")), "{messages:?}"); } + +/// Even-Odd leaves out only duplicates no one means, gives an arealess part nothing, and says what +/// it did with holes and parts, without taking a part's own hole or an island in it for an overlap. +#[test] +fn even_odd_builds_part_by_part() { + let far: Ring = &[(20., 0.), (22., 2.), (22., 0.), (20., 2.), (20., 0.)]; + let inner: Ring = &[(2., 2.), (8., 2.), (8., 8.), (2., 8.), (2., 2.)]; + let middle: Ring = &[(4., 4.), (6., 4.), (6., 6.), (4., 6.), (4., 4.)]; + // Two parts with the same hole: the hole is in neither, so it stays out, and the inner part, + // inside the outer, is a hole. + let (got, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE, middle], &[inner, middle]])); + assert_eq!(got.map(|g| area(&g)), Some(64.0), "{repairs}"); + assert!(repairs.contains("parts_overlap"), "{repairs}"); + assert!(!messages.iter().any(|m| m.contains("duplicate")), "{messages:?}"); + // An island in another part's hole, and a bowtie part well apart: no overlap. + let (got, repairs, _) = one_by("Even-Odd", multi(&[&[SQUARE, inner], &[middle], &[far]])); + assert_eq!(repairs, "self_intersection, even_odd"); + assert_eq!(got.map(|g| area(&g)), Some(100.0 - 36.0 + 4.0 + 2.0)); + // A part whose exterior ring has no area takes its holes with it, as under the default. + let flat: Ring = &[(0., 0.), (5., 0.), (10., 0.), (0., 0.)]; + let g = poly(&[flat, HOLE]); + assert_eq!(one_by("Even-Odd", g.clone()).0, one_by("Shells Minus Holes", g).0); + // A hole crossing itself with a lobe outside its exterior ring: said, as under the default. + let lobes: Ring = &[(8., 4.), (12., 6.), (12., 4.), (8., 6.), (8., 4.)]; + for rule in ["Shells Minus Holes", "Even-Odd"] { + let (_, repairs, _) = one_by(rule, poly(&[SQUARE, lobes])); + assert!(repairs.contains("hole_outside_shell"), "{rule}: {repairs}"); + } + // Parts meeting along a line are joined, and that is said. + let side: Ring = &[(10., 0.), (20., 0.), (20., 10.), (10., 10.), (10., 0.)]; + let (got, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[side], &[far]])); + assert!(repairs.contains("parts_overlap"), "{repairs}"); + assert!(messages.iter().any(|m| m.contains("joined into one")), "{messages:?}"); + assert_eq!(got.map(|g| area(&g)), Some(202.0)); +} diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 9d169bc7ef..ea7e5428e0 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1279,8 +1279,10 @@ either way. roles. This matches the "linework" method of make-valid tools. Where two parts overlap is left out, a ring listed as a part inside another is a hole, and what of a hole lies outside its exterior ring is a part. It suits data whose ring roles may be wrong, such as a hole stored as a - part of its own: Shells Minus Holes fills it, Even-Odd keeps it a hole. Exact duplicate rings of - one role, an exterior of another exterior or a hole of another hole, are left out first, since + part of its own: Shells Minus Holes fills it, Even-Odd keeps it a hole. Each part is combined + first, then the parts together, so a hole two parts both have stays out of both, and a part + whose exterior ring has no area is dropped with its holes, as under Shells Minus Holes. Exact + duplicates, a hole repeated in its own part or a part repeated whole, are left out first, since each pair would cancel out, and the tool says how many. Features rebuilt under Even-Odd have `even_odd` in `repairs` after the codes. From 80c1aab21222b07ed52c0e7022756db6d5a36da6 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:58:34 -0600 Subject: [PATCH 05/12] fix(analysis): Even-Odd's notices say what it did, and rule names in any case (#2003) The Even-Odd notice says that where two holes of a part overlap is area again, and the parts notice that a part repeated whole is kept once; a duplicate part no longer adds parts_overlap of its own. Rule names are matched in any case wherever the rule is read, and the choices are made from the one table of names and rules. One part with at most one hole skips the duplicate search. The overlay's grid is checked again on what is left after narrow pieces are dropped. The guide says Even-Odd is close to linework, not the same. --- crates/analysis/src/repair.rs | 52 +++++++++++++------- crates/analysis/src/tools/repair_geometry.rs | 4 +- crates/analysis/tests/repair_geometry.rs | 15 ++++++ docs/user-guide/analysis.md | 2 +- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 409ca1d574..e979bc9cb7 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -139,22 +139,32 @@ pub(crate) enum Rule { ShellsMinusHoles, /// Each ring's own area as [`Rule::ShellsMinusHoles`] reads it (its loops by winding, so a /// ring traced twice or a bowtie keeps its area), then the rings combined by parity whatever - /// their roles: what lies inside an odd number of rings is in (GEOS's "linework" gives the same - /// area). Where two parts overlap is left out, a ring listed as a part inside another is a - /// hole, and what of a hole lies outside its exterior ring is a part. Exact duplicate rings of - /// one role are dropped first, since a pair would cancel out ([`Repaired::duplicates`]). + /// their roles, first within each part and then the parts together: what lies inside an odd + /// number of rings is in, close to GEOS's "linework" method. Where two parts overlap is left + /// out, where two holes of a part overlap is area again, a ring listed as a part inside another + /// is a hole, and what of a hole lies outside its exterior ring is a part. A hole repeated in + /// its part and a part repeated whole are dropped first, since a pair would cancel out + /// ([`Repaired::duplicates`]); linework would cancel them. EvenOdd, } /// Each rule by the name Repair Geometry's `rule` parameter gives it, in the order it offers them. -const NAMED: [(&str, Rule); 2] = [("Shells Minus Holes", Rule::ShellsMinusHoles), ("Even-Odd", Rule::EvenOdd)]; -/// The names of [`NAMED`], as the parameter's choices. -pub(crate) const RULES: [&str; 2] = [NAMED[0].0, NAMED[1].0]; +const NAMED: &[(&str, Rule)] = &[("Shells Minus Holes", Rule::ShellsMinusHoles), ("Even-Odd", Rule::EvenOdd)]; +/// The names of [`NAMED`], in its order, as the parameter's choices. +pub(crate) const RULES: [&str; NAMED.len()] = { + let mut names = [""; NAMED.len()]; + let mut i = 0; + while i < NAMED.len() { + names[i] = NAMED[i].0; + i += 1; + } + names +}; impl Rule { - /// The rule a tool parameter names, as [`NAMED`] spells it. + /// The rule a tool parameter names, as [`NAMED`] spells it, in any case. pub(crate) fn named(name: &str) -> Option { - NAMED.iter().find(|(n, _)| *n == name).map(|(_, rule)| *rule) + NAMED.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, rule)| *rule) } } @@ -605,21 +615,17 @@ fn ring_area(ring: &[Coord], at: (usize, usize), crossing: &[(usize, usize)], ro /// parts meeting along a line are joined. /// /// Besides the checks' codes it finds those the checks can't tell of a part with a ring crossing -/// itself ([`unrelated_hole_codes`]), and [`Code::PartsOverlap`] when a part was another's -/// duplicate, or when the parts' combination makes fewer polygons than it was given (parts joined) +/// itself ([`unrelated_hole_codes`]), and [`Code::PartsOverlap`] when the parts' combination makes fewer polygons than it was given (parts joined) /// or less area than they had, by more than rounding to the grid could take: it moves each edge by /// at most half a step on each axis, so changes the area by less than the rings' length times a /// step. fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) -> Result { let empty = geo::MultiPolygon::::new(vec![]); - let Distinct { keep, duplicates, duplicate_parts } = distinct_rings(parts, room).map_err(kept_for)?; + let Distinct { keep, duplicates } = distinct_rings(parts, room).map_err(kept_for)?; let mut all: Vec> = Vec::new(); let mut no_area = 0usize; let mut narrow = 0usize; let mut found = Found::new(); - if duplicate_parts > 0 { - found.push(Code::PartsOverlap); - } // `keep` is by part, then ring, and keeps every exterior ring of a part it keeps. for kept in keep.chunk_by(|a, b| a.0 == b.0) { let Some(&(p, 0)) = kept.first() else { continue }; @@ -646,6 +652,9 @@ fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) if !cut.is_empty() { let step = on_grid(rings_of(&shell.0).chain(rings_of(&cut)))?; cut = wider(cut, step, &mut narrow, room)?; + // What is left is on that grid too; checked again all the same, since a panic in the + // overlay can't be caught in the browser. + on_grid(rings_of(&shell.0).chain(rings_of(&cut)))?; } let part = if cut.is_empty() { shell @@ -673,6 +682,7 @@ fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) // step checks the grid can take every vertex, which holds for those left. let step = on_grid(rings_of(&all))?; let all = wider(all, step, &mut narrow, room)?; + on_grid(rings_of(&all))?; let given = all.len(); let each: f64 = all.iter().map(Area::unsigned_area).sum(); let length: f64 = rings_of(&all).map(|r| Euclidean.length(r)).sum(); @@ -709,9 +719,8 @@ fn unrelated_hole_codes(shell: &geo::MultiPolygon, cut: &geo::MultiPolygon< struct Distinct { /// The rings to combine, by part and then ring. keep: Vec<(usize, usize)>, - /// How many rings were left out as duplicates, and how many parts. + /// How many rings were left out as duplicates. duplicates: usize, - duplicate_parts: usize, } /// The rings of `parts` to combine under [`Rule::EvenOdd`], without exact duplicates whose @@ -724,6 +733,13 @@ struct Distinct { fn distinct_rings(parts: &[Vec>], room: Room) -> Result { let n = parts.iter().map(Vec::len).fold(0usize, usize::saturating_add); let mut keep: Vec<(usize, usize)> = room.vec(n)?; + // One part with one hole or none can have no duplicate. + if let [rings] = parts + && rings.len() < 3 + { + keep.extend((0..rings.len()).map(|r| (0, r))); + return Ok(Distinct { keep, duplicates: 0 }); + } let mut duplicates = 0usize; for (p, rings) in parts.iter().enumerate() { let Some((_, rest)) = rings.split_first() else { continue }; @@ -775,7 +791,7 @@ fn distinct_rings(parts: &[Vec>], room: Room) -> Result Vec { } if merged > 0 && rule == Rule::EvenOdd { messages.push(format!( - "{TOOL}: the parts that overlapped or touched along a line in {} were combined by the Even-Odd rule: where they overlapped is left out, and parts meeting along a line were joined into one", + "{TOOL}: the parts that overlapped or touched along a line in {} were combined by the Even-Odd rule: where they overlapped is left out, except that a part repeated whole is kept once, and parts meeting along a line were joined into one", features(merged) )); } @@ -326,7 +326,7 @@ fn notices(t: &Tally, field: &str, rule: Rule) -> Vec { } if t.even_odd > 0 { messages.push(format!( - "{TOOL}: {} {} rebuilt by the Even-Odd rule, with {EVEN_ODD} in {field}: each keeps what lies inside an odd number of its rings, whatever their roles, so where rings overlap is left out, a ring inside another is a hole, and what of a hole lies outside its exterior ring is a part", + "{TOOL}: {} {} rebuilt by the Even-Odd rule, with {EVEN_ODD} in {field}: each part keeps what lies inside an odd number of its rings, whatever their roles, so where two holes overlap is area again, a ring inside another is a hole, and what of a hole lies outside its exterior ring is a part; then the parts are combined the same way", features(t.even_odd), was(t.even_odd) )); diff --git a/crates/analysis/tests/repair_geometry.rs b/crates/analysis/tests/repair_geometry.rs index 4470faf2be..06a909747a 100644 --- a/crates/analysis/tests/repair_geometry.rs +++ b/crates/analysis/tests/repair_geometry.rs @@ -1311,3 +1311,18 @@ fn even_odd_builds_part_by_part() { assert!(messages.iter().any(|m| m.contains("joined into one")), "{messages:?}"); assert_eq!(got.map(|g| area(&g)), Some(202.0)); } + +/// What Even-Odd's notices say is what it did: a part repeated whole is left out as a duplicate, +/// not said as an overlap left out, and where two holes of a part overlap is area again. +#[test] +fn even_odd_notices_match_the_result() { + let (got, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[SQUARE]])); + assert_eq!(got.map(|g| area(&g)), Some(100.0), "{repairs}"); + assert!(messages.iter().any(|m| m.contains("1 duplicate ring was left out")), "{messages:?}"); + assert!(messages.iter().any(|m| m.contains("a part repeated whole is kept once")), "{messages:?}"); + let left: Ring = &[(2., 2.), (6., 2.), (6., 6.), (2., 6.), (2., 2.)]; + let right: Ring = &[(4., 4.), (8., 4.), (8., 8.), (4., 8.), (4., 4.)]; + let (got, _, messages) = one_by("Even-Odd", poly(&[SQUARE, left, right])); + assert_eq!(got.map(|g| area(&g)), Some(100.0 - 28.0 + 4.0)); + assert!(messages.iter().any(|m| m.contains("where two holes overlap is area again")), "{messages:?}"); +} diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index ea7e5428e0..ab0f978325 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1276,7 +1276,7 @@ either way. them. - **Even-Odd** reads each ring's own area the same way, so a bowtie keeps both triangles and a ring traced twice counts once, then keeps what lies inside an odd number of rings, whatever their - roles. This matches the "linework" method of make-valid tools. Where two parts overlap is left + roles. This is close to the "linework" method of make-valid tools. Where two parts overlap is left out, a ring listed as a part inside another is a hole, and what of a hole lies outside its exterior ring is a part. It suits data whose ring roles may be wrong, such as a hole stored as a part of its own: Shells Minus Holes fills it, Even-Odd keeps it a hole. Each part is combined From a560a36ed653e0cfb9304e9db5afdf5efb1fd290 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:25:44 -0600 Subject: [PATCH 06/12] docs(analysis): Even-Odd's parts notice says what the rule does, and the tool summary names the rules (#2003) --- crates/analysis/src/repair.rs | 2 +- crates/analysis/src/tools.rs | 2 +- crates/analysis/src/tools/repair_geometry.rs | 2 +- crates/analysis/tests/repair_geometry.rs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index e979bc9cb7..b63e1ed33c 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -573,7 +573,7 @@ fn wider(mut polys: Vec>, step: f64, narrow: &mut usize, room: /// itself isn't related to its other rings or to other parts (see `Checked::unrelated`). type Found = Vec; -/// What [`by_area`] made of a feature's parts. +/// What [`by_area`] or [`by_parity`] made of a feature's parts. struct Rebuilt { /// The rings of the result. polys: Vec>>, diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 4f53ee158e..5141d6072f 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -97,7 +97,7 @@ pub static TOOLS: &[ToolSpec] = &[ id: "repairGeometry", name: "Repair Geometry", toolbox: "Data Management › Features", - summary: "Writes every feature with its geometry made valid where it can be: rings closed, repeated vertices and degenerate parts dropped, self-intersections, overlapping parts and holes outside their shell resolved. Features with a missing or non-finite coordinate anywhere, even in a hole, or left with no usable points or, for polygons, no area once repaired, are written without geometry. Other features too complex to check, or whose repair fails, are written unchanged. Says what was repaired in each.", + summary: "Writes every feature with its geometry made valid where it can be: rings closed, repeated vertices and degenerate parts dropped, self-intersections, overlapping parts and holes outside their shell resolved by the chosen rule (Shells Minus Holes merges overlapping parts; Even-Odd keeps what lies inside an odd number of rings). Features with a missing or non-finite coordinate anywhere, even in a hole, or left with no usable points or, for polygons, no area once repaired, are written without geometry. Other features too complex to check, or whose repair fails, are written unchanged. Says what was repaired in each.", params: &[ P { name: "input", label: "Input Features", kind: K::VectorLayer, required: true, default: "", help: "" }, P { diff --git a/crates/analysis/src/tools/repair_geometry.rs b/crates/analysis/src/tools/repair_geometry.rs index 33e3f2bcfa..8b1892c6e5 100644 --- a/crates/analysis/src/tools/repair_geometry.rs +++ b/crates/analysis/src/tools/repair_geometry.rs @@ -304,7 +304,7 @@ fn notices(t: &Tally, field: &str, rule: Rule) -> Vec { } if merged > 0 && rule == Rule::EvenOdd { messages.push(format!( - "{TOOL}: the parts that overlapped or touched along a line in {} were combined by the Even-Odd rule: where they overlapped is left out, except that a part repeated whole is kept once, and parts meeting along a line were joined into one", + "{TOOL}: the parts that overlapped or touched along a line in {} were combined by the Even-Odd rule, which leaves out where parts overlap, keeps a part repeated whole once, and joins parts that meet along a line", features(merged) )); } diff --git a/crates/analysis/tests/repair_geometry.rs b/crates/analysis/tests/repair_geometry.rs index 06a909747a..b3b92cbf90 100644 --- a/crates/analysis/tests/repair_geometry.rs +++ b/crates/analysis/tests/repair_geometry.rs @@ -1308,7 +1308,7 @@ fn even_odd_builds_part_by_part() { let side: Ring = &[(10., 0.), (20., 0.), (20., 10.), (10., 10.), (10., 0.)]; let (got, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[side], &[far]])); assert!(repairs.contains("parts_overlap"), "{repairs}"); - assert!(messages.iter().any(|m| m.contains("joined into one")), "{messages:?}"); + assert!(messages.iter().any(|m| m.contains("joins parts that meet along a line")), "{messages:?}"); assert_eq!(got.map(|g| area(&g)), Some(202.0)); } @@ -1319,7 +1319,7 @@ fn even_odd_notices_match_the_result() { let (got, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[SQUARE]])); assert_eq!(got.map(|g| area(&g)), Some(100.0), "{repairs}"); assert!(messages.iter().any(|m| m.contains("1 duplicate ring was left out")), "{messages:?}"); - assert!(messages.iter().any(|m| m.contains("a part repeated whole is kept once")), "{messages:?}"); + assert!(messages.iter().any(|m| m.contains("keeps a part repeated whole once")), "{messages:?}"); let left: Ring = &[(2., 2.), (6., 2.), (6., 6.), (2., 6.), (2., 2.)]; let right: Ring = &[(4., 4.), (8., 4.), (8., 8.), (4., 8.), (4., 4.)]; let (got, _, messages) = one_by("Even-Odd", poly(&[SQUARE, left, right])); From 8177ea4c2acead4f8a488abcd2a9a92c2d4c9e29 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:27:00 -0600 Subject: [PATCH 07/12] fix(analysis): Even-Odd parts all narrower than a grid step leave no area, not a failed repair; the rebuild says why it needs no Cancel check of its own --- crates/analysis/src/repair.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index b63e1ed33c..1d57657c63 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -619,6 +619,9 @@ fn ring_area(ring: &[Coord], at: (usize, usize), crossing: &[(usize, usize)], ro /// or less area than they had, by more than rounding to the grid could take: it moves each edge by /// at most half a step on each axis, so changes the area by less than the rings' length times a /// step. +/// +/// It has no Cancel check of its own: its passes over the rings are bounded by the work budget +/// `polygon` checks before calling it, and the tool looks at Cancel between features. fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) -> Result { let empty = geo::MultiPolygon::::new(vec![]); let Distinct { keep, duplicates } = distinct_rings(parts, room).map_err(kept_for)?; @@ -682,6 +685,11 @@ fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) // step checks the grid can take every vertex, which holds for those left. let step = on_grid(rings_of(&all))?; let all = wider(all, step, &mut narrow, room)?; + // Every part narrower than a step (two thin rectangles overlapping): none left, so no area, + // as a part with none is, not a grid with no coordinates to check. + if all.is_empty() { + return Ok(Rebuilt { polys: Vec::new(), no_area, narrow, found, duplicates }); + } on_grid(rings_of(&all))?; let given = all.len(); let each: f64 = all.iter().map(Area::unsigned_area).sum(); @@ -1194,6 +1202,17 @@ mod tests { assert_eq!(hull_of(&[]), []); } + /// Under Even-Odd, parts all narrower than a step of the grid (two thin overlapping rectangles, + /// a billionth wide, where a step is 2⁻²⁷) are dropped, leaving no area: the feature has none, + /// not a repair that failed. + #[test] + fn parts_all_too_narrow_leave_no_area_under_even_odd() { + let a: Ring = &[(0., 0.), (1e-9, 0.), (1e-9, 10.), (0., 10.), (0., 0.)]; + let b: Ring = &[(0.5e-9, 0.), (1.5e-9, 0.), (1.5e-9, 10.), (0.5e-9, 10.), (0.5e-9, 0.)]; + let g = multi(&[&[a], &[b]]); + assert_eq!(repair_by(Some(&g), Rule::EvenOdd, &|_| true), Outcome::Gone(Gone::NoArea)); + } + #[test] fn what_can_not_be_repaired_has_its_reason() { assert_eq!(repair(None, &|_| true), Outcome::Gone(Gone::Null)); From cd27538e28c8369466a82f4ca81616ef96b0f018 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:48:21 -0600 Subject: [PATCH 08/12] fix(analysis): under Even-Odd, a part repeated with a vertex in the middle of a straight edge is a duplicate, not a copy that cancels its twin out --- crates/analysis/src/repair.rs | 88 +++++++++++++++++++----- crates/analysis/tests/repair_geometry.rs | 7 ++ 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 1d57657c63..2108e88ab6 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -734,10 +734,11 @@ struct Distinct { /// The rings of `parts` to combine under [`Rule::EvenOdd`], without exact duplicates whose /// canceling out no one means: a hole repeated in its own part, and a part repeated whole (its /// exterior ring and its holes, after the first). A hole repeated in another part is kept: each of -/// the parts has it, and leaving one out would fill it. The same ring is the same points in the -/// same cyclic order, from any vertex and in either direction ([`normal_form`]); only rings, and -/// parts, of the same size are compared, so a polygon that can have no duplicate costs no normal -/// form. In buffers reserved fallibly. +/// the parts has it, and leaving one out would fill it. The same ring is the same corners in the +/// same cyclic order, from any vertex and in either direction, a vertex in the middle of a straight +/// edge aside ([`normal_form`]): a copy with one more such vertex would cancel its twin out as +/// surely. Only rings, and parts, of as many corners are compared, so a polygon that can have no +/// duplicate costs no normal form. In buffers reserved fallibly. fn distinct_rings(parts: &[Vec>], room: Room) -> Result { let n = parts.iter().map(Vec::len).fold(0usize, usize::saturating_add); let mut keep: Vec<(usize, usize)> = room.vec(n)?; @@ -761,7 +762,7 @@ fn distinct_rings(parts: &[Vec>], room: Room) -> Result = room.vec(parts.len())?; for kept in keep.chunk_by(|a, b| a.0 == b.0) { let Some(&(p, _)) = kept.first() else { continue }; - let points = kept.iter().filter_map(|&(p, r)| parts.get(p)?.get(r)).map(Vec::len).fold(0usize, usize::saturating_add); + let points = kept.iter().filter_map(|&(p, r)| parts.get(p)?.get(r)).map(|r| corner_count(r)).fold(0usize, usize::saturating_add); sizes.push((kept.len(), points, p)); } sizes.sort_unstable(); @@ -803,17 +804,19 @@ fn distinct_rings(parts: &[Vec>], room: Room) -> Result Result<(Vec, usize), Limit> { - rings.sort_unstable_by_key(|(id, ring)| (ring.len(), *id)); + let mut sized = room.collect(rings.len(), rings.iter().map(|&(id, ring)| (corner_count(ring), id, ring)))?; + sized.sort_unstable_by_key(|&(n, id, _)| (n, id)); let mut kept: Vec = room.vec(rings.len())?; let mut keyed: Vec<(Vec<(u64, u64)>, usize)> = Vec::new(); - for group in rings.chunk_by(|a, b| a.1.len() == b.1.len()) { - if let [(id, _)] = group { + for group in sized.chunk_by(|a, b| a.0 == b.0) { + if let [(_, id, _)] = group { kept.push(*id); continue; } - for &(id, ring) in group { + for &(_, id, ring) in group { room.grow(&mut keyed)?; keyed.push((normal_form(ring, room)?, id)); } @@ -831,18 +834,57 @@ fn first_of_each(rings: &mut [(usize, &[Coord])], room: Room) -> Result<(Vec Result, Limit> { - let pts = match ring.split_last() { +/// A ring without its closing repeat. +fn open(ring: &[Coord]) -> &[Coord] { + match ring.split_last() { Some((last, rest)) if Some(last) == rest.first() => rest, _ => ring, - }; + } +} + +/// Whether `b`, between `a` and `c` in a ring, lies on the straight line from `a` to `c`, exactly +/// (by Shewchuk's adaptive orientation test, as geo's robust kernel gives it: J. R. Shewchuk, +/// "Adaptive precision floating-point arithmetic and fast robust geometric predicates", Discrete & +/// Computational Geometry 18(3), 1997, 305–363), and strictly between them: a vertex in the middle +/// of a straight edge, which doesn't change the ring. A spike, going back the way it came, isn't. +fn straight_through(a: Coord, b: Coord, c: Coord) -> bool { + use geo::kernels::{Kernel, Orientation, RobustKernel}; + let g = |p: Coord| geo::Coord { x: p.x, y: p.y }; + let within = |lo: f64, v: f64, hi: f64| lo.min(hi) <= v && v <= lo.max(hi); + b != a && b != c && within(a.x, b.x, c.x) && within(a.y, b.y, c.y) && RobustKernel::orient2d(g(a), g(b), g(c)) == Orientation::Collinear +} + +/// The corners of an open ring: its vertices but those in the middle of a straight edge +/// ([`straight_through`]), each tested against its neighbors as written, so a straight run of +/// several loses them all. A ring of fewer than three vertices keeps them. +fn corners(pts: &[Coord]) -> impl Iterator { let n = pts.len(); + pts.iter() + .enumerate() + .filter(move |&(i, &b)| { + n < 3 || { + let (a, c) = (pts.get((i + n - 1) % n), pts.get((i + 1) % n)); + !matches!((a, c), (Some(&a), Some(&c)) if straight_through(a, b, c)) + } + }) + .map(|(_, c)| c) +} + +/// How many corners a closed ring has ([`corners`]): the size its [`normal_form`] has. +fn corner_count(ring: &[Coord]) -> usize { + corners(open(ring)).count() +} + +/// A closed ring's corners ([`corners`]) as bits, without the closing repeat: its least rotation +/// (as a sequence of points) in whichever direction reads less, the same for the same ring whatever +/// vertex it starts at, whichever way it is wound, and whatever vertices it has in the middle of a +/// straight edge. In time and memory linear in the ring, however often it passes one vertex +/// ([`least_rotation`]). +fn normal_form(ring: &[Coord], room: Room) -> Result, Limit> { + let pts = open(ring); + let n = corners(pts).count(); // Plus 0.0 makes -0.0 the same as 0.0: the same point, which writers and reprojection give either way. - let forward = room.collect(n, pts.iter().map(|c| ((c.x + 0.0).to_bits(), (c.y + 0.0).to_bits())))?; + let forward = room.collect(n, corners(pts).map(|c| ((c.x + 0.0).to_bits(), (c.y + 0.0).to_bits())))?; let backward = room.collect(n, forward.iter().rev().copied())?; let mut best: Option> = None; for seq in [forward, backward] { @@ -1036,6 +1078,16 @@ mod tests { } } assert_ne!(normal_form(&ring(&[(0., 0.), (10., 0.), (10., 11.), (0., 10.), (0., 0.)]), room).unwrap(), want); + // A vertex in the middle of a straight edge, or a straight run of them, doesn't change the + // ring; one past the edge's end (a spike out and back along it) does. + let mid = ring(&[(0., 0.), (2.5, 0.), (5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]); + assert_eq!(normal_form(&mid, room).unwrap(), want); + assert_eq!(corner_count(&mid), 4); + let past = ring(&[(0., 0.), (12., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]); + assert_ne!(normal_form(&past, room).unwrap(), want); + // At the closing vertex too: a square starting mid-edge. + let from_mid = ring(&[(5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.), (5., 0.)]); + assert_eq!(normal_form(&from_mid, room).unwrap(), want); let mut star = vec![Coord::new(0., 0.)]; for k in 0..100_000 { let a = f64::from(k) * 1e-5; diff --git a/crates/analysis/tests/repair_geometry.rs b/crates/analysis/tests/repair_geometry.rs index b3b92cbf90..ceb97e3ed4 100644 --- a/crates/analysis/tests/repair_geometry.rs +++ b/crates/analysis/tests/repair_geometry.rs @@ -1160,6 +1160,13 @@ fn rule_cases() -> Vec<(&'static str, Geometry, Vec, f64, &'static str)> 100.0, "parts_overlap, even_odd", ), + ( + "the same part twice, once with a vertex in the middle of an edge", + multi(&[&[SQUARE], &[&[(0., 0.), (5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]]]), + vec![0], + 100.0, + "parts_overlap, even_odd", + ), // A ring's own area is read as the default reads it: traced twice, it counts once. ("a ring traced twice", poly(&[twice]), vec![0], 100.0, "self_intersection, even_odd"), ("a ring traced twice beside a part", multi(&[&[twice], &[small]]), vec![0, 0], 104.0, "self_intersection, even_odd"), From 32b6389753120622e7ccf739b4a6f49933e7583e Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:52:08 -0600 Subject: [PATCH 09/12] test(analysis): a near-copy of a part under Even-Odd keeps its area, at the repair itself, where it otherwise has none --- crates/analysis/src/repair.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 2108e88ab6..2cfebab003 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -1254,6 +1254,23 @@ mod tests { assert_eq!(hull_of(&[]), []); } + /// Under Even-Odd, a part repeated with a vertex in the middle of a straight edge is the same + /// part, left out as a duplicate, not a copy that cancels its twin out. + #[test] + fn a_part_repeated_with_a_vertex_mid_edge_is_a_duplicate_under_even_odd() { + // The same square twice, once with a vertex in the middle of its bottom edge: without + // seeing them as one, parity cancels them out and the feature has no area. + let mid: Ring = &[(0., 0.), (5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]; + let g = multi(&[&[SQUARE], &[mid]]); + match repair_by(Some(&g), Rule::EvenOdd, &|_| true) { + Outcome::Repaired(r) => { + assert_eq!(r.duplicates, 1); + assert!((area(&r.geometry) - 100.0).abs() < 1e-9, "{:?}", r.geometry); + } + other => panic!("{other:?}"), + } + } + /// Under Even-Odd, parts all narrower than a step of the grid (two thin overlapping rectangles, /// a billionth wide, where a step is 2⁻²⁷) are dropped, leaving no area: the feature has none, /// not a repair that failed. From b2a0ff439e6e51d424198816b819354c6b00e6fb Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:05:12 -0600 Subject: [PATCH 10/12] fix(analysis): a vertex is taken for one mid-edge only where the orientation test can't overflow; the guide and notes say what counts as a duplicate, and its memory is counted --- crates/analysis/src/repair.rs | 35 ++++++++++++++++++++++++++++------- docs/user-guide/analysis.md | 7 ++++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 2cfebab003..0beb8fb25d 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -110,7 +110,7 @@ pub(crate) struct Repaired { /// When the overlay rebuilt it, how far its coordinates may have moved, in their units (degrees, /// as coordinates are stored). pub moved: Option, - /// Under [`Rule::EvenOdd`], exact duplicate rings of one role dropped before the rings were + /// Under [`Rule::EvenOdd`], duplicate rings (the same corners, [`normal_form`]) of one role dropped before the rings were /// combined, since a pair would cancel out. pub duplicates: usize, } @@ -171,8 +171,9 @@ impl Rule { /// The memory, per vertex, that [`Rule::EvenOdd`] holds besides the overlay's /// ([`OVERLAY_BYTES`]), at most, while [`distinct_rings`] finds duplicates: each ring's normal form /// kept (16), and while one is made its two readings (32), Booth's failure table (16), a rotation -/// (16) and the least so far (16). -const EVEN_ODD_BYTES: u64 = 96; +/// (16) and the least so far (16); and a part's holes listed with their corner counts (32 a ring, +/// a ring at least 4 vertices: 8). +const EVEN_ODD_BYTES: u64 = 104; /// Repairs one geometry under `rule`, spending at most [`validity::MAX_WORK`] on each check and on /// the overlay, and asking `has_room` whether the browser can give the overlay's memory before it @@ -604,7 +605,7 @@ fn ring_area(ring: &[Coord], at: (usize, usize), crossing: &[(usize, usize)], ro /// The rebuild under [`Rule::EvenOdd`] of `parts` (closed rings, each with 3 distinct points or /// more; `crossing` as in [`ring_area`]), built as [`by_area`] builds its own, part by part and then /// the parts together, with parity in place of shells minus holes: -/// - exact duplicates left out first ([`distinct_rings`]): a hole repeated in its part, and a part +/// - duplicates (the same corners, [`normal_form`]) left out first ([`distinct_rings`]): a hole repeated in its part, and a part /// repeated whole, which parity would otherwise cancel out; /// - each ring's own area ([`ring_area`]); a part whose exterior ring has none is left out with its /// holes, as [`by_area`] leaves it; @@ -731,7 +732,7 @@ struct Distinct { duplicates: usize, } -/// The rings of `parts` to combine under [`Rule::EvenOdd`], without exact duplicates whose +/// The rings of `parts` to combine under [`Rule::EvenOdd`], without duplicates whose /// canceling out no one means: a hole repeated in its own part, and a part repeated whole (its /// exterior ring and its holes, after the first). A hole repeated in another part is kept: each of /// the parts has it, and leaving one out would fill it. The same ring is the same corners in the @@ -803,10 +804,14 @@ fn distinct_rings(parts: &[Vec>], room: Room) -> Result Result<(Vec, usize), Limit> { + // One ring or none: no duplicate, and no corners to count. + if let [] | [_] = rings { + return Ok((room.collect(rings.len(), rings.iter().map(|&(id, _)| id))?, 0)); + } let mut sized = room.collect(rings.len(), rings.iter().map(|&(id, ring)| (corner_count(ring), id, ring)))?; sized.sort_unstable_by_key(|&(n, id, _)| (n, id)); let mut kept: Vec = room.vec(rings.len())?; @@ -851,7 +856,15 @@ fn straight_through(a: Coord, b: Coord, c: Coord) -> bool { use geo::kernels::{Kernel, Orientation, RobustKernel}; let g = |p: Coord| geo::Coord { x: p.x, y: p.y }; let within = |lo: f64, v: f64, hi: f64| lo.min(hi) <= v && v <= lo.max(hi); - b != a && b != c && within(a.x, b.x, c.x) && within(a.y, b.y, c.y) && RobustKernel::orient2d(g(a), g(b), g(c)) == Orientation::Collinear + // The test assumes its products don't overflow: where they would (coordinates near 1e154 and + // beyond), its NaN reads as collinear, so the vertex is kept as a corner instead. + let det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); + det.is_finite() + && b != a + && b != c + && within(a.x, b.x, c.x) + && within(a.y, b.y, c.y) + && RobustKernel::orient2d(g(a), g(b), g(c)) == Orientation::Collinear } /// The corners of an open ring: its vertices but those in the middle of a straight edge @@ -1085,6 +1098,14 @@ mod tests { assert_eq!(corner_count(&mid), 4); let past = ring(&[(0., 0.), (12., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]); assert_ne!(normal_form(&past, room).unwrap(), want); + // Where the orientation test would overflow (about 1e154 and beyond), a vertex off the line + // is a corner, not taken for one on it: these two triangles aren't one ring. + let c = Coord::new; + assert!(!straight_through(c(-1e200, -1e200), c(0., 0.5e200), c(1e200, 1e200))); + assert!(straight_through(c(-1e100, -1e100), c(0., 0.), c(1e100, 1e100))); + let t1 = ring(&[(-1e200, -1e200), (1e200, 1e200), (1e200, -1e200), (-1e200, -1e200)]); + let t2 = ring(&[(-1e200, -1e200), (0., 0.5e200), (1e200, 1e200), (1e200, -1e200), (-1e200, -1e200)]); + assert_ne!(normal_form(&t1, room).unwrap(), normal_form(&t2, room).unwrap()); // At the closing vertex too: a square starting mid-edge. let from_mid = ring(&[(5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.), (5., 0.)]); assert_eq!(normal_form(&from_mid, room).unwrap(), want); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index ab0f978325..8c33b9f11e 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1281,9 +1281,10 @@ either way. exterior ring is a part. It suits data whose ring roles may be wrong, such as a hole stored as a part of its own: Shells Minus Holes fills it, Even-Odd keeps it a hole. Each part is combined first, then the parts together, so a hole two parts both have stays out of both, and a part - whose exterior ring has no area is dropped with its holes, as under Shells Minus Holes. Exact - duplicates, a hole repeated in its own part or a part repeated whole, are left out first, since - each pair would cancel out, and the tool says how many. Features rebuilt under Even-Odd have + whose exterior ring has no area is dropped with its holes, as under Shells Minus Holes. Duplicates, + a hole repeated in its own part or a part repeated whole (the same corners in the same order, + from any vertex and either way round, a vertex in the middle of a straight edge aside), are left + out first, since each pair would cancel out, and the tool says how many. Features rebuilt under Even-Odd have `even_odd` in `repairs` after the codes. | Shape | Shells Minus Holes | Even-Odd | From f86dbec70138f3671a6e35a751b51aeb7291daf9 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:49:39 -0600 Subject: [PATCH 11/12] fix(analysis): a vertex is taken for one mid-edge only where the orientation test is exact, with no product overflowing or underflowing; Even-Odd's memory counts every buffer held while it finds duplicates --- crates/analysis/src/repair.rs | 37 ++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 0beb8fb25d..8b6925cddb 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -171,9 +171,10 @@ impl Rule { /// The memory, per vertex, that [`Rule::EvenOdd`] holds besides the overlay's /// ([`OVERLAY_BYTES`]), at most, while [`distinct_rings`] finds duplicates: each ring's normal form /// kept (16), and while one is made its two readings (32), Booth's failure table (16), a rotation -/// (16) and the least so far (16); and a part's holes listed with their corner counts (32 a ring, -/// a ring at least 4 vertices: 8). -const EVEN_ODD_BYTES: u64 = 104; +/// (16) and the least so far (16); and, held with them, a part's holes listed (24 a ring), with +/// their corner counts (32), the ids kept (8) and their keys (32): 96 a ring, a ring at least 4 +/// vertices, so 24. +const EVEN_ODD_BYTES: u64 = 120; /// Repairs one geometry under `rule`, spending at most [`validity::MAX_WORK`] on each check and on /// the overlay, and asking `has_room` whether the browser can give the overlay's memory before it @@ -847,19 +848,33 @@ fn open(ring: &[Coord]) -> &[Coord] { } } +/// How far from 0, and how close together, coordinates may be for [`straight_through`]'s test to be +/// exact: products of differences up to 4e200 and down to 1e-200, and the error terms Shewchuk's +/// test adds below them, all stay within an f64's normal range. +const EXACT_SPAN: f64 = 1e100; + /// Whether `b`, between `a` and `c` in a ring, lies on the straight line from `a` to `c`, exactly /// (by Shewchuk's adaptive orientation test, as geo's robust kernel gives it: J. R. Shewchuk, /// "Adaptive precision floating-point arithmetic and fast robust geometric predicates", Discrete & /// Computational Geometry 18(3), 1997, 305–363), and strictly between them: a vertex in the middle /// of a straight edge, which doesn't change the ring. A spike, going back the way it came, isn't. +/// +/// The test is exact only where its products neither overflow nor underflow (Shewchuk's own +/// condition; past it, an overflow's NaN reads as collinear and an underflow's 0 as on the line): +/// so only within [`EXACT_SPAN`] of 0, and with no two coordinates closer than its inverse but +/// equal, is a vertex taken for one mid-edge. Elsewhere it stays a corner. Real coordinates, in +/// degrees, meters or feet, are well inside. fn straight_through(a: Coord, b: Coord, c: Coord) -> bool { use geo::kernels::{Kernel, Orientation, RobustKernel}; let g = |p: Coord| geo::Coord { x: p.x, y: p.y }; let within = |lo: f64, v: f64, hi: f64| lo.min(hi) <= v && v <= lo.max(hi); - // The test assumes its products don't overflow: where they would (coordinates near 1e154 and - // beyond), its NaN reads as collinear, so the vertex is kept as a corner instead. - let det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); - det.is_finite() + let small = |p: Coord| p.x.abs() <= EXACT_SPAN && p.y.abs() <= EXACT_SPAN; + let apart = |u: f64, v: f64| u == v || (u - v).abs() >= 1.0 / EXACT_SPAN; + let exact = small(a) + && small(b) + && small(c) + && [(a.x, b.x), (a.x, c.x), (b.x, c.x), (a.y, b.y), (a.y, c.y), (b.y, c.y)].iter().all(|&(u, v)| apart(u, v)); + exact && b != a && b != c && within(a.x, b.x, c.x) @@ -1098,11 +1113,15 @@ mod tests { assert_eq!(corner_count(&mid), 4); let past = ring(&[(0., 0.), (12., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]); assert_ne!(normal_form(&past, room).unwrap(), want); - // Where the orientation test would overflow (about 1e154 and beyond), a vertex off the line - // is a corner, not taken for one on it: these two triangles aren't one ring. + // Where the orientation test isn't exact (its products overflowing or underflowing), a + // vertex is a corner, never taken for one on the line, on it or not: these two triangles + // aren't one ring. Within its range, one on the line is mid-edge. let c = Coord::new; assert!(!straight_through(c(-1e200, -1e200), c(0., 0.5e200), c(1e200, 1e200))); + assert!(!straight_through(c(0., 0.), c(0., 1.), c(1e200, 1e200)), "off the line, overflowing"); + assert!(!straight_through(c(0., 0.), c(1e-200, 1.5e-200), c(2e-200, 2e-200)), "off the line, underflowing"); assert!(straight_through(c(-1e100, -1e100), c(0., 0.), c(1e100, 1e100))); + assert!(straight_through(c(0., 0.), c(1e-90, 1e-90), c(2e-90, 2e-90))); let t1 = ring(&[(-1e200, -1e200), (1e200, 1e200), (1e200, -1e200), (-1e200, -1e200)]); let t2 = ring(&[(-1e200, -1e200), (0., 0.5e200), (1e200, 1e200), (1e200, -1e200), (-1e200, -1e200)]); assert_ne!(normal_form(&t1, room).unwrap(), normal_form(&t2, room).unwrap()); From 5e026762b93249779b283b50eabb5826b48edba6 Mon Sep 17 00:00:00 2001 From: Cory Douglas Martin <4108748+flight1973@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:25:43 -0600 Subject: [PATCH 12/12] fix(analysis): the mid-edge test refuses a tiny nonzero coordinate too, where its later stages would underflow; Even-Odd's memory per vertex is 128, with room --- crates/analysis/src/repair.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index 8b6925cddb..1b983c36a8 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -171,10 +171,12 @@ impl Rule { /// The memory, per vertex, that [`Rule::EvenOdd`] holds besides the overlay's /// ([`OVERLAY_BYTES`]), at most, while [`distinct_rings`] finds duplicates: each ring's normal form /// kept (16), and while one is made its two readings (32), Booth's failure table (16), a rotation -/// (16) and the least so far (16); and, held with them, a part's holes listed (24 a ring), with -/// their corner counts (32), the ids kept (8) and their keys (32): 96 a ring, a ring at least 4 -/// vertices, so 24. -const EVEN_ODD_BYTES: u64 = 120; +/// (16) and the least so far (16; the table and the rotation are never held together); and, held +/// with them, a ring's place in `keep` (16), a part's holes listed (24 a ring), with their corner +/// counts (32), the ids kept (8) and their keys (32, up to twice that as a growing list reserves), +/// a ring at least 4 vertices. At most about 116 a vertex for large rings, about 48 for many small +/// ones; 128 leaves room. +const EVEN_ODD_BYTES: u64 = 128; /// Repairs one geometry under `rule`, spending at most [`validity::MAX_WORK`] on each check and on /// the overlay, and asking `has_room` whether the browser can give the overlay's memory before it @@ -848,9 +850,12 @@ fn open(ring: &[Coord]) -> &[Coord] { } } -/// How far from 0, and how close together, coordinates may be for [`straight_through`]'s test to be -/// exact: products of differences up to 4e200 and down to 1e-200, and the error terms Shewchuk's -/// test adds below them, all stay within an f64's normal range. +/// How far from 0, how near it, and how close together coordinates may be for +/// [`straight_through`]'s test to be exact: each 0 or between `1 / EXACT_SPAN` and `EXACT_SPAN` in +/// size, and any two on one axis equal or at least `1 / EXACT_SPAN` apart. The differences' +/// products then run from 1e-200 to 4e200, and the rounding tails the test's later stages +/// multiply together (each at least about 1e-116, from a coordinate or difference of 1e-100) stay +/// within an f64's normal range too. const EXACT_SPAN: f64 = 1e100; /// Whether `b`, between `a` and `c` in a ring, lies on the straight line from `a` to `c`, exactly @@ -868,7 +873,8 @@ fn straight_through(a: Coord, b: Coord, c: Coord) -> bool { use geo::kernels::{Kernel, Orientation, RobustKernel}; let g = |p: Coord| geo::Coord { x: p.x, y: p.y }; let within = |lo: f64, v: f64, hi: f64| lo.min(hi) <= v && v <= lo.max(hi); - let small = |p: Coord| p.x.abs() <= EXACT_SPAN && p.y.abs() <= EXACT_SPAN; + let sized = |v: f64| v == 0.0 || (1.0 / EXACT_SPAN..=EXACT_SPAN).contains(&v.abs()); + let small = |p: Coord| sized(p.x) && sized(p.y); let apart = |u: f64, v: f64| u == v || (u - v).abs() >= 1.0 / EXACT_SPAN; let exact = small(a) && small(b) @@ -1120,6 +1126,11 @@ mod tests { assert!(!straight_through(c(-1e200, -1e200), c(0., 0.5e200), c(1e200, 1e200))); assert!(!straight_through(c(0., 0.), c(0., 1.), c(1e200, 1e200)), "off the line, overflowing"); assert!(!straight_through(c(0., 0.), c(1e-200, 1.5e-200), c(2e-200, 2e-200)), "off the line, underflowing"); + // A tiny coordinate beside ordinary ones, where the test's later stages would underflow: off + // the line by 1e-400, which the test would read as 0. + assert!(!straight_through(c(1e-200, -1.), c(0.5, -1e-200), c(1., 1.)), "a tiny coordinate"); + // Ordinary data, floating-point noise near 0 included, is within the test's range. + assert!(straight_through(c(0., 0.), c(6e-17, 6e-17), c(1., 1.))); assert!(straight_through(c(-1e100, -1e100), c(0., 0.), c(1e100, 1e100))); assert!(straight_through(c(0., 0.), c(1e-90, 1e-90), c(2e-90, 2e-90))); let t1 = ring(&[(-1e200, -1e200), (1e200, 1e200), (1e200, -1e200), (-1e200, -1e200)]);