diff --git a/share/doc/pixwerx.xmind b/share/doc/pixwerx.xmind index 4399247..204f29e 100644 Binary files a/share/doc/pixwerx.xmind and b/share/doc/pixwerx.xmind differ diff --git a/src/core/include/pw/core/globals.hpp b/src/core/include/pw/core/globals.hpp index d020ac1..e831e87 100644 --- a/src/core/include/pw/core/globals.hpp +++ b/src/core/include/pw/core/globals.hpp @@ -4,10 +4,13 @@ #include #include #include +#include namespace pw { using std::shared_ptr; +using std::unique_ptr; +using std::weak_ptr; typedef float real_t; diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index dc0de20..c0dd064 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -35,7 +35,7 @@ namespace pw { template -class matrix : public matrixbase { +class matrix_ : public matrixbase { T m[R*C]; @@ -44,37 +44,37 @@ public: using typename matrixbase::value_type; using typename matrixbase::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 minor; + matrix_ 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& res, unsigned int r0, unsigned int c0) const; + void get_minor(matrix_& res, unsigned int r0, unsigned int c0) const; T determinant() const; @@ -99,29 +99,29 @@ public: T norm() const; - matrix& operator *= (const matrix& rhs); + matrix_& operator *= (const matrix_& rhs); - matrix& copy_from_data(const T* src) { for (unsigned int i = 0; i < R*C; ++i) { (*this).at(i) = src[i]; } return *this; } + matrix_& copy_from_data(const T* src) { for (unsigned int i = 0; i < R*C; ++i) { (*this).at(i) = src[i]; } return *this; } - matrix operator * (const matrix& rhs) const { + matrix_ operator * (const matrix_& rhs) const { return mul(*this,rhs); } - const matrix reshape() const { - matrix m; + const matrix_ reshape() const { + matrix_ 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 get_column(unsigned int col) const { - matrix c; for (unsigned int r = 0; r < R; ++r) c(r,0) = (this)(r,col); + const matrix_ get_column(unsigned int col) const { + matrix_ 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 -inline matrix operator * (const matrix& a, const T& b) +inline matrix_ operator * (const matrix_& a, const T& b) { - matrix res; + matrix_ res; for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * b; return res; } template -inline matrix operator / (const matrix& a, const T& b) +inline matrix_ operator / (const matrix_& a, const T& b) { - matrix res; T oneOverB(1./b); + matrix_ res; T oneOverB(1./b); for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * oneOverB; return res; } template -inline matrix operator + (const matrix& a, const T& b) +inline matrix_ operator + (const matrix_& a, const T& b) { - matrix res; + matrix_ res; for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b; return res; } template -inline matrix operator - (const matrix& a, const T& b) +inline matrix_ operator - (const matrix_& a, const T& b) { - matrix res; + matrix_ res; for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b; return res; } template -inline matrix operator + (const matrix& a, const matrix& b) +inline matrix_ operator + (const matrix_& a, const matrix_& b) { - matrix res; + matrix_ res; for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b.at(i); return res; } template -inline matrix operator - (const matrix& a, const matrix& b) +inline matrix_ operator - (const matrix_& a, const matrix_& b) { - matrix res; + matrix_ 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 operator - (const matrix& a, const matrix& b) ///////////////////////////////////////////////////////////////////////////// template -matrix static inline -mul(const matrix& A, const matrix& B) +matrix_ static inline +mul(const matrix_& A, const matrix_& B) { // aC == bR // set all null - matrix res; + matrix_ res; res.fill(0); // compute all resulting cells @@ -206,7 +206,7 @@ mul(const matrix& A, const matrix& B) ///////////////////////////////////////////////////////////////////////////// template -matrix& matrix::operator *= (const matrix& rhs) +matrix_& matrix_::operator *= (const matrix_& rhs) { *this = mul(*this,rhs); return *this; @@ -215,20 +215,20 @@ matrix& matrix::operator *= (const matrix& rhs) ///////////////////////////////////////////////////////////////////////////// template -matrix::matrix() +matrix_::matrix_() : matrixbase(R,C,&m[0],true) { } template -matrix::matrix(const matrix &mtc) : +matrix_::matrix_(const matrix_ &mtc) : matrixbase(R,C,&m[0],true) { *this = mtc; } template -matrix &matrix::operator = (const matrix &other) +matrix_ &matrix_::operator = (const matrix_ &other) { if (this != &other) for (unsigned int r = 0; r < R;++r) @@ -238,9 +238,9 @@ matrix &matrix::operator = (const matrix &other) } template -matrix matrix::transposed() const +matrix_ matrix_::transposed() const { - matrix res; + matrix_ 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 matrix::transposed() const } template -void matrix::get_minor(matrix& res,unsigned int r0, unsigned int c0) const +void matrix_::get_minor(matrix_& 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::get_minor(matrix& res,unsigned int r0, unsigned i } template inline -T matrix::determinant() const +T matrix_::determinant() const { T res(0); - matrix minor; + matrix_ minor; // using Laplace Expansion at compile time for (unsigned int c = 0; c < C; c++) { @@ -282,7 +282,7 @@ T matrix::determinant() const } template inline -T matrix::squared_norm() const +T matrix_::squared_norm() const { T res(0); @@ -294,7 +294,7 @@ T matrix::squared_norm() const template inline -T matrix::norm() const +T matrix_::norm() const { using std::sqrt; @@ -302,7 +302,7 @@ T matrix::norm() const } template inline -void matrix::normalize() +void matrix_::normalize() { T n = norm(); if (n > 0) { @@ -319,19 +319,19 @@ void matrix::normalize() // 4x4 template -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()); // 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); } diff --git a/src/core/include/pw/core/quaternion.hpp b/src/core/include/pw/core/quaternion.hpp index d2ec8bd..c00854e 100644 --- a/src/core/include/pw/core/quaternion.hpp +++ b/src/core/include/pw/core/quaternion.hpp @@ -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 identity(); @@ -173,7 +173,7 @@ const T quaternion::_sqrt90 = std::sqrt(0.5); template -const quaternion quaternion::from_matrix(const matrix<4,4,T> &m) { +const quaternion quaternion::from_matrix(const matrix_<4,4,T> &m) { using std::sqrt; @@ -188,9 +188,9 @@ const quaternion quaternion::from_matrix(const matrix<4,4,T> &m) { } template -const matrix<4,4,T> quaternion::to_matrix() const { +const matrix_<4,4,T> quaternion::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(); diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index 0808732..c1a1d0d 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -31,17 +31,17 @@ namespace pw { template -class vector : public matrix { +class vector : public matrix_ { public: - using typename matrix::value_type; - using matrix::operator = ; + using typename matrix_::value_type; + using matrix_::operator = ; - vector() : matrix() {} + vector() : matrix_() {} - vector(const vector& other) : matrix(other) {} + vector(const vector& other) : matrix_(other) {} - vector(const matrix& other) : matrix(other) {} + vector(const matrix_& other) : matrix_(other) {} T& operator()(unsigned int c) { return matrixbase::at(c); } const T& operator()(unsigned int c) const { return matrixbase::at(c); } diff --git a/src/scene/CMakeLists.txt b/src/scene/CMakeLists.txt index e76a52d..593de6d 100644 --- a/src/scene/CMakeLists.txt +++ b/src/scene/CMakeLists.txt @@ -4,6 +4,8 @@ set(hdrs include/pw/scene/component.hpp include/pw/scene/node.hpp include/pw/scene/nodepath.hpp + include/pw/scene/mesh.hpp + include/pw/scene/scene.hpp include/pw/scene/transform.hpp include/pw/scene/traverser.hpp ) @@ -13,6 +15,8 @@ set(srcs src/nodepath.cpp src/camera.cpp src/component.cpp + src/mesh.cpp + src/scene.cpp src/transform.cpp src/traverser.cpp ) diff --git a/src/scene/include/pw/scene/camera.hpp b/src/scene/include/pw/scene/camera.hpp index 8b2726a..52648bd 100644 --- a/src/scene/include/pw/scene/camera.hpp +++ b/src/scene/include/pw/scene/camera.hpp @@ -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 #define PW_SCENE_CAMERA_HPP @@ -28,13 +52,15 @@ public: void set_projection(const matrix44d &projection); const matrix44d& projection() const; +// void set_field_of_view(float) + protected: - double _fov; - double _near_plane; - double _far_plane; + real_t _fov; + real_t _near_plane; + real_t _far_plane; - double _ortho_size = 2; + real_t _ortho_size = 2; private: diff --git a/src/scene/include/pw/scene/component.hpp b/src/scene/include/pw/scene/component.hpp index fc32a74..c4c2b10 100644 --- a/src/scene/include/pw/scene/component.hpp +++ b/src/scene/include/pw/scene/component.hpp @@ -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 #define PW_SCENE_COMPONENT_HPP diff --git a/src/scene/include/pw/scene/mesh.hpp b/src/scene/include/pw/scene/mesh.hpp new file mode 100644 index 0000000..c7f7c97 --- /dev/null +++ b/src/scene/include/pw/scene/mesh.hpp @@ -0,0 +1,45 @@ +#ifndef PW_SCENE_MESH_HPP +#define PW_SCENE_MESH_HPP + +#include +#include + +namespace pw { + +class mesh : public component { +public: + using component::component; + + typedef std::vector index_t; + typedef std::vector 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 + diff --git a/src/scene/include/pw/scene/node.hpp b/src/scene/include/pw/scene/node.hpp index ebe63c0..69c00d0 100644 --- a/src/scene/include/pw/scene/node.hpp +++ b/src/scene/include/pw/scene/node.hpp @@ -28,20 +28,24 @@ #include #include -#include -#include namespace pw { /** * @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 { public: typedef shared_ptr ref; - typedef std::vector ref_array; - typedef std::vector ptr_array; + typedef std::vector ref_array; + + typedef node *ptr; + typedef std::vector ptr_array; friend class component; @@ -60,12 +64,15 @@ public: std::string name() const; //!< get name void set_name(const std::string &name); //!< set name - //! return list of parents - inline const ptr_array& parents() const { return _parents; } + //! return parent of this node + inline ptr parent() const { return _path.back(); } //! add a child node ref add_child(ref anode); + //! remove a child + void remove_child(ref child_node); + //! get the list of children inline const ref_array& children() const { return _children; } @@ -76,7 +83,7 @@ public: inline bool is_leaf() const { return children().empty(); } //! 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 const component::array& components() const; @@ -87,20 +94,20 @@ public: //! remove a node component void remove_component(component::ref c); + //! paths to root + const ptr_array& path() const; + protected: - ref_array _children; - ptr_array _parents; + ref_array _children; //!< list of children + ptr_array _path; //!< path to root - component::array _components; + component::array _components; // +#include + +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 diff --git a/src/scene/include/pw/scene/transform.hpp b/src/scene/include/pw/scene/transform.hpp index fe28cdc..30f1ab7 100644 --- a/src/scene/include/pw/scene/transform.hpp +++ b/src/scene/include/pw/scene/transform.hpp @@ -18,6 +18,10 @@ public: 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: matrix44d _local; diff --git a/src/scene/src/mesh.cpp b/src/scene/src/mesh.cpp new file mode 100644 index 0000000..981ea1a --- /dev/null +++ b/src/scene/src/mesh.cpp @@ -0,0 +1,9 @@ +#include "pw/scene/mesh.hpp" + + +namespace pw { + + + + +} diff --git a/src/scene/src/node.cpp b/src/scene/src/node.cpp index 9c390cc..2ac65df 100644 --- a/src/scene/src/node.cpp +++ b/src/scene/src/node.cpp @@ -9,7 +9,6 @@ node::node(const std::string &name) node::node(const node &node) : _children(node.children()) - , _parents(node._parents) , _name(node._name) { } @@ -31,43 +30,33 @@ void node::set_name(const std::string &name) 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); + + // return 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() { _children.clear(); - _parents.clear(); } // @@ -82,11 +71,16 @@ void node::remove_component(component::ref c) { component::array::iterator it = _components.end(); - while ((it = std::find(_components.begin(),_components.end(),std::shared_ptr(c))) != _components.end()) { + while ((it = std::find(_components.begin(),_components.end(),c)) != _components.end()) { _components.erase(it); } } +const node::ptr_array &node::path() const +{ + return _path; +} + const component::array& node::components() const { return _components; diff --git a/src/scene/src/scene.cpp b/src/scene/src/scene.cpp new file mode 100644 index 0000000..5b39396 --- /dev/null +++ b/src/scene/src/scene.cpp @@ -0,0 +1,11 @@ +#include "pw/scene/scene.hpp" + + +namespace pw { + +void scene::set_root(node::ref root) +{ + this->_root = root; +} + +} diff --git a/src/scene/src/traverser.cpp b/src/scene/src/traverser.cpp index 5940fde..d1d4e6b 100644 --- a/src/scene/src/traverser.cpp +++ b/src/scene/src/traverser.cpp @@ -5,11 +5,8 @@ #include #include - - namespace pw { - traverser t = traverser(); void traverser::visit(node *node) @@ -18,7 +15,7 @@ void traverser::visit(node *node) on_enter(node); // visit if (direction::up == _direction) - for (auto p : node->parents()) this->visit(p); + this->visit(node->parent()); else for (auto c : node->children()) this->visit(c.get()); @@ -35,16 +32,17 @@ void traverser::bfs(node::ref root) while (!q.empty()) { + // get the top node::ref n = q.top(); - - std::cout << "d:" << q.size() << " l:" << n->is_leaf() << " n:" << n->name(); - // visit n + // remove from stack q.pop(); +// std::cout << "d:" << q.size() << " l:" << n->is_leaf() << " n:" << n->name(); + // add all children for(auto c : n->children()) q.push(c); - std::cout << std::endl; +// std::cout << std::endl; } } diff --git a/src/scene/tests/pwscene_test_node.cpp b/src/scene/tests/pwscene_test_node.cpp index 7ad0fe9..e3c355b 100644 --- a/src/scene/tests/pwscene_test_node.cpp +++ b/src/scene/tests/pwscene_test_node.cpp @@ -6,6 +6,19 @@ #include +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) { using namespace pw; @@ -33,7 +46,7 @@ int main(int argc,char **argv) { std::cout << "n components: " << n->components().size() << std::endl; - + print_node_path((n)); return 0; diff --git a/src/scene/tests/pwscene_test_traverser.cpp b/src/scene/tests/pwscene_test_traverser.cpp index 4f08170..638521f 100644 --- a/src/scene/tests/pwscene_test_traverser.cpp +++ b/src/scene/tests/pwscene_test_traverser.cpp @@ -23,6 +23,20 @@ #include + + +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) { using namespace pw; @@ -32,15 +46,19 @@ int main(int argc,char **argv) { node::ref sub_2_1 = std::make_shared("sub-2-1"); - root->add_child(std::make_shared("sub-2"))->add_child(std::make_shared("sub-2-1")); root->add_child(std::make_shared("sub-3"))->add_child(sub_2_1); + root->add_child(std::make_shared("sub-2"))->add_child(std::make_shared("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; }