@math.gl/core 3.5.6 → 3.6.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/dist/classes/base/math-array.d.ts +66 -0
  2. package/dist/classes/base/math-array.d.ts.map +1 -0
  3. package/dist/classes/base/math-array.js +216 -0
  4. package/dist/classes/base/matrix.d.ts +14 -0
  5. package/dist/classes/base/matrix.d.ts.map +1 -0
  6. package/dist/classes/base/matrix.js +61 -0
  7. package/dist/classes/base/vector.d.ts +44 -0
  8. package/dist/classes/base/vector.d.ts.map +1 -0
  9. package/dist/classes/base/vector.js +126 -0
  10. package/dist/classes/euler.d.ts +77 -0
  11. package/dist/classes/euler.d.ts.map +1 -0
  12. package/dist/classes/euler.js +485 -0
  13. package/dist/classes/matrix3.d.ts +60 -0
  14. package/dist/classes/matrix3.d.ts.map +1 -0
  15. package/dist/classes/matrix3.js +201 -0
  16. package/dist/classes/matrix4.d.ts +226 -0
  17. package/dist/classes/matrix4.d.ts.map +1 -0
  18. package/dist/classes/matrix4.js +538 -0
  19. package/dist/classes/pose.d.ts +40 -0
  20. package/dist/classes/pose.d.ts.map +1 -0
  21. package/dist/classes/pose.js +109 -0
  22. package/dist/classes/quaternion.d.ts +63 -0
  23. package/dist/classes/quaternion.d.ts.map +1 -0
  24. package/dist/classes/quaternion.js +262 -0
  25. package/dist/classes/spherical-coordinates.d.ts +60 -0
  26. package/dist/classes/spherical-coordinates.d.ts.map +1 -0
  27. package/dist/classes/spherical-coordinates.js +148 -0
  28. package/dist/classes/vector2.d.ts +55 -0
  29. package/dist/classes/vector2.d.ts.map +1 -0
  30. package/dist/classes/vector2.js +111 -0
  31. package/dist/classes/vector3.d.ts +56 -0
  32. package/dist/classes/vector3.d.ts.map +1 -0
  33. package/dist/classes/vector3.js +133 -0
  34. package/dist/classes/vector4.d.ts +41 -0
  35. package/dist/classes/vector4.d.ts.map +1 -0
  36. package/dist/classes/vector4.js +114 -0
  37. package/dist/es5/classes/base/math-array.js +57 -61
  38. package/dist/es5/classes/base/math-array.js.map +1 -1
  39. package/dist/es5/classes/base/matrix.js +0 -14
  40. package/dist/es5/classes/base/matrix.js.map +1 -1
  41. package/dist/es5/classes/base/vector.js +0 -12
  42. package/dist/es5/classes/base/vector.js.map +1 -1
  43. package/dist/es5/classes/euler.js +63 -49
  44. package/dist/es5/classes/euler.js.map +1 -1
  45. package/dist/es5/classes/matrix3.js +60 -33
  46. package/dist/es5/classes/matrix3.js.map +1 -1
  47. package/dist/es5/classes/matrix4.js +167 -164
  48. package/dist/es5/classes/matrix4.js.map +1 -1
  49. package/dist/es5/classes/pose.js +7 -6
  50. package/dist/es5/classes/pose.js.map +1 -1
  51. package/dist/es5/classes/quaternion.js +44 -36
  52. package/dist/es5/classes/quaternion.js.map +1 -1
  53. package/dist/es5/classes/spherical-coordinates.js +12 -8
  54. package/dist/es5/classes/spherical-coordinates.js.map +1 -1
  55. package/dist/es5/classes/vector2.js.map +1 -1
  56. package/dist/es5/classes/vector3.js +7 -2
  57. package/dist/es5/classes/vector3.js.map +1 -1
  58. package/dist/es5/classes/vector4.js +7 -2
  59. package/dist/es5/classes/vector4.js.map +1 -1
  60. package/dist/es5/docs/README.md +0 -0
  61. package/dist/es5/index.js +71 -89
  62. package/dist/es5/index.js.map +1 -1
  63. package/dist/es5/lib/assert.js.map +1 -1
  64. package/dist/es5/lib/common.js +40 -43
  65. package/dist/es5/lib/common.js.map +1 -1
  66. package/dist/es5/lib/gl-matrix-extras.js.map +1 -1
  67. package/dist/es5/lib/gl-matrix.d.js +2 -0
  68. package/dist/es5/lib/gl-matrix.d.js.map +1 -0
  69. package/dist/es5/lib/math-utils.js.map +1 -1
  70. package/dist/es5/lib/validators.js.map +1 -1
  71. package/dist/esm/classes/base/math-array.js +45 -47
  72. package/dist/esm/classes/base/math-array.js.map +1 -1
  73. package/dist/esm/classes/base/matrix.js +0 -11
  74. package/dist/esm/classes/base/matrix.js.map +1 -1
  75. package/dist/esm/classes/base/vector.js +0 -10
  76. package/dist/esm/classes/base/vector.js.map +1 -1
  77. package/dist/esm/classes/euler.js +61 -48
  78. package/dist/esm/classes/euler.js.map +1 -1
  79. package/dist/esm/classes/matrix3.js +55 -33
  80. package/dist/esm/classes/matrix3.js.map +1 -1
  81. package/dist/esm/classes/matrix4.js +156 -136
  82. package/dist/esm/classes/matrix4.js.map +1 -1
  83. package/dist/esm/classes/pose.js +8 -4
  84. package/dist/esm/classes/pose.js.map +1 -1
  85. package/dist/esm/classes/quaternion.js +37 -27
  86. package/dist/esm/classes/quaternion.js.map +1 -1
  87. package/dist/esm/classes/spherical-coordinates.js +15 -5
  88. package/dist/esm/classes/spherical-coordinates.js.map +1 -1
  89. package/dist/esm/classes/vector2.js.map +1 -1
  90. package/dist/esm/classes/vector3.js +7 -2
  91. package/dist/esm/classes/vector3.js.map +1 -1
  92. package/dist/esm/classes/vector4.js +7 -2
  93. package/dist/esm/classes/vector4.js.map +1 -1
  94. package/dist/esm/docs/README.md +0 -0
  95. package/dist/esm/index.js +2 -13
  96. package/dist/esm/index.js.map +1 -1
  97. package/dist/esm/lib/assert.js.map +1 -1
  98. package/dist/esm/lib/common.js +41 -44
  99. package/dist/esm/lib/common.js.map +1 -1
  100. package/dist/esm/lib/gl-matrix-extras.js.map +1 -1
  101. package/dist/esm/lib/gl-matrix.d.js +2 -0
  102. package/dist/esm/lib/gl-matrix.d.js.map +1 -0
  103. package/dist/esm/lib/math-utils.js.map +1 -1
  104. package/dist/esm/lib/validators.js.map +1 -1
  105. package/dist/index.d.ts +17 -0
  106. package/dist/index.d.ts.map +1 -0
  107. package/dist/index.js +24 -0
  108. package/dist/lib/assert.d.ts +2 -0
  109. package/dist/lib/assert.d.ts.map +1 -0
  110. package/dist/lib/assert.js +5 -0
  111. package/dist/lib/common.d.ts +93 -0
  112. package/dist/lib/common.d.ts.map +1 -0
  113. package/dist/lib/common.js +210 -0
  114. package/dist/lib/gl-matrix-extras.d.ts +7 -0
  115. package/dist/lib/gl-matrix-extras.d.ts.map +1 -0
  116. package/dist/lib/gl-matrix-extras.js +49 -0
  117. package/dist/lib/math-utils.d.ts +28 -0
  118. package/dist/lib/math-utils.d.ts.map +1 -0
  119. package/dist/lib/math-utils.js +28 -0
  120. package/dist/lib/validators.d.ts +6 -0
  121. package/dist/lib/validators.d.ts.map +1 -0
  122. package/dist/lib/validators.js +33 -0
  123. package/package.json +5 -4
  124. package/src/classes/base/math-array.ts +265 -0
  125. package/src/classes/base/{matrix.js → matrix.ts} +15 -19
  126. package/src/classes/base/{vector.js → vector.ts} +47 -54
  127. package/src/classes/{euler.js → euler.ts} +121 -221
  128. package/src/classes/matrix3.ts +253 -0
  129. package/src/classes/matrix4.ts +668 -0
  130. package/src/classes/{pose.js → pose.ts} +52 -49
  131. package/src/classes/{quaternion.js → quaternion.ts} +107 -95
  132. package/src/classes/{spherical-coordinates.js → spherical-coordinates.ts} +84 -54
  133. package/src/classes/vector2.ts +127 -0
  134. package/src/classes/{vector3.js → vector3.ts} +43 -55
  135. package/src/classes/{vector4.js → vector4.ts} +44 -48
  136. package/src/docs/README.md +0 -0
  137. package/{dist/esm/index.d.ts → src/index.ts} +32 -34
  138. package/src/lib/{assert.js → assert.ts} +1 -1
  139. package/src/lib/common.ts +304 -0
  140. package/src/lib/{gl-matrix-extras.js → gl-matrix-extras.ts} +26 -5
  141. package/src/lib/gl-matrix.d.ts +266 -0
  142. package/src/lib/{math-utils.js → math-utils.ts} +0 -0
  143. package/src/lib/{validators.js → validators.ts} +10 -6
  144. package/dist/es5/addons/polygon.d.ts +0 -17
  145. package/dist/es5/classes/base/math-array.d.ts +0 -56
  146. package/dist/es5/classes/base/matrix.d.ts +0 -15
  147. package/dist/es5/classes/base/vector.d.ts +0 -45
  148. package/dist/es5/classes/euler.d.ts +0 -92
  149. package/dist/es5/classes/matrix3.d.ts +0 -91
  150. package/dist/es5/classes/matrix4.d.ts +0 -161
  151. package/dist/es5/classes/pose.d.ts +0 -92
  152. package/dist/es5/classes/quaternion.d.ts +0 -107
  153. package/dist/es5/classes/spherical-coordinates.d.ts +0 -87
  154. package/dist/es5/classes/vector2.d.ts +0 -65
  155. package/dist/es5/classes/vector3.d.ts +0 -65
  156. package/dist/es5/classes/vector4.d.ts +0 -34
  157. package/dist/es5/index.d.ts +0 -54
  158. package/dist/es5/lib/common.d.ts +0 -80
  159. package/dist/esm/addons/polygon.d.ts +0 -17
  160. package/dist/esm/classes/base/math-array.d.ts +0 -56
  161. package/dist/esm/classes/base/matrix.d.ts +0 -15
  162. package/dist/esm/classes/base/vector.d.ts +0 -45
  163. package/dist/esm/classes/euler.d.ts +0 -92
  164. package/dist/esm/classes/matrix3.d.ts +0 -91
  165. package/dist/esm/classes/matrix4.d.ts +0 -161
  166. package/dist/esm/classes/pose.d.ts +0 -92
  167. package/dist/esm/classes/quaternion.d.ts +0 -107
  168. package/dist/esm/classes/spherical-coordinates.d.ts +0 -87
  169. package/dist/esm/classes/vector2.d.ts +0 -65
  170. package/dist/esm/classes/vector3.d.ts +0 -65
  171. package/dist/esm/classes/vector4.d.ts +0 -34
  172. package/dist/esm/lib/common.d.ts +0 -80
  173. package/src/addons/polygon.d.ts +0 -17
  174. package/src/classes/base/math-array.d.ts +0 -56
  175. package/src/classes/base/math-array.js +0 -250
  176. package/src/classes/base/matrix.d.ts +0 -15
  177. package/src/classes/base/vector.d.ts +0 -45
  178. package/src/classes/euler.d.ts +0 -92
  179. package/src/classes/matrix3.d.ts +0 -91
  180. package/src/classes/matrix3.js +0 -225
  181. package/src/classes/matrix4.d.ts +0 -161
  182. package/src/classes/matrix4.js +0 -530
  183. package/src/classes/pose.d.ts +0 -92
  184. package/src/classes/quaternion.d.ts +0 -107
  185. package/src/classes/spherical-coordinates.d.ts +0 -87
  186. package/src/classes/vector2.d.ts +0 -65
  187. package/src/classes/vector2.js +0 -122
  188. package/src/classes/vector3.d.ts +0 -65
  189. package/src/classes/vector4.d.ts +0 -34
  190. package/src/index.d.ts +0 -54
  191. package/src/index.js +0 -86
  192. package/src/lib/common.d.ts +0 -80
  193. package/src/lib/common.js +0 -222
