Skip to content

Commit 114ce6e

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

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

src/math/Vector3.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,81 @@
33
* @author David Gossow - [email protected]
44
*/
55

6-
import Quaternion from './Quaternion.js';
6+
import { type IQuaternion } from './Quaternion.js';
7+
import { type PartialNullable } from '../types/interface-types.js';
8+
9+
export interface IVector3 {
10+
/**
11+
* The x value.
12+
*/
13+
x: number;
14+
/**
15+
* The y value.
16+
*/
17+
y: number;
18+
/**
19+
* The z value.
20+
*/
21+
z: number;
22+
}
723

824
/**
925
* A 3D vector.
1026
*/
11-
export default class Vector3 {
12-
/**
13-
* @param {Object} [options]
14-
* @param {number} [options.x=0] - The x value.
15-
* @param {number} [options.y=0] - The y value.
16-
* @param {number} [options.z=0] - The z value.
17-
*/
18-
constructor(options) {
19-
options = options || {};
20-
this.x = options.x || 0;
21-
this.y = options.y || 0;
22-
this.z = options.z || 0;
27+
export default class Vector3 implements IVector3 {
28+
x: number;
29+
y: number;
30+
z: number;
31+
32+
constructor(options?: PartialNullable<IVector3> | null) {
33+
this.x = options?.x ?? 0;
34+
this.y = options?.y ?? 0;
35+
this.z = options?.z ?? 0;
2336
}
37+
2438
/**
2539
* Set the values of this vector to the sum of itself and the given vector.
2640
*
2741
* @param {Vector3} v - The vector to add with.
2842
*/
29-
add(v) {
43+
add(v: IVector3): void {
3044
this.x += v.x;
3145
this.y += v.y;
3246
this.z += v.z;
3347
}
48+
3449
/**
3550
* Set the values of this vector to the difference of itself and the given vector.
3651
*
3752
* @param {Vector3} v - The vector to subtract with.
3853
*/
39-
subtract(v) {
54+
subtract(v: IVector3): void {
4055
this.x -= v.x;
4156
this.y -= v.y;
4257
this.z -= v.z;
4358
}
59+
4460
/**
4561
* Multiply the given Quaternion with this vector.
4662
*
4763
* @param {Quaternion} q - The quaternion to multiply with.
4864
*/
49-
multiplyQuaternion(q) {
50-
var ix = q.w * this.x + q.y * this.z - q.z * this.y;
51-
var iy = q.w * this.y + q.z * this.x - q.x * this.z;
52-
var iz = q.w * this.z + q.x * this.y - q.y * this.x;
53-
var iw = -q.x * this.x - q.y * this.y - q.z * this.z;
65+
multiplyQuaternion(q: IQuaternion) {
66+
const ix = q.w * this.x + q.y * this.z - q.z * this.y;
67+
const iy = q.w * this.y + q.z * this.x - q.x * this.z;
68+
const iz = q.w * this.z + q.x * this.y - q.y * this.x;
69+
const iw = -q.x * this.x - q.y * this.y - q.z * this.z;
5470
this.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;
5571
this.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;
5672
this.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;
5773
}
74+
5875
/**
5976
* Clone a copy of this vector.
6077
*
6178
* @returns {Vector3} The cloned vector.
6279
*/
63-
clone() {
80+
clone(): Vector3 {
6481
return new Vector3(this);
6582
}
6683
}

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';
22
export { default as Quaternion } from './Quaternion.js';
33
export { default as Transform } from './Transform.js';
4-
export { default as Vector3 } from './Vector3.js';
4+
export { default as Vector3, type IVector3 } from './Vector3.js';

0 commit comments

Comments
 (0)