Bart Janssens / SVL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Mat2.h Source File

Mat2.h

00001 /*
00002     File:           Mat2.h
00003 
00004     Function:       Defines a 2 x 2 matrix.
00005 
00006     Author(s):      Andrew Willmott
00007 
00008     Copyright:      (c) 1995-2001, Andrew Willmott
00009  */
00010 
00011 #ifndef __Mat2__
00012 #define __Mat2__
00013 
00014 #include "Vec2.h"
00015 
00016 
00017 // --- Mat2 Class -------------------------------------------------------------
00018 
00019 class Mat2
00020 {
00021 public:
00022 
00023     // Constructors
00024 
00025                 Mat2();
00026                 Mat2(Real a, Real b, Real c, Real d);   // Create from rows
00027                 Mat2(const Mat2 &m);                    // Copy constructor
00028                 Mat2(ZeroOrOne k);
00029                 Mat2(Block k);
00030 
00031     // Accessor functions
00032 
00033     Int         Rows() const { return(2); };
00034     Int         Cols() const { return(2); };
00035 
00036     Vec2        &operator [] (Int i);
00037     const Vec2  &operator [] (Int i) const;
00038 
00039     Real        *Ref() const;               // Return pointer to data
00040 
00041     // Assignment operators
00042 
00043     Mat2        &operator =  (const Mat2 &m);
00044     Mat2        &operator =  (ZeroOrOne k);
00045     Mat2        &operator =  (Block k);
00046     Mat2        &operator += (const Mat2 &m);
00047     Mat2        &operator -= (const Mat2 &m);
00048     Mat2        &operator *= (const Mat2 &m);
00049     Mat2        &operator *= (Real s);
00050     Mat2        &operator /= (Real s);
00051 
00052     // Comparison operators
00053 
00054     Bool        operator == (const Mat2 &m) const;  // M == N?
00055     Bool        operator != (const Mat2 &m) const;  // M != N?
00056 
00057     // Arithmetic operators
00058 
00059     Mat2        operator + (const Mat2 &m) const;   // M + N
00060     Mat2        operator - (const Mat2 &m) const;   // M - N
00061     Mat2        operator - () const;                // -M
00062     Mat2        operator * (const Mat2 &m) const;   // M * N
00063     Mat2        operator * (Real s) const;          // M * s
00064     Mat2        operator / (Real s) const;          // M / s
00065 
00066     // Initialisers
00067 
00068     Void        MakeZero();                         // Zero matrix
00069     Void        MakeDiag(Real k = vl_one);          // I
00070     Void        MakeBlock(Real k = vl_one);         // all elts=k
00071 
00072     // Vector Transformations
00073 
00074     Mat2&       MakeRot(Real theta);
00075     Mat2&       MakeScale(const Vec2 &s);
00076 
00077     // Private...
00078 
00079 protected:
00080 
00081     Vec2        row[2];     // Rows of the matrix
00082 };
00083 
00084 
00085 // --- Matrix operators -------------------------------------------------------
00086 
00087 inline Vec2     &operator *= (Vec2 &v, const Mat2 &m);      // v *= m
00088 inline Vec2     operator * (const Mat2 &m, const Vec2 &v);  // m * v
00089 inline Vec2     operator * (const Vec2 &v, const Mat2 &m);  // v * m
00090 inline Mat2     operator * (Real s, const Mat2 &m);         // s * m
00091 
00092 inline Mat2     trans(const Mat2 &m);               // Transpose
00093 inline Real     trace(const Mat2 &m);               // Trace
00094 inline Mat2     adj(const Mat2 &m);                 // Adjoint
00095 Real            det(const Mat2 &m);                 // Determinant
00096 Mat2            inv(const Mat2 &m);                 // Inverse
00097 Mat2            oprod(const Vec2 &a, const Vec2 &b);
00098                                                     // Outer product
00099 
00100 // The xform functions help avoid dependence on whether row or column
00101 // vectors are used to represent points and vectors.
00102 inline Vec2     xform(const Mat2 &m, const Vec2 &v); // Transform of v by m
00103 inline Mat2     xform(const Mat2 &m, const Mat2 &n); // xform v -> m(n(v))
00104 
00105 //std::ostream         &operator << (std::ostream &s, const Mat2 &m);
00106 //std::istream         &operator >> (std::istream &s, Mat2 &m);
00107 
00108 void printMat2(const Mat2 &m);
00109 
00110 
00111 // --- Inlines ----------------------------------------------------------------
00112 
00113 inline Vec2 &Mat2::operator [] (Int i)
00114 {
00115     CheckRange(i, 0, 2, "(Mat2::[i]) index out of range");
00116     return(row[i]);
00117 }
00118 
00119 inline const Vec2 &Mat2::operator [] (Int i) const
00120 {
00121     CheckRange(i, 0, 2, "(Mat2::[i]) index out of range");
00122     return(row[i]);
00123 }
00124 
00125 inline Real *Mat2::Ref() const
00126 {
00127     return((Real*) row);
00128 }
00129 
00130 inline Mat2::Mat2()
00131 {
00132 }
00133 
00134 inline Mat2::Mat2(Real a, Real b, Real c, Real d)
00135 {
00136     row[0][0] = a;  row[0][1] = b;
00137     row[1][0] = c;  row[1][1] = d;
00138 }
00139 
00140 inline Mat2::Mat2(const Mat2 &m)
00141 {
00142     row[0] = m[0];
00143     row[1] = m[1];
00144 }
00145 
00146 
00147 inline Void Mat2::MakeZero()
00148 {
00149     row[0][0] = vl_zero; row[0][1] = vl_zero;
00150     row[1][0] = vl_zero; row[1][1] = vl_zero;
00151 }
00152 
00153 inline Void Mat2::MakeDiag(Real k)
00154 {
00155     row[0][0] = k;          row[0][1] = vl_zero;
00156     row[1][0] = vl_zero;    row[1][1] = k;
00157 }
00158 
00159 inline Void Mat2::MakeBlock(Real k)
00160 {
00161     row[0][0] = k; row[0][1] = k;
00162     row[1][0] = k; row[1][1] = k;
00163 }
00164 
00165 inline Mat2::Mat2(ZeroOrOne k)
00166 {
00167     MakeDiag(k);
00168 }
00169 
00170 inline Mat2::Mat2(Block k)
00171 {
00172     MakeBlock((ZeroOrOne) k);
00173 }
00174 
00175 inline Mat2 &Mat2::operator = (ZeroOrOne k)
00176 {
00177     MakeDiag(k);
00178 
00179     return(SELF);
00180 }
00181 
00182 inline Mat2 &Mat2::operator = (Block k)
00183 {
00184     MakeBlock((ZeroOrOne) k);
00185 
00186     return(SELF);
00187 }
00188 
00189 inline Mat2 &Mat2::operator = (const Mat2 &m)
00190 {
00191     row[0] = m[0];
00192     row[1] = m[1];
00193 
00194     return(SELF);
00195 }
00196 
00197 inline Mat2 &Mat2::operator += (const Mat2 &m)
00198 {
00199     row[0] += m[0];
00200     row[1] += m[1];
00201 
00202     return(SELF);
00203 }
00204 
00205 inline Mat2 &Mat2::operator -= (const Mat2 &m)
00206 {
00207     row[0] -= m[0];
00208     row[1] -= m[1];
00209 
00210     return(SELF);
00211 }
00212 
00213 inline Mat2 &Mat2::operator *= (const Mat2 &m)
00214 {
00215     SELF = SELF * m;
00216 
00217     return(SELF);
00218 }
00219 
00220 inline Mat2 &Mat2::operator *= (Real s)
00221 {
00222     row[0] *= s;
00223     row[1] *= s;
00224 
00225     return(SELF);
00226 }
00227 
00228 inline Mat2 &Mat2::operator /= (Real s)
00229 {
00230     row[0] /= s;
00231     row[1] /= s;
00232 
00233     return(SELF);
00234 }
00235 
00236 
00237 inline Mat2 Mat2::operator + (const Mat2 &m) const
00238 {
00239     Mat2 result;
00240 
00241     result[0] = row[0] + m[0];
00242     result[1] = row[1] + m[1];
00243 
00244     return(result);
00245 }
00246 
00247 inline Mat2 Mat2::operator - (const Mat2 &m) const
00248 {
00249     Mat2 result;
00250 
00251     result[0] = row[0] - m[0];
00252     result[1] = row[1] - m[1];
00253 
00254     return(result);
00255 }
00256 
00257 inline Mat2 Mat2::operator - () const
00258 {
00259     Mat2 result;
00260 
00261     result[0] = -row[0];
00262     result[1] = -row[1];
00263 
00264     return(result);
00265 }
00266 
00267 inline Mat2 Mat2::operator * (const Mat2 &m) const
00268 {
00269 #define N(x,y) row[x][y]
00270 #define M(x,y) m.row[x][y]
00271 #define R(x,y) result[x][y]
00272 
00273     Mat2 result;
00274 
00275     R(0,0) = N(0,0) * M(0,0) + N(0,1) * M(1,0);
00276     R(0,1) = N(0,0) * M(0,1) + N(0,1) * M(1,1);
00277     R(1,0) = N(1,0) * M(0,0) + N(1,1) * M(1,0);
00278     R(1,1) = N(1,0) * M(0,1) + N(1,1) * M(1,1);
00279 
00280     return(result);
00281 
00282 #undef N
00283 #undef M
00284 #undef R
00285 }
00286 
00287 inline Mat2 Mat2::operator * (Real s) const
00288 {
00289     Mat2 result;
00290 
00291     result[0] = row[0] * s;
00292     result[1] = row[1] * s;
00293 
00294     return(result);
00295 }
00296 
00297 inline Mat2 Mat2::operator / (Real s) const
00298 {
00299     Mat2 result;
00300 
00301     result[0] = row[0] / s;
00302     result[1] = row[1] / s;
00303 
00304     return(result);
00305 }
00306 
00307 inline Mat2  operator *  (Real s, const Mat2 &m)
00308 {
00309     return(m * s);
00310 }
00311 
00312 inline Vec2 operator * (const Mat2 &m, const Vec2 &v)
00313 {
00314     Vec2 result;
00315 
00316     result[0] = m[0][0] * v[0] + m[0][1] * v[1];
00317     result[1] = m[1][0] * v[0] + m[1][1] * v[1];
00318 
00319     return(result);
00320 }
00321 
00322 inline Vec2 operator * (const Vec2 &v, const Mat2 &m)
00323 {
00324     Vec2 result;
00325 
00326     result[0] = v[0] * m[0][0] + v[1] * m[1][0];
00327     result[1] = v[0] * m[0][1] + v[1] * m[1][1];
00328 
00329     return(result);
00330 }
00331 
00332 inline Vec2 &operator *= (Vec2 &v, const Mat2 &m)
00333 {
00334     Real t;
00335 
00336     t    = v[0] * m[0][0] + v[1] * m[1][0];
00337     v[1] = v[0] * m[0][1] + v[1] * m[1][1];
00338     v[0] = t;
00339 
00340     return(v);
00341 }
00342 
00343 
00344 inline Mat2 trans(const Mat2 &m)
00345 {
00346     Mat2 result;
00347 
00348     result[0][0] = m[0][0]; result[0][1] = m[1][0];
00349     result[1][0] = m[0][1]; result[1][1] = m[1][1];
00350 
00351     return(result);
00352 }
00353 
00354 inline Real trace(const Mat2 &m)
00355 {
00356     return(m[0][0] + m[1][1]);
00357 }
00358 
00359 inline Mat2 adj(const Mat2 &m)
00360 {
00361     Mat2 result;
00362 
00363     result[0] =  cross(m[1]);
00364     result[1] = -cross(m[0]);
00365 
00366     return(result);
00367 }
00368 
00369 #ifdef VL_ROW_ORIENT
00370 inline Vec2 xform(const Mat2 &m, const Vec2 &v)
00371 { return(v * m); }
00372 inline Mat2 xform(const Mat2 &m, const Mat2 &n)
00373 { return(n * m); }
00374 #else
00375 inline Vec2 xform(const Mat2 &m, const Vec2 &v)
00376 { return(m * v); }
00377 inline Mat2 xform(const Mat2 &m, const Mat2 &n)
00378 { return(m * n); }
00379 #endif
00380 
00381 #endif