@lakuna/umath 1.5.0 → 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 (73) hide show
  1. package/dist/index.d.ts +4 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +6 -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 +14 -28
  10. package/dist/linalg/Matrix2.d.ts.map +1 -1
  11. package/dist/linalg/Matrix2.js +28 -42
  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 +39 -78
  18. package/dist/linalg/Matrix4.d.ts.map +1 -1
  19. package/dist/linalg/Matrix4.js +85 -170
  20. package/dist/linalg/Matrix4.js.map +1 -1
  21. package/dist/linalg/Quaternion.d.ts +27 -58
  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/SlowVector.d.ts +165 -0
  29. package/dist/linalg/SlowVector.d.ts.map +1 -0
  30. package/dist/linalg/SlowVector.js +369 -0
  31. package/dist/linalg/SlowVector.js.map +1 -0
  32. package/dist/linalg/Vector.d.ts +14 -15
  33. package/dist/linalg/Vector.d.ts.map +1 -1
  34. package/dist/linalg/Vector2.d.ts +28 -50
  35. package/dist/linalg/Vector2.d.ts.map +1 -1
  36. package/dist/linalg/Vector2.js +59 -85
  37. package/dist/linalg/Vector2.js.map +1 -1
  38. package/dist/linalg/Vector3.d.ts +32 -58
  39. package/dist/linalg/Vector3.d.ts.map +1 -1
  40. package/dist/linalg/Vector3.js +67 -107
  41. package/dist/linalg/Vector3.js.map +1 -1
  42. package/dist/linalg/Vector4.d.ts +26 -46
  43. package/dist/linalg/Vector4.d.ts.map +1 -1
  44. package/dist/linalg/Vector4.js +54 -82
  45. package/dist/linalg/Vector4.js.map +1 -1
  46. package/dist/utility/MatrixSizeError.d.ts +1 -1
  47. package/dist/utility/MatrixSizeError.d.ts.map +1 -1
  48. package/dist/utility/MatrixSizeError.js +1 -1
  49. package/dist/utility/MatrixSizeError.js.map +1 -1
  50. package/dist/utility/VectorSizeError.d.ts +12 -0
  51. package/dist/utility/VectorSizeError.d.ts.map +1 -0
  52. package/dist/utility/VectorSizeError.js +15 -0
  53. package/dist/utility/VectorSizeError.js.map +1 -0
  54. package/dist/utility/createAxisAngleLike.d.ts +10 -0
  55. package/dist/utility/createAxisAngleLike.d.ts.map +1 -0
  56. package/dist/utility/createAxisAngleLike.js +10 -0
  57. package/dist/utility/createAxisAngleLike.js.map +1 -0
  58. package/package.json +1 -3
  59. package/src/index.ts +7 -0
  60. package/src/linalg/DualQuaternion.ts +51 -134
  61. package/src/linalg/Matrix2.ts +30 -81
  62. package/src/linalg/Matrix3.ts +55 -149
  63. package/src/linalg/Matrix4.ts +135 -291
  64. package/src/linalg/Quaternion.ts +60 -151
  65. package/src/linalg/SlowMatrix.ts +82 -81
  66. package/src/linalg/SlowVector.ts +449 -0
  67. package/src/linalg/Vector.ts +14 -16
  68. package/src/linalg/Vector2.ts +63 -153
  69. package/src/linalg/Vector3.ts +75 -194
  70. package/src/linalg/Vector4.ts +60 -143
  71. package/src/utility/MatrixSizeError.ts +1 -1
  72. package/src/utility/VectorSizeError.ts +14 -0
  73. package/src/utility/createAxisAngleLike.ts +13 -0
@@ -322,13 +322,8 @@ export const scaleAndAdd = <T extends Vector4Like>(
322
322
  * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
323
323
  * @public
324
324
  */
325
- export const distance = (a: Vector4Like, b: Vector4Like): number => {
326
- const x = a[0] - b[0];
327
- const y = a[1] - b[1];
328
- const z = a[2] - b[2];
329
- const w = a[3] - b[3];
330
- return Math.sqrt(x * x + y * y + z * z + w * w); // `Math.hypot` is slower.
331
- };
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]);
332
327
 
333
328
  /**
334
329
  * Calculate the squared Euclidean distance between two vectors.
@@ -352,13 +347,8 @@ export const squaredDistance = (a: Vector4Like, b: Vector4Like): number => {
352
347
  * @returns The magnitude.
353
348
  * @public
354
349
  */
