/*************************************************************************************** ***** CLASS MATRIX4D HEADER ***** **************************************************************************************** ** Author: Peter Stuart ** Email: chester@selway.umt.edu ** Date: 3/7/98 ** Description: This class implements a 4x4 matrix and all of the operation you could ** ever want with one (well, maybe not ALL). Operations include: matrix cross product, ** minor, determinant, cofactor, transpose, inverse matrix, and loads of operators ** for multiplying matrices by other base types. Also part of the class are methods ** for creating transformation matrices, such as translation, rotation, scaling, and ** 3D shearing. ** ** NOTE: The matrices, vectors, and points use homogeneous coordinates ** ***************************************************************************************/ #include #include "Vector.hpp" #include "Point.hpp" #ifndef __GGMATRIX4D__ #define __GGMATRIX4D__ #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE !FALSE #endif class Matrix4D { private: /* Private Variables: matrix[4][4]: 4x4 array that holds the values */ double matrix[4][4]; // 3x3 matrix determinant for use with the other methods double det3x3(double mat[3][3]) const { return (mat[0][0]*mat[1][1]*mat[2][2]) + (mat[0][1]*mat[1][2]*mat[2][0]) + (mat[0][2]*mat[1][0]*mat[2][1]) - (mat[2][0]*mat[1][1]*mat[0][2]) - (mat[2][1]*mat[1][2]*mat[0][0]) - (mat[2][2]*mat[1][0]*mat[0][1]); } public: // inlined identity matrix creation Matrix4D identity() { Matrix4D temp; // returning matrix temp.matrix[0][0] = 1.0; temp.matrix[1][1] = 1.0; temp.matrix[2][2] = 1.0; temp.matrix[3][3] = 1.0; return temp; }; // Constructors, Copy Constructors, and Destructor Matrix4D(); Matrix4D(const double[4][4]); Matrix4D(const Matrix4D&); virtual ~Matrix4D(); // basic matrix operations Matrix4D cross(const Matrix4D&) const; double minor(const int, const int) const; double determinant(); Matrix4D cofactor(); Matrix4D transpose(); Matrix4D inverse(); // transformation matrix creation friend Matrix4D translate(const double, const double, const double); friend Matrix4D rotate(const double, const double, const double); friend Matrix4D scale(const double, const double, const double); friend Matrix4D shearXY(const double, const double); friend Matrix4D shearXZ(const double, const double); friend Matrix4D shearYZ(const double, const double); // Assignment operator, arithmetic operators, relational operators, and output stream Matrix4D& operator = (const Matrix4D&); friend Matrix4D operator + (const Matrix4D&, const Matrix4D&); friend Matrix4D operator - (const Matrix4D&, const Matrix4D&); friend Matrix4D operator * (const Matrix4D&, const Matrix4D&); friend Matrix4D operator * (const double, const Matrix4D&); friend Matrix4D operator * (const Matrix4D&, const double); friend Vector operator * (const Matrix4D&, const Vector&); friend Vector operator * (const Vector&, const Matrix4D&); friend Point operator * (const Matrix4D&, const Point&); friend Point operator * (const Point&, const Matrix4D&); friend Matrix4D operator / (const Matrix4D&, const double); Matrix4D& operator += (const Matrix4D&); Matrix4D& operator -= (const Matrix4D&); Matrix4D& operator *= (const double); Matrix4D& operator /= (const double); friend int operator == (const Matrix4D&, const Matrix4D&); friend int operator != (const Matrix4D&, const Matrix4D&); friend ostream& operator << (ostream&, const Matrix4D&); // variable access void setElement(const int row, const int col, const double val) { if (row < 4 && row >= 0 && col < 4 && col >=0) matrix[row][col] = val; }; double getElement(const int row, const int col) const { return matrix[row][col]; }; }; const Matrix4D zeroMatrix; #endif;