Skip to content

Commit f594bc2

Browse files
authored
Migrate math module to TypeScript (#906)
* chore(eslint): Enforce newline at EOF with `eol-last` rule Signed-off-by: Drew Hoener <[email protected]> * refactor(math): Rename files to TS. This commit will have lint/compile errors but is necessary for git continuity. Without it, git will think the files have been deleted and recreated because of the diff. Signed-off-by: Drew Hoener <[email protected]> * refactor(Vector3): Refactor Vector3 to typescript module, add interface to represent structure and type methods. Signed-off-by: Drew Hoener <[email protected]> * refactor(Quaternion): Refactor Quaternion to typescript module, add interface to represent structure and type methods. Signed-off-by: Drew Hoener <[email protected]> * feat(types): Add utility types Signed-off-by: Drew Hoener <[email protected]> * refactor(Transform): Refactor Transform to typescript module, add interface to represent structure and type methods. Signed-off-by: Drew Hoener <[email protected]> * refactor(Pose): Refactor Pose to typescript module, add interface to represent structure and type methods. Signed-off-by: Drew Hoener <[email protected]> --------- Signed-off-by: Drew Hoener <[email protected]>
1 parent b6b7183 commit f594bc2

File tree

8 files changed

+185
-105
lines changed

8 files changed

+185
-105
lines changed

eslint.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export default tseslint.config(
6161
quotes: [2, 'single'],
6262
'no-proto': 2,
6363
'linebreak-style': 2,
64-
'key-spacing': [2, {afterColon: true}]
64+
'key-spacing': [2, {afterColon: true}],
65+
'eol-last': ['error', 'always'],
6566
},
6667
files: ['**/*.{js,jsx,ts,tsx,cjs}'],
6768
}

src/math/Pose.js renamed to src/math/Pose.ts

Lines changed: 34 additions & 23 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 { PartialNullable } from '../types/interface-types.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
*
29-
* @param {Transform} tf - The transform to be applied.
37+
* @param {ITransform} 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.js renamed to 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/Transform.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/math/Transform.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @fileOverview
3+
* @author David Gossow - [email protected]
4+
*/
5+
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+
}
19+
20+
/**
21+
* A Transform in 3-space. Values are copied into this object.
22+
*/
23+
export default class Transform implements ITransform {
24+
25+
translation: Vector3;
26+
rotation: Quaternion;
27+
28+
constructor(options: ITransform) {
29+
// Copy the values into this object if they exist
30+
this.translation = new Vector3(options.translation);
31+
this.rotation = new Quaternion(options.rotation);
32+
}
33+
34+
/**
35+
* Clone a copy of this transform.
36+
*
37+
* @returns {Transform} The cloned transform.
38+
*/
39+
clone(): Transform {
40+
return new Transform(this);
41+
}
42+
}

0 commit comments

Comments
 (0)