Skip to content

Add GeoJSON.Polygon.triangleIndices(), ported from earcut - #26

Merged
nighthawk merged 3 commits into
mainfrom
feat/triangulate
Nov 7, 2026
Merged

nighthawk merged 3 commits into
mainfrom
feat/triangulate

Conversation

@nighthawk

@nighthawk nighthawk commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Drawing a polygon's fill with a GPU needs it as triangles, and a naive ear clipper that rescans the whole ring for every ear and every crossing gets slow on detailed data: about 2 s for all countries on a Mac. This adds triangulation as GeoJSON.Polygon.triangleIndices(), the counterpart of turf.js's tesselate.

It's a port of mapbox/earcut (ISC, attribution in the file), with a z-order index so that checking an ear only looks at nearby corners. Two additions make it hold up on detailed coastlines, both noted as departures from the original:

  • Untangling: rings that cross themselves in tiny loops are untangled first, using a grid of edges. Without that, earcut drops large parts of such polygons; a figure of eight is in the tests.
  • Holes outside the polygon: these are skipped. Earcut bridged them across the whole polygon, which cost Great Britain a third of its area in the countries data.

It returns indexes into the rings' positions, so callers can keep their own vertex data, and each triangle is counter-clockwise.

The second commit fixes EncodedPolylineTests, which never ran: the package builds in Swift 5 mode, so its swift(>=6) guard was always false. It now checks compiler(>=6), and the test passes.

Performance: all 14,789 country polygons (368k positions) triangulate in about 190 ms in a release build on an M-series Mac. Every polygon's area matches the previous fill, or is closer to the true area.

Testing: swift test covers squares, both windings, concave shapes, holes, holes outside, self-crossing rings, a figure of eight, and a 20,000-corner ring.

🤖 Generated with Claude Code

nighthawk and others added 2 commits November 7, 2026 11:42
A port of mapbox/earcut, as turf.js's `tesselate` uses, with two additions for detailed coastlines: holes that are outside the polygon are skipped rather than bridged across it, and rings that cross themselves are untangled first, so that tiny wrong-way loops don't stall the ear clipping.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The package builds in Swift 5 language mode, so `swift(>=6)` was always false and EncodedPolylineTests never ran; `compiler(>=6)` checks the toolchain instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nighthawk

nighthawk commented Nov 7, 2026 •

Copy link
Copy Markdown
Member Author

Written by Claude: suggested squash-merge message

feat: add GeoJSON.Polygon.triangleIndices(), ported from earcut

- Add `GeoJSON.Polygon.triangleIndices()`, a port of mapbox/earcut with a z-order index
- Untangle rings that cross themselves before clipping ears
- Skip holes that lie outside the polygon instead of bridging across it
- Run the Swift Testing tests, which a `swift(>=6)` guard had kept from running
- Test on Linux with Swift 5.10, 6.3 and 6.4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nighthawk nighthawk self-assigned this Nov 7, 2026
@nighthawk nighthawk added the enhancement New feature or request label Nov 7, 2026
@nighthawk
nighthawk merged commit 5f3bcae into main Nov 7, 2026
4 checks passed
@nighthawk
nighthawk deleted the feat/triangulate branch November 7, 2026 03:31
@nighthawk nighthawk mentioned this pull request Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant