Browse Source

Vector2, Vector3, Quaternion: make the coordinates private

customisations
alemart 10 months ago
parent
commit
ae646530be

+ 6
- 6
docs/api/quaternion.md View File

@@ -8,25 +8,25 @@ A number system used in encantar.js to represent rotations in 3D space.
8 8
 
9 9
 ### x
10 10
 
11
-`quaternion.x: number`
11
+`quaternion.x: number, read-only`
12 12
 
13 13
 The x coordinate of the quaternion (imaginary).
14 14
 
15 15
 ### y
16 16
 
17
-`quaternion.y: number`
17
+`quaternion.y: number, read-only`
18 18
 
19 19
 The y coordinate of the quaternion (imaginary).
20 20
 
21 21
 ### z
22 22
 
23
-`quaternion.z: number`
23
+`quaternion.z: number, read-only`
24 24
 
25 25
 The z coordinate of the quaternion (imaginary).
26 26
 
27 27
 ### w
28 28
 
29
-`quaternion.w: number`
29
+`quaternion.w: number, read-only`
30 30
 
31 31
 The w coordinate of the quaternion (real).
32 32
 
@@ -36,11 +36,11 @@ The w coordinate of the quaternion (real).
36 36
 
37 37
 `quaternion.length(): number`
38 38
 
39
-Compute the length of the quaternion: `sqrt(x^2 + y^2 + z^2 + w^2)`.
39
+Compute the magnitude of the quaternion.
40 40
 
41 41
 **Returns**
42 42
 
43
-The length of the quaternion.
43
+The magnitude of the quaternion.
44 44
 
45 45
 ### equals
46 46
 

+ 4
- 4
docs/api/vector2.md View File

@@ -8,13 +8,13 @@ A vector in 2D space.
8 8
 
9 9
 ### x
10 10
 
11
-`vector.x: number`
11
+`vector.x: number, read-only`
12 12
 
13 13
 The x coordinate of the vector.
14 14
 
15 15
 ### y
16 16
 
17
-`vector.y: number`
17
+`vector.y: number, read-only`
18 18
 
19 19
 The y coordinate of the vector.
20 20
 
@@ -24,11 +24,11 @@ The y coordinate of the vector.
24 24
 
25 25
 `vector.length(): number`
26 26
 
27
-Compute the length of the vector: `sqrt(x^2 + y^2)`.
27
+Compute the magnitude of the vector.
28 28
 
29 29
 **Returns**
30 30
 
31
-The length of the vector.
31
+The magnitude of the vector.
32 32
 
33 33
 ### dot
34 34
 

+ 5
- 5
docs/api/vector3.md View File

@@ -8,19 +8,19 @@ A vector in 3D space.
8 8
 
9 9
 ### x
10 10
 
11
-`vector.x: number`
11
+`vector.x: number, read-only`
12 12
 
13 13
 The x coordinate of the vector.
14 14
 
15 15
 ### y
16 16
 
17
-`vector.y: number`
17
+`vector.y: number, read-only`
18 18
 
19 19
 The y coordinate of the vector.
20 20
 
21 21
 ### z
22 22
 
23
-`vector.z: number`
23
+`vector.z: number, read-only`
24 24
 
25 25
 The z coordinate of the vector.
26 26
 
@@ -30,11 +30,11 @@ The z coordinate of the vector.
30 30
 
31 31
 `vector.length(): number`
32 32
 
33
-Compute the length of the vector: `sqrt(x^2 + y^2 + z^2)`.
33
+Compute the magnitude of the vector.
34 34
 
35 35
 **Returns**
36 36
 
37
-The length of the vector.
37
+The magnitude of the vector.
38 38
 
39 39
 ### dot
40 40
 

+ 17
- 11
src/geometry/pose-filter.ts View File

@@ -150,9 +150,11 @@ export class PoseFilter
150 150
             const w = (T - i) * d;
151 151
 
152 152
             // weighted avg: sum from i=0 to T-1 { (T-i) * t[i] } * (2/(T^2+T))
