Skip to content

Commit 5f8d930

Browse files
committed
math-utility
1 parent 6507cd1 commit 5f8d930

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

VR/client/math/matrix3.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export function identity3x3() {
2+
return [
3+
[1.0, 0.0, 0.0],
4+
[0.0, 1.0, 0.0],
5+
[0.0, 0.0, 1.0],
6+
];
7+
}
8+
9+
export function transpose3x3(rows) {
10+
return [
11+
[rows[0][0], rows[1][0], rows[2][0]],
12+
[rows[0][1], rows[1][1], rows[2][1]],
13+
[rows[0][2], rows[1][2], rows[2][2]],
14+
];
15+
}
16+
17+
export function multiply3x3(a, b) {
18+
const out = [
19+
[0.0, 0.0, 0.0],
20+
[0.0, 0.0, 0.0],
21+
[0.0, 0.0, 0.0],
22+
];
23+
for (let i = 0; i < 3; i += 1) {
24+
for (let j = 0; j < 3; j += 1) {
25+
out[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
26+
}
27+
}
28+
return out;
29+
}

VR/client/math/quaternions.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as THREE from "three";
2+
3+
import { transpose3x3 } from "./matrix3.js";
4+
5+
export function quaternionFromRowwiseDirector(matrixRows) {
6+
const m = new THREE.Matrix4();
7+
m.set(
8+
matrixRows[0][0], matrixRows[1][0], matrixRows[2][0], 0.0,
9+
matrixRows[0][1], matrixRows[1][1], matrixRows[2][1], 0.0,
10+
matrixRows[0][2], matrixRows[1][2], matrixRows[2][2], 0.0,
11+
0.0, 0.0, 0.0, 1.0
12+
);
13+
return new THREE.Quaternion().setFromRotationMatrix(m);
14+
}
15+
16+
export function columnwiseDirectorFromQuaternion(quaternion) {
17+
// Convert quaternion to column-wise director for elastica representation.
18+
// Maybe consider using three.js's conversion, but we just want 3x3 orientation.
19+
const x = quaternion.x;
20+
const y = quaternion.y;
21+
const z = quaternion.z;
22+
const w = quaternion.w;
23+
const xx = x * x;
24+
const yy = y * y;
25+
const zz = z * z;
26+
const xy = x * y;
27+
const xz = x * z;
28+
const yz = y * z;
29+
const wx = w * x;
30+
const wy = w * y;
31+
const wz = w * z;
32+
return [
33+
[1.0 - 2.0 * (yy + zz), 2.0 * (xy - wz), 2.0 * (xz + wy)],
34+
[2.0 * (xy + wz), 1.0 - 2.0 * (xx + zz), 2.0 * (yz - wx)],
35+
[2.0 * (xz - wy), 2.0 * (yz + wx), 1.0 - 2.0 * (xx + yy)],
36+
];
37+
}
38+
39+
export function rowwiseDirectorFromQuaternion(quaternion) {
40+
// Typically quaternion from controller is in world space,
41+
// We need to convert to row-wise director for elastica representation.
42+
return transpose3x3(columnwiseDirectorFromQuaternion(quaternion));
43+
}

0 commit comments

Comments
 (0)