/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Jan 5, 2005 */ package ml.math; /** * The Matrix3 class is a representation of a 3x3 matrix and some of the * operations possible on it. * * @author gregor */ public class Matrix3 implements Cloneable { public double X[][]; /** * Simple constructor. it does not initialise the values * of the matrix. */ public Matrix3() { X=new double[3][]; for(int i=0;i<3;i++) { X[i]=new double[3]; } } /** * Clear the matrix by setting all values to 0.0. */ public void clear() { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { X[i][j]=0.0; } } } /** * Return a deep copy of the matrix. * * @return a copy of the matrix */ @Override public Matrix3 clone() { Matrix3 ret=new Matrix3(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ret.X[i][j]=X[i][j]; } } return ret; } /** * Build the transpose of the matrix and return it. This does * not affect the original matrix instance. * * @return the transposed matrix \f$A^T\f$ */ public Matrix3 transpose() { Matrix3 ret=new Matrix3(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ret.X[j][i]=X[i][j]; } } return ret; } /** * Calculate the inverse of this matrix and return it. This does * not affect the original instance of the matrix. * * @return the inverse matrix \f$A^{-1}\f$ */ public Matrix3 invert() { Matrix3 a=new Matrix3(); double det, invDet; a.X[0][0]=X[1][1]*X[2][2] - X[1][2]*X[2][1]; a.X[0][1]=X[0][2]*X[2][1] - X[0][1]*X[2][2]; a.X[0][2]=X[0][1]*X[1][2] - X[0][2]*X[1][1]; a.X[1][0]=X[1][2]*X[2][0] - X[1][0]*X[2][2]; a.X[1][1]=X[0][0]*X[2][2] - X[0][2]*X[2][0]; a.X[1][2]=X[0][2]*X[1][0] - X[0][0]*X[1][2]; a.X[2][0]=X[1][0]*X[2][1] - X[1][1]*X[2][0]; a.X[2][1]=X[0][1]*X[2][0] - X[0][0]*X[2][1]; a.X[2][2]=X[0][0]*X[1][1] - X[0][1]*X[1][0]; det=X[0][0]*a.X[0][0] + X[0][1]*a.X[1][0] + X[0][2]*a.X[2][0]; invDet=1.0/det; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { a.X[i][j]*=invDet; } } return a; } /** * Calculate the determinant of this matrix. * * @return The determinant of this matrix */ public double det() { return X[0][0]*X[1][1]*X[2][2] + X[0][1]*X[1][2]*X[2][0] + X[0][2]*X[1][0]*X[2][1] - X[0][2]*X[1][1]*X[2][0] - X[0][1]*X[1][0]*X[2][2] - X[0][0]*X[1][2]*X[2][1]; } /** * Test if the given matrix is equal to this one. Two * matrices are equal if all components are equal. * * @param a the matrix to test for equality * @return true of matrices are equal, false otherwise */ public boolean equals(Matrix3 a) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(X[i][j]!=a.X[i][j]) { return false; } } } return true; } /** * Add given matrix a to the current one and return the result. * This operation does not affect the current matrix. * * @param a the matrix to add * @return the sum of both matrices \f$a_{ij}+b_{ij}\f$ */ public Matrix3 add(Matrix3 a) { Matrix3 ret=new Matrix3(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ret.X[i][j]=X[i][j]+a.X[i][j]; } } return ret; } /** * Subtract given matrix a from the current one and return the result. * This operation does not affect the current matrix. * * @param a the matrix to subtract * @return the difference of both matrices \f$b_{ij}-a_{ij}\f$ */ public Matrix3 sub(Matrix3 a) { Matrix3 ret=new Matrix3(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ret.X[i][j]=X[i][j]-a.X[i][j]; } } return ret; } /** * Multiply given matrix to the current one from the right and return * the result. This operation does not affect the current matrix. * * @param a the matrix to multiply * @return the multiplication result */ public Matrix3 multiply(Matrix3 a) { Matrix3 ret=new Matrix3(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ret.X[i][j]=X[i][0]*a.X[0][j]+X[i][1]*a.X[1][j]+X[i][2]*a.X[2][j]; } } return ret; } /** * Multiply the current matrix with the given vector from the left and * return the resulting vector. The operation does not affect the current * matrix. * * @param a the vector to multiply from the right * @return the resulting vector of the multiplication */ public Vector3D multiply(Vector3D a) { Vector3D res = new Vector3D(); res.X1=X[0][0]*a.X1+X[0][1]*a.X2+X[0][2]*a.X3; res.X2=X[1][0]*a.X1+X[1][1]*a.X2+X[1][2]*a.X3; res.X3=X[2][0]*a.X1+X[2][1]*a.X2+X[2][2]*a.X3; return res; } /** * Multiply the current matrix with the given scalar value and return * the result. The operation does not affect the current matrix. * * @param a the scalar value to multiply * @return the result of the multiplication \f$c \cdot a_{ij}\f$ */ public Matrix3 multiply(double a) { Matrix3 ret=new Matrix3(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ret.X[i][j]=X[i][j]*a; } } return ret; } }