@math.gl/core 4.0.0 → 4.0.1

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 (124) hide show
  1. package/dist/classes/base/math-array.d.ts +1 -2
  2. package/dist/classes/base/math-array.js +220 -266
  3. package/dist/classes/base/matrix.d.ts +1 -2
  4. package/dist/classes/base/matrix.js +52 -53
  5. package/dist/classes/base/vector.d.ts +1 -2
  6. package/dist/classes/base/vector.js +121 -135
  7. package/dist/classes/euler.d.ts +2 -3
  8. package/dist/classes/euler.js +462 -533
  9. package/dist/classes/matrix3.d.ts +2 -3
  10. package/dist/classes/matrix3.js +185 -190
  11. package/dist/classes/matrix4.d.ts +1 -2
  12. package/dist/classes/matrix4.js +516 -452
  13. package/dist/classes/pose.d.ts +4 -5
  14. package/dist/classes/pose.js +104 -119
  15. package/dist/classes/quaternion.d.ts +1 -2
  16. package/dist/classes/quaternion.js +257 -233
  17. package/dist/classes/spherical-coordinates.d.ts +3 -4
  18. package/dist/classes/spherical-coordinates.js +138 -147
  19. package/dist/classes/vector2.d.ts +1 -2
  20. package/dist/classes/vector2.js +101 -85
  21. package/dist/classes/vector3.d.ts +1 -2
  22. package/dist/classes/vector3.js +125 -136
  23. package/dist/classes/vector4.d.ts +2 -3
  24. package/dist/classes/vector4.js +106 -114
  25. package/dist/gl-matrix/common.d.ts +0 -1
  26. package/dist/gl-matrix/common.js +38 -6
  27. package/dist/gl-matrix/mat3.d.ts +0 -1
  28. package/dist/gl-matrix/mat3.js +699 -425
  29. package/dist/gl-matrix/mat4.d.ts +0 -1
  30. package/dist/gl-matrix/mat4.js +1879 -1339
  31. package/dist/gl-matrix/quat.d.ts +0 -1
  32. package/dist/gl-matrix/quat.js +655 -257
  33. package/dist/gl-matrix/vec2.d.ts +0 -1
  34. package/dist/gl-matrix/vec2.js +529 -212
  35. package/dist/gl-matrix/vec3.d.ts +0 -1
  36. package/dist/gl-matrix/vec3.js +703 -335
  37. package/dist/gl-matrix/vec4.d.ts +0 -1
  38. package/dist/gl-matrix/vec4.js +579 -293
  39. package/dist/gl-matrix/wip/index.d.ts +3 -4
  40. package/dist/gl-matrix/wip/index.js +2 -1
  41. package/dist/gl-matrix/wip/mat2.d.ts +0 -1
  42. package/dist/gl-matrix/wip/mat2.js +333 -157
  43. package/dist/gl-matrix/wip/mat2d.d.ts +0 -1
  44. package/dist/gl-matrix/wip/mat2d.js +377 -202
  45. package/dist/gl-matrix/wip/quat2.d.ts +0 -1
  46. package/dist/gl-matrix/wip/quat2.js +647 -409
  47. package/dist/index.cjs +60 -100
  48. package/dist/index.cjs.map +7 -0
  49. package/dist/index.d.ts +21 -22
  50. package/dist/index.js +23 -16
  51. package/dist/lib/assert.d.ts +0 -1
  52. package/dist/lib/assert.js +3 -4
  53. package/dist/lib/common.d.ts +2 -3
  54. package/dist/lib/common.js +163 -134
  55. package/dist/lib/gl-matrix-extras.d.ts +0 -1
  56. package/dist/lib/gl-matrix-extras.js +39 -36
  57. package/dist/lib/math-utils.d.ts +0 -1
  58. package/dist/lib/math-utils.js +2 -1
  59. package/dist/lib/validators.d.ts +0 -1
  60. package/dist/lib/validators.js +22 -25
  61. package/package.json +5 -6
  62. package/src/classes/matrix3.ts +1 -1
  63. package/dist/classes/base/math-array.d.ts.map +0 -1
  64. package/dist/classes/base/math-array.js.map +0 -1
  65. package/dist/classes/base/matrix.d.ts.map +0 -1
  66. package/dist/classes/base/matrix.js.map +0 -1
  67. package/dist/classes/base/vector.d.ts.map +0 -1
  68. package/dist/classes/base/vector.js.map +0 -1
  69. package/dist/classes/euler.d.ts.map +0 -1
  70. package/dist/classes/euler.js.map +0 -1
  71. package/dist/classes/matrix3.d.ts.map +0 -1
  72. package/dist/classes/matrix3.js.map +0 -1
  73. package/dist/classes/matrix4.d.ts.map +0 -1
  74. package/dist/classes/matrix4.js.map +0 -1
  75. package/dist/classes/pose.d.ts.map +0 -1
  76. package/dist/classes/pose.js.map +0 -1
  77. package/dist/classes/quaternion.d.ts.map +0 -1
  78. package/dist/classes/quaternion.js.map +0 -1
  79. package/dist/classes/spherical-coordinates.d.ts.map +0 -1
  80. package/dist/classes/spherical-coordinates.js.map +0 -1
  81. package/dist/classes/vector2.d.ts.map +0 -1
  82. package/dist/classes/vector2.js.map +0 -1
  83. package/dist/classes/vector3.d.ts.map +0 -1
  84. package/dist/classes/vector3.js.map +0 -1
  85. package/dist/classes/vector4.d.ts.map +0 -1
  86. package/dist/classes/vector4.js.map +0 -1
  87. package/dist/gl-matrix/common.d.ts.map +0 -1
  88. package/dist/gl-matrix/common.js.map +0 -1
  89. package/dist/gl-matrix/mat3.d.ts.map +0 -1
  90. package/dist/gl-matrix/mat3.js.map +0 -1
  91. package/dist/gl-matrix/mat4.d.ts.map +0 -1
  92. package/dist/gl-matrix/mat4.js.map +0 -1
  93. package/dist/gl-matrix/quat.d.ts.map +0 -1
  94. package/dist/gl-matrix/quat.js.map +0 -1
  95. package/dist/gl-matrix/vec2.d.ts.map +0 -1
  96. package/dist/gl-matrix/vec2.js.map +0 -1
  97. package/dist/gl-matrix/vec3.d.ts.map +0 -1
  98. package/dist/gl-matrix/vec3.js.map +0 -1
  99. package/dist/gl-matrix/vec4.d.ts.map +0 -1
  100. package/dist/gl-matrix/vec4.js.map +0 -1
  101. package/dist/gl-matrix/wip/index.d.ts.map +0 -1
  102. package/dist/gl-matrix/wip/index.js.map +0 -1
  103. package/dist/gl-matrix/wip/mat2.d.ts.map +0 -1
  104. package/dist/gl-matrix/wip/mat2.js.map +0 -1
  105. package/dist/gl-matrix/wip/mat2d.d.ts.map +0 -1
  106. package/dist/gl-matrix/wip/mat2d.js.map +0 -1
  107. package/dist/gl-matrix/wip/quat2.d.ts.map +0 -1
  108. package/dist/gl-matrix/wip/quat2.js.map +0 -1
  109. package/dist/gl-matrix/wip/types.d.js +0 -2
  110. package/dist/gl-matrix/wip/types.d.js.map +0 -1
  111. package/dist/index.d.ts.map +0 -1
  112. package/dist/index.js.map +0 -1
  113. package/dist/lib/assert.d.ts.map +0 -1
  114. package/dist/lib/assert.js.map +0 -1
  115. package/dist/lib/common.d.ts.map +0 -1
  116. package/dist/lib/common.js.map +0 -1
  117. package/dist/lib/gl-matrix-extras.d.ts.map +0 -1
  118. package/dist/lib/gl-matrix-extras.js.map +0 -1
  119. package/dist/lib/gl-matrix.d.js +0 -2
  120. package/dist/lib/gl-matrix.d.js.map +0 -1
  121. package/dist/lib/math-utils.d.ts.map +0 -1
  122. package/dist/lib/math-utils.js.map +0 -1
  123. package/dist/lib/validators.d.ts.map +0 -1
  124. package/dist/lib/validators.js.map +0 -1
