update on design of scenegraph
This commit is contained in:
parent
f7043fc0cb
commit
62b2302a32
18 changed files with 342 additions and 136 deletions
Binary file not shown.
|
@ -4,10 +4,13 @@
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
using std::shared_ptr;
|
using std::shared_ptr;
|
||||||
|
using std::unique_ptr;
|
||||||
|
using std::weak_ptr;
|
||||||
|
|
||||||
typedef float real_t;
|
typedef float real_t;
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
template <unsigned int R, unsigned int C, typename T>
|
template <unsigned int R, unsigned int C, typename T>
|
||||||
class matrix : public matrixbase<T> {
|
class matrix_ : public matrixbase<T> {
|
||||||
|
|
||||||
T m[R*C];
|
T m[R*C];
|
||||||
|
|
||||||
|
@ -44,37 +44,37 @@ public:
|
||||||
using typename matrixbase<T>::value_type;
|
using typename matrixbase<T>::value_type;
|
||||||
using typename matrixbase<T>::size_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();
|
const T one_over_n = T(1) / this->norm();
|
||||||
return *this * one_over_n;
|
return *this * one_over_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline const matrix
|
inline const matrix_
|
||||||
get_inverse() const
|
get_inverse() const
|
||||||
{
|
{
|
||||||
matrix resMat;
|
matrix_ resMat;
|
||||||
|
|
||||||
for ( unsigned int r = 0; r < C; ++r) {
|
for ( unsigned int r = 0; r < C; ++r) {
|
||||||
for ( unsigned int j = 0; j < R; ++j) {
|
for ( unsigned int j = 0; j < R; ++j) {
|
||||||
short sgn = ( (r+j)%2) ? -1 : 1;
|
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);
|
this->get_minor(minor,r,j);
|
||||||
resMat.at(r,j) = minor.determinant() * sgn;
|
resMat.at(r,j) = minor.determinant() * sgn;
|
||||||
}
|
}
|
||||||
|
@ -86,12 +86,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
matrix& invert() {
|
matrix_& invert() {
|
||||||
*this = this->get_inverse();
|
*this = this->get_inverse();
|
||||||
return *this;
|
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;
|
T determinant() const;
|
||||||
|
|
||||||
|
@ -99,29 +99,29 @@ public:
|
||||||
|
|
||||||
T norm() const;
|
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);
|
return mul(*this,rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
const matrix<C,R,T> reshape() const {
|
const matrix_<C,R,T> reshape() const {
|
||||||
matrix<C,R,T> m;
|
matrix_<C,R,T> m;
|
||||||
for (unsigned int r = 0; r < R; ++r)
|
for (unsigned int r = 0; r < R; ++r)
|
||||||
for (unsigned int c = 0; c < C; ++c)
|
for (unsigned int c = 0; c < C; ++c)
|
||||||
m(r,c) = (*this)(c,r);
|
m(r,c) = (*this)(c,r);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
const matrix<R,1,T> get_column(unsigned int col) const {
|
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);
|
matrix_<R,1,T> c; for (unsigned int r = 0; r < R; ++r) c(r,0) = (this)(r,col);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
const matrix<1,C,T> get_row(unsigned int row) const {
|
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);
|
matrix_<1,C,T> r; for (unsigned int c = 0; c < C; ++c) r(0,c) = (this)(row,c);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,49 +131,49 @@ public:
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
template <unsigned int aR, unsigned int aC, typename T>
|
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;
|
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * b;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int aR, unsigned int aC, typename T>
|
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;
|
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * oneOverB;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int aR, unsigned int aC, typename T>
|
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;
|
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int aR, unsigned int aC, typename T>
|
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;
|
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int R, unsigned int C, typename T>
|
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);
|
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b.at(i);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int R, unsigned int C, typename T>
|
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);
|
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b.at(i);
|
||||||
return res;
|
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>
|
template <unsigned int aR,unsigned int aCbR, unsigned int bC, typename T>
|
||||||
matrix<aR,bC,T> static inline
|
matrix_<aR,bC,T> static inline
|
||||||
mul(const matrix<aR,aCbR,T>& A, const matrix<aCbR,bC,T>& B)
|
mul(const matrix_<aR,aCbR,T>& A, const matrix_<aCbR,bC,T>& B)
|
||||||
{
|
{
|
||||||
// aC == bR
|
// aC == bR
|
||||||
// set all null
|
// set all null
|
||||||
matrix<aR,bC,T> res;
|
matrix_<aR,bC,T> res;
|
||||||
res.fill(0);
|
res.fill(0);
|
||||||
|
|
||||||
// compute all resulting cells
|
// 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>
|
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);
|
*this = mul(*this,rhs);
|
||||||
return *this;
|
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>
|
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)
|
: matrixbase<T>(R,C,&m[0],true)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int R, unsigned int C,typename T>
|
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)
|
matrixbase<T>(R,C,&m[0],true)
|
||||||
{
|
{
|
||||||
*this = mtc;
|
*this = mtc;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int R, unsigned int C,typename T>
|
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)
|
if (this != &other)
|
||||||
for (unsigned int r = 0; r < R;++r)
|
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>
|
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 r = this->rows();r-->0;)
|
||||||
for (unsigned int c = this->cols();c-->0;)
|
for (unsigned int c = this->cols();c-->0;)
|
||||||
res.at(c,r) = this->at(r,c);
|
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>
|
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;
|
unsigned int r = 0;
|
||||||
for (unsigned int ri = 0; ri < R; ri++)
|
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
|
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);
|
T res(0);
|
||||||
|
|
||||||
matrix<R-1,C-1,T> minor;
|
matrix_<R-1,C-1,T> minor;
|
||||||
|
|
||||||
// using Laplace Expansion at compile time
|
// using Laplace Expansion at compile time
|
||||||
for (unsigned int c = 0; c < C; c++) {
|
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
|
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);
|
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
|
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;
|
using std::sqrt;
|
||||||
|
|
||||||
|
@ -302,7 +302,7 @@ T matrix<R,C,T>::norm() const
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int R, unsigned int C,typename T> inline
|
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();
|
T n = norm();
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
|
@ -319,19 +319,19 @@ void matrix<R,C,T>::normalize()
|
||||||
// 4x4
|
// 4x4
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class matrix44 : public matrix<4,4,T>
|
class matrix44 : public matrix_<4,4,T>
|
||||||
{
|
{
|
||||||
public:
|
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;
|
*this = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix44& operator = (const matrix<4,4,T>& rhs) {
|
matrix44& operator = (const matrix_<4,4,T>& rhs) {
|
||||||
if (this != &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(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(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
|
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);
|
frustum.fill(0);
|
||||||
|
|
||||||
|
@ -363,12 +363,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static
|
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 Bottom,T Top,
|
||||||
T Near, T Far)
|
T Near, T Far)
|
||||||
{
|
{
|
||||||
|
|
||||||
matrix<4,4,T> ortho;
|
matrix_<4,4,T> ortho;
|
||||||
|
|
||||||
ortho.fill(0);
|
ortho.fill(0);
|
||||||
ortho(0,0) = 2 / (Right-Left);
|
ortho(0,0) = 2 / (Right-Left);
|
||||||
|
@ -386,7 +386,7 @@ public:
|
||||||
|
|
||||||
|
|
||||||
inline static
|
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 height = zNear * tan(fovY/T(360) * pi<T>()); // half height of near plane
|
||||||
const T width = height * aspectRatio; // half width of near plane
|
const T width = height * aspectRatio; // half width of near plane
|
||||||
|
@ -496,13 +496,13 @@ public:
|
||||||
//
|
//
|
||||||
|
|
||||||
template <> inline
|
template <> inline
|
||||||
float matrix<1,1,float>::determinant() const
|
float matrix_<1,1,float>::determinant() const
|
||||||
{
|
{
|
||||||
return this->at(0);
|
return this->at(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <> inline
|
template <> inline
|
||||||
double matrix<1,1,double>::determinant() const
|
double matrix_<1,1,double>::determinant() const
|
||||||
{
|
{
|
||||||
return this->at(0);
|
return this->at(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,10 +126,10 @@ public:
|
||||||
inline void normalize() { *this = this->normalized(); }
|
inline void normalize() { *this = this->normalized(); }
|
||||||
|
|
||||||
//! conversion from a matrix
|
//! 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
|
//! conversion to a matrix
|
||||||
const matrix<4,4,T> to_matrix() const;
|
const matrix_<4,4,T> to_matrix() const;
|
||||||
|
|
||||||
//! return identiy quaternion
|
//! return identiy quaternion
|
||||||
static const quaternion<T> identity();
|
static const quaternion<T> identity();
|
||||||
|
@ -173,7 +173,7 @@ const T quaternion<T>::_sqrt90 = std::sqrt(0.5);
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
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;
|
using std::sqrt;
|
||||||
|
|
||||||
|
@ -188,9 +188,9 @@ const quaternion<T> quaternion<T>::from_matrix(const matrix<4,4,T> &m) {
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
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 xx = x() * x();
|
||||||
T xy = x() * y();
|
T xy = x() * y();
|
||||||
|
|
|
@ -31,17 +31,17 @@
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
template <unsigned int components,typename T>
|
template <unsigned int components,typename T>
|
||||||
class vector : public matrix<components,1,T> {
|
class vector : public matrix_<components,1,T> {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using typename matrix<components,1,T>::value_type;
|
using typename matrix_<components,1,T>::value_type;
|
||||||
using matrix<components,1,T>::operator = ;
|
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); }
|
T& operator()(unsigned int c) { return matrixbase<T>::at(c); }
|
||||||
const T& operator()(unsigned int c) const { return matrixbase<T>::at(c); }
|
const T& operator()(unsigned int c) const { return matrixbase<T>::at(c); }
|
||||||
|
|
|
@ -4,6 +4,8 @@ set(hdrs
|
||||||
include/pw/scene/component.hpp
|
include/pw/scene/component.hpp
|
||||||
include/pw/scene/node.hpp
|
include/pw/scene/node.hpp
|
||||||
include/pw/scene/nodepath.hpp
|
include/pw/scene/nodepath.hpp
|
||||||
|
include/pw/scene/mesh.hpp
|
||||||
|
include/pw/scene/scene.hpp
|
||||||
include/pw/scene/transform.hpp
|
include/pw/scene/transform.hpp
|
||||||
include/pw/scene/traverser.hpp
|
include/pw/scene/traverser.hpp
|
||||||
)
|
)
|
||||||
|
@ -13,6 +15,8 @@ set(srcs
|
||||||
src/nodepath.cpp
|
src/nodepath.cpp
|
||||||
src/camera.cpp
|
src/camera.cpp
|
||||||
src/component.cpp
|
src/component.cpp
|
||||||
|
src/mesh.cpp
|
||||||
|
src/scene.cpp
|
||||||
src/transform.cpp
|
src/transform.cpp
|
||||||
src/traverser.cpp
|
src/traverser.cpp
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,3 +1,27 @@
|
||||||
|
/*
|
||||||
|
* 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_SCENE_CAMERA_HPP
|
#ifndef PW_SCENE_CAMERA_HPP
|
||||||
#define PW_SCENE_CAMERA_HPP
|
#define PW_SCENE_CAMERA_HPP
|
||||||
|
|
||||||
|
@ -28,13 +52,15 @@ public:
|
||||||
void set_projection(const matrix44d &projection);
|
void set_projection(const matrix44d &projection);
|
||||||
const matrix44d& projection() const;
|
const matrix44d& projection() const;
|
||||||
|
|
||||||
|
// void set_field_of_view(float)
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
double _fov;
|
real_t _fov;
|
||||||
double _near_plane;
|
real_t _near_plane;
|
||||||
double _far_plane;
|
real_t _far_plane;
|
||||||
|
|
||||||
double _ortho_size = 2;
|
real_t _ortho_size = 2;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -1,3 +1,27 @@
|
||||||
|
/*
|
||||||
|
* 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_SCENE_COMPONENT_HPP
|
#ifndef PW_SCENE_COMPONENT_HPP
|
||||||
#define PW_SCENE_COMPONENT_HPP
|
#define PW_SCENE_COMPONENT_HPP
|
||||||
|
|
||||||
|
|
45
src/scene/include/pw/scene/mesh.hpp
Normal file
45
src/scene/include/pw/scene/mesh.hpp
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#ifndef PW_SCENE_MESH_HPP
|
||||||
|
#define PW_SCENE_MESH_HPP
|
||||||
|
|
||||||
|
#include <pw/core/matrix.hpp>
|
||||||
|
#include <pw/scene/component.hpp>
|
||||||
|
|
||||||
|
namespace pw {
|
||||||
|
|
||||||
|
class mesh : public component {
|
||||||
|
public:
|
||||||
|
using component::component;
|
||||||
|
|
||||||
|
typedef std::vector<int32_t> index_t;
|
||||||
|
typedef std::vector<float> vertex_t;
|
||||||
|
|
||||||
|
enum topology_type {
|
||||||
|
triangles,
|
||||||
|
triangle_strip,
|
||||||
|
triangle_fan,
|
||||||
|
quads,
|
||||||
|
lines,
|
||||||
|
line_strip,
|
||||||
|
points
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
// index data
|
||||||
|
// vertex d ata
|
||||||
|
// normal data
|
||||||
|
// color data
|
||||||
|
// uv data
|
||||||
|
// tangents data
|
||||||
|
|
||||||
|
|
||||||
|
// boundary
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -28,20 +28,24 @@
|
||||||
#include <pw/core/globals.hpp>
|
#include <pw/core/globals.hpp>
|
||||||
#include <pw/scene/component.hpp>
|
#include <pw/scene/component.hpp>
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief nodes represent the structure for the scene graph
|
* @brief nodes represent the structure for the scene graph
|
||||||
|
*
|
||||||
|
* Unlike earlier designs the scenegraph here is not a DAG
|
||||||
|
* but uses multiple references to components like Unity. This makes the
|
||||||
|
* design of the scenegraph and traversal much more straight-forward.
|
||||||
*/
|
*/
|
||||||
class node {
|
class node {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef shared_ptr<node> ref;
|
typedef shared_ptr<node> ref;
|
||||||
typedef std::vector<ref> ref_array;
|
typedef std::vector<ref> ref_array;
|
||||||
typedef std::vector<node*> ptr_array;
|
|
||||||
|
typedef node *ptr;
|
||||||
|
typedef std::vector<ptr> ptr_array;
|
||||||
|
|
||||||
friend class component;
|
friend class component;
|
||||||
|
|
||||||
|
@ -60,12 +64,15 @@ public:
|
||||||
std::string name() const; //!< get name
|
std::string name() const; //!< get name
|
||||||
void set_name(const std::string &name); //!< set name
|
void set_name(const std::string &name); //!< set name
|
||||||
|
|
||||||
//! return list of parents
|
//! return parent of this node
|
||||||
inline const ptr_array& parents() const { return _parents; }
|
inline ptr parent() const { return _path.back(); }
|
||||||
|
|
||||||
//! add a child node
|
//! add a child node
|
||||||
ref add_child(ref anode);
|
ref add_child(ref anode);
|
||||||
|
|
||||||
|
//! remove a child
|
||||||
|
void remove_child(ref child_node);
|
||||||
|
|
||||||
//! get the list of children
|
//! get the list of children
|
||||||
inline const ref_array& children() const { return _children; }
|
inline const ref_array& children() const { return _children; }
|
||||||
|
|
||||||
|
@ -76,7 +83,7 @@ public:
|
||||||
inline bool is_leaf() const { return children().empty(); }
|
inline bool is_leaf() const { return children().empty(); }
|
||||||
|
|
||||||
//! check if this is the root node
|
//! check if this is the root node
|
||||||
inline bool is_root() const { return parents().empty(); }
|
inline bool is_root() const { return _path.empty(); }
|
||||||
|
|
||||||
//! list of components
|
//! list of components
|
||||||
const component::array& components() const;
|
const component::array& components() const;
|
||||||
|
@ -87,20 +94,20 @@ public:
|
||||||
//! remove a node component
|
//! remove a node component
|
||||||
void remove_component(component::ref c);
|
void remove_component(component::ref c);
|
||||||
|
|
||||||
|
//! paths to root
|
||||||
|
const ptr_array& path() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
ref_array _children;
|
ref_array _children; //!< list of children
|
||||||
ptr_array _parents;
|
ptr_array _path; //!< path to root
|
||||||
|
|
||||||
component::array _components;
|
component::array _components; //<! components
|
||||||
|
|
||||||
std::string _name;
|
std::string _name;
|
||||||
|
|
||||||
private:
|
// change mask
|
||||||
|
// layer mask
|
||||||
//! add a node as parent node
|
|
||||||
void add_parent(node *anode);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
50
src/scene/include/pw/scene/scene.hpp
Normal file
50
src/scene/include/pw/scene/scene.hpp
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* 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_SCENE_SCENE_HPP
|
||||||
|
#define PW_SCENE_SCENE_HPP
|
||||||
|
|
||||||
|
#include <pw/core/globals.hpp>
|
||||||
|
#include <pw/scene/node.hpp>
|
||||||
|
|
||||||
|
namespace pw {
|
||||||
|
|
||||||
|
class scene {
|
||||||
|
public:
|
||||||
|
|
||||||
|
//! set root node
|
||||||
|
void set_root(node::ref root);
|
||||||
|
|
||||||
|
// request the root node
|
||||||
|
inline node::ref root() const { return _root; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
node::ref _root; //!< root node
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -18,6 +18,10 @@ public:
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
inline void translate(const real_t &x, const real_t &y, const real_t &z) {
|
||||||
|
_local.at(0,3) += x;_local.at(1,3) += y;_local.at(2,3) += z;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
matrix44d _local;
|
matrix44d _local;
|
||||||
|
|
9
src/scene/src/mesh.cpp
Normal file
9
src/scene/src/mesh.cpp
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#include "pw/scene/mesh.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace pw {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -9,7 +9,6 @@ node::node(const std::string &name)
|
||||||
|
|
||||||
node::node(const node &node)
|
node::node(const node &node)
|
||||||
: _children(node.children())
|
: _children(node.children())
|
||||||
, _parents(node._parents)
|
|
||||||
, _name(node._name)
|
, _name(node._name)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -31,43 +30,33 @@ void node::set_name(const std::string &name)
|
||||||
|
|
||||||
node::ref node::add_child(ref anode)
|
node::ref node::add_child(ref anode)
|
||||||
{
|
{
|
||||||
anode->add_parent(this);
|
// remove old nodepath
|
||||||
|
anode->_path.clear();
|
||||||
|
|
||||||
|
// take parent nodepath ...
|
||||||
|
if (!this->is_root()) anode->_path = this->_path;
|
||||||
|
// add itself
|
||||||
|
anode->_path.push_back(this);
|
||||||
|
|
||||||
|
// add as child
|
||||||
_children.push_back(anode);
|
_children.push_back(anode);
|
||||||
|
|
||||||
|
// return
|
||||||
return anode;
|
return anode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void node::add_parent(node *anode)
|
void node::remove_child(ref child_node)
|
||||||
{
|
{
|
||||||
_parents.push_back(anode);
|
node::ref_array::iterator it = _children.end();
|
||||||
|
|
||||||
|
while ((it = std::find(_children.begin(),_children.end(),child_node)) != _children.end()) {
|
||||||
|
_children.erase(it);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//void node::visit(visitor& visitor)
|
|
||||||
//{
|
|
||||||
// visitor.enter(this);
|
|
||||||
|
|
||||||
// if (visitor.direction == node::visitor::direction::up)
|
|
||||||
// this->ascend(visitor);
|
|
||||||
// else {
|
|
||||||
// this->descend(visitor);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// visitor.leave(this);
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void node::ascend(visitor& visitor)
|
|
||||||
//{
|
|
||||||
// for (auto p : this->parents()) p->visit(visitor);
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void node::descend(visitor& visitor)
|
|
||||||
//{
|
|
||||||
// for (auto c : this->children()) c->visit(visitor);
|
|
||||||
//}
|
|
||||||
|
|
||||||
node::~node()
|
node::~node()
|
||||||
{
|
{
|
||||||
_children.clear();
|
_children.clear();
|
||||||
_parents.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -82,11 +71,16 @@ void node::remove_component(component::ref c)
|
||||||
{
|
{
|
||||||
component::array::iterator it = _components.end();
|
component::array::iterator it = _components.end();
|
||||||
|
|
||||||
while ((it = std::find(_components.begin(),_components.end(),std::shared_ptr<component>(c))) != _components.end()) {
|
while ((it = std::find(_components.begin(),_components.end(),c)) != _components.end()) {
|
||||||
_components.erase(it);
|
_components.erase(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const node::ptr_array &node::path() const
|
||||||
|
{
|
||||||
|
return _path;
|
||||||
|
}
|
||||||
|
|
||||||
const component::array& node::components() const
|
const component::array& node::components() const
|
||||||
{
|
{
|
||||||
return _components;
|
return _components;
|
||||||
|
|
11
src/scene/src/scene.cpp
Normal file
11
src/scene/src/scene.cpp
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#include "pw/scene/scene.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace pw {
|
||||||
|
|
||||||
|
void scene::set_root(node::ref root)
|
||||||
|
{
|
||||||
|
this->_root = root;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -5,11 +5,8 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
|
|
||||||
traverser t = traverser();
|
traverser t = traverser();
|
||||||
|
|
||||||
void traverser::visit(node *node)
|
void traverser::visit(node *node)
|
||||||
|
@ -18,7 +15,7 @@ void traverser::visit(node *node)
|
||||||
on_enter(node);
|
on_enter(node);
|
||||||
// visit
|
// visit
|
||||||
if (direction::up == _direction)
|
if (direction::up == _direction)
|
||||||
for (auto p : node->parents()) this->visit(p);
|
this->visit(node->parent());
|
||||||
else
|
else
|
||||||
for (auto c : node->children()) this->visit(c.get());
|
for (auto c : node->children()) this->visit(c.get());
|
||||||
|
|
||||||
|
@ -35,16 +32,17 @@ void traverser::bfs(node::ref root)
|
||||||
|
|
||||||
while (!q.empty())
|
while (!q.empty())
|
||||||
{
|
{
|
||||||
|
// get the top
|
||||||
node::ref n = q.top();
|
node::ref n = q.top();
|
||||||
|
// remove from stack
|
||||||
std::cout << "d:" << q.size() << " l:" << n->is_leaf() << " n:" << n->name();
|
|
||||||
// visit n
|
|
||||||
q.pop();
|
q.pop();
|
||||||
|
|
||||||
|
// std::cout << "d:" << q.size() << " l:" << n->is_leaf() << " n:" << n->name();
|
||||||
|
|
||||||
// add all children
|
// add all children
|
||||||
for(auto c : n->children()) q.push(c);
|
for(auto c : n->children()) q.push(c);
|
||||||
|
|
||||||
std::cout << std::endl;
|
// std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,19 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
void print_node_path(pw::node::ref node) {
|
||||||
|
|
||||||
|
auto parents = node->path();
|
||||||
|
|
||||||
|
std::cout << node->name() << " - ";
|
||||||
|
for (auto p : node->path()) {
|
||||||
|
std::cout << " p:'" << p->name() << "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc,char **argv) {
|
int main(int argc,char **argv) {
|
||||||
|
|
||||||
using namespace pw;
|
using namespace pw;
|
||||||
|
@ -33,7 +46,7 @@ int main(int argc,char **argv) {
|
||||||
|
|
||||||
std::cout << "n components: " << n->components().size() << std::endl;
|
std::cout << "n components: " << n->components().size() << std::endl;
|
||||||
|
|
||||||
|
print_node_path((n));
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -23,6 +23,20 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void print_node_path(pw::node::ref node) {
|
||||||
|
|
||||||
|
auto parents = node->path();
|
||||||
|
|
||||||
|
std::cout << node->name() << " - ";
|
||||||
|
for (auto p : node->path()) {
|
||||||
|
std::cout << " p:'" << p->name() << "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
int main(int argc,char **argv) {
|
int main(int argc,char **argv) {
|
||||||
|
|
||||||
using namespace pw;
|
using namespace pw;
|
||||||
|
@ -32,15 +46,19 @@ int main(int argc,char **argv) {
|
||||||
|
|
||||||
node::ref sub_2_1 = std::make_shared<node>("sub-2-1");
|
node::ref sub_2_1 = std::make_shared<node>("sub-2-1");
|
||||||
|
|
||||||
root->add_child(std::make_shared<node>("sub-2"))->add_child(std::make_shared<node>("sub-2-1"));
|
|
||||||
root->add_child(std::make_shared<node>("sub-3"))->add_child(sub_2_1);
|
root->add_child(std::make_shared<node>("sub-3"))->add_child(sub_2_1);
|
||||||
|
root->add_child(std::make_shared<node>("sub-2"))->add_child(std::make_shared<node>("sub-2-2"));
|
||||||
|
|
||||||
std::cout << "sub-2-1 parent count: " << sub_2_1->parents().size() << std::endl;
|
// std::cout << "sub-2-1 parent count: " << sub_2_1->parents().size() << std::endl;
|
||||||
|
|
||||||
|
|
||||||
traverser tv;
|
// traverser tv;
|
||||||
|
// tv.bfs(root);
|
||||||
|
|
||||||
|
|
||||||
|
// print_node_path(sub_2_1);
|
||||||
|
// print_node_path(root->children()[2]->children().front());
|
||||||
|
|
||||||
tv.bfs(root);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue