Bart Janssens / SVL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Mat3.h Source File

Mat3.h

00001 /*
00002     File:           Mat3.h
00003 
00004     Function:       Defines a 3 x 3 matrix.
00005 
00006     Author(s):      Andrew Willmott
00007 
00008     Copyright:      (c) 1995-2001, Andrew Willmott
00009 */
00010 
00011 #ifndef __Mat3__
00012 #define __Mat3__
00013 
00014 #include "Vec3.h"
00015 
00016 
00017 // --- Mat3 Class -------------------------------------------------------------
00018 
00019 
00020 class Vec4;
00021 
00022 class Mat3
00023 {
00024 public:
00025 
00026     // Constructors
00027 
00028                 Mat3();
00029                 Mat3(Real a, Real b, Real c,
00030                      Real d, Real e, Real f,
00031                      Real g, Real h, Real i);
00032                 Mat3(const Mat3 &m);
00033                 Mat3(ZeroOrOne k);
00034                 Mat3(Block k);
00035 
00036     // Accessor functions
00037 
00038     Int         Rows() const { return(3); };
00039     Int         Cols() const { return(3); };
00040 
00041     Vec3        &operator [] (Int i);
00042     const Vec3  &operator [] (Int i) const;
00043 
00044     Real        *Ref() const;               // Return pointer to data
00045 
00046     // Assignment operators
00047 
00048     Mat3        &operator =  (const Mat3 &m);
00049     Mat3        &operator =  (ZeroOrOne k);
00050     Mat3        &operator =  (Block k);
00051     Mat3        &operator += (const Mat3 &m);
00052     Mat3        &operator -= (const Mat3 &m);
00053     Mat3        &operator *= (const Mat3 &m);
00054     Mat3        &operator *= (Real s);
00055     Mat3        &operator /= (Real s);
00056 
00057     // Comparison operators
00058 
00059     Bool        operator == (const Mat3 &m) const;  // M == N?
00060     Bool        operator != (const Mat3 &m) const;  // M != N?
00061 
00062     // Arithmetic operators
00063 
00064     Mat3        operator + (const Mat3 &m) const;   // M + N
00065     Mat3        operator - (const Mat3 &m) const;   // M - N
00066     Mat3        operator - () const;                // -M
00067     Mat3        operator * (const Mat3 &m) const;   // M * N
00068     Mat3        operator * (Real s) const;          // M * s
00069     Mat3        operator / (Real s) const;          // M / s
00070 
00071     // Initialisers
00072 
00073     Void        MakeZero();                 // Zero matrix
00074     Void        MakeDiag(Real k = vl_one);  // I
00075     Void        MakeBlock(Real k = vl_one); // all elts = k
00076 
00077     // Vector Transforms
00078 
00079     Mat3&       MakeRot(const Vec3 &axis, Real theta);
00080     Mat3&       MakeRot(const Vec4 &q);     // Rotate by quaternion
00081     Mat3&       MakeScale(const Vec3 &s);
00082 
00083     // Homogeneous Transforms
00084 
00085     Mat3&       MakeHRot(Real theta);       // Rotate by theta rads
00086     Mat3&       MakeHScale(const Vec2 &s);  // Scale by s
00087     Mat3&       MakeHTrans(const Vec2 &t);  // Translation by t
00088 
00089     // Private...
00090 
00091 protected:
00092 
00093     Vec3        row[3];
00094 };
00095 
00096 
00097 // --- Matrix operators -------------------------------------------------------
00098 
00099 inline Vec3     &operator *= (Vec3 &v, const Mat3 &m);      // v *= m
00100 inline Vec3     operator * (const Mat3 &m, const Vec3 &v);  // m * v
00101 inline Vec3     operator * (const Vec3 &v, const Mat3 &m);  // v * m
00102 inline Mat3     operator * (const Real s, const Mat3 &m);   // s * m
00103 
00104 Mat3            trans(const Mat3 &m);                   // Transpose
00105 Real            trace(const Mat3 &m);                   // Trace
00106 Mat3            adj(const Mat3 &m);                     // Adjoint
00107 Real            det(const Mat3 &m);                     // Determinant
00108 Mat3            inv(const Mat3 &m);                     // Inverse
00109 Mat3            oprod(const Vec3 &a, const Vec3 &b);    // Outer product
00110 
00111 // The xform functions help avoid dependence on whether row or column
00112 // vectors are used to represent points and vectors.
00113 inline Vec3     xform(const Mat3 &m, const Vec3 &v); // Transform of v by m
00114 inline Vec2     xform(const Mat3 &m, const Vec2 &v); // Hom. xform of v by m
00115 inline Mat3     xform(const Mat3 &m, const Mat3 &n); // Xform v -> m(n(v))
00116 
00117 //std::ostream         &operator << (std::ostream &s, const Mat3 &m);
00118 //std::istream         &operator >> (std::istream &s, Mat3 &m);
00119 
00120 void printMat3(const Mat3 &m);
00121 
00122 
00123 // --- Inlines ----------------------------------------------------------------
00124 
00125 inline Mat3::Mat3()
00126 {
00127 }
00128 
00129 inline Vec3 &Mat3::operator [] (Int i)
00130 {
00131     CheckRange(i, 0, 3, "(Mat3::[i]) index out of range");
00132     return(row[i]);
00133 }
00134 
00135 inline const Vec3 &Mat3::operator [] (Int i) const
00136 {
00137     CheckRange(i, 0, 3, "(Mat3::[i]) index out of range");
00138     return(row[i]);
00139 }
00140 
00141 inline Real *Mat3::Ref() const
00142 {
00143     return((Real *) row);
00144 }
00145 
00146 inline Mat3::Mat3(ZeroOrOne k)
00147 {
00148     MakeDiag(k);
00149 }
00150 
00151 inline Mat3::Mat3(Block k)
00152 {
00153     MakeBlock((ZeroOrOne) k);
00154 }
00155 
00156 inline Mat3 &Mat3::operator = (ZeroOrOne k)
00157 {
00158     MakeDiag(k);
00159 
00160     return(SELF);
00161 }
00162 
00163 inline Mat3 &Mat3::operator = (Block k)
00164 {
00165     MakeBlock((ZeroOrOne) k);
00166 
00167     return(SELF);
00168 }
00169 
00170 inline Mat3 operator *  (const Real s, const Mat3 &m)
00171 {
00172     return(m * s);
00173 }
00174 
00175 inline Vec3 operator * (const Mat3 &m, const Vec3 &v)
00176 {
00177     Vec3 result;
00178 
00179     result[0] = v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2];
00180     result[1] = v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2];
00181     result[2] = v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2];
00182 
00183     return(result);
00184 }
00185 
00186 inline Vec3 operator * (const Vec3 &v, const Mat3 &m)
00187 {
00188     Vec3 result;
00189 
00190     result[0] = v[0] * m[0][0] + v[1] * m[1][0] + v[2] * m[2][0];
00191     result[1] = v[0] * m[0][1] + v[1] * m[1][1] + v[2] * m[2][1];
00192     result[2] = v[0] * m[0][2] + v[1] * m[1][2] + v[2] * m[2][2];
00193 
00194     return(result);
00195 }
00196 
00197 inline Vec3 &operator *= (Vec3 &v, const Mat3 &m)
00198 {
00199     Real t0, t1;
00200 
00201     t0   = v[0] * m[0][0] + v[1] * m[1][0] + v[2] * m[2][0];
00202     t1   = v[0] * m[0][1] + v[1] * m[1][1] + v[2] * m[2][1];
00203     v[2] = v[0] * m[0][2] + v[1] * m[1][2] + v[2] * m[2][2];
00204     v[0] = t0;
00205     v[1] = t1;
00206 
00207     return(v);
00208 }
00209 
00210 #ifdef VL_ROW_ORIENT
00211 inline Vec2 xform(const Mat3 &m, const Vec2 &v)
00212 { return(proj(Vec3(v, 1.0) * m)); }
00213 inline Vec3 xform(const Mat3 &m, const Vec3 &v)
00214 { return(v * m); }
00215 inline Mat3 xform(const Mat3 &m, const Mat3 &n)
00216 { return(n * m); }
00217 #else
00218 inline Vec2 xform(const Mat3 &m, const Vec2 &v)
00219 { return(proj(m * Vec3(v, 1.0))); }
00220 inline Vec3 xform(const Mat3 &m, const Vec3 &v)
00221 { return(m * v); }
00222 inline Mat3 xform(const Mat3 &m, const Mat3 &n)
00223 { return(m * n); }
00224 #endif
00225 
00226 #endif