@@ -1,315 +1,713 @@
1
+ // @eslint-disable
2
+ // @ts-nocheck
1
3
  import * as glMatrix from './common.js';
2
4
  import * as mat3 from './mat3.js';
3
5
  import * as vec3 from './vec3.js';
4
6
  import * as vec4 from './vec4.js';
7
+ // const glMatrix: {EPSILON = 0.000001};
8
+ /**
9
+ * Quaternion in the format XYZW
10
+ * @module quat
11
+ */
12
+ /**
13
+ * Creates a new identity quat
14
+ *
15
+ * @returns {quat} a new quaternion
16
+ */
5
17
  export function create() {
6
- const out = new glMatrix.ARRAY_TYPE(4);
7
-
8
- if (glMatrix.ARRAY_TYPE != Float32Array) {
18
+ const out = new glMatrix.ARRAY_TYPE(4);
19
+ if (glMatrix.ARRAY_TYPE != Float32Array) {
20
+ out[0] = 0;
21
+ out[1] = 0;
22
+ out[2] = 0;
23
+ }
24
+ out[3] = 1;
25
+ return out;
26
+ }
27
+ /**
28
+ * Set a quat to the identity quaternion
29
+ *
30
+ * @param {quat} out the receiving quaternion
31
+ * @returns {quat} out
32
+ */
33
+ export function identity(out) {
9
34
  out[0] = 0;
10
35
  out[1] = 0;
11
36
  out[2] = 0;
12
- }
13
-
14
- out[3] = 1;
15
- return out;
16
- }
17
- export function identity(out) {
18
- out[0] = 0;
19
- out[1] = 0;
20
- out[2] = 0;
21
- out[3] = 1;
22
- return out;
37
+ out[3] = 1;
38
+ return out;
23
39
  }
40
+ /**
41
+ * Sets a quat from the given angle and rotation axis,
42
+ * then returns it.
43
+ *
44
+ * @param {quat} out the receiving quaternion
45
+ * @param {ReadonlyVec3} axis the axis around which to rotate
46
+ * @param {Number} rad the angle in radians
47
+ * @returns {quat} out
48
+ **/
24
49
  export function setAxisAngle(out, axis, rad) {
25
- rad = rad * 0.5;
26
- const s = Math.sin(rad);
27
- out[0] = s * axis[0];
28
- out[1] = s * axis[1];
29
- out[2] = s * axis[2];
30
- out[3] = Math.cos(rad);
31
- return out;
50
+ rad = rad * 0.5;
51
+ const s = Math.sin(rad);
52
+ out[0] = s * axis[0];
53
+ out[1] = s * axis[1];
54
+ out[2] = s * axis[2];
55
+ out[3] = Math.cos(rad);
56
+ return out;
32
57
  }
