Skip to content

Migrate library to TS #851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/core/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type PartialNullable<T> = {
[P in keyof T]?: T[P] | null;
};
55 changes: 33 additions & 22 deletions src/math/Pose.js → src/math/Pose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,78 @@
* @fileOverview
* @author David Gossow - [email protected]
*/
import Vector3, { type IVector3 } from './Vector3.js';
import Quaternion, { type IQuaternion } from './Quaternion.js';
import { type ITransform } from './Transform.js';
import type { PartialNullable } from '../core/types/index.js';

import Vector3 from './Vector3.js';
import Quaternion from './Quaternion.js';
import Transform from './Transform.js';
export interface IPose {
/**
* The ROSLIB.Vector3 describing the position.
*/
position: IVector3;
/**
* The ROSLIB.Quaternion describing the orientation.
*/
orientation: IQuaternion;
}

/**
* A Pose in 3D space. Values are copied into this object.
*/
export default class Pose {
/**
* @param {Object} [options]
* @param {Vector3} [options.position] - The ROSLIB.Vector3 describing the position.
* @param {Quaternion} [options.orientation] - The ROSLIB.Quaternion describing the orientation.
*/
constructor(options) {
options = options || {};
// copy the values into this object if they exist
options = options || {};
this.position = new Vector3(options.position);
this.orientation = new Quaternion(options.orientation);
export default class Pose implements IPose {

position: Vector3;
orientation: Quaternion;

constructor(options?: PartialNullable<IPose>) {
this.position = new Vector3(options?.position);
this.orientation = new Quaternion(options?.orientation);
}

/**
* Apply a transform against this pose.
*
* @param {Transform} tf - The transform to be applied.
*/
applyTransform(tf) {
applyTransform(tf: ITransform) {
this.position.multiplyQuaternion(tf.rotation);
this.position.add(tf.translation);
var tmp = tf.rotation.clone();
const tmp = new Quaternion(tf.rotation);
tmp.multiply(this.orientation);
this.orientation = tmp;
}

/**
* Clone a copy of this pose.
*
* @returns {Pose} The cloned pose.
*/
clone() {
clone(): Pose {
return new Pose(this);
}

/**
* Multiply this pose with another pose without altering this pose.
*
* @returns {Pose} The result of the multiplication.
*/
multiply(pose) {
var p = pose.clone();
multiply(pose: Pose): Pose {
const p = pose.clone();
p.applyTransform({
rotation: this.orientation,
translation: this.position
});
return p;
}

/**
* Compute the inverse of this pose.
*
* @returns {Pose} The inverse of the pose.
*/
getInverse() {
var inverse = this.clone();
getInverse(): Pose {
const inverse = this.clone();
inverse.orientation.invert();
inverse.position.multiplyQuaternion(inverse.orientation);
inverse.position.x *= -1;
Expand Down
73 changes: 48 additions & 25 deletions src/math/Quaternion.js → src/math/Quaternion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,66 @@
* @fileOverview
* @author David Gossow - [email protected]
*/
import type { PartialNullable } from '../core/types/index.js';

export interface IQuaternion {
/**
* The x value.
*/
x: number;
/**
* The y value.
*/
y: number;
/**
* The z value.
*/
z: number;
/**
* The w value.
*/
w: number;
}

/**
* A Quaternion.
*/
export default class Quaternion {
/**
* @param {Object} [options]
* @param {number|null} [options.x=0] - The x value.
* @param {number|null} [options.y=0] - The y value.
* @param {number|null} [options.z=0] - The z value.
* @param {number|null} [options.w=1] - The w value.
*/
constructor(options) {
options = options || {};
this.x = options.x || 0;
this.y = options.y || 0;
this.z = options.z || 0;
this.w = typeof options.w === 'number' ? options.w : 1;
export default class Quaternion implements IQuaternion {
x: number;
y: number;
z: number;
w: number;

constructor(options?: PartialNullable<IQuaternion> | null) {
this.x = options?.x ?? 0;
this.y = options?.y ?? 0;
this.z = options?.z ?? 0;
this.w = typeof options?.w === 'number' ? options.w : 1;
}

/**
* Perform a conjugation on this quaternion.
*/
conjugate() {
conjugate(): void {
this.x *= -1;
this.y *= -1;
this.z *= -1;
}

/**
* Return the norm of this quaternion.
*/
norm() {
norm(): number {
return Math.sqrt(
this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w
);
}

/**
* Perform a normalization on this quaternion.
*/
normalize() {
var l = Math.sqrt(
normalize(): void {
let l = Math.sqrt(
this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w
);
if (l === 0) {
Expand All @@ -57,34 +77,37 @@ export default class Quaternion {
this.w = this.w * l;
}
}

/**
* Convert this quaternion into its inverse.
*/
invert() {
invert(): void {
this.conjugate();
this.normalize();
}

/**
* Set the values of this quaternion to the product of itself and the given quaternion.
*
* @param {Quaternion} q - The quaternion to multiply with.
*/
multiply(q) {
var newX = this.x * q.w + this.y * q.z - this.z * q.y + this.w * q.x;
var newY = -this.x * q.z + this.y * q.w + this.z * q.x + this.w * q.y;
var newZ = this.x * q.y - this.y * q.x + this.z * q.w + this.w * q.z;
var newW = -this.x * q.x - this.y * q.y - this.z * q.z + this.w * q.w;
multiply(q: IQuaternion): void {
const newX = this.x * q.w + this.y * q.z - this.z * q.y + this.w * q.x;
const newY = -this.x * q.z + this.y * q.w + this.z * q.x + this.w * q.y;
const newZ = this.x * q.y - this.y * q.x + this.z * q.w + this.w * q.z;
const newW = -this.x * q.x - this.y * q.y - this.z * q.z + this.w * q.w;
this.x = newX;
this.y = newY;
this.z = newZ;
this.w = newW;
}

/**
* Clone a copy of this quaternion.
*
* @returns {Quaternion} The cloned quaternion.
*/
clone() {
clone(): Quaternion {
return new Quaternion(this);
}
}
31 changes: 0 additions & 31 deletions src/math/Transform.js

This file was deleted.

42 changes: 42 additions & 0 deletions src/math/Transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @fileOverview
* @author David Gossow - [email protected]
*/

import Vector3, { type IVector3 } from './Vector3.js';
import Quaternion, { type IQuaternion } from './Quaternion.js';

export interface ITransform {
/**
* The ROSLIB.Vector3 describing the translation.
*/
translation: IVector3;
/**
* The ROSLIB.Quaternion describing the rotation.
*/
rotation: IQuaternion;
}

/**
* A Transform in 3-space. Values are copied into this object.
*/
export default class Transform implements ITransform {

translation: Vector3;
rotation: Quaternion;

constructor(options: ITransform) {
// Copy the values into this object if they exist
this.translation = new Vector3(options.translation);
this.rotation = new Quaternion(options.rotation);
}

/**
* Clone a copy of this transform.
*
* @returns {Transform} The cloned transform.
*/
clone(): Transform {
return new Transform(this);
}
}
Loading