UNPKG

ts-ritofile

Version:

TypeScript library for reading and writing League of Legends game file formats

75 lines (62 loc) 1.9 kB
import { JsonSerializable } from '../core/json-encoder'; export class Quaternion implements JsonSerializable { public x: number; public y: number; public z: number; public w: number; constructor(x: number, y: number, z: number, w: number) { this.x = x; this.y = y; this.z = z; this.w = w; } multiply(scalar: number): Quaternion { this.x *= scalar; this.y *= scalar; this.z *= scalar; this.w *= scalar; return this; } static slerp(quat1: Quaternion, quat2: Quaternion, weight: number): Quaternion { if (weight < 0.005) return quat1; if (weight > 0.995) return quat2; const epsilon = 1e-6; let cosOmega = quat1.x * quat2.x + quat1.y * quat2.y + quat1.z * quat2.z + quat1.w * quat2.w; let flip = false; if (cosOmega < 0.0) { flip = true; cosOmega = -cosOmega; } let s1: number, s2: number; if (cosOmega > (1.0 - epsilon)) { s1 = 1.0 - weight; s2 = flip ? -weight : weight; } else { const omega = Math.acos(cosOmega); const invSinOmega = 1 / Math.sin(omega); s1 = Math.sin((1.0 - weight) * omega) * invSinOmega; s2 = flip ? -Math.sin(weight * omega) * invSinOmega : Math.sin(weight * omega) * invSinOmega; } return new Quaternion( s1 * quat1.x + s2 * quat2.x, s1 * quat1.y + s2 * quat2.y, s1 * quat1.z + s2 * quat2.z, s1 * quat1.w + s2 * quat2.w ); } toString(): string { return `${this.x.toFixed(4)} ${this.y.toFixed(4)} ${this.z.toFixed(4)} ${this.w.toFixed(4)}`; } toArray(): number[] { return [this.x, this.y, this.z, this.w]; } __json__(): number[] { return this.toArray(); } // Keep toJSON for standard JavaScript compatibility toJSON(): number[] { return this.__json__(); } }