Java Matrix Invert invertMatrix3x3(float[] result, float[] m)

Here you can find the source of invertMatrix3x3(float[] result, float[] m)

Description

Invert a 3x3 matrix.

License

Open Source License

Parameter

Parameter Description
result the inverted matrix (stored as a 4x4 matrix, but only 3x3 is returned)
m the matrix to be inverted (stored as a 4x4 matrix, but only 3x3 is used)

Return

true if the matrix could be inverted false if the matrix could not be inverted

Declaration

public static boolean invertMatrix3x3(float[] result, float[] m) 

Method Source Code

//package com.java2s;
/*/*from   w  w w.  j a va  2  s.com*/
This file is part of jpcsp.
    
Jpcsp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
Jpcsp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Jpcsp.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Invert a 3x3 matrix.
     * 
     * Based on
     *     http://en.wikipedia.org/wiki/Invert_matrix#Inversion_of_3.C3.973_matrices
     * 
     * @param result   the inverted matrix (stored as a 4x4 matrix, but only 3x3 is returned)
     * @param m        the matrix to be inverted (stored as a 4x4 matrix, but only 3x3 is used)
     * @return         true if the matrix could be inverted
     *                 false if the matrix could not be inverted
     */
    public static boolean invertMatrix3x3(float[] result, float[] m) {
        float A = m[5] * m[10] - m[6] * m[9];
        float B = m[6] * m[8] - m[4] * m[10];
        float C = m[4] * m[9] - m[5] * m[8];
        float det = m[0] * A + m[1] * B + m[2] * C;

        if (det == 0.f) {
            // Matrix could not be inverted
            return false;
        }

        float invertedDet = 1.f / det;
        result[0] = A * invertedDet;
        result[1] = (m[2] * m[9] - m[1] * m[10]) * invertedDet;
        result[2] = (m[1] * m[6] - m[2] * m[5]) * invertedDet;
        result[4] = B * invertedDet;
        result[5] = (m[0] * m[10] - m[2] * m[8]) * invertedDet;
        result[6] = (m[2] * m[4] - m[0] * m[6]) * invertedDet;
        result[8] = C * invertedDet;
        result[9] = (m[8] * m[1] - m[0] * m[9]) * invertedDet;
        result[10] = (m[0] * m[5] - m[1] * m[4]) * invertedDet;

        return true;
    }
}

Related

  1. invert2x2(final double[][] src, final double[][] dest)
  2. invert3x3(float[] mat)
  3. invert44Matrix(final double[] m, final double[] invOut)
  4. invertDataSet(double[][] dataSet)
  5. invertMatrix(int[][] matrix)
  6. invertMatrix4x4(double[][] A)
  7. invertRegions(int[][] image)
  8. invertSymmetric2x2(final double[][] m, final double[][] inverse)
  9. invertSymmetric3x3(final double[][] m, final double[][] inverse)