@minecraft/math 2.2.11 → 2.3.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 (46) hide show
  1. package/api-report/math.api.md +127 -0
  2. package/dist/minecraft-math.d.ts +1034 -431
  3. package/dist/minecraft-math.js +833 -6
  4. package/dist/minecraft-math.js.map +3 -3
  5. package/lib/__mocks__/minecraft-server.js +17 -0
  6. package/lib/__mocks__/minecraft-server.js.map +1 -0
  7. package/lib/src/aabb/coreHelpers.js +282 -0
  8. package/lib/src/aabb/coreHelpers.js.map +1 -0
  9. package/lib/src/aabb/coreHelpers.test.js +227 -0
  10. package/lib/src/aabb/coreHelpers.test.js.map +1 -0
  11. package/lib/src/aabb/index.js +4 -0
  12. package/lib/src/aabb/index.js.map +1 -0
  13. package/lib/src/general/clamp.js.map +1 -0
  14. package/lib/src/general/index.js.map +1 -0
  15. package/lib/{index.js → src/index.js} +1 -0
  16. package/lib/src/index.js.map +1 -0
  17. package/lib/src/index.test.js.map +1 -0
  18. package/lib/{vector3 → src/vector3}/coreHelpers.js +294 -2
  19. package/lib/src/vector3/coreHelpers.js.map +1 -0
  20. package/lib/src/vector3/coreHelpers.test.js +578 -0
  21. package/lib/src/vector3/coreHelpers.test.js.map +1 -0
  22. package/lib/src/vector3/index.js.map +1 -0
  23. package/lib/src/vector3/vectorWrapper.js +509 -0
  24. package/lib/src/vector3/vectorWrapper.js.map +1 -0
  25. package/lib/src/vector3/vectorWrapper.test.js +575 -0
  26. package/lib/src/vector3/vectorWrapper.test.js.map +1 -0
  27. package/lib/types/math-beta.d.ts +1034 -431
  28. package/lib/types/math-public.d.ts +1034 -431
  29. package/lib/types/math.d.ts +1034 -431
  30. package/package.json +1 -1
  31. package/lib/general/clamp.js.map +0 -1
  32. package/lib/general/index.js.map +0 -1
  33. package/lib/index.js.map +0 -1
  34. package/lib/index.test.js.map +0 -1
  35. package/lib/vector3/coreHelpers.js.map +0 -1
  36. package/lib/vector3/coreHelpers.test.js +0 -264
  37. package/lib/vector3/coreHelpers.test.js.map +0 -1
  38. package/lib/vector3/index.js.map +0 -1
  39. package/lib/vector3/vectorWrapper.js +0 -230
  40. package/lib/vector3/vectorWrapper.js.map +0 -1
  41. package/lib/vector3/vectorWrapper.test.js +0 -228
  42. package/lib/vector3/vectorWrapper.test.js.map +0 -1
  43. /package/lib/{general → src/general}/clamp.js +0 -0
  44. /package/lib/{general → src/general}/index.js +0 -0
  45. /package/lib/{index.test.js → src/index.test.js} +0 -0
  46. /package/lib/{vector3 → src/vector3}/index.js +0 -0
@@ -1,9 +1,9 @@
1
- // lib/general/clamp.js
1
+ // lib/src/general/clamp.js
2
2
  function clampNumber(val, min, max) {
3
3
  return Math.min(Math.max(val, min), max);
4
4
  }
5
5
 
