Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Matrix.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007         Adapted from
00008 
00009         The Universe Development Kit
00010         Copyright (C) 2000  Sean O'Neil
00011         s_p_oneil@hotmail.com
00012 
00013     This library is free software; you can redistribute it and/or
00014     modify it under the terms of the GNU Lesser General Public
00015     License as published by the Free Software Foundation; either
00016     version 2.1 of the License, or (at your option) any later version.
00017 
00018     This library is distributed in the hope that it will be useful,
00019     but WITHOUT ANY WARRANTY; without even the implied warranty of
00020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021     Lesser General Public License for more details.
00022 
00023     You should have received a copy of the GNU Lesser General Public
00024     License along with this library; if not, write to the Free Software
00025     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 
00027     $Id: Matrix.cpp,v 1.6 2002/01/22 19:30:05 tksuoran Exp $
00028 */
00029 
00030 
00031 #include "Teddy/Maths/Matrix.h"
00032 #include "Teddy/Maths/Quaternion.h"
00033 
00034 
00035 namespace Teddy {
00036 namespace Maths {
00037 
00038 
00039 static const float a_identity_matrix[] = {
00040     1,0,0,0, 
00041     0,1,0,0,
00042     0,0,1,0,
00043     0,0,0,1
00044 };
00045 static const float a_swap_xy_matrix[] = {
00046     0,1,0,0, 
00047     1,0,0,0,
00048     0,0,1,0,
00049     0,0,0,1
00050 };
00051 static const float a_swap_xz_matrix[] = {
00052     0,0,1,0, 
00053     0,1,0,0,
00054     1,0,0,0,
00055     0,0,0,1
00056 };
00057 static const float a_swap_yz_matrix[] = {
00058     1,0,0,0, 
00059     0,0,1,0,
00060     0,1,0,0,
00061     0,0,0,1
00062 };
00063 static const float a_mirror_x_matrix[] = {
00064    -1, 0, 0, 0, 
00065     0, 1, 0, 0,
00066     0, 0, 1, 0,
00067     0, 0, 0, 1
00068 };
00069 static const float a_mirror_y_matrix[] = {
00070     1, 0, 0, 0, 
00071     0,-1, 0, 0,
00072     0, 0, 1, 0,
00073     0, 0, 0, 1
00074 };
00075 static const float a_mirror_z_matrix[] = {
00076     1, 0, 0, 0, 
00077     0, 1, 0, 0,
00078     0, 0,-1, 0,
00079     0, 0, 0, 1
00080 };
00081 static const float *const mat_i   = &a_identity_matrix[0];
00082 static const float *const mat_sxy = &a_swap_xy_matrix [0];
00083 static const float *const mat_sxz = &a_swap_xz_matrix [0];
00084 static const float *const mat_syz = &a_swap_yz_matrix [0];
00085 static const float *const mat_mx  = &a_mirror_x_matrix[0];
00086 static const float *const mat_my  = &a_mirror_y_matrix[0];
00087 static const float *const mat_mz  = &a_mirror_z_matrix[0];
00088 /*static*/ const Matrix Matrix::IDENTITY( mat_i );
00089 /*static*/ const Matrix Matrix::SWAP_XY ( mat_sxy );
00090 /*static*/ const Matrix Matrix::SWAP_XZ ( mat_sxz );
00091 /*static*/ const Matrix Matrix::SWAP_YZ ( mat_syz );
00092 /*static*/ const Matrix Matrix::MIRROR_X( mat_mx );
00093 /*static*/ const Matrix Matrix::MIRROR_Y( mat_my );
00094 /*static*/ const Matrix Matrix::MIRROR_Z( mat_mz );
00095 
00096 
00097 void Matrix::operator=( const Quaternion &q ){
00098     // 9 muls, 15 adds
00099     double x2 = q.v[0] + q.v[0];
00100     double y2 = q.v[1] + q.v[1];
00101     double z2 = q.v[2] + q.v[2];
00102     double xx = q.v[0] * x2;  double xy = q.v[0] * y2;  double xz = q.v[0] * z2;
00103     double yy = q.v[1] * y2;  double yz = q.v[1] * z2;  double zz = q.v[2] * z2;
00104     double wx = q.v[3] * x2;  double wy = q.v[3] * y2;  double wz = q.v[3] * z2;
00105 
00106     m[0][3] = 
00107     m[1][3] = 
00108     m[2][3] = 
00109     m[3][0] = m[3][1] = m[3][2] = 0; 
00110     m[3][3] = 1;
00111     m[0][0] = (float)(1-(yy+zz));  m[1][0] = (float)(xy+wz);      m[2][0] = (float)(xz-wy);
00112     m[0][1] = (float)(xy-wz);      m[1][1] = (float)(1-(xx+zz));  m[2][1] = (float)(yz+wx);
00113     m[0][2] = (float)(xz+wy);      m[1][2] = (float)(yz-wx);      m[2][2] = (float)(1-(xx+yy));
00114 }
00115 
00116 Matrix Matrix::operator*( const Matrix &mat ) const {
00117     // 36 muls, 27 adds
00118     // | m[0][0] m[1][0] m[2][0] m[3][0] |   | mat.m[0][0] mat.m[1][0] mat.m[2][0] mat.m[3][0] |   | m[0][0]*mat.m[0][0]+m[1][0]*mat.m[0][1]+m[2][0]*mat.m[0][2] m[0][0]*mat.m[1][0]+m[1][0]*mat.m[1][1]+m[2][0]*mat.m[1][2] m[0][0]*mat.m[2][0]+m[1][0]*mat.m[2][1]+m[2][0]*mat.m[2][2] m[0][0]*mat.m[3][0]+m[1][0]*mat.m[3][1]+m[2][0]*mat.m[3][2]+m[3][0] |
00119     // | m[0][1] m[1][1] m[2][1] m[3][1] |   | mat.m[0][1] mat.m[1][1] mat.m[2][1] mat.m[3][1] |   | m[0][1]*mat.m[0][0]+m[1][1]*mat.m[0][1]+m[2][1]*mat.m[0][2] m[0][1]*mat.m[1][0]+m[1][1]*mat.m[1][1]+m[2][1]*mat.m[1][2] m[0][1]*mat.m[2][0]+m[1][1]*mat.m[2][1]+m[2][1]*mat.m[2][2] m[0][1]*mat.m[3][0]+m[1][1]*mat.m[3][1]+m[2][1]*mat.m[3][2]+m[3][1] |
00120     // | m[0][2] m[1][2] m[2][2] m[3][2] | * | mat.m[0][2] mat.m[1][2] mat.m[2][2] mat.m[3][2] | = | m[0][2]*mat.m[0][0]+m[1][2]*mat.m[0][1]+m[2][2]*mat.m[0][2] m[0][2]*mat.m[1][0]+m[1][2]*mat.m[1][1]+m[2][2]*mat.m[1][2] m[0][2]*mat.m[2][0]+m[1][2]*mat.m[2][1]+m[2][2]*mat.m[2][2] m[0][2]*mat.m[3][0]+m[1][2]*mat.m[3][1]+m[2][2]*mat.m[3][2]+m[3][2] |
00121     // | 0   0   0   1   |   | 0     0     0     1     |   | 0                             0                             0                             1                                 |
00122     Matrix mRet;
00123 
00124     mRet.m[0][0] = m[0][0]*mat.m[0][0] + m[1][0]*mat.m[0][1] + m[2][0]*mat.m[0][2] + m[3][0]*mat.m[0][3];
00125     mRet.m[1][0] = m[0][0]*mat.m[1][0] + m[1][0]*mat.m[1][1] + m[2][0]*mat.m[1][2] + m[3][0]*mat.m[1][3];
00126     mRet.m[2][0] = m[0][0]*mat.m[2][0] + m[1][0]*mat.m[2][1] + m[2][0]*mat.m[2][2] + m[3][0]*mat.m[2][3];
00127     mRet.m[3][0] = m[0][0]*mat.m[3][0] + m[1][0]*mat.m[3][1] + m[2][0]*mat.m[3][2] + m[3][0]*mat.m[3][3];
00128 
00129     mRet.m[0][1] = m[0][1]*mat.m[0][0] + m[1][1]*mat.m[0][1] + m[2][1]*mat.m[0][2] + m[3][1]*mat.m[0][3];
00130     mRet.m[1][1] = m[0][1]*mat.m[1][0] + m[1][1]*mat.m[1][1] + m[2][1]*mat.m[1][2] + m[3][1]*mat.m[1][3];
00131     mRet.m[2][1] = m[0][1]*mat.m[2][0] + m[1][1]*mat.m[2][1] + m[2][1]*mat.m[2][2] + m[3][1]*mat.m[2][3];
00132     mRet.m[3][1] = m[0][1]*mat.m[3][0] + m[1][1]*mat.m[3][1] + m[2][1]*mat.m[3][2] + m[3][1]*mat.m[3][3];
00133 
00134     mRet.m[0][2] = m[0][2]*mat.m[0][0] + m[1][2]*mat.m[0][1] + m[2][2]*mat.m[0][2] + m[3][2]*mat.m[0][3];
00135     mRet.m[1][2] = m[0][2]*mat.m[1][0] + m[1][2]*mat.m[1][1] + m[2][2]*mat.m[1][2] + m[3][2]*mat.m[1][3];
00136     mRet.m[2][2] = m[0][2]*mat.m[2][0] + m[1][2]*mat.m[2][1] + m[2][2]*mat.m[2][2] + m[3][2]*mat.m[2][3];
00137     mRet.m[3][2] = m[0][2]*mat.m[3][0] + m[1][2]*mat.m[3][1] + m[2][2]*mat.m[3][2] + m[3][2]*mat.m[3][3];
00138 
00139     mRet.m[0][3] = m[0][3]*mat.m[0][0] + m[1][3]*mat.m[0][1] + m[2][3]*mat.m[0][2] + m[3][3]*mat.m[0][3];
00140     mRet.m[1][3] = m[0][3]*mat.m[1][0] + m[1][3]*mat.m[1][1] + m[2][3]*mat.m[1][2] + m[3][3]*mat.m[1][3];
00141     mRet.m[2][3] = m[0][3]*mat.m[2][0] + m[1][3]*mat.m[2][1] + m[2][3]*mat.m[2][2] + m[3][3]*mat.m[2][3];
00142     mRet.m[3][3] = m[0][3]*mat.m[3][0] + m[1][3]*mat.m[3][1] + m[2][3]*mat.m[3][2] + m[3][3]*mat.m[3][3];
00143 
00144     return mRet;
00145 }
00146 
00147 
00148 };  //  namespace Maths
00149 };  //  namespace Teddy
00150