153
-            t.x += ti.x * w;
154
-            t.y += ti.y * w;
155
-            t.z += ti.z * w;
153
+            t._set(
154
+                t.x + ti.x * w,
155
+                t.y + ti.y * w,
156
+                t.z + ti.z * w
157
+            );
156 158
         }
157 159
 
158 160
         // average *nearby* rotations
@@ -169,16 +171,20 @@ export class PoseFilter
169 171
                 // XXX since Quaternion._fromRotationMatrix() computes w >= 0,
170 172
                 // this will never happen. Leave this here for extra safety
171 173
                 // in case anything changes?
172
-                qi.x = -qi.x;
173
-                qi.y = -qi.y;
174
-                qi.z = -qi.z;
175
-                qi.w = -qi.w;
174
+                qi._set(
175
+                    -qi.x,
176
+                    -qi.y,
177
+                    -qi.z,
178
+                    -qi.w
179
+                );
176 180
             }
177 181
 
178
-            q.x += qi.x * w;
179
-            q.y += qi.y * w;
180
-            q.z += qi.z * w;
181
-            q.w += qi.w * w;
182
+            q._set(
183
+                q.x + qi.x * w,
184
+                q.y + qi.y * w,
185
+                q.z + qi.z * w,
186
+                q.w + qi.w * w
187
+            );
182 188
         }
183 189
         //q._normalize();
184 190
 

+ 87
- 36
src/geometry/quaternion.ts View File

@@ -37,16 +37,16 @@ const EPSILON = 1e-6;
37 37
 export class Quaternion
38 38
 {
39 39
     /** x coordinate (imaginary) */
40
-    public x: number;
40
+    private _x: number;
41 41
 
42 42
     /** y coordinate (imaginary) */
43
-    public y: number;
43
+    private _y: number;
44 44
 
45 45
     /** z coordinate (imaginary) */
46
-    public z: number;
46
+    private _z: number;
47 47
 
48 48
     /** w coordinate (real) */
49
-    public w: number;
49
+    private _w: number;
50 50
 
51 51
 
52 52
 
@@ -59,10 +59,10 @@ export class Quaternion
59 59
      */
60 60
     constructor(x: number = 0, y: number = 0, z: number = 0, w: number = 1)
61 61
     {
62
-        this.x = +x;
63
-        this.y = +y;
64
-        this.z = +z;
65
-        this.w = +w;
62
+        this._x = +x;
63
+        this._y = +y;
64
+        this._z = +z;
65
+        this._w = +w;
66 66
     }
67 67
 
68 68
     /**
@@ -75,15 +75,47 @@ export class Quaternion
75 75
     }
76 76
 
77 77
     /**
78
+     * The x coordinate of the quaternion (imaginary)
79
+     */
80
+    get x(): number
81
+    {
82
+        return this._x;
83
+    }
84
+
85
+    /**
86
+     * The y coordinate of the quaternion (imaginary)
87
+     */
88
+    get y(): number
89
+    {
90
+        return this._y;
91
+    }
92
+
93
+    /**
94
+     * The z coordinate of the quaternion (imaginary)
95
+     */
96
+    get z(): number
97
+    {
98
+        return this._z;
99
+    }
100
+
101
+    /**
102
+     * The w coordinate of the quaternion (real)
103
+     */
104
+    get w(): number
105
+    {
106
+        return this._w;
107
+    }
108
+
109
+    /**
78 110
      * The length of this quaternion
79 111
      * @returns sqrt(x^2 + y^2 + z^2 + w^2)
80 112
      */
81 113
     length(): number
82 114
     {
83
-        const x = this.x;
84
-        const y = this.y;
85
-        const z = this.z;
86
-        const w = this.w;
115
+        const x = this._x;
116
+        const y = this._y;
117
+        const z = this._z;
118
+        const w = this._w;
87 119
 
88 120
         return Math.sqrt(x*x + y*y + z*z + w*w);
89 121
     }
@@ -95,7 +127,7 @@ export class Quaternion
95 127
      */
96 128
     equals(q: Quaternion): boolean
97 129
     {
98
-        return this.w === q.w && this.x === q.x && this.y === q.y && this.z === q.z;
130
+        return this._w === q._w && this._x === q._x && this._y === q._y && this._z === q._z;
99 131
     }
100 132
 
101 133
     /**
@@ -104,10 +136,10 @@ export class Quaternion
104 136
      */
105 137
     toString(): string
106 138
     {
107
-        const x = this.x.toFixed(4);
108
-        const y = this.y.toFixed(4);
109
-        const z = this.z.toFixed(4);
110
-        const w = this.w.toFixed(4);
139
+        const x = this._x.toFixed(4);
140
+        const y = this._y.toFixed(4);
141
+        const z = this._z.toFixed(4);
142
+        const w = this._w.toFixed(4);
111 143
 
112 144
         return `Quaternion(${x},${y},${z},${w})`;
113 145
     }
@@ -124,10 +156,10 @@ export class Quaternion
124 156
         if(length < EPSILON) // zero?
125 157
             return this;
126 158
 
127
-        this.x /= length;
128
-        this.y /= length;
129
-        this.z /= length;
130
-        this.w /= length;
159
+        this._x /= length;
160
+        this._y /= length;
161
+        this._z /= length;
162
+        this._w /= length;
131 163
 
132 164
         return this;
133 165
     }
@@ -139,9 +171,28 @@ export class Quaternion
139 171
      */
140 172
     _conjugate(): Quaternion
141 173
     {
142
-        this.x = -this.x;
143
-        this.y = -this.y;
144
-        this.z = -this.z;
174
+        this._x = -this._x;
175
+        this._y = -this._y;
176
+        this._z = -this._z;
177
+
178
+        return this;
179
+    }
180
+
181
+    /**
182
+     * Set the coordinates of this quaternion
183
+     * @param x x-coordinate
184
+     * @param y y-coordinate
185
+     * @param z z-coordinate
186
+     * @param w w-coordinate
187
+     * @returns this quaternion
188
+     * @internal
189
+     */
190
+    _set(x: number, y: number, z: number, w: number): Quaternion
191
+    {
192
+        this._x = +x;
193
+        this._y = +y;
194
+        this._z = +z;
195
+        this._w = +w;
145 196
 
146 197
         return this;
147 198
     }
@@ -154,10 +205,10 @@ export class Quaternion
154 205
      */
155 206
     _copyFrom(q: Quaternion): Quaternion
156 207
     {
157
-        this.x = q.x;
158
-        this.y = q.y;
159
-        this.z = q.z;
160
-        this.w = q.w;
208
+        this._x = q._x;
209
+        this._y = q._y;
210
+        this._z = q._z;
211
+        this._w = q._w;
161 212
 
162 213
         return this;
163 214
     }
@@ -176,10 +227,10 @@ export class Quaternion
176 227
             return Speedy.Matrix.Eye(3);
177 228
 
178 229
         // let q = (x,y,z,w) be a unit quaternion
179
-        const x = this.x / length;
180
-        const y = this.y / length;
181
-        const z = this.z / length;
182
-        const w = this.w / length;
230
+        const x = this._x / length;
231
+        const y = this._y / length;
232
+        const z = this._z / length;
233
+        const w = this._w / length;
183 234
 
184 235
         /*
185 236
 
@@ -334,10 +385,10 @@ export class Quaternion
334 385
         const z = 0.5 * Math.sqrt(Math.max(0, tr - 2 * (m11 + m22))); // |z|
335 386
 
336 387
         const length = Math.sqrt(x*x + y*y + z*z + w*w); // should be ~ 1
337
-        this.x = (x * sx) / length;
338
-        this.y = (y * sy) / length;
339
-        this.z = (z * sz) / length;
340
-        this.w = w / length;
388
+        this._x = (x * sx) / length;
389
+        this._y = (y * sy) / length;
390
+        this._z = (z * sz) / length;
391
+        this._w = w / length;
341 392
 
342 393
         return this;
343 394
     }

+ 42
- 26
src/geometry/vector2.ts View File

@@ -38,10 +38,10 @@ let ZERO: Nullable<Vector2> = null;
38 38
 export class Vector2
39 39
 {
40 40
     /** x coordinate */
41
-    public x: number;
41
+    private _x: number;
42 42
 
