Factoring the Translation

A 3D affine transform can be factored into a 3D linear transformation, folloed by a translation. Factoring the translation out of a matrix is trivial. Given a matrix \(M\), we want to factor it into \(M=TX\). \(T\) is going to be an identity matrix with the translation part of \(M\). \(X\) is going to be the same as \(M\), but with the translation part removed.

struct FactorTranslationResult {
    Matrix T; // Translation
    Matrix X; // Linear transformation
};

FactorTranslationResult FactorTranslation(Matrix M) {
    FactorTranslationResult result;

    result.T = Matrix4();
    result.T[12] = M[12];
    result.T[13] = M[13];
    result.T[14] = M[14];

    result.X = M;
    result.X[12] = 0;
    result.X[13] = 0;
    result.X[14] = 0;

    return result;
}