From 2adb782b24f76160c9aedcc52cc8dce93b3dbe52 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 20 Jan 2009 04:02:10 +0000 Subject: [PATCH] updated Makefile, tests.cc, util/Matrix git-svn-id: svn://anubis/fart/trunk@16 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- Makefile | 1 + test/tests.cc | 21 +++++++++++++++++++++ util/Matrix.cc | 24 +++++++++++++++++++++++- util/Matrix.h | 1 + 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d68ff37..506dedb 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ $(TARGET): make -C main $(CXX) -o $@ main/*.o util/*.o shapes/*.o $(CXXFLAGS) $(LDFLAGS) +.PHONY: tests tests: make -C test $(CXX) -o $@ test/*.o util/*.o shapes/*.o $(CXXFLAGS) $(LDFLAGS) diff --git a/test/tests.cc b/test/tests.cc index ed070d3..f7ec3bb 100644 --- a/test/tests.cc +++ b/test/tests.cc @@ -1,8 +1,29 @@ #include #include +#include "util/Matrix.h" using namespace std; int main() { + Matrix m; + int v = 1; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + m[i][j] = v++; + } + } + + cout << "m:" << endl << m; + + Matrix i = m.getInverse(); + + cout << "i:" << endl << i; + + Matrix mult = m * i; + cout << "mult:" << endl << mult; + + return 0; } diff --git a/util/Matrix.cc b/util/Matrix.cc index aaa0d98..1303870 100644 --- a/util/Matrix.cc +++ b/util/Matrix.cc @@ -59,6 +59,8 @@ double Matrix::determinant() void Matrix::calculateInverse() { + if (m_inverse_calculated) + return; m_inverse_calculated = true; m_inverse_valid = false; double det = determinant(); @@ -170,6 +172,23 @@ void Matrix::calculateInverse() m_inverse_valid = true; } +Matrix Matrix::getInverse() +{ + calculateInverse(); + Matrix m; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + m.m_matrix[i][j] = m_inverse[i][j]; + m.m_inverse[i][j] = m_matrix[i][j]; + } + } + m.m_inverse_calculated = true; + m.m_inverse_valid = m_inverse_valid; + return m; +} + Matrix operator*(const Matrix & m1, const Matrix & m2) { Matrix res; @@ -225,7 +244,10 @@ std::ostream & operator<<(std::ostream & out, const Matrix & m) if (j < 3) out << ", "; } - out << "]" << std::endl; + out << "]"; + if (i == 3) + out << " ]"; + out << std::endl; } return out; } diff --git a/util/Matrix.h b/util/Matrix.h index 5b42019..964aea2 100644 --- a/util/Matrix.h +++ b/util/Matrix.h @@ -14,6 +14,7 @@ class Matrix const Matrix_row_t & operator[](int idx) const { return m_matrix[idx]; } static Matrix identity(); double determinant(); + Matrix getInverse(); protected: double m_matrix[4][4];