Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]



```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "versor",
"version": "0.2.1",
"version": "0.2.2",
"description": "Versor",
"homepage": "https://github.com/d3/versor",
"repository": {
Expand Down
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const acos = Math.acos,
asin = Math.asin,
atan2 = Math.atan2,
cos = Math.cos,
hypot = Math.hypot,
max = Math.max,
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]) {
Expand All @@ -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) {
Expand Down
50 changes: 50 additions & 0 deletions test/versor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});