[Bf-blender-cvs] [7699ea4d27c] functions: refactor graph generation

Jacques Lucke noreply at git.blender.org
Wed Feb 20 17:21:15 CET 2019


Commit: 7699ea4d27c49e1ebeaab370554531a5627535f2
Author: Jacques Lucke
Date:   Wed Feb 20 13:44:03 2019 +0100
Branches: functions
https://developer.blender.org/rB7699ea4d27c49e1ebeaab370554531a5627535f2

refactor graph generation

===================================================================

M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/c_wrapper.cpp
D	source/blender/functions/function_nodes/function_nodes.hpp
R050	source/blender/functions/function_nodes/function_nodes.cpp	source/blender/functions/nodes/graph_generation.cpp
A	source/blender/functions/nodes/graph_generation.hpp

===================================================================

diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index c8e57696137..706e2cf2113 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -32,15 +32,14 @@ set(SRC
 
 	FN_functions.hpp
 
+	types/types.hpp
 	types/numeric.cpp
 	types/numeric.hpp
-	types/types.hpp
-
-	function_nodes/function_nodes.hpp
-	function_nodes/function_nodes.cpp
 
 	nodes/nodes.hpp
 	nodes/nodes.cpp
+	nodes/graph_generation.hpp
+	nodes/graph_generation.cpp
 	nodes/socket_inputs.cpp
 	nodes/test_nodes.cpp
 )
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index 813f1cd2862..4e9f93b98bf 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -1,8 +1,9 @@
 #include "FN_functions.h"
 #include "FN_functions.hpp"
 
-#include "function_nodes/function_nodes.hpp"
 #include "nodes/nodes.hpp"
+#include "nodes/graph_generation.hpp"
+
 #include "BLI_lazy_init.hpp"
 
 #include <iostream>
@@ -209,10 +210,9 @@ FnFunction FN_get_generated_function()
 	return wrap(fn_ref);
 }
 
