Bart Janssens / SVL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Mat4.h Source File

Mat4.h

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