From ad6ef8f315bd66e3087627cf4bb789479427b0e0 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 28 Sep 2010 18:18:42 +0000 Subject: [PATCH] moved some Matrix and Transform functionality into header files git-svn-id: svn://anubis/fart/trunk@310 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- util/Matrix.cc | 43 ------------------------------------------- util/Matrix.h | 46 +++++++++++++++++++++++++++++++++++++++++++--- util/Transform.cc | 41 ----------------------------------------- util/Transform.h | 41 +++++++++++++++++++++++++++++++++-------- 4 files changed, 76 insertions(+), 95 deletions(-) diff --git a/util/Matrix.cc b/util/Matrix.cc index b6e36bc..ebf11bb 100644 --- a/util/Matrix.cc +++ b/util/Matrix.cc @@ -206,49 +206,6 @@ Matrix & Matrix::operator*=(const Matrix & other) return (*this); } -Matrix operator*(const Matrix & m1, const Matrix & m2) -{ - Matrix res; - for (int i = 0; i < 4; i++) - { - for (int j = 0; j < 4; j++) - { - res[i][j] = m1[i][0] * m2[0][j] - + m1[i][1] * m2[1][j] - + m1[i][2] * m2[2][j] - + m1[i][3] * m2[3][j]; - } - } - return res; -} - -/* transform a point */ -Vector operator*(const Matrix & m, const Vector & v) -{ - Vector res; - for (int i = 0; i < 3; i++) - { - res[i] = m[i][0] * v[0] - + m[i][1] * v[1] - + m[i][2] * v[2] - + m[i][3]; /* v[3] is implicitly 1.0 */ - } - return res; -} - -/* transform a direction */ -Vector operator%(const Matrix & m, const Vector & v) -{ - Vector res; - for (int i = 0; i < 3; i++) - { - res[i] = m[i][0] * v[0] - + m[i][1] * v[1] - + m[i][2] * v[2]; - } - return res; -} - bool operator==(const Matrix & m1, const Matrix & m2) { for (int i = 0; i < 4; i++) diff --git a/util/Matrix.h b/util/Matrix.h index 5ccd23f..d03263f 100644 --- a/util/Matrix.h +++ b/util/Matrix.h @@ -17,6 +17,49 @@ class Matrix 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]; @@ -25,9 +68,6 @@ class Matrix void calculateInverse(); }; -Matrix operator*(const Matrix & m1, const Matrix & m2); -Vector operator*(const Matrix & m, const Vector & v); /* transform point */ -Vector operator%(const Matrix & m, const Vector & v); /* transform direction */ bool operator==(const Matrix & m1, const Matrix & m2); std::ostream & operator<<(std::ostream & out, const Matrix & m); diff --git a/util/Transform.cc b/util/Transform.cc index ea76787..dc16242 100644 --- a/util/Transform.cc +++ b/util/Transform.cc @@ -2,17 +2,6 @@ #include "Transform.h" #include -Transform::Transform() -{ -} - -Transform Transform::getInverse() -{ - Transform inv; - inv.m_matrix = m_matrix.getInverse(); - return inv; -} - void Transform::lookAt(const Vector & eye, const Vector & focus, const Vector & up) @@ -81,33 +70,3 @@ void Transform::scale(double xs, double ys, double zs) t[2][2] = zs; m_matrix *= t; } - -Vector Transform::transform_point(const Vector & v) -{ - return m_matrix * v; -} - -Vector Transform::transform_direction(const Vector & v) -{ - return m_matrix % v; -} - -Vector Transform::transform_normal(const Vector & v) -{ - return (m_matrix % v).normalize(); -} - -Ray Transform::transform_ray(const Ray & r) -{ - Vector newPosition = m_matrix * r.getOrigin(); - Vector newDirection = m_matrix % r.getDirection(); - Ray res(newPosition, newDirection); - return res; -} - -Transform Transform::operator*(const Transform & other) const -{ - Transform t; - t.m_matrix = m_matrix * other.m_matrix; - return t; -} diff --git a/util/Transform.h b/util/Transform.h index da9654f..37849bd 100644 --- a/util/Transform.h +++ b/util/Transform.h @@ -11,8 +11,13 @@ class Transform { public: - Transform(); - Transform getInverse(); + Transform getInverse() + { + Transform inv; + inv.m_matrix = m_matrix.getInverse(); + return inv; + } + void lookAt(const Vector & eye, const Vector & focus, const Vector & up); @@ -32,16 +37,36 @@ class Transform scale((*vec)[0], (*vec)[1], (*vec)[2]); } Matrix & getMatrix() { return m_matrix; } - Vector transform_point(const Vector & v); - Vector transform_direction(const Vector & v); - Vector transform_normal(const Vector & v); - Ray transform_ray(const Ray & r); - Transform operator*(const Transform & other) const; + Vector transform_point(const Vector & v) + { + return m_matrix * v; + } + + Vector transform_direction(const Vector & v) + { + return m_matrix % v; + } + + Vector transform_normal(const Vector & v) + { + return (m_matrix % v).normalize(); + } + + Ray transform_ray(const Ray & r) + { + return Ray(m_matrix * r.getOrigin(), m_matrix % r.getDirection()); + } + + Transform operator*(const Transform & other) const + { + Transform t; + t.m_matrix = m_matrix * other.m_matrix; + return t; + } protected: Matrix m_matrix; }; - #endif