still not there yet

This commit is contained in:
Hartmut Seichter 2019-01-22 09:06:22 +01:00
parent b2172d88fb
commit 830b63cf88
13 changed files with 369 additions and 356 deletions

View file

@ -1,6 +1,7 @@
# core
* rewrite matrix and associated code
* rename rect to rectangle
# scripting
@ -8,5 +9,8 @@
* refactor script into something like a runtime - script
# scene
* add_child and add_component should have guards - use std::lock_guard

View file

@ -19,24 +19,28 @@ set(hdrs
include/pw/core/image.hpp
)
set(misc
${CMAKE_SOURCE_DIR}/README.md
${CMAKE_SOURCE_DIR}/LICENSE
${CMAKE_SOURCE_DIR}/TODO.md
)
set(srcs
# src/buffer.cpp
src/image.cpp
src/debug.cpp
src/mesh.cpp
# src/mesh.cpp
src/core.cpp
src/serialize.cpp
src/timer.cpp
src/image.cpp
${CMAKE_SOURCE_DIR}/README.md
${CMAKE_SOURCE_DIR}/LICENSE
${CMAKE_SOURCE_DIR}/TODO.md
)
add_library(pwcore
STATIC
${hdrs}
${srcs}
${misc}
)
target_include_directories(
@ -47,8 +51,5 @@ target_include_directories(
target_link_libraries(pwcore)
#add_subdirectory(src)
add_subdirectory(tests)

View file

@ -24,48 +24,39 @@
#define PW_CORE_MATRIX_HPP
#include <pw/core/globals.hpp>
#include <pw/core/matrixbase.hpp>
#include <pw/core/math.hpp>
#include <pw/core/matrixbase.hpp>
#include <numeric>
namespace pw {
template <std::size_t R,std::size_t C,bool RowMajor = false>
struct matrixtraits_
template <std::size_t R,std::size_t C, typename T, bool RowMajor = false>
struct matrix_ : matrixbase_<T, matrix_<R, C, T>>
{
//! rows
inline std::size_t rows() const { return R; }
//! return number of columns
inline std::size_t cols() const { return C; }
};
template <typename T, std::size_t R,std::size_t C,bool RowMajor = false>
struct matrix_ : matrixbase_<T, matrix_<T, R, C>>,matrixtraits_<R,C,RowMajor>
{
T data[R*C];
// typedef matrixbase_<T, matrix_<T, R, C>> Base;
using matrixbase_<T, matrix_<T, R, C>>::matrixbase_;
using matrixtraits_<R,C,RowMajor>::rows;
using matrixtraits_<R,C,RowMajor>::cols;
using matrixbase_<T, matrix_<R, C, T>>::matrixbase_;
static const std::size_t rows = R;
static const std::size_t cols = C;
typedef matrix_<R,1,T> col_type;
typedef matrix_<1,C,T> row_type;
matrix_(const matrix_& other)
{
*this = other;
}
explicit matrix_(std::initializer_list<T> args)
{
typename std::initializer_list<T>::iterator it = args.begin();
for (;it != args.end();it++) data[it-args.begin()] = *it;
}
matrix_& operator = (const matrix_<T,R,C,RowMajor>& other)
matrix_& operator = (const matrix_& other)
{
for (size_t i = 0; i < other.size();i++) (*this)[i] = other[i];
return *this;
@ -100,16 +91,16 @@ struct matrix_ : matrixbase_<T, matrix_<T, R, C>>,matrixtraits_<R,C,RowMajor>
//! set identity
inline matrix_& set_identity()
{
for (unsigned int r = 0;r < rows(); r++)
for (unsigned int c = 0; c < cols(); c++)
for (std::size_t r = 0;r < rows; r++)
for (std::size_t c = 0; c < cols; c++)
(*this)(r,c) = (c == r) ? T(1) : T(0);
return *this;
}
template<std::size_t Rs,std::size_t Cs,bool RowMajorSlice = RowMajor>
matrix_<T,Rs,Cs,RowMajorSlice> slice(std::size_t r,std::size_t c) const
auto slice(std::size_t r,std::size_t c) const
{
matrix_<T,Rs,Cs,RowMajorSlice> s;
matrix_<Rs,Cs,T,RowMajorSlice> s;
for (std::size_t ri = 0;ri < Rs;ri++)
for (std::size_t ci = 0;ci < Cs;ci++)
s(ri,ci) = (*this)(ri+r,ci+c);
@ -117,7 +108,7 @@ struct matrix_ : matrixbase_<T, matrix_<T, R, C>>,matrixtraits_<R,C,RowMajor>
}
template<std::size_t Rs,std::size_t Cs,bool RowMajorSlice = RowMajor>
matrix_& set_slice(const matrix_<T,Rs,Cs,RowMajorSlice>& s,std::size_t r,std::size_t c)
matrix_& set_slice(const matrix_<Rs,Cs,T,RowMajorSlice>& s,std::size_t r,std::size_t c)
{
for (std::size_t ri = 0;ri < Rs;ri++)
for (std::size_t ci = 0;ci < Cs;ci++)
@ -127,9 +118,9 @@ struct matrix_ : matrixbase_<T, matrix_<T, R, C>>,matrixtraits_<R,C,RowMajor>
template<std::size_t Rs,std::size_t Cs,bool RowMajorSlice = RowMajor>
matrix_<T,Rs,Cs,RowMajorSlice> minor(std::size_t r0,std::size_t c0) const
auto minor(std::size_t r0,std::size_t c0) const
{
matrix_<T,Rs,Cs,RowMajorSlice> m;
matrix_<Rs,Cs,T,RowMajorSlice> m;
size_t r = 0;
for (size_t ri = 0; ri < R; ri++) {
size_t c = 0;
@ -155,17 +146,17 @@ struct matrix_ : matrixbase_<T, matrix_<T, R, C>>,matrixtraits_<R,C,RowMajor>
return det;
}
matrix_<T,C,R,RowMajor> transposed() const {
matrix_<T,C,R,RowMajor> res;
for (size_t r = this->rows();r-->0;)
for (size_t c = this->cols();c-->0;)
auto transposed() const {
matrix_<C,R,T,RowMajor> res;
for (size_t r = rows;r-->0;)
for (size_t c = cols;c-->0;)
res(c,r) = (*this)(r,c);
return res;
}
matrix_<T,C,R,RowMajor> inverse() const {
auto inverse() const {
T invDet = T(1) / this->determinant();
matrix_<T,C,R,RowMajor> inv;
matrix_<R,C,T,RowMajor> inv;
for (int j = 0; j < C; j++)
for (int i = 0; i < R; i++)
{
@ -199,16 +190,21 @@ struct matrix_ : matrixbase_<T, matrix_<T, R, C>>,matrixtraits_<R,C,RowMajor>
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;
}
};
template <> inline
float matrix_<float,1,1>::determinant() const
float matrix_<1,1,float>::determinant() const
{
return (*this)(0,0);
}
template <> inline
double matrix_<double,1,1>::determinant() const
double matrix_<1,1,double>::determinant() const
{
return (*this)(0,0);
}
@ -216,11 +212,11 @@ double matrix_<double,1,1>::determinant() const
template <typename T, std::size_t R, std::size_t Ca,std::size_t Cb>
auto operator * (const matrix_<T, R, Ca>& A,
const matrix_<T, R, Cb>& B
auto operator * (const matrix_<R, Ca, T>& A,
const matrix_<R, Cb, T>& B
)
{
matrix_<T,R,Cb> result; result.zero(); // zero the output
matrix_<R,Cb,T> result; result.zero(); // zero the output
for (size_t r = 0; r < R; r++)
for (size_t c = 0; c < Cb; c++)
for (size_t iI = 0; iI < R; iI++)
@ -234,49 +230,49 @@ auto operator * (const matrix_<T, R, Ca>& A,
//
template <typename T> using matrix2x2_ = matrix_<T, 2, 2>;
template <typename T> using matrix3x3_ = matrix_<T, 3, 3>;
template <typename T> using matrix4x4_ = matrix_<T, 4, 4>;
template <typename T> using matrix2x2_ = matrix_<2, 2, T>;
//template <typename T> using matrix3x3_ = matrix_<T, 3, 3>;
//template <typename T> using matrix4x4_ = matrix_<T, 4, 4>;
using matrix2x2f = matrix_<float, 2, 2>;
using matrix2x2d = matrix_<double, 2, 2>;
using matrix2x2 = matrix_<real_t, 2, 2>;
using matrix2x2f = matrix_<2, 2,float>;
using matrix2x2d = matrix_<2, 2,double>;
using matrix2x2 = matrix_<2, 2,real_t>;
using matrix3x3f = matrix_<float, 3, 3>;
using matrix3x3d = matrix_<double, 3, 3>;
using matrix3x3 = matrix_<real_t, 3, 3>;
//using matrix3x3f = matrix_<float, 3, 3>;
//using matrix3x3d = matrix_<double, 3, 3>;
//using matrix3x3 = matrix_<real_t, 3, 3>;
using matrix4x4f = matrix_<float, 4, 4>;
using matrix4x4d = matrix_<double, 4, 4>;
using matrix4x4 = matrix_<real_t, 4, 4>;
//using matrix4x4f = matrix_<float, 4, 4>;
//using matrix4x4d = matrix_<double, 4, 4>;
//using matrix4x4 = matrix_<real_t, 4, 4>;
//
//
//
template <typename T>
struct matrix_tools {
//template <typename T>
//struct matrix_tools {
inline static
matrix4x4_<T> projection_from_frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
{
matrix4x4_<T> frustum;
// inline static
// matrix4x4_<T> projection_from_frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
// {
// matrix4x4_<T> frustum;
frustum.fill(0);
// frustum.fill(0);
frustum(0,0) = T(2) * zNear/(Right-Left);
frustum(1,1) = T(2) * zNear/(Top-Bottom);
// frustum(0,0) = T(2) * zNear/(Right-Left);
// frustum(1,1) = T(2) * zNear/(Top-Bottom);
frustum(0,2) = (Right+Left)/(Right-Left); //A
frustum(1,2) = (Top+Bottom)/(Top-Bottom); //B
frustum(2,2) = - (zFar+zNear)/(zFar-zNear); //C
frustum(3,2) = -(T(2) * zFar*zNear)/(zFar-zNear); //D
// frustum(0,2) = (Right+Left)/(Right-Left); //A
// frustum(1,2) = (Top+Bottom)/(Top-Bottom); //B
// frustum(2,2) = - (zFar+zNear)/(zFar-zNear); //C
// frustum(3,2) = -(T(2) * zFar*zNear)/(zFar-zNear); //D
frustum(2,3) = -T(1);
// frustum(2,3) = -T(1);
return frustum;
}
};
// return frustum;
// }
//};
}

View file

@ -87,6 +87,16 @@ struct matrixbase_ {
return derived().data[i];
}
static T dot(const Derived &a,const Derived &b) {
Derived r; for (size_t i = 0;i < a.size();i++) r[i] = a[i] * b[i];
return std::accumulate(std::begin(r), std::end(r), T(0));
}
static const Derived lerp(const Derived &a,const Derived &b,const T& t) {
return a + (b - a) * t;
}
// inline Derived& operator *= (const T& b) { for (auto & e : *this) e *= b; return derived(); }
// inline Derived& operator /= (const T& b) { for (auto & e : *this) e /= b; return derived(); }
// inline Derived& operator += (const T& b) { for (auto & e : *this) e += b; return derived(); }

View file

@ -24,6 +24,7 @@
#define PW_CORE_QUATERNION_HPP
#include <pw/core/vector.hpp>
#include <pw/core/axisangle.hpp>
namespace pw {
@ -40,8 +41,8 @@ struct quaternion_ : vector4_<T> {
using Base::y;
using Base::z;
using Base::w;
// using Base::lerp;
// using Base::operator*;
using Base::lerp;
using Base::operator*;
using Base::operator/;
inline const quaternion_ operator * (const quaternion_& rhs) const {
@ -53,6 +54,10 @@ struct quaternion_ : vector4_<T> {
);
}
inline const quaternion_ operator / (const T& rhs) const {
return quaternion_( { x() / rhs, y() / rhs, z() / rhs, w() / rhs, } );
}
//! conjugate
inline quaternion_ conjugate() const { return quaternion_( { -x(),-y(),-z(),w() } ); }
@ -103,8 +108,24 @@ struct quaternion_ : vector4_<T> {
wtemp);
}
static const quaternion_ normalized_lerp(const quaternion_ &a,const quaternion_ &b,const T &t) {
return quaternion_(lerp(a,b,t).normalized());
static const quaternion_<T> normalized_lerp(const quaternion_<T> &a,const quaternion_<T> &b,const T &t) {
return quaternion_<T>(lerp(a,b,t).normalized());
}
static const quaternion_<T> from_axisangle(const axisangle_<T> &aa) {
using std::sin;
using std::cos;
const T sinHalfAngle( sin(aa.angle() * T(0.5) ));
return quaternion_<T>( { aa.axis().x() * sinHalfAngle, // x
aa.axis().y() * sinHalfAngle, // y
aa.axis().z() * sinHalfAngle, // z
cos(aa.angle() * T(0.5)) // w
}
);
}
};

View file

@ -33,12 +33,12 @@ namespace pw {
struct serialize {
template <typename T,size_t R,size_t C>
inline static std::string matrix(const matrix_<T,R,C>& m) {
template <size_t R,size_t C,typename T>
inline static std::string matrix(const matrix_<R,C,T>& m) {
std::stringstream ss;
for (int r = 0; r < m.rows();r++) {
for (int c = 0; c < m.cols();c++) {
for (int r = 0; r < m.rows;r++) {
for (int c = 0; c < m.cols;c++) {
ss << m(r,c) << " ";
}
ss << std::endl;

View file

@ -41,8 +41,6 @@ struct size_ {
template <typename To_>
size_<To_> cast() const { return size_<To_>(static_cast<To_>(width),static_cast<To_>(height)); }
};
typedef size_<real_t> size;

View file

@ -27,46 +27,36 @@
namespace pw {
template <typename T, std::size_t N>
struct vector_ : matrix_<T, N, 1,false>
template <std::size_t N,typename T>
struct vector_ : matrix_<N,1,T>
{
typedef matrix_<T, N, 1, false> Base;
static const size_t coefficients = N;
using typename Base::value_type;
using Base::Base;
static T dot(const Base &a,const Base &b) {
vector_ r; for (size_t i = 0;i < N;i++) r[i] = a[i] * b[i];
return std::accumulate(std::begin(r), std::end(r), T(0));
}
static T angle_between(const Base &a,const Base &b) {
return std::acos( dot( a.normalized(), b.normalized() ) );
}
static const vector_ lerp(const vector_ &a,const vector_ &b,const T& t) {
return a + (b - a) * t;
}
using matrix_<N,1,T>::matrix_;
// static T angle_between(const vector_ &a,const vector_ &b) {
// return std::acos( dot( a.normalized(), b.normalized() ) );
// }
};
template <typename T>
struct vector2_ : vector_<T,2> {
using vector_<T,2>::vector_;
struct vector2_ : vector_<2,T> {
using vector_<2,T>::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 auto homogenous(T w = 1) const { return vector_<T,3>(x(),y(),w); }
inline auto homogenous(T w = 1) const { return vector_<3,T>( { x(),y(),w } ); }
};
#if defined(_D)
template <typename T>
struct vector3_ : vector_<T,3> {
@ -117,27 +107,28 @@ struct vector4_ : vector_<T,4> {
};
#endif
//
//
//
using vector2f = vector2_<float>;
using vector2d = vector2_<double>;
using vector2 = vector2_<real_t>;
//using vector2 = vector2_<real_t>;
using vector3f = vector3_<float>;
using vector3d = vector3_<double>;
using vector3 = vector3_<real_t>;
//using vector3f = vector3_<float>;
//using vector3d = vector3_<double>;
//using vector3 = vector3_<real_t>;
//using vector4f = vector4_<float>;
//using vector4d = vector4_<double>;
//using vector4 = vector4_<real_t>;
using vector4f = vector4_<float>;
using vector4d = vector4_<double>;
using vector4 = vector4_<real_t>;
}
#if defined(___OLDSTUFF)
template <unsigned int components,typename T>
@ -328,4 +319,6 @@ typedef vector4_<unsigned int> vector4ui;
#endif
#endif

View file

@ -5,25 +5,25 @@ add_executable(pwcore_test_matrix
target_link_libraries(pwcore_test_matrix
pwcore)
add_executable(pwcore_test_vector
pwcore_test_vector.cpp
)
#add_executable(pwcore_test_vector
# pwcore_test_vector.cpp
# )
target_link_libraries(pwcore_test_vector
pwcore)
#target_link_libraries(pwcore_test_vector
# pwcore)
add_executable(pwcore_test_quaternion
pwcore_test_quaternion.cpp
)
#add_executable(pwcore_test_quaternion
# pwcore_test_quaternion.cpp
# )
target_link_libraries(pwcore_test_quaternion
pwcore)
#target_link_libraries(pwcore_test_quaternion
# pwcore)
add_executable(pwcore_test_axisangle
pwcore_test_axisangle.cpp
)
#add_executable(pwcore_test_axisangle
# pwcore_test_axisangle.cpp
# )
target_link_libraries(pwcore_test_axisangle
pwcore)
#target_link_libraries(pwcore_test_axisangle
# pwcore)

View file

@ -8,11 +8,10 @@ int main(int argc,char **argv) {
pw::axisangle_<float> aa = pw::axisangle_<float>();
// pw::quaternionf qf = pw::quaternionf::from_axisangle(aa);
pw::quaternionf qf = pw::quaternionf::from_axisangle(aa);
// std::cout << "aa as quaternion as vector = " << pw::serialize::matrix(qf.as_vector()) << std::endl;
// std::cout << "aa.matrix() = " << pw::serialize::matrix(qf.to_matrix()) << std::endl;
std::cout << "aa as quaternion as matrix = " << pw::serialize::matrix(qf.to_matrix()) << std::endl;
std::cout << "aa.matrix() = " << pw::serialize::matrix(qf.to_matrix()) << std::endl;
return 0;

View file

@ -2,27 +2,13 @@
#include <pw/core/matrix.hpp>
#include <pw/core/vector.hpp>
//#include <pw/core/serialize.hpp>
#include <pw/core/serialize.hpp>
#include <pw/core/debug.hpp>
#include <iostream>
#include <sstream>
template <typename T_,typename O_,size_t R,size_t C> inline static
std::basic_ostream<O_>& operator << (std::basic_ostream<O_>& os,
const pw::matrix_<T_,R,C>& m
)
{
for (size_t r = 0; r < R;r++){
for (size_t c = 0;c < C;c++) {
os << m(r,c) << " ";
}
os << std::endl;
}
return os;
}
int main(int argc,char **argv) {
@ -39,36 +25,39 @@ int main(int argc,char **argv) {
vector2f v3( { 1.f,2.f } );
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;
vector2f r_m22 = m22.row(0);
debug::d() << "offset(0,1) col-major " << m22.offset(0,1);
debug::d() << "det " << m22.determinant();
std::cout << "m22 " << m22 << std::endl;
std::cout << "m22-1 " << m22_inv << std::endl;
std::cout << "m22-i " << m22_id << std::endl;
std::cout << "v22_t " << v2_t << std::endl;
std::cout << "v3_t " << v3_t << std::endl;
std::cout << "m22 " << pw::serialize::matrix(m22) << std::endl;
std::cout << "m22-1 " << pw::serialize::matrix(m22_inv) << std::endl;
std::cout << "m22-i " << pw::serialize::matrix(m22_id) << std::endl;
std::cout << "v22_t " << pw::serialize::matrix(v2_t) << std::endl;
std::cout << "v3_t " << pw::serialize::matrix(v3_t) << std::endl;
std::cout << "v2_f " << v2_f << std::endl;
std::cout << "v2_b " << v2_b << std::endl;
std::cout << "v2_f " << pw::serialize::matrix(v2_f) << std::endl;
std::cout << "v2_b " << pw::serialize::matrix(v2_b) << std::endl;
std::cout << "v2_b.norm " << v2_b.norm() << std::endl;
// v2_b.normalize();
std::cout << "v2_b.normalized " << v2_b.normalized() << std::endl;
std::cout << "v2_b.normalized " << pw::serialize::matrix(v2_b.normalized()) << std::endl;
std::cout << "v2_b~v3_t " << rad_to_deg(vector2f::angle_between(v2,v3)) << std::endl;
// std::cout << "v2_b~v3_t " << rad_to_deg(vector2f::angle_between(v2,v3)) << std::endl;

View file

@ -20,10 +20,8 @@ int main(int argc,char **argv) {
pw::quaternionf qi = qf.inverse();
std::cout << "qf.inverse() (qi) = " << pw::serialize::matrix(qi) << std::endl;
// pw::quaternionf qmid = pw::quaternionf::normalized_lerp(qi,qf,0.5f);
// std::cout << "qmid.dot() (half between qf and qi) = " << pw::rad_to_deg(std::acos(qmid.dot(qf))) << std::endl;
pw::quaternionf qmid = pw::quaternionf::normalized_lerp(qi,qf,0.5f);
// std::cout << "qmid.dot() (half between qf and qi) = " << pw::rad_to_deg(quaternionf::angle_between()) << std::endl;

View file

@ -6,30 +6,34 @@
int main(int argc,char **argv) {
pw::vector4_<float> v4;
pw::vector3f v;
pw::vector2_<float> v2;
v4.fill(1.5);
v2.x = 0.1f;
std::cout << "v4 = " << pw::serialize::matrix(v4) << std::endl;
// pw::vector4_<float> v4;
// pw::vector3f v;
std::cout << "rows() : " << v4.rows() << std::endl;
std::cout << "cols() : " << v4.cols() << std::endl;
std::cout << "ptr() : " << v4.ptr() << std::endl;
std::cout << "ptr()[0] : " << v4.ptr()[0] << std::endl;
std::cout << "(0,0) : " << v4(0,0) << std::endl;
// v4.fill(1.5);
auto v3 = v4.xyz();
// std::cout << "v4 = " << pw::serialize::matrix(v4) << std::endl;
auto v3_p = v4.project();
// std::cout << "rows() : " << v4.rows() << std::endl;
// std::cout << "cols() : " << v4.cols() << std::endl;
// std::cout << "ptr() : " << v4.ptr() << std::endl;
// std::cout << "ptr()[0] : " << v4.ptr()[0] << std::endl;
// std::cout << "(0,0) : " << v4(0,0) << std::endl;
auto v3_h = v.homogenous();
// auto v3 = v4.xyz();
// auto v3_lerp = vector4f::lerp()
// auto v3_p = v4.project();
std::cout << "v3 = " << pw::serialize::matrix(v3) << std::endl;
// auto v3_h = v.homogenous();
std::cout << "v3.normalized() = " << pw::serialize::matrix(v3.normalized()) << std::endl;
//// auto v3_lerp = vector4f::lerp()
// std::cout << "v3 = " << pw::serialize::matrix(v3) << std::endl;
// std::cout << "v3.normalized() = " << pw::serialize::matrix(v3.normalized()) << std::endl;
return 0;