cmtkMatrix3x3.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: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkMatrix3x3_h_included_
00034 #define __cmtkMatrix3x3_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkTypes.h>
00039 #include <Base/cmtkFixedVector.h>
00040 
00041 #include <System/cmtkConsole.h>
00042 
00043 namespace
00044 cmtk
00045 {
00046 
00049 
00051 template<class T=Types::Coordinate>
00052 class Matrix3x3
00053 {
00054 public:
00056   typedef Matrix3x3<T> Self;
00057 
00063   Matrix3x3();
00064 
00066   Matrix3x3( const Self& other );
00067 
00072   Matrix3x3( const T *const values ) 
00073   {
00074     if ( values ) this->Set( values );
00075   }
00076 
00078   template<class T2>
00079   Matrix3x3( const T2 (&matrix)[3][3] ) 
00080   {
00081     for ( size_t j = 0; j < 3; ++j )
00082       for ( size_t i = 0; i < 3; ++i )
00083         this->Matrix[j][i] = matrix[j][i];
00084   }
00085 
00087   Self& Set( const T *const values );
00088 
00090   Self& Fill( const T value );
00091 
00093   Self& Invert2x2();
00094 
00096   Self& Invert3x3();
00097 
00099   Self GetTranspose() const;
00100 
00102   T* operator[]( const size_t i )
00103   { 
00104     return this->Matrix[i]; 
00105   }
00106 
00108   const T* operator[]( const size_t i ) const
00109   { 
00110     return this->Matrix[i]; 
00111   }
00112   
00114   Self& Compose( const Types::Coordinate params[8] );
00115   
00117   bool Decompose( Types::Coordinate params[8], const Types::Coordinate *center = NULL ) const;
00118 
00120   Self& operator*=( const Self& other );
00121   
00123   Self& operator*=( const T scalar );
00124   
00126   const Self operator*( const Self& other ) const;
00127 
00129   Self& operator=( const Self& other );
00130 
00131   /* Multiply with homogeneous 3d vector (implicitly made homogeneous).
00132    *\note The '&' declaration of both arguments forces the C++ compiler to
00133    * maintain them as explicitly sized arrays, rather than collapsing to
00134    * pointers. This is what allows us to overload this function based on the
00135    * array size (!) of its arguments.
00136    */
00137   template<class T2> void Multiply( const FixedVector<2,T2>& u, FixedVector<2,T2>& v ) const 
00138   {
00139     for ( int idx=0; idx < 2; ++idx ) 
00140       v[idx] = u[0]*Matrix[0][idx] + u[1]*Matrix[1][idx] + Matrix[2][idx];
00141   }
00142   
00149   template<class T2> void Multiply( const FixedVector<3,T2>& u,  FixedVector<3,T2>& v ) const 
00150   {
00151     for ( int idx=0; idx < 3; ++idx ) 
00152       v[idx] = u[0]*Matrix[0][idx] + u[1]*Matrix[1][idx] + u[2]*Matrix[2][idx];
00153   }
00154   
00156   template<class T2> void Multiply( FixedVector<2,T2>& v ) const 
00157   {
00158     const FixedVector<2,T2> u( v );
00159     this->Multiply( u, v );
00160   }
00161   
00163   template<class T2> void Multiply( FixedVector<3,T2>& v ) const 
00164   {
00165     const FixedVector<3,T2> u( v );
00166     this->Multiply( u, v );
00167   }
00168 
00170   T Determinant() const 
00171   {
00172     return ( this->Matrix[0][0]*this->Matrix[1][1]*this->Matrix[2][2] + 
00173              this->Matrix[0][1]*this->Matrix[1][2]*this->Matrix[2][0] + 
00174              this->Matrix[0][2]*this->Matrix[1][0]*this->Matrix[2][1] - 
00175              this->Matrix[0][2]*this->Matrix[1][1]*this->Matrix[2][0] - 
00176              this->Matrix[0][0]*this->Matrix[1][2]*this->Matrix[2][1] - 
00177              this->Matrix[0][1]*this->Matrix[1][0]*this->Matrix[2][2] );
00178   }
00179 
00181   T FrobeniusNorm() const;
00182 
00184   void ComputeEigenvalues( T (&lambda)[3] ) const;
00185 
00186 private:
00188   T Matrix[3][3];
00189 };
00190 
00192 typedef Matrix3x3<Types::Coordinate> CoordinateMatrix3x3;
00193 
00195 template<class T>
00196 inline
00197 Console& operator<< ( Console& stream, const Matrix3x3<T>& m )
00198 {
00199   stream << "3x3 Matrix:\n";
00200   for ( int i = 0; i < 3; ++i ) 
00201     {
00202     for ( int j = 0; j < 3; ++j )
00203       stream << m[i][j] << "\t";
00204     stream << "\n";
00205     }
00206   return stream;
00207 }
00208 
00210 
00211 } // namespace cmtk
00212 
00213 #endif // #ifndef __cmtkMatrix3x3_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines