MeshKit  1.0
Mat4.h
Go to the documentation of this file.
00001 #ifndef GFXMATH_MAT4_INCLUDED // -*- C++ -*-
00002 #define GFXMATH_MAT4_INCLUDED
00003 
00004 /************************************************************************
00005 
00006   4x4 Matrix class
00007 
00008   $Id: Mat4.h,v 1.14 1997/06/18 15:55:22 garland Exp $
00009 
00010  ************************************************************************/
00011 
00012 #include "Vec3.h"
00013 #include "Vec4.h"
00014 
00015 class Mat4
00016 {
00017 private:
00018     Vec4 row[4];
00019 
00020 protected:
00021 
00022     inline void copy(const Mat4& m);
00023     inline Vec4 col(int i) const
00024         { return Vec4(row[0][i],row[1][i],row[2][i],row[3][i]); }
00025 
00026 public:
00027     // Standard matrices
00028     static Mat4 identity;
00029     static Mat4 zero;
00030     static Mat4 unit;
00031 
00032     static Mat4 trans(double,double,double);
00033     static Mat4 scale(double,double,double);
00034     static Mat4 xrot(double); //
00035     static Mat4 yrot(double); // Arguments are in radians
00036     static Mat4 zrot(double); //
00037 
00038 
00039     // Standard constructors
00040     Mat4() { copy(zero); }
00041     Mat4(const Vec4& r0,const Vec4& r1,const Vec4& r2,const Vec4& r3)
00042     { row[0]=r0; row[1]=r1; row[2]=r2; row[3]=r3; }
00043     Mat4(const Mat4& m) { copy(m); }
00044 
00045     // Access methods
00046     // M(i, j) == row i;col j
00047     double& operator()(int i, int j)       { return row[i][j]; }
00048     double  operator()(int i, int j) const { return row[i][j]; }
00049     const Vec4& operator[](int i) const { return row[i]; }
00050 
00051     // Comparison methods
00052     inline int operator==(const Mat4&);
00053 
00054     // Assignment methods
00055     inline Mat4& operator=(const Mat4& m) { copy(m); return *this; }
00056     inline Mat4& operator+=(const Mat4& m);
00057     inline Mat4& operator-=(const Mat4& m);
00058 
00059     inline Mat4& operator*=(double s);
00060     inline Mat4& operator/=(double s);
00061 
00062 
00063     // Arithmetic methods
00064     inline Mat4 operator+(const Mat4& m) const;
00065     inline Mat4 operator-(const Mat4& m) const;
00066     inline Mat4 operator-() const;
00067 
00068     inline Mat4 operator*(double s) const;
00069     inline Mat4 operator/(double s) const;
00070     Mat4 operator*(const Mat4& m) const;
00071 
00072     inline Vec4 operator*(const Vec4& v) const; // [x y z w]
00073     inline Vec3 operator*(const Vec3& v) const; // [x y z w]
00074 
00075     // Matrix operations
00076     double det() const;
00077     Mat4 transpose() const;
00078     Mat4 adjoint() const;
00079     double inverse(Mat4&) const;
00080     double cramerInverse(Mat4&) const;
00081 
00082     // Output methods
00083     friend std::ostream& operator<<(std::ostream&, const Mat4&);
00084 };
00085 
00086 
00087 
00088 inline void Mat4::copy(const Mat4& m)
00089 {
00090     row[0] = m.row[0]; row[1] = m.row[1];
00091     row[2] = m.row[2]; row[3] = m.row[3];
00092 }
00093 
00094 inline int Mat4::operator==(const Mat4& m)
00095 {
00096     return row[0]==m.row[0] &&
00097            row[1]==m.row[1] &&
00098            row[2]==m.row[2] &&
00099            row[3]==m.row[3] ;
00100 }
00101 
00102 inline Mat4& Mat4::operator+=(const Mat4& m)
00103 {
00104     row[0] += m.row[0]; row[1] += m.row[1];
00105     row[2] += m.row[2]; row[3] += m.row[3];
00106     return *this;
00107 }
00108 
00109 inline Mat4& Mat4::operator-=(const Mat4& m)
00110 {
00111     row[0] -= m.row[0]; row[1] -= m.row[1];
00112     row[2] -= m.row[2]; row[3] -= m.row[3];
00113     return *this;
00114 }
00115 
00116 inline Mat4& Mat4::operator*=(double s)
00117 {
00118     row[0] *= s; row[1] *= s; row[2] *= s; row[3] *= s;
00119     return *this;
00120 }
00121 
00122 inline Mat4& Mat4::operator/=(double s)
00123 {
00124     row[0] /= s; row[1] /= s; row[2] /= s; row[3] /= s;
00125     return *this;
00126 }
00127 
00128 inline Mat4 Mat4::operator+(const Mat4& m) const
00129 {
00130     return Mat4(row[0]+m.row[0],
00131                 row[1]+m.row[1],
00132                 row[2]+m.row[2],
00133                 row[3]+m.row[3]);
00134 }
00135 
00136 inline Mat4 Mat4::operator-(const Mat4& m) const
00137 {
00138     return Mat4(row[0]-m.row[0],
00139                 row[1]-m.row[1],
00140                 row[2]-m.row[2],
00141                 row[3]-m.row[3]);
00142 }
00143 
00144 inline Mat4 Mat4::operator-() const
00145 {
00146     return Mat4(-row[0], -row[1], -row[2], -row[3]);
00147 }
00148 
00149 inline Mat4 Mat4::operator*(double s) const
00150 {
00151     return Mat4(row[0]*s, row[1]*s, row[2]*s, row[3]*s);
00152 }
00153 
00154 inline Mat4 Mat4::operator/(double s) const
00155 {
00156     return Mat4(row[0]/s, row[1]/s, row[2]/s, row[3]/s);
00157 }
00158 
00159 inline Vec4 Mat4::operator*(const Vec4& v) const
00160 {
00161     return Vec4(row[0]*v, row[1]*v, row[2]*v, row[3]*v);
00162 }
00163 
00164 //
00165 // Transform a homogeneous 3-vector and reproject into normal 3-space
00166 //
00167 inline Vec3 Mat4::operator*(const Vec3& v) const
00168 {
00169     Vec4 u=Vec4(v,1);
00170     double w=row[3]*u;
00171 
00172     if(w==0.0)
00173         return Vec3(row[0]*u, row[1]*u, row[2]*u);
00174     else
00175         return Vec3(row[0]*u/w, row[1]*u/w, row[2]*u/w);
00176 }
00177 
00178 inline std::ostream& operator<<(std::ostream& out, const Mat4& M)
00179 {
00180     return out<<M.row[0]<<std::endl<<M.row[1]<<std::endl<<M.row[2]<<std::endl<<M.row[3];
00181 }
00182 
00183 
00184 // GFXMATH_MAT4_INCLUDED
00185 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines