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