OpenGL.Net icon indicating copy to clipboard operation
OpenGL.Net copied to clipboard

Matrix4x4f with very large/small scaling is "not invertible"

Open SuuperW opened this issue 5 years ago • 1 comments

When I attempted to get the inverse of a Matrix4x4f I got an exception saying "not invertible", when I know this matrix can be inverted.

The problem is here: https://github.com/luca-piccioni/OpenGL.Net/blob/master/OpenGL.Net/Matrix.cs#L6133 A matrix is considered "not invertible" if it's determinant is between +/- 1e-6. There are no comments in the code or the commit (cc1a5354a0a24751af257b4f9410c11e1be7cd8e) explaining why this is the case.

System.Numerics can properly compute the inverse of such a matrix.

I see in Matrix.cs that in some situations (which I don't understand) it will actually use System.Numerics' implementation of inverting a matrix. Is there any way I can cause it to do this in my project, in order to solve this problem in my application? (My app is a Windows Forms App, using the .NET Framework, and I do have System.Numerics in my project; the NuGet package manager installed it along with OpenGL.Net.Math.)

SuuperW avatar Jul 07 '19 12:07 SuuperW

The 1e-6f constant magic number is a "somewhat little number" under which the inverse computation becomes unstable. It is a very small number, near to the float.Epsilon value.

If you are able to determine a smaller number that make this unit test pass, you can suggest it here or in a pull request.

System.Numerics are supported only if building with a .NET Framework greater or equal to 4.6. The vector is actually accelerated only in Release/x64 configurations (on Intel). Debug configurations (no accelerated), are much slower than the other code (indeed the Vector.IsHardwareAccelerated test).

luca-piccioni avatar Jul 07 '19 17:07 luca-piccioni