update on design of scenegraph
This commit is contained in:
parent
f7043fc0cb
commit
62b2302a32
18 changed files with 342 additions and 136 deletions
|
@ -4,10 +4,13 @@
|
|||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace pw {
|
||||
|
||||
using std::shared_ptr;
|
||||
using std::unique_ptr;
|
||||
using std::weak_ptr;
|
||||
|
||||
typedef float real_t;
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
namespace pw {
|
||||
|
||||
template <unsigned int R, unsigned int C, typename T>
|
||||
class matrix : public matrixbase<T> {
|
||||
class matrix_ : public matrixbase<T> {
|
||||
|
||||
T m[R*C];
|
||||
|
||||
|
@ -44,37 +44,37 @@ public:
|
|||
using typename matrixbase<T>::value_type;
|
||||
using typename matrixbase<T>::size_type;
|
||||
|
||||
matrix();
|
||||
matrix_();
|
||||
|
||||
matrix(const matrix& mtc);
|
||||
matrix_(const matrix_& mtc);
|
||||
|
||||
matrix& operator = (const matrix& other);
|
||||
matrix_& operator = (const matrix_& other);
|
||||
|
||||
matrix transposed() const;
|
||||
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 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 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 normalized() const {
|
||||
inline const matrix_ normalized() const {
|
||||
const T one_over_n = T(1) / this->norm();
|
||||
return *this * one_over_n;
|
||||
}
|
||||
|
||||
|
||||
inline const matrix
|
||||
inline const matrix_
|
||||
get_inverse() const
|
||||
{
|
||||
matrix resMat;
|
||||
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;
|
||||
matrix_<R-1,C-1,T> minor;
|
||||
this->get_minor(minor,r,j);
|
||||
resMat.at(r,j) = minor.determinant() * sgn;
|
||||
}
|
||||
|
@ -86,12 +86,12 @@ public:
|
|||
}
|
||||
|
||||
inline
|
||||
matrix& invert() {
|
||||
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;
|
||||
void get_minor(matrix_<R-1,C-1,T>& res, unsigned int r0, unsigned int c0) const;
|
||||
|
||||
T determinant() const;
|
||||
|
||||
|
@ -99,29 +99,29 @@ public:
|
|||
|
||||
T norm() const;
|
||||
|
||||
matrix<R,C,T>& operator *= (const matrix<R,C,T>& rhs);
|
||||
matrix_<R,C,T>& operator *= (const matrix_<R,C,T>& rhs);
|
||||
|
||||
matrix<R,C,T>& copy_from_data(const T* src) { for (unsigned int i = 0; i < R*C; ++i) { (*this).at(i) = src[i]; } return *this; }
|
||||
matrix_<R,C,T>& copy_from_data(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 {
|
||||
matrix_<R,C,T> operator * (const matrix_<R,C,T>& rhs) const {
|
||||
return mul(*this,rhs);
|
||||
}
|
||||
|
||||
const matrix<C,R,T> reshape() const {
|
||||
matrix<C,R,T> m;
|
||||
const 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;
|
||||
}
|
||||
|
||||
const matrix<R,1,T> get_column(unsigned int col) const {
|
||||
matrix<R,1,T> c; for (unsigned int r = 0; r < R; ++r) c(r,0) = (this)(r,col);
|
||||
const matrix_<R,1,T> get_column(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;
|
||||
}
|
||||
|
||||
const matrix<1,C,T> get_row(unsigned int row) const {
|
||||
matrix<1,C,T> r; for (unsigned int c = 0; c < C; ++c) r(0,c) = (this)(row,c);
|
||||
const matrix_<1,C,T> get_row(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;
|
||||
}
|
||||
|
||||
|
@ -131,49 +131,49 @@ public:
|
|||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <unsigned int aR, unsigned int aC, typename T>
|
||||
inline matrix<aR,aC,T> operator * (const matrix<aR,aC,T>& a, const T& b)
|
||||
inline matrix_<aR,aC,T> operator * (const matrix_<aR,aC,T>& a, const T& b)
|
||||
{
|
||||
matrix<aR,aC,T> res;
|
||||
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)
|
||||
inline matrix_<aR,aC,T> operator / (const matrix_<aR,aC,T>& a, const T& b)
|
||||
{
|
||||
matrix<aR,aC,T> res; T oneOverB(1./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)
|
||||
inline matrix_<aR,aC,T> operator + (const matrix_<aR,aC,T>& a, const T& b)
|
||||
{
|
||||
matrix<aR,aC,T> res;
|
||||
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)
|
||||
inline matrix_<aR,aC,T> operator - (const matrix_<aR,aC,T>& a, const T& b)
|
||||
{
|
||||
matrix<aR,aC,T> res;
|
||||
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)
|
||||
inline matrix_<R,C,T> operator + (const matrix_<R,C,T>& a, const matrix_<R,C,T>& b)
|
||||
{
|
||||
matrix<R,C,T> res;
|
||||
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)
|
||||
inline matrix_<R,C,T> operator - (const matrix_<R,C,T>& a, const matrix_<R,C,T>& b)
|
||||
{
|
||||
matrix<R,C,T> res;
|
||||
matrix_<R,C,T> res;
|
||||
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b.at(i);
|
||||
return res;
|
||||
}
|
||||
|
@ -181,12 +181,12 @@ inline matrix<R,C,T> operator - (const matrix<R,C,T>& a, const matrix<R,C,T>& b)
|
|||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
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)
|
||||
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> res;
|
||||
res.fill(0);
|
||||
|
||||
// compute all resulting cells
|
||||
|
@ -206,7 +206,7 @@ mul(const matrix<aR,aCbR,T>& A, const matrix<aCbR,bC,T>& B)
|
|||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <unsigned int R, unsigned int C,typename T>
|
||||
matrix<R,C,T>& matrix<R,C,T>::operator *= (const matrix& rhs)
|
||||
matrix_<R,C,T>& matrix_<R,C,T>::operator *= (const matrix_& rhs)
|
||||
{
|
||||
*this = mul(*this,rhs);
|
||||
return *this;
|
||||
|
@ -215,20 +215,20 @@ matrix<R,C,T>& matrix<R,C,T>::operator *= (const matrix& rhs)
|
|||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <unsigned int R, unsigned int C,typename T>
|
||||
matrix<R,C,T>::matrix()
|
||||
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) :
|
||||
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)
|
||||
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)
|
||||
|
@ -238,9 +238,9 @@ matrix<R,C,T> &matrix<R,C,T>::operator = (const matrix<R,C,T> &other)
|
|||
}
|
||||
|
||||
template <unsigned int R, unsigned int C,typename T>
|
||||
matrix<R,C,T> matrix<R,C,T>::transposed() const
|
||||
matrix_<R,C,T> matrix_<R,C,T>::transposed() const
|
||||
{
|
||||
matrix<C,R,T> res;
|
||||
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);
|
||||
|
@ -248,7 +248,7 @@ matrix<R,C,T> matrix<R,C,T>::transposed() const
|
|||
}
|
||||
|
||||
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
|
||||
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++)
|
||||
|
@ -266,11 +266,11 @@ void matrix<R,C,T>::get_minor(matrix<R-1,C-1,T>& res,unsigned int r0, unsigned i
|
|||
}
|
||||
|
||||
template <unsigned int R, unsigned int C,typename T> inline
|
||||
T matrix<R,C,T>::determinant() const
|
||||
T matrix_<R,C,T>::determinant() const
|
||||
{
|
||||
T res(0);
|
||||
|
||||
matrix<R-1,C-1,T> minor;
|
||||
matrix_<R-1,C-1,T> minor;
|
||||
|
||||
// using Laplace Expansion at compile time
|
||||
for (unsigned int c = 0; c < C; c++) {
|
||||
|
@ -282,7 +282,7 @@ T matrix<R,C,T>::determinant() const
|
|||
}
|
||||
|
||||
template <unsigned int R, unsigned int C,typename T> inline
|
||||
T matrix<R,C,T>::squared_norm() const
|
||||
T matrix_<R,C,T>::squared_norm() const
|
||||
{
|
||||
T res(0);
|
||||
|
||||
|
@ -294,7 +294,7 @@ T matrix<R,C,T>::squared_norm() const
|
|||
|
||||
|
||||
template <unsigned int R, unsigned int C,typename T> inline
|
||||
T matrix<R,C,T>::norm() const
|
||||
T matrix_<R,C,T>::norm() const
|
||||
{
|
||||
using std::sqrt;
|
||||
|
||||
|
@ -302,7 +302,7 @@ T matrix<R,C,T>::norm() const
|
|||
}
|
||||
|
||||
template <unsigned int R, unsigned int C,typename T> inline
|
||||
void matrix<R,C,T>::normalize()
|
||||
void matrix_<R,C,T>::normalize()
|
||||
{
|
||||
T n = norm();
|
||||
if (n > 0) {
|
||||
|
@ -319,19 +319,19 @@ void matrix<R,C,T>::normalize()
|
|||
// 4x4
|
||||
|
||||
template <typename T>
|
||||
class matrix44 : public matrix<4,4,T>
|
||||
class matrix44 : public matrix_<4,4,T>
|
||||
{
|
||||
public:
|
||||
|
||||
using matrix<4,4,T>::matrix;
|
||||
using matrix_<4,4,T>::matrix_;
|
||||
|
||||
|
||||
matrix44(const matrix<4,4,T>& i)
|
||||
matrix44(const matrix_<4,4,T>& i)
|
||||
{
|
||||
*this = i;
|
||||
}
|
||||
|
||||
matrix44& operator = (const matrix<4,4,T>& rhs) {
|
||||
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);
|
||||
|
@ -343,9 +343,9 @@ public:
|
|||
}
|
||||
|
||||
inline static
|
||||
matrix<4,4,T> projection_from_frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
|
||||
matrix_<4,4,T> projection_from_frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
|
||||
{
|
||||
matrix<4,4,T> frustum;
|
||||
matrix_<4,4,T> frustum;
|
||||
|
||||
frustum.fill(0);
|
||||
|
||||
|
@ -363,12 +363,12 @@ public:
|
|||
}
|
||||
|
||||
inline static
|
||||
matrix<4,4,T> orthogonal_projection(T Left, T Right,
|
||||
matrix_<4,4,T> orthogonal_projection(T Left, T Right,
|
||||
T Bottom,T Top,
|
||||
T Near, T Far)
|
||||
{
|
||||
|
||||
matrix<4,4,T> ortho;
|
||||
matrix_<4,4,T> ortho;
|
||||
|
||||
ortho.fill(0);
|
||||
ortho(0,0) = 2 / (Right-Left);
|
||||
|
@ -386,7 +386,7 @@ public:
|
|||
|
||||
|
||||
inline static
|
||||
matrix<4,4,T> perspective_projection(T fovY, T aspectRatio, T zNear, T zFar)
|
||||
matrix_<4,4,T> perspective_projection(T fovY, T aspectRatio, T zNear, T zFar)
|
||||
{
|
||||
const T height = zNear * tan(fovY/T(360) * pi<T>()); // half height of near plane
|
||||
const T width = height * aspectRatio; // half width of near plane
|
||||
|
@ -496,13 +496,13 @@ public:
|
|||
//
|
||||
|
||||
template <> inline
|
||||
float matrix<1,1,float>::determinant() const
|
||||
float matrix_<1,1,float>::determinant() const
|
||||
{
|
||||
return this->at(0);
|
||||
}
|
||||
|
||||
template <> inline
|
||||
double matrix<1,1,double>::determinant() const
|
||||
double matrix_<1,1,double>::determinant() const
|
||||
{
|
||||
return this->at(0);
|
||||
}
|
||||
|
|
|
@ -126,10 +126,10 @@ public:
|
|||
inline void normalize() { *this = this->normalized(); }
|
||||
|
||||
//! conversion from a matrix
|
||||
inline static const quaternion from_matrix(const matrix<4,4,T> &m);
|
||||
inline static const quaternion from_matrix(const matrix_<4,4,T> &m);
|
||||
|
||||
//! conversion to a matrix
|
||||
const matrix<4,4,T> to_matrix() const;
|
||||
const matrix_<4,4,T> to_matrix() const;
|
||||
|
||||
//! return identiy quaternion
|
||||
static const quaternion<T> identity();
|
||||
|
@ -173,7 +173,7 @@ const T quaternion<T>::_sqrt90 = std::sqrt(0.5);
|
|||
|
||||
|
||||
template <typename T>
|
||||
const quaternion<T> quaternion<T>::from_matrix(const matrix<4,4,T> &m) {
|
||||
const quaternion<T> quaternion<T>::from_matrix(const matrix_<4,4,T> &m) {
|
||||
|
||||
using std::sqrt;
|
||||
|
||||
|
@ -188,9 +188,9 @@ const quaternion<T> quaternion<T>::from_matrix(const matrix<4,4,T> &m) {
|
|||
}
|
||||
|
||||
template <typename T>
|
||||
const matrix<4,4,T> quaternion<T>::to_matrix() const {
|
||||
const matrix_<4,4,T> quaternion<T>::to_matrix() const {
|
||||
|
||||
matrix<4,4,T> m; m.set_identity();
|
||||
matrix_<4,4,T> m; m.set_identity();
|
||||
|
||||
T xx = x() * x();
|
||||
T xy = x() * y();
|
||||
|
|
|
@ -31,17 +31,17 @@
|
|||
namespace pw {
|
||||
|
||||
template <unsigned int components,typename T>
|
||||
class vector : public matrix<components,1,T> {
|
||||
class vector : public matrix_<components,1,T> {
|
||||
public:
|
||||
|
||||
using typename matrix<components,1,T>::value_type;
|
||||
using matrix<components,1,T>::operator = ;
|
||||
using typename matrix_<components,1,T>::value_type;
|
||||
using matrix_<components,1,T>::operator = ;
|
||||
|
||||
vector() : matrix<components,1,T>() {}
|
||||
vector() : matrix_<components,1,T>() {}
|
||||
|
||||
vector(const vector& other) : matrix<components,1,T>(other) {}
|
||||
vector(const vector& other) : matrix_<components,1,T>(other) {}
|
||||
|
||||
vector(const matrix<components,1,T>& 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); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue