@carbonenginejs/runtime-utils 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 (101) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +23 -0
  3. package/README.md +56 -0
  4. package/THIRD-PARTY-NOTICES.md +38 -0
  5. package/docs/README.md +81 -0
  6. package/docs/architecture.md +101 -0
  7. package/docs/concepts/foundation-consolidation.md +86 -0
  8. package/docs/const-kb.md +92 -0
  9. package/docs/core-types/DECORATOR-TODOS.md +25 -0
  10. package/docs/core-types/README.md +203 -0
  11. package/docs/reference/api.md +70 -0
  12. package/docs/reference/classes/README.md +115 -0
  13. package/package.json +132 -0
  14. package/src/arrays.js +5 -0
  15. package/src/audio/audioFormats.js +34 -0
  16. package/src/audio/index.js +1 -0
  17. package/src/box3.js +1385 -0
  18. package/src/bytes.js +56 -0
  19. package/src/compression.js +56 -0
  20. package/src/constants/index.js +6 -0
  21. package/src/constants.js +15 -0
  22. package/src/curve.js +419 -0
  23. package/src/d3d/dxgiFormats.js +46 -0
  24. package/src/d3d/index.js +2 -0
  25. package/src/d3d/primitiveTopology.js +11 -0
  26. package/src/document/CjsCarbonDocument.js +212 -0
  27. package/src/document/CjsClassRegistry.js +373 -0
  28. package/src/document/CjsDocumentDehydrator.js +142 -0
  29. package/src/document/CjsDocumentHydrator.js +156 -0
  30. package/src/document/CjsStructRegistry.js +348 -0
  31. package/src/document/hydrationAdapter.js +129 -0
  32. package/src/document/index.js +6 -0
  33. package/src/geometry/box.js +137 -0
  34. package/src/geometry/cylinder.js +244 -0
  35. package/src/geometry/helpers/LICENSE +15 -0
  36. package/src/geometry/helpers/earcut.js +766 -0
  37. package/src/geometry/helpers/misc.js +103 -0
  38. package/src/geometry/index.js +8 -0
  39. package/src/geometry/json.js +165 -0
  40. package/src/geometry/lathe.js +172 -0
  41. package/src/geometry/octahedron.js +0 -0
  42. package/src/geometry/plane.js +81 -0
  43. package/src/geometry/shape.js +95 -0
  44. package/src/geometry/sphere.js +123 -0
  45. package/src/geometry/torus.js +96 -0
  46. package/src/graphics/colorSpaces.js +22 -0
  47. package/src/graphics/index.js +4 -0
  48. package/src/graphics/pixelFormats.js +158 -0
  49. package/src/graphics/textureDimensions.js +22 -0
  50. package/src/graphics/trinityEnums.js +87 -0
  51. package/src/index.js +58 -0
  52. package/src/is.js +524 -0
  53. package/src/json.js +23 -0
  54. package/src/lifecycle/CjsLifecycleState.js +77 -0
  55. package/src/lifecycle/index.js +1 -0
  56. package/src/lne3.js +497 -0
  57. package/src/lookup.js +48 -0
  58. package/src/mat3.js +131 -0
  59. package/src/mat4.js +699 -0
  60. package/src/math/index.js +25 -0
  61. package/src/math/scalar.js +63 -0
  62. package/src/media/index.js +1 -0
  63. package/src/media/mediaTypes.js +50 -0
  64. package/src/mesh.js +394 -0
  65. package/src/model/CjsEventEmitter.js +333 -0
  66. package/src/model/CjsModel.js +1544 -0
  67. package/src/model/CjsModelState.js +72 -0
  68. package/src/model/index.js +4 -0
  69. package/src/model/sourceRecordUtils.js +54 -0
  70. package/src/noise.js +310 -0
  71. package/src/num.js +827 -0
  72. package/src/path.js +21 -0
  73. package/src/pln.js +762 -0
  74. package/src/pool.js +160 -0
  75. package/src/quat.js +144 -0
  76. package/src/ray3.js +1085 -0
  77. package/src/renderContext/formats.js +145 -0
  78. package/src/renderContext/index.js +5 -0
  79. package/src/renderContext/presentation.js +125 -0
  80. package/src/renderContext/resources.js +27 -0
  81. package/src/renderContext/upscaling.js +22 -0
  82. package/src/renderContext/window.js +20 -0
  83. package/src/runtime/CjsRuntimeState.js +50 -0
  84. package/src/schema/CjsSchema.js +1009 -0
  85. package/src/schema/index.js +17 -0
  86. package/src/shader/index.js +1 -0
  87. package/src/shader/shaderStages.js +37 -0
  88. package/src/sph3.js +754 -0
  89. package/src/tangent.js +288 -0
  90. package/src/text.js +40 -0
  91. package/src/tri3.js +650 -0
  92. package/src/types/carbonTypes.js +635 -0
  93. package/src/types/index.js +2 -0
  94. package/src/utils.js +58 -0
  95. package/src/validation.js +46 -0
  96. package/src/vec2.js +229 -0
  97. package/src/vec3.js +1172 -0
  98. package/src/vec4.js +347 -0
  99. package/src/vertex.js +108 -0
  100. package/src/webgpu/index.js +1 -0
  101. package/src/webgpu/textureFormats.js +121 -0
