@lakuna/umath 1.4.3 → 2.0.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 (76) hide show
  1. package/dist/index.d.ts +6 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +11 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/linalg/DualQuaternion.d.ts +23 -46
  6. package/dist/linalg/DualQuaternion.d.ts.map +1 -1
  7. package/dist/linalg/DualQuaternion.js +47 -70
  8. package/dist/linalg/DualQuaternion.js.map +1 -1
  9. package/dist/linalg/Matrix2.d.ts +16 -30
  10. package/dist/linalg/Matrix2.d.ts.map +1 -1
  11. package/dist/linalg/Matrix2.js +30 -44
  12. package/dist/linalg/Matrix2.js.map +1 -1
  13. package/dist/linalg/Matrix3.d.ts +21 -42
  14. package/dist/linalg/Matrix3.d.ts.map +1 -1
  15. package/dist/linalg/Matrix3.js +43 -84
  16. package/dist/linalg/Matrix3.js.map +1 -1
  17. package/dist/linalg/Matrix4.d.ts +40 -79
  18. package/dist/linalg/Matrix4.d.ts.map +1 -1
  19. package/dist/linalg/Matrix4.js +86 -171
  20. package/dist/linalg/Matrix4.js.map +1 -1
  21. package/dist/linalg/Quaternion.d.ts +29 -60
  22. package/dist/linalg/Quaternion.d.ts.map +1 -1
  23. package/dist/linalg/Quaternion.js +54 -85
  24. package/dist/linalg/Quaternion.js.map +1 -1
  25. package/dist/linalg/SlowMatrix.d.ts.map +1 -1
  26. package/dist/linalg/SlowMatrix.js +52 -78
  27. package/dist/linalg/SlowMatrix.js.map +1 -1
  28. package/dist/linalg/SlowSquareMatrix.js +1 -1
  29. package/dist/linalg/SlowSquareMatrix.js.map +1 -1
  30. package/dist/linalg/SlowVector.d.ts +165 -0
  31. package/dist/linalg/SlowVector.d.ts.map +1 -0
  32. package/dist/linalg/SlowVector.js +369 -0
  33. package/dist/linalg/SlowVector.js.map +1 -0
  34. package/dist/linalg/Vector.d.ts +20 -15
  35. package/dist/linalg/Vector.d.ts.map +1 -1
  36. package/dist/linalg/Vector2.d.ts +42 -48
  37. package/dist/linalg/Vector2.d.ts.map +1 -1
  38. package/dist/linalg/Vector2.js +74 -82
  39. package/dist/linalg/Vector2.js.map +1 -1
  40. package/dist/linalg/Vector3.d.ts +46 -56
  41. package/dist/linalg/Vector3.d.ts.map +1 -1
  42. package/dist/linalg/Vector3.js +82 -104
  43. package/dist/linalg/Vector3.js.map +1 -1
  44. package/dist/linalg/Vector4.d.ts +40 -44
  45. package/dist/linalg/Vector4.d.ts.map +1 -1
  46. package/dist/linalg/Vector4.js +69 -79
  47. package/dist/linalg/Vector4.js.map +1 -1
  48. package/dist/utility/MatrixSizeError.d.ts +1 -1
  49. package/dist/utility/MatrixSizeError.d.ts.map +1 -1
  50. package/dist/utility/MatrixSizeError.js +1 -1
  51. package/dist/utility/MatrixSizeError.js.map +1 -1
  52. package/dist/utility/VectorSizeError.d.ts +12 -0
  53. package/dist/utility/VectorSizeError.d.ts.map +1 -0
  54. package/dist/utility/VectorSizeError.js +15 -0
  55. package/dist/utility/VectorSizeError.js.map +1 -0
  56. package/dist/utility/createAxisAngleLike.d.ts +10 -0
  57. package/dist/utility/createAxisAngleLike.d.ts.map +1 -0
  58. package/dist/utility/createAxisAngleLike.js +10 -0
  59. package/dist/utility/createAxisAngleLike.js.map +1 -0
  60. package/package.json +8 -10
  61. package/src/index.ts +16 -0
  62. package/src/linalg/DualQuaternion.ts +51 -134
  63. package/src/linalg/Matrix2.ts +32 -83
  64. package/src/linalg/Matrix3.ts +55 -149
  65. package/src/linalg/Matrix4.ts +136 -292
  66. package/src/linalg/Quaternion.ts +62 -153
  67. package/src/linalg/SlowMatrix.ts +82 -81
  68. package/src/linalg/SlowSquareMatrix.ts +1 -1
  69. package/src/linalg/SlowVector.ts +449 -0
  70. package/src/linalg/Vector.ts +21 -16
  71. package/src/linalg/Vector2.ts +84 -147
  72. package/src/linalg/Vector3.ts +102 -188
  73. package/src/linalg/Vector4.ts +88 -137
  74. package/src/utility/MatrixSizeError.ts +1 -1
  75. package/src/utility/VectorSizeError.ts +14 -0
  76. package/src/utility/createAxisAngleLike.ts +13 -0