@@ -1,55 +1,77 @@
1
1
  // Copyright (c) 2017 Uber Technologies, Inc.
2
- //
3
- // Permission is hereby granted, free of charge, to any person obtaining a copy
4
- // of this software and associated documentation files (the "Software"), to deal
5
- // in the Software without restriction, including without limitation the rights
6
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- // copies of the Software, and to permit persons to whom the Software is
8
- // furnished to do so, subject to the following conditions:
9
- //
10
- // The above copyright notice and this permission notice shall be included in
11
- // all copies or substantial portions of the Software.
12
- //
13
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- // THE SOFTWARE.
20
-
2
+ // MIT License
21
3
  // Adaptation of THREE.js Spherical class, under MIT license
4
+ import Vector3 from './vector3';
22
5
  import {formatValue, equals, config} from '../lib/common';
23
6
  import {degrees, radians, clamp} from '../lib/common';
24
- import Vector3 from './vector3';
25
-
26
- // @ts-ignore: error TS2307: Cannot find module 'gl-matrix/...'.
27
7
  import * as vec3 from 'gl-matrix/vec3';
8
+ import {NumericArray} from '@math.gl/types';
9
+
10
+ type SphericalCoordinatesOptions = {
11
+ phi?: number;
12
+ theta?: number;
13
+ radius?: number;
14
+ bearing?: number;
15
+ pitch?: number;
16
+ altitude?: number;
17
+ radiusScale?: number;
18
+ };
19
+
20
+ type FormatOptions = {
21
+ printTypes?: boolean;
22
+ };
28
23
 