6
- // lib/vector3/coreHelpers.js
6
+ // lib/src/vector3/coreHelpers.js
7
7
  var Vector3Utils = class _Vector3Utils {
8
8
  /**
9
9
  * equals
@@ -85,6 +85,30 @@ var Vector3Utils = class _Vector3Utils {
85
85
  static floor(v) {
86
86
  return { x: Math.floor(v.x), y: Math.floor(v.y), z: Math.floor(v.z) };
87
87
  }
88
+ /**
89
+ * ceil
90
+ *
91
+ * Ceil the components of a vector to produce a new vector
92
+ */
93
+ static ceil(v) {
94
+ return { x: Math.ceil(v.x), y: Math.ceil(v.y), z: Math.ceil(v.z) };
95
+ }
96
+ /**
97
+ * min
98
+ *
99
+ * Min the components of two vectors to produce a new vector
100
+ */
101
+ static min(a, b) {
102
+ return { x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), z: Math.min(a.z, b.z) };
103
+ }
104
+ /**
105
+ * max
106
+ *
107
+ * Max the components of two vectors to produce a new vector
108
+ */
109
+ static max(a, b) {
110
+ return { x: Math.max(a.x, b.x), y: Math.max(a.y, b.y), z: Math.max(a.z, b.z) };
111
+ }
88
112
  /**
89
113
  * toString
90
114
  *
@@ -189,7 +213,79 @@ var Vector3Utils = class _Vector3Utils {
189
213
  return { x: v.x * cos - v.y * sin, y: v.y * cos + v.x * sin, z: v.z };
190
214
  }
191
215
  };
192
- var Vector2Utils = class {
216
+ var Vector2Utils = class _Vector2Utils {
217
+ /**
218
+ * equals
219
+ *
220
+ * Check the equality of two vectors
221
+ */
222
+ static equals(v1, v2) {
223
+ return v1.x === v2.x && v1.y === v2.y;
224
+ }
225
+ /**
226
+ * add
227
+ *
228
+ * Add two vectors to produce a new vector
229
+ */
230
+ static add(v1, v2) {
231
+ return { x: v1.x + (v2.x ?? 0), y: v1.y + (v2.y ?? 0) };
232
+ }
233
+ /**
234
+ * subtract
235
+ *
236
+ * Subtract two vectors to produce a new vector (v1-v2)
237
+ */
238
+ static subtract(v1, v2) {
239
+ return { x: v1.x - (v2.x ?? 0), y: v1.y - (v2.y ?? 0) };
240
+ }
241
+ /** scale
242
+ *
243
+ * Multiple all entries in a vector by a single scalar value producing a new vector
244
+ */
245
+ static scale(v1, scale) {
246
+ return { x: v1.x * scale, y: v1.y * scale };
247
+ }
248
+ /**
249
+ * dot
250
+ *
251
+ * Calculate the dot product of two vectors
252
+ */
253
+ static dot(a, b) {
254
+ return a.x * b.x + a.y * b.y;
255
+ }
256
+ /**
257
+ * magnitude
258
+ *
259
+ * The magnitude of a vector
260
+ */
261
+ static magnitude(v) {
262
+ return Math.sqrt(v.x ** 2 + v.y ** 2);
263
+ }
264
+ /**
265
+ * distance
266
+ *
267
+ * Calculate the distance between two vectors
268
+ */
269
+ static distance(a, b) {
270
+ return _Vector2Utils.magnitude(_Vector2Utils.subtract(a, b));
271
+ }
272
+ /**
273
+ * normalize
274
+ *
275
+ * Takes a vector 3 and normalizes it to a unit vector
276
+ */
277
+ static normalize(v) {
278
+ const mag = _Vector2Utils.magnitude(v);
279
+ return { x: v.x / mag, y: v.y / mag };
280
+ }
281
+ /**
282
+ * floor
283
+ *
284
+ * Floor the components of a vector to produce a new vector
285
+ */
286
+ static floor(v) {
287
+ return { x: Math.floor(v.x), y: Math.floor(v.y) };
288
+ }
193
289
  /**
194
290
  * toString
195
291
  *
@@ -203,10 +299,10 @@ var Vector2Utils = class {
203
299
  /**
204
300
  * fromString
205
301
  *
206
- * Gets a Vector2 from the string representation produced by {@link Vector3Utils.toString}. If any numeric value is not a number
302
+ * Gets a Vector2 from the string representation produced by {@link Vector2Utils.toString}. If any numeric value is not a number
207
303
  * or the format is invalid, undefined is returned.
208
304
  * @param str - The string to parse
209
- * @param delimiter - The delimiter used to separate the components. Defaults to the same as the default for {@link Vector3Utils.toString}
305
+ * @param delimiter - The delimiter used to separate the components. Defaults to the same as the default for {@link Vector2Utils.toString}
210
306
  */
211
307
  static fromString(str, delimiter = ",") {
212
308
  const parts = str.split(delimiter);
@@ -219,6 +315,189 @@ var Vector2Utils = class {
219
315
  }
220
316
  return { x: output[0], y: output[1] };
221
317
  }
