brainblock over - constructor needs not only a using but also a signature

This commit is contained in:
Hartmut Seichter 2019-01-22 09:51:07 +01:00
parent 878ea70ef7
commit 8d563cfc22
3 changed files with 68 additions and 50 deletions

View file

@ -42,25 +42,29 @@ struct matrix_ : matrixbase_<T, matrix_<R, C, T>>
static const std::size_t rows = R; static const std::size_t rows = R;
static const std::size_t cols = C; static const std::size_t cols = C;
typedef matrix_<R,1,T> col_type; typedef matrix_<R,1,T> col_type;
typedef matrix_<1,C,T> row_type; typedef matrix_<1,C,T> row_type;
typedef matrix_<C,R,T> transpose_type;
matrix_(const matrix_& other) matrix_(const matrix_<R,C,T>& other)
{ {
*this = other; *this = other;
} }
matrix_<R,C,T>& operator = (const matrix_<R,C,T>& other)
{
for (size_t i = 0; i < other.size();i++) (*this)[i] = other[i];
return *this;
}
explicit matrix_(std::initializer_list<T> args) explicit matrix_(std::initializer_list<T> args)
{ {
typename std::initializer_list<T>::iterator it = args.begin(); typename std::initializer_list<T>::iterator it = args.begin();
for (;it != args.end();it++) data[it-args.begin()] = *it; 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 <typename... Arguments> template <typename... Arguments>
matrix_& set(Arguments ...values) matrix_& set(Arguments ...values)
@ -147,7 +151,7 @@ struct matrix_ : matrixbase_<T, matrix_<R, C, T>>
} }
auto transposed() const { auto transposed() const {
matrix_<C,R,T,RowMajor> res; transpose_type res;
for (size_t r = rows;r-->0;) for (size_t r = rows;r-->0;)
for (size_t c = cols;c-->0;) for (size_t c = cols;c-->0;)
res(c,r) = (*this)(r,c); res(c,r) = (*this)(r,c);
@ -190,8 +194,10 @@ struct matrix_ : matrixbase_<T, matrix_<R, C, T>>
return res; return res;
} }
row_type row(size_t row_) const { auto row(size_t row_) const {
row_type r; for (size_t i = 0; i < cols; i++) r[i] = (*this)(row_,i); return r; 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 <typename T, std::size_t R, std::size_t Ca,std::size_t Cb> template <typename T, std::size_t R, std::size_t Ca,std::size_t Cb>
auto operator * (const matrix_<R, Ca, T>& A, auto operator * (const matrix_<R, Ca, T>& A,
const matrix_<R, Cb, T>& B const matrix_<R, Cb, T>& B

View file

@ -27,21 +27,28 @@
namespace pw { namespace pw {
template <std::size_t N,typename T> //template <std::size_t N,typename T>
struct vector_ : matrix_<N,1,T> //struct vector_ : matrix_<N,1,T>
{ //{
static const size_t coefficients = N; // static const size_t coefficients = N;
using matrix_<N,1,T>::matrix_; // using matrix_<N,1,T>::matrix_;
// using matrix_<N,1,T>::operator =;
// static T angle_between(const vector_ &a,const vector_ &b) { //// static T angle_between(const vector_ &a,const vector_ &b) {
// return std::acos( dot( a.normalized(), b.normalized() ) ); //// return std::acos( dot( a.normalized(), b.normalized() ) );
// } //// }
}; //};
template <typename T> template <typename T>
struct vector2_ : vector_<2,T> { struct vector2_ : matrix_<2,1,T> {
using vector_<2,T>::vector_;
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 const T& x() const { return (*this)[0]; }
inline T& x() { 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 const T& y() const { return (*this)[1]; }
inline T& y() { 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 <typename T>
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_<T> forward() { return vector3_<T> ( { T(0), T(0),-T(1) } ); }
inline static vector3_<T> backward() { return vector3_<T>( { T(0), T(0), T(1) } ); }
inline static vector3_<T> right() { return vector3_<T> ( { T(1), T(0), T(0) } ); }
inline static vector3_<T> left() { return vector3_<T> ( {-T(1), T(0), T(0) } ); }
inline static vector3_<T> up() { return vector3_<T> ( { T(0), T(1), T(0) } ); }
inline static vector3_<T> down() { return vector3_<T> ( { T(0),-T(1), T(0) } ); }
};
#if defined(_D) #if defined(_D)
template <typename T>
struct vector3_ : vector_<T,3> {
using vector_<T,3>::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_<T,4>( { x(),y(),z(),w } ); }
inline static vector3_<T> forward() { return vector3_<T> ( { T(0), T(0),-T(1) } ); }
inline static vector3_<T> backward() { return vector3_<T>( { T(0), T(0), T(1) } ); }
inline static vector3_<T> right() { return vector3_<T> ( { T(1), T(0), T(0) } ); }
inline static vector3_<T> left() { return vector3_<T> ( {-T(1), T(0), T(0) } ); }
inline static vector3_<T> up() { return vector3_<T> ( { T(0), T(1), T(0) } ); }
inline static vector3_<T> down() { return vector3_<T> ( { T(0),-T(1), T(0) } ); }
};
template <typename T> template <typename T>
struct vector4_ : vector_<T,4> { struct vector4_ : vector_<T,4> {

View file

@ -29,15 +29,15 @@ int main(int argc,char **argv) {
auto m22_inv = m22.inverse(); auto m22_inv = m22.inverse();
auto m22_id = m22_inv * m22; auto m22_id = m22_inv * m22;
auto v2_t = m22_id * v2; auto v2_t = m22_id * v2;
auto v3_t = m22_id * v3; auto v3_t = m22_id * v3;
auto v2_f = m22 * v2; auto v2_f = m22 * v2;
auto v2_b = m22_inv * v2_f; auto v2_b = m22_inv * v2_f;
vector2_<float> 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() << "offset(0,1) col-major " << m22.offset(0,1);
debug::d() << "det " << m22.determinant(); debug::d() << "det " << m22.determinant();