@wandelbots/nova-js 4.3.0-pr.318.c6d70a6 → 4.3.0-pr.318.e48e5ca
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/experimental/math/index.d.mts +22 -2
- package/dist/experimental/math/index.d.mts.map +1 -1
- package/dist/experimental/math/index.mjs +56 -5
- package/dist/experimental/math/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/lib/experimental/math/Pose.ts +67 -10
- package/src/lib/experimental/math/Quaternion.ts +19 -0
|
@@ -24,8 +24,23 @@ declare class Pose implements Pose$1 {
|
|
|
24
24
|
inverse(): Pose;
|
|
25
25
|
/** Apply this pose's transform to a point, returning the transformed point. */
|
|
26
26
|
transformPoint(point: number[]): number[];
|
|
27
|
-
/**
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Compare to another pose using separate position/orientation tolerances,
|
|
29
|
+
* matching wb-robotix's `Pose::isApprox()`: Euclidean distance for
|
|
30
|
+
* position, and quaternion angular distance for orientation - not a naive
|
|
31
|
+
* per-component diff, since two rotation vectors can represent nearly
|
|
32
|
+
* identical rotations while differing componentwise near a
|
|
33
|
+
* canonicalization boundary (e.g. close to the +/-pi wraparound).
|
|
34
|
+
*/
|
|
35
|
+
isApprox(other: Pose$1, deltaPosition?: number, deltaOrientation?: number): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Position + orientation as a 6-element [x, y, z, roll, pitch, yaw] vector
|
|
38
|
+
* (Euler angles in rad, XYZ Tait-Bryan / Rx*Ry*Rz convention), matching
|
|
39
|
+
* wb-robotix's `Pose::toCartesian()`.
|
|
40
|
+
*/
|
|
41
|
+
toCartesian(): number[];
|
|
42
|
+
/** Human-readable `[x, y, z][rx, ry, rz]` representation, matching wb-robotix's `Pose::string()`. */
|
|
43
|
+
toString(precision?: number): string;
|
|
29
44
|
toJSON(): Pose$1;
|
|
30
45
|
}
|
|
31
46
|
//#endregion
|
|
@@ -111,6 +126,11 @@ declare class Quaternion implements QuaternionData {
|
|
|
111
126
|
conjugate(): Quaternion;
|
|
112
127
|
/** Inverse rotation. Equivalent to `conjugate()` since a `Pose`'s orientation is always a unit quaternion. */
|
|
113
128
|
inverse(): Quaternion;
|
|
129
|
+
/**
|
|
130
|
+
* The angle (in rad, always within [0, pi]) of the rotation that takes
|
|
131
|
+
* `other` to `this`, matching Eigen's built-in `Quaternion::angularDistance()`.
|
|
132
|
+
*/
|
|
133
|
+
angularDistance(other: QuaternionData): number;
|
|
114
134
|
/** Rotate a 3D vector by this quaternion. */
|
|
115
135
|
rotateVector(v: number[]): number[];
|
|
116
136
|
toJSON(): QuaternionData;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../../src/lib/experimental/math/Pose.ts","../../../src/lib/experimental/math/augmentMath.ts","../../../src/lib/experimental/math/Quaternion.ts"],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../../src/lib/experimental/math/Pose.ts","../../../src/lib/experimental/math/augmentMath.ts","../../../src/lib/experimental/math/Quaternion.ts"],"mappings":";;;;;;;;;cA+Ba,gBAAgB;WAClB;WACA;EAGP,YAAA,qBACA;SAkBK,KAAK,MAAM,SAAW;SAUtB,YAAY;;;;;;;EAUnB,SAAS,OAAO,SAAW;;EAc3B,WAAW;;EAWX,eAAe;;;;;;;;;EAaf,SACE,OAAO,QACP,wBACA;;;;;;EA0BF;;EAWA,SAAS;EAKT,UAAU;;;;KC1JP,iBAAiB;KAEjB,kBAAkB,MAAM,cAAc,GAAG,8BACzC,QAAQ,gBAAgB;KAOxB;KAaA,iBACH,GACA,oCACE,wBAAwB,qBACxB,IACA,kBAAkB,kBAChB,OACA,0BAA0B,OACxB,kBAAkB,OAAO,qBACzB,sBACK,WAAW,IAAI,kBAAkB,EAAE,QAAQ,sBAC9C;;;;;;;KAQE,kBACV,GACA,yCACE,oBAAoB,iBAAiB,GAAG;;;;;;;;iBA2D5B,aAAa,GAAG,OAAO,IAAI,kBAAkB;KAKxD,uBACH,GACA,oCACE,wBAAwB,qBACxB,IACA,eAAc,YAAY,MAAM,cAAc,UACxC,MAAM,MAAM,QAAQ,kBAAkB,MAC1C,sBACK,WAAW,IAAI,uBAAuB,EAAE,QAAQ,sBACnD;;;;;;KAOH,mBAAmB,KAAK,uBAAuB;KAExC,eAAe,KAAK;WACrB,KAAK,mBAAmB;;;;;;;;iBA6BnB,YAAY,MAAM,OAAO;;;;;;;;;;KCzJ7B;EAAmB;EAAW;EAAW;EAAW;;cAInD,sBAAsB;WACxB;WACA;WACA;WACA;EAEG,YAAA,YAAO,YAAO,YAAO;SAO1B,YAAY;;;;;;SASZ,mBAAmB,2BAA2B;;;;;;;;;;;EAwBrD;;EAaA,SAAS,OAAO,iBAAiB;;EAYjC,aAAa;;EAKb,WAAW;;;;;EAQX,gBAAgB,OAAO;;EAgBvB,aAAa;EAmBb,UAAU"}
|
|
@@ -68,6 +68,20 @@ var Quaternion = class Quaternion {
|
|
|
68
68
|
inverse() {
|
|
69
69
|
return this.conjugate();
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* The angle (in rad, always within [0, pi]) of the rotation that takes
|
|
73
|
+
* `other` to `this`, matching Eigen's built-in `Quaternion::angularDistance()`.
|
|
74
|
+
*/
|
|
75
|
+
angularDistance(other) {
|
|
76
|
+
const relative = this.multiply({
|
|
77
|
+
w: other.w,
|
|
78
|
+
x: -other.x,
|
|
79
|
+
y: -other.y,
|
|
80
|
+
z: -other.z
|
|
81
|
+
});
|
|
82
|
+
const vecNorm = Math.sqrt(relative.x * relative.x + relative.y * relative.y + relative.z * relative.z);
|
|
83
|
+
return 2 * Math.atan2(vecNorm, Math.abs(relative.w));
|
|
84
|
+
}
|
|
71
85
|
/** Rotate a 3D vector by this quaternion. */
|
|
72
86
|
rotateVector(v) {
|
|
73
87
|
const vx = v[0] ?? 0;
|
|
@@ -99,6 +113,7 @@ const ZERO_VECTOR = [
|
|
|
99
113
|
0,
|
|
100
114
|
0
|
|
101
115
|
];
|
|
116
|
+
const TOLERANCE_MILLI = .001;
|
|
102
117
|
function addVectors(a, b) {
|
|
103
118
|
return [
|
|
104
119
|
(a[0] ?? 0) + (b[0] ?? 0),
|
|
@@ -113,6 +128,9 @@ function negateVector(a) {
|
|
|
113
128
|
-(a[2] ?? 0)
|
|
114
129
|
];
|
|
115
130
|
}
|
|
131
|
+
function isFiniteVector3(v) {
|
|
132
|
+
return v.length === 3 && v.every((n) => Number.isFinite(n));
|
|
133
|
+
}
|
|
116
134
|
/**
|
|
117
135
|
* A `Pose` (position + axis-angle orientation) with methods for composing
|
|
118
136
|
* and inverting transforms. Instances are plain-data compatible with the
|
|
@@ -124,8 +142,10 @@ var Pose = class Pose {
|
|
|
124
142
|
position;
|
|
125
143
|
orientation;
|
|
126
144
|
constructor(position = ZERO_VECTOR, orientation = ZERO_VECTOR) {
|
|
127
|
-
|
|
128
|
-
|
|
145
|
+
if (!isFiniteVector3(position)) throw new Error(`Pose constructor: position must be an array of 3 finite numbers, got ${JSON.stringify(position)}`);
|
|
146
|
+
if (!isFiniteVector3(orientation)) throw new Error(`Pose constructor: orientation must be an array of 3 finite numbers, got ${JSON.stringify(orientation)}`);
|
|
147
|
+
this.position = [...position];
|
|
148
|
+
this.orientation = [...orientation];
|
|
129
149
|
}
|
|
130
150
|
static from(pose) {
|
|
131
151
|
if (pose instanceof Pose) return pose;
|
|
@@ -158,11 +178,42 @@ var Pose = class Pose {
|
|
|
158
178
|
const q = Quaternion.fromRotationVector(this.orientation);
|
|
159
179
|
return addVectors(this.position, q.rotateVector(point));
|
|
160
180
|
}
|
|
161
|
-
/**
|
|
162
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Compare to another pose using separate position/orientation tolerances,
|
|
183
|
+
* matching wb-robotix's `Pose::isApprox()`: Euclidean distance for
|
|
184
|
+
* position, and quaternion angular distance for orientation - not a naive
|
|
185
|
+
* per-component diff, since two rotation vectors can represent nearly
|
|
186
|
+
* identical rotations while differing componentwise near a
|
|
187
|
+
* canonicalization boundary (e.g. close to the +/-pi wraparound).
|
|
188
|
+
*/
|
|
189
|
+
isApprox(other, deltaPosition = TOLERANCE_MILLI, deltaOrientation = TOLERANCE_MILLI) {
|
|
163
190
|
const otherPosition = other.position ?? ZERO_VECTOR;
|
|
164
191
|
const otherOrientation = other.orientation ?? ZERO_VECTOR;
|
|
165
|
-
|
|
192
|
+
const positionDistance = Math.sqrt(this.position.reduce((sum, v, i) => sum + (v - (otherPosition[i] ?? 0)) ** 2, 0));
|
|
193
|
+
const orientationDistance = Quaternion.fromRotationVector(this.orientation).angularDistance(Quaternion.fromRotationVector(otherOrientation));
|
|
194
|
+
return positionDistance <= deltaPosition && orientationDistance <= deltaOrientation;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Position + orientation as a 6-element [x, y, z, roll, pitch, yaw] vector
|
|
198
|
+
* (Euler angles in rad, XYZ Tait-Bryan / Rx*Ry*Rz convention), matching
|
|
199
|
+
* wb-robotix's `Pose::toCartesian()`.
|
|
200
|
+
*/
|
|
201
|
+
toCartesian() {
|
|
202
|
+
const { w, x, y, z } = Quaternion.fromRotationVector(this.orientation);
|
|
203
|
+
const roll = Math.atan2(2 * (w * x - y * z), 1 - 2 * (x * x + y * y));
|
|
204
|
+
const pitch = Math.asin(Math.min(1, Math.max(-1, 2 * (x * z + w * y))));
|
|
205
|
+
const yaw = Math.atan2(2 * (w * z - x * y), 1 - 2 * (y * y + z * z));
|
|
206
|
+
return [
|
|
207
|
+
...this.position,
|
|
208
|
+
roll,
|
|
209
|
+
pitch,
|
|
210
|
+
yaw
|
|
211
|
+
];
|
|
212
|
+
}
|
|
213
|
+
/** Human-readable `[x, y, z][rx, ry, rz]` representation, matching wb-robotix's `Pose::string()`. */
|
|
214
|
+
toString(precision = 6) {
|
|
215
|
+
const fmt = (v) => Number(v.toPrecision(precision));
|
|
216
|
+
return `[${this.position.map(fmt).join(", ")}][${this.orientation.map(fmt).join(", ")}]`;
|
|
166
217
|
}
|
|
167
218
|
toJSON() {
|
|
168
219
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/lib/experimental/math/Quaternion.ts","../../../src/lib/experimental/math/Pose.ts","../../../src/lib/experimental/math/augmentMath.ts"],"sourcesContent":["/**\n * A unit quaternion representing a 3D rotation. Mirrors the subset of\n * `Eigen::Quaternion`'s API (see wb-robotix's `QuaternionBasePlugin.h`) that\n * `Pose` composition needs: Hamilton product, conjugate/inverse, vector\n * rotation, and axis-angle rotation-vector conversion. Instances are\n * immutable - every method returns a new `Quaternion`.\n */\n\nexport type QuaternionData = { w: number; x: number; y: number; z: number }\n\nconst EPSILON = 1e-12\n\nexport class Quaternion implements QuaternionData {\n readonly w: number\n readonly x: number\n readonly y: number\n readonly z: number\n\n constructor(w = 1, x = 0, y = 0, z = 0) {\n this.w = w\n this.x = x\n this.y = y\n this.z = z\n }\n\n static identity(): Quaternion {\n return new Quaternion()\n }\n\n /**\n * Construct from an axis-angle rotation vector [rx, ry, rz] (magnitude =\n * angle in rad), matching `Eigen::Quaternion::FromRotationVector` (ported\n * from wb-robotix's `QuaternionBasePlugin.h`).\n */\n static fromRotationVector(rotationVector: number[]): Quaternion {\n const rx = rotationVector[0] ?? 0\n const ry = rotationVector[1] ?? 0\n const rz = rotationVector[2] ?? 0\n\n const angle = Math.sqrt(rx * rx + ry * ry + rz * rz)\n if (angle < EPSILON) {\n return Quaternion.identity()\n }\n const half = angle / 2\n const s = Math.sin(half) / angle\n return new Quaternion(Math.cos(half), rx * s, ry * s, rz * s)\n }\n\n /**\n * Rotation vector [rx, ry, rz] representation (magnitude = angle in rad),\n * matching `Eigen::Quaternion::toRotationVector()` (ported from\n * wb-robotix's `QuaternionBasePlugin.h`): using `atan2` rather than `acos`\n * keeps the angle canonically within [0, pi] regardless of which of the\n * two antipodal unit quaternions (this or its negation) represents the\n * rotation, rather than acos's [0, 2*pi] range - important since composed\n * poses need to match the same canonical rotation vector the robot\n * controller itself would report for a pose.\n */\n toRotationVector(): number[] {\n const vecNorm = Math.sqrt(\n this.x * this.x + this.y * this.y + this.z * this.z,\n )\n if (vecNorm < EPSILON) {\n return [0, 0, 0]\n }\n const angle = 2 * Math.atan2(vecNorm, Math.abs(this.w))\n const scale = (this.w >= 0 ? angle : -angle) / vecNorm\n return [this.x * scale, this.y * scale, this.z * scale]\n }\n\n /** Hamilton product: `a.multiply(b)` applied to a vector rotates by `b` first, then by `a`. */\n multiply(other: QuaternionData): Quaternion {\n const { w: aw, x: ax, y: ay, z: az } = this\n const { w: bw, x: bx, y: by, z: bz } = other\n return new Quaternion(\n aw * bw - ax * bx - ay * by - az * bz,\n aw * bx + ax * bw + ay * bz - az * by,\n aw * by - ax * bz + ay * bw + az * bx,\n aw * bz + ax * by - ay * bx + az * bw,\n )\n }\n\n /** Conjugate: negates the vector part. Equal to `inverse()` for unit quaternions. */\n conjugate(): Quaternion {\n return new Quaternion(this.w, -this.x, -this.y, -this.z)\n }\n\n /** Inverse rotation. Equivalent to `conjugate()` since a `Pose`'s orientation is always a unit quaternion. */\n inverse(): Quaternion {\n return this.conjugate()\n }\n\n /** Rotate a 3D vector by this quaternion. */\n rotateVector(v: number[]): number[] {\n const vx = v[0] ?? 0\n const vy = v[1] ?? 0\n const vz = v[2] ?? 0\n const { w, x, y, z } = this\n\n // t = 2 * cross(q.xyz, v)\n const tx = 2 * (y * vz - z * vy)\n const ty = 2 * (z * vx - x * vz)\n const tz = 2 * (x * vy - y * vx)\n\n // v' = v + w*t + cross(q.xyz, t)\n return [\n vx + w * tx + (y * tz - z * ty),\n vy + w * ty + (z * tx - x * tz),\n vz + w * tz + (x * ty - y * tx),\n ]\n }\n\n toJSON(): QuaternionData {\n return { w: this.w, x: this.x, y: this.y, z: this.z }\n }\n}\n","import type { Pose as PoseData } from \"@wandelbots/nova-api/v2\"\nimport { Quaternion } from \"./Quaternion.ts\"\n\nconst ZERO_VECTOR = [0, 0, 0]\n\nfunction addVectors(a: number[], b: number[]): number[] {\n return [\n (a[0] ?? 0) + (b[0] ?? 0),\n (a[1] ?? 0) + (b[1] ?? 0),\n (a[2] ?? 0) + (b[2] ?? 0),\n ]\n}\n\nfunction negateVector(a: number[]): number[] {\n return [-(a[0] ?? 0), -(a[1] ?? 0), -(a[2] ?? 0)]\n}\n\n/**\n * A `Pose` (position + axis-angle orientation) with methods for composing\n * and inverting transforms. Instances are plain-data compatible with the\n * wire-format `PoseData` type (own `position`/`orientation` properties only,\n * no enumerable methods), so they can be passed directly back into API calls\n * that expect a pose.\n */\nexport class Pose implements PoseData {\n readonly position: number[]\n readonly orientation: number[]\n\n constructor(\n position: number[] = ZERO_VECTOR,\n orientation: number[] = ZERO_VECTOR,\n ) {\n this.position = position\n this.orientation = orientation\n }\n\n static from(pose: PoseData): Pose {\n if (pose instanceof Pose) {\n return pose\n }\n return new Pose(\n pose.position ?? ZERO_VECTOR,\n pose.orientation ?? ZERO_VECTOR,\n )\n }\n\n static identity(): Pose {\n return new Pose()\n }\n\n /**\n * Compose this pose with `other`, treating `other` as being expressed in\n * this pose's coordinate frame. Equivalent to the homogeneous transform\n * product `this * other`: applying the result to a point is the same as\n * applying `other` first, then `this`.\n */\n multiply(other: PoseData): Pose {\n const q1 = Quaternion.fromRotationVector(this.orientation)\n const q2 = Quaternion.fromRotationVector(other.orientation ?? ZERO_VECTOR)\n\n const position = addVectors(\n this.position,\n q1.rotateVector(other.position ?? ZERO_VECTOR),\n )\n const orientation = q1.multiply(q2).toRotationVector()\n\n return new Pose(position, orientation)\n }\n\n /** The inverse transform, such that `pose.multiply(pose.inverse())` is the identity pose. */\n inverse(): Pose {\n const qInverse = Quaternion.fromRotationVector(this.orientation).inverse()\n\n const position = qInverse.rotateVector(negateVector(this.position))\n // Negating an axis-angle vector gives the inverse rotation directly.\n const orientation = negateVector(this.orientation)\n\n return new Pose(position, orientation)\n }\n\n /** Apply this pose's transform to a point, returning the transformed point. */\n transformPoint(point: number[]): number[] {\n const q = Quaternion.fromRotationVector(this.orientation)\n return addVectors(this.position, q.rotateVector(point))\n }\n\n /** Compare to another pose within a tolerance, since floating-point pose math rarely produces exact equality. */\n isApprox(other: PoseData, epsilon = 1e-9): boolean {\n const otherPosition = other.position ?? ZERO_VECTOR\n const otherOrientation = other.orientation ?? ZERO_VECTOR\n\n return (\n this.position.every(\n (v, i) => Math.abs(v - (otherPosition[i] ?? 0)) <= epsilon,\n ) &&\n this.orientation.every(\n (v, i) => Math.abs(v - (otherOrientation[i] ?? 0)) <= epsilon,\n )\n )\n }\n\n toJSON(): PoseData {\n return { position: this.position, orientation: this.orientation }\n }\n}\n","import type { Pose as PoseData } from \"@wandelbots/nova-api/v2\"\nimport type { Nova } from \"../../Nova.ts\"\nimport { Pose } from \"./Pose.ts\"\n\ntype PoseKeys = keyof PoseData\n\ntype IsExactlyPoseData<T> = [Exclude<keyof T, PoseKeys>] extends [never]\n ? [Exclude<PoseKeys, keyof T>] extends [never]\n ? true\n : false\n : false\n\n// Depth cap avoids \"type instantiation is excessively deep\" on nova-api's\n// large (and possibly cyclic, e.g. compound colliders) generated type graph.\ntype MaxDepth = [\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n]\n\ntype AugmentPosesCore<\n T,\n Depth extends readonly unknown[],\n> = Depth[\"length\"] extends MaxDepth[\"length\"]\n ? T\n : IsExactlyPoseData<T> extends true\n ? Pose\n : T extends readonly (infer U)[]\n ? DeepPoseAugmented<U, [...Depth, unknown]>[]\n : T extends object\n ? { [K in keyof T]: DeepPoseAugmented<T[K], [...Depth, unknown]> }\n : T\n\n/**\n * Type-level counterpart to `augmentPoses`: recursively replaces any\n * `PoseData`-shaped field with `Pose` (distributing over unions, e.g. so\n * optional `Pose | undefined` fields keep the `| undefined`), so the static\n * type matches what's actually returned at runtime.\n */\nexport type DeepPoseAugmented<\n T,\n Depth extends readonly unknown[] = [],\n> = T extends unknown ? AugmentPosesCore<T, Depth> : never\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n !(value instanceof Pose)\n )\n}\n\nfunction isNumberTriple(value: unknown): value is number[] {\n return (\n Array.isArray(value) &&\n value.length === 3 &&\n value.every((n) => typeof n === \"number\")\n )\n}\n\n/**\n * Structural check for the wire shape of `Pose` (`{ position, orientation }`,\n * each a 3-number array, and nothing else). This API has no other type with\n * this exact shape, so it reliably identifies `Pose` values without needing\n * per-endpoint knowledge of which fields are poses.\n */\nfunction isPoseShape(value: Record<string, unknown>): boolean {\n return (\n Object.keys(value).length === 2 &&\n isNumberTriple(value.position) &&\n isNumberTriple(value.orientation)\n )\n}\n\nfunction augmentPosesInPlace(value: unknown): void {\n if (Array.isArray(value)) {\n for (const item of value) {\n augmentPosesInPlace(item)\n }\n return\n }\n\n if (isPlainObject(value)) {\n if (isPoseShape(value)) {\n Object.setPrototypeOf(value, Pose.prototype)\n } else {\n for (const key of Object.keys(value)) {\n augmentPosesInPlace(value[key])\n }\n }\n }\n}\n\n/**\n * Recursively walks a parsed API response/request body, upgrading any\n * `Pose`-shaped objects in place to `Pose` instances so they gain math\n * methods (`multiply`, `inverse`, ...) while remaining JSON/wire-compatible.\n * Used by `augmentMath` for `nova.api.*` responses; exported so it can also\n * be applied manually to e.g. websocket messages.\n */\nexport function augmentPoses<T>(value: T): DeepPoseAugmented<T> {\n augmentPosesInPlace(value)\n return value as DeepPoseAugmented<T>\n}\n\ntype WithAugmentedPosesCore<\n T,\n Depth extends readonly unknown[],\n> = Depth[\"length\"] extends MaxDepth[\"length\"]\n ? T\n : T extends (...args: infer A) => Promise<infer R>\n ? (...args: A) => Promise<DeepPoseAugmented<R>>\n : T extends object\n ? { [K in keyof T]: WithAugmentedPosesCore<T[K], [...Depth, unknown]> }\n : T\n\n/**\n * Recursively maps every method on `Nova[\"api\"]` (and its nested API groups)\n * so calls are typed as returning `DeepPoseAugmented` results, matching what\n * `augmentMath` does at runtime.\n */\ntype WithAugmentedPoses<T> = WithAugmentedPosesCore<T, []>\n\nexport type NovaWithMath = Omit<Nova, \"api\"> & {\n readonly api: WithAugmentedPoses<Nova[\"api\"]>\n}\n\nfunction wrapWithPoseAugmentation<T extends object>(target: T): T {\n return new Proxy(target, {\n get(t, prop) {\n const value = Reflect.get(t, prop, t)\n if (typeof value === \"function\") {\n return (...args: unknown[]) => {\n const result = (value as (...a: unknown[]) => unknown).apply(t, args)\n return result instanceof Promise\n ? result.then((data) => augmentPoses(data))\n : result\n }\n }\n if (value !== null && typeof value === \"object\") {\n return wrapWithPoseAugmentation(value)\n }\n return value\n },\n }) as T\n}\n\n/**\n * Wraps a `Nova` instance so every `nova.api.*` call automatically upgrades\n * `Pose`-shaped fields in its response to `Pose` instances (with math\n * methods like `multiply`/`inverse`). Returns a new proxied view - does not\n * mutate the original `nova` instance.\n */\nexport function augmentMath(nova: Nova): NovaWithMath {\n const proxy = new Proxy(nova, {\n get(target, prop) {\n if (prop === \"api\") {\n return wrapWithPoseAugmentation(target.api)\n }\n // receiver is `target`, not the proxy, so `this` inside Nova's own\n // methods stays bound to the original (un-proxied) instance.\n return Reflect.get(target, prop, target)\n },\n })\n\n return proxy as unknown as NovaWithMath\n}\n"],"mappings":";AAUA,MAAM,UAAU;AAEhB,IAAa,aAAb,MAAa,WAAqC;CAChD;CACA;CACA;CACA;CAEA,YAAY,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG;EACtC,KAAK,IAAI;EACT,KAAK,IAAI;EACT,KAAK,IAAI;EACT,KAAK,IAAI;CACX;CAEA,OAAO,WAAuB;EAC5B,OAAO,IAAI,WAAW;CACxB;;;;;;CAOA,OAAO,mBAAmB,gBAAsC;EAC9D,MAAM,KAAK,eAAe,MAAM;EAChC,MAAM,KAAK,eAAe,MAAM;EAChC,MAAM,KAAK,eAAe,MAAM;EAEhC,MAAM,QAAQ,KAAK,KAAK,KAAK,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A,SAAS,oBAAoB,OAAsB;CACjD,IAAI,MAAM,QAAQ,KAAK,GAAG;EACxB,KAAK,MAAM,QAAQ,OACjB,oBAAoB,IAAI;EAE1B;CACF;CAEA,IAAI,cAAc,KAAK,GACrB,IAAI,YAAY,KAAK,GACnB,OAAO,eAAe,OAAO,KAAK,SAAS;MAE3C,KAAK,MAAM,OAAO,OAAO,KAAK,KAAK,GACjC,oBAAoB,MAAM,IAAI;AAItC;;;;;;;;AASA,SAAgB,aAAgB,OAAgC;CAC9D,oBAAoB,KAAK;CACzB,OAAO;AACT;AAwBA,SAAS,yBAA2C,QAAc;CAChE,OAAO,IAAI,MAAM,QAAQ,EACvB,IAAI,GAAG,MAAM;EACX,MAAM,QAAQ,QAAQ,IAAI,GAAG,MAAM,CAAC;EACpC,IAAI,OAAO,UAAU,YACnB,QAAQ,GAAG,SAAoB;GAC7B,MAAM,SAAU,MAAuC,MAAM,GAAG,IAAI;GACpE,OAAO,kBAAkB,UACrB,OAAO,MAAM,SAAS,aAAa,IAAI,CAAC,IACxC;EACN;EAEF,IAAI,UAAU,QAAQ,OAAO,UAAU,UACrC,OAAO,yBAAyB,KAAK;EAEvC,OAAO;CACT,EACF,CAAC;AACH;;;;;;;AAQA,SAAgB,YAAY,MAA0B;CAYpD,OAAO,IAXW,MAAM,MAAM,EAC5B,IAAI,QAAQ,MAAM;EAChB,IAAI,SAAS,OACX,OAAO,yBAAyB,OAAO,GAAG;EAI5C,OAAO,QAAQ,IAAI,QAAQ,MAAM,MAAM;CACzC,EACF,CAEW;AACb"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/lib/experimental/math/Quaternion.ts","../../../src/lib/experimental/math/Pose.ts","../../../src/lib/experimental/math/augmentMath.ts"],"sourcesContent":["/**\n * A unit quaternion representing a 3D rotation. Mirrors the subset of\n * `Eigen::Quaternion`'s API (see wb-robotix's `QuaternionBasePlugin.h`) that\n * `Pose` composition needs: Hamilton product, conjugate/inverse, vector\n * rotation, and axis-angle rotation-vector conversion. Instances are\n * immutable - every method returns a new `Quaternion`.\n */\n\nexport type QuaternionData = { w: number; x: number; y: number; z: number }\n\nconst EPSILON = 1e-12\n\nexport class Quaternion implements QuaternionData {\n readonly w: number\n readonly x: number\n readonly y: number\n readonly z: number\n\n constructor(w = 1, x = 0, y = 0, z = 0) {\n this.w = w\n this.x = x\n this.y = y\n this.z = z\n }\n\n static identity(): Quaternion {\n return new Quaternion()\n }\n\n /**\n * Construct from an axis-angle rotation vector [rx, ry, rz] (magnitude =\n * angle in rad), matching `Eigen::Quaternion::FromRotationVector` (ported\n * from wb-robotix's `QuaternionBasePlugin.h`).\n */\n static fromRotationVector(rotationVector: number[]): Quaternion {\n const rx = rotationVector[0] ?? 0\n const ry = rotationVector[1] ?? 0\n const rz = rotationVector[2] ?? 0\n\n const angle = Math.sqrt(rx * rx + ry * ry + rz * rz)\n if (angle < EPSILON) {\n return Quaternion.identity()\n }\n const half = angle / 2\n const s = Math.sin(half) / angle\n return new Quaternion(Math.cos(half), rx * s, ry * s, rz * s)\n }\n\n /**\n * Rotation vector [rx, ry, rz] representation (magnitude = angle in rad),\n * matching `Eigen::Quaternion::toRotationVector()` (ported from\n * wb-robotix's `QuaternionBasePlugin.h`): using `atan2` rather than `acos`\n * keeps the angle canonically within [0, pi] regardless of which of the\n * two antipodal unit quaternions (this or its negation) represents the\n * rotation, rather than acos's [0, 2*pi] range - important since composed\n * poses need to match the same canonical rotation vector the robot\n * controller itself would report for a pose.\n */\n toRotationVector(): number[] {\n const vecNorm = Math.sqrt(\n this.x * this.x + this.y * this.y + this.z * this.z,\n )\n if (vecNorm < EPSILON) {\n return [0, 0, 0]\n }\n const angle = 2 * Math.atan2(vecNorm, Math.abs(this.w))\n const scale = (this.w >= 0 ? angle : -angle) / vecNorm\n return [this.x * scale, this.y * scale, this.z * scale]\n }\n\n /** Hamilton product: `a.multiply(b)` applied to a vector rotates by `b` first, then by `a`. */\n multiply(other: QuaternionData): Quaternion {\n const { w: aw, x: ax, y: ay, z: az } = this\n const { w: bw, x: bx, y: by, z: bz } = other\n return new Quaternion(\n aw * bw - ax * bx - ay * by - az * bz,\n aw * bx + ax * bw + ay * bz - az * by,\n aw * by - ax * bz + ay * bw + az * bx,\n aw * bz + ax * by - ay * bx + az * bw,\n )\n }\n\n /** Conjugate: negates the vector part. Equal to `inverse()` for unit quaternions. */\n conjugate(): Quaternion {\n return new Quaternion(this.w, -this.x, -this.y, -this.z)\n }\n\n /** Inverse rotation. Equivalent to `conjugate()` since a `Pose`'s orientation is always a unit quaternion. */\n inverse(): Quaternion {\n return this.conjugate()\n }\n\n /**\n * The angle (in rad, always within [0, pi]) of the rotation that takes\n * `other` to `this`, matching Eigen's built-in `Quaternion::angularDistance()`.\n */\n angularDistance(other: QuaternionData): number {\n const relative = this.multiply({\n w: other.w,\n x: -other.x,\n y: -other.y,\n z: -other.z,\n })\n const vecNorm = Math.sqrt(\n relative.x * relative.x +\n relative.y * relative.y +\n relative.z * relative.z,\n )\n return 2 * Math.atan2(vecNorm, Math.abs(relative.w))\n }\n\n /** Rotate a 3D vector by this quaternion. */\n rotateVector(v: number[]): number[] {\n const vx = v[0] ?? 0\n const vy = v[1] ?? 0\n const vz = v[2] ?? 0\n const { w, x, y, z } = this\n\n // t = 2 * cross(q.xyz, v)\n const tx = 2 * (y * vz - z * vy)\n const ty = 2 * (z * vx - x * vz)\n const tz = 2 * (x * vy - y * vx)\n\n // v' = v + w*t + cross(q.xyz, t)\n return [\n vx + w * tx + (y * tz - z * ty),\n vy + w * ty + (z * tx - x * tz),\n vz + w * tz + (x * ty - y * tx),\n ]\n }\n\n toJSON(): QuaternionData {\n return { w: this.w, x: this.x, y: this.y, z: this.z }\n }\n}\n","import type { Pose as PoseData } from \"@wandelbots/nova-api/v2\"\nimport { Quaternion } from \"./Quaternion.ts\"\n\nconst ZERO_VECTOR = [0, 0, 0]\n\n// Matches wb-robotix's Constants.h TOLERANCE_MILLI, the default isApprox tolerance.\nconst TOLERANCE_MILLI = 1e-3\n\nfunction addVectors(a: number[], b: number[]): number[] {\n return [\n (a[0] ?? 0) + (b[0] ?? 0),\n (a[1] ?? 0) + (b[1] ?? 0),\n (a[2] ?? 0) + (b[2] ?? 0),\n ]\n}\n\nfunction negateVector(a: number[]): number[] {\n return [-(a[0] ?? 0), -(a[1] ?? 0), -(a[2] ?? 0)]\n}\n\nfunction isFiniteVector3(v: number[]): boolean {\n return v.length === 3 && v.every((n) => Number.isFinite(n))\n}\n\n/**\n * A `Pose` (position + axis-angle orientation) with methods for composing\n * and inverting transforms. Instances are plain-data compatible with the\n * wire-format `PoseData` type (own `position`/`orientation` properties only,\n * no enumerable methods), so they can be passed directly back into API calls\n * that expect a pose.\n */\nexport class Pose implements PoseData {\n readonly position: number[]\n readonly orientation: number[]\n\n constructor(\n position: number[] = ZERO_VECTOR,\n orientation: number[] = ZERO_VECTOR,\n ) {\n if (!isFiniteVector3(position)) {\n throw new Error(\n `Pose constructor: position must be an array of 3 finite numbers, got ${JSON.stringify(position)}`,\n )\n }\n if (!isFiniteVector3(orientation)) {\n throw new Error(\n `Pose constructor: orientation must be an array of 3 finite numbers, got ${JSON.stringify(orientation)}`,\n )\n }\n // Copy defensively so mutating the caller's arrays afterward can't\n // change this (supposedly immutable) instance's state.\n this.position = [...position]\n this.orientation = [...orientation]\n }\n\n static from(pose: PoseData): Pose {\n if (pose instanceof Pose) {\n return pose\n }\n return new Pose(\n pose.position ?? ZERO_VECTOR,\n pose.orientation ?? ZERO_VECTOR,\n )\n }\n\n static identity(): Pose {\n return new Pose()\n }\n\n /**\n * Compose this pose with `other`, treating `other` as being expressed in\n * this pose's coordinate frame. Equivalent to the homogeneous transform\n * product `this * other`: applying the result to a point is the same as\n * applying `other` first, then `this`.\n */\n multiply(other: PoseData): Pose {\n const q1 = Quaternion.fromRotationVector(this.orientation)\n const q2 = Quaternion.fromRotationVector(other.orientation ?? ZERO_VECTOR)\n\n const position = addVectors(\n this.position,\n q1.rotateVector(other.position ?? ZERO_VECTOR),\n )\n const orientation = q1.multiply(q2).toRotationVector()\n\n return new Pose(position, orientation)\n }\n\n /** The inverse transform, such that `pose.multiply(pose.inverse())` is the identity pose. */\n inverse(): Pose {\n const qInverse = Quaternion.fromRotationVector(this.orientation).inverse()\n\n const position = qInverse.rotateVector(negateVector(this.position))\n // Negating an axis-angle vector gives the inverse rotation directly.\n const orientation = negateVector(this.orientation)\n\n return new Pose(position, orientation)\n }\n\n /** Apply this pose's transform to a point, returning the transformed point. */\n transformPoint(point: number[]): number[] {\n const q = Quaternion.fromRotationVector(this.orientation)\n return addVectors(this.position, q.rotateVector(point))\n }\n\n /**\n * Compare to another pose using separate position/orientation tolerances,\n * matching wb-robotix's `Pose::isApprox()`: Euclidean distance for\n * position, and quaternion angular distance for orientation - not a naive\n * per-component diff, since two rotation vectors can represent nearly\n * identical rotations while differing componentwise near a\n * canonicalization boundary (e.g. close to the +/-pi wraparound).\n */\n isApprox(\n other: PoseData,\n deltaPosition = TOLERANCE_MILLI,\n deltaOrientation = TOLERANCE_MILLI,\n ): boolean {\n const otherPosition = other.position ?? ZERO_VECTOR\n const otherOrientation = other.orientation ?? ZERO_VECTOR\n\n const positionDistance = Math.sqrt(\n this.position.reduce(\n (sum, v, i) => sum + (v - (otherPosition[i] ?? 0)) ** 2,\n 0,\n ),\n )\n const orientationDistance = Quaternion.fromRotationVector(\n this.orientation,\n ).angularDistance(Quaternion.fromRotationVector(otherOrientation))\n\n return (\n positionDistance <= deltaPosition &&\n orientationDistance <= deltaOrientation\n )\n }\n\n /**\n * Position + orientation as a 6-element [x, y, z, roll, pitch, yaw] vector\n * (Euler angles in rad, XYZ Tait-Bryan / Rx*Ry*Rz convention), matching\n * wb-robotix's `Pose::toCartesian()`.\n */\n toCartesian(): number[] {\n const { w, x, y, z } = Quaternion.fromRotationVector(this.orientation)\n\n const roll = Math.atan2(2 * (w * x - y * z), 1 - 2 * (x * x + y * y))\n const pitch = Math.asin(Math.min(1, Math.max(-1, 2 * (x * z + w * y))))\n const yaw = Math.atan2(2 * (w * z - x * y), 1 - 2 * (y * y + z * z))\n\n return [...this.position, roll, pitch, yaw]\n }\n\n /** Human-readable `[x, y, z][rx, ry, rz]` representation, matching wb-robotix's `Pose::string()`. */\n toString(precision = 6): string {\n const fmt = (v: number) => Number(v.toPrecision(precision))\n return `[${this.position.map(fmt).join(\", \")}][${this.orientation.map(fmt).join(\", \")}]`\n }\n\n toJSON(): PoseData {\n return { position: this.position, orientation: this.orientation }\n }\n}\n","import type { Pose as PoseData } from \"@wandelbots/nova-api/v2\"\nimport type { Nova } from \"../../Nova.ts\"\nimport { Pose } from \"./Pose.ts\"\n\ntype PoseKeys = keyof PoseData\n\ntype IsExactlyPoseData<T> = [Exclude<keyof T, PoseKeys>] extends [never]\n ? [Exclude<PoseKeys, keyof T>] extends [never]\n ? true\n : false\n : false\n\n// Depth cap avoids \"type instantiation is excessively deep\" on nova-api's\n// large (and possibly cyclic, e.g. compound colliders) generated type graph.\ntype MaxDepth = [\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n]\n\ntype AugmentPosesCore<\n T,\n Depth extends readonly unknown[],\n> = Depth[\"length\"] extends MaxDepth[\"length\"]\n ? T\n : IsExactlyPoseData<T> extends true\n ? Pose\n : T extends readonly (infer U)[]\n ? DeepPoseAugmented<U, [...Depth, unknown]>[]\n : T extends object\n ? { [K in keyof T]: DeepPoseAugmented<T[K], [...Depth, unknown]> }\n : T\n\n/**\n * Type-level counterpart to `augmentPoses`: recursively replaces any\n * `PoseData`-shaped field with `Pose` (distributing over unions, e.g. so\n * optional `Pose | undefined` fields keep the `| undefined`), so the static\n * type matches what's actually returned at runtime.\n */\nexport type DeepPoseAugmented<\n T,\n Depth extends readonly unknown[] = [],\n> = T extends unknown ? AugmentPosesCore<T, Depth> : never\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n !(value instanceof Pose)\n )\n}\n\nfunction isNumberTriple(value: unknown): value is number[] {\n return (\n Array.isArray(value) &&\n value.length === 3 &&\n value.every((n) => typeof n === \"number\")\n )\n}\n\n/**\n * Structural check for the wire shape of `Pose` (`{ position, orientation }`,\n * each a 3-number array, and nothing else). This API has no other type with\n * this exact shape, so it reliably identifies `Pose` values without needing\n * per-endpoint knowledge of which fields are poses.\n */\nfunction isPoseShape(value: Record<string, unknown>): boolean {\n return (\n Object.keys(value).length === 2 &&\n isNumberTriple(value.position) &&\n isNumberTriple(value.orientation)\n )\n}\n\nfunction augmentPosesInPlace(value: unknown): void {\n if (Array.isArray(value)) {\n for (const item of value) {\n augmentPosesInPlace(item)\n }\n return\n }\n\n if (isPlainObject(value)) {\n if (isPoseShape(value)) {\n Object.setPrototypeOf(value, Pose.prototype)\n } else {\n for (const key of Object.keys(value)) {\n augmentPosesInPlace(value[key])\n }\n }\n }\n}\n\n/**\n * Recursively walks a parsed API response/request body, upgrading any\n * `Pose`-shaped objects in place to `Pose` instances so they gain math\n * methods (`multiply`, `inverse`, ...) while remaining JSON/wire-compatible.\n * Used by `augmentMath` for `nova.api.*` responses; exported so it can also\n * be applied manually to e.g. websocket messages.\n */\nexport function augmentPoses<T>(value: T): DeepPoseAugmented<T> {\n augmentPosesInPlace(value)\n return value as DeepPoseAugmented<T>\n}\n\ntype WithAugmentedPosesCore<\n T,\n Depth extends readonly unknown[],\n> = Depth[\"length\"] extends MaxDepth[\"length\"]\n ? T\n : T extends (...args: infer A) => Promise<infer R>\n ? (...args: A) => Promise<DeepPoseAugmented<R>>\n : T extends object\n ? { [K in keyof T]: WithAugmentedPosesCore<T[K], [...Depth, unknown]> }\n : T\n\n/**\n * Recursively maps every method on `Nova[\"api\"]` (and its nested API groups)\n * so calls are typed as returning `DeepPoseAugmented` results, matching what\n * `augmentMath` does at runtime.\n */\ntype WithAugmentedPoses<T> = WithAugmentedPosesCore<T, []>\n\nexport type NovaWithMath = Omit<Nova, \"api\"> & {\n readonly api: WithAugmentedPoses<Nova[\"api\"]>\n}\n\nfunction wrapWithPoseAugmentation<T extends object>(target: T): T {\n return new Proxy(target, {\n get(t, prop) {\n const value = Reflect.get(t, prop, t)\n if (typeof value === \"function\") {\n return (...args: unknown[]) => {\n const result = (value as (...a: unknown[]) => unknown).apply(t, args)\n return result instanceof Promise\n ? result.then((data) => augmentPoses(data))\n : result\n }\n }\n if (value !== null && typeof value === \"object\") {\n return wrapWithPoseAugmentation(value)\n }\n return value\n },\n }) as T\n}\n\n/**\n * Wraps a `Nova` instance so every `nova.api.*` call automatically upgrades\n * `Pose`-shaped fields in its response to `Pose` instances (with math\n * methods like `multiply`/`inverse`). Returns a new proxied view - does not\n * mutate the original `nova` instance.\n */\nexport function augmentMath(nova: Nova): NovaWithMath {\n const proxy = new Proxy(nova, {\n get(target, prop) {\n if (prop === \"api\") {\n return wrapWithPoseAugmentation(target.api)\n }\n // receiver is `target`, not the proxy, so `this` inside Nova's own\n // methods stays bound to the original (un-proxied) instance.\n return Reflect.get(target, prop, target)\n },\n })\n\n return proxy as unknown as NovaWithMath\n}\n"],"mappings":";AAUA,MAAM,UAAU;AAEhB,IAAa,aAAb,MAAa,WAAqC;CAChD;CACA;CACA;CACA;CAEA,YAAY,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG;EACtC,KAAK,IAAI;EACT,KAAK,IAAI;EACT,KAAK,IAAI;EACT,KAAK,IAAI;CACX;CAEA,OAAO,WAAuB;EAC5B,OAAO,IAAI,WAAW;CACxB;;;;;;CAOA,OAAO,mBAAmB,gBAAsC;EAC9D,MAAM,KAAK,eAAe,MAAM;EAChC,MAAM,KAAK,eAAe,MAAM;EAChC,MAAM,KAAK,eAAe,MAAM;EAEhC,MAAM,QAAQ,KAAK,KAAK,KAAK,KAAK,KAAK,KAAK,KAAK,EAAE;EACnD,IAAI,QAAQ,SACV,OAAO,WAAW,SAAS;EAE7B,MAAM,OAAO,QAAQ;EACrB,MAAM,IA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48e5ca",
|
|
5
5
|
"description": "Official JS client for the Wandelbots API",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"packageManager": "pnpm@11.9.0+sha512.bd682d5d03fe525ef7c9fd6780c6884d1e756ac4c9c9fe00c538782824310dcf90e3ddc4f53835f06dfaebd5085e41855e0bcbb3b60de2ac5bbab89e5036f03b",
|
|
@@ -3,6 +3,9 @@ import { Quaternion } from "./Quaternion.ts"
|
|
|
3
3
|
|
|
4
4
|
const ZERO_VECTOR = [0, 0, 0]
|
|
5
5
|
|
|
6
|
+
// Matches wb-robotix's Constants.h TOLERANCE_MILLI, the default isApprox tolerance.
|
|
7
|
+
const TOLERANCE_MILLI = 1e-3
|
|
8
|
+
|
|
6
9
|
function addVectors(a: number[], b: number[]): number[] {
|
|
7
10
|
return [
|
|
8
11
|
(a[0] ?? 0) + (b[0] ?? 0),
|
|
@@ -15,6 +18,10 @@ function negateVector(a: number[]): number[] {
|
|
|
15
18
|
return [-(a[0] ?? 0), -(a[1] ?? 0), -(a[2] ?? 0)]
|
|
16
19
|
}
|
|
17
20
|
|
|
21
|
+
function isFiniteVector3(v: number[]): boolean {
|
|
22
|
+
return v.length === 3 && v.every((n) => Number.isFinite(n))
|
|
23
|
+
}
|
|
24
|
+
|
|
18
25
|
/**
|
|
19
26
|
* A `Pose` (position + axis-angle orientation) with methods for composing
|
|
20
27
|
* and inverting transforms. Instances are plain-data compatible with the
|
|
@@ -30,8 +37,20 @@ export class Pose implements PoseData {
|
|
|
30
37
|
position: number[] = ZERO_VECTOR,
|
|
31
38
|
orientation: number[] = ZERO_VECTOR,
|
|
32
39
|
) {
|
|
33
|
-
|
|
34
|
-
|
|
40
|
+
if (!isFiniteVector3(position)) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Pose constructor: position must be an array of 3 finite numbers, got ${JSON.stringify(position)}`,
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
if (!isFiniteVector3(orientation)) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Pose constructor: orientation must be an array of 3 finite numbers, got ${JSON.stringify(orientation)}`,
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
// Copy defensively so mutating the caller's arrays afterward can't
|
|
51
|
+
// change this (supposedly immutable) instance's state.
|
|
52
|
+
this.position = [...position]
|
|
53
|
+
this.orientation = [...orientation]
|
|
35
54
|
}
|
|
36
55
|
|
|
37
56
|
static from(pose: PoseData): Pose {
|
|
@@ -84,21 +103,59 @@ export class Pose implements PoseData {
|
|
|
84
103
|
return addVectors(this.position, q.rotateVector(point))
|
|
85
104
|
}
|
|
86
105
|
|
|
87
|
-
/**
|
|
88
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Compare to another pose using separate position/orientation tolerances,
|
|
108
|
+
* matching wb-robotix's `Pose::isApprox()`: Euclidean distance for
|
|
109
|
+
* position, and quaternion angular distance for orientation - not a naive
|
|
110
|
+
* per-component diff, since two rotation vectors can represent nearly
|
|
111
|
+
* identical rotations while differing componentwise near a
|
|
112
|
+
* canonicalization boundary (e.g. close to the +/-pi wraparound).
|
|
113
|
+
*/
|
|
114
|
+
isApprox(
|
|
115
|
+
other: PoseData,
|
|
116
|
+
deltaPosition = TOLERANCE_MILLI,
|
|
117
|
+
deltaOrientation = TOLERANCE_MILLI,
|
|
118
|
+
): boolean {
|
|
89
119
|
const otherPosition = other.position ?? ZERO_VECTOR
|
|
90
120
|
const otherOrientation = other.orientation ?? ZERO_VECTOR
|
|
91
121
|
|
|
122
|
+
const positionDistance = Math.sqrt(
|
|
123
|
+
this.position.reduce(
|
|
124
|
+
(sum, v, i) => sum + (v - (otherPosition[i] ?? 0)) ** 2,
|
|
125
|
+
0,
|
|
126
|
+
),
|
|
127
|
+
)
|
|
128
|
+
const orientationDistance = Quaternion.fromRotationVector(
|
|
129
|
+
this.orientation,
|
|
130
|
+
).angularDistance(Quaternion.fromRotationVector(otherOrientation))
|
|
131
|
+
|
|
92
132
|
return (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
) &&
|
|
96
|
-
this.orientation.every(
|
|
97
|
-
(v, i) => Math.abs(v - (otherOrientation[i] ?? 0)) <= epsilon,
|
|
98
|
-
)
|
|
133
|
+
positionDistance <= deltaPosition &&
|
|
134
|
+
orientationDistance <= deltaOrientation
|
|
99
135
|
)
|
|
100
136
|
}
|
|
101
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Position + orientation as a 6-element [x, y, z, roll, pitch, yaw] vector
|
|
140
|
+
* (Euler angles in rad, XYZ Tait-Bryan / Rx*Ry*Rz convention), matching
|
|
141
|
+
* wb-robotix's `Pose::toCartesian()`.
|
|
142
|
+
*/
|
|
143
|
+
toCartesian(): number[] {
|
|
144
|
+
const { w, x, y, z } = Quaternion.fromRotationVector(this.orientation)
|
|
145
|
+
|
|
146
|
+
const roll = Math.atan2(2 * (w * x - y * z), 1 - 2 * (x * x + y * y))
|
|
147
|
+
const pitch = Math.asin(Math.min(1, Math.max(-1, 2 * (x * z + w * y))))
|
|
148
|
+
const yaw = Math.atan2(2 * (w * z - x * y), 1 - 2 * (y * y + z * z))
|
|
149
|
+
|
|
150
|
+
return [...this.position, roll, pitch, yaw]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Human-readable `[x, y, z][rx, ry, rz]` representation, matching wb-robotix's `Pose::string()`. */
|
|
154
|
+
toString(precision = 6): string {
|
|
155
|
+
const fmt = (v: number) => Number(v.toPrecision(precision))
|
|
156
|
+
return `[${this.position.map(fmt).join(", ")}][${this.orientation.map(fmt).join(", ")}]`
|
|
157
|
+
}
|
|
158
|
+
|
|
102
159
|
toJSON(): PoseData {
|
|
103
160
|
return { position: this.position, orientation: this.orientation }
|
|
104
161
|
}
|
|
@@ -90,6 +90,25 @@ export class Quaternion implements QuaternionData {
|
|
|
90
90
|
return this.conjugate()
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/**
|
|
94
|
+
* The angle (in rad, always within [0, pi]) of the rotation that takes
|
|
95
|
+
* `other` to `this`, matching Eigen's built-in `Quaternion::angularDistance()`.
|
|
96
|
+
*/
|
|
97
|
+
angularDistance(other: QuaternionData): number {
|
|
98
|
+
const relative = this.multiply({
|
|
99
|
+
w: other.w,
|
|
100
|
+
x: -other.x,
|
|
101
|
+
y: -other.y,
|
|
102
|
+
z: -other.z,
|
|
103
|
+
})
|
|
104
|
+
const vecNorm = Math.sqrt(
|
|
105
|
+
relative.x * relative.x +
|
|
106
|
+
relative.y * relative.y +
|
|
107
|
+
relative.z * relative.z,
|
|
108
|
+
)
|
|
109
|
+
return 2 * Math.atan2(vecNorm, Math.abs(relative.w))
|
|
110
|
+
}
|
|
111
|
+
|
|
93
112
|
/** Rotate a 3D vector by this quaternion. */
|
|
94
113
|
rotateVector(v: number[]): number[] {
|
|
95
114
|
const vx = v[0] ?? 0
|