Optimize gnomonic projections in faceijk.c#1174
Open
justinhwang wants to merge 1 commit into
Open
Conversation
Replace the transcendental-heavy inverse and forward gnomonic projections with algebraically exact identities. _hex2dToVec3 (inverse): drop atan2 by taking cos/sin of theta directly from the input vector; replace atan + final cos(r)/sin(r) with a single sqrt via cos(atan r) = 1/sqrt(1+r^2), sin(atan r) = r/sqrt(1+r^2); fold the per-face azimuth and Class III rotation into cos/sin via angle-addition against precomputed tables. ~6 transcendentals -> 1 sqrt. _vec3ToHex2d (forward): replace acos + tan with one sqrt via tan(acos x) = sqrt(1-x^2)/x (radicand factored to avoid cancellation near face centers); replace atan2 + two normalizations with direct cos/sin of the azimuth from un-normalized tangent-plane components (the common magnitude factor cancels); fold the face-axis offset and Class III rotation via angle-subtraction. The face-center guard is re-expressed on sqd (sqd < EPSILON*EPSILON) since acos is gone. ~5 transcendentals + 2 sqrt -> 2 sqrt. Removes the now-unused _vec3AzimuthRads. Adds precomputed cos/sin companion tables for faceAxesAzRadsCII[face][0], shared by both functions. All 317 C tests pass. Benchmarks (Release -O3, 40x interleaved runs, min microseconds/iter): benchmark before after delta latLngToCell 0.1789 0.1345 -24.8% cellToLatLng 0.0908 0.0696 -23.3% cellToBoundary 0.3887 0.2685 -30.9% cellArea (allCellsAtRes) 28247 19743 -30.1% directedEdgeToBoundary 1.2262 0.9250 -24.6% polygonToCellsSF 448.21 379.74 -15.3% polygonToCellsAlameda 689.77 607.62 -11.9% polygonToCellsSouthernExp 21252 19320 -9.1% cellToVertexes 0.6904 0.6897 -0.1% Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Replace the transcendental-heavy inverse and forward gnomonic projections with algebraically exact identities.
_hex2dToVec3 (inverse): drop atan2 by taking cos/sin of theta directly from the input vector; replace atan + final cos(r)/sin(r) with a single sqrt via cos(atan r) = 1/sqrt(1+r^2), sin(atan r) = r/sqrt(1+r^2); fold the per-face azimuth and Class III rotation into cos/sin via angle-addition against precomputed tables. ~6 transcendentals -> 1 sqrt.
_vec3ToHex2d (forward): replace acos + tan with one sqrt via tan(acos x) = sqrt(1-x^2)/x (radicand factored to avoid cancellation near face centers); replace atan2 + two normalizations with direct cos/sin of the azimuth from un-normalized tangent-plane components (the common magnitude factor cancels); fold the face-axis offset and Class III rotation via angle-subtraction. The face-center guard is re-expressed on sqd (sqd < EPSILON*EPSILON) since acos is gone. ~5 transcendentals + 2 sqrt -> 2 sqrt. Removes the now-unused _vec3AzimuthRads.
Adds precomputed cos/sin companion tables for faceAxesAzRadsCII[face][0], shared by both functions.
All 317 C tests pass.
Benchmarks (Release -O3, 40x interleaved runs, min microseconds/iter):