Repository navigation
Add GeoJSON.Polygon.triangleIndices(), ported from earcut - #26
Merged
Merged
Conversation
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>
Member
Author
|
Written by Claude: suggested squash-merge message |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'stesselate.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:
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 itsswift(>=6)guard was always false. It now checkscompiler(>=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 testcovers 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