Skip to content

Commit

Permalink
chore: missing parameters for geographical position and orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Oct 23, 2024
1 parent cc2d603 commit a42f2d0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
60 changes: 59 additions & 1 deletion src/data/object/DataObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ import { TimeService } from '../../service/TimeService';
import { TransformationSpace } from './space/TransformationSpace';
import { EventEmitter } from 'events';
import { DataService } from '../../service/DataService';
import { Orientation } from '../position';
import { AngularVelocity, LinearVelocity, Velocity } from '../values';

@SerializableObject()
/**
* A data object is an instance that can be anything ranging from a person or asset to
* a more abstract object such as a Wi-Fi access point or {@link ReferenceSpace}.
* @example
* ```typescript
* const dataObject = new DataObject();
* dataObject.displayName = "Sample Object";
* dataObject.position = new AbsolutePosition(1, 2, 3);
* dataObject.setParent("parentUID");
* ```
*
* ## Usage
*
Expand All @@ -32,9 +42,9 @@ import { DataService } from '../../service/DataService';
* ```typescript
* myObject.bind(myModel).save();
* ```
* @public
* @category data
*/
@SerializableObject()
export class DataObject {
/**
* Object display name
Expand Down Expand Up @@ -91,6 +101,54 @@ export class DataObject {
this.setPosition(position);
}

/**
* Get the orientation of the object
* relative to the global reference space
*/
get orientation(): Orientation {
return this._position ? this.getPosition().orientation : undefined;
}

/**
* Set the orientation of the object
* relative to the global reference space
*/
set orientation(orientation: Orientation) {
if (this._position) {
this.getPosition().orientation = orientation;
}
}

get velocity(): Velocity {
return this._position ? this.getPosition().velocity : undefined;
}

set velocity(velocity: Velocity) {
if (this._position) {
this.getPosition().velocity = velocity;
}
}

get linearVelocity(): LinearVelocity {
return this._position ? this.getPosition().linearVelocity : undefined;
}

set linearVelocity(velocity: LinearVelocity) {
if (this._position) {
this.getPosition().linearVelocity = velocity;
}
}

get angularVelocity(): AngularVelocity {
return this._position ? this.getPosition().angularVelocity : undefined;
}

set angularVelocity(velocity: AngularVelocity) {
if (this._position) {
this.getPosition().angularVelocity = velocity;
}
}

/**
* Get the current absolute position of the object
* @param {TransformationSpace} [referenceSpace] Reference space to transform it to
Expand Down
8 changes: 3 additions & 5 deletions src/data/position/GeographicalPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import { GCS, HAVERSINE, Unit, Vector3 } from '../../utils';
*/
@SerializableObject()
export class GeographicalPosition extends Absolute3DPosition {
constructor(lat?: number, lng?: number, amsl?: number) {
constructor(lat?: number, lng?: number, amsl?: number, gcs: GCS = GCS.WGS84) {
super();
this.latitude = lat;
this.longitude = lng;
this.z = amsl;
this.fromVector(new Vector3(lng, lat, amsl), gcs);
}

/**
Expand Down Expand Up @@ -130,7 +128,7 @@ export class GeographicalPosition extends Absolute3DPosition {
GCS.WGS84,
);
} else if (unit instanceof GCS) {
converted = unit.convert(vector, GCS.WGS84);
converted = unit !== GCS.WGS84 ? unit.convert(vector, GCS.WGS84) : vector;
}
this.x = converted.x;
this.y = converted.y;
Expand Down
12 changes: 11 additions & 1 deletion src/data/position/Orientation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Quaternion } from '../../utils/math';
import { Quaternion, Vector3 } from '../../utils/math';
import { NumberType, SerializableMember, SerializableObject } from '../decorators';
import * as THREE from '../../utils/math/_internal';
import { TimeService } from '../../service/TimeService';
Expand Down Expand Up @@ -28,6 +28,16 @@ export class Orientation extends Quaternion {
this.timestamp = TimeService.now();
}

static fromBearing(bearing: number, unit: AngleUnit = AngleUnit.DEGREE): Orientation {
return Orientation.fromEuler({
x: 0,
y: 0,
z: bearing,
order: 'ZYX',
unit,
});
}

static fromQuaternion(quat: Quaternion | THREE.Quaternion): Orientation {
return new Orientation(quat.x, quat.y, quat.z, quat.w);
}
Expand Down
6 changes: 5 additions & 1 deletion test/specs/position/position.geographical.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import 'mocha';
import { GeographicalPosition, AngleUnit, GCS, LengthUnit } from '../../../src';
import { GeographicalPosition, AngleUnit, GCS, LengthUnit, Orientation, Accuracy2D } from '../../../src';

describe('GeographicalPosition', () => {
it('should convert latitude and latittude to XYZ coordinates', () => {
Expand Down Expand Up @@ -65,5 +65,9 @@ describe('GeographicalPosition', () => {
expect(Math.round(vector.x)).to.equal(4025669);
expect(Math.round(vector.y)).to.equal(309202);
expect(Math.round(vector.z)).to.equal(4920958);

const position = new GeographicalPosition(50.820548, 4.392123, 53, GCS.WGS84)
.setOrientation(Orientation.fromBearing(180))
.setAccuracy(new Accuracy2D(5, 5, LengthUnit.METER));
});
});

0 comments on commit a42f2d0

Please sign in to comment.