this is basically a design document version - it sets the scenery for upcoming additions

This commit is contained in:
Hartmut Seichter 2018-04-02 01:06:50 +02:00
parent 32aac45162
commit 550d27273f
1504 changed files with 1051518 additions and 127 deletions

View file

@ -1,18 +1,4 @@
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/../deps/glfw-3.2.1/include
${CMAKE_CURRENT_SOURCE_DIR}/../deps/lua-5.3.4/src
${CMAKE_CURRENT_SOURCE_DIR}/../deps/glad/include
)
add_subdirectory(src)
add_subdirectory(tests)
add_library(pw
STATIC
script.hpp
script.cpp
core.hpp
core.cpp
window.hpp
window.cpp
)
target_link_libraries(pw lualib glfw glad)

View file

@ -0,0 +1,37 @@
#ifndef PW_CORE_AXISANGLE_HPP
#define PW_CORE_AXISANGLE_HPP
#include <pw/core/vector.hpp>
namespace pw {
template <typename T>
class axisangle {
protected:
vector3<T> _axis;
T _angle;
public:
axisangle() {}
axisangle(const vector3<T> &axis,const T &angle)
: _axis(axis)
, _angle(angle)
{
}
vector3<T> axis() const { return _axis; }
void set_axis(const vector3<T> &axis) { _axis = axis; }
T angle() const { return _angle; }
void set_angle(const T &angle) { _angle = angle; }
};
}
#endif

View file

@ -1,7 +1,5 @@
#ifndef PW_CORE_HPP
#define PW_CORE_HPP
#ifndef PW_CORE_CORE_HPP
#define PW_CORE_CORE_HPP
#include "script.hpp"
#endif

View file

@ -0,0 +1,5 @@
#ifndef PW_CORE_GLOBALS_HPP
#define PW_CORE_GLOBALS_HPP
#endif

View file

@ -0,0 +1,11 @@
#ifndef PW_CORE_IMAGE_HPP
#define PW_CORE_IMAGE_HPP
#include <pw/core/globals.hpp>
namespace pw {
}
#endif

View file

@ -0,0 +1,15 @@
#ifndef PW_CORE_MATH_HPP
#define PW_CORE_MATH_HPP
#include <cmath>
namespace pw {
const static double __PW_PI = 3.1415926535897932384626433832795028841971693993751058209;
template <typename T>
inline const T Pi() { return static_cast<T>(__PW_PI); }
}
#endif

View file

