Skip to content

Handle interiorcuts edge cases - #350

Merged
gpeairs merged 8 commits into
mainfrom
gp/interiorcuts-edge-cases
Oct 29, 2026
Merged

gpeairs merged 8 commits into
mainfrom
gp/interiorcuts-edge-cases

Conversation

@gpeairs

@gpeairs gpeairs commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Close #317.

A hole whose lowest vertex Clipper rounded to just outside its enclosing contour had nothing below it and was silently dropped. Fix in simple cases, throw an error when the problematic case is detected. In principle this can still miss problems when the near-touch is another hole or a non-neighboring edge but is generally conservative (may warn with nothing technically wrong in rare cases). Also even the detectable case is very rare, not encountered in fuzzing at um scale on fm grid. No performance consequences. Need to clean up comments at least.

Also drop consecutive repeated points, left by a cut landing on a vertex or a hole touching its enclosing contour.

Encountered a different bug fuzzing this, where floating point issues could cause a ray not to see a vertex it was exactly above, and fixed it here (more exact Int128 computation for intersections).


Original version of this PR: Pinch it onto the nearest edge instead, and warn if no edge is within a unit.

Also drop consecutive repeated points, left by a cut landing on a vertex or a hole touching its enclosing contour.

This has no performance hit for axis-aligned geometry (far == near) and a measurable one otherwise. kink_crosses queries the interval tree over the full x-extent of the enclosing edge, which for a long sloped edge with many attached holes looked potentially quadratic. In practice each attachment splits the edge into
sub-nodes, so later queries cover only the sub-segment's x-range. Measured on a rotated (10°) 1000×1000 square with a grid of square holes (every cut has a non-integer hit, so every hole pays the check):

holes base to_polygons PR to_polygons difference2d itself
2,500 32 ms 50 ms —
10,000 230 ms 550 ms —
40,000 2.7 s 4.4 s 1.5 s

This is could be worth adding more complexity to improve, but the fix itself is already unpleasantly complex. Trying to put a point on a diagonal line is dangerous when working with integer grids, whether for Clipper or GDS. Clipper2 has at least one hole-dropping bug itself, for a union that closes off a concavity on a diagonal that gets unlucky, which is pretty hard to hit accidentally in a realistic design. I don't know if we want to be in the business of patching together our own integer-grid geometry library. Maybe we just detect when something goes wrong and throw an error that tells the user to split T-junctions first (which the conformal rendering pipeline requires anyway), or something. These cases feel too rare in real devices to be worth the performance and complexity cost of a full fix.

@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

The cut from a hole's lowest vertex attaches to the enclosing edge at a
lattice point, which kinks that edge by up to a unit. Rounded inward, the
kink can cross a hole vertex just inside the edge; rounded outward, it can
cross the contour's next edge where that folds back at an acute vertex.
Use the nearer lattice point unless its kink crosses a segment and the
other's does not.

A hole whose lowest vertex Clipper rounded to just outside its enclosing
contour had nothing below it and was silently dropped. Pinch it onto the
nearest edge instead, and warn if no edge is within a unit.

Drop consecutive repeated points, left by a cut landing on a vertex or a
hole touching its enclosing contour. The PolyText GDS fingerprints shrink
accordingly: same polygons, 34 fewer points in the characters demo.
@gpeairs
gpeairs force-pushed the gp/interiorcuts-edge-cases branch from e69cd3a to b3c3c4d Compare October 29, 2026 12:10
@gpeairs
gpeairs marked this pull request as ready for review October 29, 2026 12:10
@gpeairs
gpeairs merged commit fafb6ff into main Oct 29, 2026
11 checks passed
@gpeairs
gpeairs deleted the gp/interiorcuts-edge-cases branch October 29, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle interiorcuts edge cases

1 participant