Skip to content

Commit 1c11f45

Browse files
committed
Convert math structures to Typescript Native
1 parent a293e18 commit 1c11f45

File tree

10 files changed

+166
-113
lines changed

10 files changed

+166
-113
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.js renamed to src/math/Pose.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,43 @@
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
}
2633
/**
2734
* Apply a transform against this pose.
2835
*
2936
* @param {Transform} tf - The transform to be applied.
3037
*/
31-
applyTransform(tf) {
38+
applyTransform(tf: ITransform) {
3239
this.position.multiplyQuaternion(tf.rotation);
3340
this.position.add(tf.translation);
34-
var tmp = tf.rotation.clone();
41+
const tmp = new Quaternion(tf.rotation);
3542
tmp.multiply(this.orientation);
3643
this.orientation = tmp;
3744
}
@@ -40,16 +47,16 @@ export default class Pose {
4047
*
4148
* @returns {Pose} The cloned pose.
4249
*/
43-
clone() {
50+
clone(): Pose {
4451
return new Pose(this);
4552
}
4653
/**
4754
* Multiply this pose with another pose without altering this pose.
4855
*
4956
* @returns {Pose} The result of the multiplication.
5057
*/
51-
multiply(pose) {
52-
var p = pose.clone();
58+
multiply(pose: Pose): Pose {
59+
const p = pose.clone();
5360
p.applyTransform({
5461
rotation: this.orientation,
5562
translation: this.position
@@ -61,8 +68,8 @@ export default class Pose {
6168
*
6269
* @returns {Pose} The inverse of the pose.
6370
*/
64-
getInverse() {
65-
var inverse = this.clone();
71+
getInverse(): Pose {
72+
const inverse = this.clone();
6673
inverse.orientation.invert();
6774
inverse.position.multiplyQuaternion(inverse.orientation);
6875
inverse.position.x *= -1;

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

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,63 @@
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
}
2441
/**
2542
* Perform a conjugation on this quaternion.
2643
*/
27-
conjugate() {
44+
conjugate(): void {
2845
this.x *= -1;
2946
this.y *= -1;
3047
this.z *= -1;
3148
}
3249
/**
3350
* Return the norm of this quaternion.
3451
*/
35-
norm() {
52+
norm(): number {
3653
return Math.sqrt(
3754
this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w
3855
);
3956
}
4057
/**
4158
* Perform a normalization on this quaternion.
4259
*/
43-
normalize() {
44-
var l = Math.sqrt(
60+
normalize(): void {
61+
let l = Math.sqrt(
4562
this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w
4663
);
4764
if (l === 0) {
@@ -60,7 +77,7 @@ export default class Quaternion {
6077
/**
6178
* Convert this quaternion into its inverse.
6279
*/
63-
invert() {
80+
invert(): void {
6481
this.conjugate();
6582
this.normalize();
6683
}
@@ -69,11 +86,11 @@ export default class Quaternion {
6986
*
7087
* @param {Quaternion} q - The quaternion to multiply with.
7188
*/
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;
89+
multiply(q: IQuaternion): void {
90+
const newX = this.x * q.w + this.y * q.z - this.z * q.y + this.w * q.x;
91+
const newY = -this.x * q.z + this.y * q.w + this.z * q.x + this.w * q.y;
92+
const newZ = this.x * q.y - this.y * q.x + this.z * q.w + this.w * q.z;
93+
const newW = -this.x * q.x - this.y * q.y - this.z * q.z + this.w * q.w;
7794
this.x = newX;
7895
this.y = newY;
7996
this.z = newZ;
@@ -84,7 +101,7 @@ export default class Quaternion {
84101
*
85102
* @returns {Quaternion} The cloned quaternion.
86103
*/
87-
clone() {
104+
clone(): Quaternion {
88105
return new Quaternion(this);
89106
}
90107
}

src/math/Transform.js

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

src/math/Transform.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
* Clone a copy of this transform.
35+
*
36+
* @returns {Transform} The cloned transform.
37+
*/
38+
clone(): Transform {
39+
return new Transform(this);
40+
}
41+
}

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

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,44 @@
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
}
2437
/**
2538
* Set the values of this vector to the sum of itself and the given vector.
2639
*
2740
* @param {Vector3} v - The vector to add with.
2841
*/
29-
add(v) {
42+
add(v: IVector3): void {
3043
this.x += v.x;
3144
this.y += v.y;
3245
this.z += v.z;
@@ -36,7 +49,7 @@ export default class Vector3 {
3649
*
3750
* @param {Vector3} v - The vector to subtract with.
3851
*/
39-
subtract(v) {
52+
subtract(v: IVector3): void {
4053
this.x -= v.x;
4154
this.y -= v.y;
4255
this.z -= v.z;
@@ -46,11 +59,11 @@ export default class Vector3 {
4659
*
4760
* @param {Quaternion} q - The quaternion to multiply with.
4861
*/
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;
62+
multiplyQuaternion(q: IQuaternion) {
63+
const ix = q.w * this.x + q.y * this.z - q.z * this.y;
64+
const iy = q.w * this.y + q.z * this.x - q.x * this.z;
65+
const iz = q.w * this.z + q.x * this.y - q.y * this.x;
66+
const iw = -q.x * this.x - q.y * this.y - q.z * this.z;
5467
this.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;
5568
this.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;
5669
this.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;
@@ -60,7 +73,7 @@ export default class Vector3 {
6073
*
6174
* @returns {Vector3} The cloned vector.
6275
*/
63-
clone() {
76+
clone(): Vector3 {
6477
return new Vector3(this);
6578
}
6679
}
File renamed without changes.

0 commit comments

Comments
 (0)