[OpenSCAD] Matrix inversion

Lucas Vinicius Hartmann lucas.hartmann at gmail.com
Sat Jun 11 17:33:15 EDT 2016


(Sorry if this is a double post, I sent this message the first time before
completing registration on the mailing list.)

Sometime ago I wrote a small set of functions for construction and use
transformation matrices, including matrix inversion. Here is the code:
https://github.com/lhartmann/openscad_m4lib/blob/master/m4.scad

Matrix inversion code works most of the time, except I just detected a bug
I can not solve. A simple dual rotation matrix, 90º around Y then 90º
around Z, is yielding nan+inf on all elements of the inverse. Works nicely
for other angles, though...

M = m4rz(90) * m4ry(90); // Rotates around Y first, then Z
Minv = m4inv(M); // Fails

I managed to narrow it down to m4inv_solve(...) which does diagonal zero
detection. This is the testing code I used to isolate the error:

M = m4rz(90)*m4ry(90); // Test matrix
echo( // Failing
    m4inv_solve(0, // Solve row 0: Detect diagonal zero, swap rows, calls
solve2(...).
        m4inv_mx(M) // Expand to Row-Reduced Echelon Format
    )
);
echo( // Working by bypassing m4inv_solve(...)
    m4inv_solve2(0, // Solve row 0 (normalize, scale and subtract from all
other rows)
        m4inv_rowswap(0,2, // Swap rows 0 and 2 by hand
            m4inv_mx(M) // Expand to Row-Reduced Echelon Format
        )
    )
);

This is the code for the misbehaving function, it should check move zeroes
away from the diagonal of row i:

// Solve one step, ensure nonzero at diagonal element.
function m4inv_zero(x) = abs(x) < 1e-5;
function m4inv_solve(i,mx) =
                 !m4inv_zero(mx[i][i  ]) ?
m4inv_solve2(i,                     mx ) :
        i+1<4 && !m4inv_zero(mx[i][i+1]) ? m4inv_solve2(i,
m4inv_rowswap(i,i+1,mx)) :
        i+2<4 && !m4inv_zero(mx[i][i+2]) ? m4inv_solve2(i,
m4inv_rowswap(i,i+2,mx)) :
        i+3<4 && !m4inv_zero(mx[i][i+3]) ? m4inv_solve2(i,
m4inv_rowswap(i,i+3,mx)) :
        m4identity(); // Singular matrix, reset to identity

I looked at the code for m4inv_solve(...) above for a while, but don't get
why it is failing... Any ideas?

--
Lucas Vinicius Hartmann

Dizem que se você rodar o CD do Windows ao contrário ele mostra uma
mensagem demoníaca... Mas isso nem é o pior, se você rodar ele normal ele
instala o Windows!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20160611/f03c9cfb/attachment-0002.html>


More information about the Discuss mailing list