355
- export const getMagnitude = (vector: Vector4Like): number => {
356
- const x = vector[0];
357
- const y = vector[1];
358
- const z = vector[2];
359
- const w = vector[3];
360
- return Math.sqrt(x * x + y * y + z * z + w * w); // `Math.hypot` is slower.
361
- };
350
+ export const getMagnitude = (vector: Vector4Like): number =>
351
+ Math.hypot(vector[0], vector[1], vector[2], vector[3]);
362
352
 
363
353
  /**
364
354
  * Calculate the squared magnitude (length) of a vector.
@@ -624,17 +614,15 @@ export default class Vector4
624
614
  * @param y - The second component.
625
615
  * @param z - The third component.
626
616
  * @param w - The fourth component.
627
- * @param out - The vector to store the result in.
628
617
  * @returns A new vector.
629
618
  */
630
- public static fromValues<T extends Vector4Like = Vector4>(
619
+ public static fromValues(
631
620
  x: number,
632
621
  y: number,
633
622
  z: number,
634
- w: number,
635
- out: T = new Vector4() as Vector4 & T
636
- ): T {
637
- return fromValues(x, y, z, w, out);
623
+ w: number
624
+ ): Vector4 {
625
+ return fromValues(x, y, z, w, new Vector4());
638
626
  }
639
627
 
640
628
  /**
@@ -678,25 +666,18 @@ export default class Vector4
678
666
  /**
679
667
  * Add two vectors of the same size.
680
668
  * @param vector - The other vector.
681
- * @param out - The vector to store the result in.
682
669
  * @returns The sum of the vectors.
683
670
  */
684
- public add<T extends Vector4Like = Vector4>(
685
- vector: Vector4Like,
686
- out: T = new Vector4() as Vector4 & T
687
- ): T {
688
- return add(this, vector, out);
671
+ public add(vector: Vector4Like): Vector4 {
672
+ return add(this, vector, new Vector4());
689
673
  }
690
674
 
691
675
  /**
692
676
  * Copy the values from this vector to another one.
693
- * @param out - The vector to store the result in.
694
677
  * @returns The copy.
695
678
  */
696
- public clone<T extends Vector4Like = Vector4>(
697
- out: T = new Vector4() as Vector4 & T
698
- ): T {
699
- return copy(this, out);
679
+ public clone(): Vector4 {
680
+ return copy(this, new Vector4());
700
681
  }
701
682
 
702
683
  /**
@@ -711,151 +692,106 @@ export default class Vector4
711
692
  /**
712
693
  * Multiply this vector by another.
713
694
  * @param vector - The other vector.
714
- * @param out - The vector to store the result in.
715
695
  * @returns The product of the vectors.
716
696
  */
717
- public multiply<T extends Vector4Like = Vector4>(
718
- vector: Vector4Like,
719
- out: T = new Vector4() as Vector4 & T
720
- ): T {
721
- return multiply(this, vector, out);
697
+ public multiply(vector: Vector4Like): Vector4 {
698
+ return multiply(this, vector, new Vector4());
722
699
  }
723
700
 
724
701
  /**
725
702
  * Divide this vector by another.
726
703
  * @param vector - The other vector.
727
- * @param out - The vector to store the result in.
728
704
  * @returns The quotient of the vectors.
729
705
  */
730
- public divide<T extends Vector4Like = Vector4>(
731
- vector: Vector4Like,
732
- out: T = new Vector4() as Vector4 & T
733
- ): T {
734
- return divide(this, vector, out);
706
+ public divide(vector: Vector4Like): Vector4 {
707
+ return divide(this, vector, new Vector4());
735
708
  }
736
709
 
737
710
  /**
738
711
  * Subtract another vector from this one.
739
712
  * @param vector - The other vector.
740
- * @param out - The vector to store the result in.
741
713
  * @returns The difference between the vectors.
742
714
  */
743
- public subtract<T extends Vector4Like = Vector4>(
744
- vector: Vector4Like,
745
- out: T = new Vector4() as Vector4 & T
746
- ): T {
747
- return subtract(this, vector, out);
715
+ public subtract(vector: Vector4Like): Vector4 {
716
+ return subtract(this, vector, new Vector4());
748
717
  }
749
718
 
750
719
  /**
751
720
  * Absolutize the components of this vector.
752
- * @param out - The vector to store the result in.
753
721
  * @returns The absolutized vector.
754
722
  */
755
- public abs<T extends Vector4Like = Vector4>(
756
- out: T = new Vector4() as Vector4 & T
757
- ): T {
758
- return abs(this, out);
723
+ public abs(): Vector4 {
724
+ return abs(this, new Vector4());
759
725
  }
760
726
 
761
727
  /**
762
728
  * Round up the components of this vector.
763
- * @param out - The vector to store the result in.
764
729
  * @returns The rounded vector.
765
730
  */
766
- public ceil<T extends Vector4Like = Vector4>(
767
- out: T = new Vector4() as Vector4 & T
768
- ): T {
769
- return ceil(this, out);
731
+ public ceil(): Vector4 {
732
+ return ceil(this, new Vector4());
770
733
  }
771
734
 
772
735
  /**
773
736
  * Round down the components of this vector.
774
- * @param out - The vector to store the result in.
775
737
  * @returns The rounded vector.
776
738
  */
777
- public floor<T extends Vector4Like = Vector4>(
778
- out: T = new Vector4() as Vector4 & T
779
- ): T {
780
- return floor(this, out);
739
+ public floor(): Vector4 {
740
+ return floor(this, new Vector4());
781
741
  }
782
742
 
783
743
  /**
784
744
  * Round the components of this vector.
785
- * @param out - The vector to store the result in.
786
745
  * @returns The rounded vector.
787
746
  */
788
- public round<T extends Vector4Like = Vector4>(
789
- out: T = new Vector4() as Vector4 & T
790
- ): T {
791
- return round(this, out);
747
+ public round(): Vector4 {
748
+ return round(this, new Vector4());
792
749
  }
793
750
 
794
751
  /**
795
752
  * Return the minimum of this and another vector.
796
753
  * @param vector - The other vector.
797
- * @param out - The vector to store the result in.
798
754
  * @returns The minimum.
799
755
  */
800
- public min<T extends Vector4Like = Vector4>(
801
- vector: Vector4Like,
802
- out: T = new Vector4() as Vector4 & T
803
- ): T {
804
- return min(this, vector, out);
756
+ public min(vector: Vector4Like): Vector4 {
757
+ return min(this, vector, new Vector4());
805
758
  }
806
759
 
807
760
  /**
808
761
  * Return the maximum of this and another vector.
809
762
  * @param vector - The other vector.
810
- * @param out - The vector to store the result in.
811
763
  * @returns The maximum.
812
764
  */
813
- public max<T extends Vector4Like = Vector4>(
814
- vector: Vector4Like,
815
- out: T = new Vector4() as Vector4 & T
816
- ): T {
817
- return max(this, vector, out);
765
+ public max(vector: Vector4Like): Vector4 {
766
+ return max(this, vector, new Vector4());
818
767
  }
819
768
 
820
769
  /**
821
770
  * Raise each component of this vector to the given power.
822
771
  * @param scalar - The exponent (power) to raise each component to.
823
- * @param out - The vector to store the result in.
824
772
  * @returns The power (result of the exponentiation).
825
773
  */
826
- public pow<T extends Vector4Like = Vector4>(
827
- scalar: number,
828
- out: T = new Vector4() as Vector4 & T
829
- ): T {
830
- return pow(this, scalar, out);
774
+ public pow(scalar: number): Vector4 {
775
+ return pow(this, scalar, new Vector4());
831
776
  }
832
777
 
833
778
  /**
834
779
  * Scale this vector by a scalar.
835
780
  * @param scalar - The scalar.
836
- * @param out - The vector to store the result in.
837
781
  * @returns The scaled vector.
838
782
  */
839
- public scale<T extends Vector4Like = Vector4>(
840
- scalar: number,
841
- out: T = new Vector4() as Vector4 & T
842
- ): T {
843
- return scale(this, scalar, out);
783
+ public scale(scalar: number): Vector4 {
784
+ return scale(this, scalar, new Vector4());
844
785
  }
845
786
 
846
787
  /**
847
788
  * Add another vector to this one after scaling the other by a scalar.
848
789
  * @param vector - The other vector.
849
790
  * @param scalar - The scalar.
850
- * @param out - The vector to store the result in.
851
791
  * @returns The sum.
852
792
  */
853
- public scaleAndAdd<T extends Vector4Like = Vector4>(
854
- vector: Vector4Like,
855
- scalar: number,
856
- out: T = new Vector4() as Vector4 & T
857
- ): T {
858
- return scaleAndAdd(this, vector, scalar, out);
793
+ public scaleAndAdd(vector: Vector4Like, scalar: number): Vector4 {
794
+ return scaleAndAdd(this, vector, scalar, new Vector4());
859
795
  }
860
796
 
861
797
  /**
@@ -878,48 +814,47 @@ export default class Vector4
878
814
  return squaredDistance(this, vector);
879
815
  }
880
816
 
881
- /** Get the magnitude (length) of this vector. */
817
+ /** The magnitude (length) of this vector. */
882
818
  public get magnitude(): number {
883
819
  return getMagnitude(this);
884
820
  }
885
821
 
886
- /** 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. */
887
827
  public get squaredMagnitude(): number {
888
828
  return getSquaredMagnitude(this);
889
829
  }
890
830
 
831
+ public set squaredMagnitude(value: number) {
832
+ this.magnitude = Math.sqrt(value);
833
+ }
834
+
891
835
  /**
892
836
  * Negate this vector.
893
- * @param out - The vector to store the result in.
894
837
  * @returns The negated vector.
895
838
  */
896
- public negate<T extends Vector4Like = Vector4>(
897
- out: T = new Vector4() as Vector4 & T
898
- ): T {
899
- return negate(this, out);
839
+ public negate(): Vector4 {
840
+ return negate(this, new Vector4());
900
841
  }
901
842
 
902
843
  /**
903
844
  * Calculate the multiplicative inverse of the components of this vector.
904
- * @param out - The vector to store the result in.
905
845
  * @returns The inverted vector.
906
846
  */
907
- public invert<T extends Vector4Like = Vector4>(
908
- out: T = new Vector4() as Vector4 & T
909
- ): T {
910
- return invert(this, out);
847
+ public invert(): Vector4 {
848
+ return invert(this, new Vector4());
911
849
  }
912
850
 
913
851
  /**
914
852
  * Normalize this vector.
915
- * @param out - The vector to store the result in.
916
853
  * @returns The normalized vector.
917
854
  * @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
918
855
  */
919
- public normalize<T extends Vector4Like = Vector4>(
920
- out: T = new Vector4() as Vector4 & T
921
- ): T {
922
- return normalize(this, out);
856
+ public normalize(): Vector4 {
857
+ return normalize(this, new Vector4());
923
858
  }
924
859
 
925
860
  /**
@@ -936,32 +871,22 @@ export default class Vector4
936
871
  * Calculate the cross product of this and two other vectors in a four-dimensional space.
937
872
  * @param a - One other vector.
938
873
  * @param b - The other other vector.
939
- * @param out - The vector to store the result in.
940
874
  * @returns The cross product.
941
875
  * @see {@link https://en.wikipedia.org/wiki/Cross_product | Cross product}
942
876
  */
943
- public cross<T extends Vector4Like = Vector4>(
944
- a: Vector4Like,
945
- b: Vector4Like,
946
- out: T = new Vector4() as Vector4 & T
947
- ): T {
948
- return cross(this, a, b, out);
877
+ public cross(a: Vector4Like, b: Vector4Like): Vector4 {
878
+ return cross(this, a, b, new Vector4());
949
879
  }
950
880
 
951
881
  /**
952
882
  * Perform a linear interpolation between this and another vector.
953
883
  * @param vector - The other vector.
954
884
  * @param t - The interpolation amount (in `[0,1]`).
955
- * @param out - The vector to store the result in.
956
885
  * @returns The interpolated vector.
957
886
  * @see {@link https://en.wikipedia.org/wiki/Linear_interpolation | Linear interpolation}
958
887
  */
959
- public lerp<T extends Vector4Like = Vector4>(
960
- vector: Vector4Like,
961
- t: number,
962
- out: T = new Vector4() as Vector4 & T
963
- ): T {
964
- return lerp(this, vector, t, out);
888
+ public lerp(vector: Vector4Like, t: number): Vector4 {
889
+ return lerp(this, vector, t, new Vector4());
965
890
  }
966
891
 
967
892
  /**
@@ -976,15 +901,11 @@ export default class Vector4
976
901
  /**
977
902
  * Transform this vector by a four-by-four matrix.
978
903
  * @param matrix - The matrix.
979
- * @param out - The vector to store the result in.
980
904
  * @returns The transformed vector.
981
905
  * @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
982
906
  */
983
- public transformMatrix4<T extends Vector4Like = Vector4>(
984
- matrix: Matrix4Like,
985
- out: T = new Vector4() as Vector4 & T
986
- ): T {
987
- return transformMatrix4(this, matrix, out);
907
+ public transformMatrix4(matrix: Matrix4Like): Vector4 {
908
+ return transformMatrix4(this, matrix, new Vector4());
988
909
  }
989
910
 
990
911
  /**
@@ -998,14 +919,10 @@ export default class Vector4
998
919
  /**
999
920
  * Transform this vector by a quaternion.
1000
921
  * @param quaternion - The quaternion.
1001
- * @param out - The vector to store the result in.
1002
922
  * @returns The transformed vector.
1003
923
  * @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
1004
924
  */
1005
- public transformQuaternion<T extends Vector4Like = Vector4>(
1006
- quaternion: QuaternionLike,
1007
- out: T = new Vector4() as Vector4 & T
1008
- ): T {
1009
- return transformQuaternion(this, quaternion, out);
925
+ public transformQuaternion(quaternion: QuaternionLike): Vector4 {
926
+ return transformQuaternion(this, quaternion, new Vector4());
1010
927
  }
1011
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
+ }