@ -0,0 +1,583 @@
/*
* Copyright (C) 1999-2017 Hartmut Seichter
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PW_CORE_MATRIX_HPP
#define PW_CORE_MATRIX_HPP
#include <pw/core/matrixbase.hpp>
#include <pw/core/math.hpp>
#include <numeric>
namespace pw {
template <unsigned int R, unsigned int C, typename T>
class matrix : public matrixbase<T> {
T m[R*C];
public:
using typename matrixbase<T>::value_type;
matrix();
matrix(const matrix& mtc);
matrix& operator = (const matrix& other);
matrix transposed() const;
inline matrix& operator *= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) *= b; return *this; }
inline matrix& operator /= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) /= b; return *this; }
inline matrix& operator += (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) += b; return *this; }
inline matrix& operator -= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) -= b; return *this; }
inline matrix& operator += (const matrix& other) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) += other.at(i); return *this; }
inline matrix& operator -= (const matrix& other) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) -= other.at(i); return *this; }
inline const matrix
get_inverse() const
{
matrix resMat;
for ( unsigned int r = 0; r < C; ++r) {
for ( unsigned int j = 0; j < R; ++j) {
short sgn = ( (r+j)%2) ? -1 : 1;
matrix<R-1,C-1,T> minor;
this->get_minor(minor,r,j);
resMat.at(r,j) = minor.determinant() * sgn;
}
}
resMat = resMat.transposed();
resMat *= (static_cast<T>(1)/this->determinant());
return resMat;
}
inline
matrix& invert() {
*this = this->get_inverse();
return *this;
}
void get_minor(matrix<R-1,C-1,T>& res, unsigned int r0, unsigned int c0) const;
T determinant() const;
T squared_norm() const;
T norm() const;
matrix<R,C,T>& operator *= (const matrix<R,C,T>& rhs);
matrix<R,C,T>& copyFrom(const T* src) { for (unsigned int i = 0; i < R*C; ++i) { (*this).at(i) = src[i]; } return *this; }
matrix<R,C,T> operator * (const matrix<R,C,T>& rhs) const {
return mul(*this,rhs);
}
matrix<C,R,T>
reshape() const {
matrix<C,R,T> m;
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
m(r,c) = (*this)(c,r);
return m;
}
matrix<R,1,T> getColumn(unsigned int col) const {
matrix<R,1,T> c; for (unsigned int r = 0; r < R; ++r) c(r,0) = (this)(r,col);
return c;
}
matrix<1,C,T> getRow(unsigned int row) const {
matrix<1,C,T> r; for (unsigned int c = 0; c < C; ++c) r(0,c) = (this)(row,c);
return r;
}
void normalize();
};
/////////////////////////////////////////////////////////////////////////////
template <unsigned int aR, unsigned int aC, typename T>
inline matrix<aR,aC,T> operator * (const matrix<aR,aC,T>& a, const T& b)
{
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * b;
return res;
}
template <unsigned int aR, unsigned int aC, typename T>
inline matrix<aR,aC,T> operator / (const matrix<aR,aC,T>& a, const T& b)
{
matrix<aR,aC,T> res; T oneOverB(1./b);
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * oneOverB;
return res;
}
template <unsigned int aR, unsigned int aC, typename T>
inline matrix<aR,aC,T> operator + (const matrix<aR,aC,T>& a, const T& b)
{
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b;
return res;
}
template <unsigned int aR, unsigned int aC, typename T>
inline matrix<aR,aC,T> operator - (const matrix<aR,aC,T>& a, const T& b)
{
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b;
return res;
}
template <unsigned int R, unsigned int C, typename T>
inline matrix<R,C,T> operator + (const matrix<R,C,T>& a, const matrix<R,C,T>& b)
{
matrix<R,C,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b.at(i);
return res;
}
template <unsigned int R, unsigned int C, typename T>
inline matrix<R,C,T> operator - (const matrix<R,C,T>& a, const matrix<R,C,T>& b)
{
matrix<R,C,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b.at(i);
return res;
}
/////////////////////////////////////////////////////////////////////////////
template <unsigned int aR,unsigned int aCbR, unsigned int bC, typename T>
matrix<aR,bC,T> static inline
mul(const matrix<aR,aCbR,T>& A, const matrix<aCbR,bC,T>& B)
{
// aC == bR
// set all null
matrix<aR,bC,T> res = matrix<aR,bC,T>::All(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(r,c) += A(r,iI) * B(iI,c);
}
}
}
return res;
}
/////////////////////////////////////////////////////////////////////////////
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T>& matrix<R,C,T>::operator *= (const matrix& rhs)
{
*this = mul(*this,rhs);
return *this;
}
/////////////////////////////////////////////////////////////////////////////
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T>::matrix()
: matrixbase<T>(R,C,&m[0],true)
{
}
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T>::matrix(const matrix &mtc) :
matrixbase<T>(R,C,&m[0],true)
{
*this = mtc;
}
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T> &matrix<R,C,T>::operator = (const matrix<R,C,T> &other)
{
if (this != &other)
for (unsigned int r = 0; r < R;++r)
for (unsigned int c = 0; c < C;++c)
(*this).at(r,c) = other.at(r,c);
return *this;
}
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T> matrix<R,C,T>::transposed() const
{
matrix<C,R,T> res;
for (unsigned int r = this->rows();r-->0;)
for (unsigned int c = this->cols();c-->0;)
res.at(c,r) = this->at(r,c);
return res;
}
template <unsigned int R, unsigned int C,typename T>
void matrix<R,C,T>::get_minor(matrix<R-1,C-1,T>& res,unsigned int r0, unsigned int c0) const
{
unsigned int r = 0;
for (unsigned int ri = 0; ri < R; ri++)
{
unsigned int c = 0;
if (ri == r0) continue;
for (unsigned int ci = 0; ci < C; ci++)
{
if (ci == c0) continue;
res.data()[r*(C-1)+c] = this->data()[ri*C + ci];//(*this)(ri,ci);
c++;
}
r++;
}
}
template <unsigned int R, unsigned int C,typename T> inline
T matrix<R,C,T>::determinant() const
{
T res(0);
matrix<R-1,C-1,T> minor;
// using Laplace Expansion at compile time
for (unsigned int c = 0; c < C; c++) {
this->get_minor(minor,0,c);
res += ((c % 2 == 0) ? m[c] : -m[c]) * minor.determinant();
}
return res;
}
template <unsigned int R, unsigned int C,typename T> inline
T matrix<R,C,T>::squared_norm() const
{
T res(0);
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
res += ((*this)(r,c) * (*this)(r,c));
return res;
}
template <unsigned int R, unsigned int C,typename T> inline
T matrix<R,C,T>::norm() const
{
using std::sqrt;
return sqrt(this->squared_norm());
}
template <unsigned int R, unsigned int C,typename T> inline
void matrix<R,C,T>::normalize()
{
T n = norm();
if (n > 0) {
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
(*this)(r,c) /= n;
}
}
/////////////////////////////////////////////////////////////////////////////
// explicit specializations
// 4x4
template <typename T>
class matrix44 : public matrix<4,4,T>
{
public:
matrix44()
{
}
matrix44(const matrix<4,4,T>& i)
{
*this = i;
}
matrix44&
operator = (const matrix<4,4,T>& rhs)
{
if (this != &rhs){
this->at(0,0) = rhs.at(0,0);this->at(0,1) = rhs.at(0,1);this->at(0,2) = rhs.at(0,2);this->at(0,3) = rhs.at(0,3);
this->at(1,0) = rhs.at(1,0);this->at(1,1) = rhs.at(1,1);this->at(1,2) = rhs.at(1,2);this->at(1,3) = rhs.at(1,3);
this->at(2,0) = rhs.at(2,0);this->at(2,1) = rhs.at(2,1);this->at(2,2) = rhs.at(2,2);this->at(2,3) = rhs.at(2,3);
this->at(3,0) = rhs.at(3,0);this->at(3,1) = rhs.at(3,1);this->at(3,2) = rhs.at(3,2);this->at(3,3) = rhs.at(3,3);
}
return *this;
}
#if TACIT_PIXEL_STUFF_NEEDS_TO_MOVE_TO_SCENE
matrix<4,4,T>&
translate(const T& v1,const T& v2,const T& v3)
{
this->at(12) += v1;
this->at(13) += v2;
this->at(14) += v3;
return *this;
}
matrix<4,4,T>&
setTranslation(const T& v1,const T& v2,const T& v3)
{
this->identity();
this->at(12) = v1;
this->at(13) = v2;
this->at(14) = v3;
return *this;
}
matrix<4,4,T>&
setScale(const T& v1,const T& v2,const T& v3)
{
this->identity();
(*this)(0,0) = v1;
(*this)(1,1) = v2;
(*this)(2,2) = v3;
return *this;
}
matrix<4,4,T>&
scale(const T& v1,const T& v2,const T& v3)
{
(*this)(0,0) *= v1;
(*this)(1,1) *= v2;
(*this)(2,2) *= v3;
return *this;
}
static
matrix<4,4,T>
Translation(const T& v1,const T& v2,const T& v3)
{
matrix44<T> res = matrix44<T>::Identity(); res.setTranslation(v1,v2,v3);
return res;
}
inline static
matrix<4,4,T>
AngleAxis(const T& radianRotation,const matrix<3,1,T>& vec);
inline static
matrix<4,4,T> OrthogonalProjection(T Left, T Right,
T Bottom,T Top,
T Near, T Far)
{
matrix<4,4,T> ortho;
ortho.fill(0);
ortho(0,0) = 2 / (Right-Left);
ortho(1,1) = 2 / (Top-Bottom);
ortho(2,2) = -2 / (Far-Near);
ortho(0,3) = -(Right+Left)/(Right-Left);
ortho(1,3) = -(Top+Bottom)/(Top-Bottom);
ortho(2,3) = -(Far+Near)/(Far-Near);
ortho(3,3) = 1;
return ortho;
}
inline static
matrix<4,4,T> Frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
{
matrix<4,4,T> frustum;
frustum.fill(0);
frustum(0,0) = 2 * zNear/(Right-Left);
frustum(1,1) = 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) = -(2 * zFar*zNear)/(zFar-zNear); //D
frustum(2,3) = -1;
return frustum;
}
inline static
matrix<4,4,T> PerspectiveProjection(T fovY, T aspectRatio, T zNear, T zFar)
{
T height = zNear * tan(fovY/T(360) * Pi); // half height of near plane
T width = height * aspectRatio; // half width of near plane
return Frustum(-width, width, -height, height, zNear, zFar );
}
inline static
matrix<4,4,T> LookAt(const matrix<3,1,T>& eye,
const matrix<3,1,T>& target,
const matrix<3,1,T>& up);
matrix<4,4,T>&
rotate(const matrix<3,1,T>& vec, const T& rotation)
{
matrix44<T> rot = matrix44<T>::AngleAxis(rotation,vec); *this *= rot;
return *this;
}
#endif
};
//////////////////////////////////////////////////////////////////////////
//
// Specializations
//
template <> inline
float matrix<1,1,float>::determinant() const
{
return this->at(0);
}
template <> inline
double matrix<1,1,double>::determinant() const
{
return this->at(0);
}
#if TACIT_PIXEL_STUFF_NEEDS_TO_MOVE_TO_SCENE
template <typename T>
class matrix31 : public matrix<3,1,T> {
public:
using matrix<3,1,T>::operator =;
inline static
matrix<3,1,T> Cross(const matrix<3,1,T>& vec1, const matrix<3,1,T>& vec2)
{
matrix<3,1,T> res;
res.at(0) = vec1.at(1) * vec2.at(2) - vec2.at(1) * vec1.at(2);
res.at(1) = vec1.at(2) * vec2.at(0) - vec2.at(2) * vec1.at(0);
res.at(2) = vec1.at(0) * vec2.at(1) - vec2.at(0) * vec1.at(1);
return res;
}
};
template <typename T>
matrix<4,4,T>
matrix44<T>::AngleAxis(const T &radianRotation, const matrix<3,1,T> &vec)
{
matrix44<T> R = matrix44<T>::Identity();
if (vec.norm() < std::numeric_limits<T>::epsilon()) return R;
T _fCos = (T) cos (radianRotation);
matrix<3,1,T> _vCos(vec * (1 - _fCos));
matrix<3,1,T> _vSin(vec * (T)sin(radianRotation));
R.at(0) = (T) ((vec(0,0) * _vCos(0,0)) + _fCos);
R.at(4) = (T) ((vec(0,0) * _vCos(1,0)) - _vSin(2,0));
R.at(8) = (T) ((vec(0,0) * _vCos(2,0)) + _vSin(1,0));
R.at(1) = (T) ((vec(1,0) * _vCos(0,0)) + _vSin(2,0));
R.at(5) = (T) ((vec(1,0) * _vCos(1,0)) + _fCos);
R.at(9) = (T) ((vec(1,0) * _vCos(2,0)) - _vSin(0,0));
R.at(2) = (T) ((vec(2,0) * _vCos(0,0)) - _vSin(1,0));
R.at(6) = (T) ((vec(2,0) * _vCos(1,0)) + _vSin(0,0));
R.at(10)= (T) ((vec(2,0) * _vCos(2,0)) + _fCos);
return R;
}
template <typename T>
matrix<4,4,T>
matrix44<T>::LookAt(const matrix<3,1,T> &eye, const matrix<3,1,T> &target, const matrix<3,1,T> &up)
{
matrix<4,4,T> lookat = matrix<4,4,T>::Identity();
matrix<3,1,T> L; L = target - eye;
L.normalize();
matrix<3,1,T> S = matrix31<T>::Cross(L,up);
S.normalize();
matrix<3,1,T> Ud = matrix31<T>::Cross(S,L);
Ud.normalize();
lookat(0,0) = S.at(0);
lookat(0,1) = S.at(1);
lookat(0,2) = S.at(2);
lookat(0,3) = T(0);
lookat(1,0) = Ud.at(0);
lookat(1,1) = Ud.at(1);
lookat(1,2) = Ud.at(2);
lookat(1,3) = T(0);
lookat(2,0) = -L.at(0);
lookat(2,1) = -L.at(1);
lookat(2,2) = -L.at(2);
lookat(3,2) = T(0);
lookat(3,0) = eye.at(0);
lookat(3,1) = eye.at(1);
lookat(3,2) = eye.at(2);
lookat(3,3) = 1;
return lookat;
}
#endif
// predefined matricies
typedef matrix44<double> matrix44d;
typedef matrix44<float> matrix44f;
}
#endif

View file

@ -0,0 +1,130 @@
/*
* Copyright (C) 1999-2017 Hartmut Seichter
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PW_CORE_MATRIXBASE_HPP
#define PW_CORE_MATRIXBASE_HPP
namespace pw {
/**
* \brief base class for all matrix and vector operations
*/
template <typename T> class matrixbase {
protected:
T* _data;
int _data_offset;
int _cols;
int _rows;
int _row_stride;
int _col_stride;
typedef T value_type;
public:
//! assignment constructor
explicit matrixbase(int rows, int cols,T* ptr,bool row_major,int data_offset = 0)
: _data(ptr)
, _data_offset(data_offset)
, _rows(rows)
, _cols(cols)
, _row_stride(row_major ? 1 : cols)
, _col_stride(row_major ? rows : 1)
{
}
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; }
//! return number of rows
inline unsigned int rows() const { return _rows; }
//! return number of columns
inline unsigned int cols() const { return _cols; }
//! get cell count
inline unsigned int cells() const { return _rows * _cols; }
//! get data
inline T* data() { return _data + _data_offset; }
//! get data
inline const T* data() const { return _data + _data_offset; }
//! get item at index
inline T& at(unsigned int idx) { return data()[idx]; }
//! get item at index
inline const T& at(unsigned int idx) const { return data()[idx]; }
//! get item at position
inline T& at(unsigned int r,unsigned int c) { return data()[r * _row_stride + c * _col_stride]; }
//! get item at position
inline const T& at(unsigned int r,unsigned int c) const { return data()[r * _row_stride + c * _col_stride]; }
//! fill data
inline matrixbase& fill(const T& val) {
for (unsigned int i = 0; i < this->cells(); ++i) this->at(i) = val; return *this;
}
//! set identity
inline matrixbase& set_identity() {
for (unsigned int r = 0;r < rows(); r++)
for (unsigned int c = r; c < cols(); c++)
this->at(r,c) = (c == r) ? 1 : 0;
return *this;
}
//! set strides for row and column
inline void set_stride(int row_stride,int col_stride) {
_row_stride = row_stride; _col_stride = col_stride;
}
//! return row stride (elements between consequtive rows)
inline int row_stride() const { return _row_stride; }
//! return column stride (elements between consequtive columns)
inline int col_stride() const { return _col_stride; }
//! get row major
inline bool is_row_major() const { return row_stride() > col_stride(); }
//! check if squared
inline bool is_square() const { return (_rows == _cols); }
//! flip row/column order
inline void flip_order() { set_stride(col_stride(),row_stride()); }
//! get the offset for data in the matrix
inline int get_data_offset(int r,int c) const { return (r * _row_stride + c * _col_stride) + _data_offset; }
};
}
#endif

