diff --git a/src/data/object/DataObject.ts b/src/data/object/DataObject.ts index cee03cf2c..6b6fb95f1 100644 --- a/src/data/object/DataObject.ts +++ b/src/data/object/DataObject.ts @@ -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 * @@ -32,9 +42,9 @@ import { DataService } from '../../service/DataService'; * ```typescript * myObject.bind(myModel).save(); * ``` + * @public * @category data */ -@SerializableObject() export class DataObject { /** * Object display name @@ -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 diff --git a/src/data/position/GeographicalPosition.ts b/src/data/position/GeographicalPosition.ts index 0a550dfbe..a634ea555 100644 --- a/src/data/position/GeographicalPosition.ts +++ b/src/data/position/GeographicalPosition.ts @@ -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); } /** @@ -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; diff --git a/src/data/position/Orientation.ts b/src/data/position/Orientation.ts index 8b9ba7571..225250194 100644 --- a/src/data/position/Orientation.ts +++ b/src/data/position/Orientation.ts @@ -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'; @@ -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); } diff --git a/test/specs/position/position.geographical.spec.ts b/test/specs/position/position.geographical.spec.ts index 8129068a9..8f70280b2 100644 --- a/test/specs/position/position.geographical.spec.ts +++ b/test/specs/position/position.geographical.spec.ts @@ -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', () => { @@ -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)); }); });