math 0.0.3 → 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 (203) 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 -16
  199. package/skills/math/SKILL.md +140 -0
  200. package/AUTHORS +0 -1
  201. package/Cakefile +0 -28
  202. package/math.coffee +0 -112
  203. package/math.js +0 -141
@@ -0,0 +1,883 @@
1
+ import { EPSILON } from './scalar.js';
2
+
3
+ /**
4
+ * Creates a new identity dual quat
5
+ *
6
+ * @returns a new dual quaternion [real -> rotation, dual -> translation]
7
+ */
8
+ function create() {
9
+ // biome-ignore format:skip
10
+ return [
11
+ 0, 0, 0, 1, // real part
12
+ 0, 0, 0, 0, // dual part
13
+ ];
14
+ }
15
+ /**
16
+ * Creates a new quat initialized with values from an existing quaternion
17
+ *
18
+ * @param a dual quaternion to clone
19
+ * @returns new dual quaternion
20
+ * @function
21
+ */
22
+ function clone(a) {
23
+ return [a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]];
24
+ }
25
+ /**
26
+ * Creates a new dual quat initialized with the given values
27
+ *
28
+ * @param x1 X component
29
+ * @param y1 Y component
30
+ * @param z1 Z component
31
+ * @param w1 W component
32
+ * @param x2 X component
33
+ * @param y2 Y component
34
+ * @param z2 Z component
35
+ * @param w2 W component
36
+ * @returns new dual quaternion
37
+ * @function
38
+ */
39
+ function fromValues(x1, y1, z1, w1, x2, y2, z2, w2) {
40
+ return [x1, y1, z1, w1, x2, y2, z2, w2];
41
+ }
42
+ /**
43
+ * Creates a new dual quat from the given values (quat and translation)
44
+ *
45
+ * @param x1 X component
46
+ * @param y1 Y component
47
+ * @param z1 Z component
48
+ * @param w1 W component
49
+ * @param x2 X component (translation)
50
+ * @param y2 Y component (translation)
51
+ * @param z2 Z component (translation)
52
+ * @returns new dual quaternion
53
+ * @function
54
+ */
55
+ function fromRotationTranslationValues(x1, y1, z1, w1, x2, y2, z2) {
56
+ const ax = x2 * 0.5;
57
+ const ay = y2 * 0.5;
58
+ const az = z2 * 0.5;
59
+ return [
60
+ x1,
61
+ y1,
62
+ z1,
63
+ w1,
64
+ ax * w1 + ay * z1 - az * y1,
65
+ ay * w1 + az * x1 - ax * z1,
66
+ az * w1 + ax * y1 - ay * x1,
67
+ -ax * x1 - ay * y1 - az * z1,
68
+ ];
69
+ }
70
+ /**
71
+ * Creates a dual quat from a quaternion and a translation
72
+ *
73
+ * @param out dual quaternion receiving operation result
74
+ * @param q a normalized quaternion
75
+ * @param t translation vector
76
+ * @returns dual quaternion receiving operation result
77
+ * @function
78
+ */
79
+ function fromRotationTranslation(out, q, t) {
80
+ const ax = t[0] * 0.5;
81
+ const ay = t[1] * 0.5;
82
+ const az = t[2] * 0.5;
83
+ const bx = q[0];
84
+ const by = q[1];
85
+ const bz = q[2];
86
+ const bw = q[3];
87
+ out[0] = bx;
88
+ out[1] = by;
89
+ out[2] = bz;
90
+ out[3] = bw;
91
+ out[4] = ax * bw + ay * bz - az * by;
92
+ out[5] = ay * bw + az * bx - ax * bz;
93
+ out[6] = az * bw + ax * by - ay * bx;
94
+ out[7] = -ax * bx - ay * by - az * bz;
95
+ return out;
96
+ }
97
+ /**
98
+ * Creates a dual quat from a translation
99
+ *
100
+ * @param out dual quaternion receiving operation result
101
+ * @param t translation vector
102
+ * @returns dual quaternion receiving operation result
103
+ * @function
104
+ */
105
+ function fromTranslation(out, t) {
106
+ out[0] = 0;
107
+ out[1] = 0;
108
+ out[2] = 0;
109
+ out[3] = 1;
110
+ out[4] = t[0] * 0.5;
111
+ out[5] = t[1] * 0.5;
112
+ out[6] = t[2] * 0.5;
113
+ out[7] = 0;
114
+ return out;
115
+ }
116
+ /**
117
+ * Creates a dual quat from a quaternion
118
+ *
119
+ * @param out dual quaternion receiving operation result
120
+ * @param q the quaternion
121
+ * @returns dual quaternion receiving operation result
122
+ * @function
123
+ */
124
+ function fromRotation(out, q) {
125
+ out[0] = q[0];
126
+ out[1] = q[1];
127
+ out[2] = q[2];
128
+ out[3] = q[3];
129
+ out[4] = 0;
130
+ out[5] = 0;
131
+ out[6] = 0;
132
+ out[7] = 0;
133
+ return out;
134
+ }
135
+ /**
136
+ * Creates a new dual quat from a matrix (4x4)
137
+ *
138
+ * @param out the dual quaternion
139
+ * @param a the matrix
140
+ * @returns dual quat receiving operation result
141
+ */
142
+ function fromMat4(out, a) {
143
+ // Rotation: extract the quaternion from the (possibly scaled) upper-3x3.
144
+ // Inlined from mat4.getRotation/getScaling so no scratch quat or Vec3 is allocated.
145
+ const is1 = 1 / Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
146
+ const is2 = 1 / Math.sqrt(a[4] * a[4] + a[5] * a[5] + a[6] * a[6]);
147
+ const is3 = 1 / Math.sqrt(a[8] * a[8] + a[9] * a[9] + a[10] * a[10]);
148
+ const sm11 = a[0] * is1;
149
+ const sm12 = a[1] * is2;
150
+ const sm13 = a[2] * is3;
151
+ const sm21 = a[4] * is1;
152
+ const sm22 = a[5] * is2;
153
+ const sm23 = a[6] * is3;
154
+ const sm31 = a[8] * is1;
155
+ const sm32 = a[9] * is2;
156
+ const sm33 = a[10] * is3;
157
+ const trace = sm11 + sm22 + sm33;
158
+ let bx;
159
+ let by;
160
+ let bz;
161
+ let bw;
162
+ if (trace > 0) {
163
+ const S = Math.sqrt(trace + 1.0) * 2;
164
+ bw = 0.25 * S;
165
+ bx = (sm23 - sm32) / S;
166
+ by = (sm31 - sm13) / S;
167
+ bz = (sm12 - sm21) / S;
168
+ }
169
+ else if (sm11 > sm22 && sm11 > sm33) {
170
+ const S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
171
+ bw = (sm23 - sm32) / S;
172
+ bx = 0.25 * S;
173
+ by = (sm12 + sm21) / S;
174
+ bz = (sm31 + sm13) / S;
175
+ }
176
+ else if (sm22 > sm33) {
177
+ const S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
178
+ bw = (sm31 - sm13) / S;
179
+ bx = (sm12 + sm21) / S;
180
+ by = 0.25 * S;
181
+ bz = (sm23 + sm32) / S;
182
+ }
183
+ else {
184
+ const S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
185
+ bw = (sm12 - sm21) / S;
186
+ bx = (sm31 + sm13) / S;
187
+ by = (sm23 + sm32) / S;
188
+ bz = 0.25 * S;
189
+ }
190
+ out[0] = bx;
191
+ out[1] = by;
192
+ out[2] = bz;
193
+ out[3] = bw;
194
+ // Dual part from the translation (last column), inlined from fromRotationTranslation.
195
+ const ax = a[12] * 0.5;
196
+ const ay = a[13] * 0.5;
197
+ const az = a[14] * 0.5;
198
+ out[4] = ax * bw + ay * bz - az * by;
199
+ out[5] = ay * bw + az * bx - ax * bz;
200
+ out[6] = az * bw + ax * by - ay * bx;
201
+ out[7] = -ax * bx - ay * by - az * bz;
202
+ return out;
203
+ }
204
+ /**
205
+ * Copy the values from one dual quat to another
206
+ *
207
+ * @param out the receiving dual quaternion
208
+ * @param a the source dual quaternion
209
+ * @returns out
210
+ * @function
211
+ */
212
+ function copy(out, a) {
213
+ out[0] = a[0];
214
+ out[1] = a[1];
215
+ out[2] = a[2];
216
+ out[3] = a[3];
217
+ out[4] = a[4];
218
+ out[5] = a[5];
219
+ out[6] = a[6];
220
+ out[7] = a[7];
221
+ return out;
222
+ }
223
+ /**
224
+ * Set a dual quat to the identity dual quaternion
225
+ *
226
+ * @param out the receiving quaternion
227
+ * @returns out
228
+ */
229
+ function identity(out) {
230
+ out[0] = 0;
231
+ out[1] = 0;
232
+ out[2] = 0;
233
+ out[3] = 1;
234
+ out[4] = 0;
235
+ out[5] = 0;
236
+ out[6] = 0;
237
+ out[7] = 0;
238
+ return out;
239
+ }
240
+ /**
241
+ * Set the components of a dual quat to the given values
242
+ *
243
+ * @param out the receiving quaternion
244
+ * @param x1 X component
245
+ * @param y1 Y component
246
+ * @param z1 Z component
247
+ * @param w1 W component
248
+ * @param x2 X component
249
+ * @param y2 Y component
250
+ * @param z2 Z component
251
+ * @param w2 W component
252
+ * @returns out
253
+ * @function
254
+ */
255
+ function set(out, x1, y1, z1, w1, x2, y2, z2, w2) {
256
+ out[0] = x1;
257
+ out[1] = y1;
258
+ out[2] = z1;
259
+ out[3] = w1;
260
+ out[4] = x2;
261
+ out[5] = y2;
262
+ out[6] = z2;
263
+ out[7] = w2;
264
+ return out;
265
+ }
266
+ /**
267
+ * Gets the real part of a dual quat
268
+ * @param out real part
269
+ * @param a Dual Quaternion
270
+ * @return real part
271
+ */
272
+ function getReal(out, a) {
273
+ out[0] = a[0];
274
+ out[1] = a[1];
275
+ out[2] = a[2];
276
+ out[3] = a[3];
277
+ return out;
278
+ }
279
+ /**
280
+ * Gets the dual part of a dual quat
281
+ * @param out dual part
282
+ * @param a Dual Quaternion
283
+ * @return dual part
284
+ */
285
+ function getDual(out, a) {
286
+ out[0] = a[4];
287
+ out[1] = a[5];
288
+ out[2] = a[6];
289
+ out[3] = a[7];
290
+ return out;
291
+ }
292
+ /**
293
+ * Set the real component of a dual quat to the given quaternion
294
+ *
295
+ * @param out the receiving dual quaternion
296
+ * @param q a quaternion representing the real part
297
+ * @returns out
298
+ */
299
+ function setReal(out, q) {
300
+ out[0] = q[0];
301
+ out[1] = q[1];
302
+ out[2] = q[2];
303
+ out[3] = q[3];
304
+ return out;
305
+ }
306
+ /**
307
+ * Set the dual component of a dual quat to the given quaternion
308
+ *
309
+ * @param out the receiving quaternion
310
+ * @param q a quaternion representing the dual part
311
+ * @returns out
312
+ * @function
313
+ */
314
+ function setDual(out, q) {
315
+ out[4] = q[0];
316
+ out[5] = q[1];
317
+ out[6] = q[2];
318
+ out[7] = q[3];
319
+ return out;
320
+ }
321
+ /**
322
+ * Gets the translation of a normalized dual quat
323
+ * @param out translation
324
+ * @param a Dual Quaternion to be decomposed
325
+ * @return translation
326
+ */
327
+ function getTranslation(out, a) {
328
+ const ax = a[4];
329
+ const ay = a[5];
330
+ const az = a[6];
331
+ const aw = a[7];
332
+ const bx = -a[0];
333
+ const by = -a[1];
334
+ const bz = -a[2];
335
+ const bw = a[3];
336
+ out[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
337
+ out[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
338
+ out[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
339
+ return out;
340
+ }
341
+ /**
342
+ * Translates a dual quat by the given vector
343
+ *
344
+ * @param out the receiving dual quaternion
345
+ * @param a the dual quaternion to translate
346
+ * @param v vector to translate by
347
+ * @returns out
348
+ */
349
+ function translate(out, a, v) {
350
+ const ax1 = a[0];
351
+ const ay1 = a[1];
352
+ const az1 = a[2];
353
+ const aw1 = a[3];
354
+ const bx1 = v[0] * 0.5;
355
+ const by1 = v[1] * 0.5;
356
+ const bz1 = v[2] * 0.5;
357
+ const ax2 = a[4];
358
+ const ay2 = a[5];
359
+ const az2 = a[6];
360
+ const aw2 = a[7];
361
+ out[0] = ax1;
362
+ out[1] = ay1;
363
+ out[2] = az1;
364
+ out[3] = aw1;
365
+ out[4] = aw1 * bx1 + ay1 * bz1 - az1 * by1 + ax2;
366
+ out[5] = aw1 * by1 + az1 * bx1 - ax1 * bz1 + ay2;
367
+ out[6] = aw1 * bz1 + ax1 * by1 - ay1 * bx1 + az2;
368
+ out[7] = -ax1 * bx1 - ay1 * by1 - az1 * bz1 + aw2;
369
+ return out;
370
+ }
371
+ /**
372
+ * Rotates a dual quat around the X axis
373
+ *
374
+ * @param out the receiving dual quaternion
375
+ * @param a the dual quaternion to rotate
376
+ * @param rad how far should the rotation be
377
+ * @returns out
378
+ */
379
+ function rotateX(out, a, rad) {
380
+ let bx = -a[0];
381
+ let by = -a[1];
382
+ let bz = -a[2];
383
+ let bw = a[3];
384
+ const ax = a[4];
385
+ const ay = a[5];
386
+ const az = a[6];
387
+ const aw = a[7];
388
+ const ax1 = ax * bw + aw * bx + ay * bz - az * by;
389
+ const ay1 = ay * bw + aw * by + az * bx - ax * bz;
390
+ const az1 = az * bw + aw * bz + ax * by - ay * bx;
391
+ const aw1 = aw * bw - ax * bx - ay * by - az * bz;
392
+ // rotate the real (rotation) part about X — inlined quat.rotateX on out[0..3]
393
+ const half = rad * 0.5;
394
+ const s = Math.sin(half);
395
+ const c = Math.cos(half);
396
+ const rx = a[0];
397
+ const ry = a[1];
398
+ const rz = a[2];
399
+ const rw = a[3];
400
+ out[0] = rx * c + rw * s;
401
+ out[1] = ry * c + rz * s;
402
+ out[2] = rz * c - ry * s;
403
+ out[3] = rw * c - rx * s;
404
+ bx = out[0];
405
+ by = out[1];
406
+ bz = out[2];
407
+ bw = out[3];
408
+ out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
409
+ out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
410
+ out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
411
+ out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
412
+ return out;
413
+ }
414
+ /**
415
+ * Rotates a dual quat around the Y axis
416
+ *
417
+ * @param out the receiving dual quaternion
418
+ * @param a the dual quaternion to rotate
419
+ * @param rad how far should the rotation be
420
+ * @returns out
421
+ */
422
+ function rotateY(out, a, rad) {
423
+ let bx = -a[0];
424
+ let by = -a[1];
425
+ let bz = -a[2];
426
+ let bw = a[3];
427
+ const ax = a[4];
428
+ const ay = a[5];
429
+ const az = a[6];
430
+ const aw = a[7];
431
+ const ax1 = ax * bw + aw * bx + ay * bz - az * by;
432
+ const ay1 = ay * bw + aw * by + az * bx - ax * bz;
433
+ const az1 = az * bw + aw * bz + ax * by - ay * bx;
434
+ const aw1 = aw * bw - ax * bx - ay * by - az * bz;
435
+ // rotate the real (rotation) part about Y — inlined quat.rotateY on out[0..3]
436
+ const half = rad * 0.5;
437
+ const s = Math.sin(half);
438
+ const c = Math.cos(half);
439
+ const rx = a[0];
440
+ const ry = a[1];
441
+ const rz = a[2];
442
+ const rw = a[3];
443
+ out[0] = rx * c - rz * s;
444
+ out[1] = ry * c + rw * s;
445
+ out[2] = rz * c + rx * s;
446
+ out[3] = rw * c - ry * s;
447
+ bx = out[0];
448
+ by = out[1];
449
+ bz = out[2];
450
+ bw = out[3];
451
+ out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
452
+ out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
453
+ out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
454
+ out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
455
+ return out;
456
+ }
457
+ /**
458
+ * Rotates a dual quat around the Z axis
459
+ *
460
+ * @param out the receiving dual quaternion
461
+ * @param a the dual quaternion to rotate
462
+ * @param rad how far should the rotation be
463
+ * @returns out
464
+ */
465
+ function rotateZ(out, a, rad) {
466
+ let bx = -a[0];
467
+ let by = -a[1];
468
+ let bz = -a[2];
469
+ let bw = a[3];
470
+ const ax = a[4];
471
+ const ay = a[5];
472
+ const az = a[6];
473
+ const aw = a[7];
474
+ const ax1 = ax * bw + aw * bx + ay * bz - az * by;
475
+ const ay1 = ay * bw + aw * by + az * bx - ax * bz;
476
+ const az1 = az * bw + aw * bz + ax * by - ay * bx;
477
+ const aw1 = aw * bw - ax * bx - ay * by - az * bz;
478
+ // rotate the real (rotation) part about Z — inlined quat.rotateZ on out[0..3]
479
+ const half = rad * 0.5;
480
+ const s = Math.sin(half);
481
+ const c = Math.cos(half);
482
+ const rx = a[0];
483
+ const ry = a[1];
484
+ const rz = a[2];
485
+ const rw = a[3];
486
+ out[0] = rx * c + ry * s;
487
+ out[1] = ry * c - rx * s;
488
+ out[2] = rz * c + rw * s;
489
+ out[3] = rw * c - rz * s;
490
+ bx = out[0];
491
+ by = out[1];
492
+ bz = out[2];
493
+ bw = out[3];
494
+ out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
495
+ out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
496
+ out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
497
+ out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
498
+ return out;
499
+ }
500
+ /**
501
+ * Rotates a dual quat by a given quaternion (a * q)
502
+ *
503
+ * @param out the receiving dual quaternion
504
+ * @param a the dual quaternion to rotate
505
+ * @param q quaternion to rotate by
506
+ * @returns out
507
+ */
508
+ function rotateByQuatAppend(out, a, q) {
509
+ const qx = q[0];
510
+ const qy = q[1];
511
+ const qz = q[2];
512
+ const qw = q[3];
513
+ let ax = a[0];
514
+ let ay = a[1];
515
+ let az = a[2];
516
+ let aw = a[3];
517
+ out[0] = ax * qw + aw * qx + ay * qz - az * qy;
518
+ out[1] = ay * qw + aw * qy + az * qx - ax * qz;
519
+ out[2] = az * qw + aw * qz + ax * qy - ay * qx;
520
+ out[3] = aw * qw - ax * qx - ay * qy - az * qz;
521
+ ax = a[4];
522
+ ay = a[5];
523
+ az = a[6];
524
+ aw = a[7];
525
+ out[4] = ax * qw + aw * qx + ay * qz - az * qy;
526
+ out[5] = ay * qw + aw * qy + az * qx - ax * qz;
527
+ out[6] = az * qw + aw * qz + ax * qy - ay * qx;
528
+ out[7] = aw * qw - ax * qx - ay * qy - az * qz;
529
+ return out;
530
+ }
531
+ /**
532
+ * Rotates a dual quat by a given quaternion (q * a)
533
+ *
534
+ * @param out the receiving dual quaternion
535
+ * @param q quaternion to rotate by
536
+ * @param a the dual quaternion to rotate
537
+ * @returns out
538
+ */
539
+ function rotateByQuatPrepend(out, q, a) {
540
+ const qx = q[0];
541
+ const qy = q[1];
542
+ const qz = q[2];
543
+ const qw = q[3];
544
+ let bx = a[0];
545
+ let by = a[1];
546
+ let bz = a[2];
547
+ let bw = a[3];
548
+ out[0] = qx * bw + qw * bx + qy * bz - qz * by;
549
+ out[1] = qy * bw + qw * by + qz * bx - qx * bz;
550
+ out[2] = qz * bw + qw * bz + qx * by - qy * bx;
551
+ out[3] = qw * bw - qx * bx - qy * by - qz * bz;
552
+ bx = a[4];
553
+ by = a[5];
554
+ bz = a[6];
555
+ bw = a[7];
556
+ out[4] = qx * bw + qw * bx + qy * bz - qz * by;
557
+ out[5] = qy * bw + qw * by + qz * bx - qx * bz;
558
+ out[6] = qz * bw + qw * bz + qx * by - qy * bx;
559
+ out[7] = qw * bw - qx * bx - qy * by - qz * bz;
560
+ return out;
561
+ }
562
+ /**
563
+ * Rotates a dual quat around a given axis. Does the normalisation automatically
564
+ *
565
+ * @param out the receiving dual quaternion
566
+ * @param a the dual quaternion to rotate
567
+ * @param axis the axis to rotate around
568
+ * @param rad how far the rotation should be
569
+ * @returns out
570
+ */
571
+ function rotateAroundAxis(out, a, axis, rad) {
572
+ //Special case for rad = 0
573
+ if (Math.abs(rad) < EPSILON) {
574
+ return copy(out, a);
575
+ }
576
+ const axisLength = Math.sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
577
+ rad = rad * 0.5;
578
+ const s = Math.sin(rad);
579
+ const bx = (s * axis[0]) / axisLength;
580
+ const by = (s * axis[1]) / axisLength;
581
+ const bz = (s * axis[2]) / axisLength;
582
+ const bw = Math.cos(rad);
583
+ const ax1 = a[0];
584
+ const ay1 = a[1];
585
+ const az1 = a[2];
586
+ const aw1 = a[3];
587
+ out[0] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
588
+ out[1] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
589
+ out[2] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
590
+ out[3] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
591
+ const ax = a[4];
592
+ const ay = a[5];
593
+ const az = a[6];
594
+ const aw = a[7];
595
+ out[4] = ax * bw + aw * bx + ay * bz - az * by;
596
+ out[5] = ay * bw + aw * by + az * bx - ax * bz;
597
+ out[6] = az * bw + aw * bz + ax * by - ay * bx;
598
+ out[7] = aw * bw - ax * bx - ay * by - az * bz;
599
+ return out;
600
+ }
601
+ /**
602
+ * Adds two dual quat's
603
+ *
604
+ * @param out the receiving dual quaternion
605
+ * @param a the first operand
606
+ * @param b the second operand
607
+ * @returns out
608
+ * @function
609
+ */
610
+ function add(out, a, b) {
611
+ out[0] = a[0] + b[0];
612
+ out[1] = a[1] + b[1];
613
+ out[2] = a[2] + b[2];
614
+ out[3] = a[3] + b[3];
615
+ out[4] = a[4] + b[4];
616
+ out[5] = a[5] + b[5];
617
+ out[6] = a[6] + b[6];
618
+ out[7] = a[7] + b[7];
619
+ return out;
620
+ }
621
+ /**
622
+ * Multiplies two dual quat's
623
+ *
624
+ * @param out the receiving dual quaternion
625
+ * @param a the first operand
626
+ * @param b the second operand
627
+ * @returns out
628
+ */
629
+ function multiply(out, a, b) {
630
+ const ax0 = a[0];
631
+ const ay0 = a[1];
632
+ const az0 = a[2];
633
+ const aw0 = a[3];
634
+ const bx1 = b[4];
635
+ const by1 = b[5];
636
+ const bz1 = b[6];
637
+ const bw1 = b[7];
638
+ const ax1 = a[4];
639
+ const ay1 = a[5];
640
+ const az1 = a[6];
641
+ const aw1 = a[7];
642
+ const bx0 = b[0];
643
+ const by0 = b[1];
644
+ const bz0 = b[2];
645
+ const bw0 = b[3];
646
+ out[0] = ax0 * bw0 + aw0 * bx0 + ay0 * bz0 - az0 * by0;
647
+ out[1] = ay0 * bw0 + aw0 * by0 + az0 * bx0 - ax0 * bz0;
648
+ out[2] = az0 * bw0 + aw0 * bz0 + ax0 * by0 - ay0 * bx0;
649
+ out[3] = aw0 * bw0 - ax0 * bx0 - ay0 * by0 - az0 * bz0;
650
+ out[4] = ax0 * bw1 + aw0 * bx1 + ay0 * bz1 - az0 * by1 + ax1 * bw0 + aw1 * bx0 + ay1 * bz0 - az1 * by0;
651
+ out[5] = ay0 * bw1 + aw0 * by1 + az0 * bx1 - ax0 * bz1 + ay1 * bw0 + aw1 * by0 + az1 * bx0 - ax1 * bz0;
652
+ out[6] = az0 * bw1 + aw0 * bz1 + ax0 * by1 - ay0 * bx1 + az1 * bw0 + aw1 * bz0 + ax1 * by0 - ay1 * bx0;
653
+ out[7] = aw0 * bw1 - ax0 * bx1 - ay0 * by1 - az0 * bz1 + aw1 * bw0 - ax1 * bx0 - ay1 * by0 - az1 * bz0;
654
+ return out;
655
+ }
656
+ /**
657
+ * Alias for {@link quat2.multiply}
658
+ * @function
659
+ */
660
+ const mul = multiply;
661
+ /**
662
+ * Scales a dual quat by a scalar number
663
+ *
664
+ * @param out the receiving dual quat
665
+ * @param a the dual quat to scale
666
+ * @param b amount to scale the dual quat by
667
+ * @returns out
668
+ * @function
669
+ */
670
+ function scale(out, a, b) {
671
+ out[0] = a[0] * b;
672
+ out[1] = a[1] * b;
673
+ out[2] = a[2] * b;
674
+ out[3] = a[3] * b;
675
+ out[4] = a[4] * b;
676
+ out[5] = a[5] * b;
677
+ out[6] = a[6] * b;
678
+ out[7] = a[7] * b;
679
+ return out;
680
+ }
681
+ /**
682
+ * Calculates the dot product of two dual quat's (The dot product of the real parts)
683
+ *
684
+ * @param a the first operand
685
+ * @param b the second operand
686
+ * @returns dot product of a and b
687
+ */
688
+ function dot(a, b) {
689
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
690
+ }
691
+ /**
692
+ * Performs a linear interpolation between two dual quats's
693
+ * NOTE: The resulting dual quaternions won't always be normalized (The error is most noticeable when t = 0.5)
694
+ *
695
+ * @param out the receiving dual quat
696
+ * @param a the first operand
697
+ * @param b the second operand
698
+ * @param t interpolation amount, in the range [0-1], between the two inputs
699
+ * @returns out
700
+ */
701
+ function lerp(out, a, b, t) {
702
+ const mt = 1 - t;
703
+ // dot of the real (rotation) parts, matching quat2.dot
704
+ if (a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3] < 0)
705
+ t = -t;
706
+ out[0] = a[0] * mt + b[0] * t;
707
+ out[1] = a[1] * mt + b[1] * t;
708
+ out[2] = a[2] * mt + b[2] * t;
709
+ out[3] = a[3] * mt + b[3] * t;
710
+ out[4] = a[4] * mt + b[4] * t;
711
+ out[5] = a[5] * mt + b[5] * t;
712
+ out[6] = a[6] * mt + b[6] * t;
713
+ out[7] = a[7] * mt + b[7] * t;
714
+ return out;
715
+ }
716
+ /**
717
+ * Calculates the inverse of a dual quat. If they are normalized, conjugate is cheaper
718
+ *
719
+ * @param out the receiving dual quaternion
720
+ * @param a dual quat to calculate inverse of
721
+ * @returns out
722
+ */
723
+ function invert(out, a) {
724
+ const sqlen = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
725
+ out[0] = -a[0] / sqlen;
726
+ out[1] = -a[1] / sqlen;
727
+ out[2] = -a[2] / sqlen;
728
+ out[3] = a[3] / sqlen;
729
+ out[4] = -a[4] / sqlen;
730
+ out[5] = -a[5] / sqlen;
731
+ out[6] = -a[6] / sqlen;
732
+ out[7] = a[7] / sqlen;
733
+ return out;
734
+ }
735
+ /**
736
+ * Calculates the conjugate of a dual quat
737
+ * If the dual quaternion is normalized, this function is faster than quat2.inverse and produces the same result.
738
+ *
739
+ * @param out the receiving quaternion
740
+ * @param a quat to calculate conjugate of
741
+ * @returns out
742
+ */
743
+ function conjugate(out, a) {
744
+ out[0] = -a[0];
745
+ out[1] = -a[1];
746
+ out[2] = -a[2];
747
+ out[3] = a[3];
748
+ out[4] = -a[4];
749
+ out[5] = -a[5];
750
+ out[6] = -a[6];
751
+ out[7] = a[7];
752
+ return out;
753
+ }
754
+ /**
755
+ * Calculates the length of a dual quat (the length of its real/rotation part)
756
+ *
757
+ * @param a dual quat to calculate length of
758
+ * @returns length of a
759
+ */
760
+ function length(a) {
761
+ const x = a[0];
762
+ const y = a[1];
763
+ const z = a[2];
764
+ const w = a[3];
765
+ return Math.sqrt(x * x + y * y + z * z + w * w);
766
+ }
767
+ /**
768
+ * Alias for {@link quat2.length}
769
+ * @function
770
+ */
771
+ const len = length;
772
+ /**
773
+ * Calculates the squared length of a dual quat (the squared length of its real/rotation part)
774
+ *
775
+ * @param a dual quat to calculate squared length of
776
+ * @returns squared length of a
777
+ */
778
+ function squaredLength(a) {
779
+ const x = a[0];
780
+ const y = a[1];
781
+ const z = a[2];
782
+ const w = a[3];
783
+ return x * x + y * y + z * z + w * w;
784
+ }
785
+ /**
786
+ * Alias for {@link quat2.squaredLength}
787
+ * @function
788
+ */
789
+ const sqrLen = squaredLength;
790
+ /**
791
+ * Normalize a dual quat
792
+ *
793
+ * @param out the receiving dual quaternion
794
+ * @param a dual quaternion to normalize
795
+ * @returns out
796
+ * @function
797
+ */
798
+ function normalize(out, a) {
799
+ let magnitude = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
800
+ if (magnitude > 0) {
801
+ magnitude = Math.sqrt(magnitude);
802
+ const a0 = a[0] / magnitude;
803
+ const a1 = a[1] / magnitude;
804
+ const a2 = a[2] / magnitude;
805
+ const a3 = a[3] / magnitude;
806
+ const b0 = a[4];
807
+ const b1 = a[5];
808
+ const b2 = a[6];
809
+ const b3 = a[7];
810
+ const a_dot_b = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
811
+ out[0] = a0;
812
+ out[1] = a1;
813
+ out[2] = a2;
814
+ out[3] = a3;
815
+ out[4] = (b0 - a0 * a_dot_b) / magnitude;
816
+ out[5] = (b1 - a1 * a_dot_b) / magnitude;
817
+ out[6] = (b2 - a2 * a_dot_b) / magnitude;
818
+ out[7] = (b3 - a3 * a_dot_b) / magnitude;
819
+ }
820
+ return out;
821
+ }
822
+ /**
823
+ * Returns a string representation of a dual quaternion
824
+ *
825
+ * @param a dual quaternion to represent as a string
826
+ * @returns string representation of the dual quat
827
+ */
828
+ function str(a) {
829
+ return `quat2(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]}, ${a[4]}, ${a[5]}, ${a[6]}, ${a[7]})`;
830
+ }
831
+ /**
832
+ * Returns whether or not the dual quaternions have exactly the same elements in the same position (when compared with ===)
833
+ *
834
+ * @param a the first dual quaternion.
835
+ * @param b the second dual quaternion.
836
+ * @returns true if the dual quaternions are equal, false otherwise.
837
+ */
838
+ function exactEquals(a, b) {
839
+ return (a[0] === b[0] &&
840
+ a[1] === b[1] &&
841
+ a[2] === b[2] &&
842
+ a[3] === b[3] &&
843
+ a[4] === b[4] &&
844
+ a[5] === b[5] &&
845
+ a[6] === b[6] &&
846
+ a[7] === b[7]);
847
+ }
848
+ /**
849
+ * Returns whether or not the dual quaternions have approximately the same elements in the same position.
850
+ *
851
+ * @param a the first dual quat.
852
+ * @param b the second dual quat.
853
+ * @returns true if the dual quats are equal, false otherwise.
854
+ */
855
+ function equals(a, b) {
856
+ const a0 = a[0];
857
+ const a1 = a[1];
858
+ const a2 = a[2];
859
+ const a3 = a[3];
860
+ const a4 = a[4];
861
+ const a5 = a[5];
862
+ const a6 = a[6];
863
+ const a7 = a[7];
864
+ const b0 = b[0];
865
+ const b1 = b[1];
866
+ const b2 = b[2];
867
+ const b3 = b[3];
868
+ const b4 = b[4];
869
+ const b5 = b[5];
870
+ const b6 = b[6];
871
+ const b7 = b[7];
872
+ return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
873
+ Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
874
+ Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
875
+ Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
876
+ Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
877
+ Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &&
878
+ Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &&
879
+ Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)));
880
+ }
881
+
882
+ export { add, clone, conjugate, copy, create, dot, equals, exactEquals, fromMat4, fromRotation, fromRotationTranslation, fromRotationTranslationValues, fromTranslation, fromValues, getDual, getReal, getTranslation, identity, invert, len, length, lerp, mul, multiply, normalize, rotateAroundAxis, rotateByQuatAppend, rotateByQuatPrepend, rotateX, rotateY, rotateZ, scale, set, setDual, setReal, sqrLen, squaredLength, str, translate };
883
+ //# sourceMappingURL=quat2.js.map