From 8d563cfc22904a10483789303f8ba5ab5c6ce878 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Tue, 22 Jan 2019 09:51:07 +0100 Subject: [PATCH] brainblock over - constructor needs not only a using but also a signature --- src/core/include/pw/core/matrix.hpp | 27 +++++---- src/core/include/pw/core/vector.hpp | 87 +++++++++++++++------------ src/core/tests/pwcore_test_matrix.cpp | 4 +- 3 files changed, 68 insertions(+), 50 deletions(-) diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index e532f95..90e25cb 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -42,25 +42,29 @@ struct matrix_ : matrixbase_> static const std::size_t rows = R; static const std::size_t cols = C; - typedef matrix_ col_type; + typedef matrix_ col_type; typedef matrix_<1,C,T> row_type; + typedef matrix_ transpose_type; - matrix_(const matrix_& other) + matrix_(const matrix_& other) { *this = other; } + matrix_& operator = (const matrix_& other) + { + for (size_t i = 0; i < other.size();i++) (*this)[i] = other[i]; + return *this; + } + + explicit matrix_(std::initializer_list args) { typename std::initializer_list::iterator it = args.begin(); for (;it != args.end();it++) data[it-args.begin()] = *it; } - matrix_& operator = (const matrix_& other) - { - for (size_t i = 0; i < other.size();i++) (*this)[i] = other[i]; - return *this; - } + template matrix_& set(Arguments ...values) @@ -147,7 +151,7 @@ struct matrix_ : matrixbase_> } auto transposed() const { - matrix_ res; + transpose_type res; for (size_t r = rows;r-->0;) for (size_t c = cols;c-->0;) res(c,r) = (*this)(r,c); @@ -190,8 +194,10 @@ struct matrix_ : matrixbase_> return res; } - row_type row(size_t row_) const { - row_type r; for (size_t i = 0; i < cols; i++) r[i] = (*this)(row_,i); return r; + auto row(size_t row_) const { + row_type r; + for (size_t i = 0; i < cols; i++) r[i] = (*this)(row_,i); + return r; } @@ -210,7 +216,6 @@ double matrix_<1,1,double>::determinant() const } - template auto operator * (const matrix_& A, const matrix_& B diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index 6b7b19f..db427df 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -27,21 +27,28 @@ namespace pw { -template -struct vector_ : matrix_ -{ - static const size_t coefficients = N; +//template +//struct vector_ : matrix_ +//{ +// static const size_t coefficients = N; - using matrix_::matrix_; +// using matrix_::matrix_; +// using matrix_::operator =; -// static T angle_between(const vector_ &a,const vector_ &b) { -// return std::acos( dot( a.normalized(), b.normalized() ) ); -// } -}; +//// static T angle_between(const vector_ &a,const vector_ &b) { +//// return std::acos( dot( a.normalized(), b.normalized() ) ); +//// } +//}; template -struct vector2_ : vector_<2,T> { - using vector_<2,T>::vector_; +struct vector2_ : matrix_<2,1,T> { + + typedef matrix_<2,1,T> base_type; + + using base_type::base_type; + using base_type::operator = ; + + vector2_(const base_type& m) : base_type(m) {} inline const T& x() const { return (*this)[0]; } inline T& x() { return (*this)[0]; } @@ -49,40 +56,46 @@ struct vector2_ : vector_<2,T> { inline const T& y() const { return (*this)[1]; } inline T& y() { return (*this)[1]; } - inline auto homogenous(T w = 1) const { return vector_<3,T>( { x(),y(),w } ); } + inline auto homogenous(T w = 1) const { return matrix_<3,1,T>( { x(),y(),w } ); } }; +template +struct vector3_ : matrix_<3,1,T> { + + typedef matrix_<3,1,T> base_type; + + using base_type::base_type; + using base_type::operator = ; + + vector3_(const base_type& m) : base_type(m) {} + + + inline const T& x() const { return (*this)[0]; } + inline T& x() { return (*this)[0]; } + + inline const T& y() const { return (*this)[1]; } + inline T& y() { return (*this)[1]; } + + inline const T& z() const { return (*this)[2]; } + inline T& z() { return (*this)[2]; } + + inline auto xy() const { return vector2_( { x(),y() } ); } + inline auto homogenous(T w = 1) const { return matrix_<4,1,T>( { x(),y(),z(),w } ); } + + inline static vector3_ forward() { return vector3_ ( { T(0), T(0),-T(1) } ); } + inline static vector3_ backward() { return vector3_( { T(0), T(0), T(1) } ); } + inline static vector3_ right() { return vector3_ ( { T(1), T(0), T(0) } ); } + inline static vector3_ left() { return vector3_ ( {-T(1), T(0), T(0) } ); } + inline static vector3_ up() { return vector3_ ( { T(0), T(1), T(0) } ); } + inline static vector3_ down() { return vector3_ ( { T(0),-T(1), T(0) } ); } + +}; #if defined(_D) -template -struct vector3_ : vector_ { - using vector_::vector_; - - inline const T& x() const { return (*this)[0]; } - inline T& x() { return (*this)[0]; } - - inline const T& y() const { return (*this)[1]; } - inline T& y() { return (*this)[1]; } - - inline const T& z() const { return (*this)[2]; } - inline T& z() { return (*this)[2]; } - - inline auto xy() const { return vector2_( { x(),y() } ); } - inline auto homogenous(T w = 1) const { return vector_( { x(),y(),z(),w } ); } - - - inline static vector3_ forward() { return vector3_ ( { T(0), T(0),-T(1) } ); } - inline static vector3_ backward() { return vector3_( { T(0), T(0), T(1) } ); } - inline static vector3_ right() { return vector3_ ( { T(1), T(0), T(0) } ); } - inline static vector3_ left() { return vector3_ ( {-T(1), T(0), T(0) } ); } - inline static vector3_ up() { return vector3_ ( { T(0), T(1), T(0) } ); } - inline static vector3_ down() { return vector3_ ( { T(0),-T(1), T(0) } ); } - -}; template struct vector4_ : vector_ { diff --git a/src/core/tests/pwcore_test_matrix.cpp b/src/core/tests/pwcore_test_matrix.cpp index 634a52a..c25146f 100644 --- a/src/core/tests/pwcore_test_matrix.cpp +++ b/src/core/tests/pwcore_test_matrix.cpp @@ -29,15 +29,15 @@ int main(int argc,char **argv) { auto m22_inv = m22.inverse(); auto m22_id = m22_inv * m22; - auto v2_t = m22_id * v2; auto v3_t = m22_id * v3; auto v2_f = m22 * v2; auto v2_b = m22_inv * v2_f; + vector2_ r_m22 = m22.row(0).transposed(); - vector2f r_m22 = m22.row(0); + auto r_m22_h = r_m22.homogenous(); debug::d() << "offset(0,1) col-major " << m22.offset(0,1); debug::d() << "det " << m22.determinant();