318
+ /**
319
+ * clamp
320
+ *
321
+ * Clamps the components of a vector to limits to produce a new vector
322
+ */
323
+ static clamp(v, limits) {
324
+ return {
325
+ x: clampNumber(v.x, limits?.min?.x ?? Number.MIN_SAFE_INTEGER, limits?.max?.x ?? Number.MAX_SAFE_INTEGER),
326
+ y: clampNumber(v.y, limits?.min?.y ?? Number.MIN_SAFE_INTEGER, limits?.max?.y ?? Number.MAX_SAFE_INTEGER)
327
+ };
328
+ }
329
+ /**
330
+ * lerp
331
+ *
332
+ * Constructs a new vector using linear interpolation on each component from two vectors.
333
+ */
334
+ static lerp(a, b, t) {
335
+ return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
336
+ }
337
+ /**
338
+ * slerp
339
+ *
340
+ * Constructs a new vector using spherical linear interpolation on each component from two vectors.
341
+ */
342
+ static slerp(a, b, t) {
343
+ const theta = Math.acos(_Vector2Utils.dot(a, b));
344
+ const sinTheta = Math.sin(theta);
345
+ const ta = Math.sin((1 - t) * theta) / sinTheta;
346
+ const tb = Math.sin(t * theta) / sinTheta;
347
+ return _Vector2Utils.add(_Vector2Utils.scale(a, ta), _Vector2Utils.scale(b, tb));
348
+ }
349
+ /**
350
+ * multiply
351
+ *
352
+ * Element-wise multiplication of two vectors together.
353
+ * Not to be confused with {@link Vector2Utils.dot} product
354
+ */
355
+ static multiply(a, b) {
356
+ return { x: a.x * b.x, y: a.y * b.y };
357
+ }
358
+ };
359
+ var VectorXZUtils = class _VectorXZUtils {
360
+ /**
361
+ * equals
362
+ *
363
+ * Check the equality of two vectors
364
+ */
365
+ static equals(v1, v2) {
366
+ return v1.x === v2.x && v1.z === v2.z;
367
+ }
368
+ /**
369
+ * add
370
+ *
371
+ * Add two vectors to produce a new vector
372
+ */
373
+ static add(v1, v2) {
374
+ return { x: v1.x + (v2.x ?? 0), z: v1.z + (v2.z ?? 0) };
375
+ }
376
+ /**
377
+ * subtract
378
+ *
379
+ * Subtract two vectors to produce a new vector (v1-v2)
380
+ */
381
+ static subtract(v1, v2) {
382
+ return { x: v1.x - (v2.x ?? 0), z: v1.z - (v2.z ?? 0) };
383
+ }
384
+ /** scale
385
+ *
386
+ * Multiple all entries in a vector by a single scalar value producing a new vector
387
+ */
388
+ static scale(v1, scale) {
389
+ return { x: v1.x * scale, z: v1.z * scale };
390
+ }
391
+ /**
392
+ * dot
393
+ *
394
+ * Calculate the dot product of two vectors
395
+ */
396
+ static dot(a, b) {
397
+ return a.x * b.x + a.z * b.z;
398
+ }
399
+ /**
400
+ * magnitude
401
+ *
402
+ * The magnitude of a vector
403
+ */
404
+ static magnitude(v) {
405
+ return Math.sqrt(v.x ** 2 + v.z ** 2);
406
+ }
407
+ /**
408
+ * distance
409
+ *
410
+ * Calculate the distance between two vectors
411
+ */
412
+ static distance(a, b) {
413
+ return _VectorXZUtils.magnitude(_VectorXZUtils.subtract(a, b));
414
+ }
415
+ /**
416
+ * normalize
417
+ *
418
+ * Takes a vector 3 and normalizes it to a unit vector
419
+ */
420
+ static normalize(v) {
421
+ const mag = _VectorXZUtils.magnitude(v);
422
+ return { x: v.x / mag, z: v.z / mag };
423
+ }
424
+ /**
425
+ * floor
426
+ *
427
+ * Floor the components of a vector to produce a new vector
428
+ */
429
+ static floor(v) {
430
+ return { x: Math.floor(v.x), z: Math.floor(v.z) };
431
+ }
432
+ /**
433
+ * toString
434
+ *
435
+ * Create a string representation of a vectorxz
436
+ */
437
+ static toString(v, options) {
438
+ const decimals = options?.decimals ?? 2;
439
+ const str = [v.x.toFixed(decimals), v.z.toFixed(decimals)];
440
+ return str.join(options?.delimiter ?? ", ");
441
+ }
442
+ /**
443
+ * fromString
444
+ *
445
+ * Gets a VectorXZ from the string representation produced by {@link VectorXZUtils.toString}. If any numeric value is not a number
446
+ * or the format is invalid, undefined is returned.
447
+ * @param str - The string to parse
448
+ * @param delimiter - The delimiter used to separate the components. Defaults to the same as the default for {@link VectorXZUtils.toString}
449
+ */
450
+ static fromString(str, delimiter = ",") {
451
+ const parts = str.split(delimiter);
452
+ if (parts.length !== 2) {
453
+ return void 0;
454
+ }
455
+ const output = parts.map((part) => parseFloat(part));
456
+ if (output.some((part) => isNaN(part))) {
457
+ return void 0;
458
+ }
459
+ return { x: output[0], z: output[1] };
460
+ }
461
+ /**
462
+ * clamp
463
+ *
464
+ * Clamps the components of a vector to limits to produce a new vector
465
+ */
466
+ static clamp(v, limits) {
467
+ return {
468
+ x: clampNumber(v.x, limits?.min?.x ?? Number.MIN_SAFE_INTEGER, limits?.max?.x ?? Number.MAX_SAFE_INTEGER),
469
+ z: clampNumber(v.z, limits?.min?.z ?? Number.MIN_SAFE_INTEGER, limits?.max?.z ?? Number.MAX_SAFE_INTEGER)
470
+ };
471
+ }
472
+ /**
473
+ * lerp
474
+ *
475
+ * Constructs a new vector using linear interpolation on each component from two vectors.
476
+ */
477
+ static lerp(a, b, t) {
478
+ return { x: a.x + (b.x - a.x) * t, z: a.z + (b.z - a.z) * t };
479
+ }
480
+ /**
481
+ * slerp
482
+ *
483
+ * Constructs a new vector using spherical linear interpolation on each component from two vectors.
484
+ */
485
+ static slerp(a, b, t) {
486
+ const theta = Math.acos(_VectorXZUtils.dot(a, b));
487
+ const sinTheta = Math.sin(theta);
488
+ const ta = Math.sin((1 - t) * theta) / sinTheta;
489
+ const tb = Math.sin(t * theta) / sinTheta;
490
+ return _VectorXZUtils.add(_VectorXZUtils.scale(a, ta), _VectorXZUtils.scale(b, tb));
491
+ }
492
+ /**
493
+ * multiply
494
+ *
495
+ * Element-wise multiplication of two vectors together.
496
+ * Not to be confused with {@link VectorXZUtils.dot} product
497
+ */
498
+ static multiply(a, b) {
499
+ return { x: a.x * b.x, z: a.z * b.z };
500
+ }
222
501
  };