58
+ /**
59
+ * Gets the rotation axis and angle for a given
60
+ * quaternion. If a quaternion is created with
61
+ * setAxisAngle, this method will return the same
62
+ * values as providied in the original parameter list
63
+ * OR functionally equivalent values.
64
+ * Example: The quaternion formed by axis [0, 0, 1] and
65
+ * angle -90 is the same as the quaternion formed by
66
+ * [0, 0, 1] and 270. This method favors the latter.
67
+ * @param {vec3} out_axis Vector receiving the axis of rotation
68
+ * @param {ReadonlyQuat} q Quaternion to be decomposed
69
+ * @return {Number} Angle, in radians, of the rotation
70
+ */
33
71
  export function getAxisAngle(out_axis, q) {
34
- const rad = Math.acos(q[3]) * 2.0;
35
- const s = Math.sin(rad / 2.0);
36
-
37
- if (s > glMatrix.EPSILON) {
38
- out_axis[0] = q[0] / s;
39
- out_axis[1] = q[1] / s;
40
- out_axis[2] = q[2] / s;
41
- } else {
42
- out_axis[0] = 1;
43
- out_axis[1] = 0;
44
- out_axis[2] = 0;
45
- }
46
-
47
- return rad;
72
+ const rad = Math.acos(q[3]) * 2.0;
73
+ const s = Math.sin(rad / 2.0);
74
+ if (s > glMatrix.EPSILON) {
75
+ out_axis[0] = q[0] / s;
76
+ out_axis[1] = q[1] / s;
77
+ out_axis[2] = q[2] / s;
78
+ }
79
+ else {
80
+ // If s is zero, return any axis (no rotation - axis does not matter)
81
+ out_axis[0] = 1;
82
+ out_axis[1] = 0;
83
+ out_axis[2] = 0;
84
+ }
85
+ return rad;
48
86
  }
87
+ /**
88
+ * Gets the angular distance between two unit quaternions
89
+ *
90
+ * @param {ReadonlyQuat} a Origin unit quaternion
91
+ * @param {ReadonlyQuat} b Destination unit quaternion
92
+ * @return {Number} Angle, in radians, between the two quaternions
93
+ */
49
94
  export function getAngle(a, b) {
50
- const dotproduct = dot(a, b);
51
- return Math.acos(2 * dotproduct * dotproduct - 1);
95
+ const dotproduct = dot(a, b);
96
+ return Math.acos(2 * dotproduct * dotproduct - 1);
52
97
  }
98
+ /**
99
+ * Multiplies two quat's
100
+ *
101
+ * @param {quat} out the receiving quaternion
102
+ * @param {ReadonlyQuat} a the first operand
103
+ * @param {ReadonlyQuat} b the second operand
104
+ * @returns {quat} out
105
+ */
53
106
  export function multiply(out, a, b) {
54
- const ax = a[0];
55
- const ay = a[1];
56
- const az = a[2];
57
- const aw = a[3];
58
- const bx = b[0];
59
- const by = b[1];
60
- const bz = b[2];
61
- const bw = b[3];
62
- out[0] = ax * bw + aw * bx + ay * bz - az * by;
63
- out[1] = ay * bw + aw * by + az * bx - ax * bz;
64
- out[2] = az * bw + aw * bz + ax * by - ay * bx;
65
- out[3] = aw * bw - ax * bx - ay * by - az * bz;
66
- return out;
107
+ const ax = a[0];
108
+ const ay = a[1];
109
+ const az = a[2];
110
+ const aw = a[3];
111
+ const bx = b[0];
112
+ const by = b[1];
113
+ const bz = b[2];
114
+ const bw = b[3];
115
+ out[0] = ax * bw + aw * bx + ay * bz - az * by;
116
+ out[1] = ay * bw + aw * by + az * bx - ax * bz;
117
+ out[2] = az * bw + aw * bz + ax * by - ay * bx;
118
+ out[3] = aw * bw - ax * bx - ay * by - az * bz;
119
+ return out;
67
120
  }
121
+ /**
122
+ * Rotates a quaternion by the given angle about the X axis
123
+ *
124
+ * @param {quat} out quat receiving operation result
125
+ * @param {ReadonlyQuat} a quat to rotate
126
+ * @param {number} rad angle (in radians) to rotate
127
+ * @returns {quat} out
128
+ */
68
129
  export function rotateX(out, a, rad) {
69
- rad *= 0.5;
70
- const ax = a[0];
71
- const ay = a[1];
72
- const az = a[2];
73
- const aw = a[3];
74
- const bx = Math.sin(rad);
75
- const bw = Math.cos(rad);
76
- out[0] = ax * bw + aw * bx;
77
- out[1] = ay * bw + az * bx;
78
- out[2] = az * bw - ay * bx;
79
- out[3] = aw * bw - ax * bx;
80
- return out;
130
+ rad *= 0.5;
131
+ const ax = a[0];
132
+ const ay = a[1];
133
+ const az = a[2];
134
+ const aw = a[3];
135
+ const bx = Math.sin(rad);
136
+ const bw = Math.cos(rad);
137
+ out[0] = ax * bw + aw * bx;
138
+ out[1] = ay * bw + az * bx;
139
+ out[2] = az * bw - ay * bx;
140
+ out[3] = aw * bw - ax * bx;
141
+ return out;
81
142
  }
143
+ /**
144
+ * Rotates a quaternion by the given angle about the Y axis
145
+ *
146
+ * @param {quat} out quat receiving operation result
147
+ * @param {ReadonlyQuat} a quat to rotate
148
+ * @param {number} rad angle (in radians) to rotate
149
+ * @returns {quat} out
150
+ */
82
151
  export function rotateY(out, a, rad) {
83
- rad *= 0.5;
84
- const ax = a[0];
85
- const ay = a[1];
86
- const az = a[2];
87
- const aw = a[3];
88
- const by = Math.sin(rad);
89
- const bw = Math.cos(rad);
90
- out[0] = ax * bw - az * by;
91
- out[1] = ay * bw + aw * by;
92
- out[2] = az * bw + ax * by;
93
- out[3] = aw * bw - ay * by;
94
- return out;
152
+ rad *= 0.5;
153
+ const ax = a[0];
154
+ const ay = a[1];
155
+ const az = a[2];
156
+ const aw = a[3];
157
+ const by = Math.sin(rad);
158
+ const bw = Math.cos(rad);
159
+ out[0] = ax * bw - az * by;
160
+ out[1] = ay * bw + aw * by;
161
+ out[2] = az * bw + ax * by;
162
+ out[3] = aw * bw - ay * by;
163
+ return out;
95
164
  }
