@wemap/maths 11.0.1 → 12.10.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.
- package/dist/index.js +103 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +102 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/src/Matrix4.ts +7 -0
- package/src/Quaternion.ts +33 -0
package/dist/index.mjs
CHANGED
|
@@ -104,6 +104,8 @@ class Matrix {
|
|
|
104
104
|
}
|
|
105
105
|
return output;
|
|
106
106
|
}
|
|
107
|
+
// Returns the inverse of matrix M
|
|
108
|
+
// Helped from http://blog.acipo.com/matrix-inversion-in-javascript/
|
|
107
109
|
static inverse(M) {
|
|
108
110
|
if (M.length !== M[0].length) {
|
|
109
111
|
return null;
|
|
@@ -255,6 +257,7 @@ class Matrix3 {
|
|
|
255
257
|
[-v[1], v[0], 0]
|
|
256
258
|
];
|
|
257
259
|
}
|
|
260
|
+
// Helped from https://github.com/mrdoob/three.js/blob/34dc2478c684066257e4e39351731a93c6107ef5/src/math/Matrix3.js#L183
|
|
258
261
|
static inverse(me) {
|
|
259
262
|
const te = new Array(3), n11 = me[0][0], n21 = me[0][1], n31 = me[0][2], n12 = me[1][0], n22 = me[1][1], n32 = me[1][2], n13 = me[2][0], n23 = me[2][1], n33 = me[2][2], t11 = n33 * n22 - n32 * n23, t12 = n32 * n13 - n33 * n12, t13 = n23 * n12 - n22 * n13, det = n11 * t11 + n21 * t12 + n31 * t13;
|
|
260
263
|
if (det === 0) {
|
|
@@ -376,6 +379,9 @@ class Matrix4 {
|
|
|
376
379
|
[0, 0, 0, v[3]]
|
|
377
380
|
];
|
|
378
381
|
}
|
|
382
|
+
/**
|
|
383
|
+
* Column-Major ordering like Eigen and ThreeJS
|
|
384
|
+
*/
|
|
379
385
|
static fromVector(v) {
|
|
380
386
|
return [
|
|
381
387
|
[v[0], v[4], v[8], v[12]],
|
|
@@ -384,6 +390,9 @@ class Matrix4 {
|
|
|
384
390
|
[v[3], v[7], v[11], v[15]]
|
|
385
391
|
];
|
|
386
392
|
}
|
|
393
|
+
/**
|
|
394
|
+
* Column-Major ordering like Eigen and ThreeJS
|
|
395
|
+
*/
|
|
387
396
|
static toVector(m) {
|
|
388
397
|
return [
|
|
389
398
|
m[0][0],
|
|
@@ -531,6 +540,12 @@ class Matrix4 {
|
|
|
531
540
|
[m[3][0], m[3][1], m[3][2], m[3][3]]
|
|
532
541
|
];
|
|
533
542
|
}
|
|
543
|
+
static setTranslation(m, v) {
|
|
544
|
+
m[0][3] = v[0];
|
|
545
|
+
m[1][3] = v[1];
|
|
546
|
+
m[2][3] = v[2];
|
|
547
|
+
return m;
|
|
548
|
+
}
|
|
534
549
|
static toString(m) {
|
|
535
550
|
return "[\n" + m.map(
|
|
536
551
|
(row) => " [" + row.map((val) => val.toFixed(2)).join(", ") + "]"
|
|
@@ -538,6 +553,12 @@ class Matrix4 {
|
|
|
538
553
|
}
|
|
539
554
|
}
|
|
540
555
|
class Point2 {
|
|
556
|
+
/**
|
|
557
|
+
* Distance from a point to a segment
|
|
558
|
+
* @param {Number[2]} p point
|
|
559
|
+
* @param {Number[2]} v segment point 1
|
|
560
|
+
* @param {Number[2]} w segment point 2
|
|
561
|
+
*/
|
|
541
562
|
static distanceToSegment(p, v, w) {
|
|
542
563
|
const A = p[0] - v[0];
|
|
543
564
|
const B = p[1] - v[1];
|
|
@@ -565,11 +586,17 @@ class Point2 {
|
|
|
565
586
|
const dy = p[1] - yy;
|
|
566
587
|
return Math.sqrt(dx * dx + dy * dy);
|
|
567
588
|
}
|
|
589
|
+
/**
|
|
590
|
+
* Distance between two points
|
|
591
|
+
* @param {Number[2]} p1
|
|
592
|
+
* @param {Number[2]} p2
|
|
593
|
+
*/
|
|
568
594
|
static distanceToPoint(p1, p2) {
|
|
569
595
|
return Math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2);
|
|
570
596
|
}
|
|
571
597
|
}
|
|
572
598
|
class Polygon {
|
|
599
|
+
// https://github.com/substack/point-in-polygon/blob/master/index.js
|
|
573
600
|
static isPointInside(polygon, point) {
|
|
574
601
|
const x = point[0], y = point[1];
|
|
575
602
|
let inside = false;
|
|
@@ -611,13 +638,14 @@ class Polygon {
|
|
|
611
638
|
return minDist;
|
|
612
639
|
}
|
|
613
640
|
}
|
|
614
|
-
const _Quaternion = class {
|
|
641
|
+
const _Quaternion = class _Quaternion {
|
|
615
642
|
static get identity() {
|
|
616
643
|
return [1, 0, 0, 0];
|
|
617
644
|
}
|
|
618
645
|
static sum(q1, q2) {
|
|
619
646
|
return [q1[0] + q2[0], q1[1] + q2[1], q1[2] + q2[2], q1[3] + q2[3]];
|
|
620
647
|
}
|
|
648
|
+
// From https://www.mathworks.com/help/aerotbx/ug/quatrotate.html
|
|
621
649
|
static rotateMatlab(q, v) {
|
|
622
650
|
const [qw, qx, qy, qz] = q;
|
|
623
651
|
const [vx, vy, vz] = v;
|
|
@@ -627,6 +655,7 @@ const _Quaternion = class {
|
|
|
627
655
|
qw * (qw * vz - qx * vy + qy * vx) - qx * (qw * vy + qx * vz - qz * vx) + qy * (qw * vx - qy * vz + qz * vy) + qz * (qx * vx + qy * vy + qz * vz)
|
|
628
656
|
];
|
|
629
657
|
}
|
|
658
|
+
// From: https://eigen.tuxfamily.org/dox/Quaternion_8h_source.html
|
|
630
659
|
static rotate(q, v) {
|
|
631
660
|
const [qw, qx, qy, qz] = q;
|
|
632
661
|
const thisVec = [qx, qy, qz];
|
|
@@ -635,9 +664,18 @@ const _Quaternion = class {
|
|
|
635
664
|
const tmp1 = Vector3.sum(v, Vector3.multiplyScalar(uv, qw));
|
|
636
665
|
return Vector3.sum(tmp1, Vector3.cross(thisVec, uv));
|
|
637
666
|
}
|
|
667
|
+
/**
|
|
668
|
+
* Unit Quaternion inverse
|
|
669
|
+
* @param {Array.<Number>} quaternion must be a unit quaternion.
|
|
670
|
+
*/
|
|
638
671
|
static inverse(q) {
|
|
639
672
|
return [-q[0], q[1], q[2], q[3]];
|
|
640
673
|
}
|
|
674
|
+
/**
|
|
675
|
+
* Multiply quaternions
|
|
676
|
+
* @param {...Number[]} quaternions
|
|
677
|
+
* @returns {Number[]}
|
|
678
|
+
*/
|
|
641
679
|
static multiply(...quaternions) {
|
|
642
680
|
if (quaternions.length === 2) {
|
|
643
681
|
const [qw, qx, qy, qz] = quaternions[0];
|
|
@@ -651,16 +689,33 @@ const _Quaternion = class {
|
|
|
651
689
|
}
|
|
652
690
|
return quaternions.reduce((acc, quat) => this.multiply(acc, quat), _Quaternion.identity);
|
|
653
691
|
}
|
|
692
|
+
/**
|
|
693
|
+
* Axis-angle to quaternion
|
|
694
|
+
* @param {Array[Number]} axis unit vector
|
|
695
|
+
* @param {Number} angle in radians
|
|
696
|
+
*/
|
|
654
697
|
static fromAxisAngle(axis, angle) {
|
|
655
698
|
const s = Math.sin(angle / 2);
|
|
656
699
|
return [Math.cos(angle / 2), axis[0] * s, axis[1] * s, axis[2] * s];
|
|
657
700
|
}
|
|
701
|
+
/**
|
|
702
|
+
* Rotation around x-axis
|
|
703
|
+
* @param {Number} angle in radians
|
|
704
|
+
*/
|
|
658
705
|
static rotX(angle) {
|
|
659
706
|
return this.fromAxisAngle([1, 0, 0], angle);
|
|
660
707
|
}
|
|
708
|
+
/**
|
|
709
|
+
* Rotation around x-axis
|
|
710
|
+
* @param {Number} angle in radians
|
|
711
|
+
*/
|
|
661
712
|
static rotY(angle) {
|
|
662
713
|
return this.fromAxisAngle([0, 1, 0], angle);
|
|
663
714
|
}
|
|
715
|
+
/**
|
|
716
|
+
* Rotation around x-axis
|
|
717
|
+
* @param {Number} angle in radians
|
|
718
|
+
*/
|
|
664
719
|
static rotZ(angle) {
|
|
665
720
|
return this.fromAxisAngle([0, 0, 1], angle);
|
|
666
721
|
}
|
|
@@ -734,6 +789,15 @@ const _Quaternion = class {
|
|
|
734
789
|
const quat = [1 + dot, tmp[0], tmp[1], tmp[2]];
|
|
735
790
|
return _Quaternion.normalize(quat);
|
|
736
791
|
}
|
|
792
|
+
// https://github.com/stackgl/gl-quat/blob/master/slerp.js
|
|
793
|
+
/**
|
|
794
|
+
* Performs a spherical linear interpolation between two quat
|
|
795
|
+
*
|
|
796
|
+
* @param {number[]} a the first operand
|
|
797
|
+
* @param {number[]} b the second operand
|
|
798
|
+
* @param {number} t interpolation amount between the two inputs
|
|
799
|
+
* @returns {number[]} out
|
|
800
|
+
*/
|
|
737
801
|
static slerp(a, b, t) {
|
|
738
802
|
const ax = a[1], ay = a[2], az = a[3], aw = a[0];
|
|
739
803
|
let bx = b[1], by = b[2], bz = b[3], bw = b[0];
|
|
@@ -770,17 +834,42 @@ const _Quaternion = class {
|
|
|
770
834
|
static toString(q) {
|
|
771
835
|
return `[${q[0].toFixed(2)}, ${q[1].toFixed(2)}, ${q[2].toFixed(2)}, ${q[3].toFixed(2)}]`;
|
|
772
836
|
}
|
|
837
|
+
static fromUnitVectors(vFrom, vTo) {
|
|
838
|
+
let r = Vector3.dot(vFrom, vTo) + 1;
|
|
839
|
+
let x, y, z, w;
|
|
840
|
+
if (r < Number.EPSILON) {
|
|
841
|
+
r = 0;
|
|
842
|
+
if (Math.abs(vFrom[1]) > Math.abs(vFrom[2])) {
|
|
843
|
+
x = -vFrom[1];
|
|
844
|
+
y = vFrom[0];
|
|
845
|
+
z = 0;
|
|
846
|
+
w = r;
|
|
847
|
+
} else {
|
|
848
|
+
x = 0;
|
|
849
|
+
y = -vFrom[2];
|
|
850
|
+
z = vFrom[1];
|
|
851
|
+
w = r;
|
|
852
|
+
}
|
|
853
|
+
} else {
|
|
854
|
+
x = vFrom[1] * vTo[2] - vFrom[2] * vTo[1];
|
|
855
|
+
y = vFrom[2] * vTo[0] - vFrom[0] * vTo[2];
|
|
856
|
+
z = vFrom[0] * vTo[1] - vFrom[1] * vTo[0];
|
|
857
|
+
w = r;
|
|
858
|
+
}
|
|
859
|
+
const quat = [w, x, y, z];
|
|
860
|
+
return _Quaternion.normalize(quat);
|
|
861
|
+
}
|
|
773
862
|
};
|
|
863
|
+
__publicField(_Quaternion, "ROTX_PI2", _Quaternion.rotX(Math.PI / 2));
|
|
864
|
+
__publicField(_Quaternion, "ROTX_PI", _Quaternion.rotX(Math.PI));
|
|
865
|
+
__publicField(_Quaternion, "ROTX_MPI2", _Quaternion.rotX(-Math.PI / 2));
|
|
866
|
+
__publicField(_Quaternion, "ROTY_PI2", _Quaternion.rotY(Math.PI / 2));
|
|
867
|
+
__publicField(_Quaternion, "ROTY_PI", _Quaternion.rotY(Math.PI));
|
|
868
|
+
__publicField(_Quaternion, "ROTY_MPI2", _Quaternion.rotY(-Math.PI / 2));
|
|
869
|
+
__publicField(_Quaternion, "ROTZ_PI2", _Quaternion.rotZ(Math.PI / 2));
|
|
870
|
+
__publicField(_Quaternion, "ROTZ_PI", _Quaternion.rotZ(Math.PI));
|
|
871
|
+
__publicField(_Quaternion, "ROTZ_MPI2", _Quaternion.rotZ(-Math.PI / 2));
|
|
774
872
|
let Quaternion = _Quaternion;
|
|
775
|
-
__publicField(Quaternion, "ROTX_PI2", _Quaternion.rotX(Math.PI / 2));
|
|
776
|
-
__publicField(Quaternion, "ROTX_PI", _Quaternion.rotX(Math.PI));
|
|
777
|
-
__publicField(Quaternion, "ROTX_MPI2", _Quaternion.rotX(-Math.PI / 2));
|
|
778
|
-
__publicField(Quaternion, "ROTY_PI2", _Quaternion.rotY(Math.PI / 2));
|
|
779
|
-
__publicField(Quaternion, "ROTY_PI", _Quaternion.rotY(Math.PI));
|
|
780
|
-
__publicField(Quaternion, "ROTY_MPI2", _Quaternion.rotY(-Math.PI / 2));
|
|
781
|
-
__publicField(Quaternion, "ROTZ_PI2", _Quaternion.rotZ(Math.PI / 2));
|
|
782
|
-
__publicField(Quaternion, "ROTZ_PI", _Quaternion.rotZ(Math.PI));
|
|
783
|
-
__publicField(Quaternion, "ROTZ_MPI2", _Quaternion.rotZ(-Math.PI / 2));
|
|
784
873
|
function deg2rad(angle) {
|
|
785
874
|
return angle * (Math.PI / 180);
|
|
786
875
|
}
|
|
@@ -820,6 +909,8 @@ function roundFactor(num, factor) {
|
|
|
820
909
|
return Math.round(num / factor) * factor;
|
|
821
910
|
}
|
|
822
911
|
class Rotations {
|
|
912
|
+
// ZXY because yaw is around Z axis, pitch around X axis and roll around Y axis.
|
|
913
|
+
// They are conventions for a smartphone frame.
|
|
823
914
|
static eulerToQuaternionZXYDegrees(eulerAnglesDegrees) {
|
|
824
915
|
const eulerAngles = eulerAnglesDegrees.map((x) => deg2rad(x));
|
|
825
916
|
return Rotations.eulerToQuaternionZXY(eulerAngles);
|
|
@@ -885,6 +976,7 @@ class Rotations {
|
|
|
885
976
|
const eulerAngles = Rotations.quaternionToEuler(quaternion, order);
|
|
886
977
|
return eulerAngles.map((x) => rad2deg(x));
|
|
887
978
|
}
|
|
979
|
+
// https://searchcode.com/codesearch/view/55080466/
|
|
888
980
|
static quaternionToEuler(quaternion, order) {
|
|
889
981
|
const qw = quaternion[0];
|
|
890
982
|
const qx = quaternion[1];
|