From eeb00bf74c64c2e8be34d328499612c92fdde9c1 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 23 Apr 2017 17:41:19 -0600 Subject: [PATCH] common: tidy qmat --- common/qvec.cc | 28 +++++------------ include/common/qvec.hh | 69 ++++++++++++++++++++++-------------------- light/test_light.cc | 8 ++--- 3 files changed, 49 insertions(+), 56 deletions(-) diff --git a/common/qvec.cc b/common/qvec.cc index 384fcbcf..a5ce66e7 100644 --- a/common/qvec.cc +++ b/common/qvec.cc @@ -104,22 +104,9 @@ static bool gluInvertMatrixd(const double m[16], double invOut[16]) qmat4x4d qv::invert(const qmat4x4d &input, bool *ok) { - double flat_in[16]; - double flat_out[16]; - - for (int i=0; i<4; i++) - for (int j=0; j<4; j++) - flat_in[4*i+j] = input[i][j]; - - *ok = gluInvertMatrixd(flat_in, flat_out); - if (!*ok) - return qmat4x4d(); - - qmat4x4d result; - for (int i=0; i<4; i++) - for (int j=0; j<4; j++) - result[i][j] = flat_out[4*i+j]; - return result; + qmat4x4d res; + *ok = gluInvertMatrixd(input.m_values, res.m_values); + return res; } qmat4x4f qv::invert(const qmat4x4f &input, bool *ok) @@ -129,10 +116,11 @@ qmat4x4f qv::invert(const qmat4x4f &input, bool *ok) qmat2x2f qv::invert(const qmat2x2f &m, bool *ok) { - float a = m[0][0]; - float b = m[1][0]; - float c = m[0][1]; - float d = m[1][1]; + // http://www.mathwords.com/i/inverse_of_a_matrix.htm + float a = m.at(0,0); + float b = m.at(0,1); + float c = m.at(1,0); + float d = m.at(1,1); float det = a*d - b*c; if (det == 0) { diff --git a/include/common/qvec.hh b/include/common/qvec.hh index c5f6893b..8bca1fa7 100644 --- a/include/common/qvec.hh +++ b/include/common/qvec.hh @@ -238,33 +238,36 @@ using qplane3f = qplane3; using qplane3d = qplane3; /** - * M row, N column matrix + * M row, N column matrix. */ template class qmat { -protected: - qvec m_cols[N]; +public: + /** + * Column-major order. [ (row0,col0), (row1,col0), .. ] + */ + T m_values[M*N]; public: /** * Identity matrix if square, otherwise fill with 0 */ qmat() { - for (int i=0; i(0); + for (int i=0; iat(i,i) = 1; } } } // copy constructor qmat(const qmat &other) { - for (int i=0; im_cols[i] = other.m_cols[i]; + for (int i=0; im_values[i] = other.m_values[i]; } /** @@ -272,47 +275,49 @@ public: */ template qmat(const qmat &other) { - for (int i=0; i(other[i]); + for (int i=0; im_values[i] = static_cast(other.m_values[i]); } - // initializer list + // initializer list, column-major order qmat(std::initializer_list list) { assert(list.size() == M*N); const T *listPtr = list.begin(); - for (int j=0; jm_cols[j][i] = listPtr[(j*M)+i]; - } + for (int i=0; im_values[i] = listPtr[i]; } } bool operator==(const qmat &other) const { - for (int i=0; im_cols[i] != other.m_cols[i]) + for (int i=0; im_values[i] != other.m_values[i]) return false; return true; } - // access to columns + // access to elements - qvec operator[](const size_t idx) const { - assert(idx >= 0 && idx < N); - return m_cols[idx]; + T& at(int row, int col) { + assert(row >= 0 && row < M); + assert(col >= 0 && col < N); + return m_values[col * M + row]; } - qvec &operator[](const size_t idx) { - assert(idx >= 0 && idx < N); - return m_cols[idx]; + T at(int row, int col) const { + assert(row >= 0 && row < M); + assert(col >= 0 && col < N); + return m_values[col * M + row]; } // multiplication by a vector qvec operator*(const qvec &vec) const { - qvec res; - for (int j=0; jm_cols[j] * vec[j]; + qvec res(0); + for (int i=0; iat(i, j) * vec[j]; + } } return res; } @@ -326,9 +331,9 @@ public: for (int j=0; jat(i,k) * other.at(k,j); } - res[i][j] = val; + res.at(i,j) = val; } } return res; @@ -337,9 +342,9 @@ public: // multiplication by a scalar qmat operator*(const T scalar) const { - qmat res; - for (int j=0; jm_cols[j] * scalar; + qmat res(*this); + for (int i=0; i