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,916 @@
1
+ import { EPSILON, round as round$1 } from './scalar.js';
2
+
3
+ /**
4
+ * Creates a new, empty vec3
5
+ *
6
+ * @returns a new 3D vector
7
+ */
8
+ function create() {
9
+ return [0, 0, 0];
10
+ }
11
+ /**
12
+ * Creates a new vec3 initialized with values from an existing vector
13
+ *
14
+ * @param a vector to clone
15
+ * @returns a new 3D vector
16
+ */
17
+ function clone(a) {
18
+ return [a[0], a[1], a[2]];
19
+ }
20
+ /**
21
+ * Creates a new vec3 initialized with the given values
22
+ *
23
+ * @param x X component
24
+ * @param y Y component
25
+ * @param z Z component
26
+ * @returns a new 3D vector
27
+ */
28
+ function fromValues(x, y, z) {
29
+ return [x, y, z];
30
+ }
31
+ /**
32
+ * Calculates the length of a vec3
33
+ *
34
+ * @param a vector to calculate length of
35
+ * @returns length of a
36
+ */
37
+ function length(a) {
38
+ const x = a[0];
39
+ const y = a[1];
40
+ const z = a[2];
41
+ return Math.sqrt(x * x + y * y + z * z);
42
+ }
43
+ /**
44
+ * Copy the values from one vec3 to another
45
+ *
46
+ * @param out the receiving vector
47
+ * @param a the source vector
48
+ * @returns out
49
+ */
50
+ function copy(out, a) {
51
+ out[0] = a[0];
52
+ out[1] = a[1];
53
+ out[2] = a[2];
54
+ return out;
55
+ }
56
+ /**
57
+ * Set the components of a vec3 to the given values
58
+ *
59
+ * @param out the receiving vector
60
+ * @param x X component
61
+ * @param y Y component
62
+ * @param z Z component
63
+ * @returns out
64
+ */
65
+ function set(out, x, y, z) {
66
+ out[0] = x;
67
+ out[1] = y;
68
+ out[2] = z;
69
+ return out;
70
+ }
71
+ /**
72
+ * Sets all components of a vec3 to the given scalar value
73
+ *
74
+ * @param out the receiving vector
75
+ * @param s scalar value to set
76
+ * @returns out
77
+ */
78
+ function setScalar(out, s) {
79
+ out[0] = s;
80
+ out[1] = s;
81
+ out[2] = s;
82
+ return out;
83
+ }
84
+ /**
85
+ * Sets the components of a vec3 from a buffer
86
+ * @param out the receiving vector
87
+ * @param buffer the source buffer
88
+ * @param startIndex the starting index in the buffer
89
+ * @returns out
90
+ */
91
+ function fromBuffer(out, buffer, startIndex) {
92
+ out[0] = buffer[startIndex];
93
+ out[1] = buffer[startIndex + 1];
94
+ out[2] = buffer[startIndex + 2];
95
+ return out;
96
+ }
97
+ /**
98
+ * Writes the components of a vec3 to a buffer
99
+ * @param outBuffer The output buffer
100
+ * @param vec The source vector
101
+ * @param startIndex The starting index in the buffer
102
+ * @returns The output buffer
103
+ */
104
+ function toBuffer(outBuffer, vec, startIndex) {
105
+ outBuffer[startIndex] = vec[0];
106
+ outBuffer[startIndex + 1] = vec[1];
107
+ outBuffer[startIndex + 2] = vec[2];
108
+ return outBuffer;
109
+ }
110
+ /**
111
+ * Adds two vec3's
112
+ *
113
+ * @param out the receiving vector
114
+ * @param a the first operand
115
+ * @param b the second operand
116
+ * @returns out
117
+ */
118
+ function add(out, a, b) {
119
+ out[0] = a[0] + b[0];
120
+ out[1] = a[1] + b[1];
121
+ out[2] = a[2] + b[2];
122
+ return out;
123
+ }
124
+ /**
125
+ * Adds a scalar value to all components of a vec3
126
+ *
127
+ * @param out the receiving vector
128
+ * @param a the source vector
129
+ * @param b the scalar value to add
130
+ * @returns out
131
+ */
132
+ function addScalar(out, a, b) {
133
+ out[0] = a[0] + b;
134
+ out[1] = a[1] + b;
135
+ out[2] = a[2] + b;
136
+ return out;
137
+ }
138
+ /**
139
+ * Subtracts vector b from vector a
140
+ *
141
+ * @param out the receiving vector
142
+ * @param a the first operand
143
+ * @param b the second operand
144
+ * @returns out
145
+ */
146
+ function subtract(out, a, b) {
147
+ out[0] = a[0] - b[0];
148
+ out[1] = a[1] - b[1];
149
+ out[2] = a[2] - b[2];
150
+ return out;
151
+ }
152
+ /**
153
+ * Subtracts a scalar value from all components of a vec3
154
+ *
155
+ * @param out the receiving vector
156
+ * @param a the source vector
157
+ * @param b the scalar value to subtract
158
+ * @returns out
159
+ */
160
+ function subtractScalar(out, a, b) {
161
+ out[0] = a[0] - b;
162
+ out[1] = a[1] - b;
163
+ out[2] = a[2] - b;
164
+ return out;
165
+ }
166
+ /**
167
+ * Multiplies two vec3's
168
+ *
169
+ * @param out the receiving vector
170
+ * @param a the first operand
171
+ * @param b the second operand
172
+ * @returns out
173
+ */
174
+ function multiply(out, a, b) {
175
+ out[0] = a[0] * b[0];
176
+ out[1] = a[1] * b[1];
177
+ out[2] = a[2] * b[2];
178
+ return out;
179
+ }
180
+ /**
181
+ * Divides two vec3's
182
+ *
183
+ * @param out the receiving vector
184
+ * @param a the first operand
185
+ * @param b the second operand
186
+ * @returns out
187
+ */
188
+ function divide(out, a, b) {
189
+ out[0] = a[0] / b[0];
190
+ out[1] = a[1] / b[1];
191
+ out[2] = a[2] / b[2];
192
+ return out;
193
+ }
194
+ /**
195
+ * Math.ceil the components of a vec3
196
+ *
197
+ * @param out the receiving vector
198
+ * @param a vector to ceil
199
+ * @returns out
200
+ */
201
+ function ceil(out, a) {
202
+ out[0] = Math.ceil(a[0]);
203
+ out[1] = Math.ceil(a[1]);
204
+ out[2] = Math.ceil(a[2]);
205
+ return out;
206
+ }
207
+ /**
208
+ * Math.floor the components of a vec3
209
+ *
210
+ * @param out the receiving vector
211
+ * @param a vector to floor
212
+ * @returns out
213
+ */
214
+ function floor(out, a) {
215
+ out[0] = Math.floor(a[0]);
216
+ out[1] = Math.floor(a[1]);
217
+ out[2] = Math.floor(a[2]);
218
+ return out;
219
+ }
220
+ /**
221
+ * Returns the minimum of two vec3's
222
+ *
223
+ * @param out the receiving vector
224
+ * @param a the first operand
225
+ * @param b the second operand
226
+ * @returns out
227
+ */
228
+ function min(out, a, b) {
229
+ out[0] = Math.min(a[0], b[0]);
230
+ out[1] = Math.min(a[1], b[1]);
231
+ out[2] = Math.min(a[2], b[2]);
232
+ return out;
233
+ }
234
+ /**
235
+ * Returns the maximum of two vec3's
236
+ *
237
+ * @param out the receiving vector
238
+ * @param a the first operand
239
+ * @param b the second operand
240
+ * @returns out
241
+ */
242
+ function max(out, a, b) {
243
+ out[0] = Math.max(a[0], b[0]);
244
+ out[1] = Math.max(a[1], b[1]);
245
+ out[2] = Math.max(a[2], b[2]);
246
+ return out;
247
+ }
248
+ /**
249
+ * symmetric round the components of a vec3
250
+ *
251
+ * @param out the receiving vector
252
+ * @param a vector to round
253
+ * @returns out
254
+ */
255
+ function round(out, a) {
256
+ out[0] = round$1(a[0]);
257
+ out[1] = round$1(a[1]);
258
+ out[2] = round$1(a[2]);
259
+ return out;
260
+ }
261
+ /**
262
+ * Scales a vec3 by a scalar number
263
+ *
264
+ * @param out the receiving vector
265
+ * @param a the vector to scale
266
+ * @param b amount to scale the vector by
267
+ * @returns out
268
+ */
269
+ function scale(out, a, b) {
270
+ out[0] = a[0] * b;
271
+ out[1] = a[1] * b;
272
+ out[2] = a[2] * b;
273
+ return out;
274
+ }
275
+ /**
276
+ * Adds two vec3's after scaling the second operand by a scalar value
277
+ *
278
+ * @param out the receiving vector
279
+ * @param a the first operand
280
+ * @param b the second operand
281
+ * @param scale the amount to scale b by before adding
282
+ * @returns out
283
+ */
284
+ function scaleAndAdd(out, a, b, scale) {
285
+ out[0] = a[0] + b[0] * scale;
286
+ out[1] = a[1] + b[1] * scale;
287
+ out[2] = a[2] + b[2] * scale;
288
+ return out;
289
+ }
290
+ /**
291
+ * Calculates the euclidian distance between two vec3's
292
+ *
293
+ * @param a the first operand
294
+ * @param b the second operand
295
+ * @returns distance between a and b
296
+ */
297
+ function distance(a, b) {
298
+ const x = b[0] - a[0];
299
+ const y = b[1] - a[1];
300
+ const z = b[2] - a[2];
301
+ return Math.sqrt(x * x + y * y + z * z);
302
+ }
303
+ /**
304
+ * Calculates the squared euclidian distance between two vec3's
305
+ *
306
+ * @param a the first operand
307
+ * @param b the second operand
308
+ * @returns squared distance between a and b
309
+ */
310
+ function squaredDistance(a, b) {
311
+ const x = b[0] - a[0];
312
+ const y = b[1] - a[1];
313
+ const z = b[2] - a[2];
314
+ return x * x + y * y + z * z;
315
+ }
316
+ /**
317
+ * Calculates the squared length of a vec3
318
+ *
319
+ * @param a vector to calculate squared length of
320
+ * @returns squared length of a
321
+ */
322
+ function squaredLength(a) {
323
+ const x = a[0];
324
+ const y = a[1];
325
+ const z = a[2];
326
+ return x * x + y * y + z * z;
327
+ }
328
+ /**
329
+ * Negates the components of a vec3
330
+ *
331
+ * @param out the receiving vector
332
+ * @param a vector to negate
333
+ * @returns out
334
+ */
335
+ function negate(out, a) {
336
+ out[0] = -a[0];
337
+ out[1] = -a[1];
338
+ out[2] = -a[2];
339
+ return out;
340
+ }
341
+ /**
342
+ * Returns the inverse of the components of a vec3
343
+ *
344
+ * @param out the receiving vector
345
+ * @param a vector to invert
346
+ * @returns out
347
+ */
348
+ function inverse(out, a) {
349
+ out[0] = 1.0 / a[0];
350
+ out[1] = 1.0 / a[1];
351
+ out[2] = 1.0 / a[2];
352
+ return out;
353
+ }
354
+ /**
355
+ * Normalize a vec3
356
+ *
357
+ * @param out the receiving vector
358
+ * @param a vector to normalize
359
+ * @returns out
360
+ */
361
+ function normalize(out, a) {
362
+ const x = a[0];
363
+ const y = a[1];
364
+ const z = a[2];
365
+ let len = x * x + y * y + z * z;
366
+ if (len > 0) {
367
+ len = 1 / Math.sqrt(len);
368
+ }
369
+ out[0] = a[0] * len;
370
+ out[1] = a[1] * len;
371
+ out[2] = a[2] * len;
372
+ return out;
373
+ }
374
+ /**
375
+ * Calculates the dot product of two vec3's
376
+ *
377
+ * @param a the first operand
378
+ * @param b the second operand
379
+ * @returns dot product of a and b
380
+ */
381
+ function dot(a, b) {
382
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
383
+ }
384
+ /**
385
+ * Computes the cross product of two vec3's
386
+ *
387
+ * @param out the receiving vector
388
+ * @param a the first operand
389
+ * @param b the second operand
390
+ * @returns out
391
+ */
392
+ function cross(out, a, b) {
393
+ const ax = a[0];
394
+ const ay = a[1];
395
+ const az = a[2];
396
+ const bx = b[0];
397
+ const by = b[1];
398
+ const bz = b[2];
399
+ out[0] = ay * bz - az * by;
400
+ out[1] = az * bx - ax * bz;
401
+ out[2] = ax * by - ay * bx;
402
+ return out;
403
+ }
404
+ /**
405
+ * Calculates a normalized perpendicular vector to the given vector.
406
+ * Useful for finding an arbitrary orthogonal basis vector.
407
+ *
408
+ * The zero vector has no unique perpendicular - every direction qualifies - so it returns the X
409
+ * axis rather than NaN.
410
+ *
411
+ * @param out the receiving vector
412
+ * @param a the source vector
413
+ * @returns the out vector
414
+ */
415
+ function perpendicular(out, a) {
416
+ if (Math.abs(a[0]) > Math.abs(a[1])) {
417
+ const len = Math.sqrt(a[0] * a[0] + a[2] * a[2]);
418
+ const invLen = 1.0 / len;
419
+ out[0] = a[2] * invLen;
420
+ out[1] = 0;
421
+ out[2] = -a[0] * invLen;
422
+ }
423
+ else {
424
+ const len = Math.sqrt(a[1] * a[1] + a[2] * a[2]);
425
+ if (len === 0) {
426
+ // the zero vector, the only input that reaches here with no length. every direction is
427
+ // perpendicular to it, so return one rather than dividing by zero into NaN
428
+ out[0] = 1;
429
+ out[1] = 0;
430
+ out[2] = 0;
431
+ return out;
432
+ }
433
+ const invLen = 1.0 / len;
434
+ out[0] = 0;
435
+ out[1] = a[2] * invLen;
436
+ out[2] = -a[1] * invLen;
437
+ }
438
+ return out;
439
+ }
440
+ /**
441
+ * Performs a linear interpolation between two vec3's
442
+ *
443
+ * @param out the receiving vector
444
+ * @param a the first operand
445
+ * @param b the second operand
446
+ * @param t interpolation amount, in the range [0-1], between the two inputs
447
+ * @returns out
448
+ */
449
+ function lerp(out, a, b, t) {
450
+ const ax = a[0];
451
+ const ay = a[1];
452
+ const az = a[2];
453
+ out[0] = ax + t * (b[0] - ax);
454
+ out[1] = ay + t * (b[1] - ay);
455
+ out[2] = az + t * (b[2] - az);
456
+ return out;
457
+ }
458
+ /**
459
+ * Quadratic interpolation through three vectors using Lagrange interpolation.
460
+ * Passes through a at t=0, b at t=0.5, and c at t=1.
461
+ *
462
+ * @param out the receiving vector
463
+ * @param a vector at t=0
464
+ * @param b vector at t=0.5
465
+ * @param c vector at t=1
466
+ * @param t interpolation amount
467
+ * @returns out
468
+ */
469
+ function lagrange(out, a, b, c, t) {
470
+ const c0 = 2 * (t - 1) * (t - 0.5);
471
+ const c1 = -4 * (t - 1) * t;
472
+ const c2 = 2 * (t - 0.5) * t;
473
+ out[0] = c0 * a[0] + c1 * b[0] + c2 * c[0];
474
+ out[1] = c0 * a[1] + c1 * b[1] + c2 * c[1];
475
+ out[2] = c0 * a[2] + c1 * b[2] + c2 * c[2];
476
+ return out;
477
+ }
478
+ /**
479
+ * Performs a spherical linear interpolation between two vec3's
480
+ *
481
+ * @param out the receiving vector
482
+ * @param a the first operand
483
+ * @param b the second operand
484
+ * @param t interpolation amount, in the range [0-1], between the two inputs
485
+ * @returns out
486
+ */
487
+ function slerp(out, a, b, t) {
488
+ const angle = Math.acos(Math.min(Math.max(dot(a, b), -1), 1));
489
+ const sinTotal = Math.sin(angle);
490
+ const ratioA = Math.sin((1 - t) * angle) / sinTotal;
491
+ const ratioB = Math.sin(t * angle) / sinTotal;
492
+ out[0] = ratioA * a[0] + ratioB * b[0];
493
+ out[1] = ratioA * a[1] + ratioB * b[1];
494
+ out[2] = ratioA * a[2] + ratioB * b[2];
495
+ return out;
496
+ }
497
+ /**
498
+ * Performs a hermite interpolation with two control points
499
+ *
500
+ * @param out the receiving vector
501
+ * @param a the first operand
502
+ * @param b the second operand
503
+ * @param c the third operand
504
+ * @param d the fourth operand
505
+ * @param t interpolation amount, in the range [0-1], between the two inputs
506
+ * @returns out
507
+ */
508
+ function hermite(out, a, b, c, d, t) {
509
+ const factorTimes2 = t * t;
510
+ const factor1 = factorTimes2 * (2 * t - 3) + 1;
511
+ const factor2 = factorTimes2 * (t - 2) + t;
512
+ const factor3 = factorTimes2 * (t - 1);
513
+ const factor4 = factorTimes2 * (3 - 2 * t);
514
+ out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
515
+ out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
516
+ out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
517
+ return out;
518
+ }
519
+ /**
520
+ * Performs a bezier interpolation with two control points
521
+ *
522
+ * @param out the receiving vector
523
+ * @param a the first operand
524
+ * @param b the second operand
525
+ * @param c the third operand
526
+ * @param d the fourth operand
527
+ * @param t interpolation amount, in the range [0-1], between the two inputs
528
+ * @returns out
529
+ */
530
+ function bezier(out, a, b, c, d, t) {
531
+ const inverseFactor = 1 - t;
532
+ const inverseFactorTimesTwo = inverseFactor * inverseFactor;
533
+ const factorTimes2 = t * t;
534
+ const factor1 = inverseFactorTimesTwo * inverseFactor;
535
+ const factor2 = 3 * t * inverseFactorTimesTwo;
536
+ const factor3 = 3 * factorTimes2 * inverseFactor;
537
+ const factor4 = factorTimes2 * t;
538
+ out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
539
+ out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
540
+ out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
541
+ return out;
542
+ }
543
+ /**
544
+ * Transforms the vec3 with a mat4.
545
+ * 4th vector component is implicitly '1'
546
+ *
547
+ * @param out the receiving vector
548
+ * @param a the vector to transform
549
+ * @param m matrix to transform with
550
+ * @returns out
551
+ */
552
+ function transformMat4(out, a, m) {
553
+ const x = a[0];
554
+ const y = a[1];
555
+ const z = a[2];
556
+ let w = m[3] * x + m[7] * y + m[11] * z + m[15];
557
+ w = w || 1.0;
558
+ out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
559
+ out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
560
+ out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
561
+ return out;
562
+ }
563
+ /**
564
+ * Transforms the vec3 with a mat3.
565
+ *
566
+ * @param out the receiving vector
567
+ * @param a the vector to transform
568
+ * @param m the 3x3 matrix to transform with
569
+ * @returns out
570
+ */
571
+ function transformMat3(out, a, m) {
572
+ const x = a[0];
573
+ const y = a[1];
574
+ const z = a[2];
575
+ out[0] = x * m[0] + y * m[3] + z * m[6];
576
+ out[1] = x * m[1] + y * m[4] + z * m[7];
577
+ out[2] = x * m[2] + y * m[5] + z * m[8];
578
+ return out;
579
+ }
580
+ /**
581
+ * Transforms the vec3 with a quat
582
+ * Can also be used for dual quaternions. (Multiply it with the real part)
583
+ *
584
+ * @param out the receiving vector
585
+ * @param a the vector to transform
586
+ * @param q quaternion to transform with
587
+ * @returns out
588
+ */
589
+ function transformQuat(out, a, q) {
590
+ // benchmarks: https://jsperf.com/quaternion-transform-vec3-implementations-fixed
591
+ const qx = q[0];
592
+ const qy = q[1];
593
+ const qz = q[2];
594
+ const qw = q[3];
595
+ const x = a[0];
596
+ const y = a[1];
597
+ const z = a[2];
598
+ // var qvec = [qx, qy, qz];
599
+ // var uv = vec3.cross([], qvec, a);
600
+ let uvx = qy * z - qz * y;
601
+ let uvy = qz * x - qx * z;
602
+ let uvz = qx * y - qy * x;
603
+ // var uuv = vec3.cross([], qvec, uv);
604
+ let uuvx = qy * uvz - qz * uvy;
605
+ let uuvy = qz * uvx - qx * uvz;
606
+ let uuvz = qx * uvy - qy * uvx;
607
+ // vec3.scale(uv, uv, 2 * w);
608
+ const w2 = qw * 2;
609
+ uvx *= w2;
610
+ uvy *= w2;
611
+ uvz *= w2;
612
+ // vec3.scale(uuv, uuv, 2);
613
+ uuvx *= 2;
614
+ uuvy *= 2;
615
+ uuvz *= 2;
616
+ // return vec3.add(out, a, vec3.add(out, uv, uuv));
617
+ out[0] = x + uvx + uuvx;
618
+ out[1] = y + uvy + uuvy;
619
+ out[2] = z + uvz + uuvz;
620
+ return out;
621
+ }
622
+ /**
623
+ * Rotate a 3D vector around the x-axis
624
+ * @param out The receiving vec3
625
+ * @param a The vec3 point to rotate
626
+ * @param b The origin of the rotation
627
+ * @param rad The angle of rotation in radians
628
+ * @returns out
629
+ */
630
+ function rotateX(out, a, b, rad) {
631
+ const p = [];
632
+ const r = [];
633
+ //Translate point to the origin
634
+ p[0] = a[0] - b[0];
635
+ p[1] = a[1] - b[1];
636
+ p[2] = a[2] - b[2];
637
+ //perform rotation
638
+ r[0] = p[0];
639
+ r[1] = p[1] * Math.cos(rad) - p[2] * Math.sin(rad);
640
+ r[2] = p[1] * Math.sin(rad) + p[2] * Math.cos(rad);
641
+ //translate to correct position
642
+ out[0] = r[0] + b[0];
643
+ out[1] = r[1] + b[1];
644
+ out[2] = r[2] + b[2];
645
+ return out;
646
+ }
647
+ /**
648
+ * Rotate a 3D vector around the y-axis
649
+ * @param out The receiving vec3
650
+ * @param a The vec3 point to rotate
651
+ * @param b The origin of the rotation
652
+ * @param rad The angle of rotation in radians
653
+ * @returns out
654
+ */
655
+ function rotateY(out, a, b, rad) {
656
+ const p = [];
657
+ const r = [];
658
+ // translate point to the origin
659
+ p[0] = a[0] - b[0];
660
+ p[1] = a[1] - b[1];
661
+ p[2] = a[2] - b[2];
662
+ // perform rotation
663
+ r[0] = p[2] * Math.sin(rad) + p[0] * Math.cos(rad);
664
+ r[1] = p[1];
665
+ r[2] = p[2] * Math.cos(rad) - p[0] * Math.sin(rad);
666
+ // translate to correct position
667
+ out[0] = r[0] + b[0];
668
+ out[1] = r[1] + b[1];
669
+ out[2] = r[2] + b[2];
670
+ return out;
671
+ }
672
+ /**
673
+ * Rotate a 3D vector around the z-axis
674
+ * @param out The receiving vec3
675
+ * @param a The vec3 point to rotate
676
+ * @param b The origin of the rotation
677
+ * @param rad The angle of rotation in radians
678
+ * @returns out
679
+ */
680
+ function rotateZ(out, a, b, rad) {
681
+ const p = [];
682
+ const r = [];
683
+ // translate point to the origin
684
+ p[0] = a[0] - b[0];
685
+ p[1] = a[1] - b[1];
686
+ p[2] = a[2] - b[2];
687
+ // perform rotation
688
+ r[0] = p[0] * Math.cos(rad) - p[1] * Math.sin(rad);
689
+ r[1] = p[0] * Math.sin(rad) + p[1] * Math.cos(rad);
690
+ r[2] = p[2];
691
+ // translate to correct position
692
+ out[0] = r[0] + b[0];
693
+ out[1] = r[1] + b[1];
694
+ out[2] = r[2] + b[2];
695
+ return out;
696
+ }
697
+ /**
698
+ * Get the angle between two 3D vectors
699
+ * @param a The first operand
700
+ * @param b The second operand
701
+ * @returns The angle in radians
702
+ */
703
+ function angle(a, b) {
704
+ const ax = a[0];
705
+ const ay = a[1];
706
+ const az = a[2];
707
+ const bx = b[0];
708
+ const by = b[1];
709
+ const bz = b[2];
710
+ const mag = Math.sqrt((ax * ax + ay * ay + az * az) * (bx * bx + by * by + bz * bz));
711
+ const cosine = mag && dot(a, b) / mag;
712
+ return Math.acos(Math.min(Math.max(cosine, -1), 1));
713
+ }
714
+ /**
715
+ * Get the signed angle from `a` to `b` measured about `axis`, in the range (-PI, PI].
716
+ *
717
+ * Positive is counter-clockwise when `axis` points toward the viewer. Only the components of
718
+ * `a` and `b` in the plane perpendicular to `axis` contribute, so they need not be
719
+ * perpendicular to it. Returns 0 if either projects to the zero vector.
720
+ *
721
+ * Unlike {@link angle}, this distinguishes the two directions of rotation, which is what
722
+ * hinge limits and turn directions need.
723
+ *
724
+ * @param a the first operand
725
+ * @param b the second operand
726
+ * @param axis the axis to measure the rotation about, assumed to be unit length
727
+ * @returns the signed angle in radians
728
+ */
729
+ function signedAngle(a, b, axis) {
730
+ const nx = axis[0];
731
+ const ny = axis[1];
732
+ const nz = axis[2];
733
+ // drop the components along the axis - they carry no signed angle
734
+ const ad = a[0] * nx + a[1] * ny + a[2] * nz;
735
+ const ax = a[0] - nx * ad;
736
+ const ay = a[1] - ny * ad;
737
+ const az = a[2] - nz * ad;
738
+ const bd = b[0] * nx + b[1] * ny + b[2] * nz;
739
+ const bx = b[0] - nx * bd;
740
+ const by = b[1] - ny * bd;
741
+ const bz = b[2] - nz * bd;
742
+ // (a x b) . axis is |a||b|sin(theta), a . b is |a||b|cos(theta)
743
+ const cx = ay * bz - az * by;
744
+ const cy = az * bx - ax * bz;
745
+ const cz = ax * by - ay * bx;
746
+ return Math.atan2(cx * nx + cy * ny + cz * nz, ax * bx + ay * by + az * bz);
747
+ }
748
+ const _rotateTowards_axis = [0, 0, 0];
749
+ /**
750
+ * Rotates the unit vector `from` toward the unit vector `to` by at most `maxAngle` radians.
751
+ *
752
+ * When the two are already within `maxAngle` this copies `to`, so the function doubles as a
753
+ * cone clamp: the result is `to`, limited to lie within `maxAngle` of `from`. That is the form
754
+ * a rotor joint limit and a per-frame turn rate both want.
755
+ *
756
+ * Both inputs are assumed to be unit length, and the result is unit length. `maxAngle` is treated
757
+ * as 0 if negative. When the inputs are exactly antiparallel the rotation plane is undefined
758
+ * and an arbitrary perpendicular is used.
759
+ *
760
+ * @param out the receiving vector
761
+ * @param from the unit vector to rotate away from
762
+ * @param to the unit vector to rotate toward
763
+ * @param maxAngle the maximum rotation, in radians
764
+ * @returns out
765
+ */
766
+ function rotateTowards(out, from, to, maxAngle) {
767
+ const fx = from[0];
768
+ const fy = from[1];
769
+ const fz = from[2];
770
+ const tx = to[0];
771
+ const ty = to[1];
772
+ const tz = to[2];
773
+ const cosine = Math.min(Math.max(fx * tx + fy * ty + fz * tz, -1), 1);
774
+ const limit = maxAngle > 0 ? maxAngle : 0;
775
+ if (Math.acos(cosine) <= limit) {
776
+ out[0] = tx;
777
+ out[1] = ty;
778
+ out[2] = tz;
779
+ return out;
780
+ }
781
+ // the rotation plane is spanned by the two vectors, so its normal is their cross product
782
+ let axisX = fy * tz - fz * ty;
783
+ let axisY = fz * tx - fx * tz;
784
+ let axisZ = fx * ty - fy * tx;
785
+ const axisLength = Math.sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ);
786
+ if (axisLength < 1e-8) {
787
+ // antiparallel: every plane containing `from` is equally valid, so pick one
788
+ perpendicular(_rotateTowards_axis, from);
789
+ axisX = _rotateTowards_axis[0];
790
+ axisY = _rotateTowards_axis[1];
791
+ axisZ = _rotateTowards_axis[2];
792
+ }
793
+ else {
794
+ const inverseLength = 1 / axisLength;
795
+ axisX *= inverseLength;
796
+ axisY *= inverseLength;
797
+ axisZ *= inverseLength;
798
+ }
799
+ // rodrigues rotation of `from` about `axis`
800
+ const c = Math.cos(limit);
801
+ const s = Math.sin(limit);
802
+ const d = (axisX * fx + axisY * fy + axisZ * fz) * (1 - c);
803
+ out[0] = fx * c + (axisY * fz - axisZ * fy) * s + axisX * d;
804
+ out[1] = fy * c + (axisZ * fx - axisX * fz) * s + axisY * d;
805
+ out[2] = fz * c + (axisX * fy - axisY * fx) * s + axisZ * d;
806
+ return out;
807
+ }
808
+ /**
809
+ * Set the components of a vec3 to zero
810
+ *
811
+ * @param out the receiving vector
812
+ * @returns out
813
+ */
814
+ function zero(out) {
815
+ out[0] = 0.0;
816
+ out[1] = 0.0;
817
+ out[2] = 0.0;
818
+ return out;
819
+ }
820
+ /**
821
+ * Returns a string representation of a vector
822
+ *
823
+ * @param a vector to represent as a string
824
+ * @returns string representation of the vector
825
+ */
826
+ function str(a) {
827
+ return `vec3(${a[0]}, ${a[1]}, ${a[2]})`;
828
+ }
829
+ /**
830
+ * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
831
+ *
832
+ * @param a The first vector.
833
+ * @param b The second vector.
834
+ * @returns True if the vectors are equal, false otherwise.
835
+ */
836
+ function exactEquals(a, b) {
837
+ return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
838
+ }
839
+ /**
840
+ * Returns whether or not the vectors have approximately the same elements in the same position.
841
+ *
842
+ * @param a The first vector.
843
+ * @param b The second vector.
844
+ * @returns True if the vectors are equal, false otherwise.
845
+ */
846
+ function equals(a, b) {
847
+ const a0 = a[0];
848
+ const a1 = a[1];
849
+ const a2 = a[2];
850
+ const b0 = b[0];
851
+ const b1 = b[1];
852
+ const b2 = b[2];
853
+ return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
854
+ Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
855
+ Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)));
856
+ }
857
+ /**
858
+ * Returns whether or not the vector is finite
859
+ * @param a vector to test
860
+ * @returns whether or not the vector is finite
861
+ */
862
+ function finite(a) {
863
+ return Number.isFinite(a[0]) && Number.isFinite(a[1]) && Number.isFinite(a[2]);
864
+ }
865
+ /**
866
+ * Determines if a scale vector represents an inside-out transformation (reflection)
867
+ * Returns true if an odd number of scale components are negative
868
+ *
869
+ * @param scale The scale vector to test
870
+ * @returns true if the scale represents a reflection (odd number of negative components)
871
+ */
872
+ function isScaleInsideOut(scale) {
873
+ // create a bitmask of which components are negative
874
+ // each component that is < 0 contributes a bit (1, 2, or 4)
875
+ const mask = (scale[0] < 0 ? 1 : 0) | (scale[1] < 0 ? 2 : 0) | (scale[2] < 0 ? 4 : 0);
876
+ // count the number of set bits and return true if odd
877
+ // popcount: count number of 1-bits in the mask
878
+ let count = 0;
879
+ let m = mask;
880
+ while (m) {
881
+ count += m & 1;
882
+ m >>= 1;
883
+ }
884
+ return (count & 1) !== 0;
885
+ }
886
+ /**
887
+ * Alias for {@link subtract}
888
+ */
889
+ const sub = subtract;
890
+ /**
891
+ * Alias for {@link multiply}
892
+ */
893
+ const mul = multiply;
894
+ /**
895
+ * Alias for {@link divide}
896
+ */
897
+ const div = divide;
898
+ /**
899
+ * Alias for {@link distance}
900
+ */
901
+ const dist = distance;
902
+ /**
903
+ * Alias for {@link squaredDistance}
904
+ */
905
+ const sqrDist = squaredDistance;
906
+ /**
907
+ * Alias for {@link length}
908
+ */
909
+ const len = length;
910
+ /**
911
+ * Alias for {@link squaredLength}
912
+ */
913
+ const sqrLen = squaredLength;
914
+
915
+ export { add, addScalar, angle, bezier, ceil, clone, copy, create, cross, dist, distance, div, divide, dot, equals, exactEquals, finite, floor, fromBuffer, fromValues, hermite, inverse, isScaleInsideOut, lagrange, len, length, lerp, max, min, mul, multiply, negate, normalize, perpendicular, rotateTowards, rotateX, rotateY, rotateZ, round, scale, scaleAndAdd, set, setScalar, signedAngle, slerp, sqrDist, sqrLen, squaredDistance, squaredLength, str, sub, subtract, subtractScalar, toBuffer, transformMat3, transformMat4, transformQuat, zero };
916
+ //# sourceMappingURL=vec3.js.map