Skip to content

Commit

Permalink
add tests, allow non-normalization behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
micheal-parks committed Feb 29, 2024
1 parent de6ca54 commit dccb80a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@viamrobotics/three",
"version": "0.0.7",
"version": "0.0.8",
"license": "Apache-2.0",
"type": "module",
"files": [
Expand Down
52 changes: 46 additions & 6 deletions src/orientation-vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ export class OrientationVector {

#onChangeCallback: (() => void) | undefined;

autoNormalize = true;

constructor(x = 0, y = 0, z = 1, th = 0) {
this.#vec.set(x, y, z).normalize();
this.#vec.set(x, y, z);

if (this.autoNormalize) {
this.#vec.normalize();
}

this.#th = th;
}

Expand All @@ -59,7 +66,12 @@ export class OrientationVector {
}

set x(value: number) {
this.#vec.setX(value).normalize();
this.#vec.setX(value);

if (this.autoNormalize) {
this.#vec.normalize();
}

this.#onChangeCallback?.();
}

Expand All @@ -72,7 +84,12 @@ export class OrientationVector {
}

set y(value: number) {
this.#vec.setY(value).normalize();
this.#vec.setY(value);

if (this.autoNormalize) {
this.#vec.normalize();
}

this.#onChangeCallback?.();
}

Expand All @@ -85,7 +102,12 @@ export class OrientationVector {
}

set z(value: number) {
this.#vec.setY(value).normalize();
this.#vec.setY(value);

if (this.autoNormalize) {
this.#vec.normalize();
}

this.#onChangeCallback?.();
}

Expand Down Expand Up @@ -122,7 +144,12 @@ export class OrientationVector {
* Sets the value of this orientation vector.
*/
set(x = 0, y = 0, z = 0, th = 0): this {
this.#vec.set(x, y, z).normalize();
this.#vec.set(x, y, z);

if (this.autoNormalize) {
this.#vec.normalize();
}

this.th = th;

this.#onChangeCallback?.();
Expand All @@ -143,11 +170,24 @@ export class OrientationVector {
return this.#vec.length();
}

/**
* Normalizes the vector component.
*/
normalize() {
this.#vec.normalize();
return this;
}

/**
* Copies value of ov to this orientation vector.
*/
copy(ov: OrientationVector): this {
this.#vec.set(ov.x, ov.y, ov.z).normalize();
this.#vec.set(ov.x, ov.y, ov.z);

if (this.autoNormalize) {
this.#vec.normalize();
}

this.th = ov.th;

this.#onChangeCallback?.();
Expand Down
19 changes: 19 additions & 0 deletions test/viam-object3d.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, it } from 'vitest';
import { ViamObject3D } from '../src/object-3d';
import { expect } from 'vitest';

describe('ViamObject3D', () => {
const object3D = new ViamObject3D();

it('Updates the rotation when when an orientation vector is modified', () => {
object3D.orientationVector.th = Math.PI;
expect(object3D.rotation.z).toBeCloseTo(Math.PI);
expect(object3D.orientationVector.th).toBeCloseTo(Math.PI);
});

it('Updates the orientation vector when rotation is modified', () => {
object3D.rotation.z = Math.PI;
expect(object3D.orientationVector.th).toBeCloseTo(Math.PI);
expect(object3D.rotation.z).toBeCloseTo(Math.PI);
});
});

0 comments on commit dccb80a

Please sign in to comment.