package/src/sph3.js ADDED
@@ -0,0 +1,754 @@
1
+ import { vec3 } from "./vec3.js";
2
+ import { vec4 } from "./vec4.js";
3
+ import { mat4 } from "./mat4.js";
4
+ import { pln } from "./pln.js";
5
+ import { box3 } from "./box3.js";
6
+
7
+ /**
8
+ * 3D Sphere
9
+ * @typedef {Float32Array} sph3
10
+ */
11
+
12
+ export const sph3 = {};
13
+
14
+ // Scratch
15
+ let sph3_0 = null;
16
+ let box3_0 = null;
17
+
18
+ /**
19
+ * Allocates a pooled sph3
20
+ * @returns {Float32Array|sph3}
21
+ */
22
+ sph3.alloc = vec4.alloc;
23
+
24
+ /**
25
+ * Unallocates a pooled sph3
26
+ * @param {sph3|Float32Array} a
27
+ */
28
+ sph3.unalloc = vec4.unalloc;
29
+
30
+ /**
31
+ * Returns a subarray containing the position component of the sph3
32
+ * - Why does webpack fail to resolve this if referencing pln.normal?
33
+ *
34
+ * @param {sph3} a
35
+ * @returns {sph3}
36
+ */
37
+ sph3.$position = function(a)
38
+ {
39
+ return a.subarray(0, 3);
40
+ };
41
+
42
+ /**
43
+ * Clones a sphere
44
+ *
45
+ * @param {sph3} a
46
+ * @returns {sph3}
47
+ */
48
+ sph3.clone = vec4.clone;
49
+
50
+ /**
51
+ * Checks if a sphere contains a point
52
+ *
53
+ * @param {sph3} a
54
+ * @param {vec3} p
55
+ * @returns {boolean}
56
+ */
57
+ sph3.containsPoint = function(a, p)
58
+ {
59
+ if (sph3.isEmpty(a)) return false;
60
+
61
+ let x = p[0] - a[0],
62
+ y = p[1] - a[1],
63
+ z = p[2] - a[2];
64
+
65
+ return (x * x + y * y + z * z) <= (a[3] * a[3]);
66
+ };
67
+
68
+ /**
69
+ * Copies a sphere
70
+ *
71
+ * @param {sph3} a
72
+ * @param {sph3} b
73
+ * @returns {sph3}
74
+ */
75
+ sph3.copy = vec4.copy;
76
+
77
+ /**
78
+ * Creates a sphere
79
+ *
80
+ * @returns {sph3}
81
+ */
82
+ sph3.create = function()
83
+ {
84
+ return new Float32Array([ 0, 0, 0, -1 ]);
85
+ };
86
+
87
+ /**
88
+ * Returns the distance between two spheres
89
+ *
90
+ * @param {sph3} a
91
+ * @param {sph3} b
92
+ * @returns {number}
93
+ */
94
+ sph3.distance = function(a, b)
95
+ {
96
+ if (sph3.isEmpty(a) || sph3.isEmpty(b)) return Infinity;
97
+
98
+ let x = b[0] - a[0],
99
+ y = b[1] - a[1],
100
+ z = b[2] - a[2],
101
+ rii = b[3] + a[3];
102
+
103
+ return Math.sqrt(x * x + y * y + z * z) - rii;
104
+ };
105
+
106
+ /**
107
+ * Returns the distance from a sphere to a given point
108
+ *
109
+ * @param {sph3} a
110
+ * @param {vec3} p
111
+ * @returns {number}
112
+ */
113
+ sph3.distanceToPoint = function(a, p)
114
+ {
115
+ if (sph3.isEmpty(a)) return Infinity;
116
+
117
+ let x = p[0] - a[0],
118
+ y = p[1] - a[1],
119
+ z = p[2] - a[2];
120
+
121
+ return Math.sqrt(x * x + y * y + z * z) - a[3];
122
+ };
123
+
124
+ /**
125
+ * Empties a sphere
126
+ * @param {sph3} a
127
+ * @returns {sph3} a
128
+ */
129
+ sph3.empty = function(a)
130
+ {
131
+ a[0] = 0;
132
+ a[1] = 0;
133
+ a[2] = 0;
134
+ a[3] = -1;
135
+ return a;
136
+ };
137
+
138
+ /**
139
+ * Compares two sphere's for equality
140
+ *
141
+ * @param {sph3} a
142
+ * @param {sph3} b
143
+ * @returns {boolean}
144
+ */
145
+ sph3.equals = vec4.equals;
146
+
147
+ /**
148
+ * Compares a sphere to spherical components for equality
149
+ *
150
+ * @param {sph3} a
151
+ * @param {vec3} position
152
+ * @param {number} radius
153
+ * @returns {boolean}
154
+ */
155
+ sph3.equalsPositionRadius = pln.equalsNormalConstant;
156
+
157
+ /**
158
+ * Compares two sphere for exact equality
159
+ *
160
+ * @param {sph3} a
161
+ * @param {sph3} b
162
+ * @returns {boolean}
163
+ */
164
+ sph3.exactEquals = vec4.exactEquals;
165
+
166
+ /**
167
+ * Compares a sphere to spherical components for exact equality
168
+ *
169
+ * @param {sph3} a
170
+ * @param {vec3} position
171
+ * @param {number} radius
172
+ * @returns {boolean}
173
+ */
174
+ sph3.exactEqualsPositionRadius = pln.exactEqualsNormalConstant;
175
+
176
+ /**
177
+ * Extracts a spheres's components
178
+ *
179
+ * @param {sph3} a
180
+ * @param {vec3} outPosition
181
+ * @returns {number}
182
+ */
183
+ sph3.extract = pln.extract;
184
+
185
+ /**
186
+ * Sets a sphere from a box3
187
+ *
188
+ * @param {sph3} out
189
+ * @param {box3} b
190
+ * @return {sph3} out
191
+ */
192
+ sph3.fromBox3 = function(out, b)
193
+ {
194
+ if (box3.isEmpty(b)) return sph3.empty(out);
195
+
196
+ let sX = b[3] - b[0],
197
+ sY = b[4] - b[1],
198
+ sZ = b[5] - b[2];
199
+
200
+ out[0] = (b[0] + b[3]) * 0.5;
201
+ out[1] = (b[1] + b[4]) * 0.5;
202
+ out[2] = (b[2] + b[5]) * 0.5;
203
+
204
+ out[3] = Math.sqrt(sX * sX + sY * sY + sZ * sZ) * 0.5;
205
+ return out;
206
+ };
207
+
208
+ /**
209
+ * Sets a sphere from a box's bounds
210
+ *
211
+ * @param {sph3} out
212
+ * @param {vec3} min
213
+ * @param {vec3} max
214
+ * @returns {sph3}
215
+ */
216
+ sph3.fromBounds = function(out, min, max)
217
+ {
218
+ let sX = max[0] - min[0],
219
+ sY = max[1] - min[1],
220
+ sZ = max[2] - min[2];
221
+
222
+ out[0] = (min[0] + max[0]) * 0.5;
223
+ out[1] = (min[1] + max[1]) * 0.5;
224
+ out[2] = (min[2] + max[2]) * 0.5;
225
+
226
+ out[3] = Math.sqrt(sX * sX + sY * sY + sZ * sZ) * 0.5;
227
+ return out;
228
+ };
229
+
230
+ /**
231
+ * Sets a sphere from position and radius
232
+ *
233
+ * @param {sph3} out
234
+ * @param {vec3} p
235
+ * @param {number} r
236
+ * @returns {sph3} out
237
+ */
238
+ sph3.fromPositionRadius = pln.fromNormalConstant;
239
+
240
+ sph3.from = sph3.fromPositionRadius;
241
+
242
+ /**
243
+ * Sets a sphere from a mat4's translation and a given radius
244
+ *
245
+ * @param {sph3} out
246
+ * @param {(mat4|vec3)} a
247
+ * @param {number} radius
248
+ * @returns {sph3} out
249
+ */
250
+ sph3.fromTranslationRadius = function(out, a, radius)
251
+ {
252
+ const offset = a.length >= 15 ? 12 : 0;
253
+ out[0] = a[offset];
254
+ out[1] = a[offset + 1];
255
+ out[2] = a[offset + 2];
256
+ out[3] = radius;
257
+ return out;
258
+ };
259
+
260
+ /**
261
+ * Gets a sphere from a mat4's translation and max axis scale
262
+ *
263
+ * @param {sph3} out
264
+ * @param {mat4} m
265
+ * @returns {sph3} out
266
+ */
267
+ sph3.fromMat4 = function(out, m)
268
+ {
269
+ out[0] = m[12];
270
+ out[1] = m[13];
271
+ out[2] = m[14];
272
+ out[3] = mat4.maxScaleOnAxis(m);
273
+ return out;
274
+ };
275
+
276
+ let aPosition,
277
+ bPosition,
278
+ toAPosition,
279
+ toBPosition,
280
+ center;
281
+
282
+ /**
283
+ * Gets the union of two spheres
284
+ * @param {sph3} out
285
+ * @param {sph3} a
286
+ * @param {sph3} b
287
+ * @returns {sph3} out
288
+ */
289
+ sph3.union = function(out, a, b)
290
+ {
291
+ if (sph3.isEmpty(a)) return sph3.copy(out, b);
292
+ if (sph3.isEmpty(b)) return sph3.copy(out, a);
293
+
294
+ if (!aPosition)
295
+ {
296
+ aPosition = vec3.create();
297
+ bPosition = vec3.create();
298
+ toAPosition = vec3.create();
299
+ toBPosition = vec3.create();
300
+ }
301
+
302
+ const
303
+ aRadius = sph3.extract(a, aPosition),
304
+ bRadius = sph3.extract(b, bPosition);
305
+
306
+ vec3.subtract(toBPosition, bPosition, aPosition);
307
+ const separation = vec3.length(toBPosition);
308
+
309
+ if (aRadius >= separation + bRadius)
310
+ {
311
+ return out === a ? out : sph3.copy(out, a);
312
+ }
313
+
314
+ if (bRadius >= separation + aRadius)
315
+ {
316
+ return out === b ? out : sph3.copy(out, b);
317
+ }
318
+
319
+ if (!center) center = vec3.create();
320
+ const halfDistance = (aRadius + separation + bRadius) * 0.5;
321
+ vec3.scale(center, toBPosition, (-aRadius + halfDistance) / separation);
322
+ vec3.add(center, aPosition, center);
323
+ return sph3.fromPositionRadius(out, center, halfDistance);
324
+ };
325
+
326
+ /**
327
+ * Gets the union of a sphere and a sphere's components
328
+ * @param {sph3} out
329
+ * @param {sph3} a
330
+ * @param {vec3} position
331
+ * @param {Number} radius
332
+ * @returns {sph3}
333
+ */
334
+ sph3.unionPositionRadius = function(out, a, position, radius)
335
+ {
336
+ if (!sph3_0) sph3_0 = sph3.create();
337
+ return sph3.union(out, a, sph3.fromPositionRadius(sph3_0, position, radius));
338
+ };
339
+
340
+ /**
341
+ * Gets a point clamped to the sphere
342
+ *
343
+ * @author three.js authors (conversion)
344
+ * @param {vec3} out
345
+ * @param {sph3} a
346
+ * @param {vec3} p
347
+ * @returns {vec3} out
348
+ */
349
+ sph3.getClampedPoint = function(out, a, p)
350
+ {
351
+ out[0] = p[0];
352
+ out[1] = p[1];
353
+ out[2] = p[2];
354
+
355
+ if (sph3.isEmpty(a)) return out;
356
+
357
+ let x = a[0] - p[0],
358
+ y = a[1] - p[1],
359
+ z = a[2] - p[2];
360
+
361
+ if ((x * x + y * y + z * z) > (a[3] * a[3]))
362
+ {
363
+ out[0] = out[0] - a[0];
364
+ out[1] = out[1] - a[1];
365
+ out[2] = out[2] - a[2];
366
+
367
+ vec3.normalize(out, out);
368
+
369
+ out[0] = out[0] * a[3] + a[0];
370
+ out[1] = out[1] * a[3] + a[1];
371
+ out[2] = out[2] * a[3] + a[2];
372
+ }
373
+
374
+ return out;
375
+ };
376
+
377
+ /**
378
+ * Gets the position of a point on a sphere from longitude and latitude
379
+ *
380
+ * @param {vec3} out
381
+ * @param {sph3} a
382
+ * @param {number} longitude
383
+ * @param {number} latitude
384
+ * @returns {vec3} out
385
+ */
386
+ sph3.getPointFromLongLat = function(out, a, longitude, latitude)
387
+ {
388
+ out[0] = a[0] + a[3] * Math.sin(latitude) * Math.cos(longitude);
389
+ out[1] = a[1] + a[3] * Math.sin(latitude) * Math.sin(longitude);
390
+ out[2] = a[2] + a[3] * Math.cos(latitude);
391
+ return out;
392
+ };
393
+
394
+ /**
395
+ * Gets the position component of a sph3
396
+ *
397
+ * @param {vec3} out
398
+ * @param {sph3} a
399
+ * @returns {vec3} out
400
+ */
401
+ sph3.getPosition = pln.getNormal;
402
+
403
+ /**
404
+ * Checks for intersection between two sph3s
405
+ *
406
+ * @param {sph3} a
407
+ * @param {sph3} b
408
+ * @returns {boolean}
409
+ */
410
+ sph3.intersectsSph3 = function(a, b)
411
+ {
412
+ if (sph3.isEmpty(a) || sph3.isEmpty(b)) return false;
413
+
414
+ let x = b[0] - a[0],
415
+ y = b[1] - a[1],
416
+ z = b[2] - a[2];
417
+ let radii = a[3] + b[3];
418
+
419
+ return (x * x + y * y + z * z) <= (radii * radii);
420
+ };
421
+
422
+ /**
423
+ * Checks for intersection with a box3
424
+ *
425
+ * @param {sph3} a
426
+ * @param {box3} b
427
+ * @returns {boolean}
428
+ */
429
+ sph3.intersectsBox3 = function(a, b)
430
+ {
431
+ return !sph3.isEmpty(a) && !box3.isEmpty(b) && box3.intersectsSph3(b, a);
432
+ };
433
+
434
+ /**
435
+ * Checks sph for intersection with a boxes' bounds
436
+ *
437
+ * @param {sph3} a
438
+ * @param {vec3} min
439
+ * @param {vec3} max
440
+ * @returns {boolean}
441
+ */
442
+ sph3.intersectsBounds = function(a, min, max)
443
+ {
444
+ if (sph3.isEmpty(a) || box3.bounds.isEmpty(min, max)) return false;
445
+
446
+ let x = Math.max(min[0], Math.min(max[0], a[0])) - a[0],
447
+ y = Math.max(min[1], Math.min(max[1], a[1])) - a[1],
448
+ z = Math.max(min[2], Math.min(max[2], a[2])) - a[2];
449
+
450
+ return (x * x + y * y + z * z) <= a[3] * a[3];
451
+ };
452
+
453
+ /**
454
+ * Checks sph for intersection with a sphere's components
455
+ *
456
+ * @param {sph3} a
457
+ * @param {vec3} p
458
+ * @param {number} r
459
+ * @returns {boolean}
460
+ */
461
+ sph3.intersectsPositionRadius = function(a, p, r)
462
+ {
463
+ if (sph3.isEmpty(a) || r < 0) return false;
464
+
465
+ let x = p[0] - a[0],
466
+ y = p[1] - a[1],
467
+ z = p[2] - a[2];
468
+
469
+ let radii = a[3] + r;
470
+ return (x * x + y * y + z * z) <= (radii * radii);
471
+ };
472
+
473
+ /**
474
+ * Checks for intersection with a Float32Array(4) plane
475
+ *
476
+ * @param {sph3} a
477
+ * @param {pln} p
478
+ * @returns {boolean}
479
+ */
480
+ sph3.intersectsPln = function(a, p)
481
+ {
482
+ return pln.intersectsSph3(p, a);
483
+ };
484
+
485
+ /**
486
+ * Checks for intersection with a plane's components
487
+ *
488
+ * @param {sph3} a - sphere to intersect
489
+ * @param {vec3} n - plane normal
490
+ * @param {number} c - plane constant
491
+ * @returns {boolean}
492
+ */
493
+ sph3.intersectsNormalConstant = function(a, n, c)
494
+ {
495
+ if (sph3.isEmpty(a)) return false;
496
+
497
+ const
498
+ distance = a[0] * n[0] + a[1] * n[1] + a[2] * n[2] + c,
499
+ normalLength = Math.hypot(n[0], n[1], n[2]);
500
+ return Math.abs(distance) <= a[3] * normalLength;
501
+ };
502
+
503
+ /**
504
+ * Checks if a sph3 is empty
505
+ *
506
+ * @param {sph3} a
507
+ * @returns {boolean}
508
+ */
509
+ sph3.isEmpty = function(a)
510
+ {
511
+ return a[3] < 0;
512
+ };
513
+
514
+ /**
515
+ * Returns the radius component of the sph3
516
+ *
517
+ * @param {sph3} a
518
+ * @returns {number}
519
+ */
520
+ sph3.radius = pln.constant;
521
+
522
+ /**
523
+ * Sets a sph3 from values
524
+ *
525
+ * @param {sph3} out
526
+ * @param {number} px
527
+ * @param {number} py
528
+ * @param {number} pz
529
+ * @param {number} r
530
+ * @returns {sph3}
531
+ */
532
+ sph3.set = vec4.set;
533
+
534
+ /**
535
+ * Sets a sph3 from an array at an optional offset
536
+ *
537
+ * @param {sph3} out
538
+ * @param {Array} arr
539
+ * @param {number} [index=0]
540
+ * @returns {sph3} out
541
+ */
542
+ sph3.setArray = vec4.setArray;
543
+
544
+ /**
545
+ * Sets a sphere from points, at an optional position vector
546
+ *
547
+ * @param {sph3} out - the receiving sphere
548
+ * @param {Array} points - The points to create the sphere from
549
+ * @param {vec3} [position] - An optional center position
550
+ * @returns {sph3} out - the receiving sphere
551
+ */
552
+ sph3.setPoints = function(out, points, position)
553
+ {
554
+ if (!points.length) return sph3.empty(out);
555
+
556
+ if (!box3_0) box3_0 = box3.create();
557
+
558
+ if (position)
559
+ {
560
+ out[0] = position[0];
561
+ out[1] = position[1];
562
+ out[2] = position[2];
563
+ }
564
+ else
565
+ {
566
+ box3.setPoints(box3_0, points);
567
+ out[0] = (box3_0[0] + box3_0[3]) * 0.5;
568
+ out[1] = (box3_0[1] + box3_0[4]) * 0.5;
569
+ out[2] = (box3_0[2] + box3_0[5]) * 0.5;
570
+ }
571
+
572
+ let maxSquaredRadius = 0;
573
+
574
+ for (let i = 0; i < points.length; i++)
575
+ {
576
+ let x = out[0] - points[i][0],
577
+ y = out[1] - points[i][1],
578
+ z = out[2] - points[i][2];
579
+
580
+ maxSquaredRadius = Math.max(maxSquaredRadius, x * x + y * y + z * z);
581
+ }
582
+
583
+ out[3] = Math.sqrt(maxSquaredRadius);
584
+ return out;
585
+ };
586
+
587
+ /**
588
+ * Returns the squared distance between two sph3s
589
+ *
590
+ * @param {sph3} a
591
+ * @param {sph3} b
592
+ * @returns {number}
593
+ */
594
+ sph3.squaredDistance = function(a, b)
595
+ {
596
+ if (sph3.isEmpty(a) || sph3.isEmpty(b)) return Infinity;
597
+
598
+ let x = b[0] - a[0],
599
+ y = b[1] - a[1],
600
+ z = b[2] - a[2],
601
+ distance = Math.sqrt(x * x + y * y + z * z) - (b[3] + a[3]);
602
+
603
+ return distance * distance;
604
+ };
605
+
606
+ /**
607
+ * Returns the square distance from a sphere to a given point
608
+ *
609
+ * @param {sph3} a
610
+ * @param {vec3} p
611
+ * @returns {number}
612
+ */
613
+ sph3.squaredDistanceToPoint = function(a, p)
614
+ {
615
+ if (sph3.isEmpty(a)) return Infinity;
616
+
617
+ let x = p[0] - a[0],
618
+ y = p[1] - a[1],
619
+ z = p[2] - a[2],
620
+ distance = Math.sqrt(x * x + y * y + z * z) - a[3];
621
+
622
+ return distance * distance;
623
+ };
624
+
625
+ /**
626
+ * Sets an array at an optional offset, with the values of a sph3
627
+ *
628
+ * @param {sph3} a
629
+ * @param {Array} arr
630
+ * @param {number} [offset = 0]
631
+ * @returns {sph3} a
632
+ */
633
+ sph3.toArray = vec4.toArray;
634
+
635
+ /**
636
+ * Converts a sphere to bounds
637
+ *
638
+ * @param {sph3} a
639
+ * @param {vec3} minBounds
640
+ * @param {vec3} maxBounds
641
+ */
642
+ sph3.toBounds = function(a, minBounds, maxBounds)
643
+ {
644
+ if (!box3_0) box3_0 = box3.create();
645
+ box3.fromSph3(box3_0, a);
646
+ box3.toBounds(box3_0, minBounds, maxBounds);
647
+ };
648
+
649
+ /**
650
+ *
651
+ * @param a
652
+ * @param position
653
+ * @returns {*}
654
+ */
655
+ sph3.toPositionRadius = function(a, position)
656
+ {
657
+ position[0] = a[0];
658
+ position[1] = a[1];
659
+ position[2] = a[2];
660
+ return a[3];
661
+ };
662
+
663
+ /**
664
+ * Transforms a sphere with a mat4
665
+ *
666
+ * @author three.js authors (conversion)
667
+ * @param {sph3} out - the receiving sphere
668
+ * @param {sph3} a - the sphere to transform
669
+ * @param {mat4} m - the matrix to transform by
670
+ * @returns {sph3}
671
+ */
672
+ sph3.transformMat4 = function(out, a, m)
673
+ {
674
+ if (sph3.isEmpty(a)) return sph3.empty(out);
675
+
676
+ let x = a[0],
677
+ y = a[1],
678
+ z = a[2];
679
+
680
+ let sX = m[0] * m[0] + m[1] * m[1] + m[2] * m[2],
681
+ sY = m[4] * m[4] + m[5] * m[5] + m[6] * m[6],
682
+ sZ = m[8] * m[8] + m[9] * m[9] + m[10] * m[10];
683
+
684
+ out[0] = m[0] * x + m[4] * y + m[8] * z + m[12];
685
+ out[1] = m[1] * x + m[5] * y + m[9] * z + m[13];
686
+ out[2] = m[2] * x + m[6] * y + m[10] * z + m[14];
687
+
688
+ out[3] = a[3] * Math.sqrt(Math.max(sX, sY, sZ));
689
+ return out;
690
+ };
691
+
692
+ /**
693
+ * Returns the result of a sphere translated by a given vector
694
+ *
695
+ * @param {sph3} out - the receiving sphere
696
+ * @param {sph3} a - the sphere to translate
697
+ * @param {vec3} v - the vector to translate with
698
+ * @returns {sph3}
699
+ */
700
+ sph3.translate = function(out, a, v)
701
+ {
702
+ out[0] = a[0] + v[0];
703
+ out[1] = a[1] + v[1];
704
+ out[2] = a[2] + v[2];
705
+ out[3] = a[3];
706
+ return out;
707
+ };
708
+
709
+ export const {
710
+ alloc,
711
+ unalloc,
712
+ $position,
713
+ clone,
714
+ containsPoint,
715
+ copy,
716
+ create,
717
+ distance,
718
+ distanceToPoint,
719
+ empty,
720
+ equals,
721
+ equalsPositionRadius,
722
+ exactEquals,
723
+ exactEqualsPositionRadius,
724
+ extract,
725
+ fromBox3,
726
+ fromBounds,
727
+ fromPositionRadius,
728
+ from,
729
+ fromTranslationRadius,
730
+ fromMat4,
731
+ union,
732
+ unionPositionRadius,
733
+ getClampedPoint,
734
+ getPointFromLongLat,
735
+ getPosition,
736
+ intersectsSph3,
737
+ intersectsBox3,
738
+ intersectsBounds,
739
+ intersectsPositionRadius,
740
+ intersectsPln,
741
+ intersectsNormalConstant,
742
+ isEmpty,
743
+ radius,
744
+ set,
745
+ setArray,
746
+ setPoints,
747
+ squaredDistance,
748
+ squaredDistanceToPoint,
749
+ toArray,
750
+ toBounds,
751
+ toPositionRadius,
752
+ transformMat4,
753
+ translate
754
+ } = sph3;