cmtkMatrix4x4.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2429 $
00026 //
00027 //  $LastChangedDate: 2010-10-08 16:18:42 -0700 (Fri, 08 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkMatrix4x4_h_included_
00034 #define __cmtkMatrix4x4_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkTypes.h>
00039 #include <Base/cmtkFixedVector.h>
00040 #include <Base/cmtkMatrix3x3.h>
00041 
00042 #include <System/cmtkConsole.h>
00043 #include <System/cmtkSmartPtr.h>
00044 
00045 namespace
00046 cmtk
00047 {
00048 
00051 
00053 template<class T=Types::Coordinate>
00054 class Matrix4x4
00055 {
00056 public:
00058   typedef Matrix4x4<T> Self;
00059 
00061   typedef SmartPointer<Self> SmartPtr;
00062 
00064   static const Self IdentityMatrix;
00065 
00070   Matrix4x4();
00071 
00073   Matrix4x4( const Matrix3x3<T>& other );
00074 
00079   Matrix4x4( const T *const values ) 
00080   {
00081     if ( values ) this->Set( values );
00082   }
00083 
00085   template<class T2>
00086   Matrix4x4( const T2 (&matrix)[4][4] ) 
00087   {
00088     for ( size_t j = 0; j < 4; ++j )
00089       for ( size_t i = 0; i < 4; ++i )
00090         this->Matrix[j][i] = matrix[j][i];
00091   }
00092   
00094   Self& Set( const T *const values );
00095 
00097   const Self GetInverse() const;
00098 
00100   Self GetTranspose() const;
00101 
00103   T* operator[]( const size_t i ) { return &this->Matrix[i][0]; }
00104 
00106   const T* operator[]( const size_t i ) const { return &this->Matrix[i][0]; }
00107 
00109   Self& Compose( const Types::Coordinate params[15], const bool logScaleFactors = false );
00110   
00112   bool Decompose( Types::Coordinate params[12], const Types::Coordinate *center = NULL, const bool logScaleFactors = false ) const;
00113 
00115   Self& operator*=( const Self& other );
00116   
00118   const Self operator*( const Self& other ) const;
00119 
00121   Self& operator=( const Matrix3x3<T>& other );
00122 
00125   Self& ChangeCoordinateSystem( const FixedVector<3,T>& newX, const  FixedVector<3,T>& newY );
00126 
00128   static Self RotateX( const T angle );
00129   
00131   static Self RotateY( const T angle );
00132   
00134   static Self RotateZ( const T angle );
00135   
00137   T FrobeniusNorm() const;
00138 
00140   void Print( Console& console ) const
00141   {
00142     for ( int j = 0; j < 4; ++j )
00143       {
00144       for ( int i = 0; i < 4; ++i )
00145         console.printf( "%f\t", (float)this->Matrix[j][i] );
00146       console << "\n";
00147       }
00148   }
00149 
00150 private:
00152   T Matrix[4][4];
00153 };
00154 
00155 template<typename T> const Matrix4x4<T> Matrix4x4<T>::IdentityMatrix;
00156 
00158 template<class T,class T2> 
00159 FixedVector<3,T2>&
00160 operator*=( FixedVector<3,T2>& u, const Matrix4x4<T>& M )
00161 {
00162   return u = u*M;
00163 }
00164 
00166 template<class T,class T2> 
00167 FixedVector<3,T2>
00168 operator*( const FixedVector<3,T2>& u, const Matrix4x4<T>& M )
00169 {
00170   FixedVector<3,T2> v;
00171   for ( int idx=0; idx<3; ++idx ) 
00172     v[idx] = u[0]*M[0][idx] + u[1]*M[1][idx] + u[2]*M[2][idx] + M[3][idx];
00173   return v;
00174 }
00175 
00177 template<class T>
00178 inline
00179 Console& operator<< ( Console& stream, const Matrix4x4<T>& m )
00180 {
00181   stream << "4x4 Matrix:\n";
00182   for ( int i = 0; i < 4; ++i ) 
00183     {
00184     for ( int j = 0; j < 4; ++j )
00185       stream << "[" << i << "][" << j << "]=" << m[i][j] << "\t";
00186     stream << "\n";
00187     }
00188   return stream;
00189 }
00190 
00192 
00193 } // namespace cmtk
00194 
00195 #endif // #ifndef __cmtkMatrix4x4_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines