@predy-js/math 0.3.0-beta.1 → 0.3.0-beta.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './mat3';
2
2
  export * from './mat4';
3
+ export * from './vec2';
3
4
  export * from './vec3';
4
5
  export * from './vec4';
5
6
  export * from './vec';
package/dist/index.js CHANGED
@@ -138,6 +138,30 @@ function mat3Scale(out, a, v) {
138
138
  out[8] = a[8];
139
139
  return out;
140
140
  }
141
+ function transposeMat3(out, e) {
142
+ if (!e || out === e) {
143
+ let t;
144
+ t = out[1];
145
+ out[1] = out[3];
146
+ out[3] = t;
147
+ t = out[2];
148
+ out[2] = out[6];
149
+ out[6] = t;
150
+ t = out[5];
151
+ out[5] = out[7];
152
+ out[7] = t;
153
+ }
154
+ else {
155
+ const a01 = e[1], a02 = e[2], a12 = e[5];
156
+ out[1] = e[3];
157
+ out[2] = e[6];
158
+ out[3] = a01;
159
+ out[5] = e[7];
160
+ out[6] = a02;
161
+ out[7] = a12;
162
+ }
163
+ return out;
164
+ }
141
165
 
142
166
  const d2r$1 = Math.PI / 180;
143
167
  const matrixRotation = new Float32Array(9);
@@ -619,6 +643,144 @@ function mat4identity(out) {
619
643
  out[14] = 0;
620
644
  out[15] = 1;
621
645
  }
646
+ function transposeMat4(out, e) {
647
+ if (!e || out === e) {
648
+ let t;
649
+ t = out[1];
650
+ out[1] = out[4];
651
+ out[4] = t;
652
+ t = out[2];
653
+ out[2] = out[8];
654
+ out[8] = t;
655
+ t = out[3];
656
+ out[3] = out[12];
657
+ out[12] = t;
658
+ //
659
+ t = out[6];
660
+ out[6] = out[9];
661
+ out[9] = t;
662
+ t = out[7];
663
+ out[7] = out[13];
664
+ out[13] = t;
665
+ t = out[11];
666
+ out[11] = out[14];
667
+ out[14] = t;
668
+ }
669
+ else {
670
+ out[0] = e[0];
671
+ out[1] = e[4];
672
+ out[2] = e[8];
673
+ out[3] = e[12];
674
+ out[4] = e[1];
675
+ out[5] = e[5];
676
+ out[6] = e[9];
677
+ out[7] = e[13];
678
+ out[8] = e[2];
679
+ out[9] = e[6];
680
+ out[10] = e[10];
681
+ out[11] = e[14];
682
+ out[12] = e[3];
683
+ out[13] = e[7];
684
+ out[14] = e[11];
685
+ out[15] = e[15];
686
+ }
687
+ return out;
688
+ }
689
+ function getMat4Transform(te) {
690
+ // Extract the translation
691
+ const translation = [te[12], te[13], te[14]];
692
+ // Extract the scale
693
+ const sx = Math.hypot(te[0], te[1], te[2]);
694
+ const sy = Math.hypot(te[4], te[5], te[6]);
695
+ const sz = Math.hypot(te[8], te[9], te[10]);
696
+ const scale = [sx, sy, sz];
697
+ // Remove the scale from the matrix
698
+ const matrix = [
699
+ te[0] / sx, te[1] / sx, te[2] / sx, 0,
700
+ te[4] / sy, te[5] / sy, te[6] / sy, 0,
701
+ te[8] / sz, te[9] / sz, te[10] / sz, 0,
702
+ 0, 0, 0, 1,
703
+ ];
704
+ // Extract the rotation as a quaternion
705
+ const m11 = matrix[0];
706
+ const m12 = matrix[4];
707
+ const m13 = matrix[8];
708
+ const m21 = matrix[1];
709
+ const m22 = matrix[5];
710
+ const m23 = matrix[9];
711
+ const m31 = matrix[2];
712
+ const m32 = matrix[6];
713
+ const m33 = matrix[10];
714
+ const trace = m11 + m22 + m33;
715
+ let s;
716
+ let x;
717
+ let y;
718
+ let z;
719
+ let w;
720
+ if (trace > 0) {
721
+ s = 0.5 / Math.sqrt(trace + 1.0);
722
+ w = 0.25 / s;
723
+ x = (m32 - m23) * s;
724
+ y = (m13 - m31) * s;
725
+ z = (m21 - m12) * s;
726
+ }
727
+ else if (m11 > m22 && m11 > m33) {
728
+ s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
729
+ w = (m32 - m23) / s;
730
+ x = 0.25 * s;
731
+ y = (m12 + m21) / s;
732
+ z = (m13 + m31) / s;
733
+ }
734
+ else if (m22 > m33) {
735
+ s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
736
+ w = (m13 - m31) / s;
737
+ x = (m12 + m21) / s;
738
+ y = 0.25 * s;
739
+ z = (m23 + m32) / s;
740
+ }
741
+ else {
742
+ s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
743
+ w = (m21 - m12) / s;
744
+ x = (m13 + m31) / s;
745
+ y = (m23 + m32) / s;
746
+ z = 0.25 * s;
747
+ }
748
+ const rotation = [x, y, z, w];
749
+ return { translation, rotation, scale };
750
+ }
751
+
752
+ // create vec2
753
+ function vec2Create(v2) {
754
+ if (v2) {
755
+ return [v2[0], v2[1]];
756
+ }
757
+ return [0, 0];
758
+ }
759
+ // vec2 add vec2
760
+ function vec2AddVec2(out, a, b) {
761
+ out[0] = a[0] + b[0];
762
+ out[1] = a[1] + b[1];
763
+ return out;
764
+ }
765
+ // vec2 sub vec2
766
+ function vec2SubVec2(out, a, b) {
767
+ out[0] = a[0] - b[0];
768
+ out[1] = a[1] - b[1];
769
+ return out;
770
+ }
771
+ // vec2 mul vec2
772
+ function vec2MulVec2(out, a, b) {
773
+ out[0] = a[0] * b[0];
774
+ out[1] = a[1] * b[1];
775
+ return out;
776
+ }
777
+ // vec2 transform by mat3
778
+ function vec2MulMat3(out, a, m) {
779
+ const x = a[0], y = a[1];
780
+ out[0] = x * m[0] + y * m[3] + m[6];
781
+ out[1] = x * m[1] + y * m[4] + m[7];
782
+ return out;
783
+ }
622
784
 
623
785
  const cos$1 = Math.cos;
624
786
  const sin$1 = Math.sin;
@@ -628,6 +790,12 @@ function vecAdd(out, a, b) {
628
790
  }
629
791
  return out;
630
792
  }
793
+ function vecSub(out, a, b) {
794
+ for (let i = 0, len = a.length; i < len; i++) {
795
+ out[i] = a[i] - b[i];
796
+ }
797
+ return out;
798
+ }
631
799
  function vecFill(out, number) {
632
800
  for (let i = 0, len = out.length; i < len; i++) {
633
801
  out[i] = number;
@@ -932,6 +1100,7 @@ exports.clamp = clamp;
932
1100
  exports.ensureVec3 = ensureVec3;
933
1101
  exports.getMat4TR = getMat4TR;
934
1102
  exports.getMat4TRS = getMat4TRS;
1103
+ exports.getMat4Transform = getMat4Transform;
935
1104
  exports.invertMat4 = invertMat4;
936
1105
  exports.isZeroVec = isZeroVec;
937
1106
  exports.mat3FromQuat = mat3FromQuat;
@@ -961,6 +1130,13 @@ exports.quatMultiply = quatMultiply;
961
1130
  exports.quatStar = quatStar;
962
1131
  exports.rotateVec2 = rotateVec2;
963
1132
  exports.rotationFromMat3 = rotationFromMat3;
1133
+ exports.transposeMat3 = transposeMat3;
1134
+ exports.transposeMat4 = transposeMat4;
1135
+ exports.vec2AddVec2 = vec2AddVec2;
1136
+ exports.vec2Create = vec2Create;
1137
+ exports.vec2MulMat3 = vec2MulMat3;
1138
+ exports.vec2MulVec2 = vec2MulVec2;
1139
+ exports.vec2SubVec2 = vec2SubVec2;
964
1140
  exports.vec2TransformByMat3 = vec2TransformByMat3;
965
1141
  exports.vec3AddVec3 = vec3AddVec3;
966
1142
  exports.vec3Create = vec3Create;
@@ -983,3 +1159,4 @@ exports.vecMulCombine = vecMulCombine;
983
1159
  exports.vecMulScalar = vecMulScalar;
984
1160
  exports.vecNormalize = vecNormalize;
985
1161
  exports.vecSquareDistance = vecSquareDistance;
1162
+ exports.vecSub = vecSub;
package/dist/index.mjs CHANGED
@@ -134,6 +134,30 @@ function mat3Scale(out, a, v) {
134
134
  out[8] = a[8];
135
135
  return out;
136
136
  }
137
+ function transposeMat3(out, e) {
138
+ if (!e || out === e) {
139
+ let t;
140
+ t = out[1];
141
+ out[1] = out[3];
142
+ out[3] = t;
143
+ t = out[2];
144
+ out[2] = out[6];
145
+ out[6] = t;
146
+ t = out[5];
147
+ out[5] = out[7];
148
+ out[7] = t;
149
+ }
150
+ else {
151
+ const a01 = e[1], a02 = e[2], a12 = e[5];
152
+ out[1] = e[3];
153
+ out[2] = e[6];
154
+ out[3] = a01;
155
+ out[5] = e[7];
156
+ out[6] = a02;
157
+ out[7] = a12;
158
+ }
159
+ return out;
160
+ }
137
161
 
138
162
  const d2r$1 = Math.PI / 180;
139
163
  const matrixRotation = new Float32Array(9);
@@ -615,6 +639,144 @@ function mat4identity(out) {
615
639
  out[14] = 0;
616
640
  out[15] = 1;
617
641
  }
642
+ function transposeMat4(out, e) {
643
+ if (!e || out === e) {
644
+ let t;
645
+ t = out[1];
646
+ out[1] = out[4];
647
+ out[4] = t;
648
+ t = out[2];
649
+ out[2] = out[8];
650
+ out[8] = t;
651
+ t = out[3];
652
+ out[3] = out[12];
653
+ out[12] = t;
654
+ //
655
+ t = out[6];
656
+ out[6] = out[9];
657
+ out[9] = t;
658
+ t = out[7];
659
+ out[7] = out[13];
660
+ out[13] = t;
661
+ t = out[11];
662
+ out[11] = out[14];
663
+ out[14] = t;
664
+ }
665
+ else {
666
+ out[0] = e[0];
667
+ out[1] = e[4];
668
+ out[2] = e[8];
669
+ out[3] = e[12];
670
+ out[4] = e[1];
671
+ out[5] = e[5];
672
+ out[6] = e[9];
673
+ out[7] = e[13];
674
+ out[8] = e[2];
675
+ out[9] = e[6];
676
+ out[10] = e[10];
677
+ out[11] = e[14];
678
+ out[12] = e[3];
679
+ out[13] = e[7];
680
+ out[14] = e[11];
681
+ out[15] = e[15];
682
+ }
683
+ return out;
684
+ }
685
+ function getMat4Transform(te) {
686
+ // Extract the translation
687
+ const translation = [te[12], te[13], te[14]];
688
+ // Extract the scale
689
+ const sx = Math.hypot(te[0], te[1], te[2]);
690
+ const sy = Math.hypot(te[4], te[5], te[6]);
691
+ const sz = Math.hypot(te[8], te[9], te[10]);
692
+ const scale = [sx, sy, sz];
693
+ // Remove the scale from the matrix
694
+ const matrix = [
695
+ te[0] / sx, te[1] / sx, te[2] / sx, 0,
696
+ te[4] / sy, te[5] / sy, te[6] / sy, 0,
697
+ te[8] / sz, te[9] / sz, te[10] / sz, 0,
698
+ 0, 0, 0, 1,
699
+ ];
700
+ // Extract the rotation as a quaternion
701
+ const m11 = matrix[0];
702
+ const m12 = matrix[4];
703
+ const m13 = matrix[8];
704
+ const m21 = matrix[1];
705
+ const m22 = matrix[5];
706
+ const m23 = matrix[9];
707
+ const m31 = matrix[2];
708
+ const m32 = matrix[6];
709
+ const m33 = matrix[10];
710
+ const trace = m11 + m22 + m33;
711
+ let s;
712
+ let x;
713
+ let y;
714
+ let z;
715
+ let w;
716
+ if (trace > 0) {
717
+ s = 0.5 / Math.sqrt(trace + 1.0);
718
+ w = 0.25 / s;
719
+ x = (m32 - m23) * s;
720
+ y = (m13 - m31) * s;
721
+ z = (m21 - m12) * s;
722
+ }
723
+ else if (m11 > m22 && m11 > m33) {
724
+ s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
725
+ w = (m32 - m23) / s;
726
+ x = 0.25 * s;
727
+ y = (m12 + m21) / s;
728
+ z = (m13 + m31) / s;
729
+ }
730
+ else if (m22 > m33) {
731
+ s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
732
+ w = (m13 - m31) / s;
733
+ x = (m12 + m21) / s;
734
+ y = 0.25 * s;
735
+ z = (m23 + m32) / s;
736
+ }
737
+ else {
738
+ s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
739
+ w = (m21 - m12) / s;
740
+ x = (m13 + m31) / s;
741
+ y = (m23 + m32) / s;
742
+ z = 0.25 * s;
743
+ }
744
+ const rotation = [x, y, z, w];
745
+ return { translation, rotation, scale };
746
+ }
747
+
748
+ // create vec2
749
+ function vec2Create(v2) {
750
+ if (v2) {
751
+ return [v2[0], v2[1]];
752
+ }
753
+ return [0, 0];
754
+ }
755
+ // vec2 add vec2
756
+ function vec2AddVec2(out, a, b) {
757
+ out[0] = a[0] + b[0];
758
+ out[1] = a[1] + b[1];
759
+ return out;
760
+ }
761
+ // vec2 sub vec2
762
+ function vec2SubVec2(out, a, b) {
763
+ out[0] = a[0] - b[0];
764
+ out[1] = a[1] - b[1];
765
+ return out;
766
+ }
767
+ // vec2 mul vec2
768
+ function vec2MulVec2(out, a, b) {
769
+ out[0] = a[0] * b[0];
770
+ out[1] = a[1] * b[1];
771
+ return out;
772
+ }
773
+ // vec2 transform by mat3
774
+ function vec2MulMat3(out, a, m) {
775
+ const x = a[0], y = a[1];
776
+ out[0] = x * m[0] + y * m[3] + m[6];
777
+ out[1] = x * m[1] + y * m[4] + m[7];
778
+ return out;
779
+ }
618
780
 
619
781
  const cos$1 = Math.cos;
620
782
  const sin$1 = Math.sin;
@@ -624,6 +786,12 @@ function vecAdd(out, a, b) {
624
786
  }
625
787
  return out;
626
788
  }
789
+ function vecSub(out, a, b) {
790
+ for (let i = 0, len = a.length; i < len; i++) {
791
+ out[i] = a[i] - b[i];
792
+ }
793
+ return out;
794
+ }
627
795
  function vecFill(out, number) {
628
796
  for (let i = 0, len = out.length; i < len; i++) {
629
797
  out[i] = number;
@@ -923,4 +1091,4 @@ function quatStar(out, quat) {
923
1091
  return out;
924
1092
  }
925
1093
 
926
- export { NumberEpsilon, clamp, ensureVec3, getMat4TR, getMat4TRS, invertMat4, isZeroVec, mat3FromQuat, mat3FromRotation, mat3FromRotationZ, mat3MulMat3, mat3NormalFromMat4, mat3Rotate, mat3Scale, mat3Translate, mat3create, mat4Clone, mat4Determinate, mat4create, mat4fromRotationTranslationScale, mat4identity, mat4invert, mat4multiply, mat4perspective, mat4rotate, mat4scale, mat4translate, quatCreate, quatEquals, quatFromRotation, quatMultiply, quatStar, rotateVec2, rotationFromMat3, vec2TransformByMat3, vec3AddVec3, vec3Create, vec3Cross, vec3MulMat3, vec3MulMat4, vec3MulNum, vec3MulVec3, vec3RotateByMat4, vec3RotateByQuat, vec3TranslateByMat4, vec4Create, vecAdd, vecAddCombine, vecAssign, vecDot, vecFill, vecMinus, vecMulCombine, vecMulScalar, vecNormalize, vecSquareDistance };
1094
+ export { NumberEpsilon, clamp, ensureVec3, getMat4TR, getMat4TRS, getMat4Transform, invertMat4, isZeroVec, mat3FromQuat, mat3FromRotation, mat3FromRotationZ, mat3MulMat3, mat3NormalFromMat4, mat3Rotate, mat3Scale, mat3Translate, mat3create, mat4Clone, mat4Determinate, mat4create, mat4fromRotationTranslationScale, mat4identity, mat4invert, mat4multiply, mat4perspective, mat4rotate, mat4scale, mat4translate, quatCreate, quatEquals, quatFromRotation, quatMultiply, quatStar, rotateVec2, rotationFromMat3, transposeMat3, transposeMat4, vec2AddVec2, vec2Create, vec2MulMat3, vec2MulVec2, vec2SubVec2, vec2TransformByMat3, vec3AddVec3, vec3Create, vec3Cross, vec3MulMat3, vec3MulMat4, vec3MulNum, vec3MulVec3, vec3RotateByMat4, vec3RotateByQuat, vec3TranslateByMat4, vec4Create, vecAdd, vecAddCombine, vecAssign, vecDot, vecFill, vecMinus, vecMulCombine, vecMulScalar, vecNormalize, vecSquareDistance, vecSub };
package/dist/mat3.d.ts CHANGED
@@ -7,3 +7,4 @@ export declare function mat3NormalFromMat4(out: mat3 | number[], a: mat4): mat3;
7
7
  export declare function mat3Translate(out: mat3, a: mat3, v: vec2): mat3;
8
8
  export declare function mat3Rotate(out: mat3, a: mat3, rad: number): mat3;
9
9
  export declare function mat3Scale(out: mat3, a: mat3, v: vec2): mat3;
10
+ export declare function transposeMat3(out: mat3 | number[], e?: mat3): mat3;
package/dist/mat4.d.ts CHANGED
@@ -14,3 +14,9 @@ export declare function mat4scale(out: mat4, a: mat4, v: vec3): mat4;
14
14
  export declare function mat4rotate(out: mat4, a: mat4, rad: number, axis: vec3): mat4;
15
15
  export declare function mat4perspective(out: number[] | mat4, fovy: number, aspect: number, near: number, far: number, reverse?: boolean): mat4;
16
16
  export declare function mat4identity(out: mat4): void;
17
+ export declare function transposeMat4(out: mat4 | number[], e?: mat4): mat4;
18
+ export declare function getMat4Transform(te: mat4): {
19
+ translation: vec3;
20
+ rotation: vec4;
21
+ scale: vec3;
22
+ };
package/dist/vec.d.ts CHANGED
@@ -2,6 +2,7 @@ import type { vec3, vec4, vec2, mat4, mat2, mat3 } from '@predy-js/render-interf
2
2
  export type vec = number[];
3
3
  export { vec2, vec4, vec3, mat2, mat3, mat4 };
4
4
  export declare function vecAdd<T extends vec | vec3 | vec4 | vec2>(out: T | number[], a: T, b: T): T;
5
+ export declare function vecSub<T extends vec | vec3 | vec4 | vec2>(out: T | number[], a: T, b: T): T;
5
6
  export declare function vecFill<T extends vec | vec3 | vec4 | vec2>(out: T | number[], number: number): T;
6
7
  export declare function vecAddCombine<T extends vec | vec3 | vec4 | vec2>(out: T | number[], a: T, b: T): T;
7
8
  export declare function vecAssign<T extends vec | vec3 | vec4 | vec2>(out: T | number[], a: T, count: number, start?: number): T;
package/dist/vec2.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { vec2, mat3 } from '@predy-js/render-interface';
2
+ export declare function vec2Create(v2?: vec2): number[];
3
+ export declare function vec2AddVec2(out: vec2, a: vec2, b: vec2): vec2;
4
+ export declare function vec2SubVec2(out: vec2, a: vec2, b: vec2): vec2;
5
+ export declare function vec2MulVec2(out: vec2, a: vec2, b: vec2): vec2;
6
+ export declare function vec2MulMat3(out: vec2, a: vec2, m: mat3): vec2;
package/dist/vec3.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { vec3, mat4, mat3 } from '@predy-js/render-interface';
2
2
  import type { vec4 } from '@predy-js/render-interface';
3
- export declare function vec3Create(v?: vec3): vec3;
3
+ export declare function vec3Create(v?: vec3 | number[] | Float32Array): vec3;
4
4
  export declare function vec3MulVec3(out: vec3, a: vec3, b: vec3): vec3;
5
5
  export declare function vec3AddVec3(out: vec3, a: vec3, b: vec3): vec3;
6
6
  export declare function vec3Cross(out: vec3, a: vec3, b: vec3): vec3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@predy-js/math",
3
- "version": "0.3.0-beta.1",
3
+ "version": "0.3.0-beta.2",
4
4
  "description": "Mars JSON Specification",
5
5
  "module": "./dist/index.mjs",
6
6
  "main": "./dist/index.js",
@@ -28,7 +28,7 @@
28
28
  "prepublishOnly": "npm run build"
29
29
  },
30
30
  "devDependencies": {
31
- "@predy-js/render-interface": "0.3.0-beta.1",
31
+ "@predy-js/render-interface": "0.3.0-beta.2",
32
32
  "@commitlint/cli": "^13.2.1",
33
33
  "@commitlint/config-conventional": "^13.2.0",
34
34
  "@rollup/plugin-commonjs": "^21.0.3",