Bart Janssens / SVL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Mat2.cpp Source File

Mat2.cpp

00001 /*
00002     File:           Mat2.cpp
00003 
00004     Function:       Implements Mat2.h
00005 
00006     Author(s):      Andrew Willmott
00007 
00008     Copyright:      (c) 1995-2001, Andrew Willmott
00009 
00010 */
00011 
00012 #include "Mat2.h"
00013 //#include <cctype>
00014 //#include <iomanip>
00015 #include "Utils.h"
00016 
00017 
00018 Bool Mat2::operator == (const Mat2 &m) const
00019 {
00020     return(row[0] == m[0] && row[1] == m[1]);
00021 }
00022 
00023 Bool Mat2::operator != (const Mat2 &m) const
00024 {
00025     return(row[0] != m[0] || row[1] != m[1]);
00026 }
00027 
00028 
00029 Real det(const Mat2 &m)
00030 {
00031     return(dot(m[0], cross(m[1])));
00032 }
00033 
00034 Mat2 inv(const Mat2 &m)
00035 {
00036     Real            mDet;
00037     Mat2            result;
00038 
00039     result[0][0] =  m[1][1]; result[0][1] = -m[0][1];
00040     result[1][0] = -m[1][0]; result[1][1] =  m[0][0];
00041 
00042     mDet = m[0][0] * result[0][0] + m[0][1] * result[1][0];
00043     Assert(mDet != 0.0, "(Mat2::inv) matrix is non-singular");
00044     result /= mDet;
00045 
00046     return(result);
00047 }
00048 
00049 Mat2 oprod(const Vec2 &a, const Vec2 &b)
00050 // returns outerproduct of a and b:  a * trans(b)
00051 {
00052     Mat2    result;
00053 
00054     result[0] = a[0] * b;
00055     result[1] = a[1] * b;
00056 
00057     return(result);
00058 }
00059 
00060 void printMat2(const Mat2 &m)
00061 {
00062     printf("[");
00063     printVec2(m[0]);
00064     printf("\r\n");
00065     printVec2(m[1]);
00066     printf("]\r\n");
00067 }
00068 
00069 /*
00070 ostream &operator << (ostream &s, const Mat2 &m)
00071 {
00072     Int w = s.width();
00073 
00074     return(s << '[' << m[0] << "\r\n" << setw(w) << m[1] << ']' << "\r\n");
00075 }
00076 
00077 istream &operator >> (istream &s, Mat2 &m)
00078 {
00079     Mat2    result;
00080     Char    c;
00081 
00082     // Expected format: [[1 2] [3 4]]
00083     // Each vector is a row of the row matrix.
00084 
00085     while (s >> c && isspace(c))        // ignore leading white space
00086         ;
00087 
00088     if (c == '[')
00089     {
00090         s >> result[0] >> result[1];
00091 
00092         if (!s)
00093         {
00094             cerr << "Expected number while reading matrix\n";
00095             return(s);
00096         }
00097 
00098         while (s >> c && isspace(c))
00099             ;
00100 
00101         if (c != ']')
00102         {
00103             s.clear(ios::failbit);
00104             cerr << "Expected ']' while reading matrix\n";
00105             return(s);
00106         }
00107     }
00108     else
00109     {
00110         s.clear(ios::failbit);
00111         cerr << "Expected '[' while reading matrix\n";
00112         return(s);
00113     }
00114 
00115     m = result;
00116     return(s);
00117 }
00118 */
00119 
00120 
00121 Mat2 &Mat2::MakeRot(Real theta)
00122 {
00123     Real    c, s;
00124 
00125     SetReal(s, sin(theta));
00126     SetReal(c, cos(theta));
00127 
00128 #ifdef VL_ROW_ORIENT
00129     row[0][0] =  c; row[0][1] = s;
00130     row[1][0] = -s; row[1][1] = c;
00131 #else
00132     row[0][0] = c; row[0][1] = -s;
00133     row[1][0] = s; row[1][1] =  c;
00134 #endif
00135 
00136     return(SELF);
00137 }
00138 
00139 Mat2 &Mat2::MakeScale(const Vec2 &s)
00140 {
00141     row[0][0] = s[0]; row[0][1] = vl_0;
00142     row[1][0] = vl_0; row[1][1] = s[1];
00143 
00144     return(SELF);
00145 }
00146