|
@@ -0,0 +1,151 @@
|
|
1
|
+/*
|
|
2
|
+ * encantar.js
|
|
3
|
+ * GPU-accelerated Augmented Reality for the web
|
|
4
|
+ * Copyright (C) 2022-2024 Alexandre Martins <alemartf(at)gmail.com>
|
|
5
|
+ *
|
|
6
|
+ * This program is free software: you can redistribute it and/or modify
|
|
7
|
+ * it under the terms of the GNU Lesser General Public License as published
|
|
8
|
+ * by the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+ * (at your option) any later version.
|
|
10
|
+ *
|
|
11
|
+ * This program is distributed in the hope that it will be useful,
|
|
12
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+ * GNU Lesser General Public License for more details.
|
|
15
|
+ *
|
|
16
|
+ * You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+ *
|
|
19
|
+ * vector3.ts
|
|
20
|
+ * 3D vectors
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+/** Small number */
|
|
24
|
+const EPSILON = 1e-6;
|
|
25
|
+
|
|
26
|
+// public / non-internal methods do not change the contents of the quaternion
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+/**
|
|
31
|
+ * A vector in 3D space
|
|
32
|
+ */
|
|
33
|
+export class Vector3
|
|
34
|
+{
|
|
35
|
+ /** x coordinate */
|
|
36
|
+ public x: number;
|
|
37
|
+
|
|
38
|
+ /** y coordinate */
|
|
39
|
+ public y: number;
|
|
40
|
+
|
|
41
|
+ /** z coordinate */
|
|
42
|
+ public z: number;
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+ /**
|
|
47
|
+ * Constructor
|
|
48
|
+ */
|
|
49
|
+ constructor(x: number = 0, y: number = 0, z: number = 0)
|
|
50
|
+ {
|
|
51
|
+ this.x = +x;
|
|
52
|
+ this.y = +y;
|
|
53
|
+ this.z = +z;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ /**
|
|
57
|
+ * Instantiate a zero vector
|
|
58
|
+ * @returns a new zero vector
|
|
59
|
+ */
|
|
60
|
+ static Zero(): Vector3
|
|
61
|
+ {
|
|
62
|
+ return new Vector3(0, 0, 0);
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ /**
|
|
66
|
+ * The length of this vector
|
|
67
|
+ * @returns sqrt(x^2 + y^2 + z^2)
|
|
68
|
+ */
|
|
69
|
+ length(): number
|
|
70
|
+ {
|
|
71
|
+ const x = this.x;
|
|
72
|
+ const y = this.y;
|
|
73
|
+ const z = this.z;
|
|
74
|
+
|
|
75
|
+ return Math.sqrt(x*x + y*y + z*z);
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ /**
|
|
79
|
+ * Check if this and v have the same coordinates
|
|
80
|
+ * @param v a vector
|
|
81
|
+ * @returns true if this and v have the same coordinates
|
|
82
|
+ */
|
|
83
|
+ equals(v: Vector3): boolean
|
|
84
|
+ {
|
|
85
|
+ return this.x === v.x && this.y === v.y && this.z === v.z;
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ /**
|
|
89
|
+ * Convert to string
|
|
90
|
+ * @returns a string
|
|
91
|
+ */
|
|
92
|
+ toString(): string
|
|
93
|
+ {
|
|
94
|
+ const x = this.x.toFixed(5);
|
|
95
|
+ const y = this.y.toFixed(5);
|
|
96
|
+ const z = this.z.toFixed(5);
|
|
97
|
+
|
|
98
|
+ return `Vector3(${x},${y},${z})`;
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ /**
|
|
102
|
+ * Normalize this vector
|
|
103
|
+ * @returns this vector, normalized
|
|
104
|
+ * @internal
|
|
105
|
+ */
|
|
106
|
+ _normalize(): Vector3
|
|
107
|
+ {
|
|
108
|
+ const length = this.length();
|
|
109
|
+
|
|
110
|
+ if(length < EPSILON) // zero?
|
|
111
|
+ return this;
|
|
112
|
+
|
|
113
|
+ this.x /= length;
|
|
114
|
+ this.y /= length;
|
|
115
|
+ this.z /= length;
|
|
116
|
+
|
|
117
|
+ return this;
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ /**
|
|
121
|
+ * Copy v to this
|
|
122
|
+ * @param v a vector
|
|
123
|
+ * @returns this vector
|
|
124
|
+ * @internal
|
|
125
|
+ */
|
|
126
|
+ _copyFrom(v: Vector3): Vector3
|
|
127
|
+ {
|
|
128
|
+ this.x = v.x;
|
|
129
|
+ this.y = v.y;
|
|
130
|
+ this.z = v.z;
|
|
131
|
+
|
|
132
|
+ return this;
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ /**
|
|
136
|
+ * Set the coordinates of this vector
|
|
137
|
+ * @param x x-coordinate
|
|
138
|
+ * @param y y-coordinate
|
|
139
|
+ * @param z z-coordinate
|
|
140
|
+ * @returns this vector
|
|
141
|
+ * @internal
|
|
142
|
+ */
|
|
143
|
+ _set(x: number, y: number, z: number): Vector3
|
|
144
|
+ {
|
|
145
|
+ this.x = +x;
|
|
146
|
+ this.y = +y;
|
|
147
|
+ this.z = +z;
|
|
148
|
+
|
|
149
|
+ return this;
|
|
150
|
+ }
|
|
151
|
+}
|