Skip to content

Commit 74448d1

Browse files
committed
Fix [LIBOMV-985] Matrix4.Inverse fails to calculate the inverse of almost any matrix
https://metaverse.atlassian.net/browse/LIBOMV-985
1 parent 9041c95 commit 74448d1

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

OpenMetaverse.Tests/TypeTests.cs

+26
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@ public void Quaternions()
189189
Assert.IsTrue(result == expected, a.ToString() + " * " + b.ToString() + " produced " + result.ToString() +
190190
" instead of " + expected.ToString());
191191
}
192+
193+
[Test]
194+
public void testMatrix()
195+
{
196+
Matrix4 matrix = new Matrix4(0, 0, 74, 1,
197+
0, 435, 0, 1,
198+
345, 0, 34, 1,
199+
0, 0, 0, 0);
200+
201+
/* determinant of singular matrix returns zero */
202+
Assert.AreEqual(0d, (double)matrix.Determinant(), 0.001d);
203+
204+
/* inverse of identity matrix is the identity matrix */
205+
Assert.IsTrue(Matrix4.Identity == Matrix4.Inverse(Matrix4.Identity));
206+
207+
/* inverse of non-singular matrix returns True And InverseMatrix */
208+
matrix = new Matrix4(1, 1, 0, 0,
209+
1, 1, 1, 0,
210+
0, 1, 1, 0,
211+
0, 0, 0, 1);
212+
Matrix4 expectedInverse = new Matrix4(0, 1,-1, 0,
213+
1,-1, 1, 0,
214+
-1, 1, 0, 0,
215+
0, 0, 0, 1);
216+
Assert.AreEqual(expectedInverse, Matrix4.Inverse(matrix));
217+
}
192218

193219
//[Test]
194220
//public void VectorQuaternionMath()

OpenMetaverseTypes/Matrix4.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public float Determinant3x3()
161161
float det = 0f;
162162

163163
float diag1 = M11 * M22 * M33;
164-
float diag2 = M12 * M32 * M31;
164+
float diag2 = M12 * M23 * M31;
165165
float diag3 = M13 * M21 * M32;
166166
float diag4 = M31 * M22 * M13;
167167
float diag5 = M32 * M23 * M11;
@@ -950,7 +950,7 @@ public static Matrix4 Adjoint(Matrix4 matrix)
950950
for (int i = 0; i < 4; i++)
951951
{
952952
for (int j = 0; j < 4; j++)
953-
adjointMatrix[i,j] = (float)(Math.Pow(-1, i + j) * ((Minor(matrix, i, j)).Determinant()));
953+
adjointMatrix[i,j] = (float)(Math.Pow(-1, i + j) * ((Minor(matrix, i, j)).Determinant3x3()));
954954
}
955955

956956
adjointMatrix = Transpose(adjointMatrix);
@@ -986,12 +986,15 @@ public static Matrix4 Minor(Matrix4 matrix, int row, int col)
986986

987987
public override bool Equals(object obj)
988988
{
989-
return (obj is Matrix4) ? this == (Matrix4)obj : false;
989+
return (obj is Matrix4) ? this.Equals((Matrix4)obj) : false;
990990
}
991991

992992
public bool Equals(Matrix4 other)
993993
{
994-
return this == other;
994+
return M11 == other.M11 && M12 == other.M12 && M13 == other.M13 && M14 == other.M14 &&
995+
M21 == other.M21 && M22 == other.M22 && M23 == other.M23 && M24 == other.M24 &&
996+
M31 == other.M31 && M32 == other.M32 && M33 == other.M33 && M14 == other.M34 &&
997+
M41 == other.M41 && M42 == other.M42 && M43 == other.M43 && M44 == other.M44;
995998
}
996999

9971000
public override int GetHashCode()

0 commit comments

Comments
 (0)