Java Array Invert invert3x3(float m[], float inv[])

Here you can find the source of invert3x3(float m[], float inv[])

Description

Inverts the given matrix and writes the result into the given target matrix.

License

Open Source License

Parameter

Parameter Description
m The input matrix
inv The inverse matrix

Declaration

static void invert3x3(float m[], float inv[]) 

Method Source Code


//package com.java2s;
/*/* w w  w . jav a  2s . c  o m*/
 * www.javagl.de - JglTF
 *
 * Copyright 2015-2016 Marco Hutter - http://www.javagl.de
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

import java.util.Arrays;

public class Main {
    /**
     * Epsilon for floating point computations
     */
    private static final float FLOAT_EPSILON = 1e-6f;

    /**
     * Inverts the given matrix and writes the result into the given target
     * matrix. If the given matrix is not invertible, then the target matrix 
     * will be set to identity.  
     * 
     * @param m The input matrix
     * @param inv The inverse matrix
     */
    static void invert3x3(float m[], float inv[]) {
        // Adapted from http://stackoverflow.com/a/18504573
        float m0 = m[0];
        float m1 = m[1];
        float m2 = m[2];
        float m3 = m[3];
        float m4 = m[4];
        float m5 = m[5];
        float m6 = m[6];
        float m7 = m[7];
        float m8 = m[8];
        float det = m0 * (m4 * m8 - m5 * m7) - m3 * (m1 * m8 - m7 * m2) + m6 * (m1 * m5 - m4 * m2);
        if (Math.abs(det) <= FLOAT_EPSILON) {
            setIdentity3x3(inv);
            return;
        }
        float invDet = 1.0f / det;
        inv[0] = (m4 * m8 - m5 * m7) * invDet;
        inv[3] = (m6 * m5 - m3 * m8) * invDet;
        inv[6] = (m3 * m7 - m6 * m4) * invDet;
        inv[1] = (m7 * m2 - m1 * m8) * invDet;
        inv[4] = (m0 * m8 - m6 * m2) * invDet;
        inv[7] = (m1 * m6 - m0 * m7) * invDet;
        inv[2] = (m1 * m5 - m2 * m4) * invDet;
        inv[5] = (m2 * m3 - m0 * m5) * invDet;
        inv[8] = (m0 * m4 - m1 * m3) * invDet;
    }

    /**
     * Set the given matrix to be the identity matrix. 
     * 
     * @param m The matrix
     */
    static void setIdentity3x3(float m[]) {
        Arrays.fill(m, 0.0f);
        m[0] = 1.0f;
        m[4] = 1.0f;
        m[8] = 1.0f;
    }
}

Related

  1. invert(int[] v)
  2. invert(int[][] clustered)
  3. invert(Object[] array)
  4. invert(Object[] objArray)
  5. invert(T[] array)
  6. invert4x4(float m[], float inv[])
  7. invertArray(final byte[] array)
  8. invertArray(int[] a, int top)
  9. invertArray(Object[] data)