-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type matrix and vector calculations #11400
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a5ef619
add Flow types for gl-matrix
mourner 2867fcd
update vec/matrix type references to proper types
mourner 09d3ab0
fix more type references
mourner a288b3a
type more vec3/quat functions
mourner 26f237c
various vec/mat typing fixes
mourner a717dcc
fix remaining matrix types
mourner ffc4c78
bump flow by 5 versions (last version that does not make new errors)
mourner 5f01645
Merge branch 'main' into flow-type-gl-matrix
mourner 4877111
fix up flow after merge
mourner 0180b2a
revert globe matrix to double precision
mourner 7e79b0b
revert globe tile matrix to double precision
mourner d758023
address feedback
mourner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ | |
.*/_site/.* | ||
|
||
[version] | ||
0.103.0 | ||
0.108.0 | ||
|
||
[options] | ||
server.max_workers=4 | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
|
||
type VecType = Array<number> | Float32Array | Float64Array; | ||
|
||
declare module "gl-matrix" { | ||
declare type Vec2 = VecType; | ||
declare type Vec3 = VecType; | ||
declare type Vec4 = VecType; | ||
declare type Quat = VecType; | ||
declare type Mat2 = VecType; | ||
declare type Mat3 = VecType; | ||
declare type Mat4 = VecType; | ||
|
||
declare var vec2: { | ||
exactEquals(Vec2, Vec2): boolean | ||
}; | ||
|
||
declare var vec3: { | ||
create(): Float32Array, | ||
fromValues(number, number, number): Float32Array, | ||
length(Vec3): number, | ||
len(Vec3): number, | ||
squaredLength(Vec3): number, | ||
dot(Vec3, Vec3): number, | ||
equals(Vec3, Vec3): boolean, | ||
exactEquals(Vec3, Vec3): boolean, | ||
|
||
clone<T: Vec3>(T): T, | ||
normalize<T: Vec3>(T, Vec3): T, | ||
add<T: Vec3>(T, Vec3, Vec3): T, | ||
sub<T: Vec3>(T, Vec3, Vec3): T, | ||
subtract<T: Vec3>(T, Vec3, Vec3): T, | ||
cross<T: Vec3>(T, Vec3, Vec3): T, | ||
negate<T: Vec3>(T, Vec3): T, | ||
scale<T: Vec3>(T, Vec3, number): T, | ||
scaleAndAdd<T: Vec3>(T, Vec3, Vec3, number): T, | ||
multiply<T: Vec3>(T, Vec3, Vec3): T, | ||
mul<T: Vec3>(T, Vec3, Vec3): T, | ||
div<T: Vec3>(T, Vec3, Vec3): T, | ||
min<T: Vec3>(T, Vec3, Vec3): T, | ||
max<T: Vec3>(T, Vec3, Vec3): T, | ||
transformQuat<T: Vec3>(T, Vec3, Quat): T, | ||
transformMat3<T: Vec3>(T, Vec3, Mat3): T, | ||
transformMat4<T: Vec3>(T, Vec3, Mat4): T | ||
}; | ||
|
||
declare var vec4: { | ||
scale<T: Vec4>(T, Vec4, number): T, | ||
mul<T: Vec4>(T, Vec4, Vec4): T, | ||
transformMat4<T: Vec4>(T, Vec4, Mat4): T | ||
}; | ||
|
||
declare var mat2: { | ||
create(): Float32Array, | ||
rotate<T: Mat2>(T, Mat2, number): T, | ||
invert<T: Mat2>(T, Mat2): T, | ||
scale<T: Mat2>(T, Mat2, Vec2): T | ||
}; | ||
|
||
declare var mat3: { | ||
create(): Float32Array, | ||
|
||
fromMat4<T: Mat3>(T, Mat4): T, | ||
fromRotation<T: Mat3>(T, number): T, | ||
mul<T: Mat3>(T, Mat3, Mat3): T, | ||
multiply<T: Mat3>(T, Mat3, Mat3): T, | ||
adjoint<T: Mat3>(T, Mat3): T, | ||
transpose<T: Mat3>(T, Mat3): T | ||
}; | ||
|
||
declare var mat4: { | ||
create(): Float32Array, | ||
|
||
fromScaling<T: Mat4>(T, Vec3): T, | ||
fromQuat<T: Mat4>(T, Quat): T, | ||
ortho<T: Mat4>(T, number, number, number, number, number, number): T, | ||
perspective<T: Mat4>(T, number, number, number, number): T, | ||
identity<T: Mat4>(T): T, | ||
scale<T: Mat4>(T, Mat4, Vec3): T, | ||
mul<T: Mat4>(T, Mat4, Mat4): T, | ||
multiply<T: Mat4>(T, Mat4, Mat4): T, | ||
rotateX<T: Mat4>(T, Mat4, number): T, | ||
rotateY<T: Mat4>(T, Mat4, number): T, | ||
rotateZ<T: Mat4>(T, Mat4, number): T, | ||
translate<T: Mat4>(T, Mat4, Vec3): T, | ||
invert<T: Mat4>(T, Mat4): T, | ||
copy<T: Mat4>(T, Mat4): T, | ||
clone<T: Mat4>(T): T | ||
}; | ||
|
||
declare var quat: { | ||
create(): Float32Array, | ||
length(Quat): number, | ||
exactEquals(Quat, Quat): boolean, | ||
|
||
normalize<T: Quat>(T, Quat): T, | ||
conjugate<T: Quat>(T, Quat): T, | ||
identity<T: Quat>(T): T, | ||
rotateX<T: Quat>(T, Quat, number): T, | ||
rotateY<T: Quat>(T, Quat, number): T, | ||
rotateZ<T: Quat>(T, Quat, number): T | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe reducing precision of some of the intermediate matrices from 64 bits to 32 bits will become problematic. Globe matrix is pretty much equivalent of tile matrices (
createTileMatrix
for other projections) meaning that it requires high precision for representing pixel coordinates at all zoom levels. Any precision issues might be masked though by the low zoom threshold value we have for the globe view but it will eventually bite us back.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's the moment I want to check up on — which of these matrices are intermediary and need high precision? I tried to keep them
Float64Array
but had a hard time reconciling typing errors when some of these matrices eventually get passed to WebGL methods that expectFloat32Array
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mpulkki-mapbox I reverted globe matrix to double precision — any other matrices I need to do this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule of thumb would be that any matrix that is multiplied with
transform.projMatrix
should be kept 64bit. I'll go through the list quickly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mourner: it's actually quite difficult to tell whether current type annotations for matrices are correct or not. For example the function signature
calculateProjMatrix(unwrappedTileID: UnwrappedTileID, aligned: boolean = false): Float32Array
in transform.js actually returns 64bit matrices as all possible code paths inside the function returnsFloat64Arrays
. Similar patterns exists in other parts too. Or am I overlooking something perhaps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mpulkki-mapbox how so? It's explicitly returning
Float32Array
at the moment:mapbox-gl-js/src/geo/transform.js
Lines 1444 to 1445 in 74559af
The new type mismatches in the new branch are caught pretty well — certainly a huge improvement over silently passing mistypes in
main
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh you're correct! The conversion is done at the end of the function call :). It's perhaps the
calculatePosMatrix
I was looking at. That function will forward the matrix creation to a specific TileTransform class and both FlatTileTransform and GlobeTileTransform will return 64 bit matrices. Am I looking things correctly now? :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mpulkki-mapbox I reverted
createTileMatrix
toFloat64Array
, so now it should at least be consistent. I guess it can't fully figure out the indirection in this instance to see that it doesn't match the type defined inprojection/index.js
, but upgrading Flow to the latest (2.5 years newer) version with much more sound typing architecture (which is my end goal) might help.