Skip to content

Commit

Permalink
feat: 2d and 3d relative position
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Nov 21, 2023
1 parent 9eeddd1 commit 384c108
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ ModelBuilder.create()
- `openhps-core-lite.js`: UMD lite version for embedded systems

## Contributors
The framework is open source and is mainly developed by PhD Student Maxim Van de Wynckel as part of his research towards *Hybrid Positioning and Implicit Human-Computer Interaction* under the supervision of Prof. Dr. Beat Signer.
The framework is open source and is mainly developed by PhD Student Maxim Van de Wynckel as part of his research towards *Hybrid Positioning and Interoperable Positioning Systems* under the supervision of Prof. Dr. Beat Signer.

## Contributing
Use of OpenHPS, contributions and feedback is highly appreciated. Please read our [contributing guidelines](CONTRIBUTING.md) for more information.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openhps/core",
"version": "0.5.17",
"version": "0.6.0",
"description": "Open Hybrid Positioning System - Core component",
"author": "Maxim Van de Wynckel",
"license": "Apache-2.0",
Expand Down
18 changes: 18 additions & 0 deletions src/data/object/space/ReferenceSpace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ export class ReferenceSpace extends DataObject implements TransformationSpace {
this._rotation = new Quaternion();
}

/**
* Create a reference space from another object
*
* @param {DataObject} object Reference space
*/
static fromDataObject(object: DataObject): ReferenceSpace {
const space = new ReferenceSpace();
space.uid = object.uid;
space.displayName = object.displayName;
if (object.getPosition()) {
space.translation(...object.getPosition().toVector3(LengthUnit.METER).toArray());
if (object.getPosition().orientation) {
space.rotation(object.getPosition().orientation);
}
}
return space;
}

/**
* Set the parent space
* @param {TransformationSpace} space Parent space
Expand Down
10 changes: 10 additions & 0 deletions src/data/position/AbsolutePosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ export abstract class AbsolutePosition implements Position<LengthUnit> {
this.velocity.angular = value;
}

/**
* Set the orientation of the position
* @param {Orientation} orientation orientation
* @returns {AbsolutePosition} instance
*/
setOrientation(orientation: Orientation): this {
this.orientation = orientation;
return this;
}

/**
* Set the accuracy of the absolute position
* @param {number | Accuracy} accuracy Accuracy object or number
Expand Down
81 changes: 81 additions & 0 deletions src/data/position/Relative2DPosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { LengthUnit } from '../../utils';
import { Vector2, Vector3 } from '../../utils/math';
import { NumberType, SerializableMember, SerializableObject } from '../decorators';
import { RelativePosition } from './RelativePosition';

/**
* Relative 2D Position relative to another object. This indicates the translation and rotation
* relative to another reference object.
*/
@SerializableObject()
export class Relative2DPosition extends RelativePosition<Vector3, LengthUnit> {
constructor(referenceObject?: any, x?: number, y?: number, unit: LengthUnit = LengthUnit.METER) {
super(referenceObject, new Vector3());
this.referenceValue.x = unit.convert(x ? x : 0, LengthUnit.METER);
this.referenceValue.y = unit.convert(y ? y : 0, LengthUnit.METER);
}

@SerializableMember({
numberType: NumberType.DECIMAL,
})
get x(): number {
if (!this.referenceValue) {
return undefined;
}
return this.referenceValue.x;
}

set x(value: number) {
if (!this.referenceValue) {
return;
}
this.referenceValue.x = value;
}

@SerializableMember({
numberType: NumberType.DECIMAL,
})
get y(): number {
if (!this.referenceValue) {
return undefined;
}
return this.referenceValue.y;
}

set y(value: number) {
if (!this.referenceValue) {
return;
}
this.referenceValue.y = value;
}

fromVector(vector: Vector2 | Vector3, unit?: LengthUnit): this {
if (unit) {
this.x = unit.convert(vector.x, this.unit);
this.y = unit.convert(vector.y, this.unit);
} else {
this.x = vector.x;
this.y = vector.y;
}
return this;
}

toVector3(unit?: LengthUnit): Vector3 {
if (unit) {
return new Vector3(this.unit.convert(this.x, unit), this.unit.convert(this.y, unit));
} else {
return new Vector3(this.x, this.y);
}
}

/**
* Clone the position
* @returns {Absolute2DPosition} Cloned position
*/
clone(): this {
const position = super.clone();
position.x = this.x;
position.y = this.y;
return position as this;
}
}
69 changes: 69 additions & 0 deletions src/data/position/Relative3DPosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { LengthUnit, Vector3 } from '../../utils';
import { NumberType, SerializableMember, SerializableObject } from '../decorators';
import { Relative2DPosition } from './Relative2DPosition';

/**
* Relative 3D Position relative to another object. This indicates the translation and rotation
* relative to another reference object.
*/
@SerializableObject()
export class Relative3DPosition extends Relative2DPosition {
constructor(referenceObject?: any, x?: number, y?: number, z?: number, unit: LengthUnit = LengthUnit.METER) {
super(referenceObject, x, y, unit);
this.referenceValue.z = unit.convert(z ? z : 0, LengthUnit.METER);
}

@SerializableMember({
numberType: NumberType.DECIMAL,
})
get z(): number {
if (!this.referenceValue) {
return undefined;
}
return this.referenceValue.z;
}

set z(value: number) {
if (!this.referenceValue) {
return;
}
this.referenceValue.z = value;
}

fromVector(vector: Vector3, unit?: LengthUnit): this {
if (unit) {
this.x = unit.convert(vector.x, this.unit);
this.y = unit.convert(vector.y, this.unit);
this.z = unit.convert(vector.z ?? 0, this.unit);
} else {
this.x = vector.x;
this.y = vector.y;
this.z = vector.z ?? 0;
}
return this;
}

toVector3(unit?: LengthUnit): Vector3 {
if (unit) {
return new Vector3(
this.unit.convert(this.x, unit),
this.unit.convert(this.y, unit),
this.unit.convert(this.z, unit),
);
} else {
return new Vector3(this.x, this.y, this.z);
}
}

/**
* Clone the position
* @returns {Absolute3DPosition} Cloned position
*/
clone(): this {
const position = super.clone();
position.x = this.x;
position.y = this.y;
position.z = this.z;
return position as this;
}
}
2 changes: 2 additions & 0 deletions src/data/position/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export * from './AbsolutePosition';
export * from './GeographicalPosition';
export * from './Absolute2DPosition';
export * from './Absolute3DPosition';
export * from './Relative2DPosition';
export * from './Relative3DPosition';
export * from './RelativeAngle';
export * from './RelativeDistance';
export * from './RelativeAngularVelocity';
Expand Down
1 change: 1 addition & 0 deletions test/specs/position/absoluteposition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
LengthUnit,
LinearVelocity,
Orientation,
ReferenceSpace,
} from '../../../src';

describe('AbsolutePosition', () => {
Expand Down

0 comments on commit 384c108

Please sign in to comment.