Skip to content

Commit 724574e

Browse files
committed
refactor(Quaternion): Refactor Quaternion to typescript module, add interface to represent structure and type methods.
Signed-off-by: Drew Hoener <[email protected]>
1 parent 114ce6e commit 724574e

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

src/math/Quaternion.ts

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,69 @@
22
* @fileOverview
33
* @author David Gossow - [email protected]
44
*/
5+
import { type PartialNullable } from '../types/interface-types.js';
6+
7+
export interface IQuaternion {
8+
/**
9+
* The x value.
10+
*/
11+
x: number;
12+
13+
/**
14+
* The y value.
15+
*/
16+
y: number;
17+
18+
/**
19+
* The z value.
20+
*/
21+
z: number;
22+
23+
/**
24+
* The w value.
25+
*/
26+
w: number;
27+
}
528

629
/**
730
* A Quaternion.
831
*/
9-
export default class Quaternion {
10-
/**
11-
* @param {Object} [options]
12-
* @param {number|null} [options.x=0] - The x value.
13-
* @param {number|null} [options.y=0] - The y value.
14-
* @param {number|null} [options.z=0] - The z value.
15-
* @param {number|null} [options.w=1] - The w value.
16-
*/
17-
constructor(options) {
18-
options = options || {};
19-
this.x = options.x || 0;
20-
this.y = options.y || 0;
21-
this.z = options.z || 0;
22-
this.w = typeof options.w === 'number' ? options.w : 1;
32+
export default class Quaternion implements IQuaternion {
33+
x: number;
34+
y: number;
35+
z: number;
36+
w: number;
37+
38+
constructor(options?: PartialNullable<IQuaternion> | null) {
39+
this.x = options?.x ?? 0;
40+
this.y = options?.y ?? 0;
41+
this.z = options?.z ?? 0;
42+
this.w = typeof options?.w === 'number' ? options.w : 1;
2343
}
44+
2445
/**
2546
* Perform a conjugation on this quaternion.
2647
*/
27-
conjugate() {
48+
conjugate(): void {
2849
this.x *= -1;
2950
this.y *= -1;
3051
this.z *= -1;
3152
}
53+
3254
/**
3355
* Return the norm of this quaternion.
3456
*/
35-
norm() {
57+
norm(): number {
3658
return Math.sqrt(
3759
this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w
3860
);
3961
}
62+
4063
/**
4164
* Perform a normalization on this quaternion.
4265
*/
43-
normalize() {
44-
var l = Math.sqrt(
66+
normalize(): void {
67+
let l = Math.sqrt(
4568
this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w
4669
);
4770
if (l === 0) {
@@ -67,24 +90,25 @@ export default class Quaternion {
6790
/**
6891
* Set the values of this quaternion to the product of itself and the given quaternion.
6992
*
70-
* @param {Quaternion} q - The quaternion to multiply with.
93+
* @param {IQuaternion} q - The quaternion to multiply with.
7194
*/
72-
multiply(q) {
73-
var newX = this.x * q.w + this.y * q.z - this.z * q.y + this.w * q.x;
74-
var newY = -this.x * q.z + this.y * q.w + this.z * q.x + this.w * q.y;
75-
var newZ = this.x * q.y - this.y * q.x + this.z * q.w + this.w * q.z;
76-
var newW = -this.x * q.x - this.y * q.y - this.z * q.z + this.w * q.w;
95+
multiply(q: IQuaternion): void {
96+
const newX = this.x * q.w + this.y * q.z - this.z * q.y + this.w * q.x;
97+
const newY = -this.x * q.z + this.y * q.w + this.z * q.x + this.w * q.y;
98+
const newZ = this.x * q.y - this.y * q.x + this.z * q.w + this.w * q.z;
99+
const newW = -this.x * q.x - this.y * q.y - this.z * q.z + this.w * q.w;
77100
this.x = newX;
78101
this.y = newY;
79102
this.z = newZ;
80103
this.w = newW;
81104
}
105+
82106
/**
83107
* Clone a copy of this quaternion.
84108
*
85109
* @returns {Quaternion} The cloned quaternion.
86110
*/
87-
clone() {
111+
clone(): Quaternion {
88112
return new Quaternion(this);
89113
}
90114
}

src/math/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { default as Pose } from './Pose.js';
2-
export { default as Quaternion } from './Quaternion.js';
2+
export { default as Quaternion, type IQuaternion } from './Quaternion.js';
33
export { default as Transform } from './Transform.js';
44
export { default as Vector3, type IVector3 } from './Vector3.js';

0 commit comments

Comments
 (0)