diff --git a/README.md b/README.md index d9a3df3..8c76cfe 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,9 @@ q01 = versor.multiply(q0, q1); // [0.5, 0.5, 0.5, 0.5] // Euler rotation angles [λ, φ, γ] for the given quaternion. versor.rotation(q01); // [90, 0, 90] +// At the poles (φ = ±90°), only λ - γ (north) or λ + γ (south) is defined; γ is then 0. +versor.rotation(versor([30, 90, -30])); // [60, 90, 0] + ``` diff --git a/package.json b/package.json index 3962588..b10e260 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "versor", - "version": "0.2.1", + "version": "0.2.2", "description": "Versor", "homepage": "https://github.com/d3/versor", "repository": { diff --git a/src/index.js b/src/index.js index d5b6110..68a5970 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ const acos = Math.acos, - asin = Math.asin, atan2 = Math.atan2, cos = Math.cos, hypot = Math.hypot, @@ -7,8 +6,14 @@ const acos = Math.acos, min = Math.min, PI = Math.PI, sin = Math.sin, + sqrt = Math.sqrt, radians = PI / 180, - degrees = 180 / PI; + degrees = 180 / PI, + epsilon = 1e-12; + +function wrap(x) { + return x > PI ? x - 2 * PI : x < -PI ? x + 2 * PI : x; +} class Versor { static fromCartesian([x, y, z]) { @@ -28,11 +33,20 @@ class Versor { cl * cp * sg - sl * sp * cg ]; } + // (λ + γ) / 2 is undetermined at the north pole, and (λ - γ) / 2 at the south + // pole (gimbal lock); at a pole, γ = 0 and λ carries the whole rotation. static toAngles([a, b, c, d]) { + const x0 = a - c, y0 = b + d, x1 = a + c, y1 = b - d, + n = sqrt(x0 * x0 + y0 * y0), // zero at the north pole + s = sqrt(x1 * x1 + y1 * y1); // zero at the south pole + let u = atan2(y0, x0), // (λ + γ) / 2 + v = atan2(y1, x1); // (λ - γ) / 2 + if (n <= epsilon * s) u = v; + else if (s <= epsilon * n) v = u; return [ - atan2(2 * (a * b + c * d), 1 - 2 * (b * b + c * c)) * degrees, - asin(max(-1, min(1, 2 * (a * c - d * b)))) * degrees, - atan2(2 * (a * d + b * c), 1 - 2 * (c * c + d * d)) * degrees + wrap(u + v) * degrees, + (2 * atan2(s, n) - PI / 2) * degrees, + wrap(u - v) * degrees ]; } static interpolateAngles(a, b) { diff --git a/test/versor-test.js b/test/versor-test.js index c541050..ad90cfd 100644 --- a/test/versor-test.js +++ b/test/versor-test.js @@ -46,3 +46,53 @@ it("delta tweening", () => { const a = versor.cartesian([ 0, 0 ]), b = versor.cartesian([ 90, 0 ]); assert(distance2(versor.delta(a,b,0.5), [ 0.9239, 0.3827, 0, 0 ]) < 1e-8); }); + +// q and -q represent the same rotation +function rotationDistance2(a, b) { + return Math.min(distance2(a, b), distance2(a, b.map(x => -x))); +} + +it("rotation returns angles for the same rotation, including at the poles https://github.com/d3/versor/issues/11", () => { + for (const p of [90, 90 - 1e-12, 90 - 1e-9, 90 - 1e-6, 89, 45, 0, -45, -89, -90 + 1e-6, -90 + 1e-9, -90 + 1e-12, -90]) { + for (let l = -180; l < 180; l += 7.5) { + for (const g of [-151.3, -28.0005, 0, 1e-9, 28.0005, 90, 179]) { + const q = versor([l, p, g]); + assert(rotationDistance2(versor(versor.rotation(q)), q) < 1e-24, `[${l}, ${p}, ${g}]`); + } + } + } +}); + +it("rotation returns γ = 0 at the poles", () => { + for (const p of [90, -90]) { + for (const l of [-180, -135, -28.0005, 0, 28.0005, 90, 179.9]) { + const r = versor.rotation(versor([l, p])); + assert(distance2(r, [l, p, 0]) < 1e-20 || distance2(r, [l + 360, p, 0]) < 1e-20, `[${l}, ${p}] → [${r}]`); + } + } + assert(distance2(versor.rotation(versor([28, 90, -28])), [56, 90, 0]) < 1e-20); + assert(distance2(versor.rotation(versor([-28, -90, 28])), [0, -90, 0]) < 1e-20); +}); + +it("rotation matches the previous formula away from the poles", () => { + for (let p = -85; p <= 85; p += 5) { + for (let l = -175; l < 180; l += 10) { + for (const g of [-150, -28, 0, 28, 150]) { + assert(distance2(versor.rotation(versor([l, p, g])), [l, p, g]) < 1e-20, `[${l}, ${p}, ${g}]`); + } + } + } +}); + +it("spinning around a pole does not drift https://github.com/d3/d3-geo/issues/194", () => { + const spin = versor([0.5, 0, 0]); + for (const p of [90, -90]) { + let r = [0, p, 0], q = versor(r); + for (let i = 0; i < 720; i++) { + r = versor.rotation(versor.multiply(versor(r), spin)); + q = versor.multiply(q, spin); + assert(rotationDistance2(versor(r), q) < 1e-24, `step ${i} at ${p}`); + } + assert(distance2(r, [0, p, 0]) < 1e-20); + } +});