00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef __UT_Matrix2_h__
00016 #define __UT_Matrix2_h__
00017
00018 #include <iostream.h>
00019 #include <SYS/SYS_Math.h>
00020 #include "UT_Assert.h"
00021
00022 class UT_IStream;
00023 class UT_Vector2;
00024 class UT_Matrix3;
00025
00026 template <typename T> class UT_TMatrix2;
00027
00028 typedef UT_TMatrix2<float> UT_Matrix2;
00029 typedef UT_TMatrix2<double> UT_DMatrix2;
00030
00031
00032 template <typename T>
00033 UT_TMatrix2<T> operator+(const UT_TMatrix2<T> &m1, const UT_TMatrix2<T> &m2);
00034 template <typename T>
00035 UT_TMatrix2<T> operator+(const UT_TMatrix2<T> &m, const UT_Vector2 &v);
00036 template <typename T>
00037 UT_TMatrix2<T> operator+(const UT_Vector2 &v, const UT_TMatrix2<T> &m);
00038 template <typename T>
00039 UT_TMatrix2<T> operator+(const UT_TMatrix2<T> &mat, T sc);
00040 template <typename T>
00041 UT_TMatrix2<T> operator+(T sc, const UT_TMatrix2<T> &mat);
00042
00043 template <typename T>
00044 UT_TMatrix2<T> operator-(const UT_TMatrix2<T> &m1, const UT_TMatrix2<T> &m2);
00045 template <typename T>
00046 UT_TMatrix2<T> operator-(const UT_TMatrix2<T> &m, const UT_Vector2 &v);
00047 template <typename T>
00048 UT_TMatrix2<T> operator-(const UT_Vector2 &v, const UT_TMatrix2<T> &m);
00049 template <typename T>
00050 UT_TMatrix2<T> operator-(const UT_TMatrix2<T> &mat, T sc);
00051 template <typename T>
00052 UT_TMatrix2<T> operator-(T sc, const UT_TMatrix2<T> &mat);
00053
00054 template <typename T>
00055 UT_TMatrix2<T> operator*(const UT_TMatrix2<T> &m1, const UT_TMatrix2<T> &m2);
00056 template <typename T>
00057 UT_TMatrix2<T> operator*(const UT_TMatrix2<T> &mat, T sc);
00058 template <typename T>
00059 UT_TMatrix2<T> operator*(T sc, const UT_TMatrix2<T> &mat);
00060
00061 template <typename T>
00062 UT_TMatrix2<T> operator/(const UT_TMatrix2<T> &mat, T sc);
00063 template <typename T>
00064 UT_TMatrix2<T> operator/(T sc, const UT_TMatrix2<T> &mat);
00065
00066
00067
00068
00069
00070 template <typename T> class UT_TMatrix2
00071 {
00072 public:
00073
00074 UT_TMatrix2()
00075 {
00076 UT_ASSERT_COMPILETIME(sizeof(UT_TMatrix2<T>) == 4 * sizeof(T));
00077 }
00078
00079 explicit UT_TMatrix2(T val)
00080 {
00081 matx[0][0] = val; matx[0][1] = 0;
00082 matx[1][0] = 0; matx[1][1] = val;
00083 }
00084
00085
00086 explicit UT_TMatrix2(const fpreal32 m[2][2])
00087 {
00088 matx[0][0]=m[0][0]; matx[0][1]=m[0][1];
00089 matx[1][0]=m[1][0]; matx[1][1]=m[1][1];
00090 }
00091 explicit UT_TMatrix2(const fpreal64 m[2][2])
00092 {
00093 matx[0][0]=m[0][0]; matx[0][1]=m[0][1];
00094 matx[1][0]=m[1][0]; matx[1][1]=m[1][1];
00095 }
00096
00097
00098
00099
00100 UT_TMatrix2(T val00, T val01,
00101 T val10, T val11)
00102 {
00103 matx[0][0] = val00; matx[0][1] = val01;
00104 matx[1][0] = val10; matx[1][1] = val11;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 UT_TMatrix2<T> &operator=(const UT_Matrix3 &m);
00116
00117 UT_TMatrix2<T> &operator+=(const UT_TMatrix2<T> &m)
00118 {
00119 matx[0][0]+=m.matx[0][0]; matx[0][1]+=m.matx[0][1];
00120 matx[1][0]+=m.matx[1][0]; matx[1][1]+=m.matx[1][1];
00121 return *this;
00122 }
00123 UT_TMatrix2<T> &operator-=(const UT_TMatrix2<T> &m)
00124 {
00125 matx[0][0]-=m.matx[0][0]; matx[0][1]-=m.matx[0][1];
00126 matx[1][0]-=m.matx[1][0]; matx[1][1]-=m.matx[1][1];
00127 return *this;
00128 }
00129 UT_TMatrix2<T> &operator*=(const UT_TMatrix2<T> &m);
00130
00131 unsigned operator==(const UT_TMatrix2<T> &m) const;
00132 unsigned operator!=(const UT_TMatrix2<T> &m) const
00133 {
00134 return !(*this == m);
00135 }
00136
00137 UT_TMatrix2<T> &operator=(T val)
00138 {
00139 matx[0][0] = val; matx[0][1] = 0;
00140 matx[1][0] = 0; matx[1][1] = val;
00141 return *this;
00142 }
00143 UT_TMatrix2<T> &operator+=(T scalar)
00144 {
00145 matx[0][0]+=scalar; matx[0][1]+=scalar;
00146 matx[1][0]+=scalar; matx[1][1]+=scalar;
00147 return *this;
00148 }
00149 UT_TMatrix2<T> &operator-=(T scalar)
00150 {
00151 return operator+=(-scalar);
00152 }
00153 UT_TMatrix2<T> &operator*=(T scalar)
00154 {
00155 matx[0][0]*=scalar; matx[0][1]*=scalar;
00156 matx[1][0]*=scalar; matx[1][1]*=scalar;
00157 return *this;
00158 }
00159 UT_TMatrix2<T> &operator/=(T scalar)
00160 {
00161 return operator*=( 1.0f/scalar );
00162 }
00163
00164
00165 inline
00166 UT_TMatrix2<T> &operator=(const UT_Vector2 &vec);
00167 inline
00168 UT_TMatrix2<T> &operator+=(const UT_Vector2 &vec);
00169 inline
00170 UT_TMatrix2<T> &operator-=(const UT_Vector2 &vec);
00171
00172 T determinant() const
00173 {
00174 return matx[0][0]*matx[1][1] - matx[0][1]*matx[1][0];
00175 }
00176 T trace() const
00177 { return matx[0][0] + matx[1][1]; }
00178
00179
00180 int eigenvalues(UT_Vector2 &r, UT_Vector2 &i) const;
00181
00182
00183 int invert();
00184
00185
00186 int invert(UT_TMatrix2<T> &m) const;
00187
00188
00189 T tolerance() const;
00190
00191
00192
00193
00194 int solve(const UT_Vector2 &b, UT_Vector2 &x) const;
00195
00196
00197 void transpose(void)
00198 {
00199 T tmp;
00200 tmp=matx[0][1]; matx[0][1]=matx[1][0]; matx[1][0]=tmp;
00201 }
00202 UT_TMatrix2<T> transpose(void) const;
00203
00204
00205 void identity()
00206 {
00207 matx[0][0]=(T)1;matx[0][1]=(T)0;
00208 matx[1][0]=(T)0;matx[1][1]=(T)1;
00209 }
00210
00211
00212 void initialize()
00213 {
00214 matx[0][0]=matx[0][1]= (T)0;
00215 matx[1][0]=matx[1][1]= (T)0;
00216 }
00217
00218
00219
00220 T &operator()(unsigned row, unsigned col)
00221 {
00222 UT_ASSERT_P(row < 2 && col < 2);
00223 return matx[row][col];
00224 }
00225 T operator()(unsigned row, unsigned col) const
00226 {
00227 UT_ASSERT_P(row < 2 && col < 2);
00228 return matx[row][col];
00229 }
00230
00231
00232
00233
00234 T *operator()(unsigned row)
00235 {
00236 UT_ASSERT_P(row < 2);
00237 return matx[row];
00238 }
00239 const T *operator()(unsigned row) const
00240 {
00241 UT_ASSERT_P(row < 2);
00242 return matx[row];
00243 }
00244 inline
00245 UT_Vector2 operator[](unsigned row) const;
00246
00247
00248
00249
00250 T getEuclideanNorm() const
00251 { return SYSsqrt(getEuclideanNorm2()); }
00252
00253 T getEuclideanNorm2() const;
00254
00255
00256 int save(ostream &os, int binary) const;
00257 bool load(UT_IStream &is);
00258
00259 void outAsciiNoName(ostream &os) const;
00260
00261
00262 friend ostream &operator<<(ostream &os, const UT_TMatrix2<T> &v)
00263 {
00264 os << className() << ' ';
00265 v.outAsciiNoName(os);
00266 return os;
00267 }
00268 private:
00269 static const char *className(void);
00270
00271 T matx[2][2];
00272 };
00273
00274 #include "UT_Vector2.h"
00275
00276 template <>
00277 inline
00278 float UT_TMatrix2<float>::tolerance() const
00279 {
00280 return 1e-5f;
00281 }
00282
00283 template <>
00284 inline
00285 double UT_TMatrix2<double>::tolerance() const
00286 {
00287 return 1e-11;
00288 }
00289
00290 template <typename T>
00291 inline
00292 UT_TMatrix2<T> &UT_TMatrix2<T>::operator=(const UT_Vector2 &vec)
00293 {
00294 matx[0][0] = matx[0][1] = vec.x();
00295 matx[1][0] = matx[1][1] = vec.y();
00296 return *this;
00297 }
00298
00299 template <typename T>
00300 inline
00301 UT_TMatrix2<T> &UT_TMatrix2<T>::operator+=(const UT_Vector2 &vec)
00302 {
00303 matx[0][0]+=vec.x(); matx[0][1]+=vec.x();
00304 matx[1][0]+=vec.y(); matx[1][1]+=vec.y();
00305 return *this;
00306 }
00307
00308 template <typename T>
00309 inline
00310 UT_TMatrix2<T> &UT_TMatrix2<T>::operator-=(const UT_Vector2 &vec)
00311 {
00312 matx[0][0]-=vec.x(); matx[0][1]-=vec.x();
00313 matx[1][0]-=vec.y(); matx[1][1]-=vec.y();
00314 return *this;
00315 }
00316
00317 template <typename T>
00318 inline
00319 UT_Vector2 UT_TMatrix2<T>::operator[](unsigned row) const
00320 {
00321 UT_ASSERT_P(row < 2);
00322 return UT_Vector2(matx[row]);
00323 }
00324
00325
00326
00327 template <typename T>
00328 inline
00329 UT_TMatrix2<T> operator+(const UT_Vector2 &vec, const UT_TMatrix2<T> &mat)
00330 {
00331 return mat+vec;
00332 }
00333
00334 template <typename T>
00335 inline
00336 UT_TMatrix2<T> operator+(T sc, const UT_TMatrix2<T> &m1)
00337 {
00338 return m1+sc;
00339 }
00340
00341 template <typename T>
00342 inline
00343 UT_TMatrix2<T> operator-(const UT_TMatrix2<T> &m1, T sc)
00344 {
00345 return m1+(-sc);
00346 }
00347
00348 template <typename T>
00349 inline
00350 UT_TMatrix2<T> operator*(T sc, const UT_TMatrix2<T> &m1)
00351 {
00352 return m1*sc;
00353 }
00354
00355 template <typename T>
00356 inline
00357 UT_TMatrix2<T> operator/(const UT_TMatrix2<T> &m1, T scalar)
00358 {
00359 return m1 * (1.0f/scalar);
00360 }
00361
00362 #if defined( WIN32 ) || defined( LINUX ) || defined(MBSD) || defined(GAMEOS)
00363 #include "UT_Matrix2.C"
00364 #endif
00365
00366 #endif