/* * 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 6, 2005 */ package ml.test; import junit.framework.TestCase; import ml.math.Matrix4; import ml.math.Vector3D; /** * This is the test case for ml.test.Matrix3 * * @author gregor */ public class Matrix4Test extends TestCase { public static void main(String[] args) { junit.awtui.TestRunner.run(Matrix4Test.class); } public void testMatrix4() { Matrix4 a=new Matrix4(); assertTrue(a.X!=null); for(int i=0;i<4;i++) { assertTrue(a.X[i]!=null); } } public void testClear() { Matrix4 a=new Matrix4(); a.clear(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { assertTrue(a.X[i][j]==0); } } } public void testTranspose() { Matrix4 a, b; a = new Matrix4(); b = new Matrix4(); a.X[0][0] = 1.0; a.X[0][1] = 2.0; a.X[0][2] = 3.0; a.X[0][3] = 4.0; a.X[1][0] = 1.0; a.X[1][1] = 2.0; a.X[1][2] = 3.0; a.X[1][3] = 4.0; a.X[2][0] = 1.0; a.X[2][1] = 2.0; a.X[2][2] = 3.0; a.X[2][3] = 4.0; a.X[3][0] = 1.0; a.X[3][1] = 2.0; a.X[3][2] = 3.0; a.X[3][3] = 4.0; b.X[0][0] = 1.0; b.X[0][1] = 1.0; b.X[0][2] = 1.0; b.X[0][3] = 1.0; b.X[1][0] = 2.0; b.X[1][1] = 2.0; b.X[1][2] = 2.0; b.X[1][3] = 2.0; b.X[2][0] = 3.0; b.X[2][1] = 3.0; b.X[2][2] = 3.0; b.X[2][3] = 3.0; b.X[3][0] = 4.0; b.X[3][1] = 4.0; b.X[3][2] = 4.0; b.X[3][3] = 4.0; assertTrue(a.transpose().equals(b)); } public void testInvert() { Matrix4 a, b; a = new Matrix4(); a.X[0][0] = 1.0; a.X[0][1] = 0.0; a.X[0][2] = 0.0; a.X[0][3] = 0.0; a.X[1][0] = 0.0; a.X[1][1] = 1.0; a.X[1][2] = 0.0; a.X[1][3] = 0.0; a.X[2][0] = 0.0; a.X[2][1] = 0.0; a.X[2][2] = 1.0; a.X[2][3] = 0.0; a.X[3][0] = 0.0; a.X[3][1] = 0.0; a.X[3][2] = 0.0; a.X[3][3] = 1.0; b = a.invert(); assertTrue(b.equals(a)); a.X[0][0] = 1.0 / 3.0; b.X[0][0] = 3.0; assertTrue(b.equals(a.invert())); a.X[0][0] = 1.0; a.X[0][1] = 2.0; a.X[0][2] = 3.0; a.X[0][3] = 4.0; a.X[1][0] = 2.0; a.X[1][1] = 2.0; a.X[1][2] = 3.0; a.X[1][3] = 4.0; a.X[2][0] = 3.0; a.X[2][1] = 3.0; a.X[2][2] = 3.0; a.X[2][3] = 4.0; a.X[3][0] = 4.0; a.X[3][1] = 4.0; a.X[3][2] = 4.0; a.X[3][3] = 4.0; Matrix4 c=new Matrix4(); c.X[0][0] = -1.0; c.X[0][1] = 1.0; c.X[0][2] = 0.0; c.X[0][3] = 0.0; c.X[1][0] = 1.0; c.X[1][1] = -2.0; c.X[1][2] = 1.0; c.X[1][3] = 0.0; c.X[2][0] = 0.0; c.X[2][1] = 1.0; c.X[2][2] = -2.0; c.X[2][3] = 1.0; c.X[3][0] = 0.0; c.X[3][1] = 0.0; c.X[3][2] = 1.0; c.X[3][3] = -0.75; assertTrue(a.invert().equals(c)); a.X[0][0] = 1.0; a.X[0][1] = 2.0; a.X[0][2] = 1.0; a.X[0][3] = 1.0; a.X[1][0] = 1.0; a.X[1][1] = 1.0; a.X[1][2] = 1.0; a.X[1][3] = 2.0; a.X[2][0] = 1.0; a.X[2][1] = 1.0; a.X[2][2] = 2.0; a.X[2][3] = 3.0; a.X[3][0] = 1.0; a.X[3][1] = 1.0; a.X[3][2] = 1.0; a.X[3][3] = 4.0; c.X[0][0] = -1.0; c.X[0][1] = 4.0; c.X[0][2] = -1.0; c.X[0][3] = -1.0; c.X[1][0] = 1.0; c.X[1][1] = -1.5; c.X[1][2] = 0.0; c.X[1][3] = 0.5; c.X[2][0] = 0.0; c.X[2][1] = -0.5; c.X[2][2] = 1.0; c.X[2][3] = -0.5; c.X[3][0] = 0.0; c.X[3][1] = -0.5; c.X[3][2] = 0.0; c.X[3][3] = 0.5; assertTrue(a.invert().equals(c)); } public void testDet() { Matrix4 a; a = new Matrix4(); a.X[0][0] = 1.0; a.X[0][1] = 2.0; a.X[0][2] = 3.0; a.X[0][3] = 4.0; a.X[1][0] = 1.0; a.X[1][1] = 2.0; a.X[1][2] = 3.0; a.X[1][3] = 4.0; a.X[2][0] = 1.0; a.X[2][1] = 2.0; a.X[2][2] = 3.0; a.X[2][3] = 4.0; a.X[3][0] = 1.0; a.X[3][1] = 2.0; a.X[3][2] = 3.0; a.X[3][3] = 4.0; assertTrue(a.det() == 0); } public void testMultiplyMatrix4() { Matrix4 a, b, c; a=new Matrix4(); b=new Matrix4(); c=new Matrix4(); a.X[0][0]=1.0; a.X[0][1]=2.0; a.X[0][2]=3.0; a.X[0][3]=4.0; a.X[1][0]=1.0; a.X[1][1]=2.0; a.X[1][2]=3.0; a.X[1][3]=4.0; a.X[2][0]=1.0; a.X[2][1]=2.0; a.X[2][2]=3.0; a.X[2][3]=4.0; a.X[3][0]=1.0; a.X[3][1]=2.0; a.X[3][2]=3.0; a.X[3][3]=8.0; b.X[0][0]=1.0; b.X[0][1]=1.0; b.X[0][2]=1.0; b.X[0][3]=1.0; b.X[1][0]=2.0; b.X[1][1]=2.0; b.X[1][2]=2.0; b.X[1][3]=2.0; b.X[2][0]=3.0; b.X[2][1]=3.0; b.X[2][2]=3.0; b.X[2][3]=3.0; b.X[3][0]=4.0; b.X[3][1]=4.0; b.X[3][2]=4.0; b.X[3][3]=4.0; c.X[0][0]=30.0; c.X[0][1]=30.0; c.X[0][2]=30.0; c.X[0][3]=30.0; c.X[1][0]=30.0; c.X[1][1]=30.0; c.X[1][2]=30.0; c.X[1][3]=30.0; c.X[2][0]=30.0; c.X[2][1]=30.0; c.X[2][2]=30.0; c.X[2][3]=30.0; c.X[3][0]=46.0; c.X[3][1]=46.0; c.X[3][2]=46.0; c.X[3][3]=46.0; assertTrue(a.multiply(b).equals(c)); } public void testMultiplyVector3D() { Matrix4 a; Vector3D b, c; a=new Matrix4(); b=new Vector3D(); c=new Vector3D(); a.X[0][0]=1.0; a.X[0][1]=2.0; a.X[0][2]=3.0; a.X[0][3]=0.0; a.X[1][0]=1.0; a.X[1][1]=2.0; a.X[1][2]=3.0; a.X[2][3]=0.0; a.X[2][0]=1.0; a.X[2][1]=2.0; a.X[2][2]=6.0; a.X[2][3]=0.0; a.X[3][0]=0.0; a.X[3][1]=0.0; a.X[3][2]=0.0; a.X[3][3]=1.0; b.X1=1.0; b.X2=2.0; b.X3=3.0; c.X1=14.0; c.X2=14.0; c.X3=23.0; assertTrue(a.multiply(b).equals(c)); } public void testMultiplyDouble3D() { Matrix4 a, b; a=new Matrix4(); b=new Matrix4(); a.X[0][0]=1.0; a.X[0][1]=2.0; a.X[0][2]=3.0; a.X[0][3]=4.0; a.X[1][0]=1.0; a.X[1][1]=2.0; a.X[1][2]=3.0; a.X[1][3]=4.0; a.X[2][0]=1.0; a.X[2][1]=2.0; a.X[2][2]=3.0; a.X[2][3]=4.0; a.X[3][0]=1.0; a.X[3][1]=2.0; a.X[3][2]=3.0; a.X[3][3]=4.0; b.X[0][0]=2.0; b.X[0][1]=4.0; b.X[0][2]=6.0; b.X[0][3]=8.0; b.X[1][0]=2.0; b.X[1][1]=4.0; b.X[1][2]=6.0; b.X[1][3]=8.0; b.X[2][0]=2.0; b.X[2][1]=4.0; b.X[2][2]=6.0; b.X[2][3]=8.0; b.X[3][0]=2.0; b.X[3][1]=4.0; b.X[3][2]=6.0; b.X[3][3]=8.0; assertTrue(a.multiply(2).equals(b)); assertTrue(b.multiply(0.5).equals(a)); } }