@@ -249,6 +249,27 @@ export const max = <T extends Vector4Like>(
249
249
  out
250
250
  );
251
251
 
252
+ /**
253
+ * Raise each component of a vector to the given power.
254
+ * @param vector - The base.
255
+ * @param scalar - The exponent (power) to raise each component to.
256
+ * @param out - The vector to store the result in.
257
+ * @returns The power (result of the exponentiation).
258
+ * @public
259
+ */
260
+ export const pow = <T extends Vector4Like>(
261
+ vector: Vector4Like,
262
+ scalar: number,
263
+ out: T
264
+ ): T =>
265
+ fromValues(
266
+ vector[0] ** scalar,
267
+ vector[1] ** scalar,
268
+ vector[2] ** scalar,
269
+ vector[3] ** scalar,
270
+ out
271
+ );
272
+
252
273
  /**
253
274
  * Scale a vector by a scalar.
254
275
  * @param vector - The multiplier.
@@ -301,13 +322,8 @@ export const scaleAndAdd = <T extends Vector4Like>(
301
322
  * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
302
323
  * @public
303
324
  */
304
- export const distance = (a: Vector4Like, b: Vector4Like): number => {
305
- const x = a[0] - b[0];
306
- const y = a[1] - b[1];
307
- const z = a[2] - b[2];
308
- const w = a[3] - b[3];
309
- return Math.sqrt(x * x + y * y + z * z + w * w); // `Math.hypot` is slower.
310
- };
325
+ export const distance = (a: Vector4Like, b: Vector4Like): number =>
326
+ Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]);
311
327
 
312
328
  /**
313
329
  * Calculate the squared Euclidean distance between two vectors.
@@ -331,13 +347,8 @@ export const squaredDistance = (a: Vector4Like, b: Vector4Like): number => {
331
347
  * @returns The magnitude.
332
348
  * @public
333
349
  */
334
- export const getMagnitude = (vector: Vector4Like): number => {
335
- const x = vector[0];
336
- const y = vector[1];
337
- const z = vector[2];
338
- const w = vector[3];
339
- return Math.sqrt(x * x + y * y + z * z + w * w); // `Math.hypot` is slower.
340
- };
350
+ export const getMagnitude = (vector: Vector4Like): number =>
351
+ Math.hypot(vector[0], vector[1], vector[2], vector[3]);
341
352
 
342
353
  /**
343
354
  * Calculate the squared magnitude (length) of a vector.
@@ -603,17 +614,15 @@ export default class Vector4
603
614
  * @param y - The second component.
604
615
  * @param z - The third component.
605
616
  * @param w - The fourth component.
606
- * @param out - The vector to store the result in.
607
617
  * @returns A new vector.
608
618
  */
