Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix4x4 Decompose failing #34874

Closed
ptasev opened this issue Apr 12, 2020 · 1 comment
Closed

Matrix4x4 Decompose failing #34874

ptasev opened this issue Apr 12, 2020 · 1 comment

Comments

@ptasev
Copy link

ptasev commented Apr 12, 2020

I'm not sure if this is a bug (since I don't quite understand the math fully) and I was hoping someone here could tell me. I am creating an SRT matrix, and then inverting it. When I decompose the inverted version it fails. Is this behavior expected?

I am using dot net core 3.1.3. My CPU is AMD Ryzen 5 2600.

using System;
using System.Numerics;

namespace tmp
{
    class Program
    {
        static void Main(string[] args)
        {
            var pos = new Vector3(0.217761353f, 0.129548967f, 7.627072E-05f);
            var rot = new Quaternion(0.527541637f, 0.470849365f, 0.527542233f, -0.47085017f);
            var scale = new Vector3(0.7462687f, 0.480769f, 0.9999995f);

            // Create SRT matrix
            var transMat = Matrix4x4.CreateTranslation(pos);
            var rotMat = Matrix4x4.CreateFromQuaternion(rot);
            var scaleMat = Matrix4x4.CreateScale(scale);
            var regm = scaleMat * rotMat * transMat;

            var b = Matrix4x4.Decompose(regm, out Vector3 s, out Quaternion r, out Vector3 t);
            if (!b) throw new Exception("Bad decompose!");

            // Create inverse of the SRT matrix above
            Matrix4x4.Invert(transMat, out Matrix4x4 tmi);
            Matrix4x4.Invert(rotMat, out Matrix4x4 rmi);
            Matrix4x4.Invert(scaleMat, out Matrix4x4 smi);
            var invm = tmi * rmi * smi;

            b = Matrix4x4.Decompose(invm, out Vector3 si, out Quaternion ri, out Vector3 ti);
            if (!b) throw new Exception("Bad decompose!"); // Throws here
        }
    }
}

I'm starting to think this is due to floating point precision. I see the implementation of Decompose has an epsilon for the rotation matrix at the end that I'm failing to meet. I just don't understand how only the inverse is losing so much precision.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Numerics untriaged New issue has not been triaged by the area owner labels Apr 12, 2020
@ghost
Copy link

ghost commented Apr 12, 2020

Tagging @tannergooding as an area owner. If you would like to be tagged for a label, please notify danmosemsft.

@ptasev ptasev closed this as completed Apr 21, 2020
@tannergooding tannergooding removed the untriaged New issue has not been triaged by the area owner label Sep 14, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants