fart/util/Matrix.h
Josh Holtrop ad6ef8f315 moved some Matrix and Transform functionality into header files
git-svn-id: svn://anubis/fart/trunk@310 7f9b0f55-74a9-4bce-be96-3c2cd072584d
2010-09-28 18:18:42 +00:00

76 lines
2.0 KiB
C++

#ifndef MATRIX_H
#define MATRIX_H MATRIX_H
#include "Vector.h"
#include <iostream>
class Matrix
{
public:
Matrix();
typedef double Matrix_row_t[4];
Matrix_row_t & operator[](int idx) { return m_matrix[idx]; }
const Matrix_row_t & operator[](int idx) const { return m_matrix[idx]; }
static Matrix identity();
double determinant();
Matrix getInverse();
Matrix & operator*=(const Matrix & other);
Matrix operator*(const Matrix & m2) const
{
Matrix res;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
res[i][j] = m_matrix[i][0] * m2.m_matrix[0][j]
+ m_matrix[i][1] * m2.m_matrix[1][j]
+ m_matrix[i][2] * m2.m_matrix[2][j]
+ m_matrix[i][3] * m2.m_matrix[3][j];
}
}
return res;
}
/* transform a point */
Vector operator*(const Vector & v)
{
Vector res;
for (int i = 0; i < 3; i++)
{
res[i] = m_matrix[i][0] * v[0]
+ m_matrix[i][1] * v[1]
+ m_matrix[i][2] * v[2]
+ m_matrix[i][3]; /* v[3] is implicitly 1.0 */
}
return res;
}
/* transform a direction */
Vector operator%(const Vector & v)
{
Vector res;
for (int i = 0; i < 3; i++)
{
res[i] = m_matrix[i][0] * v[0]
+ m_matrix[i][1] * v[1]
+ m_matrix[i][2] * v[2];
}
return res;
}
protected:
double m_matrix[4][4];
double m_inverse[4][4];
bool m_inverse_calculated;
bool m_inverse_valid;
void calculateInverse();
};
bool operator==(const Matrix & m1, const Matrix & m2);
std::ostream & operator<<(std::ostream & out, const Matrix & m);
#endif