165
+ /**
166
+ * Rotates a quaternion by the given angle about the Z axis
167
+ *
168
+ * @param {quat} out quat receiving operation result
169
+ * @param {ReadonlyQuat} a quat to rotate
170
+ * @param {number} rad angle (in radians) to rotate
171
+ * @returns {quat} out
172
+ */
96
173
  export function rotateZ(out, a, rad) {
97
- rad *= 0.5;
98
- const ax = a[0];
99
- const ay = a[1];
100
- const az = a[2];
101
- const aw = a[3];
102
- const bz = Math.sin(rad);
103
- const bw = Math.cos(rad);
104
- out[0] = ax * bw + ay * bz;
105
- out[1] = ay * bw - ax * bz;
106
- out[2] = az * bw + aw * bz;
107
- out[3] = aw * bw - az * bz;
108
- return out;
174
+ rad *= 0.5;
175
+ const ax = a[0];
176
+ const ay = a[1];
177
+ const az = a[2];
178
+ const aw = a[3];
179
+ const bz = Math.sin(rad);
180
+ const bw = Math.cos(rad);
181
+ out[0] = ax * bw + ay * bz;
182
+ out[1] = ay * bw - ax * bz;
183
+ out[2] = az * bw + aw * bz;
184
+ out[3] = aw * bw - az * bz;
185
+ return out;
109
186
  }
187
+ /**
188
+ * Calculates the W component of a quat from the X, Y, and Z components.
189
+ * Assumes that quaternion is 1 unit in length.
190
+ * Any existing W component will be ignored.
191
+ *
192
+ * @param {quat} out the receiving quaternion
193
+ * @param {ReadonlyQuat} a quat to calculate W component of
194
+ * @returns {quat} out
195
+ */
110
196
  export function calculateW(out, a) {
111
- const x = a[0];
112
- const y = a[1];
113
- const z = a[2];
114
- out[0] = x;
115
- out[1] = y;
116
- out[2] = z;
117
- out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
118
- return out;
197
+ const x = a[0];
198
+ const y = a[1];
199
+ const z = a[2];
200
+ out[0] = x;
201
+ out[1] = y;
202
+ out[2] = z;
203
+ out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
204
+ return out;
119
205
  }
206
+ /**
207
+ * Calculate the exponential of a unit quaternion.
208
+ *
209
+ * @param {quat} out the receiving quaternion
210
+ * @param {ReadonlyQuat} a quat to calculate the exponential of
211
+ * @returns {quat} out
212
+ */
120
213
  export function exp(out, a) {
121
- const x = a[0];
122
- const y = a[1];
123
- const z = a[2];
124
- const w = a[3];
125
- const r = Math.sqrt(x * x + y * y + z * z);
126
- const et = Math.exp(w);
127
- const s = r > 0 ? et * Math.sin(r) / r : 0;
128
- out[0] = x * s;
129
- out[1] = y * s;
130
- out[2] = z * s;
131
- out[3] = et * Math.cos(r);
132
- return out;
214
+ const x = a[0];
215
+ const y = a[1];
216
+ const z = a[2];
217
+ const w = a[3];
218
+ const r = Math.sqrt(x * x + y * y + z * z);
219
+ const et = Math.exp(w);
220
+ const s = r > 0 ? (et * Math.sin(r)) / r : 0;
221
+ out[0] = x * s;
222
+ out[1] = y * s;
223
+ out[2] = z * s;
224
+ out[3] = et * Math.cos(r);
225
+ return out;
133
226
  }
227
+ /**
228
+ * Calculate the natural logarithm of a unit quaternion.
229
+ *
230
+ * @param {quat} out the receiving quaternion
231
+ * @param {ReadonlyQuat} a quat to calculate the exponential of
232
+ * @returns {quat} out
233
+ */
134
234
  export function ln(out, a) {
135
- const x = a[0];
136
- const y = a[1];
137
- const z = a[2];
138
- const w = a[3];
139
- const r = Math.sqrt(x * x + y * y + z * z);
140
- const t = r > 0 ? Math.atan2(r, w) / r : 0;
141
- out[0] = x * t;
142
- out[1] = y * t;
143
- out[2] = z * t;
144
- out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
145
- return out;
235
+ const x = a[0];
236
+ const y = a[1];
237
+ const z = a[2];
238
+ const w = a[3];
239
+ const r = Math.sqrt(x * x + y * y + z * z);
240
+ const t = r > 0 ? Math.atan2(r, w) / r : 0;
241
+ out[0] = x * t;
242
+ out[1] = y * t;
243
+ out[2] = z * t;
244
+ out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
245
+ return out;
146
246
  }
247
+ /**
248
+ * Calculate the scalar power of a unit quaternion.
249
+ *
250
+ * @param {quat} out the receiving quaternion
251
+ * @param {ReadonlyQuat} a quat to calculate the exponential of
252
+ * @param {Number} b amount to scale the quaternion by
253
+ * @returns {quat} out
254
+ */
147
255
  export function pow(out, a, b) {
148
- ln(out, a);
149
- scale(out, out, b);
150
- exp(out, out);
151
- return out;
256
+ ln(out, a);
257
+ scale(out, out, b);
258
+ exp(out, out);
259
+ return out;
152
260
  }
261
+ /**
262
+ * Performs a spherical linear interpolation between two quat
263
+ *
264
+ * @param {quat} out the receiving quaternion
265
+ * @param {ReadonlyQuat} a the first operand
266
+ * @param {ReadonlyQuat} b the second operand
267
+ * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
268
+ * @returns {quat} out
269
+ */
153
270
  export function slerp(out, a, b, t) {
154
- const ax = a[0];
155
- const ay = a[1];
156
- const az = a[2];
157
- const aw = a[3];
158
- let bx = b[0];
159
- let by = b[1];
160
- let bz = b[2];
161
- let bw = b[3];
162
- let cosom;
163
- let omega;
164
- let scale0;
165
- let scale1;
166
- let sinom;
167
- cosom = ax * bx + ay * by + az * bz + aw * bw;
168
-
169
- if (cosom < 0.0) {
170
- cosom = -cosom;
171
- bx = -bx;
172
- by = -by;
173
- bz = -bz;
174
- bw = -bw;
175
- }
176
-
177
- if (1.0 - cosom > glMatrix.EPSILON) {
178
- omega = Math.acos(cosom);
179
- sinom = Math.sin(omega);
180
- scale0 = Math.sin((1.0 - t) * omega) / sinom;
181
- scale1 = Math.sin(t * omega) / sinom;
182
- } else {
183
- scale0 = 1.0 - t;
184
- scale1 = t;
185
- }
186
-
187
- out[0] = scale0 * ax + scale1 * bx;
188
- out[1] = scale0 * ay + scale1 * by;
189
- out[2] = scale0 * az + scale1 * bz;
190
- out[3] = scale0 * aw + scale1 * bw;
191
- return out;
271
+ // benchmarks:
272
+ // http://jsperf.com/quaternion-slerp-implementations
273
+ const ax = a[0];
274
+ const ay = a[1];
275
+ const az = a[2];
276
+ const aw = a[3];
277
+ let bx = b[0];
278
+ let by = b[1];
279
+ let bz = b[2];
280
+ let bw = b[3];
281
+ let cosom;
282
+ let omega;
283
+ let scale0;
284
+ let scale1;
285
+ let sinom;
286
+ // calc cosine
287
+ cosom = ax * bx + ay * by + az * bz + aw * bw;
288
+ // adjust signs (if necessary)
289
+ if (cosom < 0.0) {
290
+ cosom = -cosom;
291
+ bx = -bx;
292
+ by = -by;
293
+ bz = -bz;
294
+ bw = -bw;
295
+ }
296
+ // calculate coefficients
297
+ if (1.0 - cosom > glMatrix.EPSILON) {
298
+ // standard case (slerp)
299
+ omega = Math.acos(cosom);
300
+ sinom = Math.sin(omega);
301
+ scale0 = Math.sin((1.0 - t) * omega) / sinom;
302
+ scale1 = Math.sin(t * omega) / sinom;
303
+ }
304
+ else {
305
+ // "from" and "to" quaternions are very close
306
+ // ... so we can do a linear interpolation
307
+ scale0 = 1.0 - t;
308
+ scale1 = t;
309
+ }
310
+ // calculate final values
311
+ out[0] = scale0 * ax + scale1 * bx;
312
+ out[1] = scale0 * ay + scale1 * by;
313
+ out[2] = scale0 * az + scale1 * bz;
314
+ out[3] = scale0 * aw + scale1 * bw;
315
+ return out;
192
316
  }
317
+ /**
318
+ * Generates a random unit quaternion
319
+ *
320
+ * @param {quat} out the receiving quaternion
321
+ * @returns {quat} out
322
+ */
323
+ // export function random(out) {
324
+ // // Implementation of http://planning.cs.uiuc.edu/node198.html
325
+ // // TODO: Calling random 3 times is probably not the fastest solution
326
+ // let u1 = glMatrix.RANDOM();
327
+ // let u2 = glMatrix.RANDOM();
328
+ // let u3 = glMatrix.RANDOM();
329
+ // let sqrt1MinusU1 = Math.sqrt(1 - u1);
330
+ // let sqrtU1 = Math.sqrt(u1);
331
+ // out[0] = sqrt1MinusU1 * Math.sin(2.0 * Math.PI * u2);
332
+ // out[1] = sqrt1MinusU1 * Math.cos(2.0 * Math.PI * u2);
333
+ // out[2] = sqrtU1 * Math.sin(2.0 * Math.PI * u3);
334
+ // out[3] = sqrtU1 * Math.cos(2.0 * Math.PI * u3);
335
+ // return out;
336
+ // }
337
+ /**
338
+ * Calculates the inverse of a quat
339
+ *
340
+ * @param {quat} out the receiving quaternion
341
+ * @param {ReadonlyQuat} a quat to calculate inverse of
342
+ * @returns {quat} out
343
+ */
193
344
  export function invert(out, a) {
194
- const a0 = a[0];
195
- const a1 = a[1];
196
- const a2 = a[2];
197
- const a3 = a[3];
198
- const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
199
- const invDot = dot ? 1.0 / dot : 0;
200
- out[0] = -a0 * invDot;
201
- out[1] = -a1 * invDot;
202
- out[2] = -a2 * invDot;
203
- out[3] = a3 * invDot;
204
- return out;
345
+ const a0 = a[0];
346
+ const a1 = a[1];
347
+ const a2 = a[2];
348
+ const a3 = a[3];
349
+ const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
350
+ const invDot = dot ? 1.0 / dot : 0;
351
+ // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
352
+ out[0] = -a0 * invDot;
353
+ out[1] = -a1 * invDot;
354
+ out[2] = -a2 * invDot;
355
+ out[3] = a3 * invDot;
356
+ return out;
205
357
  }
358
+ /**
359
+ * Calculates the conjugate of a quat
360
+ * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
361
+ *
362
+ * @param {quat} out the receiving quaternion
363
+ * @param {ReadonlyQuat} a quat to calculate conjugate of
364
+ * @returns {quat} out
365
+ */
206
366
  export function conjugate(out, a) {
207
- out[0] = -a[0];
208
- out[1] = -a[1];
209
- out[2] = -a[2];
210
- out[3] = a[3];
211
- return out;
367
+ out[0] = -a[0];
368
+ out[1] = -a[1];
369
+ out[2] = -a[2];
370
+ out[3] = a[3];
371
+ return out;
212
372
  }
373
+ /**
374
+ * Creates a quaternion from the given 3x3 rotation matrix.
375
+ *
376
+ * NOTE: The resultant quaternion is not normalized, so you should be sure
377
+ * to renormalize the quaternion yourself where necessary.
378
+ *
379
+ * @param {quat} out the receiving quaternion
380
+ * @param {ReadonlyMat3} m rotation matrix
381
+ * @returns {quat} out
382
+ * @function
383
+ */
213
384
  export function fromMat3(out, m) {
214
- const fTrace = m[0] + m[4] + m[8];
215
- let fRoot;
216
-
217
- if (fTrace > 0.0) {
218
- fRoot = Math.sqrt(fTrace + 1.0);
219
- out[3] = 0.5 * fRoot;
220
- fRoot = 0.5 / fRoot;
221
- out[0] = (m[5] - m[7]) * fRoot;
222
- out[1] = (m[6] - m[2]) * fRoot;
223
- out[2] = (m[1] - m[3]) * fRoot;
224
- } else {
225
- let i = 0;
226
- if (m[4] > m[0]) i = 1;
227
- if (m[8] > m[i * 3 + i]) i = 2;
228
- const j = (i + 1) % 3;
229
- const k = (i + 2) % 3;
230
- fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
231
- out[i] = 0.5 * fRoot;
232
- fRoot = 0.5 / fRoot;
233
- out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
234
- out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
235
- out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
236
- }
237
-
238
- return out;
385
+ // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
386
+ // article "Quaternion Calculus and Fast Animation".
387
+ const fTrace = m[0] + m[4] + m[8];
388
+ let fRoot;
389
+ if (fTrace > 0.0) {
390
+ // |w| > 1/2, may as well choose w > 1/2
391
+ fRoot = Math.sqrt(fTrace + 1.0); // 2w
392
+ out[3] = 0.5 * fRoot;
393
+ fRoot = 0.5 / fRoot; // 1/(4w)
394
+ out[0] = (m[5] - m[7]) * fRoot;
395
+ out[1] = (m[6] - m[2]) * fRoot;
396
+ out[2] = (m[1] - m[3]) * fRoot;
397
+ }
398
+ else {
399
+ // |w| <= 1/2
400
+ let i = 0;
401
+ if (m[4] > m[0])
402
+ i = 1;
403
+ if (m[8] > m[i * 3 + i])
404
+ i = 2;
405
+ const j = (i + 1) % 3;
406
+ const k = (i + 2) % 3;
407
+ fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
408
+ out[i] = 0.5 * fRoot;
409
+ fRoot = 0.5 / fRoot;
410
+ out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
411
+ out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
412
+ out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
413
+ }
414
+ return out;
239
415
  }
416
+ /**
417
+ * Creates a quaternion from the given euler angle x, y, z using the provided intrinsic order for the conversion.
418
+ *
419
+ * @param {quat} out the receiving quaternion
420
+ * @param {Number} x Angle to rotate around X axis in degrees.
421
+ * @param {Number} y Angle to rotate around Y axis in degrees.
422
+ * @param {Number} z Angle to rotate around Z axis in degrees.
423
+ * @param {'zyx'|'xyz'|'yxz'|'yzx'|'zxy'|'zyx'} order Intrinsic order for conversion, default is zyx.
424
+ * @returns {quat} out
425
+ * @function
426
+ */
427
+ // export function fromEuler(out, x, y, z, order = glMatrix.ANGLE_ORDER) {
428
+ // let halfToRad = Math.PI / 360;
429
+ // x *= halfToRad;
430
+ // z *= halfToRad;
431
+ // y *= halfToRad;
432
+ // let sx = Math.sin(x);
433
+ // let cx = Math.cos(x);
434
+ // let sy = Math.sin(y);
435
+ // let cy = Math.cos(y);
436
+ // let sz = Math.sin(z);
437
+ // let cz = Math.cos(z);
438
+ // switch (order) {
439
+ // case "xyz":
440
+ // out[0] = sx * cy * cz + cx * sy * sz;
441
+ // out[1] = cx * sy * cz - sx * cy * sz;
442
+ // out[2] = cx * cy * sz + sx * sy * cz;
443
+ // out[3] = cx * cy * cz - sx * sy * sz;
444
+ // break;
445
+ // case "xzy":
446
+ // out[0] = sx * cy * cz - cx * sy * sz;
447
+ // out[1] = cx * sy * cz - sx * cy * sz;
448
+ // out[2] = cx * cy * sz + sx * sy * cz;
449
+ // out[3] = cx * cy * cz + sx * sy * sz;
450
+ // break;
451
+ // case "yxz":
452
+ // out[0] = sx * cy * cz + cx * sy * sz;
453
+ // out[1] = cx * sy * cz - sx * cy * sz;
454
+ // out[2] = cx * cy * sz - sx * sy * cz;
455
+ // out[3] = cx * cy * cz + sx * sy * sz;
456
+ // break;
457
+ // case "yzx":
458
+ // out[0] = sx * cy * cz + cx * sy * sz;
459
+ // out[1] = cx * sy * cz + sx * cy * sz;
460
+ // out[2] = cx * cy * sz - sx * sy * cz;
461
+ // out[3] = cx * cy * cz - sx * sy * sz;
462
+ // break;
463
+ // case "zxy":
464
+ // out[0] = sx * cy * cz - cx * sy * sz;
465
+ // out[1] = cx * sy * cz + sx * cy * sz;
466
+ // out[2] = cx * cy * sz + sx * sy * cz;
467
+ // out[3] = cx * cy * cz - sx * sy * sz;
468
+ // break;
469
+ // case "zyx":
470
+ // out[0] = sx * cy * cz - cx * sy * sz;
471
+ // out[1] = cx * sy * cz + sx * cy * sz;
472
+ // out[2] = cx * cy * sz - sx * sy * cz;
473
+ // out[3] = cx * cy * cz + sx * sy * sz;
474
+ // break;
475
+ // default:
476
+ // throw new Error('Unknown angle order ' + order);
477
+ // }
478
+ // return out;
479
+ // }
480
+ /**
481
+ * Returns a string representation of a quaternion
482
+ *
483
+ * @param {ReadonlyQuat} a vector to represent as a string
484
+ * @returns {String} string representation of the vector
485
+ */
240
486
  export function str(a) {
241
- return "quat(".concat(a[0], ", ").concat(a[1], ", ").concat(a[2], ", ").concat(a[3], ")");
487
+ return `quat(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]})`;
242
488
  }
489
+ /**
490
+ * Creates a new quat initialized with values from an existing quaternion
491
+ *
492
+ * @param {ReadonlyQuat} a quaternion to clone
493
+ * @returns {quat} a new quaternion
494
+ * @function
495
+ */
243
496
  export const clone = vec4.clone;
497
+ /**
498
+ * Creates a new quat initialized with the given values
499
+ *
500
+ * @param {Number} x X component
501
+ * @param {Number} y Y component
502
+ * @param {Number} z Z component
503
+ * @param {Number} w W component
504
+ * @returns {quat} a new quaternion
505
+ * @function
506
+ */
244
507
  export const fromValues = vec4.fromValues;
508
+ /**
509
+ * Copy the values from one quat to another
510
+ *
511
+ * @param {quat} out the receiving quaternion
512
+ * @param {ReadonlyQuat} a the source quaternion
513
+ * @returns {quat} out
514
+ * @function
515
+ */
245
516
  export const copy = vec4.copy;
517
+ /**
518
+ * Set the components of a quat to the given values
519
+ *
520
+ * @param {quat} out the receiving quaternion
521
+ * @param {Number} x X component
522
+ * @param {Number} y Y component
523
+ * @param {Number} z Z component
524
+ * @param {Number} w W component
525
+ * @returns {quat} out
526
+ * @function
527
+ */
246
528
  export const set = vec4.set;
529
+ /**
530
+ * Adds two quat's
531
+ *
532
+ * @param {quat} out the receiving quaternion
533
+ * @param {ReadonlyQuat} a the first operand
534
+ * @param {ReadonlyQuat} b the second operand
535
+ * @returns {quat} out
536
+ * @function
537
+ */
247
538
  export const add = vec4.add;
539
+ /**
540
+ * Alias for {@link quat.multiply}
541
+ * @function
542
+ */
248
543
  export const mul = multiply;
544
+ /**
545
+ * Scales a quat by a scalar number
546
+ *
547
+ * @param {quat} out the receiving vector
548
+ * @param {ReadonlyQuat} a the vector to scale
549
+ * @param {Number} b amount to scale the vector by
550
+ * @returns {quat} out
551
+ * @function
552
+ */
249
553
  export const scale = vec4.scale;
554
+ /**
555
+ * Calculates the dot product of two quat's
556
+ *
557
+ * @param {ReadonlyQuat} a the first operand
558
+ * @param {ReadonlyQuat} b the second operand
559
+ * @returns {Number} dot product of a and b
560
+ * @function
561
+ */
250
562
  export const dot = vec4.dot;
563
+ /**
564
+ * Performs a linear interpolation between two quat's
565
+ *
566
+ * @param {quat} out the receiving quaternion
567
+ * @param {ReadonlyQuat} a the first operand
568
+ * @param {ReadonlyQuat} b the second operand
569
+ * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
570
+ * @returns {quat} out
571
+ * @function
572
+ */
251
573
  export const lerp = vec4.lerp;
574
+ /**
575
+ * Calculates the length of a quat
576
+ *
577
+ * @param {ReadonlyQuat} a vector to calculate length of
578
+ * @returns {Number} length of a
579
+ */
252
580
  export const length = vec4.length;
581
+ /**
582
+ * Alias for {@link quat.length}
583
+ * @function
584
+ */
253
585
  export const len = length;
586
+ /**
587
+ * Calculates the squared length of a quat
588
+ *
589
+ * @param {ReadonlyQuat} a vector to calculate squared length of
590
+ * @returns {Number} squared length of a
591
+ * @function
592
+ */
254
593
  export const squaredLength = vec4.squaredLength;
594
+ /**
595
+ * Alias for {@link quat.squaredLength}
596
+ * @function
597
+ */
255
598
  export const sqrLen = squaredLength;
599
+ /**
600
+ * Normalize a quat
601
+ *
602
+ * @param {quat} out the receiving quaternion
603
+ * @param {ReadonlyQuat} a quaternion to normalize
604
+ * @returns {quat} out
605
+ * @function
606
+ */
256
607
  export const normalize = vec4.normalize;
608
+ /**
609
+ * Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===)
610
+ *
611
+ * @param {ReadonlyQuat} a The first quaternion.
612
+ * @param {ReadonlyQuat} b The second quaternion.
613
+ * @returns {Boolean} True if the vectors are equal, false otherwise.
614
+ */
257
615
  export const exactEquals = vec4.exactEquals;
616
+ /**
617
+ * Returns whether or not the quaternions point approximately to the same direction.
618
+ *
619
+ * Both quaternions are assumed to be unit length.
620
+ *
621
+ * @param {ReadonlyQuat} a The first unit quaternion.
622
+ * @param {ReadonlyQuat} b The second unit quaternion.
623
+ * @returns {Boolean} True if the quaternions are equal, false otherwise.
624
+ */
258
625
  export function equals(a, b) {
259
- return Math.abs(vec4.dot(a, b)) >= 1 - glMatrix.EPSILON;
626
+ return Math.abs(vec4.dot(a, b)) >= 1 - glMatrix.EPSILON;
260
627
  }
261
- export const rotationTo = function () {
262
- const tmpvec3 = vec3.create();
263
- const xUnitVec3 = vec3.fromValues(1, 0, 0);
264
- const yUnitVec3 = vec3.fromValues(0, 1, 0);
265
- return function (out, a, b) {
266
- const dot = vec3.dot(a, b);
267
-
268
- if (dot < -0.999999) {
269
- vec3.cross(tmpvec3, xUnitVec3, a);
270
- if (vec3.len(tmpvec3) < 0.000001) vec3.cross(tmpvec3, yUnitVec3, a);
271
- vec3.normalize(tmpvec3, tmpvec3);
272
- setAxisAngle(out, tmpvec3, Math.PI);
273
- return out;
274
- } else if (dot > 0.999999) {
275
- out[0] = 0;
276
- out[1] = 0;
277
- out[2] = 0;
278
- out[3] = 1;
279
- return out;
280
- }
281
-
282
- vec3.cross(tmpvec3, a, b);
283
- out[0] = tmpvec3[0];
284
- out[1] = tmpvec3[1];
285
- out[2] = tmpvec3[2];
286
- out[3] = 1 + dot;
287
- return normalize(out, out);
288
- };
289
- }();
290
- export const sqlerp = function () {
291
- const temp1 = create();
292
- const temp2 = create();
293
- return function (out, a, b, c, d, t) {
294
- slerp(temp1, a, d, t);
295
- slerp(temp2, b, c, t);
296
- slerp(out, temp1, temp2, 2 * t * (1 - t));
297
- return out;
298
- };
299
- }();
300
- export const setAxes = function () {
301
- const matr = mat3.create();
302
- return function (out, view, right, up) {
303
- matr[0] = right[0];
304
- matr[3] = right[1];
305
- matr[6] = right[2];
306
- matr[1] = up[0];
307
- matr[4] = up[1];
308
- matr[7] = up[2];
309
- matr[2] = -view[0];
310
- matr[5] = -view[1];
311
- matr[8] = -view[2];
312
- return normalize(out, fromMat3(out, matr));
313
- };
314
- }();
315
- //# sourceMappingURL=quat.js.map
628
+ /**
629
+ * Sets a quaternion to represent the shortest rotation from one
630
+ * vector to another.
631
+ *
632
+ * Both vectors are assumed to be unit length.
633
+ *
634
+ * @param {quat} out the receiving quaternion.
635
+ * @param {ReadonlyVec3} a the initial vector
636
+ * @param {ReadonlyVec3} b the destination vector
637
+ * @returns {quat} out
638
+ */
639
+ export const rotationTo = (function () {
640
+ const tmpvec3 = vec3.create();
641
+ const xUnitVec3 = vec3.fromValues(1, 0, 0);
642
+ const yUnitVec3 = vec3.fromValues(0, 1, 0);
643
+ return function (out, a, b) {
644
+ const dot = vec3.dot(a, b);
645
+ if (dot < -0.999999) {
646
+ vec3.cross(tmpvec3, xUnitVec3, a);
647
+ if (vec3.len(tmpvec3) < 0.000001)
648
+ vec3.cross(tmpvec3, yUnitVec3, a);
649
+ vec3.normalize(tmpvec3, tmpvec3);
650
+ setAxisAngle(out, tmpvec3, Math.PI);
651
+ return out;
652
+ }
653
+ else if (dot > 0.999999) {
654
+ out[0] = 0;
655
+ out[1] = 0;
656
+ out[2] = 0;
657
+ out[3] = 1;
658
+ return out;
659
+ }
660
+ vec3.cross(tmpvec3, a, b);
661
+ out[0] = tmpvec3[0];
662
+ out[1] = tmpvec3[1];
663
+ out[2] = tmpvec3[2];
664
+ out[3] = 1 + dot;
665
+ return normalize(out, out);
666
+ };
667
+ })();
668
+ /**
669
+ * Performs a spherical linear interpolation with two control points
670
+ *
671
+ * @param {quat} out the receiving quaternion
672
+ * @param {ReadonlyQuat} a the first operand
673
+ * @param {ReadonlyQuat} b the second operand
674
+ * @param {ReadonlyQuat} c the third operand
675
+ * @param {ReadonlyQuat} d the fourth operand
676
+ * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
677
+ * @returns {quat} out
678
+ */
679
+ export const sqlerp = (function () {
680
+ const temp1 = create();
681
+ const temp2 = create();
682
+ return function (out, a, b, c, d, t) {
683
+ slerp(temp1, a, d, t);
684
+ slerp(temp2, b, c, t);
685
+ slerp(out, temp1, temp2, 2 * t * (1 - t));
686
+ return out;
687
+ };
688
+ })();
689
+ /**
690
+ * Sets the specified quaternion with values corresponding to the given
691
+ * axes. Each axis is a vec3 and is expected to be unit length and
692
+ * perpendicular to all other specified axes.
693
+ *
694
+ * @param {ReadonlyVec3} view the vector representing the viewing direction
695
+ * @param {ReadonlyVec3} right the vector representing the local "right" direction
696
+ * @param {ReadonlyVec3} up the vector representing the local "up" direction
697
+ * @returns {quat} out
698
+ */
699
+ export const setAxes = (function () {
700
+ const matr = mat3.create();
701
+ return function (out, view, right, up) {
702
+ matr[0] = right[0];
703
+ matr[3] = right[1];
704
+ matr[6] = right[2];
705
+ matr[1] = up[0];
706
+ matr[4] = up[1];
707
+ matr[7] = up[2];
708
+ matr[2] = -view[0];
709
+ matr[5] = -view[1];
710
+ matr[8] = -view[2];
711
+ return normalize(out, fromMat3(out, matr));
712
+ };
713
+ })();