Skip to content

Optimize gnomonic projections in faceijk.c#1174

Open
justinhwang wants to merge 1 commit into
uber:masterfrom
justinhwang:justinhwang/c-projection-optimizations
Open

Optimize gnomonic projections in faceijk.c#1174
justinhwang wants to merge 1 commit into
uber:masterfrom
justinhwang:justinhwang/c-projection-optimizations

Conversation

@justinhwang

Copy link
Copy Markdown
Contributor

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%

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>
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.

1 participant