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

Mat4.h

Go to the documentation of this file.
00001 /*
00002     File:           Mat4.h
00003 
00004     Function:       Defines a 4 x 4 matrix.
00005                     
00006     Author(s):      Andrew Willmott
00007 
00008     Copyright:      (c) 1995-2000, Andrew Willmott
00009  */
00010 
00011 #ifndef __Mat4__
00012 #define __Mat4__
00013 
00014 #include "vl/VL.h"
00015 // Defines the actual type for TMat4 etc.
00016 #include "vl/Vec4.h"
00017 
00018 
00019 // --- Mat4 Class -------------------------------------------------------------
00020 
00021 class TVec3;
00022 
00023 class TMat4 
00024 {
00025 public:
00026     
00027     // Constructors
00028     
00029     inline      TMat4();
00030                 TMat4(TMReal a, TMReal b, TMReal c, TMReal d,
00031                     TMReal e, TMReal f, TMReal g, TMReal h,
00032                     TMReal i, TMReal j, TMReal k, TMReal l,
00033                     TMReal m, TMReal n, TMReal o, TMReal p);
00034                 TMat4(const TMat4 &m);                  // Copy constructor
00035                 TMat4(ZeroOrOne k);
00036                 TMat4(Block k);
00037                     
00038     // Accessor functions   
00039     inline Int          Rows() const { return(4); };
00040     inline Int          Cols() const { return(4); };
00041     
00042     inline TMVec4       &operator [] (Int i);          
00043     inline const TMVec4 &operator [] (Int i) const; 
00044 
00045     inline TMReal       *Ref() const;           // Return pointer to data
00046 
00047     // Assignment operators 
00048     TMat4               &operator =  (const TMat4 &m);     
00049     inline TMat4        &operator =  (ZeroOrOne k);    
00050     inline TMat4        &operator =  (Block k);    
00051     TMat4               &operator += (const TMat4 &m);  
00052     TMat4               &operator -= (const TMat4 &m);      
00053     TMat4               &operator *= (const TMat4 &m);      
00054     TMat4               &operator *= (TMReal s);                    
00055     TMat4               &operator /= (TMReal s);                
00056 
00057     // Comparison operators 
00058     Bool                operator == (const TMat4 &m) const; // M == N?
00059     Bool                operator != (const TMat4 &m) const; // M != N?
00060     
00061     // Arithmetic operators 
00062     TMat4               operator + (const TMat4 &m) const;  // M + N
00063     TMat4               operator - (const TMat4 &m) const;  // M - N
00064     TMat4               operator - () const;                // -M
00065     TMat4               operator * (const TMat4 &m) const;  // M * N
00066     TMat4               operator * (TMReal s) const;        // M * s
00067     TMat4               operator / (TMReal s) const;        // M / s
00068 
00069     // Initialisers 
00070     Void        MakeZero();                                 // Zero matrix
00071     Void        MakeDiag(TMReal k = vl_one);                // I
00072     Void        MakeBlock(TMReal k = vl_one);               // all elts = k
00073 
00074     // Homogeneous Transforms   
00075     TMat4&      MakeHRot(const TMVec3 &axis, Real theta);       
00076                                     // Rotate by theta radians about axis   
00077     TMat4&      MakeHRot(const TQuaternion &q); // Rotate by quaternion   
00078     TMat4&      MakeHScale(const TMVec3 &s);    // Scale by components of s
00079 
00080     TMat4&      MakeHTrans(const TMVec3 &t);    // Translation by t
00081 
00082     TMat4&      Transpose();                    // transpose in place
00083     TMat4&      AddShift(const TMVec3 &t);      // concat shift
00084     
00085     // Private...   
00086 protected:  
00087 
00088     TMVec4  row[4]; 
00089 };
00090 
00091 
00092 // --- Matrix operators -------------------------------------------------------
00093 
00094 TMVec4          operator * (const TMat4 &m, const TMVec4 &v);   // m * v
00095 TMVec4          operator * (const TMVec4 &v, const TMat4 &m);   // v * m
00096 TMVec4          &operator *= (TMVec4 &a, const TMat4 &m);       // v *= m
00097 inline TMat4    operator * (TMReal s, const TMat4 &m);          // s * m
00098 
00099 TMat4           trans(const TMat4 &m);              // Transpose            
00100 TMReal          trace(const TMat4 &m);              // Trace
00101 TMat4           adj(const TMat4 &m);                // Adjoint
00102 TMReal          det(const TMat4 &m);                // Determinant
00103 TMat4           inv(const TMat4 &m);                // Inverse
00104 TMat4           oprod(const TMVec4 &a, const TMVec4 &b);
00105                                                     // Outer product
00106 
00107 // The xform functions help avoid dependence on whether row or column
00108 // vectors are used to represent points and vectors.
00109 inline TVec4    xform(const TMat4 &m, const TVec4 &v); // Transform of v by m
00110 inline TVec3    xform(const TMat4 &m, const TVec3 &v); // Hom. xform of v by m
00111 inline TMat4    xform(const TMat4 &m, const TMat4 &n); // Xform v -> m(n(v))
00112 
00113 ostream         &operator << (ostream &s, const TMat4 &m);
00114 istream         &operator >> (istream &s, TMat4 &m);
00115     
00116 
00117 // --- Inlines ----------------------------------------------------------------
00118 
00119 inline TMat4::TMat4()
00120 {
00121 }
00122 
00123 inline TMVec4 &TMat4::operator [] (Int i)
00124 {
00125     CheckRange(i, 0, 4, "(Mat4::[i]) index out of range");
00126     return(row[i]);
00127 }
00128 
00129 inline const TMVec4 &TMat4::operator [] (Int i) const
00130 {
00131     CheckRange(i, 0, 4, "(Mat4::[i]) index out of range");
00132     return(row[i]);
00133 }
00134 
00135 inline TMReal *TMat4::Ref() const
00136 {
00137     return((TMReal *) row);
00138 }
00139 
00140 inline TMat4::TMat4(ZeroOrOne k)
00141 {
00142     MakeDiag(k);
00143 }
00144 
00145 inline TMat4::TMat4(Block k)
00146 {
00147     MakeBlock((ZeroOrOne) k);
00148 }
00149 
00150 inline TMat4 &TMat4::operator = (ZeroOrOne k)
00151 {
00152     MakeDiag(k);
00153 
00154     return(SELF);
00155 }
00156 
00157 inline TMat4 &TMat4::operator = (Block k)
00158 {
00159     MakeBlock((ZeroOrOne) k);
00160 
00161     return(SELF);
00162 }
00163 
00164 inline TMat4 operator * (TMReal s, const TMat4 &m)
00165 {
00166     return(m * s);
00167 }
00168 
00169 #ifdef VL_ROW_ORIENT
00170 inline TVec3 xform(const TMat4 &m, const TVec3 &v)
00171 { return(proj(TVec4(v, 1.0) * m)); }
00172 inline TVec4 xform(const TMat4 &m, const TVec4 &v)
00173 { return(v * m); }
00174 inline TMat4 xform(const TMat4 &m, const TMat4 &n)
00175 { return(n * m); }
00176 #else
00177 inline TVec3 xform(const TMat4 &m, const TVec3 &v)
00178 { return(proj(m * TVec4(v, 1.0))); }
00179 inline TVec4 xform(const TMat4 &m, const TVec4 &v)
00180 { return(m * v); }
00181 inline TMat4 xform(const TMat4 &m, const TMat4 &n)
00182 { return(m * n); }
00183 #endif
00184 
00185 #endif

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