refactor(math): redefine world coordinates to zoom-16 pre-scaled space#306
Merged
refactor(math): redefine world coordinates to zoom-16 pre-scaled space#306
Conversation
Previously, wgs84ToWorld() produced coordinates in 0..256 range (z0). worldToScreen() then needed scale = 2^z to convert world β screen at every zoom level. This change pre-scales world coordinates to zoom 16, so the range is now 0..16,777,216 (= 256 Γ 2^16). worldToScreen() now uses scale = 2^(z-16): - at z16, scale = 1.0 β world coords ARE screen coords - at other zooms, a single scalar multiply is all that is needed Motivation: Rapid (and similar editors) project GeoJSON geometry once and cache the result as 'world coordinates'. With the old z0 range, every cached world point had to be multiplied by 2^z on every frame and at every zoom change. With z16 pre-scaling, geometry cached at world-z16 can be rendered directly at z16 with no recalculation. At other zoom levels only one scalar multiply separates cache from screen β no per-vertex loop. Follows on from #297. API notes: - project() / unproject() compose wgs84ToWorld + worldToScreen (and inverses) so those methods remain numerically invariant β no callers of project/unproject need to change. - Tile.tileExtent values are now in z16 world space (0..16,777,216) instead of z0 (0..256).
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.
This PR redefines the "world" coordinate system that we added #297 to pre-scale the coordinate up to z16.
Background
Previously,
wgs84ToWorld()produced coordinates in the 0..256 range (zoom 0). Converting world β screen then required:This means every cached world coordinate must be multiplied by
2^zon every frame, at every zoom level.What this changes
World coordinates are now pre-scaled to zoom 16, giving a range of 0..16,777,216 (= 256 Γ 2^16). The transform becomes:
Key property: at z16 the scale is exactly 1.0 β world coordinates are pixel-perfect screen coordinates with no transform needed.
Motivation
Editors like Rapid project GeoJSON geometry once and cache the result as world coordinates, then render that cache every frame. With z0 world coords, this cached geometry must still be reprojected into screen coordinates for rendering.
By pre-scaling to z16 (much closer to the zooms that we actually the geometry at),
API compatibility
project()/unproject()composewgs84ToWorld + worldToScreen(and inverses). These methods remain numerically invariant β all callers ofproject/unprojectcontinue to work unchanged.Tile.tileExtentvalues are now in z16 world space (0..16,777,216) instead of z0 (0..256).