-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2111 from fstoner/sineLatitude
Using cosineAndSine to convert to WebMercator.
- Loading branch information
Showing
4 changed files
with
230 additions
and
1 deletion.
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
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,211 @@ | ||
/** | ||
* @private | ||
*/ | ||
vec2 cordic(float angle) | ||
{ | ||
// Scale the vector by the appropriate factor for the 24 iterations to follow. | ||
vec2 vector = vec2(6.0725293500888267e-1, 0.0); | ||
// Iteration 1 | ||
float sense = (angle < 0.0) ? -1.0 : 1.0; | ||
// float factor = sense * 1.0; // 2^-0 | ||
mat2 rotation = mat2(1.0, sense, -sense, 1.0); | ||
vector = rotation * vector; | ||
angle -= sense * 7.8539816339744828e-1; // atan(2^-0) | ||
// Iteration 2 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
float factor = sense * 5.0e-1; // 2^-1 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 4.6364760900080609e-1; // atan(2^-1) | ||
// Iteration 3 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 2.5e-1; // 2^-2 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 2.4497866312686414e-1; // atan(2^-2) | ||
// Iteration 4 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 1.25e-1; // 2^-3 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 1.2435499454676144e-1; // atan(2^-3) | ||
// Iteration 5 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 6.25e-2; // 2^-4 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 6.2418809995957350e-2; // atan(2^-4) | ||
// Iteration 6 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 3.125e-2; // 2^-5 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 3.1239833430268277e-2; // atan(2^-5) | ||
// Iteration 7 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 1.5625e-2; // 2^-6 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 1.5623728620476831e-2; // atan(2^-6) | ||
// Iteration 8 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 7.8125e-3; // 2^-7 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 7.8123410601011111e-3; // atan(2^-7) | ||
// Iteration 9 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 3.90625e-3; // 2^-8 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 3.9062301319669718e-3; // atan(2^-8) | ||
// Iteration 10 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 1.953125e-3; // 2^-9 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 1.9531225164788188e-3; // atan(2^-9) | ||
// Iteration 11 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 9.765625e-4; // 2^-10 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 9.7656218955931946e-4; // atan(2^-10) | ||
// Iteration 12 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 4.8828125e-4; // 2^-11 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 4.8828121119489829e-4; // atan(2^-11) | ||
// Iteration 13 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 2.44140625e-4; // 2^-12 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 2.4414062014936177e-4; // atan(2^-12) | ||
// Iteration 14 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 1.220703125e-4; // 2^-13 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 1.2207031189367021e-4; // atan(2^-13) | ||
// Iteration 15 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 6.103515625e-5; // 2^-14 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 6.1035156174208773e-5; // atan(2^-14) | ||
// Iteration 16 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 3.0517578125e-5; // 2^-15 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 3.0517578115526096e-5; // atan(2^-15) | ||
// Iteration 17 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 1.52587890625e-5; // 2^-16 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 1.5258789061315762e-5; // atan(2^-16) | ||
// Iteration 18 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 7.62939453125e-6; // 2^-17 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 7.6293945311019700e-6; // atan(2^-17) | ||
// Iteration 19 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 3.814697265625e-6; // 2^-18 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 3.8146972656064961e-6; // atan(2^-18) | ||
// Iteration 20 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 1.9073486328125e-6; // 2^-19 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 1.9073486328101870e-6; // atan(2^-19) | ||
// Iteration 21 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 9.5367431640625e-7; // 2^-20 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 9.5367431640596084e-7; // atan(2^-20) | ||
// Iteration 22 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 4.76837158203125e-7; // 2^-21 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 4.7683715820308884e-7; // atan(2^-21) | ||
// Iteration 23 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 2.384185791015625e-7; // 2^-22 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
angle -= sense * 2.3841857910155797e-7; // atan(2^-22) | ||
// Iteration 24 | ||
sense = (angle < 0.0) ? -1.0 : 1.0; | ||
factor = sense * 1.1920928955078125e-7; // 2^-23 | ||
rotation[0][1] = factor; | ||
rotation[1][0] = -factor; | ||
vector = rotation * vector; | ||
// angle -= sense * 1.1920928955078068e-7; // atan(2^-23) | ||
|
||
return vector; | ||
} | ||
|
||
/** | ||
* Computes the cosine and sine of the provided angle using the CORDIC algorithm. | ||
* | ||
* @name czm_cosineAndSine | ||
* @glslFunction | ||
* | ||
* @param {float} angle The angle in radians. | ||
* | ||
* @returns {vec2} The resulting cosine of the angle (as the x coordinate) and sine of the angle (as the y coordinate). | ||
* | ||
* @example | ||
* vec2 v = czm_cosineAndSine(czm_piOverSix); | ||
* float cosine = v.x; | ||
* float sine = v.y; | ||
*/ | ||
vec2 czm_cosineAndSine(float angle) | ||
{ | ||
if (angle < -czm_piOverTwo || angle > czm_piOverTwo) | ||
{ | ||
if (angle < 0.0) | ||
{ | ||
return -cordic(angle + czm_pi); | ||
} | ||
else | ||
{ | ||
return -cordic(angle - czm_pi); | ||
} | ||
} | ||
else | ||
{ | ||
return cordic(angle); | ||
} | ||
} |
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