Skip to content

Commit 29a19d0

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

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

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/index.ts

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

0 commit comments

Comments
 (0)