Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Mat3.h

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

Generated at Sat Aug 5 00:16:47 2000 for Class Library by doxygen 1.1.0 written by Dimitri van Heesch, © 1997-2000