/
Absolute3DPosition.ts
72 lines (67 loc) · 2.2 KB
/
Absolute3DPosition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { SerializableMember, SerializableObject } from '../decorators';
import { LengthUnit, Vector3 } from '../../utils';
import { Absolute2DPosition } from './Absolute2DPosition';
/**
* Absolute cartesian 3D position. This class uses a [[Vector3]]. This location can be used both as
* an absolute location or relative location.
*
* @category Position
*/
@SerializableObject()
export class Absolute3DPosition extends Absolute2DPosition {
constructor(x?: number, y?: number, z?: number, unit: LengthUnit = LengthUnit.METER) {
super(x, y, unit);
this.vector.z = unit.convert(z ? z : 0, LengthUnit.METER);
}
@SerializableMember()
get z(): number {
if (!this.vector) {
return undefined;
}
return this.vector.z;
}
set z(value: number) {
if (!this.vector) {
return;
}
this.vector.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, this.unit);
} else {
this.x = vector.x;
this.y = vector.y;
this.z = vector.z;
}
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
*/
public clone(): this {
const position = new Absolute3DPosition(this.x, this.y, this.z);
position.unit = this.unit;
position.accuracy = this.accuracy;
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 as this;
}
}