43 43
     /** y coordinate */
44
-    public y: number;
44
+    private _y: number;
45 45
 
46 46
 
47 47
 
@@ -52,8 +52,8 @@ export class Vector2
52 52
      */
53 53
     constructor(x: number = 0, y: number = 0)
54 54
     {
55
-        this.x = +x;
56
-        this.y = +y;
55
+        this._x = +x;
56
+        this._y = +y;
57 57
     }
58 58
 
59 59
     /**
@@ -71,7 +71,23 @@ export class Vector2
71 71
      */
72 72
     static get ZERO(): Vector2
73 73
     {
74
-        return ZERO || (ZERO = Object.freeze(Vector2.Zero()));
74
+        return ZERO || (ZERO = Object.freeze(Vector2.Zero()) as Vector2);
75
+    }
76
+
77
+    /**
78
+     * The x coordinate of the vector
79
+     */
80
+    get x(): number
81
+    {
82
+        return this._x;
83
+    }
84
+
85
+    /**
86
+     * The y coordinate of the vector
87
+     */
88
+    get y(): number
89
+    {
90
+        return this._y;
75 91
     }
76 92
 
77 93
     /**
@@ -80,8 +96,8 @@ export class Vector2
80 96
      */
81 97
     length(): number
82 98
     {
83
-        const x = this.x;
84
-        const y = this.y;
99
+        const x = this._x;
100
+        const y = this._y;
85 101
 
86 102
         return Math.sqrt(x*x + y*y);
87 103
     }
@@ -93,7 +109,7 @@ export class Vector2
93 109
      */
94 110
     dot(v: Vector2): number
95 111
     {
96
-        return this.x * v.x + this.y * v.y;
112
+        return this._x * v._x + this._y * v._y;
97 113
     }
98 114
 
99 115
     /**
@@ -103,8 +119,8 @@ export class Vector2
103 119
      */
104 120
     distanceTo(v: Vector2): number
105 121
     {
106
-        const dx = this.x - v.x;
107
-        const dy = this.y - v.y;
122
+        const dx = this._x - v._x;
123
+        const dy = this._y - v._y;
108 124
 
109 125
         return Math.sqrt(dx*dx + dy*dy);
110 126
     }
@@ -126,7 +142,7 @@ export class Vector2
126 142
      */
127 143
     equals(v: Vector2): boolean
128 144
     {
129
-        return this.x === v.x && this.y === v.y;
145
+        return this._x === v._x && this._y === v._y;
130 146
     }
131 147
 
132 148
     /**
@@ -135,8 +151,8 @@ export class Vector2
135 151
      */
136 152
     toString(): string
137 153
     {
138
-        const x = this.x.toFixed(5);
139
-        const y = this.y.toFixed(5);
154
+        const x = this._x.toFixed(5);
155
+        const y = this._y.toFixed(5);
140 156
 
141 157
         return `Vector2(${x},${y})`;
142 158
     }
@@ -148,7 +164,7 @@ export class Vector2
148 164
      */
149 165
     _clone(): Vector2
150 166
     {
151
-        return new Vector2(this.x, this.y);
167
+        return new Vector2(this._x, this._y);
152 168
     }
153 169
 
154 170
     /**
@@ -160,8 +176,8 @@ export class Vector2
160 176
      */
161 177
     _set(x: number, y: number): Vector2
162 178
     {
163
-        this.x = +x;
164
-        this.y = +y;
179
+        this._x = +x;
180
+        this._y = +y;
165 181
 
166 182
         return this;
167 183
     }
@@ -174,8 +190,8 @@ export class Vector2
174 190
      */
175 191
     _copyFrom(v: Vector2): Vector2
176 192
     {
177
-        this.x = v.x;
178
-        this.y = v.y;
193
+        this._x = v._x;
194
+        this._y = v._y;
179 195
 
180 196
         return this;
181 197
     }
@@ -192,8 +208,8 @@ export class Vector2
192 208
         if(length < EPSILON) // zero?
193 209
             return this;
194 210
 
195
-        this.x /= length;
196
-        this.y /= length;
211
+        this._x /= length;
212
+        this._y /= length;
197 213
 
