@math.gl/core 3.6.1 → 3.6.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.
@@ -1,540 +0,0 @@
1
- // Copyright (c) 2017 Uber Technologies, Inc.
2
- // MIT License
3
- import Matrix from './base/matrix';
4
- import { checkVector } from '../lib/validators';
5
- /* eslint-disable camelcase */
6
- import { vec2_transformMat4AsVector, vec3_transformMat4AsVector } from '../lib/gl-matrix-extras';
7
- import * as mat4 from 'gl-matrix/mat4';
8
- import * as vec2 from 'gl-matrix/vec2';
9
- import * as vec3 from 'gl-matrix/vec3';
10
- import * as vec4 from 'gl-matrix/vec4';
11
- var INDICES;
12
- (function (INDICES) {
13
- INDICES[INDICES["COL0ROW0"] = 0] = "COL0ROW0";
14
- INDICES[INDICES["COL0ROW1"] = 1] = "COL0ROW1";
15
- INDICES[INDICES["COL0ROW2"] = 2] = "COL0ROW2";
16
- INDICES[INDICES["COL0ROW3"] = 3] = "COL0ROW3";
17
- INDICES[INDICES["COL1ROW0"] = 4] = "COL1ROW0";
18
- INDICES[INDICES["COL1ROW1"] = 5] = "COL1ROW1";
19
- INDICES[INDICES["COL1ROW2"] = 6] = "COL1ROW2";
20
- INDICES[INDICES["COL1ROW3"] = 7] = "COL1ROW3";
21
- INDICES[INDICES["COL2ROW0"] = 8] = "COL2ROW0";
22
- INDICES[INDICES["COL2ROW1"] = 9] = "COL2ROW1";
23
- INDICES[INDICES["COL2ROW2"] = 10] = "COL2ROW2";
24
- INDICES[INDICES["COL2ROW3"] = 11] = "COL2ROW3";
25
- INDICES[INDICES["COL3ROW0"] = 12] = "COL3ROW0";
26
- INDICES[INDICES["COL3ROW1"] = 13] = "COL3ROW1";
27
- INDICES[INDICES["COL3ROW2"] = 14] = "COL3ROW2";
28
- INDICES[INDICES["COL3ROW3"] = 15] = "COL3ROW3";
29
- })(INDICES || (INDICES = {}));
30
- const DEFAULT_FOVY = (45 * Math.PI) / 180;
31
- const DEFAULT_ASPECT = 1;
32
- const DEFAULT_NEAR = 0.1;
33
- const DEFAULT_FAR = 500;
34
- const IDENTITY_MATRIX = Object.freeze([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
35
- /** 4x4 matrix */
36
- export default class Matrix4 extends Matrix {
37
- static get IDENTITY() {
38
- return getIdentityMatrix();
39
- }
40
- static get ZERO() {
41
- return getZeroMatrix();
42
- }
43
- get ELEMENTS() {
44
- return 16;
45
- }
46
- get RANK() {
47
- return 4;
48
- }
49
- get INDICES() {
50
- return INDICES;
51
- }
52
- constructor(array) {
53
- // PERF NOTE: initialize elements as double precision numbers
54
- super(-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0);
55
- if (arguments.length === 1 && Array.isArray(array)) {
56
- this.copy(array);
57
- }
58
- else {
59
- this.identity();
60
- }
61
- }
62
- copy(array) {
63
- this[0] = array[0];
64
- this[1] = array[1];
65
- this[2] = array[2];
66
- this[3] = array[3];
67
- this[4] = array[4];
68
- this[5] = array[5];
69
- this[6] = array[6];
70
- this[7] = array[7];
71
- this[8] = array[8];
72
- this[9] = array[9];
73
- this[10] = array[10];
74
- this[11] = array[11];
75
- this[12] = array[12];
76
- this[13] = array[13];
77
- this[14] = array[14];
78
- this[15] = array[15];
79
- return this.check();
80
- }
81
- // eslint-disable-next-line max-params
82
- set(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33) {
83
- this[0] = m00;
84
- this[1] = m10;
85
- this[2] = m20;
86
- this[3] = m30;
87
- this[4] = m01;
88
- this[5] = m11;
89
- this[6] = m21;
90
- this[7] = m31;
91
- this[8] = m02;
92
- this[9] = m12;
93
- this[10] = m22;
94
- this[11] = m32;
95
- this[12] = m03;
96
- this[13] = m13;
97
- this[14] = m23;
98
- this[15] = m33;
99
- return this.check();
100
- }
101
- // accepts row major order, stores as column major
102
- // eslint-disable-next-line max-params
103
- setRowMajor(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
104
- this[0] = m00;
105
- this[1] = m10;
106
- this[2] = m20;
107
- this[3] = m30;
108
- this[4] = m01;
109
- this[5] = m11;
110
- this[6] = m21;
111
- this[7] = m31;
112
- this[8] = m02;
113
- this[9] = m12;
114
- this[10] = m22;
115
- this[11] = m32;
116
- this[12] = m03;
117
- this[13] = m13;
118
- this[14] = m23;
119
- this[15] = m33;
120
- return this.check();
121
- }
122
- toRowMajor(result) {
123
- result[0] = this[0];
124
- result[1] = this[4];
125
- result[2] = this[8];
126
- result[3] = this[12];
127
- result[4] = this[1];
128
- result[5] = this[5];
129
- result[6] = this[9];
130
- result[7] = this[13];
131
- result[8] = this[2];
132
- result[9] = this[6];
133
- result[10] = this[10];
134
- result[11] = this[14];
135
- result[12] = this[3];
136
- result[13] = this[7];
137
- result[14] = this[11];
138
- result[15] = this[15];
139
- return result;
140
- }
141
- // Constructors
142
- /** Set to identity matrix */
143
- identity() {
144
- return this.copy(IDENTITY_MATRIX);
145
- }
146
- /**
147
- *
148
- * @param object
149
- * @returns self
150
- */
151
- fromObject(object) {
152
- return this.check();
153
- }
154
- /**
155
- * Calculates a 4x4 matrix from the given quaternion
156
- * @param quaternion Quaternion to create matrix from
157
- * @returns self
158
- */
159
- fromQuaternion(quaternion) {
160
- mat4.fromQuat(this, quaternion);
161
- return this.check();
162
- }
163
- /**
164
- * Generates a frustum matrix with the given bounds
165
- * @param view.left - Left bound of the frustum
166
- * @param view.right - Right bound of the frustum
167
- * @param view.bottom - Bottom bound of the frustum
168
- * @param view.top - Top bound of the frustum
169
- * @param view.near - Near bound of the frustum
170
- * @param view.far - Far bound of the frustum. Can be set to Infinity.
171
- * @returns self
172
- */
173
- frustum(view) {
174
- const { left, right, bottom, top, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view;
175
- if (far === Infinity) {
176
- computeInfinitePerspectiveOffCenter(this, left, right, bottom, top, near);
177
- }
178
- else {
179
- mat4.frustum(this, left, right, bottom, top, near, far);
180
- }
181
- return this.check();
182
- }
183
- /**
184
- * Generates a look-at matrix with the given eye position, focal point,
185
- * and up axis
186
- * @param view.eye - (vector) Position of the viewer
187
- * @param view.center - (vector) Point the viewer is looking at
188
- * @param view.up - (vector) Up axis
189
- * @returns self
190
- */
191
- lookAt(view) {
192
- const { eye, center = [0, 0, 0], up = [0, 1, 0] } = view;
193
- mat4.lookAt(this, eye, center, up);
194
- return this.check();
195
- }
196
- /**
197
- * Generates a orthogonal projection matrix with the given bounds
198
- * from "traditional" view space parameters
199
- * @param view.left - Left bound of the frustum
200
- * @param view.right number Right bound of the frustum
201
- * @param view.bottom - Bottom bound of the frustum
202
- * @param view.top number Top bound of the frustum
203
- * @param view.near - Near bound of the frustum
204
- * @param view.far number Far bound of the frustum
205
- * @returns self
206
- */
207
- ortho(view) {
208
- const { left, right, bottom, top, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view;
209
- mat4.ortho(this, left, right, bottom, top, near, far);
210
- return this.check();
211
- }
212
- /**
213
- * Generates an orthogonal projection matrix with the same parameters
214
- * as a perspective matrix (plus focalDistance)
215
- * @param view.fovy Vertical field of view in radians
216
- * @param view.aspect Aspect ratio. Typically viewport width / viewport height
217
- * @param view.focalDistance Distance in the view frustum used for extent calculations
218
- * @param view.near Near bound of the frustum
219
- * @param view.far Far bound of the frustum
220
- * @returns self
221
- */
222
- orthographic(view) {
223
- const { fovy = DEFAULT_FOVY, aspect = DEFAULT_ASPECT, focalDistance = 1, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view;
224
- checkRadians(fovy);
225
- const halfY = fovy / 2;
226
- const top = focalDistance * Math.tan(halfY); // focus_plane is the distance from the camera
227
- const right = top * aspect;
228
- return this.ortho({
229
- left: -right,
230
- right,
231
- bottom: -top,
232
- top,
233
- near,
234
- far
235
- });
236
- }
237
- /**
238
- * Generates a perspective projection matrix with the given bounds
239
- * @param view.fovy Vertical field of view in radians
240
- * @param view.aspect Aspect ratio. typically viewport width/height
241
- * @param view.near Near bound of the frustum
242
- * @param view.far Far bound of the frustum
243
- * @returns self
244
- */
245
- perspective(view) {
246
- const { fovy = (45 * Math.PI) / 180, aspect = 1, near = 0.1, far = 500 } = view;
247
- checkRadians(fovy);
248
- mat4.perspective(this, fovy, aspect, near, far);
249
- return this.check();
250
- }
251
- // Accessors
252
- determinant() {
253
- return mat4.determinant(this);
254
- }
255
- /**
256
- * Extracts the non-uniform scale assuming the matrix is an affine transformation.
257
- * The scales are the "lengths" of the column vectors in the upper-left 3x3 matrix.
258
- * @param result
259
- * @returns self
260
- */
261
- getScale(result = [-0, -0, -0]) {
262
- // explicit is faster than hypot...
263
- result[0] = Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2]);
264
- result[1] = Math.sqrt(this[4] * this[4] + this[5] * this[5] + this[6] * this[6]);
265
- result[2] = Math.sqrt(this[8] * this[8] + this[9] * this[9] + this[10] * this[10]);
266
- // result[0] = Math.hypot(this[0], this[1], this[2]);
267
- // result[1] = Math.hypot(this[4], this[5], this[6]);
268
- // result[2] = Math.hypot(this[8], this[9], this[10]);
269
- return result;
270
- }
271
- /**
272
- * Gets the translation portion, assuming the matrix is a affine transformation matrix.
273
- * @param result
274
- * @returns self
275
- */
276
- getTranslation(result = [-0, -0, -0]) {
277
- result[0] = this[12];
278
- result[1] = this[13];
279
- result[2] = this[14];
280
- return result;
281
- }
282
- /**
283
- * Gets upper left 3x3 pure rotation matrix (non-scaling), assume affine transformation matrix
284
- * @param result
285
- * @param scaleResult
286
- * @returns self
287
- */
288
- getRotation(result, scaleResult) {
289
- result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0];
290
- scaleResult = scaleResult || [-0, -0, -0];
291
- const scale = this.getScale(scaleResult);
292
- const inverseScale0 = 1 / scale[0];
293
- const inverseScale1 = 1 / scale[1];
294
- const inverseScale2 = 1 / scale[2];
295
- result[0] = this[0] * inverseScale0;
296
- result[1] = this[1] * inverseScale1;
297
- result[2] = this[2] * inverseScale2;
298
- result[3] = 0;
299
- result[4] = this[4] * inverseScale0;
300
- result[5] = this[5] * inverseScale1;
301
- result[6] = this[6] * inverseScale2;
302
- result[7] = 0;
303
- result[8] = this[8] * inverseScale0;
304
- result[9] = this[9] * inverseScale1;
305
- result[10] = this[10] * inverseScale2;
306
- result[11] = 0;
307
- result[12] = 0;
308
- result[13] = 0;
309
- result[14] = 0;
310
- result[15] = 1;
311
- return result;
312
- }
313
- /**
314
- *
315
- * @param result
316
- * @param scaleResult
317
- * @returns self
318
- */
319
- getRotationMatrix3(result, scaleResult) {
320
- result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0];
321
- scaleResult = scaleResult || [-0, -0, -0];
322
- const scale = this.getScale(scaleResult);
323
- const inverseScale0 = 1 / scale[0];
324
- const inverseScale1 = 1 / scale[1];
325
- const inverseScale2 = 1 / scale[2];
326
- result[0] = this[0] * inverseScale0;
327
- result[1] = this[1] * inverseScale1;
328
- result[2] = this[2] * inverseScale2;
329
- result[3] = this[4] * inverseScale0;
330
- result[4] = this[5] * inverseScale1;
331
- result[5] = this[6] * inverseScale2;
332
- result[6] = this[8] * inverseScale0;
333
- result[7] = this[9] * inverseScale1;
334
- result[8] = this[10] * inverseScale2;
335
- return result;
336
- }
337
- // Modifiers
338
- transpose() {
339
- mat4.transpose(this, this);
340
- return this.check();
341
- }
342
- invert() {
343
- mat4.invert(this, this);
344
- return this.check();
345
- }
346
- // Operations
347
- multiplyLeft(a) {
348
- mat4.multiply(this, a, this);
349
- return this.check();
350
- }
351
- multiplyRight(a) {
352
- mat4.multiply(this, this, a);
353
- return this.check();
354
- }
355
- // Rotates a matrix by the given angle around the X axis
356
- rotateX(radians) {
357
- mat4.rotateX(this, this, radians);
358
- // mat4.rotate(this, this, radians, [1, 0, 0]);
359
- return this.check();
360
- }
361
- // Rotates a matrix by the given angle around the Y axis.
362
- rotateY(radians) {
363
- mat4.rotateY(this, this, radians);
364
- // mat4.rotate(this, this, radians, [0, 1, 0]);
365
- return this.check();
366
- }
367
- /**
368
- * Rotates a matrix by the given angle around the Z axis.
369
- * @param radians
370
- * @returns self
371
- */
372
- rotateZ(radians) {
373
- mat4.rotateZ(this, this, radians);
374
- // mat4.rotate(this, this, radians, [0, 0, 1]);
375
- return this.check();
376
- }
377
- /**
378
- *
379
- * @param param0
380
- * @returns self
381
- */
382
- rotateXYZ(angleXYZ) {
383
- return this.rotateX(angleXYZ[0]).rotateY(angleXYZ[1]).rotateZ(angleXYZ[2]);
384
- }
385
- /**
386
- *
387
- * @param radians
388
- * @param axis
389
- * @returns self
390
- */
391
- rotateAxis(radians, axis) {
392
- mat4.rotate(this, this, radians, axis);
393
- return this.check();
394
- }
395
- /**
396
- *
397
- * @param factor
398
- * @returns self
399
- */
400
- scale(factor) {
401
- mat4.scale(this, this, Array.isArray(factor) ? factor : [factor, factor, factor]);
402
- return this.check();
403
- }
404
- /**
405
- *
406
- * @param vec
407
- * @returns self
408
- */
409
- translate(vector) {
410
- mat4.translate(this, this, vector);
411
- return this.check();
412
- }
413
- // Transforms
414
- /**
415
- * Transforms any 2, 3 or 4 element vector. 2 and 3 elements are treated as points
416
- * @param vector
417
- * @param result
418
- * @returns self
419
- */
420
- transform(vector, result) {
421
- if (vector.length === 4) {
422
- result = vec4.transformMat4(result || [-0, -0, -0, -0], vector, this);
423
- checkVector(result, 4);
424
- return result;
425
- }
426
- return this.transformAsPoint(vector, result);
427
- }
428
- /**
429
- * Transforms any 2 or 3 element array as point (w implicitly 1)
430
- * @param vector
431
- * @param result
432
- * @returns self
433
- */
434
- transformAsPoint(vector, result) {
435
- const { length } = vector;
436
- let out;
437
- switch (length) {
438
- case 2:
439
- out = vec2.transformMat4(result || [-0, -0], vector, this);
440
- break;
441
- case 3:
442
- out = vec3.transformMat4(result || [-0, -0, -0], vector, this);
443
- break;
444
- default:
445
- throw new Error('Illegal vector');
446
- }
447
- checkVector(out, vector.length);
448
- return out;
449
- }
450
- /**
451
- * Transforms any 2 or 3 element array as vector (w implicitly 0)
452
- * @param vector
453
- * @param result
454
- * @returns self
455
- */
456
- transformAsVector(vector, result) {
457
- let out;
458
- switch (vector.length) {
459
- case 2:
460
- out = vec2_transformMat4AsVector(result || [-0, -0], vector, this);
461
- break;
462
- case 3:
463
- out = vec3_transformMat4AsVector(result || [-0, -0, -0], vector, this);
464
- break;
465
- default:
466
- throw new Error('Illegal vector');
467
- }
468
- checkVector(out, vector.length);
469
- return out;
470
- }
471
- /** @deprecated */
472
- transformPoint(vector, result) {
473
- return this.transformAsPoint(vector, result);
474
- }
475
- /** @deprecated */
476
- transformVector(vector, result) {
477
- return this.transformAsPoint(vector, result);
478
- }
479
- /** @deprecated */
480
- transformDirection(vector, result) {
481
- return this.transformAsVector(vector, result);
482
- }
483
- // three.js math API compatibility
484
- makeRotationX(radians) {
485
- return this.identity().rotateX(radians);
486
- }
487
- makeTranslation(x, y, z) {
488
- return this.identity().translate([x, y, z]);
489
- }
490
- }
491
- // TODO initializing static members directly is an option, but make sure no tree-shaking issues
492
- let ZERO;
493
- let IDENTITY;
494
- function getZeroMatrix() {
495
- if (!ZERO) {
496
- ZERO = new Matrix4([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
497
- Object.freeze(ZERO);
498
- }
499
- return ZERO;
500
- }
501
- function getIdentityMatrix() {
502
- if (!IDENTITY) {
503
- IDENTITY = new Matrix4();
504
- Object.freeze(IDENTITY);
505
- }
506
- return IDENTITY;
507
- }
508
- // HELPER FUNCTIONS
509
- function checkRadians(possiblyDegrees) {
510
- if (possiblyDegrees > Math.PI * 2) {
511
- throw Error('expected radians');
512
- }
513
- }
514
- // eslint-disable-next-line max-params
515
- function computeInfinitePerspectiveOffCenter(result, left, right, bottom, top, near) {
516
- const column0Row0 = (2 * near) / (right - left);
517
- const column1Row1 = (2 * near) / (top - bottom);
518
- const column2Row0 = (right + left) / (right - left);
519
- const column2Row1 = (top + bottom) / (top - bottom);
520
- const column2Row2 = -1;
521
- const column2Row3 = -1;
522
- const column3Row2 = -2 * near;
523
- result[0] = column0Row0;
524
- result[1] = 0;
525
- result[2] = 0;
526
- result[3] = 0;
527
- result[4] = 0;
528
- result[5] = column1Row1;
529
- result[6] = 0;
530
- result[7] = 0;
531
- result[8] = column2Row0;
532
- result[9] = column2Row1;
533
- result[10] = column2Row2;
534
- result[11] = column2Row3;
535
- result[12] = 0;
536
- result[13] = 0;
537
- result[14] = column3Row2;
538
- result[15] = 0;
539
- return result;
540
- }
@@ -1,109 +0,0 @@
1
- // Copyright (c) 2017 Uber Technologies, Inc.
2
- // MIT License
3
- import Matrix4 from './matrix4';
4
- import Vector3 from './vector3';
5
- import Euler from './euler';
6
- export default class Pose {
7
- constructor({ x = 0, y = 0, z = 0, roll = 0, pitch = 0, yaw = 0, position, orientation } = {}) {
8
- if (Array.isArray(position) && position.length === 3) {
9
- this.position = new Vector3(position);
10
- }
11
- else {
12
- this.position = new Vector3(x, y, z);
13
- }
14
- if (Array.isArray(orientation) && orientation.length === 4) {
15
- // @ts-expect-error
16
- this.orientation = new Euler(orientation, orientation[3]);
17
- }
18
- else {
19
- this.orientation = new Euler(roll, pitch, yaw, Euler.RollPitchYaw);
20
- }
21
- }
22
- get x() {
23
- return this.position.x;
24
- }
25
- set x(value) {
26
- this.position.x = value;
27
- }
28
- get y() {
29
- return this.position.y;
30
- }
31
- set y(value) {
32
- this.position.y = value;
33
- }
34
- get z() {
35
- return this.position.z;
36
- }
37
- set z(value) {
38
- this.position.z = value;
39
- }
40
- get roll() {
41
- return this.orientation.roll;
42
- }
43
- set roll(value) {
44
- this.orientation.roll = value;
45
- }
46
- get pitch() {
47
- return this.orientation.pitch;
48
- }
49
- set pitch(value) {
50
- this.orientation.pitch = value;
51
- }
52
- get yaw() {
53
- return this.orientation.yaw;
54
- }
55
- set yaw(value) {
56
- this.orientation.yaw = value;
57
- }
58
- getPosition() {
59
- return this.position;
60
- }
61
- getOrientation() {
62
- return this.orientation;
63
- }
64
- equals(pose) {
65
- if (!pose) {
66
- return false;
67
- }
68
- return this.position.equals(pose.position) && this.orientation.equals(pose.orientation);
69
- }
70
- exactEquals(pose) {
71
- if (!pose) {
72
- return false;
73
- }
74
- return (this.position.exactEquals(pose.position) && this.orientation.exactEquals(pose.orientation));
75
- }
76
- getTransformationMatrix() {
77
- // setup pre computations for the sin/cos of the angles
78
- const sr = Math.sin(this.roll);
79
- const sp = Math.sin(this.pitch);
80
- const sw = Math.sin(this.yaw);
81
- const cr = Math.cos(this.roll);
82
- const cp = Math.cos(this.pitch);
83
- const cw = Math.cos(this.yaw);
84
- // Create matrix
85
- return new Matrix4().setRowMajor(cw * cp, // 0,0
86
- -sw * cr + cw * sp * sr, // 0,1
87
- sw * sr + cw * sp * cr, // 0,2
88
- this.x, // 0,3
89
- sw * cp, // 1,0
90
- cw * cr + sw * sp * sr, // 1,1
91
- -cw * sr + sw * sp * cr, // 1,2
92
- this.y, // 1,3
93
- -sp, // 2,0
94
- cp * sr, // 2,1
95
- cp * cr, // 2,2
96
- this.z, // 2,3
97
- 0, 0, 0, 1);
98
- }
99
- getTransformationMatrixFromPose(pose) {
100
- return new Matrix4()
101
- .multiplyRight(this.getTransformationMatrix())
102
- .multiplyRight(pose.getTransformationMatrix().invert());
103
- }
104
- getTransformationMatrixToPose(pose) {
105
- return new Matrix4()
106
- .multiplyRight(pose.getTransformationMatrix())
107
- .multiplyRight(this.getTransformationMatrix().invert());
108
- }
109
- }