/* Bullet Continuous Collision Detection and Physics Library Maya Plugin Copyright (c) 2008 Walt Disney Studios This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Written by: Nicola Candussi */ //mat.h #ifndef MVL_MAT_H #define MVL_MAT_H #include #include "base.h" #include "traits.h" namespace mvl { template class mat { public: typedef T value_type; typedef T& reference; typedef T const& const_reference; typedef T* iterator; typedef T const* const_iterator; typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; public: enum { Rows = R, Cols = C, Size = Rows * Cols, }; public: //constructors explicit mat() {} template mat(mat const& m) { *this = m; } explicit mat(value_type val) { for(int i = 0; i < Size; ++i){ m_data[i] = val; } } explicit mat(value_type m00, value_type m01, value_type m10, value_type m11) { operator()(0, 0) = m00; operator()(0, 1) = m01; operator()(1, 0) = m10; operator()(1, 1) = m11; } explicit mat(value_type m00, value_type m01, value_type m02, value_type m10, value_type m11, value_type m12, value_type m20, value_type m21, value_type m22) { operator()(0, 0) = m00; operator()(0, 1) = m01; operator()(0, 2) = m02; operator()(1, 0) = m10; operator()(1, 1) = m11; operator()(1, 2) = m12; operator()(2, 0) = m20; operator()(2, 1) = m21; operator()(2, 2) = m22; } explicit mat(value_type m00, value_type m01, value_type m02, value_type m03, value_type m10, value_type m11, value_type m12, value_type m13, value_type m20, value_type m21, value_type m22, value_type m23, value_type m30, value_type m31, value_type m32, value_type m33) { operator()(0, 0) = m00; operator()(0, 1) = m01; operator()(0, 2) = m02; operator()(0, 3) = m03; operator()(1, 0) = m10; operator()(1, 1) = m11; operator()(1, 2) = m12; operator()(1, 3) = m13; operator()(2, 0) = m20; operator()(2, 1) = m21; operator()(2, 2) = m22; operator()(2, 3) = m23; operator()(3, 0) = m30; operator()(3, 1) = m31; operator()(3, 2) = m32; operator()(3, 3) = m33; } public: //data access value_type operator()(std::size_t i, std::size_t j) const { return m_data[j * Rows + i]; } reference operator()(std::size_t i, std::size_t j) { return m_data[j * Rows + i]; } public: //stl static std::size_t size() { return Size; } static std::size_t max_size() { return Size; } static bool empty() { return false; } iterator begin() { return m_data; } iterator end() { return m_data + Size; } const_iterator begin() const { return m_data; } const_iterator end() const { return m_data + Size; } reverse_iterator rbegin() { return reverse_iterator(end()); } reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } value_type front() { return m_data[0]; } value_type back() { return m_data[Size - 1]; } const_reference front() const { return m_data[0]; } const_reference back() const { return m_data[Size - 1]; } friend std::ostream& operator << (std::ostream& out, mat const& m) { out << "("; for(size_t i = 0; i < Rows - 1; i++) { for(size_t j = 0; j < Cols; j++) { out << m(i, j) << ", "; } out << std::endl; } for(size_t j = 0; j < Cols - 1; j++) { out << m(Rows - 1, j) << ", "; } out << m(Rows - 1, Cols - 1) << ")"; return out; } public: // mat& operator=(mat const& rhs) { std::copy(rhs.begin(),rhs.end(), begin()); return *this; } template mat& operator=(mat const& rhs) { std::copy(rhs.begin(),rhs.end(), begin()); return *this; } private: //data is stored in column major order, so the matrix can passed directly to the graphics APIs T m_data[Size]; }; //assignment operators // OP(mat, mat) // OP(mat, T) #define MAT_IMPLEMENT_MACRO(OP) \ template \ inline \ mat& \ operator OP(mat& lhs, mat const& rhs) { \ for(int i = 0; i < C * R; ++i) { \ lhs[i] OP rhs[i]; \ } \ return lhs; \ } \ \ template \ inline \ mat& \ operator OP(mat& lhs, typename mat::value_type const& rhs) { \ for(int i = 0; i < C * R; ++i) { \ lhs[i] OP rhs[i]; \ } \ return lhs; \ } \ MAT_IMPLEMENT_MACRO(+=) MAT_IMPLEMENT_MACRO(-=) MAT_IMPLEMENT_MACRO(*=) MAT_IMPLEMENT_MACRO(/=) #undef MAT_IMPLEMENT_MACRO //operator + (mat, mat) template inline mat::value_type, R, C> operator + (mat const& lhs, mat const& rhs) { mat::value_type, R, C> res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = lhs(i, j) + rhs(i, j); } } return res; } //operator - (mat, mat) template inline mat::value_type, R, C> operator - (mat const& lhs, mat const& rhs) { mat::value_type, R, C> res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = lhs(i, j) - rhs(i, j); } } return res; } //operator * (mat, POD) template inline mat::value_type, R, C> operator * (mat const& lhs, T2 const& rhs) { mat::value_type, R, C> res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = lhs(i, j) * rhs; } } return res; } //operator * (POD, mat) template inline mat::value_type, R, C> operator * (T1 const& lhs, mat const& rhs) { mat::value_type, R, C> res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = lhs * rhs(i, j); } } return res; } //operator / (mat, POD) template inline mat::value_type, R, C> operator / (mat const& lhs, T2 const& rhs) { mat::value_type, R, C> res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = lhs(i, j) / rhs; } } return res; } //element_prod(mat, mat) template inline mat::value_type, R, C> element_prod(mat const& lhs, mat const& rhs) { mat::value_type, R, C> res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = lhs(i, j) * rhs(i, j); } } return res; } //element_div(mat, mat) template inline mat::value_type, R, C> element_div(mat const& lhs, mat const& rhs) { mat::value_type, R, C> res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = lhs(i, j) / rhs(i, j); } } return res; } //unary operator -(mat) template inline mat operator -(mat const& rhs) { mat res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = -rhs(i, j); } } return res; } //matrix transpose template inline mat trans(mat const& rhs) { mat res; for(int i = 0; i < R ; ++i) { for(int j = 0; j < C; ++j) { res(i, j) = rhs(j, i); } } return res; } //identity matrix template inline mat identity() { mat res; for(int i = 0; i < Sz ; ++i) { for(int j = 0; j < Sz; ++j) { res(i, j) = i == j ? 1 : 0; } } return res; } //matrix diagonal as vector (for square matrices) template inline vec diag(mat const& rhs) { vec res; for(int i = 0; i < N; ++i) { res[i] = rhs(i, i); } return res; } //matrix row as vector template inline vec row(mat const& rhs, std::size_t r) { vec res; for(int i = 0; i < C; ++i) { res[i] = rhs(r, i); } return res; } //matrix column as vector template inline vec col(mat const& rhs, std::size_t c) { vec res; for(int i = 0; i < R; ++i) { res[i] = rhs(i, c); } return res; } //matrix-matrix product template inline mat::value_type, R1, C2> prod(mat const& lhs, mat const& rhs) { mat::value_type, R1, C2> res; for(int i = 0; i < R1; ++i) { for(int j = 0; j < C2; ++j) { res(i, j) = 0; for(int k = 0; k < C1; ++k) { res(i, j) += lhs(i, k) * rhs(k, j); } } } return res; } //matrix - column vector product template inline vec::value_type, R> prod(mat const& lhs, vec const& rhs) { vec::value_type, R> res; for(int i = 0; i < R; ++i) { res(i) = 0; for(int j = 0; j < C; ++j) { res(i) += lhs(i, j) * rhs(j); } } return res; } } // namespace mvl #endif