TomCrypto / Embree.NET

C# wrapper for the Embree library at http://embree.github.io.
Other
18 stars 7 forks source link

MathLibrary Matrix Invert is wrong #3

Open jcobreros opened 7 years ago

jcobreros commented 7 years ago

It should be (Something like):

public static Matrix Invert(Matrix mat)
        {
            // Work out determinant of the 3x3 submatrix.
            var det = mat.U.X * (mat.V.Y * mat.W.Z - mat.W.Y * mat.V.Z)
                    - mat.V.X * (mat.W.Z * mat.U.Y - mat.W.Y * mat.U.Z)
                    + mat.W.X * (mat.U.Y * mat.V.Z - mat.V.Y * mat.U.Z);

            if (Math.Abs(det) < 0)
                throw new ArgumentException("Matrix is not invertible");

            float ux = mat.V.Y * mat.W.Z - mat.V.Z * mat.W.Y;
            float uy = mat.W.Y * mat.U.Z - mat.U.Y * mat.W.Z;
            float uz = mat.U.Y * mat.V.Z - mat.U.Z * mat.V.Y;
            float vx = mat.W.X * mat.V.Z - mat.V.X * mat.W.Z;
            float vy = mat.U.X * mat.W.Z - mat.W.X * mat.U.Z;
            float vz = mat.V.X * mat.U.Z - mat.U.X * mat.V.Z;
            float wx = mat.V.X * mat.W.Y - mat.W.X * mat.V.Y;
            float wy = mat.W.X * mat.U.Y - mat.U.X * mat.W.Y;
            float wz = mat.U.X * mat.V.Y - mat.V.X * mat.U.Y;

            Vector inv_u = new Vector(ux, uy, uz);
            Vector inv_v = new Vector(vx, vy, vz);
            Vector inv_w = new Vector(wx, wy, wz);

            // Transform the translation column by this inverse matrix.
            var inv_t = -(mat.T.X * inv_u + mat.T.Y * inv_v + mat.T.Z * inv_w);

            return new Matrix(inv_u, inv_v, inv_w, inv_t);
        }