UNPKG

@thi.ng/matrices

Version:

Matrix & quaternion operations for 2D/3D geometry processing

18 lines (17 loc) 544 B
import { EPS } from "@thi.ng/math/api"; import { normalize3, normalize4 } from "@thi.ng/vectors/normalize"; const quatFromAxisAngle = (axis, theta) => { theta *= 0.5; return normalize3([0, 0, 0, Math.cos(theta)], axis, Math.sin(theta)); }; const quatToAxisAngle = (quat) => { const n = normalize4([], quat); const w = n[3]; const m = Math.sqrt(1 - w * w); const theta = 2 * Math.acos(w); return m > EPS ? [[n[0] / m, n[1] / m, n[2] / m], theta] : [[n[0], n[1], n[2]], theta]; }; export { quatFromAxisAngle, quatToAxisAngle };