From 04c05a028c36b8d512202e8856955c04191619f6 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 5 May 2020 14:31:43 +0200 Subject: DerivedNodeTree data structure --- source/blender/blenkernel/BKE_derived_node_tree.hh | 373 +++++++++++++++++++++ source/blender/blenkernel/CMakeLists.txt | 2 + .../blender/blenkernel/intern/derived_node_tree.cc | 29 ++ 3 files changed, 404 insertions(+) create mode 100644 source/blender/blenkernel/BKE_derived_node_tree.hh create mode 100644 source/blender/blenkernel/intern/derived_node_tree.cc diff --git a/source/blender/blenkernel/BKE_derived_node_tree.hh b/source/blender/blenkernel/BKE_derived_node_tree.hh new file mode 100644 index 00000000000..71e699aeaaa --- /dev/null +++ b/source/blender/blenkernel/BKE_derived_node_tree.hh @@ -0,0 +1,373 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __BKE_DERIVED_NODE_TREE_HH__ +#define __BKE_DERIVED_NODE_TREE_HH__ + +#include "BKE_node_tree_ref.hh" + +namespace BKE { + +class DSocket; +class DInputSocket; +class DOutputSocket; +class DNode; +class DParentNode; +class DGroupInput; +class DerivedNodeTree; + +class DSocket : BLI::NonCopyable, BLI::NonMovable { + protected: + DNode *m_node; + const SocketRef *m_socket_ref; + bool m_is_input; + uint m_id; + + friend DerivedNodeTree; + + public: + const DNode &node() const; + + uint id() const; + uint index() const; + + bool is_input() const; + bool is_output() const; + + const DSocket &as_base() const; + const DInputSocket &as_input() const; + const DOutputSocket &as_output() const; + + PointerRNA *rna() const; + StringRefNull idname() const; + StringRefNull name() const; +}; + +class DInputSocket : public DSocket { + private: + Vector m_linked_sockets; + Vector m_linked_group_inputs; + + friend DerivedNodeTree; + + public: + const InputSocketRef &socket_ref() const; + + ArrayRef linked_sockets() const; + ArrayRef linked_group_inputs() const; + + bool is_linked() const; +}; + +class DOutputSocket : public DSocket { + private: + Vector m_linked_sockets; + + friend DerivedNodeTree; + + public: + const OutputSocketRef &socket_ref() const; + ArrayRef linked_sockets() const; +}; + +class DGroupInput : BLI::NonCopyable, BLI::NonMovable { + private: + const InputSocketRef *m_socket_ref; + DParentNode *m_parent; + Vector m_linked_sockets; + uint m_id; + + friend DerivedNodeTree; + + public: + const InputSocketRef &socket_ref() const; + const DParentNode *parent() const; + ArrayRef linked_sockets() const; + uint id() const; +}; + +class DNode : BLI::NonCopyable, BLI::NonMovable { + private: + const NodeRef *m_node_ref; + DParentNode *m_parent; + + Vector m_inputs; + Vector m_outputs; + + uint m_id; + + friend DerivedNodeTree; + + public: + const NodeRef &node_ref() const; + const DParentNode *parent() const; + + ArrayRef inputs() const; + ArrayRef outputs() const; + + const DInputSocket &input(uint index) const; + const DOutputSocket &output(uint index) const; + + uint id() const; + + PointerRNA *rna() const; + StringRefNull idname() const; + StringRefNull name() const; +}; + +class DParentNode : BLI::NonCopyable, BLI::NonMovable { + private: + const NodeRef *m_node_ref; + DParentNode *m_parent; + uint m_id; + + friend DerivedNodeTree; + + public: + const DParentNode *parent() const; + const NodeRef &node_ref() const; + uint id() const; +}; + +using NodeTreeRefMap = Map>; + +class DerivedNodeTree : BLI::NonCopyable, BLI::NonMovable { + private: + LinearAllocator<> m_allocator; + bNodeTree *m_btree; + Vector m_nodes_by_id; + Vector m_group_inputs; + Vector m_parent_nodes; + + Vector m_sockets_by_id; + Vector m_input_sockets; + Vector m_output_sockets; + + StringMap> m_nodes_by_idname; + + public: + DerivedNodeTree(bNodeTree *btree, NodeTreeRefMap &node_tree_refs); + ~DerivedNodeTree(); + + ArrayRef all_nodes() const; +}; + +/* -------------------------------------------------------------------- + * DSocket inline methods. + */ + +inline const DNode &DSocket::node() const +{ + return *m_node; +} + +inline uint DSocket::id() const +{ + return m_id; +} + +inline uint DSocket::index() const +{ + return m_socket_ref->index(); +} + +inline bool DSocket::is_input() const +{ + return m_is_input; +} + +inline bool DSocket::is_output() const +{ + return !m_is_input; +} + +inline const DSocket &DSocket::as_base() const +{ + return *this; +} + +inline const DInputSocket &DSocket::as_input() const +{ + return *(DInputSocket *)this; +} + +inline const DOutputSocket &DSocket::as_output() const +{ + return *(DOutputSocket *)this; +} + +inline PointerRNA *DSocket::rna() const +{ + return m_socket_ref->rna(); +} + +inline StringRefNull DSocket::idname() const +{ + return m_socket_ref->idname(); +} + +inline StringRefNull DSocket::name() const +{ + return m_socket_ref->name(); +} + +/* -------------------------------------------------------------------- + * DInputSocket inline methods. + */ + +inline const InputSocketRef &DInputSocket::socket_ref() const +{ + return m_socket_ref->as_input(); +} + +inline ArrayRef DInputSocket::linked_sockets() const +{ + return m_linked_sockets.as_ref(); +} + +inline ArrayRef DInputSocket::linked_group_inputs() const +{ + return m_linked_group_inputs.as_ref(); +} + +inline bool DInputSocket::is_linked() const +{ + return m_linked_sockets.size() > 0 || m_linked_group_inputs.size() > 0; +} + +/* -------------------------------------------------------------------- + * DOutputSocket inline methods. + */ + +inline const OutputSocketRef &DOutputSocket::socket_ref() const +{ + return m_socket_ref->as_output(); +} + +inline ArrayRef DOutputSocket::linked_sockets() const +{ + return m_linked_sockets.as_ref(); +} + +/* -------------------------------------------------------------------- + * DGroupInput inline methods. + */ + +inline const InputSocketRef &DGroupInput::socket_ref() const +{ + return *m_socket_ref; +} + +inline const DParentNode *DGroupInput::parent() const +{ + return m_parent; +} + +inline ArrayRef DGroupInput::linked_sockets() const +{ + return m_linked_sockets.as_ref(); +} + +inline uint DGroupInput::id() const +{ + return m_id; +} + +/* -------------------------------------------------------------------- + * DNode inline methods. + */ + +inline const NodeRef &DNode::node_ref() const +{ + return *m_node_ref; +} + +inline const DParentNode *DNode::parent() const +{ + return m_parent; +} + +inline ArrayRef DNode::inputs() const +{ + return m_inputs.as_ref(); +} + +inline ArrayRef DNode::outputs() const +{ + return m_outputs.as_ref(); +} + +inline const DInputSocket &DNode::input(uint index) const +{ + return *m_inputs[index]; +} + +inline const DOutputSocket &DNode::output(uint index) const +{ + return *m_outputs[index]; +} + +inline uint DNode::id() const +{ + return m_id; +} + +inline PointerRNA *DNode::rna() const +{ + return m_node_ref->rna(); +} + +inline StringRefNull DNode::idname() const +{ + return m_node_ref->idname(); +} + +inline StringRefNull DNode::name() const +{ + return m_node_ref->name(); +} + +/* -------------------------------------------------------------------- + * DParentNode inline methods. + */ + +inline const DParentNode *DParentNode::parent() const +{ + return m_parent; +} + +inline const NodeRef &DParentNode::node_ref() const +{ + return *m_node_ref; +} + +inline uint DParentNode::id() const +{ + return m_id; +} + +/* -------------------------------------------------------------------- + * DerivedNodeTree inline methods. + */ + +inline ArrayRef DerivedNodeTree::all_nodes() const +{ + return m_nodes_by_id.as_ref(); +} + +} // namespace BKE + +#endif /* __BKE_DERIVED_NODE_TREE_HH__ */ diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt index d571db1c042..6e6b20db0f0 100644 --- a/source/blender/blenkernel/CMakeLists.txt +++ b/source/blender/blenkernel/CMakeLists.txt @@ -106,6 +106,7 @@ set(SRC intern/customdata_file.c intern/data_transfer.c intern/deform.c + intern/derived_node_tree.cc intern/displist.c intern/displist_tangent.c intern/dynamicpaint.c @@ -292,6 +293,7 @@ set(SRC BKE_customdata_file.h BKE_data_transfer.h BKE_deform.h + BKE_derived_node_tree.hh BKE_displist.h BKE_displist_tangent.h BKE_duplilist.h diff --git a/source/blender/blenkernel/intern/derived_node_tree.cc b/source/blender/blenkernel/intern/derived_node_tree.cc new file mode 100644 index 00000000000..7a0896a1f17 --- /dev/null +++ b/source/blender/blenkernel/intern/derived_node_tree.cc @@ -0,0 +1,29 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "BKE_derived_node_tree.hh" + +namespace BKE { + +DerivedNodeTree::DerivedNodeTree(bNodeTree *UNUSED(btree), NodeTreeRefMap &UNUSED(node_tree_refs)) +{ +} + +DerivedNodeTree::~DerivedNodeTree() +{ +} + +} // namespace BKE -- cgit v1.2.3