29
24
  // TODO - import epsilon
30
25
  const EPSILON = 0.000001;
26
+ const EARTH_RADIUS_METERS = 6371000;
31
27
 
32
- const EARTH_RADIUS_METERS = 6.371e6;
33
-
34
- // Todo [rho, theta, phi] ?
28
+ /**
29
+ * The poles (phi) are at the positive and negative y axis.
30
+ * The equator starts at positive z.
31
+ * @link https://en.wikipedia.org/wiki/Spherical_coordinate_system
32
+ */
35
33
  export default class SphericalCoordinates {
36
- // @ts-ignore TS2740: Type '{}' is missing the following properties from type
34
+ phi: number;
35
+ theta: number;
36
+ radius: number;
37
+ radiusScale: number;
38
+ // bearing: number;
39
+ // pitch: number;
40
+ // altitude: number;
41
+
42
+ // lnglatZ coordinates
43
+ // longitude: number;
44
+ // latitude: number;
45
+ // lng: number;
46
+ // lat: number;
47
+ // z: number;
48
+
49
+ /**
50
+ * Creates a new SphericalCoordinates object
51
+ * @param options
52
+ * @param [options.phi] =0 - rotation around X (latitude)
53
+ * @param [options.theta] =0 - rotation around Y (longitude)
54
+ * @param [options.radius] =1 - Distance from center
55
+ * @param [options.bearing]
56
+ * @param [options.pitch]
57
+ * @param [options.altitude]
58
+ * @param [options.radiusScale] =1
59
+ */
37
60
  // eslint-disable-next-line complexity
38
61
  constructor({
39
62
  phi = 0,
40
63
  theta = 0,
41
64
  radius = 1,
42
- bearing = undefined,
43
- pitch = undefined,
44
- altitude = undefined,
65
+ bearing,
66
+ pitch,
67
+ altitude,
45
68
  radiusScale = EARTH_RADIUS_METERS
46
- } = {}) {
69
+ }: SphericalCoordinatesOptions = {}) {
47
70
  this.phi = phi;
48
71
  this.theta = theta;
49
72
  // TODO - silently accepts illegal 0
50
73
  this.radius = radius || altitude || 1; // radial distance from center
51
74
  this.radiusScale = radiusScale || 1; // Used by lngLatZ
52
-
53
75
  if (bearing !== undefined) {
54
76
  this.bearing = bearing; // up / down towards top and bottom pole
55
77
  }
@@ -59,17 +81,17 @@ export default class SphericalCoordinates {
59
81
  this.check();
60
82
  }
61
83
 
62
- toString() {
84
+ toString(): string {
63
85
  return this.formatString(config);
64
86
  }
65
87
 
66
- formatString({printTypes = false}) {
88
+ formatString({printTypes = false}: FormatOptions): string {
67
89
  const f = formatValue;
68
90
  return `${printTypes ? 'Spherical' : ''}\
69
91
  [rho:${f(this.radius)},theta:${f(this.theta)},phi:${f(this.phi)}]`;
70
92
  }
71
93
 
72
- equals(other) {
94
+ equals(other: SphericalCoordinates): boolean {
73
95
  return (
74
96
  equals(this.radius, other.radius) &&
75
97
  equals(this.theta, other.theta) &&
@@ -77,71 +99,79 @@ export default class SphericalCoordinates {
77
99
  );
78
100
  }
79
101
 
80
- exactEquals(other) {
102
+ exactEquals(other: SphericalCoordinates): boolean {
81
103
  return this.radius === other.radius && this.theta === other.theta && this.phi === other.phi;
82
104
  }
83
105
 
84
106
  /* eslint-disable brace-style */
85
107
  // Cartographic (bearing 0 north, pitch 0 look from above)
86
- get bearing() {
108
+ get bearing(): number {
87
109
  return 180 - degrees(this.phi);
88
110
  }
89
- set bearing(v) {
111
+
112
+ set bearing(v: number) {
90
113
  this.phi = Math.PI - radians(v);
91
114
  }
92
- get pitch() {
115
+
116
+ get pitch(): number {
93
117
  return degrees(this.theta);
94
118
  }
95
- set pitch(v) {
119
+
120
+ set pitch(v: number) {
96
121
  this.theta = radians(v);
97
122
  }
123
+
98
124
  // get pitch() { return 90 - degrees(this.phi); }
99
125
  // set pitch(v) { this.phi = radians(v) + Math.PI / 2; }
100
126
  // get altitude() { return this.radius - 1; } // relative altitude
101
-
102
127
  // lnglatZ coordinates
103
- get longitude() {
128
+ get longitude(): number {
104
129
  return degrees(this.phi);
105
130
  }
106
- get latitude() {
131
+
132
+ get latitude(): number {
107
133
  return degrees(this.theta);
108
134
  }
109
- get lng() {
135
+
136
+ get lng(): number {
110
137
  return degrees(this.phi);
111
138
  }
112
- get lat() {
139
+
140
+ get lat(): number {
113
141
  return degrees(this.theta);
114
142
  }
115
- get z() {
143
+
144
+ get z(): number {
116
145
  return (this.radius - 1) * this.radiusScale;
117
146
  }
118
- /* eslint-enable brace-style */
119
147
 
120
- set(radius, phi, theta) {
148
+ /* eslint-enable brace-style */
149
+ set(radius: number, phi: number, theta: number): this {
121
150
  this.radius = radius;
122
151
  this.phi = phi;
123
152
  this.theta = theta;
124
153
  return this.check();
125
154
  }
126
155
 
127
- clone() {
156
+ clone(): SphericalCoordinates {
128
157
  return new SphericalCoordinates().copy(this);
129
158
  }
130
159
 
131
- copy(other) {
160
+ copy(other: SphericalCoordinates): this {
132
161
  this.radius = other.radius;
133
162
  this.phi = other.phi;
134
163
  this.theta = other.theta;
135
164
  return this.check();
136
165
  }
137
166
 
138
- fromLngLatZ([lng, lat, z]) {
167
+ fromLngLatZ([lng, lat, z]: [number, number, number]): this {
139
168
  this.radius = 1 + z / this.radiusScale;
140
169
  this.phi = radians(lat);
141
170
  this.theta = radians(lng);
171
+ return this.check();
142
172
  }
143
173
 
144
- fromVector3(v) {
174
+ fromVector3(v: Readonly<NumericArray>): this {
145
175
  this.radius = vec3.length(v);
146
176
  if (this.radius > 0) {
147
177
  this.theta = Math.atan2(v[0], v[1]); // equator angle around y-up axis
@@ -150,19 +180,19 @@ export default class SphericalCoordinates {
150
180
  return this.check();
151
181
  }
152
182
 
153
- toVector3() {
183
+ toVector3(): Vector3 {
154
184
  return new Vector3(0, 0, this.radius)
155
185
  .rotateX({radians: this.theta})
156
186
  .rotateZ({radians: this.phi});
157
187
  }
158
188
 
159
189
  // restrict phi to be betwee EPS and PI-EPS
160
- makeSafe() {
190
+ makeSafe(): this {
161
191
  this.phi = Math.max(EPSILON, Math.min(Math.PI - EPSILON, this.phi));
162
192
  return this;
163
193
  }
164
194
 
165
- check() {
195
+ check(): this {
166
196
  // this.makeSafe();
167
197
  if (!Number.isFinite(this.phi) || !Number.isFinite(this.theta) || !(this.radius > 0)) {
168
198
  throw new Error('SphericalCoordinates: some fields set to invalid numbers');
@@ -0,0 +1,127 @@
1
+ // Copyright (c) 2017 Uber Technologies, Inc.
2
+ // MIT License
3
+ import Vector from './base/vector';
4
+ import {config, isArray} from '../lib/common';
5
+ import {checkNumber} from '../lib/validators';
6
+ import * as vec2 from 'gl-matrix/vec2';
7
+ /* eslint-disable camelcase */
8
+ import {vec2_transformMat4AsVector} from '../lib/gl-matrix-extras';
9
+ import {NumericArray} from '@math.gl/types';
10
+
11
+ /**
12
+ * Two-element vector class.
13
+ * Subclass of Array<number>
14
+ */
15
+ export default class Vector2 extends Vector {
16
+ // Creates a new, empty vec2
17
+ constructor(x: number | Readonly<NumericArray> = 0, y: number = 0) {
18
+ // PERF NOTE: initialize elements as double precision numbers
19
+ super(2); // -0, -0);
20
+ if (isArray(x) && arguments.length === 1) {
21
+ this.copy(x as Readonly<NumericArray>);
22
+ } else {
23
+ if (config.debug) {
24
+ checkNumber(x);
25
+ checkNumber(y);
26
+ }
27
+ this[0] = x as number;
28
+ this[1] = y;
29
+ }
30
+ }
31
+
32
+ set(x: number, y: number): this {
33
+ this[0] = x;
34
+ this[1] = y;
35
+ return this.check();
36
+ }
37
+
38
+ copy(array: Readonly<NumericArray>): this {
39
+ this[0] = array[0];
40
+ this[1] = array[1];
41
+ return this.check();
42
+ }
43
+
44
+ fromObject(object: {x: number; y: number}): this {
45
+ if (config.debug) {
46
+ checkNumber(object.x);
47
+ checkNumber(object.y);
48
+ }
49
+ this[0] = object.x;
50
+ this[1] = object.y;
51
+ return this.check();
52
+ }
53
+
54
+ toObject(object: {x?: number; y?: number}): {x: number; y: number} {
55
+ object.x = this[0];
56
+ object.y = this[1];
57
+ return object as {x: number; y: number};
58
+ }
59
+
60
+ // Getters/setters
61
+
62
+ get ELEMENTS(): number {
63
+ return 2;
64
+ }
65
+
66
+ /**
67
+ * Returns angle from x axis
68
+ * @returns
69
+ */
70
+ horizontalAngle(): number {
71
+ return Math.atan2(this.y, this.x);
72
+ }
73
+
74
+ /**
75
+ * Returns angle from y axis
76
+ * @returns
77
+ */
78
+ verticalAngle(): number {
79
+ return Math.atan2(this.x, this.y);
80
+ }
81
+
82
+ // Transforms
83
+
84
+ /**
85
+ * Transforms as point
86
+ * @param matrix4
87
+ * @returns
88
+ */
89
+ transform(matrix4: Readonly<NumericArray>): this {
90
+ return this.transformAsPoint(matrix4);
91
+ }
92
+
93
+ /**
94
+ * transforms as point (4th component is implicitly 1)
95
+ * @param matrix4
96
+ * @returns
97
+ */
98
+ transformAsPoint(matrix4: Readonly<NumericArray>): this {
99
+ vec2.transformMat4(this, this, matrix4);
100
+ return this.check();
101
+ }
102
+
103
+ /**
104
+ * transforms as vector (4th component is implicitly 0, ignores translation. slightly faster)
105
+ * @param matrix4
106
+ * @returns
107
+ */
108
+ transformAsVector(matrix4: Readonly<NumericArray>): this {
109
+ vec2_transformMat4AsVector(this, this, matrix4);
110
+ return this.check();
111
+ }
112
+
113
+ transformByMatrix3(matrix3: Readonly<NumericArray>): this {
114
+ vec2.transformMat3(this, this, matrix3);
115
+ return this.check();
116
+ }
117
+
118
+ transformByMatrix2x3(matrix2x3: Readonly<NumericArray>): this {
119
+ vec2.transformMat2d(this, this, matrix2x3);
120
+ return this.check();
121
+ }
122
+
123
+ transformByMatrix2(matrix2: Readonly<NumericArray>): this {
124
+ vec2.transformMat2(this, this, matrix2);
125
+ return this.check();
126
+ }
127
+ }
@@ -1,51 +1,41 @@
1
1
  // Copyright (c) 2017 Uber Technologies, Inc.
2
- //
3
- // Permission is hereby granted, free of charge, to any person obtaining a copy
4
- // of this software and associated documentation files (the "Software"), to deal
5
- // in the Software without restriction, including without limitation the rights
6
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- // copies of the Software, and to permit persons to whom the Software is
8
- // furnished to do so, subject to the following conditions:
9
- //
10
- // The above copyright notice and this permission notice shall be included in
11
- // all copies or substantial portions of the Software.
12
- //
13
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- // THE SOFTWARE.
20
-
2
+ // MIT License
3
+ import {NumericArray} from '@math.gl/types';
21
4
  import Vector from './base/vector';
22
5
  import {config, isArray} from '../lib/common';
23
6
  import {checkNumber} from '../lib/validators';
24
-
25
- // @ts-ignore: error TS2307: Cannot find module 'gl-matrix/...'.
26
7
  import * as vec3 from 'gl-matrix/vec3';
27
- // eslint-disable-next-line camelcase
8
+ /* eslint-disable camelcase */
28
9
  import {vec3_transformMat2, vec3_transformMat4AsVector} from '../lib/gl-matrix-extras';
29
10
 
30
11
  const ORIGIN = [0, 0, 0];
31
- const constants = {};
32
12
 
13
+ let ZERO: Vector3;
14
+
15
+ /**
16
+ * Three-element vector class.
17
+ * Subclass of Array<number>
18
+ */
33
19
  export default class Vector3 extends Vector {
34
- static get ZERO() {
35
- return (constants.ZERO = constants.ZERO || Object.freeze(new Vector3(0, 0, 0, 0)));
20
+ static get ZERO(): Vector3 {
21
+ if (!ZERO) {
22
+ ZERO = new Vector3(0, 0, 0);
23
+ Object.freeze(ZERO);
24
+ }
25
+ return ZERO;
36
26
  }
37
27
 
38
28
  /**
39
29
  * @class
40
- * @param {Number | [Number, Number, Number]} x
41
- * @param {Number} y - rotation around X (latitude)
42
- * @param {Number} z - rotation around X (latitude)
30
+ * @param x
31
+ * @param y
32
+ * @param z
43
33
  */
44
- constructor(x = 0, y = 0, z = 0) {
34
+ constructor(x: number | Readonly<NumericArray> = 0, y: number = 0, z: number = 0) {
45
35
  // PERF NOTE: initialize elements as double precision numbers
46
36
  super(-0, -0, -0);
47
37
  if (arguments.length === 1 && isArray(x)) {
48
- this.copy(x);
38
+ this.copy(x as NumericArray);
49
39
  } else {
50
40
  // this.set(x, y, z);
51
41
  if (config.debug) {
@@ -53,28 +43,28 @@ export default class Vector3 extends Vector {
53
43
  checkNumber(y);
54
44
  checkNumber(z);
55
45
  }
56
- // @ts-ignore TS2412: Property '0' of type 'number | [number, number, number]' is not assignable to numeric index type 'number'
46
+ // @ts-expect-error TS2412: Property '0' of type 'number | [number, number, number]' is not assignable to numeric index type 'number'
57
47
  this[0] = x;
58
48
  this[1] = y;
59
49
  this[2] = z;
60
50
  }
61
51
  }
62
52
 
63
- set(x, y, z) {
53
+ set(x: number, y: number, z: number): this {
64
54
  this[0] = x;
65
55
  this[1] = y;
66
56
  this[2] = z;
67
57
  return this.check();
68
58
  }
69
59
 
70
- copy(array) {
60
+ copy(array: Readonly<NumericArray>): this {
71
61
  this[0] = array[0];
72
62
  this[1] = array[1];
73
63
  this[2] = array[2];
74
64
  return this.check();
75
65
  }
76
66
 
77
- fromObject(object) {
67
+ fromObject(object: {x: number; y: number; z: number}): this {
78
68
  if (config.debug) {
79
69
  checkNumber(object.x);
80
70
  checkNumber(object.y);
@@ -86,51 +76,49 @@ export default class Vector3 extends Vector {
86
76
  return this.check();
87
77
  }
88
78
 
89
- toObject(object) {
79
+ toObject(object: {x?: number; y?: number; z?: number}): {x: number; y: number; z: number} {
90
80
  object.x = this[0];
91
81
  object.y = this[1];
92
82
  object.z = this[2];
93
- return object;
83
+ return object as {x: number; y: number; z: number};
94
84
  }
95
85
 
96
86
  // Getters/setters
97
- /* eslint-disable no-multi-spaces, brace-style, no-return-assign */
98
- get ELEMENTS() {
87
+
88
+ get ELEMENTS(): number {
99
89
  return 3;
100
90
  }
101
-
102
- // x,y inherited from Vector
103
-
104
- get z() {
91
+ get z(): number {
105
92
  return this[2];
106
93
  }
107
- set z(value) {
94
+ set z(value: number) {
108
95
  this[2] = checkNumber(value);
109
96
  }
110
- /* eslint-enable no-multi-spaces, brace-style, no-return-assign */
111
97
 
112
- angle(vector) {
98
+ // ACCESSORS
99
+
100
+ angle(vector: Readonly<NumericArray>): number {
113
101
  return vec3.angle(this, vector);
114
102
  }
115
103
 
116
104
  // MODIFIERS
117
105
 
118
- cross(vector) {
106
+ cross(vector: Readonly<NumericArray>): this {
119
107
  vec3.cross(this, this, vector);
120
108
  return this.check();
121
109
  }
122
110
 
123
- rotateX({radians, origin = ORIGIN}) {
111
+ rotateX({radians, origin = ORIGIN}: {radians: number; origin?: Readonly<NumericArray>}): this {
124
112
  vec3.rotateX(this, this, origin, radians);
125
113
  return this.check();
126
114
  }
127
115
 
128
- rotateY({radians, origin = ORIGIN}) {
116
+ rotateY({radians, origin = ORIGIN}: {radians: number; origin?: Readonly<NumericArray>}): this {
129
117
  vec3.rotateY(this, this, origin, radians);
130
118
  return this.check();
131
119
  }
132
120
 
133
- rotateZ({radians, origin = ORIGIN}) {
121
+ rotateZ({radians, origin = ORIGIN}: {radians: number; origin?: Readonly<NumericArray>}): this {
134
122
  vec3.rotateZ(this, this, origin, radians);
135
123
  return this.check();
136
124
  }
@@ -138,33 +126,33 @@ export default class Vector3 extends Vector {
138
126
  // Transforms
139
127
 
140
128
  // transforms as point (4th component is implicitly 1)
141
- transform(matrix4) {
129
+ transform(matrix4: Readonly<NumericArray>): this {
142
130
  return this.transformAsPoint(matrix4);
143
131
  }
144
132
 
145
133
  // transforms as point (4th component is implicitly 1)
146
- transformAsPoint(matrix4) {
134
+ transformAsPoint(matrix4: Readonly<NumericArray>): this {
147
135
  vec3.transformMat4(this, this, matrix4);
148
136
  return this.check();
149
137
  }
150
138
 
151
139
  // transforms as vector (4th component is implicitly 0, ignores translation. slightly faster)
152
- transformAsVector(matrix4) {
140
+ transformAsVector(matrix4: Readonly<NumericArray>): this {
153
141
  vec3_transformMat4AsVector(this, this, matrix4);
154
142
  return this.check();
155
143
  }
156
144
 
157
- transformByMatrix3(matrix3) {
145
+ transformByMatrix3(matrix3: Readonly<NumericArray>): this {
158
146
  vec3.transformMat3(this, this, matrix3);
159
147
  return this.check();
160
148
  }
161
149
 
162
- transformByMatrix2(matrix2) {
150
+ transformByMatrix2(matrix2: Readonly<NumericArray>): this {
163
151
  vec3_transformMat2(this, this, matrix2);
164
152
  return this.check();
165
153
  }
166
154
 
167
- transformByQuaternion(quaternion) {
155
+ transformByQuaternion(quaternion: Readonly<NumericArray>): this {
168
156
  vec3.transformQuat(this, this, quaternion);
169
157
  return this.check();
170
158
  }