-FnFunction FN_testing(bNodeTree *bnodetree)
+FnFunction FN_testing(bNodeTree *btree)
 {
-	FN::Nodes::FunctionNodeTree tree(bnodetree);
-	auto fgraph = tree.to_function_graph();
+	auto fgraph = FN::Nodes::btree_to_graph(btree);
 	std::cout << fgraph.graph()->to_dot() << std::endl;
 
 	auto fn = FN::SharedFunction::New("Function from Node Tree", fgraph.signature());
diff --git a/source/blender/functions/function_nodes/function_nodes.hpp b/source/blender/functions/function_nodes/function_nodes.hpp
deleted file mode 100644
index f974d5d7cca..00000000000
--- a/source/blender/functions/function_nodes/function_nodes.hpp
+++ /dev/null
@@ -1,39 +0,0 @@
-#pragma once
-
-#include "FN_functions.hpp"
-#include "DNA_node_types.h"
-#include "BLI_listbase_wrapper.hpp"
-
-namespace FN { namespace Nodes {
-
-	using bNodeList = ListBaseWrapper<bNode, true>;
-	using bLinkList = ListBaseWrapper<bNodeLink, true>;
-	using bSocketList = ListBaseWrapper<bNodeSocket, true>;
-
-	class FunctionNodeTree {
-	private:
-		bNodeTree *m_tree;
-
-	public:
-		FunctionNodeTree(bNodeTree *tree)
-			: m_tree(tree) {}
-
-		bNodeTree *orig_tree() const
-		{
-			return m_tree;
-		}
-
-		bNodeList nodes() const
-		{
-			return bNodeList(&m_tree->nodes);
-		}
-
-		bLinkList links() const
-		{
-			return bLinkList(&m_tree->links);
-		}
-
-		FunctionGraph to_function_graph() const;
-	};
-
-} } /* namespace FN::Nodes */
\ No newline at end of file
diff --git a/source/blender/functions/function_nodes/function_nodes.cpp b/source/blender/functions/nodes/graph_generation.cpp
similarity index 50%
rename from source/blender/functions/function_nodes/function_nodes.cpp
rename to source/blender/functions/nodes/graph_generation.cpp
index 5ae684f0f4d..2f719e7e7a4 100644
--- a/source/blender/functions/function_nodes/function_nodes.cpp
+++ b/source/blender/functions/nodes/graph_generation.cpp
@@ -1,16 +1,7 @@
-#include "function_nodes.hpp"
-#include "nodes/nodes.hpp"
-
-#include "BLI_listbase.h"
-
-#include "BKE_node.h"
-#include "BKE_idprop.h"
-
-#include "RNA_access.h"
-
-#include "DNA_object_types.h"
+#include "graph_generation.hpp"
 
 namespace FN { namespace Nodes {
+
 	using namespace Types;
 
 	static SharedType &get_type_of_socket(bNodeTree *UNUSED(btree), bNodeSocket *bsocket)
@@ -27,67 +18,51 @@ namespace FN { namespace Nodes {
 		}
 	}
 
-	static SharedFunction get_function_with_inputs(const SmallTypeVector &types)
-	{
-		InputParameters inputs;
-		for (SharedType &type : types) {
-			inputs.append(InputParameter("Input", type));
-		}
-		return SharedFunction::New("Inputs", Signature(inputs, {}));
-	}
-
-	static SharedFunction get_function_with_outputs(const SmallTypeVector &types)
-	{
-		OutputParameters outputs;
-		for (SharedType &type : types) {
-			outputs.append(OutputParameter("Output", type));
-		}
-		return SharedFunction::New("Outputs", Signature({}, outputs));
-	}
 
-	static void insert_output_node(
+	static void insert_input_node(
 		bNodeTree *btree,
 		bNode *bnode,
 		SharedDataFlowGraph &graph,
 		SocketMap &socket_map)
 	{
-		SmallTypeVector types;
-		for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
-			types.append(get_type_of_socket(btree, bsocket));
+		OutputParameters outputs;
+		for (bNodeSocket *bsocket : bSocketList(&bnode->outputs)) {
+			SharedType &type = get_type_of_socket(btree, bsocket);
+			outputs.append(OutputParameter(bsocket->name, type));
 		}
 
-		SharedFunction fn = get_function_with_inputs(types);
+		auto fn = SharedFunction::New("Function Input", Signature({}, outputs));
 		const Node *node = graph->insert(fn);
 
 		uint i = 0;
-		for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
-			socket_map.add(bsocket, node->input(i));
+		for (bNodeSocket *bsocket : bSocketList(&bnode->outputs)) {
+			socket_map.add(bsocket, node->output(i));
 			i++;
 		}
 	}
 
-	static void insert_input_node(
+	static void insert_output_node(
 		bNodeTree *btree,
 		bNode *bnode,
 		SharedDataFlowGraph &graph,
 		SocketMap &socket_map)
 	{
-		SmallTypeVector types;
-		for (bNodeSocket *bsocket : bSocketList(&bnode->outputs)) {
-			types.append(get_type_of_socket(btree, bsocket));
+		InputParameters inputs;
+		for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
+			SharedType &type = get_type_of_socket(btree, bsocket);
+			inputs.append(InputParameter(bsocket->name, type));
 		}
 
-		SharedFunction fn = get_function_with_outputs(types);
+		auto fn = SharedFunction::New("Function Output", Signature(inputs, {}));
 		const Node *node = graph->insert(fn);
 
 		uint i = 0;
-		for (bNodeSocket *bsocket : bSocketList(&bnode->outputs)) {
-			socket_map.add(bsocket, node->output(i));
+		for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
+			socket_map.add(bsocket, node->input(i));
 			i++;
 		}
 	}
 
-
 	static bool is_input_node(const bNode *bnode)
 	{
 		return STREQ(bnode->idname, "fn_FunctionInputNode");
@@ -98,50 +73,69 @@ namespace FN { namespace Nodes {
 		return STREQ(bnode->idname, "fn_FunctionOutputNode");
 	}
 
-	FunctionGraph FunctionNodeTree::to_function_graph() const
+	static void find_interface_nodes(
+		bNodeTree *btree,
+		bNode **r_input,
+		bNode **r_output)
 	{
-		bNodeTree *btree = this->orig_tree();
+		bNode *input = nullptr;
+		bNode *output = nullptr;
+
+		for (bNode *bnode : bNodeList(&btree->nodes)) {
+			if (is_input_node(bnode)) input = bnode;
+			if (is_output_node(bnode)) output = bnode;
+		}
 
+		*r_input = input;
+		*r_output = output;
+	}
+
+	FunctionGraph btree_to_graph(bNodeTree *btree)
+	{
 		SocketMap socket_map;
+		auto graph = SharedDataFlowGraph::New();
+
+		bNode *input_node;
+		bNode *output_node;
+		find_interface_nodes(btree, &input_node, &output_node);
+
+		for (bNode *bnode : bNodeList(&btree->nodes)) {
+			if (bnode == input_node || bnode == output_node) {
+				continue;
+			}
 
-		SharedDataFlowGraph graph = SharedDataFlowGraph::New();
+			auto inserter = get_node_inserter(bnode->idname);
+			inserter(btree, bnode, graph, socket_map);
+		}
 
 		SmallSocketVector input_sockets;
 		SmallSocketVector output_sockets;
 
-		for (bNode *bnode : this->nodes()) {
-			if (is_input_node(bnode)) {
-				insert_input_node(btree, bnode, graph, socket_map);
-				for (bNodeSocket *bsocket : bSocketList(&bnode->outputs)) {
-					Socket socket = socket_map.lookup(bsocket);
-					input_sockets.append(socket);
-				}
-			}
-			else if (is_output_node(bnode)) {
-				insert_output_node(btree, bnode, graph, socket_map);
-				for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
-					Socket socket = socket_map.lookup(bsocket);
-					output_sockets.append(socket);
-				}
+		if (input_node != nullptr) {
+			insert_input_node(btree, input_node, graph, socket_map);
+			for (bNodeSocket *bsocket : bSocketList(&input_node->outputs)) {
+				input_sockets.append(socket_map.lookup(bsocket));
 			}
-			else {
-				auto insert = get_node_inserter(bnode->idname);
-				insert(btree, bnode, graph, socket_map);
+		}
+		if (output_node != nullptr) {
+			insert_output_node(btree, output_node, graph, socket_map);
+			for (bNodeSocket *bsocket : bSocketList(&output_node->inputs)) {
+				output_sockets.append(socket_map.lookup(bsocket));
 			}
 		}
 
-		for (bNodeLink *blink : this->links()) {
+		for (bNodeLink *blink : bLinkList(&btree->links)) {
 			Socket from = socket_map.lookup(blink->fromsock);
 			Socket to = socket_map.lookup(blink->tosock);
 			graph->link(from, to);
 		}
 
-		for (bNode *bnode : this->nodes()) {
+		for (bNode *bnode : bNodeList(&btree->nodes)) {
 			for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
 				Socket socket = socket_map.lookup(bsocket);
 				if (!socket.is_linked()) {
-					auto insert = get_socket_inserter(bsocket->idname);
-					Socket new_origin = insert(btree, bsocket, graph);
+					auto inserter = get_socket_inserter(bsocket->idname);
+					Socket new_origin = inserter(btree, bsocket, graph);
 					graph->link(new_origin, socket);
 				}
 			}
@@ -151,6 +145,7 @@ namespace FN { namespace Nodes {
 		FunctionGraph fgraph(graph, input_sockets, output_sockets);
 
 		return fgraph;
+
 	}
 
-} } /* FN::Nodes */
\ No newline at end of file
+} }
\ No newline at end of file
diff --git a/source/blender/functions/nodes/graph_generation.hpp b/source/blender/functions/nodes/graph_generation.hpp
new file mode 100644
index 00000000000..718ce71f94c
--- /dev/null
+++ b/source/blender/functions/nodes/graph_generation.hpp
@@ -0,0 +1,9 @@
+#pragma once
+
+#include "nodes.hpp"
+
+namespace FN { namespace Nodes {
+
+	FunctionGraph btree_to_graph(bNodeTree *btree);
+
+} } /* namespace FN::Nodes */
\ No newline at end of file



More information about the Bf-blender-cvs mailing list