UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

167 lines 5.26 kB
import { __decorate, __metadata } from "tslib"; import { LengthUnit } from '../../utils/unit'; import { Velocity } from '../values/Velocity'; import { Orientation } from './Orientation'; import { NumberType, SerializableMember, SerializableObject } from '../decorators'; import { TimeService } from '../../service/TimeService'; import { Accuracy } from '../values/Accuracy'; import { Accuracy1D } from '../values/Accuracy1D'; import { EUCLIDEAN } from '../../utils'; /** * An absolute position of a {@link DataObject}. * @category Position */ let AbsolutePosition = class AbsolutePosition { constructor() { /** * Position recording timestamp */ this.timestamp = TimeService.now(); /** * Velocity at recorded position */ this.velocity = new Velocity(); /** * Position unit */ this.unit = LengthUnit.METER; } /** * Get the position probability * @returns {number} Probability between 0 and 1 */ get probability() { if (!this._probability) { return 1 / this.accuracy.valueOf(); } return this._probability; } set probability(value) { if (value > 1 || value < 0) { throw new Error(`${this.constructor.name} should be between 0 and 1.`); } this._probability = value; } /** * Position accuracy * @returns {Accuracy} Position accuracy */ get accuracy() { if (!this._accuracy) { this._accuracy = new Accuracy1D(1, this.unit); } return this._accuracy; } set accuracy(value) { if (!value) { throw new Error(`Accuracy can not be undefined!`); } this._accuracy = value; } /** * Get the linear velocity * @returns {LinearVelocity} Linear velocity */ get linearVelocity() { if (!this.velocity) { return undefined; } return this.velocity.linear; } /** * Set the linear velocity */ set linearVelocity(value) { if (!this.velocity) { this.velocity = new Velocity(); } this.velocity.linear = value; } /** * Get the angular velocity * @returns {AngularVelocity} Angular velocity */ get angularVelocity() { if (!this.velocity) { return undefined; } return this.velocity.angular; } /** * Set the angular velocity */ set angularVelocity(value) { if (!this.velocity) { this.velocity = new Velocity(); } this.velocity.angular = value; } /** * Set the orientation of the position * @param {Orientation} orientation orientation * @returns {AbsolutePosition} instance */ setOrientation(orientation) { this.orientation = orientation; return this; } /** * Set the accuracy of the absolute position * @param {number | Accuracy} accuracy Accuracy object or number * @param {Unit} [unit] Optional unit * @returns {AbsolutePosition} instance */ setAccuracy(accuracy, unit) { if (typeof accuracy === 'number') { this.accuracy = new Accuracy1D(accuracy, unit || this.unit); } else { this.accuracy = accuracy; } return this; } /** * Get the distance from this location to a destination * @param {AbsolutePosition} destination Destination location * @param {DistanceFn} [distanceFunction] Distance function to use (default EUCLIDEAN distance) * @returns {number} Distance between this point and destination */ distanceTo(destination, distanceFunction = EUCLIDEAN) { return distanceFunction(this.toVector3().toArray(), destination.toVector3().toArray()); } equals(position) { return this.toVector3(this.unit).equals(position.toVector3(this.unit)); } /** * Clone the position * @returns {AbsolutePosition} Cloned position */ clone() { const position = new this.constructor(); position.unit = this.unit; position._accuracy = this._accuracy ? this._accuracy.clone() : undefined; position.orientation = this.orientation ? this.orientation.clone() : undefined; position.velocity = this.velocity ? this.velocity.clone() : undefined; position.timestamp = this.timestamp; position.referenceSpaceUID = this.referenceSpaceUID; return position; } }; __decorate([SerializableMember({ index: true, numberType: NumberType.LONG }), __metadata("design:type", Number)], AbsolutePosition.prototype, "timestamp", void 0); __decorate([SerializableMember(), __metadata("design:type", Velocity)], AbsolutePosition.prototype, "velocity", void 0); __decorate([SerializableMember(), __metadata("design:type", Orientation)], AbsolutePosition.prototype, "orientation", void 0); __decorate([SerializableMember(), __metadata("design:type", LengthUnit)], AbsolutePosition.prototype, "unit", void 0); __decorate([SerializableMember({ index: true }), __metadata("design:type", String)], AbsolutePosition.prototype, "referenceSpaceUID", void 0); __decorate([SerializableMember({ name: 'accuracy' }), __metadata("design:type", Accuracy)], AbsolutePosition.prototype, "_accuracy", void 0); __decorate([SerializableMember({ name: 'probability', numberType: NumberType.DECIMAL }), __metadata("design:type", Number)], AbsolutePosition.prototype, "_probability", void 0); AbsolutePosition = __decorate([SerializableObject()], AbsolutePosition); export { AbsolutePosition };