223
502
  var VECTOR3_UP = { x: 0, y: 1, z: 0 };
224
503
  var VECTOR3_DOWN = { x: 0, y: -1, z: 0 };
@@ -235,8 +514,9 @@ var VECTOR3_SOUTH = { x: 0, y: 0, z: -1 };
235
514
  var VECTOR3_HALF = { x: 0.5, y: 0.5, z: 0.5 };
236
515
  var VECTOR3_NEGATIVE_ONE = { x: -1, y: -1, z: -1 };
237
516
  var VECTOR2_ZERO = { x: 0, y: 0 };
517
+ var VECTORXZ_ZERO = { x: 0, z: 0 };
238
518
 
239
- // lib/vector3/vectorWrapper.js
519
+ // lib/src/vector3/vectorWrapper.js
240
520
  var Vector3Builder = class {
241
521
  x;
242
522
  y;
@@ -351,6 +631,30 @@ var Vector3Builder = class {
351
631
  floor() {
352
632
  return this.assign(Vector3Utils.floor(this));
353
633
  }
634
+ /**
635
+ * ceil
636
+ *
637
+ * Ceil the components of a vector to produce a new vector
638
+ */
639
+ ceil() {
640
+ return this.assign(Vector3Utils.ceil(this));
641
+ }
642
+ /**
643
+ * min
644
+ *
645
+ * Min the components of two vectors to produce a new vector
646
+ */
647
+ min(vec) {
648
+ return this.assign(Vector3Utils.min(this, vec));
649
+ }
650
+ /**
651
+ * max
652
+ *
653
+ * Max the components of two vectors to produce a new vector
654
+ */
655
+ max(vec) {
656
+ return this.assign(Vector3Utils.max(this, vec));
657
+ }
354
658
  /**
355
659
  * toString
356
660
  *
@@ -444,8 +748,528 @@ var Vector2Builder = class {
444
748
  toString(options) {
445
749
  return Vector2Utils.toString(this, options);
446
750
  }
751
+ /**
752
+ * Assigns the values of the passed in vector to this vector. Returns itself.
753
+ */
754
+ assign(vec) {
755
+ this.x = vec.x;
756
+ this.y = vec.y;
757
+ return this;
758
+ }
759
+ /**
760
+ * equals
761
+ *
762
+ * Check the equality of two vectors
763
+ */
764
+ equals(v) {
765
+ return Vector2Utils.equals(this, v);
766
+ }
767
+ /**
768
+ * add
769
+ *
770
+ * Adds the vector v to this, returning itself.
771
+ */
772
+ add(v) {
773
+ return this.assign(Vector2Utils.add(this, v));
774
+ }
775
+ /**
776
+ * subtract
777
+ *
778
+ * Subtracts the vector v from this, returning itself.
779
+ */
780
+ subtract(v) {
781
+ return this.assign(Vector2Utils.subtract(this, v));
782
+ }
783
+ /** scale
784
+ *
785
+ * Scales this by the passed in value, returning itself.
786
+ */
787
+ scale(val) {
788
+ return this.assign(Vector2Utils.scale(this, val));
789
+ }
790
+ /**
791
+ * dot
792
+ *
793
+ * Computes the dot product of this and the passed in vector.
794
+ */
795
+ dot(vec) {
796
+ return Vector2Utils.dot(this, vec);
797
+ }
798
+ /**
799
+ * magnitude
800
+ *
801
+ * The magnitude of the vector
802
+ */
803
+ magnitude() {
804
+ return Vector2Utils.magnitude(this);
805
+ }
806
+ /**
807
+ * distance
808
+ *
809
+ * Calculate the distance between two vectors
810
+ */
811
+ distance(vec) {
812
+ return Vector2Utils.distance(this, vec);
813
+ }
814
+ /**
815
+ * normalize
816
+ *
817
+ * Normalizes this vector, returning itself.
818
+ */
819
+ normalize() {
820
+ return this.assign(Vector2Utils.normalize(this));
821
+ }
822
+ /**
823
+ * floor
824
+ *
825
+ * Floor the components of a vector to produce a new vector
826
+ */
827
+ floor() {
828
+ return this.assign(Vector2Utils.floor(this));
829
+ }
830
+ /**
831
+ * clamp
832
+ *
833
+ * Clamps the components of a vector to limits to produce a new vector
834
+ */
835
+ clamp(limits) {
836
+ return this.assign(Vector2Utils.clamp(this, limits));
837
+ }
838
+ /**
839
+ * lerp
840
+ *
841
+ * Constructs a new vector using linear interpolation on each component from two vectors.
842
+ */
843
+ lerp(vec, t) {
844
+ return this.assign(Vector2Utils.lerp(this, vec, t));
845
+ }
846
+ /**
847
+ * slerp
848
+ *
849
+ * Constructs a new vector using spherical linear interpolation on each component from two vectors.
850
+ */
851
+ slerp(vec, t) {
852
+ return this.assign(Vector2Utils.slerp(this, vec, t));
853
+ }
854
+ /**
855
+ * multiply
856
+ *
857
+ * Element-wise multiplication of two vectors together.
858
+ * Not to be confused with {@link Vector2Builder.dot} product
859
+ */
860
+ multiply(vec) {
861
+ return this.assign(Vector2Utils.multiply(this, vec));
862
+ }
863
+ };
864
+ var VectorXZBuilder = class {
865
+ x;
866
+ z;
867
+ constructor(first, second) {
868
+ if (typeof first === "object") {
869
+ this.x = first.x;
870
+ this.z = first.z;
871
+ } else if (typeof first === "string") {
872
+ const parsed = VectorXZUtils.fromString(first, second ?? ",");
873
+ if (!parsed) {
874
+ this.x = 0;
875
+ this.z = 0;
876
+ return;
877
+ }
878
+ this.x = parsed.x;
879
+ this.z = parsed.z;
880
+ } else {
881
+ this.x = first;
882
+ this.z = second ?? 0;
883
+ }
884
+ }
885
+ toString(options) {
886
+ return VectorXZUtils.toString(this, options);
887
+ }
888
+ /**
889
+ * Assigns the values of the passed in vector to this vector. Returns itself.
890
+ */
891
+ assign(vec) {
892
+ this.x = vec.x;
893
+ this.z = vec.z;
894
+ return this;
895
+ }
896
+ /**
897
+ * equals
898
+ *
899
+ * Check the equality of two vectors
900
+ */
901
+ equals(v) {
902
+ return VectorXZUtils.equals(this, v);
903
+ }
904
+ /**
905
+ * add
906
+ *
907
+ * Adds the vector v to this, returning itself.
908
+ */
909
+ add(v) {
910
+ return this.assign(VectorXZUtils.add(this, v));
911
+ }
912
+ /**
913
+ * subtract
914
+ *
915
+ * Subtracts the vector v from this, returning itself.
916
+ */
917
+ subtract(v) {
918
+ return this.assign(VectorXZUtils.subtract(this, v));
919
+ }
920
+ /** scale
921
+ *
922
+ * Scales this by the passed in value, returning itself.
923
+ */
924
+ scale(val) {
925
+ return this.assign(VectorXZUtils.scale(this, val));
926
+ }
927
+ /**
928
+ * dot
929
+ *
930
+ * Computes the dot product of this and the passed in vector.
931
+ */
932
+ dot(vec) {
933
+ return VectorXZUtils.dot(this, vec);
934
+ }
935
+ /**
936
+ * magnitude
937
+ *
938
+ * The magnitude of the vector
939
+ */
940
+ magnitude() {
941
+ return VectorXZUtils.magnitude(this);
942
+ }
943
+ /**
944
+ * distance
945
+ *
946
+ * Calculate the distance between two vectors
947
+ */
948
+ distance(vec) {
949
+ return VectorXZUtils.distance(this, vec);
950
+ }
951
+ /**
952
+ * normalize
953
+ *
954
+ * Normalizes this vector, returning itself.
955
+ */
956
+ normalize() {
957
+ return this.assign(VectorXZUtils.normalize(this));
958
+ }
959
+ /**
960
+ * floor
961
+ *
962
+ * Floor the components of a vector to produce a new vector
963
+ */
964
+ floor() {
965
+ return this.assign(VectorXZUtils.floor(this));
966
+ }
967
+ /**
968
+ * clamp
969
+ *
970
+ * Clamps the components of a vector to limits to produce a new vector
971
+ */
972
+ clamp(limits) {
973
+ return this.assign(VectorXZUtils.clamp(this, limits));
974
+ }
975
+ /**
976
+ * lerp
977
+ *
978
+ * Constructs a new vector using linear interpolation on each component from two vectors.
979
+ */
980
+ lerp(vec, t) {
981
+ return this.assign(VectorXZUtils.lerp(this, vec, t));
982
+ }
983
+ /**
984
+ * slerp
985
+ *
986
+ * Constructs a new vector using spherical linear interpolation on each component from two vectors.
987
+ */
988
+ slerp(vec, t) {
989
+ return this.assign(VectorXZUtils.slerp(this, vec, t));
990
+ }
991
+ /**
992
+ * multiply
993
+ *
994
+ * Element-wise multiplication of two vectors together.
995
+ * Not to be confused with {@link VectorXZBuilder.dot} product
996
+ */
997
+ multiply(vec) {
998
+ return this.assign(VectorXZUtils.multiply(this, vec));
999
+ }
1000
+ };
1001
+
1002
+ // lib/src/aabb/coreHelpers.js
1003
+ import { BlockVolume } from "@minecraft/server";
1004
+ var AABBInvalidExtentError = class extends Error {
1005
+ constructor(extent) {
1006
+ super(`Invalid AABB extent of '${Vector3Utils.toString(extent)}'`);
1007
+ }
1008
+ };
1009
+ var AABBUtils = class _AABBUtils {
1010
+ constructor() {
1011
+ }
1012
+ /**
1013
+ * EPSILON
1014
+ *
1015
+ * The internal epsilon value that determines validity and used for block volume tolerance.
1016
+ */
1017
+ static EPSILON = 1e-5;
1018
+ /**
1019
+ * createFromCornerPoints
1020
+ *
1021
+ * Gets an AABB from points defining it's corners, the order doesn't matter.
1022
+ * @param pointA - The first corner point.
1023
+ * @param pointB - The second corner point.
1024
+ * @throws {@link AABBInvalidExtentError}
1025
+ * This exception is thrown if the resulting AABB is invalid.
1026
+ *
1027
+ * @returns - The resulting AABB.
1028
+ */
1029
+ static createFromCornerPoints(pointA, pointB) {
1030
+ const min = Vector3Utils.min(pointA, pointB);
1031
+ const max = Vector3Utils.max(pointA, pointB);
1032
+ const extent = Vector3Utils.multiply(Vector3Utils.subtract(max, min), { x: 0.5, y: 0.5, z: 0.5 });
1033
+ const aabb = { center: Vector3Utils.add(min, extent), extent };
1034
+ _AABBUtils.throwErrorIfInvalid(aabb);
1035
+ return aabb;
1036
+ }
1037
+ /**
1038
+ * isValid
1039
+ *
1040
+ * Determines if the AABB has non-zero extent on all axes.
1041
+ * @param aabb - The AABB to test for validity.
1042
+ * @returns - True if all extent axes are non-zero, otherwise false.
1043
+ */
1044
+ static isValid(aabb) {
1045
+ return aabb.extent.x >= _AABBUtils.EPSILON && aabb.extent.y >= _AABBUtils.EPSILON && aabb.extent.z >= _AABBUtils.EPSILON;
1046
+ }
1047
+ /**
1048
+ * throwErrorIfInvalid
1049
+ *
1050
+ * Throws an error if the AABB is invalid.
1051
+ * @param aabb - The AABB to test for validity.
1052
+ * @throws {@link AABBInvalidExtentError}
1053
+ * This exception is thrown if the input AABB is invalid.
1054
+ */
1055
+ static throwErrorIfInvalid(aabb) {
1056
+ if (!_AABBUtils.isValid(aabb)) {
1057
+ throw new AABBInvalidExtentError(aabb.extent);
1058
+ }
1059
+ }
1060
+ /**
1061
+ * equals
1062
+ *
1063
+ * Compares the equality of two AABBs.
1064
+ * @param aabb - The first AABB in the comparison.
1065
+ * @param other - The second AABB in the comparison.
1066
+ * @throws {@link AABBInvalidExtentError}
1067
+ * This exception is thrown if either of the input AABBs are invalid.
1068
+ *
1069
+ * @returns - True if the center and extent of both AABBs are equal.
1070
+ */
1071
+ static equals(aabb, other) {
1072
+ _AABBUtils.throwErrorIfInvalid(aabb);
1073
+ _AABBUtils.throwErrorIfInvalid(other);
1074
+ return Vector3Utils.equals(aabb.center, other.center) && Vector3Utils.equals(aabb.extent, other.extent);
1075
+ }
1076
+ /**
1077
+ * getMin
1078
+ *
1079
+ * Gets the minimum corner of an AABB.
1080
+ * @param aabb - The AABB to retrieve the minimum corner of.
1081
+ * @throws {@link AABBInvalidExtentError}
1082
+ * This exception is thrown if the input AABB is invalid.
1083
+ *
1084
+ * @returns - The minimum corner of the AABB.
1085
+ */
1086
+ static getMin(aabb) {
1087
+ _AABBUtils.throwErrorIfInvalid(aabb);
1088
+ return Vector3Utils.subtract(aabb.center, aabb.extent);
1089
+ }
1090
+ /**
1091
+ * getMax
1092
+ *
1093
+ * Gets the maximum corner of an AABB.
1094
+ * @param aabb - The AABB to retrieve the maximum corner of.
1095
+ * @throws {@link AABBInvalidExtentError}
1096
+ * This exception is thrown if the input AABB is invalid.
1097
+ *
1098
+ * @returns - The maximum corner of the AABB.
1099
+ */
1100
+ static getMax(aabb) {
1101
+ _AABBUtils.throwErrorIfInvalid(aabb);
1102
+ return Vector3Utils.add(aabb.center, aabb.extent);
1103
+ }
1104
+ /**
1105
+ * getSpan
1106
+ *
1107
+ * Gets the span of an AABB.
1108
+ * @param aabb - The AABB to retrieve the span of.
1109
+ * @throws {@link AABBInvalidExtentError}
1110
+ * This exception is thrown if the input AABB is invalid.
1111
+ *
1112
+ * @returns - The span of the AABB.
1113
+ */
1114
+ static getSpan(aabb) {
1115
+ _AABBUtils.throwErrorIfInvalid(aabb);
1116
+ return Vector3Utils.multiply(aabb.extent, { x: 2, y: 2, z: 2 });
1117
+ }
1118
+ /**
1119
+ * getBlockVolume
1120
+ *
1121
+ * Creates the smallest BlockVolume that includes all of a source AABB.
1122
+ * @param aabb - The source AABB.
1123
+ * @throws {@link AABBInvalidExtentError}
1124
+ * This exception is thrown if the input AABB is invalid.
1125
+ *
1126
+ * @returns - The BlockVolume containing the source AABB.
1127
+ */
1128
+ static getBlockVolume(aabb) {
1129
+ _AABBUtils.throwErrorIfInvalid(aabb);
1130
+ const epsilon = _AABBUtils.EPSILON;
1131
+ const epsilonVec = { x: epsilon, y: epsilon, z: epsilon };
1132
+ const from = Vector3Utils.floor(Vector3Utils.add(_AABBUtils.getMin(aabb), epsilonVec));
1133
+ const to = Vector3Utils.ceil(Vector3Utils.subtract(_AABBUtils.getMax(aabb), epsilonVec));
1134
+ return new BlockVolume(from, to);
1135
+ }
1136
+ /**
1137
+ * translate
1138
+ *
1139
+ * Creates a translated AABB given a source AABB and translation vector.
1140
+ * @param aabb - The source AABB.
1141
+ * @param delta - The translation vector to add to the AABBs center.
1142
+ * @throws {@link AABBInvalidExtentError}
1143
+ * This exception is thrown if the input AABB is invalid.
1144
+ *
1145
+ * @returns - The resulting translated AABB.
1146
+ */
1147
+ static translate(aabb, delta) {
1148
+ _AABBUtils.throwErrorIfInvalid(aabb);
1149
+ return { center: Vector3Utils.add(aabb.center, delta), extent: aabb.extent };
1150
+ }
1151
+ /**
1152
+ * dilate
1153
+ *
1154
+ * Creates a dilated AABB given a source AABB and dilation vector.
1155
+ * @param aabb - The source AABB.
1156
+ * @param size - The dilation vector to add to the AABBs extent.
1157
+ * @throws {@link AABBInvalidExtentError}
1158
+ * This exception is thrown if the input AABB is invalid.
1159
+ *
1160
+ * @returns - The resulting dilated AABB.
1161
+ */
1162
+ static dilate(aabb, size) {
1163
+ _AABBUtils.throwErrorIfInvalid(aabb);
1164
+ const epsilon = _AABBUtils.EPSILON;
1165
+ const epsilonVec = { x: epsilon, y: epsilon, z: epsilon };
1166
+ let dilatedExtent = Vector3Utils.add(aabb.extent, size);
1167
+ dilatedExtent = Vector3Utils.clamp(dilatedExtent, { min: epsilonVec });
1168
+ return { center: aabb.center, extent: dilatedExtent };
1169
+ }
1170
+ /**
1171
+ * expand
1172
+ *
1173
+ * Creates an expanded AABB given two source AABBs.
1174
+ * @param aabb - The first source AABB.
1175
+ * @param other - The second source AABB.
1176
+ * @throws {@link AABBInvalidExtentError}
1177
+ * This exception is thrown if either of the input AABBs are invalid.
1178
+ *
1179
+ * @returns - The resulting expanded AABB.
1180
+ */
1181
+ static expand(aabb, other) {
1182
+ _AABBUtils.throwErrorIfInvalid(aabb);
1183
+ _AABBUtils.throwErrorIfInvalid(other);
1184
+ const aabbMin = _AABBUtils.getMin(aabb);
1185
+ const otherMin = _AABBUtils.getMin(other);
1186
+ const min = Vector3Utils.min(aabbMin, otherMin);
1187
+ const aabbMax = _AABBUtils.getMax(aabb);
1188
+ const otherMax = _AABBUtils.getMax(other);
1189
+ const max = Vector3Utils.max(aabbMax, otherMax);
1190
+ return _AABBUtils.createFromCornerPoints(min, max);
1191
+ }
1192
+ /**
1193
+ * getIntersection
1194
+ *
1195
+ * Creates an AABB of the intersecting area of two source AABBs.
1196
+ * @param aabb - The first source AABB.
1197
+ * @param other - The second source AABB.
1198
+ * @throws {@link AABBInvalidExtentError}
1199
+ * This exception is thrown if either of the input AABBs are invalid.
1200
+ *
1201
+ * @returns - The resulting intersecting AABB if they intersect, otherwise returns undefined.
1202
+ */
1203
+ static getIntersection(aabb, other) {
1204
+ _AABBUtils.throwErrorIfInvalid(aabb);
1205
+ _AABBUtils.throwErrorIfInvalid(other);
1206
+ if (!_AABBUtils.intersects(aabb, other)) {
1207
+ return void 0;
1208
+ }
1209
+ const aabbMin = _AABBUtils.getMin(aabb);
1210
+ const otherMin = _AABBUtils.getMin(other);
1211
+ const min = Vector3Utils.max(aabbMin, otherMin);
1212
+ const aabbMax = _AABBUtils.getMax(aabb);
1213
+ const otherMax = _AABBUtils.getMax(other);
1214
+ const max = Vector3Utils.min(aabbMax, otherMax);
1215
+ return _AABBUtils.createFromCornerPoints(min, max);
1216
+ }
1217
+ /**
1218
+ * intersects
1219
+ *
1220
+ * Calculates if two AABBs are intersecting.
1221
+ * @param aabb - The first AABB.
1222
+ * @param other - The second AABB.
1223
+ * @throws {@link AABBInvalidExtentError}
1224
+ * This exception is thrown if either of the input AABBs are invalid.
1225
+ *
1226
+ * @returns - True if the AABBs are intersecting, otherwise false.
1227
+ */
1228
+ static intersects(aabb, other) {
1229
+ _AABBUtils.throwErrorIfInvalid(aabb);
1230
+ _AABBUtils.throwErrorIfInvalid(other);
1231
+ const aabbMin = _AABBUtils.getMin(aabb);
1232
+ const aabbMax = _AABBUtils.getMax(aabb);
1233
+ const otherMin = _AABBUtils.getMin(other);
1234
+ const otherMax = _AABBUtils.getMax(other);
1235
+ if (otherMax.x < aabbMin.x || otherMin.x > aabbMax.x) {
1236
+ return false;
1237
+ }
1238
+ if (otherMax.y < aabbMin.y || otherMin.y > aabbMax.y) {
1239
+ return false;
1240
+ }
1241
+ if (otherMax.z < aabbMin.z || otherMin.z > aabbMax.z) {
1242
+ return false;
1243
+ }
1244
+ return true;
1245
+ }
1246
+ /**
1247
+ * isInside
1248
+ *
1249
+ * Calculates if a position is inside of an AABB.
1250
+ * @param aabb - The AABB to test against.
1251
+ * @param pos - The position to test.
1252
+ * @throws {@link AABBInvalidExtentError}
1253
+ * This exception is thrown if the input AABB is invalid.
1254
+ *
1255
+ * @returns True if the position is inside of the AABB, otherwise returns false.
1256
+ */
1257
+ static isInside(aabb, pos) {
1258
+ _AABBUtils.throwErrorIfInvalid(aabb);
1259
+ const min = _AABBUtils.getMin(aabb);
1260
+ if (pos.x < min.x || pos.y < min.y || pos.z < min.z) {
1261
+ return false;
1262
+ }
1263
+ const max = _AABBUtils.getMax(aabb);
1264
+ if (pos.x > max.x || pos.y > max.y || pos.z > max.z) {
1265
+ return false;
1266
+ }
1267
+ return true;
1268
+ }
447
1269
  };
448
1270
  export {
1271
+ AABBInvalidExtentError,
1272
+ AABBUtils,
449
1273
  VECTOR2_ZERO,
450
1274
  VECTOR3_BACK,
451
1275
  VECTOR3_DOWN,
@@ -461,10 +1285,13 @@ export {
461
1285
  VECTOR3_UP,
462
1286
  VECTOR3_WEST,
463
1287
  VECTOR3_ZERO,
1288
+ VECTORXZ_ZERO,
464
1289
  Vector2Builder,
465
1290
  Vector2Utils,
466
1291
  Vector3Builder,
467
1292
  Vector3Utils,
1293
+ VectorXZBuilder,
1294
+ VectorXZUtils,
468
1295
  clampNumber
469
1296
  };
470
1297
  //# sourceMappingURL=minecraft-math.js.map