609
- public static fromValues<T extends Vector4Like = Vector4>(
619
+ public static fromValues(
610
620
  x: number,
611
621
  y: number,
612
622
  z: number,
613
- w: number,
614
- out: T = new Vector4() as Vector4 & T
615
- ): T {
616
- return fromValues(x, y, z, w, out);
623
+ w: number
624
+ ): Vector4 {
625
+ return fromValues(x, y, z, w, new Vector4());
617
626
  }
618
627
 
619
628
  /**
@@ -657,25 +666,18 @@ export default class Vector4
657
666
  /**
658
667
  * Add two vectors of the same size.
659
668
  * @param vector - The other vector.
660
- * @param out - The vector to store the result in.
661
669
  * @returns The sum of the vectors.
662
670
  */
663
- public add<T extends Vector4Like = Vector4>(
664
- vector: Vector4Like,
665
- out: T = new Vector4() as Vector4 & T
666
- ): T {
667
- return add(this, vector, out);
671
+ public add(vector: Vector4Like): Vector4 {
672
+ return add(this, vector, new Vector4());
668
673
  }
669
674
 
670
675
  /**
671
676
  * Copy the values from this vector to another one.
672
- * @param out - The vector to store the result in.
673
677
  * @returns The copy.
674
678
  */
675
- public clone<T extends Vector4Like = Vector4>(
676
- out: T = new Vector4() as Vector4 & T
677
- ): T {
678
- return copy(this, out);
679
+ public clone(): Vector4 {
680
+ return copy(this, new Vector4());
679
681
  }
680
682
 
681
683
  /**
@@ -690,138 +692,106 @@ export default class Vector4
690
692
  /**
691
693
  * Multiply this vector by another.
692
694
  * @param vector - The other vector.
693
- * @param out - The vector to store the result in.
694
695
  * @returns The product of the vectors.
695
696
  */
696
- public multiply<T extends Vector4Like = Vector4>(
697
- vector: Vector4Like,
698
- out: T = new Vector4() as Vector4 & T
699
- ): T {
700
- return multiply(this, vector, out);
697
+ public multiply(vector: Vector4Like): Vector4 {
698
+ return multiply(this, vector, new Vector4());
701
699
  }
702
700
 
703
701
  /**
704
702
  * Divide this vector by another.
705
703
  * @param vector - The other vector.
706
- * @param out - The vector to store the result in.
707
704
  * @returns The quotient of the vectors.
708
705
  */
709
- public divide<T extends Vector4Like = Vector4>(
710
- vector: Vector4Like,
711
- out: T = new Vector4() as Vector4 & T
712
- ): T {
713
- return divide(this, vector, out);
706
+ public divide(vector: Vector4Like): Vector4 {
707
+ return divide(this, vector, new Vector4());
714
708
  }
715
709
 
716
710
  /**
717
711
  * Subtract another vector from this one.
718
712
  * @param vector - The other vector.
719
- * @param out - The vector to store the result in.
720
713
  * @returns The difference between the vectors.
721
714
  */
722
- public subtract<T extends Vector4Like = Vector4>(
723
- vector: Vector4Like,
724
- out: T = new Vector4() as Vector4 & T
725
- ): T {
726
- return subtract(this, vector, out);
715
+ public subtract(vector: Vector4Like): Vector4 {
716
+ return subtract(this, vector, new Vector4());
727
717
  }
728
718
 
729
719
  /**
730
720
  * Absolutize the components of this vector.
731
- * @param out - The vector to store the result in.
732
721
  * @returns The absolutized vector.
733
722
  */
734
- public abs<T extends Vector4Like = Vector4>(
735
- out: T = new Vector4() as Vector4 & T
736
- ): T {
737
- return abs(this, out);
723
+ public abs(): Vector4 {
724
+ return abs(this, new Vector4());
738
725
  }
739
726
 
740
727
  /**
741
728
  * Round up the components of this vector.
742
- * @param out - The vector to store the result in.
743
729
  * @returns The rounded vector.
744
730
  */
745
- public ceil<T extends Vector4Like = Vector4>(
746
- out: T = new Vector4() as Vector4 & T
747
- ): T {
748
- return ceil(this, out);
731
+ public ceil(): Vector4 {
732
+ return ceil(this, new Vector4());
749
733
  }
750
734
 
751
735
  /**
752
736
  * Round down the components of this vector.
753
- * @param out - The vector to store the result in.
754
737
  * @returns The rounded vector.
755
738
  */
756
- public floor<T extends Vector4Like = Vector4>(
757
- out: T = new Vector4() as Vector4 & T
758
- ): T {
759
- return floor(this, out);
739
+ public floor(): Vector4 {
740
+ return floor(this, new Vector4());
760
741
  }
761
742
 
762
743
  /**
763
744
  * Round the components of this vector.
764
- * @param out - The vector to store the result in.
765
745
  * @returns The rounded vector.
766
746
  */
767
- public round<T extends Vector4Like = Vector4>(
768
- out: T = new Vector4() as Vector4 & T
769
- ): T {
770
- return round(this, out);
747
+ public round(): Vector4 {
748
+ return round(this, new Vector4());
771
749
  }
772
750
 
773
751
  /**
774
752
  * Return the minimum of this and another vector.
775
753
  * @param vector - The other vector.
776
- * @param out - The vector to store the result in.
777
754
  * @returns The minimum.
778
755
  */
779
- public min<T extends Vector4Like = Vector4>(
780
- vector: Vector4Like,
781
- out: T = new Vector4() as Vector4 & T
782
- ): T {
783
- return min(this, vector, out);
756
+ public min(vector: Vector4Like): Vector4 {
757
+ return min(this, vector, new Vector4());
784
758
  }
785
759
 
786
760
  /**
787
761
  * Return the maximum of this and another vector.
788
762
  * @param vector - The other vector.
789
- * @param out - The vector to store the result in.
790
763
  * @returns The maximum.
791
764
  */
792
- public max<T extends Vector4Like = Vector4>(
793
- vector: Vector4Like,
794
- out: T = new Vector4() as Vector4 & T
795
- ): T {
796
- return max(this, vector, out);
765
+ public max(vector: Vector4Like): Vector4 {
766
+ return max(this, vector, new Vector4());
767
+ }
768
+
769
+ /**
770
+ * Raise each component of this vector to the given power.
771
+ * @param scalar - The exponent (power) to raise each component to.
772
+ * @returns The power (result of the exponentiation).
773
+ */
774
+ public pow(scalar: number): Vector4 {
775
+ return pow(this, scalar, new Vector4());
797
776
  }
798
777
 
799
778
  /**
800
779
  * Scale this vector by a scalar.
801
780
  * @param scalar - The scalar.
802
- * @param out - The vector to store the result in.
803
781
  * @returns The scaled vector.
804
782
  */
805
- public scale<T extends Vector4Like = Vector4>(
806
- scalar: number,
807
- out: T = new Vector4() as Vector4 & T
808
- ): T {
809
- return scale(this, scalar, out);
783
+ public scale(scalar: number): Vector4 {
784
+ return scale(this, scalar, new Vector4());
810
785
  }
811
786
 
812
787
  /**
813
788
  * Add another vector to this one after scaling the other by a scalar.
814
789
  * @param vector - The other vector.
815
790
  * @param scalar - The scalar.
816
- * @param out - The vector to store the result in.
817
791
  * @returns The sum.
818
792
  */
819
- public scaleAndAdd<T extends Vector4Like = Vector4>(
820
- vector: Vector4Like,
821
- scalar: number,
822
- out: T = new Vector4() as Vector4 & T
823
- ): T {
824
- return scaleAndAdd(this, vector, scalar, out);
793
+ public scaleAndAdd(vector: Vector4Like, scalar: number): Vector4 {
794
+ return scaleAndAdd(this, vector, scalar, new Vector4());
825
795
  }
826
796
 
827
797
  /**
@@ -844,48 +814,47 @@ export default class Vector4
844
814
  return squaredDistance(this, vector);
845
815
  }
846
816
 
847
- /** Get the magnitude (length) of this vector. */
817
+ /** The magnitude (length) of this vector. */
848
818
  public get magnitude(): number {
849
819
  return getMagnitude(this);
850
820
  }
851
821
 
852
- /** Get the squared magnitude (length) of this vector. */
822
+ public set magnitude(value: number) {
823
+ scale(normalize(this, this), value, this);
824
+ }
825
+
826
+ /** The squared magnitude (length) of this vector. */
853
827
  public get squaredMagnitude(): number {
854
828
  return getSquaredMagnitude(this);
855
829
  }
856
830
 
831
+ public set squaredMagnitude(value: number) {
832
+ this.magnitude = Math.sqrt(value);
833
+ }
834
+
857
835
  /**
858
836
  * Negate this vector.
859
- * @param out - The vector to store the result in.
860
837
  * @returns The negated vector.
861
838
  */
862
- public negate<T extends Vector4Like = Vector4>(
863
- out: T = new Vector4() as Vector4 & T
864
- ): T {
865
- return negate(this, out);
839
+ public negate(): Vector4 {
840
+ return negate(this, new Vector4());
866
841
  }
867
842
 
868
843
  /**
869
844
  * Calculate the multiplicative inverse of the components of this vector.
870
- * @param out - The vector to store the result in.
871
845
  * @returns The inverted vector.
872
846
  */
873
- public invert<T extends Vector4Like = Vector4>(
874
- out: T = new Vector4() as Vector4 & T
875
- ): T {
876
- return invert(this, out);
847
+ public invert(): Vector4 {
848
+ return invert(this, new Vector4());
877
849
  }
878
850
 
879
851
  /**
880
852
  * Normalize this vector.
881
- * @param out - The vector to store the result in.
882
853
  * @returns The normalized vector.
883
854
  * @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
884
855
  */
885
- public normalize<T extends Vector4Like = Vector4>(
886
- out: T = new Vector4() as Vector4 & T
887
- ): T {
888
- return normalize(this, out);
856
+ public normalize(): Vector4 {
857
+ return normalize(this, new Vector4());
889
858
  }
890
859
 
891
860
  /**
@@ -902,32 +871,22 @@ export default class Vector4
902
871
  * Calculate the cross product of this and two other vectors in a four-dimensional space.
903
872
  * @param a - One other vector.
904
873
  * @param b - The other other vector.
905
- * @param out - The vector to store the result in.
906
874
  * @returns The cross product.
907
875
  * @see {@link https://en.wikipedia.org/wiki/Cross_product | Cross product}
908
876
  */
909
- public cross<T extends Vector4Like = Vector4>(
910
- a: Vector4Like,
911
- b: Vector4Like,
912
- out: T = new Vector4() as Vector4 & T
913
- ): T {
914
- return cross(this, a, b, out);
877
+ public cross(a: Vector4Like, b: Vector4Like): Vector4 {
878
+ return cross(this, a, b, new Vector4());
915
879
  }
916
880
 
917
881
  /**
918
882
  * Perform a linear interpolation between this and another vector.
919
883
  * @param vector - The other vector.
920
884
  * @param t - The interpolation amount (in `[0,1]`).
921
- * @param out - The vector to store the result in.
922
885
  * @returns The interpolated vector.
923
886
  * @see {@link https://en.wikipedia.org/wiki/Linear_interpolation | Linear interpolation}
924
887
  */
925
- public lerp<T extends Vector4Like = Vector4>(
926
- vector: Vector4Like,
927
- t: number,
928
- out: T = new Vector4() as Vector4 & T
929
- ): T {
930
- return lerp(this, vector, t, out);
888
+ public lerp(vector: Vector4Like, t: number): Vector4 {
889
+ return lerp(this, vector, t, new Vector4());
931
890
  }
932
891
 
933
892
  /**
@@ -942,15 +901,11 @@ export default class Vector4
942
901
  /**
943
902
  * Transform this vector by a four-by-four matrix.
944
903
  * @param matrix - The matrix.
945
- * @param out - The vector to store the result in.
946
904
  * @returns The transformed vector.
947
905
  * @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
948
906
  */
949
- public transformMatrix4<T extends Vector4Like = Vector4>(
950
- matrix: Matrix4Like,
951
- out: T = new Vector4() as Vector4 & T
952
- ): T {
953
- return transformMatrix4(this, matrix, out);
907
+ public transformMatrix4(matrix: Matrix4Like): Vector4 {
908
+ return transformMatrix4(this, matrix, new Vector4());
954
909
  }
955
910
 
956
911
  /**
@@ -964,14 +919,10 @@ export default class Vector4
964
919
  /**
965
920
  * Transform this vector by a quaternion.
966
921
  * @param quaternion - The quaternion.
967
- * @param out - The vector to store the result in.
968
922
  * @returns The transformed vector.
969
923
  * @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
970
924
  */
971
- public transformQuaternion<T extends Vector4Like = Vector4>(
972
- quaternion: QuaternionLike,
973
- out: T = new Vector4() as Vector4 & T
974
- ): T {
975
- return transformQuaternion(this, quaternion, out);
925
+ public transformQuaternion(quaternion: QuaternionLike): Vector4 {
926
+ return transformQuaternion(this, quaternion, new Vector4());
976
927
  }
977
928
  }
@@ -2,7 +2,7 @@
2
2
  * An error resulting from trying to use a matrix that is the wrong size.
3
3
  * @public
4
4
  */
5
- export default class PartialMatrixError extends Error {
5
+ export default class MatrixSizeError extends Error {
6
6
  /**
7
7
  * Create an error resulting from trying to use a matrix that is the wrong size.
8
8
  * @param message - The message of the error.
@@ -0,0 +1,14 @@
1
+ /**
2
+ * An error resulting from trying to use a vector that is the wrong size.
3
+ * @public
4
+ */
5
+ export default class VectorSizeError extends Error {
6
+ /**
7
+ * Create an error resulting from trying to use a vector that is the wrong size.
8
+ * @param message - The message of the error.
9
+ */
10
+ public constructor(message = "Invalid vector dimensions.") {
11
+ super(message);
12
+ this.name = "VectorSizeError";
13
+ }
14
+ }
@@ -0,0 +1,13 @@
1
+ import type AxisAngle from "../types/AxisAngle.js";
2
+ import { createVector3Like } from "../linalg/Vector3.js";
3
+
4
+ /**
5
+ * Create an empty axis-angle pair.
6
+ * @returns An empty axis-angle pair.
7
+ * @public
8
+ */
9
+ export default function createAxisAngleLike(): AxisAngle & {
10
+ axis: Float32Array;
11
+ } {
12
+ return { angle: 0, axis: createVector3Like() };
13
+ }