View file

@ -0,0 +1,207 @@
/*
* Copyright (C) 1999-2017 Hartmut Seichter
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef TP_CORE_QUATERNION_HPP
#define TP_CORE_QUATERNION_HPP
#include <pw/core/vector.hpp>
#include <pw/core/matrix.hpp>
namespace pw {
/**
* simplified quaternion class
*/
template <typename T>
class quaternion {
static constexpr T _sqrt90 = noexcept(std::sqrt(0.5));
protected:
vector4<T> _q;
public:
typedef T value_type;
quaternion() { *this = identity(); }
quaternion(const T& x,const T& y,const T& z,const T& w) {
this->set(x,y,z,w);
}
inline
void set(const T& x,const T& y,const T& z,const T& w) {
_q.set(x,y,z,w);
}
inline const vector4<T> as_vector() const { return _q; }
inline T& x() { return _q.x(); }
inline T& y() { return _q.y(); }
inline T& z() { return _q.z(); }
inline T& w() { return _q.w(); }
inline const T& x() const { return _q.x(); }
inline const T& y() const { return _q.y(); }
inline const T& z() const { return _q.z(); }
inline const T& w() const { return _q.w(); }
//! conversion from a matrix
inline static const quaternion from_matrix(const matrix<4,4,T> &m);
//! conversion to a matrix
const matrix<4,4,T> to_matrix() const;
//! return identiy quaternion
static quaternion<T> identity();
static quaternion<T> rotate_180_degree_around_x(); ///< rotate 180 degree around X axis
static quaternion<T> rotate_180_degree_around_y(); ///< rotate 180 degree around Y axis
static quaternion<T> rotate_180_degree_around_z(); ///< rotate 180 degree around Z axis
static quaternion<T> rotate_90_degree_around_x(bool negative = false);
static quaternion<T> rotate_90_degree_around_y(bool negative = false);
static quaternion<T> rotate_90_degree_around_z(bool negative = false);
template <typename AxisAngleType>
static const quaternion<T> from_axisangle(const AxisAngleType &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() * 0.5) // w
);
}
};
template <typename T>
const quaternion<T> quaternion<T>::from_matrix(const matrix<4,4,T> &m) {
using std::sqrt;
T wtemp = sqrt(T(1) + m.at(0,0) + m.at(1,1) + m.at(2,2)) / T(2.0);
const T w4 = T(4.0) * wtemp;
return quaternion<T>(
(m.at(2,1) - m.at(1,2)) / w4,
(m.at(0,2) - m.at(2,0)) / w4,
(m.at(1,0) - m.at(0,1)) / w4,
wtemp);
}
template <typename T>
const matrix<4,4,T> quaternion<T>::to_matrix() const {
matrix<4,4,T> m; m.set_identity();
T xx = x() * x();
T xy = x() * y();
T xz = x() * z();
T xw = x() * w();
T yy = y() * y();
T yz = y() * z();
T yw = y() * w();
T zz = z() * z();
T zw = z() * w();
m.at(0,0) = 1 - 2 * ( yy + zz );
m.at(0,1) = 2 * ( xy - zw );
m.at(0,2) = 2 * ( xz + yw );
m.at(1,0) = 2 * ( xy + zw );
m.at(1,1) = 1 - 2 * ( xx + zz );
m.at(1,2) = 2 * ( yz - xw );
m.at(2,0) = 2 * ( xz - yw );
m.at(2,1) = 2 * ( yz + xw );
m.at(2,2) = 1 - 2 * ( xx + yy );
return m;
}
template <typename T>
quaternion<T> quaternion<T>::identity()
{
return quaternion<T>(0,0,0,1);
}
template <typename T>
quaternion<T> quaternion<T>::rotate_180_degree_around_x()
{
return quaternion<T>(1,0,0,0);
}
template <typename T>
quaternion<T> quaternion<T>::rotate_180_degree_around_y()
{
return quaternion<T>(0,1,0,0);
}
template <typename T>
quaternion<T> quaternion<T>::rotate_180_degree_around_z()
{
return quaternion<T>(0,0,1,0);
}
template <typename T>
quaternion<T> quaternion<T>::rotate_90_degree_around_x(bool negative/* = false*/)
{
return quaternion<T>((negative) ? -_sqrt90 : _sqrt90,0,0,_sqrt90);
}
template <typename T>
quaternion<T> quaternion<T>::rotate_90_degree_around_y(bool negative/* = false*/)
{
return quaternion<T>(0, (negative) ? -_sqrt90 : _sqrt90,0,_sqrt90);
}
template <typename T>
quaternion<T> quaternion<T>::rotate_90_degree_around_z(bool negative/* = false*/)
{
return quaternion<T>(0,0,(negative) ? -_sqrt90 : _sqrt90, _sqrt90);
}
//
//
//
typedef quaternion<float> quaternionf;
typedef quaternion<double> quaterniond;
}
#endif

View file

@ -1,51 +0,0 @@
#ifndef PW_SCRIPT_HPP
#define PW_SCRIPT_HPP
#include <lua.hpp>
#include <memory>
#include <pw/core/scripting.hpp>
namespace pw {
class script;
class main_loop {
script &_s;
bool _stop = false;
main_loop(script& s);
};
class script {
protected:
scripting::state _lua;
public:
script();
~script();
bool update();
bool load(const std::string &s);
int run(const std::string& s);
void load(sol::table &ns);
friend class main_loop;
};
}
#endif

View file

@ -1,13 +0,0 @@
#ifndef PW_SCRIPTING_HPP
#define PW_SCRIPTING_HPP
#include "sol.hpp"
namespace pw {
// include external namespace of sol
namespace scripting = sol;
}
#endif

View file

@ -0,0 +1,31 @@
#ifndef PW_CORE_SERIALIZE_HPP
#define PW_CORE_SERIALIZE_HPP
#include <pw/core/matrixbase.hpp>
#include <string>
#include <sstream>
namespace pw {
struct serialize {
template <typename T>
inline static std::string matrix(const matrixbase<T>& m) {
std::stringstream ss;
for (int r = 0; r < m.rows();r++) {
for (int c = 0; c < m.cols();c++) {
ss << m.at(r,c) << " ";
}
ss << std::endl;
}
return ss.str();
}
};
}
#endif

