Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Tools/Math/Matrix2x2.h

Go to the documentation of this file.

    00001 /**
    00002  * @file Matrix2x2.h
    00003  * Contains template class Matrix2x2 of type V
    00004  *
    00005  * @author <a href="mailto:Kai_Engel@gmx.de">Kai Engel</a>
    00006  * @author Kai Engel
    00007  */
    00008
    00009 #ifndef __Matrix2x2_h__
    00010 #define __Matrix2x2_h__
    00011
    00012 #include "Vector2.h"
    00013
    00014 /** This class represents a 2x2-matrix */
    00015 template <class V> class Matrix2x2 {
    00016 public:
    00017   /** The columns of the matrix */
    00018   Vector2<V> c[2];
    00019
    00020   /** Default constructor. */
    00021   Matrix2x2<V>()
    00022   {
    00023     c[0]=Vector2<V>(1,0);
    00024     c[1]=Vector2<V>(0,1);
    00025   }
    00026
    00027   //! Constructor
    00028   /*!
    00029   \param c0 the first column of the matrix.
    00030   \param c1 the second column of the matrix.
    00031   */
    00032   Matrix2x2<V>(const Vector2<V>& c0,const Vector2<V>& c1)
    00033  {
    00034     c[0]=c0;
    00035     c[1]=c1;
    00036   }
    00037
    00038   //! Assignment operator
    00039   /*!
    00040   \param other The other matrix that is assigned to this one
    00041   \return A reference to this object after the assignment.
    00042   */
    00043   Matrix2x2<V>& operator=(const Matrix2x2<V>& other)
    00044   {
    00045     c[0] = other.c[0];
    00046     c[1] = other.c[1];
    00047     return *this;
    00048   }
    00049
    00050   //! Copy constructor
    00051   /*!
    00052   \param other The other matrix that is copied to this one
    00053    */
    00054   Matrix2x2<V>(const Matrix2x2<V>& other)
    00055   {
    00056     *this = other;
    00057   }
    00058
    00059
    00060   /**
    00061    * Array-like member access.
    00062    * \param  i index
    00063    * \return reference to column
    00064    */
    00065   Vector2<V>& operator[](int i)
    00066   {
    00067     return c[i];
    00068   }
    00069
    00070   //! Multiplication of this matrix by vector.
    00071   /*!
    00072   \param vector The vector this one is multiplied by
    00073   \return A reference to a new vector containing the result
    00074     of the calculation.
    00075   */
    00076   Vector2<V> operator*(const Vector2<V>& vector) const
    00077   {
    00078     return (c[0]*vector.x + c[1]*vector.y);
    00079   }
    00080
    00081
    00082   //! Multiplication of this matrix by another matrix.
    00083   /*!
    00084   \param other The other matrix this one is multiplied by
    00085   \return An object containing the result of the calculation.
    00086   */
    00087   Matrix2x2<V> operator*(const Matrix2x2<V>& other) const
    00088   {
    00089     Matrix2x2<V> returnMatrix;
    00090     returnMatrix.c[0].x = c[0].x * other.c[0].x + c[1].x * other.c[0].y;
    00091     returnMatrix.c[0].y = c[0].y * other.c[0].x + c[1].y * other.c[0].y;
    00092     returnMatrix.c[1].x = c[0].x * other.c[1].x + c[1].x * other.c[1].y;
    00093     returnMatrix.c[1].y = c[0].y * other.c[1].x + c[1].y * other.c[1].y;
    00094     return returnMatrix;
    00095   }
    00096
    00097   //! Multiplication of this matrix by another matrix.
    00098   /*!
    00099   \param other The other matrix this one is multiplied by
    00100   \return A reference this object after the calculation.
    00101   */
    00102   Matrix2x2<V> operator*=(const Matrix2x2<V>& other)
    00103   {
    00104     return *this = *this * other;
    00105   }
    00106
    00107   //! Multiplication of this matrix by a factor.
    00108   /*!
    00109   \param factor The factor this matrix is multiplied by
    00110   \return A reference to this object after the calculation.
    00111   */
    00112   Matrix2x2<V>& operator*=(const V& factor)
    00113   {
    00114     c[0] *= factor;
    00115     c[1] *= factor;
    00116     return *this;
    00117   }
    00118
    00119
    00120   //! Division of this matrix by a factor.
    00121   /*!
    00122   \param factor The factor this matrix is divided by
    00123   \return A reference to this object after the calculation.
    00124    */
    00125   Matrix2x2<V>& operator/=(const V& factor)
    00126   {
    00127     return *this *= 1 / factor;
    00128   }
    00129
    00130
    00131   //! Multiplication of this matrix by a factor.
    00132   /*!
    00133   \param factor The factor this matrix is multiplied by
    00134   \return A new object that contains the result of the calculation.
    00135   */
    00136   Matrix2x2<V> operator*(const V& factor) const
    00137   {
    00138     return Matrix2x2<V>(*this) *= factor;
    00139   }
    00140
    00141
    00142   //! Division of this matrix by a factor.
    00143   /*!
    00144   \param factor The factor this matrix is divided by
    00145   \return A new object that contains the result of the calculation.
    00146   */
    00147   Matrix2x2<V> operator/(const V& factor) const
    00148   {
    00149     return Matrix2x2<V>(*this) /= factor;
    00150   }
    00151
    00152
    00153   // by Kai
    00154   Matrix2x2<V> operator+(const Matrix2x2<V>& other) const
    00155   {
    00156     return Matrix2x2<V>(Vector2<V>(c[0].x + other.c[0].x,c[0].y + other.c[0].y),
    00157                         Vector2<V>(c[1].x + other.c[1].x,c[1].y + other.c[1].y));
    00158   }
    00159
    00160
    00161   // by Kai
    00162   Matrix2x2<V> operator-(const Matrix2x2<V>& other) const
    00163   {
    00164     return Matrix2x2<V>(Vector2<V>(c[0].x - other.c[0].x,c[0].y - other.c[0].y),
    00165                         Vector2<V>(c[1].x - other.c[1].x,c[1].y - other.c[1].y));
    00166   }
    00167
    00168   // by Kai
    00169   Matrix2x2<V> invert()
    00170   {
    00171     double globFactor;
    00172     if ((c[1].y * c[0].x - c[0].y * c[1].x) != 0.0)
    00173       globFactor = 1 / (c[1].y * c[0].x - c[0].y * c[1].x);
    00174     else globFactor = 0.00000000000000001;
    00175     return Matrix2x2<V>(Vector2<V>(globFactor * c[1].y,-globFactor * c[0].y),
    00176                         Vector2<V>(-globFactor * c[1].x,globFactor * c[0].x));
    00177   }
    00178
    00179
    00180   //! Comparison of another matrix with this one.
    00181   /*!
    00182   \param other The other matrix that will be compared to this one
    00183   \return Whether the two matrices are equal.
    00184   */
    00185   bool operator==(const Matrix2x2<V>& other) const
    00186   {
    00187     return (c[0]==other.c[0] && c[1]==other.c[1]);
    00188   }
    00189
    00190
    00191   //! Comparison of another matrix with this one.
    00192   /*!
    00193   \param other The other matrix that will be compared to this one
    00194   \return Whether the two matrixs are unequal.
    00195   */
    00196   bool operator!=(const Matrix2x2<V>& other) const
    00197   {
    00198     return !(*this == other);
    00199   }
    00200
    00201
    00202   /*! Transpose the matrix
    00203   \return A new object containing transposed matrix
    00204   */
    00205   Matrix2x2<V> transpose() const
    00206   {
    00207     return Matrix2x2<V>(Vector2<V>(c[0].x,c[1].x),
    00208                         Vector2<V>(c[0].y,c[1].y));
    00209   }
    00210
    00211
    00212   //! Calculation of the determinant of this matrix.
    00213   /*!
    00214   \return The determinant.
    00215   */
    00216   V det() const
    00217     {
    00218        return c[0].x * c[1].y - c[1].x * c[0].y;
    00219     }
    00220 };
    00221
    00222 /**
    00223 * Streaming operator that reads a Matrix2x2<V> from a stream.
    00224 * @param stream The stream from which is read.
    00225 * @param matrix2x2 The Matrix2x2<V> object.
    00226 * @return The stream.
    00227 */
    00228 template <class V> In& operator>>(In& stream, Matrix2x2<V>& matrix2x2);
    00229
    00230 /**
    00231 * Streaming operator that writes a Matrix2x2<V> to a stream.
    00232 * @param stream The stream to write on.
    00233 * @param matrix2x2 The Matrix2x2<V> object.
    00234 * @return The stream.
    00235 */
    00236 template <class V> Out& operator<<(Out& stream, const Matrix2x2<V>& matrix2x2);
    00237
    00238 //#endif // keis klasse nicht kompiliert
    00239
    00240 #endif // __Matrix2x2_h__