|
| 1 | +package trrt.rendering3d.primitives; |
| 2 | +public class Matrix3x3 |
| 3 | +{ |
| 4 | + //R means row and C means column. R2C3 would be second row third column. |
| 5 | + public final double R1C1, R1C2, R1C3, R2C1, R2C2, R2C3, R3C1, R3C2, R3C3; |
| 6 | + |
| 7 | + //overloaded constructor which accepts three Vector3s. |
| 8 | + public Matrix3x3(Vector3 column1, Vector3 column2, Vector3 column3) |
| 9 | + { |
| 10 | + R1C1 = column1.x; R1C2 = column2.x; R1C3 = column3.x; |
| 11 | + R2C1 = column1.y; R2C2 = column2.y; R2C3 = column3.y; |
| 12 | + R3C1 = column1.z; R3C2 = column2.z; R3C3 = column3.z; |
| 13 | + } |
| 14 | + |
| 15 | + //overloaded constructor which allows all 9 values of the matrix. |
| 16 | + public Matrix3x3(double r1c1, double r1c2, double r1c3, double r2c1, double r2c2, double r2c3, double r3c1, double r3c2, double r3c3) |
| 17 | + { |
| 18 | + R1C1 = r1c1; R1C2 = r1c2; R1C3 = r1c3; |
| 19 | + R2C1 = r2c1; R2C2 = r2c2; R2C3 = r2c3; |
| 20 | + R3C1 = r3c1; R3C2 = r3c2; R3C3 = r3c3; |
| 21 | + } |
| 22 | + |
| 23 | + //formats the values in the matrix into a string. |
| 24 | + public String toString() |
| 25 | + { |
| 26 | + return String.format("\n|%39s\n|%10.2f%10.2f%10.2f%9s\n|%39s\n|%10.2f%10.2f%10.2f%9s\n|%39s\n|%10.2f%10.2f%10.2f%9s\n|%39s\n", |
| 27 | + "|", R1C1, R1C2, R1C3, "|", "|", R2C1, R2C2, R2C3, "|", "|", R3C1, R3C2, R3C3, "|", "|"); |
| 28 | + } |
| 29 | + |
| 30 | + //returns the determinant of the 3x3 matrix. |
| 31 | + public double getDeterminant() |
| 32 | + { |
| 33 | + return R1C1*(R2C2*R3C3-R2C3*R3C2)-R1C2*(R2C1*R3C3-R2C3*R3C1)+R1C3*(R2C1*R3C2-R2C2*R3C1); |
| 34 | + } |
| 35 | + |
| 36 | + //returns the cofactor matrix. |
| 37 | + public Matrix3x3 getCofactorMatrix() |
| 38 | + { |
| 39 | + return new Matrix3x3 |
| 40 | + ( |
| 41 | + R2C2*R3C3-R2C3*R3C2, -(R2C1*R3C3-R2C3*R3C1), R2C1*R3C2-R2C2*R3C1, |
| 42 | + -(R1C2*R3C3-R1C3*R3C2), R1C1*R3C3-R1C3*R3C1, -(R1C1*R3C2-R1C2*R3C1), |
| 43 | + R1C2*R2C3-R1C3*R2C2, -(R1C1*R2C3-R1C3*R2C1), R1C1*R2C2-R1C2*R2C1 |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + //returns the adjugate matrix, basically just the transposed cofactor matrix. |
| 48 | + public Matrix3x3 getAdjugateMatrix() |
| 49 | + { |
| 50 | + return new Matrix3x3 |
| 51 | + ( |
| 52 | + R2C2*R3C3-R2C3*R3C2, -(R1C2*R3C3-R1C3*R3C2), R1C2*R2C3-R1C3*R2C2, |
| 53 | + -(R2C1*R3C3-R2C3*R3C1), R1C1*R3C3-R1C3*R3C1, -(R1C1*R2C3-R1C3*R2C1), |
| 54 | + R2C1*R3C2-R2C2*R3C1, -(R1C1*R3C2-R1C2*R3C1), R1C1*R2C2-R1C2*R2C1 |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + //returns the inverse of the matrix, which is just the adjugate/det |
| 59 | + public Matrix3x3 getInverse() |
| 60 | + { |
| 61 | + return Matrix3x3.multiply(getAdjugateMatrix(), 1/getDeterminant()); |
| 62 | + } |
| 63 | + |
| 64 | + //#region ----------- static methods ------------- |
| 65 | + |
| 66 | + //applies matrix m1 to matrix m2 and returns the resulting matrix. order matters! |
| 67 | + public static Matrix3x3 multiply(Matrix3x3 m1, Matrix3x3 m2) |
| 68 | + { |
| 69 | + return new Matrix3x3( |
| 70 | + m1.R1C1*m2.R1C1 + m1.R1C2*m2.R2C1 + m1.R1C3*m2.R3C1, m1.R1C1*m2.R1C2 + m1.R1C2*m2.R2C2 + m1.R1C3*m2.R3C2, m1.R1C1*m2.R1C3 + m1.R1C2*m2.R2C3 + m1.R1C3*m2.R3C3, |
| 71 | + m1.R2C1*m2.R1C1 + m1.R2C2*m2.R2C1 + m1.R2C3*m2.R3C1, m1.R2C1*m2.R1C2 + m1.R2C2*m2.R2C2 + m1.R2C3*m2.R3C2, m1.R2C1*m2.R1C3 + m1.R2C2*m2.R2C3 + m1.R2C3*m2.R3C3, |
| 72 | + m1.R3C1*m2.R1C1 + m1.R3C2*m2.R2C1 + m1.R3C3*m2.R3C1, m1.R3C1*m2.R1C2 + m1.R3C2*m2.R2C2 + m1.R3C3*m2.R3C2, m1.R3C1*m2.R1C3 + m1.R3C2*m2.R2C3 + m1.R3C3*m2.R3C3); |
| 73 | + } |
| 74 | + |
| 75 | + //multiplies a matrix by a scalar value |
| 76 | + public static Matrix3x3 multiply(Matrix3x3 matrix, double scalar) |
| 77 | + { |
| 78 | + return new Matrix3x3 |
| 79 | + ( |
| 80 | + matrix.R1C1*scalar, matrix.R1C2*scalar, matrix.R1C3*scalar, |
| 81 | + matrix.R2C1*scalar, matrix.R2C2*scalar, matrix.R2C3*scalar, |
| 82 | + matrix.R3C1*scalar, matrix.R3C2*scalar, matrix.R3C3*scalar |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + // |
| 87 | + public static Matrix3x3 rotationMatrixAxisX(double angle) |
| 88 | + { |
| 89 | + //local variables to mitigate preforming the same slow trig function multiple times. |
| 90 | + double sinAngle = Math.sin(angle); |
| 91 | + double cosAngle = Math.sqrt(1-sinAngle*sinAngle); //same as math.cos function |
| 92 | + |
| 93 | + /* | cos -sin 0 | |
| 94 | + | sin cos 0 | |
| 95 | + | 0 0 1 | */ |
| 96 | + |
| 97 | + return new Matrix3x3 |
| 98 | + ( |
| 99 | + 1, 0, 0, |
| 100 | + 0, cosAngle, -sinAngle, |
| 101 | + 0, sinAngle, cosAngle |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + public static Matrix3x3 rotationMatrixAxisY(double angle) |
| 106 | + { |
| 107 | + //local variables to mitigate preforming the same slow trig function multiple times. |
| 108 | + double sinAngle = Math.sin(angle); |
| 109 | + double cosAngle = Math.sqrt(1-sinAngle*sinAngle); //same as math.cos function |
| 110 | + |
| 111 | + /* | cos 0 sin | |
| 112 | + | 0 1 0 | |
| 113 | + |-sin 0 cos | */ |
| 114 | + |
| 115 | + return new Matrix3x3 |
| 116 | + ( |
| 117 | + cosAngle, 0, sinAngle, |
| 118 | + 0, 1, 0, |
| 119 | + -sinAngle, 0, cosAngle |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + public static Matrix3x3 rotationMatrixAxisZ(double angle) |
| 124 | + { |
| 125 | + //local variables to mitigate preforming the same slow trig function multiple times. |
| 126 | + double sinAngle = Math.sin(angle); |
| 127 | + double cosAngle = Math.sqrt(1-sinAngle*sinAngle); //same as math.cos function |
| 128 | + |
| 129 | + /* | cos -sin 0 | |
| 130 | + | sin cos 0 | |
| 131 | + | 0 0 1 | */ |
| 132 | + |
| 133 | + return new Matrix3x3 |
| 134 | + ( |
| 135 | + cosAngle, -sinAngle, 0, |
| 136 | + sinAngle, cosAngle, 0, |
| 137 | + 0, 0, 1 |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + //returns a matrix which can preform a rotation "angle" radians about "axis" |
| 142 | + public static Matrix3x3 axisAngleMatrix(Vector3 axis, double angle) |
| 143 | + { |
| 144 | + axis = axis.getNormalized(); |
| 145 | + |
| 146 | + //local variables to mitigate preforming the same slow trig function multiple times. |
| 147 | + double sin = Math.sin(angle); |
| 148 | + double cos = Math.sqrt(1-sin*sin); //same as math.cos function |
| 149 | + double cos1 = 1-cos; |
| 150 | + |
| 151 | + return new Matrix3x3 |
| 152 | + ( |
| 153 | + cos+axis.x*axis.x*cos1, axis.x*axis.y*cos1-axis.z*sin, axis.x*axis.z*cos1+axis.y*sin, |
| 154 | + axis.y*axis.x*cos1+axis.z*sin, cos+axis.y*axis.y*cos1, axis.y*axis.z*cos1-axis.x*sin, |
| 155 | + axis.z*axis.x*cos1-axis.y*sin, axis.z*axis.y*cos1+axis.x*sin, cos+axis.z*axis.z*cos1 |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + //#endregion |
| 160 | +} |
0 commit comments