Skip to content

math: performance & quality pass#307

Merged
bhousel merged 1 commit intomainfrom
docs-and-math-review
May 5, 2026
Merged

math: performance & quality pass#307
bhousel merged 1 commit intomainfrom
docs-and-math-review

Conversation

@bhousel
Copy link
Copy Markdown
Collaborator

@bhousel bhousel commented May 5, 2026

A focused quality and performance pass over the @rapid-sdk/math package.

Constants

  • Add WORLD_ZOOM = 16, WORLD_SIZE = 256 * 2^WORLD_ZOOM, WORLD_HALF, ANGLE_EPSILON = 1e-12
  • Replace magic number 16777216 with WORLD_SIZE throughout Viewport, Tiler, and tests

Types

  • Add Vec4 type alias for [number, number, number, number]
  • Fix Extent.rectangle() return type to Vec4

closes #301

Overloads / scalar fast-paths

Avoids tuple allocation in hot loops:

  • vecDot: scalar overload (ax, ay, bx, by)
  • vecCross: scalar overload (ax, ay, bx, by)
  • vecLengthSquare: scalar overloads (ax, ay) and (ax, ay, bx, by)
  • geoSphericalDistance: scalar overload (lon1, lat1, lon2, lat2)
  • geomLineIntersection: scalar 4-point overload; removes private _geomLineIntersection
  • geomPathIntersections / geomPathHasIntersections use 4-point overload
  • geomRotatePoints: precomputes trig, inlines scalar rotation
  • geoSphericalClosestPoint: uses scalar geoSphericalDistance in loop
  • vecProject: uses scalar vecDot in hot path

Modernization

  • Replace Math.pow with ** throughout

Transform

  • Constructor now accepts Partial<TransformProps>
  • Snap near-zero and near-TAU rotation to 0 using ANGLE_EPSILON after numWrap

Viewport / Tiler

  • Skip vecRotate when |rotation| <= ANGLE_EPSILON (epsilon-gated fast path)
  • Normalize sin/cos to zero in visibleDimensions / visiblePolygon when near-zero
  • Fix visibleDimensions assertion indexing and expected diagonal-rotation values

Dependencies

  • Move @types/geojson to packages/math (correct ownership)

Constants
- Add WORLD_ZOOM = 16, WORLD_SIZE = 256 * 2^WORLD_ZOOM, WORLD_HALF, ANGLE_EPSILON = 1e-12
- Replace magic number 16777216 with WORLD_SIZE throughout Viewport, Tiler, and tests

Types
- Add Vec4 type alias for [number, number, number, number]
- Fix Extent.rectangle() return type to Vec4 (closes #301)

Overloads / scalar fast-paths (avoid tuple allocation in hot loops)
- vecDot: scalar overload (ax, ay, bx, by)
- vecCross: scalar overload (ax, ay, bx, by)
- vecLengthSquare: scalar overload (ax, ay) and (ax, ay, bx, by)
- geoSphericalDistance: scalar overload (lon1, lat1, lon2, lat2)
- geomLineIntersection: scalar 4-point overload; remove private _geomLineIntersection
- geomPathIntersections / geomPathHasIntersections use 4-point overload
- geomRotatePoints: precompute trig, inline scalar rotation
- geoSphericalClosestPoint: use scalar geoSphericalDistance in loop
- vecProject: use scalar vecDot in hot path

Modernization
- Replace Math.pow with ** throughout

Transform
- Constructor accepts Partial<TransformProps>
- Snap near-zero and near-TAU rotation to 0 using ANGLE_EPSILON after numWrap

Viewport / Tiler
- Skip vecRotate when |rotation| <= ANGLE_EPSILON (epsilon-gated fast path)
- Normalize sin/cos to zero in visibleDimensions / visiblePolygon when near-zero
- Fix visibleDimensions assertion indexing and expected diagonal-rotation values

Dependencies
- Move @types/geojson to packages/math (correct ownership)
@bhousel bhousel merged commit cebe20a into main May 5, 2026
1 check passed
@bhousel bhousel deleted the docs-and-math-review branch May 5, 2026 15:39
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.

Extent.rectangle() should return a tuple type instead of number[]

1 participant