Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

matrix3x3.cc

Go to the documentation of this file.
00001 /*
00002   svas_server -- virtual World Server of Svas
00003   Copyright (c) 2001, 2002 David Moreno Montero
00004  
00005  
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation; either version 2 of the License, or
00009   (at your option) any later version.
00010  
00011   This program is distributed in the hope that it will be useful, but
00012   WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   General Public License for more details.
00015  
00016   You should have received a copy of the GNU General Public License
00017   along with this program; if not, write to the Free Software
00018   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00019   02111-1307, USA.  
00020 
00021 */
00022 
00023 /// Adapted from 
00024 ///
00025 /// Physics for Game Developers
00026 /// David M. Bourg
00027 /// O'Reilly
00028 /// ISBN 0-596-00006-5
00029 
00030 
00031 #include "matrix3x3.h"
00032 #include "log.h"
00033 #include <stdio.h>
00034 
00035 inline Matrix3x3::Matrix3x3(void){
00036   e11=1; e12=0; e13=0;
00037   e21=0; e22=1; e23=0;
00038   e31=0; e32=0; e33=1;
00039 }
00040 
00041 inline Matrix3x3::Matrix3x3(double aa, double ab, double ac,
00042              double ba, double bb, double bc,
00043              double ca, double cb, double cc){
00044   e11=aa;  e12=ab;  e13=ac;
00045   e21=ba;  e22=bb;  e23=bc;
00046   e31=ca;  e32=cb;  e33=cc;
00047 }
00048 
00049 inline string Matrix3x3::toString(){
00050   char str[256];
00051   sprintf(str, "((%g,%g,%g)(%g,%g,%g)(%g,%g,%g))",
00052       e11,e12,e13,
00053       e21,e22,e23,
00054       e31,e32,e33);
00055   return string(str);
00056 }
00057 
00058 inline double Matrix3x3::Determinant(){
00059   return 
00060     e11*e22*e33 -
00061     e11*e32*e23 +
00062     e21*e32*e13 -
00063     e21*e12*e33 +
00064     e31*e12*e23 -
00065     e31*e22*e13;
00066 }
00067 
00068 inline Matrix3x3 Matrix3x3::Transpose(){
00069   return Matrix3x3(e11,e21,e31,e12,e22,e32,e13,e23,e33);
00070 }
00071 
00072 inline Matrix3x3 Matrix3x3::Inverse(){
00073   double d=Determinant();
00074   
00075   d=(d==0) ? 1 : d;
00076   
00077   return Matrix3x3( (e22*e33-e23*e32)/d,
00078            -(e12*e33-e13*e32)/d,
00079            -(e12*e33-e13*e22)/d,
00080            -(e21*e33-e23*e31)/d,
00081             (e11*e33-e13*e31)/d,
00082            -(e11*e23-e13*e21)/d,
00083             (e21*e32-e22*e31)/d,
00084            -(e11*e32-e12*e31)/d,
00085             (e11*e22-e12*e21)/d);
00086 }
00087 
00088 inline Matrix3x3 &Matrix3x3::operator+=(const Matrix3x3 &m){
00089   e11+=m.e11;  e12+=m.e12;  e13+=m.e13;
00090   e21+=m.e21;  e22+=m.e22;  e23+=m.e23;
00091   e31+=m.e31;  e32+=m.e32;  e33+=m.e33;
00092   return *this;
00093 }
00094 
00095 inline Matrix3x3 &Matrix3x3::operator-=(const Matrix3x3 &m){
00096   e11-=m.e11;  e12-=m.e12;  e13-=m.e13;
00097   e21-=m.e21;  e22-=m.e22;  e23-=m.e23;
00098   e31-=m.e31;  e32-=m.e32;  e33-=m.e33;
00099   return *this;
00100 }
00101 
00102 inline Matrix3x3 operator*(const Matrix3x3 &A, const Matrix3x3 &B){
00103   log <<"This method need check!"<<endl; //FIXME
00104   return Matrix3x3(A.e11*B.e11 + A.e12*B.e21 + A.e13*B.e31,
00105            A.e21*B.e11 + A.e22*B.e21 + A.e23*B.e31,
00106            A.e31*B.e11 + A.e32*B.e21 + A.e33*B.e31,
00107 
00108            A.e11*B.e12 + A.e12*B.e22 + A.e13*B.e32,
00109            A.e21*B.e12 + A.e22*B.e22 + A.e23*B.e32,
00110            A.e31*B.e12 + A.e32*B.e22 + A.e33*B.e32,
00111 
00112            A.e11*B.e13 + A.e12*B.e23 + A.e13*B.e33,
00113            A.e21*B.e13 + A.e22*B.e23 + A.e23*B.e33,
00114            A.e31*B.e13 + A.e32*B.e23 + A.e33*B.e33);
00115 
00116   /*
00117   int i,j,k;
00118   double t;
00119   Matrix3x3 ret;
00120   for (i=0;i<3;i++)
00121     for (j=0;j<3;j++){
00122       t=0;
00123       for (k=0;k<3;k++)
00124     t+=A.mat[(i*3)+k]*B.mat[(k*3)+j];
00125       ret.mat[(i*3)+j]=t;
00126     }
00127   return ret;
00128   */
00129 }
00130 
00131 inline Vector3d operator*(const Matrix3x3 &M, const Vector3d &v){
00132   return Vector3d ( M.e11*v.x + M.e12*v.y + M.e13*v.z,
00133             M.e21*v.x + M.e22*v.y + M.e23*v.z,
00134             M.e31*v.x + M.e32*v.y + M.e33*v.z);
00135 }
00136 
00137 inline Vector3d operator*(const Vector3d &v, const Matrix3x3 &M){
00138   return Vector3d ( M.e11*v.x + M.e21*v.y + M.e31*v.z,
00139             M.e12*v.x + M.e22*v.y + M.e32*v.z,
00140             M.e13*v.x + M.e23*v.y + M.e33*v.z);
00141 }

Generated on Mon Jun 17 19:53:44 2002 for Svas Server by doxygen1.2.16