math 0.0.0 → 0.1.0

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