198 214
         return this;
199 215
     }
@@ -206,8 +222,8 @@ export class Vector2
206 222
      */
207 223
     _add(v: Vector2): Vector2
208 224
     {
209
-        this.x += v.x;
210
-        this.y += v.y;
225
+        this._x += v._x;
226
+        this._y += v._y;
211 227
 
212 228
         return this;
213 229
     }
@@ -220,8 +236,8 @@ export class Vector2
220 236
      */
221 237
     _subtract(v: Vector2): Vector2
222 238
     {
223
-        this.x -= v.x;
224
-        this.y -= v.y;
239
+        this._x -= v._x;
240
+        this._y -= v._y;
225 241
 
226 242
         return this;
227 243
     }
@@ -234,8 +250,8 @@ export class Vector2
234 250
      */
235 251
     _scale(s: number): Vector2
236 252
     {
237
-        this.x *= s;
238
-        this.y *= s;
253
+        this._x *= s;
254
+        this._y *= s;
239 255
 
240 256
         return this;
241 257
     }

+ 64
- 40
src/geometry/vector3.ts View File

@@ -38,13 +38,13 @@ let ZERO: Nullable<Vector3> = null;
38 38
 export class Vector3
39 39
 {
40 40
     /** x coordinate */
41
-    public x: number;
41
+    private _x: number;
42 42
 
43 43
     /** y coordinate */
44
-    public y: number;
44
+    private _y: number;
45 45
 
46 46
     /** z coordinate */
47
-    public z: number;
47
+    private _z: number;
48 48
 
49 49
 
50 50
 
@@ -53,9 +53,9 @@ export class Vector3
53 53
      */
54 54
     constructor(x: number = 0, y: number = 0, z: number = 0)
55 55
     {
56
-        this.x = +x;
57
-        this.y = +y;
58
-        this.z = +z;
56
+        this._x = +x;
57
+        this._y = +y;
58
+        this._z = +z;
59 59
     }
60 60
 
61 61
     /**
@@ -73,7 +73,31 @@ export class Vector3
73 73
      */
74 74
     static get ZERO(): Vector3
75 75
     {
76
-        return ZERO || (ZERO = Object.freeze(Vector3.Zero()));
76
+        return ZERO || (ZERO = Object.freeze(Vector3.Zero()) as Vector3);
77
+    }
78
+
79
+    /**
80
+     * The x coordinate of the vector
81
+     */
82
+    get x(): number
83
+    {
84
+        return this._x;
85
+    }
86
+
87
+    /**
88
+     * The y coordinate of the vector
89
+     */
90
+    get y(): number
91
+    {
92
+        return this._y;
93
+    }
94
+
95
+    /**
96
+     * The z coordinate of the vector
97
+     */
98
+    get z(): number
99
+    {
100
+        return this._z;
77 101
     }
78 102
 
79 103
     /**
@@ -82,9 +106,9 @@ export class Vector3
82 106
      */
83 107
     length(): number
84 108
     {
85
-        const x = this.x;
86
-        const y = this.y;
87
-        const z = this.z;
109
+        const x = this._x;
110
+        const y = this._y;
111
+        const z = this._z;
88 112
 
89 113
         return Math.sqrt(x*x + y*y + z*z);
90 114
     }
@@ -96,7 +120,7 @@ export class Vector3
96 120
      */
97 121
     dot(v: Vector3): number
98 122
     {
99
-        return this.x * v.x + this.y * v.y + this.z * v.z;
123
+        return this._x * v._x + this._y * v._y + this._z * v._z;
100 124
     }
101 125
 
102 126
     /**
@@ -106,9 +130,9 @@ export class Vector3
106 130
      */
107 131
     distanceTo(v: Vector3): number
108 132
     {
109
-        const dx = this.x - v.x;
110
-        const dy = this.y - v.y;
111
-        const dz = this.z - v.z;
133
+        const dx = this._x - v._x;
134
+        const dy = this._y - v._y;
135
+        const dz = this._z - v._z;
112 136
 
113 137
         return Math.sqrt(dx*dx + dy*dy + dz*dz);
114 138
     }
@@ -130,9 +154,9 @@ export class Vector3
130 154
      */
131 155
     cross(v: Vector3): Vector3
132 156
     {
133
-        const x = this.y * v.z - this.z * v.y;
134
-        const y = this.z * v.x - this.x * v.z;
135
-        const z = this.x * v.y - this.y * v.x;
157
+        const x = this._y * v._z - this._z * v._y;
158
+        const y = this._z * v._x - this._x * v._z;
159
+        const z = this._x * v._y - this._y * v._x;
136 160
 
137 161
         return new Vector3(x, y, z);
138 162
     }
@@ -144,7 +168,7 @@ export class Vector3
144 168
      */
145 169
     equals(v: Vector3): boolean
146 170
     {
147
-        return this.x === v.x && this.y === v.y && this.z === v.z;
171
+        return this._x === v._x && this._y === v._y && this._z === v._z;
148 172
     }
149 173
 
150 174
     /**
@@ -153,9 +177,9 @@ export class Vector3
153 177
      */
154 178
     toString(): string
155 179
     {
156
-        const x = this.x.toFixed(5);
157
-        const y = this.y.toFixed(5);
158
-        const z = this.z.toFixed(5);
180
+        const x = this._x.toFixed(5);
181
+        const y = this._y.toFixed(5);
182
+        const z = this._z.toFixed(5);
159 183
 
160 184
         return `Vector3(${x},${y},${z})`;
161 185
     }
@@ -167,7 +191,7 @@ export class Vector3
167 191
      */
168 192
     _clone(): Vector3
169 193
     {
170
-        return new Vector3(this.x, this.y, this.z);
194
+        return new Vector3(this._x, this._y, this._z);
171 195
     }
172 196
 
173 197
     /**
@@ -180,9 +204,9 @@ export class Vector3
180 204
      */
181 205
     _set(x: number, y: number, z: number): Vector3
182 206
     {
183
-        this.x = +x;
184
-        this.y = +y;
185
-        this.z = +z;
207
+        this._x = +x;
208
+        this._y = +y;
209
+        this._z = +z;
186 210
 
187 211
         return this;
188 212
     }
@@ -195,9 +219,9 @@ export class Vector3
195 219
      */
196 220
     _copyFrom(v: Vector3): Vector3
197 221
     {
198
-        this.x = v.x;
199
-        this.y = v.y;
200
-        this.z = v.z;
222
+        this._x = v._x;
223
+        this._y = v._y;
224
+        this._z = v._z;
201 225
 
202 226
         return this;
203 227
     }
@@ -214,9 +238,9 @@ export class Vector3
214 238
         if(length < EPSILON) // zero?
215 239
             return this;
216 240
 
217
-        this.x /= length;
218
-        this.y /= length;
219
-        this.z /= length;
241
+        this._x /= length;
242
+        this._y /= length;
243
+        this._z /= length;
220 244
 
221 245
         return this;
222 246
     }
@@ -229,9 +253,9 @@ export class Vector3
229 253
      */
230 254
     _add(v: Vector3): Vector3
231 255
     {
232
-        this.x += v.x;
233
-        this.y += v.y;
234
-        this.z += v.z;
256
+        this._x += v._x;
257
+        this._y += v._y;
258
+        this._z += v._z;
235 259
 
236 260
         return this;
237 261
     }
@@ -244,9 +268,9 @@ export class Vector3
244 268
      */
245 269
     _subtract(v: Vector3): Vector3
246 270
     {
247
-        this.x -= v.x;
248
-        this.y -= v.y;
249
-        this.z -= v.z;
271
+        this._x -= v._x;
272
+        this._y -= v._y;
273
+        this._z -= v._z;
250 274
 
251 275
         return this;
252 276
     }
@@ -259,9 +283,9 @@ export class Vector3
259 283
      */
260 284
     _scale(s: number): Vector3
261 285
     {
262
-        this.x *= s;
263
-        this.y *= s;
264
-        this.z *= s;
286
+        this._x *= s;
287
+        this._y *= s;
288
+        this._z *= s;
265 289
 
266 290
         return this;
267 291
     }

Loading…
Cancel
Save