View file

@ -0,0 +1,220 @@
/*
* Copyright (C) 1999-2017 Hartmut Seichter
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PW_CORE_VECTOR_HPP
#define PW_CORE_VECTOR_HPP
#include <pw/core/matrix.hpp>
namespace pw {
template <unsigned int components,typename T>
class vector : public matrix<components,1,T> {
public:
vector() : matrix<components,1,T>() {}
vector(const vector& other) : matrix<components,1,T>(other) {}
vector(const matrix<components,1,T>& other) : matrix<components,1,T>(other) {}
T& operator()(unsigned int c) { return matrixbase<T>::at(c); }
const T& operator()(unsigned int c) const { return matrixbase<T>::at(c); }
T dot(const vector<components,T>& other) const
{
T res = 0;
for (unsigned int i = 0; i < components; i++) res += (*this)(i) * other(i);
return res;
}
#if OLD_TACITPIXEL
T getAngle(const vector<components,T>& other) const
{
vector<components,T> nself(*this); vector<components,T> nothr = other;
nself.normalize(); nothr.normalize();
return acos( nothr.dot(nself) );
}
#endif
};
// Vec3 -----------------------------------------------------------------------
template <class T>
class vector3 : public vector<3,T>
{
public:
using vector<3,T>::operator=;
vector3() : vector<3,T>() {}
vector3(const vector<3,T>& other) : matrix<3,1,T>(other) {}
vector3(T c1, T c2, T c3) { this->set(c1,c2,c3); }
void set(T c1, T c2, T c3) { (*this)(0) = c1; (*this)(1) = c2; (*this)(2) = c3; }
inline void set_x(const T& v) { x() = v; }
inline void set_y(const T& v) { y() = v; }
inline void set_z(const T& v) { z() = v; }
inline const vector3 cross(const vector3<T>& vec2) const {
return vector3<T>((*this)(1) * vec2(2) - vec2(1) * (*this)(2),
(*this)(2) * vec2(0) - vec2(2) * (*this)(0),
(*this)(0) * vec2(1) - vec2(0) * (*this)(1));
}
inline const T& x() const { return (*this)(0); }
inline T& x() { return (*this)(0); }
const T& y() const { return (*this)(1); }
T& y() { return (*this)(1); }
const T& z() const { return (*this)(2); }
T& z() { return (*this)(2); }
const vector<4,T> project(const T& w) const {
return vector<4,T>(x(),y(),z(),w);
}
inline std::tuple<T,T,T> values() const {
return std::make_tuple(x(),y(),z());
}
};
// Vec2x -----------------------------------------------------------------------
template <class T> class vector2 : public vector<2,T>
{
public:
vector2() {}
vector2(T v1,T v2) {
this->set(v1,v2);
}
void set(T v1,T v2) {
(*this)(0) = v1; (*this)(1) = v2;
}
const T& x() const { return (*this)(0); }
T& x() { return (*this)(0); }
const T& y() const { return (*this)(1); }
T& y() { return (*this)(1); }
};
// Vec4 -----------------------------------------------------------------------
template <typename T> class vector4 : public vector<4,T> {
public:
vector4() {}
vector4(const vector<3,T>& rv,T pad = T(0)) {
this->set(rv(0),rv(1),rv(2),pad);
}
vector4(const T& v1,const T& v2,const T& v3,const T& v4) {
this->set(v1,v2,v3,v4);
}
void set(const T& v1,const T& v2,const T& v3,const T& v4) {
(*this)(0) = v1;
(*this)(1) = v2;
(*this)(2) = v3;
(*this)(3) = v4;
}
inline const vector3<T> xyz() const { return vector3<T>(x(),y(),z()); }
inline const vector2<T> xy() const { return vector2<T>(x(),y()); }
const T& x() const { return (*this)(0); }
T& x() { return (*this)(0); }
const T& y() const { return (*this)(1); }
T& y() { return (*this)(1); }
const T& z() const { return (*this)(2); }
T& z() { return (*this)(2); }
const T& w() const { return (*this)(3); }
T& w() { return (*this)(3); }
};
typedef vector2<unsigned char> vector2ub;
typedef vector2<char> vector2b;
typedef vector2<unsigned short> vector2us;
typedef vector2<short> vector2s;
typedef vector2<unsigned int> vector2ui;
typedef vector2<int> vector2i;
typedef vector2<unsigned long> vector2ul;
typedef vector2<long> vector2l;
typedef vector2<double> vector2d;
typedef vector2<float> vector2f;
typedef vector3<double> vector3d;
typedef vector3<float> vector3f;
typedef vector3<int> vector3i;
typedef vector3<unsigned int> vector3ui;
typedef vector4<double> vector4d;
typedef vector4<float> vector4f;
typedef vector4<int> vector4i;
typedef vector4<unsigned int> vector4ui;
}
//////////////////////////////////////////////////////////////////////////
#endif

View file

@ -1,43 +0,0 @@
#ifndef PW_WINDOW_HPP
#define PW_WINDOW_HPP
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "sol.hpp"
namespace pw {
class window;
class context {
GLFWwindow* _window;
public:
context(window& other);
~context();
};
class window {
GLFWwindow* _window;
public:
window();
~window();
bool update();
void set_title(const char* t);
void set_size(int w, int h);
context* get_context();
static void load(sol::table &ns);
};
}
#endif

View file

@ -0,0 +1,38 @@
set(hdrs
# ../include/pw/core/context.hpp
../include/pw/core/axisangle.hpp
../include/pw/core/core.hpp
# ../include/pw/core/script.hpp
# ../include/pw/core/scripting.hpp
../include/pw/core/math.hpp
../include/pw/core/matrixbase.hpp
../include/pw/core/matrix.hpp
../include/pw/core/vector.hpp
../include/pw/core/quaternion.hpp
../include/pw/core/serialize.hpp
../include/pw/core/image.hpp
../include/pw/core/globals.hpp
)
set(srcs
# script.cpp
# context.cpp
core.cpp
serialize.cpp
)
add_library(pwcore
STATIC
${hdrs}
${srcs}
)
target_include_directories(
pwcore
PUBLIC
../include
)
target_link_libraries(pwcore)

View file

@ -1,8 +1,24 @@
#include <GLFW/glfw3.h>
//#include <GLFW/glfw3.h>
#include <lua.hpp>
#include <lualib.h>
#include "core.hpp"
#include "pw/core/matrixbase.hpp"
#include "pw/core/matrix.hpp"
void test_matrixbase() {
using namespace pw;
matrix44f m;
m.set_identity();
}

0
src/core/src/image.cpp Normal file
View file

View file

@ -1,35 +0,0 @@
#include "script.hpp"
#include "window.hpp"
pw::script::script()
{
// open all libraries
_lua.open_libraries();
// load pw namespace
sol::table pw = _lua.create_named_table("pw");
window::load(pw);
}
pw::script::~script()
{
}
void pw::script::run(const char *s)
{
sol::function_result res = _lua.script(s);
// res.status() == sol::f
// _lua.script("w = pw.window.new()");
// _lua.script("w:set_title(\"pixwerks alpha\")");
// _lua.script("w:set_size(320,240)");
// _lua.script("while w:update() do end");
// luaL_loadstring(_state,s);
// int r = lua_pcall(_state,0,LUA_MULTRET,0);
}

View file

@ -0,0 +1,2 @@
#include "pw/core/serialize.hpp"

View file

@ -1,54 +0,0 @@
#include "window.hpp"
#include "glad/glad.h"
pw::window::window() {
_window = glfwCreateWindow(640, 480, "pixwerxs", NULL, NULL);
glfwMakeContextCurrent(_window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
}
pw::window::~window() {
glfwDestroyWindow(_window);
}
bool pw::window::update()
{
if (!glfwWindowShouldClose(_window)) {
// do other stuff
glfwSwapBuffers(_window);
glfwPollEvents();
return true;
}
return false;
}
void pw::window::set_title(const char *t)
{
glfwSetWindowTitle(_window,t);
}
void pw::window::set_size(int w,int h) {
glfwSetWindowSize(_window,w,h);
}
void pw::window::load(sol::table &ns)
{
glfwInit();
ns.new_usertype<window>("window",
"update",&window::update,
"set_title",&window::set_title,
"set_size",&window::set_size
);
}

View file

@ -0,0 +1,29 @@
add_executable(pwcore_test_matrix
pwcore_test_matrix.cpp
)
target_link_libraries(pwcore_test_matrix
pwcore)
add_executable(pwcore_test_vector
pwcore_test_vector.cpp
)
target_link_libraries(pwcore_test_vector
pwcore)
add_executable(pwcore_test_quaternion
pwcore_test_quaternion.cpp
)
target_link_libraries(pwcore_test_quaternion
pwcore)
add_executable(pwcore_test_axisangle
pwcore_test_axisangle.cpp
)
target_link_libraries(pwcore_test_axisangle
pwcore)

View file

@ -0,0 +1,19 @@
#include <pw/core/axisangle.hpp>
#include <pw/core/quaternion.hpp>
#include <pw/core/serialize.hpp>
#include <iostream>
int main(int argc,char **argv) {
pw::axisangle<float> aa = pw::axisangle<float>();
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;
return 0;
}

View file

@ -0,0 +1,38 @@
#include <pw/core/matrix.hpp>
#include <pw/core/serialize.hpp>
#include <iostream>
int main(int argc,char **argv) {
pw::matrix44d m;
m.set_identity();
std::cout << "m = " << pw::serialize::matrix(m) << std::endl;
std::cout << "row_stride() : " << m.row_stride() << std::endl;
std::cout << "col_stride() : " << m.col_stride() << std::endl;
std::cout << "rows() : " << m.rows() << std::endl;
std::cout << "cols() : " << m.cols() << std::endl;
std::cout << "data() : " << m.data() << std::endl;
std::cout << "data()[0] : " << m.data()[0] << std::endl;
std::cout << "at(0,0) : " << m.at(0,0) << std::endl;
std::cout << "determinant(): " << m.determinant() << std::endl;
pw::matrix44d mi = m.get_inverse();
std::cout << "mi.at(0,0) : " << mi.at(0,0) << std::endl;
pw::matrix44d mscale = m * 4.2;
std::cout << "mscale = " << pw::serialize::matrix(mscale) << std::endl;
return 0;
}

View file

@ -0,0 +1,15 @@
#include <pw/core/quaternion.hpp>
#include <pw/core/serialize.hpp>
#include <iostream>
int main(int argc,char **argv) {
pw::quaternion<float> qf = pw::quaternionf::rotate_180_degree_around_x();
std::cout << "qf = " << pw::serialize::matrix(qf.as_vector()) << std::endl;
std::cout << "qf.matrix() = " << pw::serialize::matrix(qf.to_matrix()) << std::endl;
return 0;
}

View file

@ -0,0 +1,29 @@
#include <pw/core/vector.hpp>
#include <pw/core/serialize.hpp>
#include <iostream>
int main(int argc,char **argv) {
pw::vector4<float> v4;
v4.fill(1.5);
std::cout << "v4 = " << pw::serialize::matrix(v4) << std::endl;
std::cout << "row_stride() : " << v4.row_stride() << std::endl;
std::cout << "col_stride() : " << v4.col_stride() << std::endl;
std::cout << "rows() : " << v4.rows() << std::endl;
std::cout << "cols() : " << v4.cols() << std::endl;
std::cout << "data() : " << v4.data() << std::endl;
std::cout << "data()[0] : " << v4.data()[0] << std::endl;
std::cout << "at(0,0) : " << v4.at(0,0) << std::endl;
pw::vector3f v3 = v4.xyz();
std::cout << "v3 = " << pw::serialize::matrix(v3) << std::endl;
return 0;
}