Matrix4d.h

Go to the documentation of this file.
00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API 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 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00027 //
00029 
00030 #ifndef __MATRIX4D_H__
00031 #define __MATRIX4D_H__
00032 
00033 #include "H3DApi.h"
00034 #include "H3DBasicTypes.h"
00035 #include "Vec4d.h"
00036 #include "H3DTemplateOperators.h"
00037 #include "Exception.h"
00038 #include "Matrix3d.h"
00039 
00040 namespace H3D {
00041   namespace ArithmeticTypes {
00042     class Quaternion;
00043     class Rotation;
00044     class Matrix4f;
00045 
00048     class H3DAPI_API Matrix4d {
00049     public:
00052       H3D_API_EXCEPTION( SingularMatrix4d );
00053 
00055       inline Matrix4d() { setToIdentity(); }
00056       
00058       inline Matrix4d( H3DDouble m00, H3DDouble m01, 
00059                        H3DDouble m02, H3DDouble m03,
00060                        H3DDouble m10, H3DDouble m11, 
00061                        H3DDouble m12, H3DDouble m13,
00062                        H3DDouble m20, H3DDouble m21, 
00063                        H3DDouble m22, H3DDouble m23,
00064                        H3DDouble m30, H3DDouble m31, 
00065                        H3DDouble m32, H3DDouble m33 ) {
00066         m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; 
00067         m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; 
00068         m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; 
00069         m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; 
00070       }
00071                                 
00073       Matrix4d( const Rotation &r );
00074 
00076       Matrix4d( const Quaternion &q );
00077 
00080       explicit Matrix4d( const Matrix3d &m );
00081 
00083       Matrix4d( const Matrix4f &m );
00084 
00086       inline void setToIdentity() {
00087         m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
00088         m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
00089         m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 0;
00090         m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
00091       }
00092                         
00101       Matrix4d transformInverse() const;
00102         
00104       Matrix4d inverse() const;
00105 
00107       inline Matrix4d transpose() const {
00108         return Matrix4d( m[0][0], m[1][0], m[2][0], m[3][0],
00109                          m[0][1], m[1][1], m[2][1], m[3][1],
00110                          m[0][2], m[1][2], m[2][2], m[3][2],
00111                          m[0][3], m[1][3], m[2][3], m[3][3] );
00112       };
00113       
00115       inline H3DDouble* operator[]( const int i ) { return m[i]; }
00116                                 
00118       inline const H3DDouble* operator[]( const int i ) const { return m[i]; }
00119 
00121       inline Vec4d getRow( int i ) const { 
00122         return Vec4d( m[i][0], m[i][1], m[i][2], m[i][3] ); 
00123       }
00124 
00126       inline Vec4d getColumn( int i ) const { 
00127         return Vec4d( m[0][i], m[1][i], m[2][i], m[3][i] ); 
00128       }
00129 
00131       inline H3DDouble getElement( int i, int j ) const { 
00132         return m[i][j];
00133       }
00134 
00136       inline void setElement( int i, int j, H3DDouble v ) { 
00137         m[i][j] = v;
00138       }
00139 
00141                         inline Matrix3d getScaleRotationPart() const {
00142                                 return Matrix3d( m[0][0], m[0][1], m[0][2],
00143                                                                                                  m[1][0], m[1][1], m[1][2],
00144                                                                                                  m[2][0], m[2][1], m[2][2] );
00145                         }
00146 
00148                         Matrix3d getRotationPart() const;
00149 
00151       inline Vec3d getScalePart() const {
00152         return getScaleRotationPart().getScalePart();
00153       }
00154 
00155     private:
00157       H3DDouble m[4][4];
00158     };
00159 
00166 
00168     inline Matrix4d operator*( const Matrix4d &m1, const Matrix4d &m2 ) {
00169       return Matrix4d( 
00170   m1[0][0]*m2[0][0] + m1[0][1]*m2[1][0] + m1[0][2]*m2[2][0] + m1[0][3]*m2[3][0],
00171   m1[0][0]*m2[0][1] + m1[0][1]*m2[1][1] + m1[0][2]*m2[2][1] + m1[0][3]*m2[3][1],
00172   m1[0][0]*m2[0][2] + m1[0][1]*m2[1][2] + m1[0][2]*m2[2][2] + m1[0][3]*m2[3][2],
00173   m1[0][0]*m2[0][3] + m1[0][1]*m2[1][3] + m1[0][2]*m2[2][3] + m1[0][3]*m2[3][3],
00174   
00175   m1[1][0]*m2[0][0] + m1[1][1]*m2[1][0] + m1[1][2]*m2[2][0] + m1[1][3]*m2[3][0],
00176   m1[1][0]*m2[0][1] + m1[1][1]*m2[1][1] + m1[1][2]*m2[2][1] + m1[1][3]*m2[3][1],
00177   m1[1][0]*m2[0][2] + m1[1][1]*m2[1][2] + m1[1][2]*m2[2][2] + m1[1][3]*m2[3][2],
00178   m1[1][0]*m2[0][3] + m1[1][1]*m2[1][3] + m1[1][2]*m2[2][3] + m1[1][3]*m2[3][3],
00179   
00180   m1[2][0]*m2[0][0] + m1[2][1]*m2[1][0] + m1[2][2]*m2[2][0] + m1[2][3]*m2[3][0],
00181   m1[2][0]*m2[0][1] + m1[2][1]*m2[1][1] + m1[2][2]*m2[2][1] + m1[2][3]*m2[3][1],
00182   m1[2][0]*m2[0][2] + m1[2][1]*m2[1][2] + m1[2][2]*m2[2][2] + m1[2][3]*m2[3][2],
00183   m1[2][0]*m2[0][3] + m1[2][1]*m2[1][3] + m1[2][2]*m2[2][3] + m1[2][3]*m2[3][3],
00184   
00185   m1[3][0]*m2[0][0] + m1[3][1]*m2[1][0] + m1[3][2]*m2[2][0] + m1[3][3]*m2[3][0],
00186   m1[3][0]*m2[0][1] + m1[3][1]*m2[1][1] + m1[3][2]*m2[2][1] + m1[3][3]*m2[3][1],
00187   m1[3][0]*m2[0][2] + m1[3][1]*m2[1][2] + m1[3][2]*m2[2][2] + m1[3][3]*m2[3][2],
00188   m1[3][0]*m2[0][3] + m1[3][1]*m2[1][3] + m1[3][2]*m2[2][3] + m1[3][3]*m2[3][3]
00189   );
00190     }
00191 
00193     inline Matrix4d operator+( const Matrix4d &m1, const Matrix4d &m2 ) {
00194       return Matrix4d( 
00195   m1[0][0]+m2[0][0], m1[0][1]+m2[0][1], m1[0][2]+m2[0][2], m1[0][3]+m2[0][3],
00196   m1[1][0]+m2[1][0], m1[1][1]+m2[1][1], m1[1][2]+m2[1][2], m1[1][3]+m2[1][3],
00197   m1[2][0]+m2[2][0], m1[2][1]+m2[2][1], m1[2][2]+m2[2][2], m1[2][3]+m2[2][3],
00198   m1[3][0]+m2[3][0], m1[3][1]+m2[3][1], m1[3][2]+m2[3][2], m1[3][3]+m2[3][3] );
00199     }
00200     
00202     inline Matrix4d operator*( const Matrix4d &m, const float &f ) {
00203       return Matrix4d( m[0][0]*f, m[0][1]*f, m[0][2]*f, m[0][3]*f,
00204                        m[1][0]*f, m[1][1]*f, m[1][2]*f, m[1][3]*f,
00205                        m[2][0]*f, m[2][1]*f, m[2][2]*f, m[2][3]*f,
00206                        m[3][0]*f, m[3][1]*f, m[3][2]*f, m[3][3]*f );
00207     }
00209     inline Matrix4d operator*( const Matrix4d &m, const double &d ) {
00210       return Matrix4d( (H3DDouble)(m[0][0]*d), (H3DDouble)(m[0][1]*d), (H3DDouble)(m[0][2]*d), (H3DDouble)(m[0][3]*d),
00211                        (H3DDouble)(m[1][0]*d), (H3DDouble)(m[1][1]*d), (H3DDouble)(m[1][2]*d), (H3DDouble)(m[1][3]*d),
00212                        (H3DDouble)(m[2][0]*d), (H3DDouble)(m[2][1]*d), (H3DDouble)(m[2][2]*d), (H3DDouble)(m[2][3]*d),
00213                        (H3DDouble)(m[3][0]*d), (H3DDouble)(m[3][1]*d), (H3DDouble)(m[3][2]*d), (H3DDouble)(m[3][3]*d) );
00214     }
00215 
00217     inline Matrix4d operator*( const Matrix4d &m, const int &f ) {
00218       return Matrix4d( m[0][0]*f, m[0][1]*f, m[0][2]*f, m[0][3]*f,
00219                        m[1][0]*f, m[1][1]*f, m[1][2]*f, m[1][3]*f,
00220                        m[2][0]*f, m[2][1]*f, m[2][2]*f, m[2][3]*f,
00221                        m[3][0]*f, m[3][1]*f, m[3][2]*f, m[3][3]*f );
00222     }
00223 
00225     inline Matrix4d operator*( const Matrix4d &m, const long &f ) {
00226       return Matrix4d( m[0][0]*f, m[0][1]*f, m[0][2]*f, m[0][3]*f,
00227                        m[1][0]*f, m[1][1]*f, m[1][2]*f, m[1][3]*f,
00228                        m[2][0]*f, m[2][1]*f, m[2][2]*f, m[2][3]*f,
00229                        m[3][0]*f, m[3][1]*f, m[3][2]*f, m[3][3]*f );
00230     }
00231           
00233     inline bool operator==( const Matrix4d &m1, const Matrix4d &m2 ) {
00234        return m1[0][0]==m2[0][0] && m1[0][1]==m2[0][1] && m1[0][2]==m2[0][2] && 
00235    m1[0][3]==m2[0][3] && m1[1][0]==m2[1][0] && m1[1][1]==m2[1][1] && 
00236    m1[1][2]==m2[1][2] && m1[1][3]==m2[1][3] && m1[2][0]==m2[2][0] && 
00237    m1[2][1]==m2[2][1] && m1[2][2]==m2[2][2] && m1[2][3]==m2[2][3] &&
00238    m1[3][0]==m2[3][0] && m1[3][1]==m2[3][1] && m1[3][2]==m2[3][2] && 
00239    m1[3][3]==m2[3][3];
00240      }
00241 
00243     H3DAPI_API ostream& operator<<( ostream &os, const Matrix4d &m );
00244 
00246     inline Matrix4d operator*( const float &a, const Matrix4d &b ) { return b * a; }
00247 
00249     inline Matrix4d operator*( const double &a, const Matrix4d &b ) { return b * a; }
00250 
00252     inline Matrix4d operator*( const int &a, const Matrix4d &b ) { return b * a; }
00253 
00255     inline Matrix4d operator*( const long &a, const Matrix4d &b ) { return b * a; }
00256     
00258     inline Matrix4d operator-( const Matrix4d &m ) { return m * -1; }
00259     
00261     inline Matrix4d operator-( const Matrix4d &a, const Matrix4d &b ) 
00262         { return a + (-b); }
00263     
00265   }
00266 }
00267 
00268 #endif

Generated on Thu Aug 24 12:38:33 2006 for H3D API by  doxygen 1.4.5