Skip to content

Commit a1b44a0

Browse files
committed
Convert math structures to Typescript Native
1 parent c9ca3df commit a1b44a0

File tree

8 files changed

+238
-170
lines changed

8 files changed

+238
-170
lines changed

src/core/types/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type PartialNullable<T> = {
2+
[P in keyof T]?: T[P] | null;
3+
};

src/math/Pose.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,78 @@
22
* @fileOverview
33
* @author David Gossow - [email protected]
44
*/
5+
import Vector3, { type IVector3 } from './Vector3.js';
6+
import Quaternion, { type IQuaternion } from './Quaternion.js';
7+
import { type ITransform } from './Transform.js';
8+
import type { PartialNullable } from '../core/types/index.js';
59

6-
import Vector3 from './Vector3.js';
7-
import Quaternion from './Quaternion.js';
8-
import Transform from './Transform.js';
10+
export interface IPose {
11+
/**
12+
* The ROSLIB.Vector3 describing the position.
13+
*/
14+
position: IVector3;
15+
/**
16+
* The ROSLIB.Quaternion describing the orientation.
17+
*/
18+
orientation: IQuaternion;
19+
}
920

1021
/**
1122
* A Pose in 3D space. Values are copied into this object.
1223
*/
13-
export default class Pose {
14-
/**
15-
* @param {Object} [options]
16-
* @param {Vector3} [options.position] - The ROSLIB.Vector3 describing the position.
17-
* @param {Quaternion} [options.orientation] - The ROSLIB.Quaternion describing the orientation.
18-
*/
19-
constructor(options) {
20-
options = options || {};
21-
// copy the values into this object if they exist
22-
options = options || {};
23-
this.position = new Vector3(options.position);
24-
this.orientation = new Quaternion(options.orientation);
24+
export default class Pose implements IPose {
25+
26+
position: Vector3;
27+
orientation: Quaternion;
28+
29+
constructor(options?: PartialNullable<IPose>) {
30+
this.position = new Vector3(options?.position);
31+
this.orientation = new Quaternion(options?.orientation);
2532
}
33+
2634
/**
2735
* Apply a transform against this pose.
2836
*
2937
* @param {Transform} tf - The transform to be applied.
3038
*/
31-
applyTransform(tf) {
39+
applyTransform(tf: ITransform) {
3240
this.position.multiplyQuaternion(tf.rotation);
3341
this.position.add(tf.translation);
34-
var tmp = tf.rotation.clone();
42+
const tmp = new Quaternion(tf.rotation);
3543
tmp.multiply(this.orientation);
3644
this.orientation = tmp;
3745
}
46+
3847
/**
3948
* Clone a copy of this pose.
4049
*
4150
* @returns {Pose} The cloned pose.
4251
*/
43-
clone() {
52+
clone(): Pose {
4453
return new Pose(this);
4554
}
55+
4656
/**
4757
* Multiply this pose with another pose without altering this pose.
4858
*
4959
* @returns {Pose} The result of the multiplication.
5060
*/
51-
multiply(pose) {
52-
var p = pose.clone();
61+
multiply(pose: Pose): Pose {
62+
const p = pose.clone();
5363
p.applyTransform({
5464
rotation: this.orientation,
5565
translation: this.position
5666
});
5767
return p;
5868
}
69+
5970
/**
6071
* Compute the inverse of this pose.
6172
*
6273
* @returns {Pose} The inverse of the pose.
6374
*/
64-
getInverse() {
65-
var inverse = this.clone();
75+
getInverse(): Pose {
76+
const inverse = this.clone();
6677
inverse.orientation.invert();
6778
inverse.position.multiplyQuaternion(inverse.orientation);
6879
inverse.position.x *= -1;

src/math/Quaternion.ts

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

626
/**
727
* A Quaternion.
828
*/
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;
29+
export default class Quaternion implements IQuaternion {
30+
x: number;
31+
y: number;
32+
z: number;
33+
w: number;
34+
35+
constructor(options?: PartialNullable<IQuaternion> | null) {
36+
this.x = options?.x ?? 0;
37+
this.y = options?.y ?? 0;
38+
this.z = options?.z ?? 0;
39+
this.w = typeof options?.w === 'number' ? options.w : 1;
2340
}
41+
2442
/**
2543
* Perform a conjugation on this quaternion.
2644
*/
27-
conjugate() {
45+
conjugate(): void {
2846
this.x *= -1;
2947
this.y *= -1;
3048
this.z *= -1;
3149
}
50+
3251
/**
3352
* Return the norm of this quaternion.
3453
*/
35-
norm() {
54+
norm(): number {
3655
return Math.sqrt(
3756
this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w
3857
);
3958
}
59+
4060
/**
4161
* Perform a normalization on this quaternion.
4262
*/
43-
normalize() {
44-
var l = Math.sqrt(
63+
normalize(): void {
64+
let l = Math.sqrt(
4565
this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w
4666
);
4767
if (l === 0) {
@@ -57,34 +77,37 @@ export default class Quaternion {
5777
this.w = this.w * l;
5878
}
5979
}
80+
6081
/**
6182
* Convert this quaternion into its inverse.
6283
*/
63-
invert() {
84+
invert(): void {
6485
this.conjugate();
6586
this.normalize();
6687
}
88+
6789
/**
6890
* Set the values of this quaternion to the product of itself and the given quaternion.
6991
*
7092
* @param {Quaternion} q - The quaternion to multiply with.
7193
*/
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;
94+
multiply(q: IQuaternion): void {
95+
const newX = this.x * q.w + this.y * q.z - this.z * q.y + this.w * q.x;
96+
const newY = -this.x * q.z + this.y * q.w + this.z * q.x + this.w * q.y;
97+
const newZ = this.x * q.y - this.y * q.x + this.z * q.w + this.w * q.z;
98+
const newW = -this.x * q.x - this.y * q.y - this.z * q.z + this.w * q.w;
7799
this.x = newX;
78100
this.y = newY;
79101
this.z = newZ;
80102
this.w = newW;
81103
}
104+
82105
/**
83106
* Clone a copy of this quaternion.
84107
*
85108
* @returns {Quaternion} The cloned quaternion.
86109
*/
87-
clone() {
110+
clone(): Quaternion {
88111
return new Quaternion(this);
89112
}
90113
}

src/math/Transform.ts

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

6-
import Vector3 from './Vector3.js';
7-
import Quaternion from './Quaternion.js';
6+
import Vector3, { type IVector3 } from './Vector3.js';
7+
import Quaternion, { type IQuaternion } from './Quaternion.js';
8+
9+
export interface ITransform {
10+
/**
11+
* The ROSLIB.Vector3 describing the translation.
12+
*/
13+
translation: IVector3;
14+
/**
15+
* The ROSLIB.Quaternion describing the rotation.
16+
*/
17+
rotation: IQuaternion;
18+
}
819

920
/**
1021
* A Transform in 3-space. Values are copied into this object.
1122
*/
12-
export default class Transform {
13-
/**
14-
* @param {Object} options
15-
* @param {Vector3} options.translation - The ROSLIB.Vector3 describing the translation.
16-
* @param {Quaternion} options.rotation - The ROSLIB.Quaternion describing the rotation.
17-
*/
18-
constructor(options) {
23+
export default class Transform implements ITransform {
24+
25+
translation: Vector3;
26+
rotation: Quaternion;
27+
28+
constructor(options: ITransform) {
1929
// Copy the values into this object if they exist
2030
this.translation = new Vector3(options.translation);
2131
this.rotation = new Quaternion(options.rotation);
2232
}
33+
2334
/**
2435
* Clone a copy of this transform.
2536
*
2637
* @returns {Transform} The cloned transform.
2738
*/
28-
clone() {
39+
clone(): Transform {
2940
return new Transform(this);
3041
}
3142
}

src/math/Vector3.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,82 @@
22
* @fileOverview
33
* @author David Gossow - [email protected]
44
*/
5+
import { type IQuaternion } from './Quaternion.js';
6+
import type { PartialNullable } from '../core/types/index.js';
57

6-
import Quaternion from './Quaternion.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
}

0 commit comments

Comments
 (0)