TAPs 0.7.7.3
TAPsMatrix2x2.hpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsMatrix2x2.hpp
00003 
00004 Matrix2x2 class is a class for 2-by-2 matrices.
00005 
00006 SUKITTI PUNAK   (09/12/2004)
00007 UPDATE          (07/30/2005)
00008 ******************************************************************************/
00009 #ifndef TAPs_MATRIX2X2_H
00010 #define TAPs_MATRIX2X2_H
00011 
00012 #include "TAPsMath.hpp"
00013 
00014 template <typename T> class Vector2;
00015 
00016 BEGIN_NAMESPACE_TAPs
00017 //=============================================================================
00018 template <typename T>
00019 class Matrix2x2 {
00020 //=============================================================================
00021 private:
00022     // Matrix Elements
00023     T e[4];
00024 
00025 public:
00026     //-------------------------------------------------------------------------
00027     // Output Operator <<
00028     friend std::ostream & operator<< ( std::ostream &output, Matrix2x2<T> const &M )
00029     {
00030         int width = 14;
00031         //output << typeid(*this).name() << "( ";
00032         output  << "Matrix2x2<" << typeid(T).name() << "> =\n"
00033                 << "| " << std::setw(width) << M.e[0] 
00034                         << std::setw(width) << M.e[1] << " |\n"
00035                 << "| " << std::setw(width) << M.e[2] 
00036                         << std::setw(width) << M.e[3] << " |\n";
00037         //output << std::endl;
00038         return output;
00039     }
00040     //-------------------------------------------------------------------------
00041     // Constructors and Destructor
00042     Matrix2x2 ();       // default constructor (Identity Matrix)
00043     Matrix2x2 ( Matrix2x2<T> const &M );    // copy constructor
00044     Matrix2x2 ( T );                        // constructor
00045     Matrix2x2 ( T, T, T, T );               // constructor
00046     Matrix2x2 ( T const af[4] );            // constructor from array
00047     ~Matrix2x2 ();                          // destructor
00048     //-------------------------------------------------------------------------
00049     // Member Access
00050     inline T &       operator[] ( int i );
00051     inline T const & operator[] ( int i ) const;
00052     inline T &       operator() ( int r, int c );
00053     inline T const & operator() ( int r, int c ) const;
00054     //-------------------------------------------------------------------------
00055     // Convert to one dimension array
00056     operator const T *() const;
00057     operator T *();
00058     //-------------------------------------------------------------------------
00059     // Useful Functions
00060     inline void SetAllElements ( T );               // set all elements
00061     inline void SetAllElements ( T, T, T, T );      // set all elements
00062     inline void SetAllElements ( T const af[4] );   // set all elements
00063     inline void MakeIdentity ();                    // set to identity matrix
00064     inline void MakeDiagonal ( T d );               // set to diagonal matrix
00065     inline void MakeDiagonal ( T const af[4] ); // set to diagonal matrix
00066     inline void MakeZero ();                        // set all elements to zero
00067     inline bool IsIdentity () const;                // Is it an identity matrix?
00068     inline bool IsSymmetric () const                // Is it a symmetric matrix?
00069         { return e[1] == e[2]; }
00070     inline bool IsSquare () const                   // Is it a square matrix?
00071         { return true;}
00072     //-------------------------------------------------------------------------
00073     // Matrix Operations
00074     inline Matrix2x2<T> & Transposed ();
00075     inline Matrix2x2<T>   GetTranspose () const;
00076     inline Matrix2x2<T> & Inversed ();
00077     Matrix2x2<T>   GetInverse () const;
00078     inline T GetDeterminant () const;
00079     //-------------------------------------------------------------------------
00080     // Assignment Overloaded Operator
00081     Matrix2x2<T> & operator= ( Matrix2x2<T> const &M );
00082     //-------------------------------------------------------------------------
00083     // Unary Overloaded Operators
00084     Matrix2x2<T> operator- ();  // negation
00085     //-------------------------------------------------------------------------
00086     // Assign Overloaded Operators
00087     Matrix2x2<T> &operator+= ( Matrix2x2<T> const &M ); // += with matrix
00088     Matrix2x2<T> &operator-= ( Matrix2x2<T> const &M ); // -= with matrix
00089     Matrix2x2<T> &operator*= ( Matrix2x2<T> const &M ); // *= with matrix
00090     Matrix2x2<T> &operator*= ( T s );                   // *= with scalar
00091     Matrix2x2<T> &operator/= ( T s );                   // /= with scalar
00092     //-------------------------------------------------------------------------
00093     // Binary Overloaded Operators
00094     Matrix2x2<T> operator+ ( Matrix2x2<T> const &M ) const; // matrix + matrix
00095     Matrix2x2<T> operator- ( Matrix2x2<T> const &M ) const; // matrix - matrix
00096     Matrix2x2<T> operator* ( Matrix2x2<T> const &M ) const; // matrix * matrix
00097     Matrix2x2<T> operator* ( T s ) const;                   // matrix * scalar
00098     friend inline Matrix2x2<T> operator* ( T s, Matrix2x2<T> const &M ) // scalar * matrix
00099     {   return M * s;   }
00100     Matrix2x2<T> operator/ ( T s ) const;                   // matrix / scalar
00101     inline void MultLeft ( Matrix2x2<T> const &M );     // this matrix = matrix M * this matrix
00102     inline void MultRight ( Matrix2x2<T> const &M );    // this matrix = this matrix * matrix M
00103     //-------------------------------------------------------------------------
00104     // Matrix * Vector
00105     Vector2<T> operator* ( Vector2<T> const &V ) const;
00106     //-------------------------------------------------------------------------
00107 }; // END CLASS Matrix2x2
00108 //=============================================================================
00109 //-----------------------------------------------------------------------------
00110 // Define Matrix2x2s
00111 typedef Matrix2x2<int>          Matrix2x2i;
00112 typedef Matrix2x2<float>        Matrix2x2f;
00113 typedef Matrix2x2<double>       Matrix2x2d;
00114 typedef Matrix2x2<long double>  Matrix2x2ld;
00115 //=============================================================================
00116 END_NAMESPACE_TAPs
00117 //-----------------------------------------------------------------------------
00118 // Include definition if TAPs_USE_EXPORT is not defined
00119 //#if !defined( TAPs_USE_EXPORT )
00120     #include "TAPsMatrix2x2.cpp"
00121 //#endif
00122 //-----------------------------------------------------------------------------
00123 #endif
00124 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00125 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines