| 1 | package com.mojang.math;
|
| 2 |
|
| 3 | import org.joml.Math;
|
| 4 | import org.joml.Matrix3f;
|
| 5 | import org.joml.Matrix4f;
|
| 6 | import org.joml.Matrix4fc;
|
| 7 | import org.joml.Quaternionf;
|
| 8 | import org.joml.Vector3f;
|
| 9 |
|
| 10 | public class MatrixUtil {
|
| 11 | private static final float G = 3.0F + 2.0F * Math.sqrt(2.0F);
|
| 12 | private static final GivensParameters PI_4 = GivensParameters.fromPositiveAngle((float) (java.lang.Math.PI / 4));
|
| 13 |
|
| 14 | private MatrixUtil() {
|
| 15 | }
|
| 16 |
|
| 17 | public static Matrix4f mulComponentWise(final Matrix4f m, final float factor) {
|
| 18 | return m.set(
|
| 19 | m.m00() * factor,
|
| 20 | m.m01() * factor,
|
| 21 | m.m02() * factor,
|
| 22 | m.m03() * factor,
|
| 23 | m.m10() * factor,
|
| 24 | m.m11() * factor,
|
| 25 | m.m12() * factor,
|
| 26 | m.m13() * factor,
|
| 27 | m.m20() * factor,
|
| 28 | m.m21() * factor,
|
| 29 | m.m22() * factor,
|
| 30 | m.m23() * factor,
|
| 31 | m.m30() * factor,
|
| 32 | m.m31() * factor,
|
| 33 | m.m32() * factor,
|
| 34 | m.m33() * factor
|
| 35 | );
|
| 36 | }
|
| 37 |
|
| 38 | private static GivensParameters approxGivensQuat(final float a11, final float a12, final float a22) {
|
| 39 | float ch = 2.0F * (a11 - a22);
|
| 40 | float sh = a12;
|
| 41 | return G * sh * sh < ch * ch ? GivensParameters.fromUnnormalized(sh, ch) : PI_4;
|
| 42 | }
|
| 43 |
|
| 44 | private static GivensParameters qrGivensQuat(final float a1, final float a2) {
|
| 45 | float p = (float)java.lang.Math.hypot(a1, a2);
|
| 46 | float sh = p > 1.0E-6F ? a2 : 0.0F;
|
| 47 | float ch = Math.abs(a1) + Math.max(p, 1.0E-6F);
|
| 48 | if (a1 < 0.0F) {
|
| 49 | float f = sh;
|
| 50 | sh = ch;
|
| 51 | ch = f;
|
| 52 | }
|
| 53 |
|
| 54 | return GivensParameters.fromUnnormalized(sh, ch);
|
| 55 | }
|
| 56 |
|
| 57 | private static void similarityTransform(final Matrix3f a, final Matrix3f q) {
|
| 58 | a.mul(q);
|
| 59 | q.transpose();
|
| 60 | q.mul(a);
|
| 61 | a.set(q);
|
| 62 | }
|
| 63 |
|
| 64 | private static void stepJacobi(final Matrix3f m, final Matrix3f tmpMat, final Quaternionf tmpQ, final Quaternionf output) {
|
| 65 | if (m.m01 * m.m01 + m.m10 * m.m10 > 1.0E-6F) {
|
| 66 | GivensParameters p = approxGivensQuat(m.m00, 0.5F * (m.m01 + m.m10), m.m11);
|
| 67 | Quaternionf qt = p.aroundZ(tmpQ);
|
| 68 | output.mul(qt);
|
| 69 | p.aroundZ(tmpMat);
|
| 70 | similarityTransform(m, tmpMat);
|
| 71 | }
|
| 72 |
|
| 73 | if (m.m02 * m.m02 + m.m20 * m.m20 > 1.0E-6F) {
|
| 74 | GivensParameters p = approxGivensQuat(m.m00, 0.5F * (m.m02 + m.m20), m.m22).inverse();
|
| 75 | Quaternionf qt = p.aroundY(tmpQ);
|
| 76 | output.mul(qt);
|
| 77 | p.aroundY(tmpMat);
|
| 78 | similarityTransform(m, tmpMat);
|
| 79 | }
|
| 80 |
|
| 81 | if (m.m12 * m.m12 + m.m21 * m.m21 > 1.0E-6F) {
|
| 82 | GivensParameters p = approxGivensQuat(m.m11, 0.5F * (m.m12 + m.m21), m.m22);
|
| 83 | Quaternionf qt = p.aroundX(tmpQ);
|
| 84 | output.mul(qt);
|
| 85 | p.aroundX(tmpMat);
|
| 86 | similarityTransform(m, tmpMat);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | public static void eigenvalueJacobi(final Matrix3f inOut, final int steps, final Quaternionf result) {
|
| 91 | result.identity();
|
| 92 | Matrix3f scratchMat = new Matrix3f();
|
| 93 | Quaternionf scratchQ = new Quaternionf();
|
| 94 |
|
| 95 | for (int i = 0; i < steps; i++) {
|
| 96 | stepJacobi(inOut, scratchMat, scratchQ, result);
|
| 97 | }
|
| 98 |
|
| 99 | result.normalize();
|
| 100 | }
|
| 101 |
|
| 102 | public static void svdDecompose(final Matrix4fc input, final Vector3f t, final Quaternionf u, final Vector3f s, final Quaternionf v) {
|
| 103 | float scaleFactor = 1.0F / input.m33();
|
| 104 | svdDecompose(new Matrix3f(input).scale(scaleFactor), u, s, v);
|
| 105 | input.getTranslation(t).mul(scaleFactor);
|
| 106 | }
|
| 107 |
|
| 108 | private static void svdDecompose(final Matrix3f input, final Quaternionf u, final Vector3f s, final Quaternionf v) {
|
| 109 | Matrix3f scratch = new Matrix3f(input);
|
| 110 | scratch.transpose();
|
| 111 | scratch.mul(input);
|
| 112 | eigenvalueJacobi(scratch, 5, v);
|
| 113 | float columnScaleSquare0 = scratch.m00;
|
| 114 | float columnScaleSquare1 = scratch.m11;
|
| 115 | boolean zeroColumn0 = columnScaleSquare0 < 1.0E-6;
|
| 116 | boolean zeroColumn1 = columnScaleSquare1 < 1.0E-6;
|
| 117 | Matrix3f u012s = input.rotate(v);
|
| 118 | u.identity();
|
| 119 | Quaternionf tmpQ = new Quaternionf();
|
| 120 | GivensParameters p;
|
| 121 | if (zeroColumn0) {
|
| 122 | p = qrGivensQuat(u012s.m11, -u012s.m10);
|
| 123 | } else {
|
| 124 | p = qrGivensQuat(u012s.m00, u012s.m01);
|
| 125 | }
|
| 126 |
|
| 127 | Quaternionf qt0 = p.aroundZ(tmpQ);
|
| 128 | Matrix3f u12s = p.aroundZ(scratch);
|
| 129 | u.mul(qt0);
|
| 130 | u12s.transpose().mul(u012s);
|
| 131 | scratch = u012s;
|
| 132 | if (zeroColumn0) {
|
| 133 | p = qrGivensQuat(u12s.m22, -u12s.m20);
|
| 134 | } else {
|
| 135 | p = qrGivensQuat(u12s.m00, u12s.m02);
|
| 136 | }
|
| 137 |
|
| 138 | p = p.inverse();
|
| 139 | Quaternionf qt1 = p.aroundY(tmpQ);
|
| 140 | Matrix3f u2s = p.aroundY(scratch);
|
| 141 | u.mul(qt1);
|
| 142 | u2s.transpose().mul(u12s);
|
| 143 | scratch = u12s;
|
| 144 | if (zeroColumn1) {
|
| 145 | p = qrGivensQuat(u2s.m22, -u2s.m21);
|
| 146 | } else {
|
| 147 | p = qrGivensQuat(u2s.m11, u2s.m12);
|
| 148 | }
|
| 149 |
|
| 150 | Quaternionf qt2 = p.aroundX(tmpQ);
|
| 151 | Matrix3f sm = p.aroundX(scratch);
|
| 152 | u.mul(qt2);
|
| 153 | sm.transpose().mul(u2s);
|
| 154 | s.set(sm.m00, sm.m11, sm.m22);
|
| 155 | v.conjugate();
|
| 156 | }
|
| 157 |
|
| 158 | public static boolean checkPropertyRaw(final Matrix4fc matrix, final int property) {
|
| 159 | return (matrix.properties() & property) != 0;
|
| 160 | }
|
| 161 |
|
| 162 | public static boolean checkProperty(final Matrix4fc matrix, final int property) {
|
| 163 | if (checkPropertyRaw(matrix, property)) {
|
| 164 | return true;
|
| 165 | } else if (matrix instanceof Matrix4f mutableMatrix) {
|
| 166 | int currentProperties = mutableMatrix.properties();
|
| 167 | mutableMatrix.determineProperties();
|
| 168 | mutableMatrix.assume(mutableMatrix.properties() | currentProperties);
|
| 169 | return checkPropertyRaw(matrix, property);
|
| 170 | } else {
|
| 171 | return false;
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | public static boolean isIdentity(final Matrix4fc matrix) {
|
| 176 | return checkProperty(matrix, 4);
|
| 177 | }
|
| 178 |
|
| 179 | public static boolean isPureTranslation(final Matrix4fc matrix) {
|
| 180 | return checkProperty(matrix, 8);
|
| 181 | }
|
| 182 | }
|
| 183 |
|