From 28e3ef513e6e508489472066f2aac73ee0951c80 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Thu, 17 Jan 2019 14:14:34 +0100 Subject: [PATCH] need to rewrite the whole matrix stack --- src/core/include/pw/core/matrix.hpp | 73 +++++++++++++++++-------- src/core/include/pw/core/matrixbase.hpp | 11 ++++ src/core/include/pw/core/mesh.hpp | 2 + src/core/include/pw/core/vector.hpp | 4 +- src/core/src/mesh.cpp | 11 ++++ 5 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index 2f9a13f..2843872 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -48,6 +48,11 @@ public: matrix_& operator = (const matrix_& other); + matrix_(std::initializer_list il) + { +// for (int i = 0;i < il.size();i++) (*this).at(i) = il[i]; + } + matrix_ transposed() const; inline matrix_& operator *= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) *= b; return *this; } @@ -102,7 +107,7 @@ public: matrix_& copy_from_data(const T* src) { for (unsigned int i = 0; i < R*C; ++i) { (*this).at(i) = src[i]; } return *this; } matrix_ operator * (const matrix_& rhs) const { - return mul(*this,rhs); + return mul(rhs); } const matrix_ reshape() const { @@ -124,6 +129,26 @@ public: } void normalize(); + + template + matrix_ mul(const matrix_& B) const + { + // aC == bR + // set all null + matrix_ res; + res.fill(0); + + // compute all resulting cells + for (unsigned int r = 0; r < R; ++r) { + for (unsigned int c = 0; c < bC; ++c) { + // building inner product + for (unsigned int iI = 0; iI < R;iI++) { + res.at(r,c) += (*this).at(r,iI) * B.at(iI,c); + } + } + } + return res; + } }; ///////////////////////////////////////////////////////////////////////////// @@ -176,28 +201,32 @@ inline matrix_ operator - (const matrix_& a, const matrix_& return res; } + + +//template +//matrix_ static inline +//mul(const matrix_& A, const matrix_& B) +//{ +// // aC == bR +// // set all null +// matrix_ res; +// res.fill(0); + +// // compute all resulting cells +// for (unsigned int r = 0; r < aR; ++r) { +// for (unsigned int c = 0; c < bC; ++c) { +// // building inner product +// for (unsigned int iI = 0; iI < aCbR;iI++) { +// res.at(r,c) += A.at(r,iI) * B.at(iI,c); +// } +// } +// } +// return res; +//} + + ///////////////////////////////////////////////////////////////////////////// -template -matrix_ static inline -mul(const matrix_& A, const matrix_& B) -{ - // aC == bR - // set all null - matrix_ res; - res.fill(0); - - // compute all resulting cells - for (unsigned int r = 0; r < aR; ++r) { - for (unsigned int c = 0; c < bC; ++c) { - // building inner product - for (unsigned int iI = 0; iI < aCbR;iI++) { - res.at(r,c) += A.at(r,iI) * B.at(iI,c); - } - } - } - return res; -} @@ -206,7 +235,7 @@ mul(const matrix_& A, const matrix_& B) template matrix_& matrix_::operator *= (const matrix_& rhs) { - *this = mul(*this,rhs); + *this = mul(rhs); return *this; } diff --git a/src/core/include/pw/core/matrixbase.hpp b/src/core/include/pw/core/matrixbase.hpp index 327a88e..85608ec 100644 --- a/src/core/include/pw/core/matrixbase.hpp +++ b/src/core/include/pw/core/matrixbase.hpp @@ -23,6 +23,9 @@ #ifndef PW_CORE_MATRIXBASE_HPP #define PW_CORE_MATRIXBASE_HPP + +#include + namespace pw { /** @@ -36,6 +39,9 @@ public: typedef unsigned int size_type; + matrixbase() {} + + //! assignment constructor explicit matrixbase(int rows, int cols,T* ptr,bool row_major,int data_offset = 0) : _data(ptr) @@ -47,6 +53,11 @@ public: { } +// matrixbase(std::initializer_list il) +// { +// for (int i = 0;i < il.size();i++) this->at(i) = il[i]; +// } + inline const T& get_element(int e) const { return this->at(e); } inline void set_element(int e,const T &v) { this->at(e) = v; } diff --git a/src/core/include/pw/core/mesh.hpp b/src/core/include/pw/core/mesh.hpp index e2b5852..7b85d57 100644 --- a/src/core/include/pw/core/mesh.hpp +++ b/src/core/include/pw/core/mesh.hpp @@ -51,6 +51,8 @@ public: const indexarray_t& indices() const { return _indices; } const vertex3array_t& vertices() const { return _vertices; } + void apply(const matrix4x4& m); + void reset(); protected: diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index 9875df1..bd1d2a7 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -98,8 +98,8 @@ public: T& z() { return (*this)(2); } - const vector_<4,T> project(const T& w) const { - return vector_<4,T>(x(),y(),z(),w); + vector_<4,T> project(const T& w) const { + return vector_<4,T>({ x(),y(),z(),w } ); } const vector_<2,T> unproject() const { diff --git a/src/core/src/mesh.cpp b/src/core/src/mesh.cpp index 1423211..7e69764 100644 --- a/src/core/src/mesh.cpp +++ b/src/core/src/mesh.cpp @@ -3,6 +3,17 @@ namespace pw { +void mesh::apply(const matrix4x4 &m) +{ + for (auto &v : _vertices) + { +// v = m * v.project(1); + + auto vh = v.project(1); + m.mul(vh); + } +} + }