[Bf-blender-cvs] [999a8026008] functions: generate function from data flow graph

Jacques Lucke noreply at git.blender.org
Sun Feb 10 20:26:32 CET 2019


Commit: 999a8026008bc9063c3da0ba3948f6a4f68c9a1f
Author: Jacques Lucke
Date:   Thu Feb 7 16:47:38 2019 +0100
Branches: functions
https://developer.blender.org/rB999a8026008bc9063c3da0ba3948f6a4f68c9a1f

generate function from data flow graph

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

M	source/blender/blenlib/BLI_small_set_vector.hpp
M	source/blender/blenlib/BLI_small_vector.hpp
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_functions.h
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/c_wrapper.cpp
M	source/blender/functions/core/core.hpp
M	source/blender/functions/core/data_flow_graph.hpp
A	source/blender/functions/core/graph_to_function.cpp
A	source/blender/functions/core/graph_to_function.hpp
M	source/blender/modifiers/intern/MOD_functiondeform.c

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

diff --git a/source/blender/blenlib/BLI_small_set_vector.hpp b/source/blender/blenlib/BLI_small_set_vector.hpp
index d3992915c84..8c18c53234f 100644
--- a/source/blender/blenlib/BLI_small_set_vector.hpp
+++ b/source/blender/blenlib/BLI_small_set_vector.hpp
@@ -13,6 +13,9 @@ namespace BLI {
 		SmallSetVector(const std::initializer_list<T> &values)
 			: SmallSet<T>(values) {}
 
+		SmallSetVector(const SmallVector<T> &values)
+			: SmallSet<T>(values) {}
+
 		int index(const T &value) const
 		{
 			for (uint i = 0; i < this->size(); i++) {
diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index a7041952d46..c9e919a9b35 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -120,13 +120,7 @@ namespace BLI {
 			return -1;
 		}
 
-		T &operator[](const int index)
-		{
-			BLI_assert(index >= 0 && index < this->size());
-			return this->m_elements[index];
-		}
-
-		T operator[](const int index) const
+		T &operator[](const int index) const
 		{
 			BLI_assert(index >= 0 && index < this->size());
 			return this->m_elements[index];
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 9139b3336b1..3161e2a98d7 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -16,6 +16,8 @@ set(SRC
 	core/cpu.cpp
 	core/data_flow_graph.cpp
 	core/dot_export.cpp
+	core/graph_to_function.hpp
+	core/graph_to_function.cpp
 
 	FN_functions.hpp
 
diff --git a/source/blender/functions/FN_functions.h b/source/blender/functions/FN_functions.h
index b5e49a0c78a..852c3756f48 100644
--- a/source/blender/functions/FN_functions.h
+++ b/source/blender/functions/FN_functions.h
@@ -35,6 +35,8 @@ FnType FN_type_get_fvec3(void);
 
 FnFunction FN_get_deform_function(int type);
 
+FnFunction FN_get_generated_function();
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index 3eb9fa1db57..c84b700225a 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -2,4 +2,6 @@
 
 #include "core/core.hpp"
 #include "core/data_flow_graph.hpp"
+#include "./core/graph_to_function.hpp"
+
 #include "core/cpu.hpp"
\ No newline at end of file
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index aea8ff92f39..456f810e1ef 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -129,7 +129,15 @@ public:
 	}
 };
 
-FnFunction FN_get_deform_function(int type)
+class PassThroughFloat : public FN::TupleCallBody {
+public:
+	virtual void call(const FN::Tuple &fn_in, FN::Tuple &fn_out) const override
+	{
+		fn_out.set<float>(0, fn_in.get<float>(0));
+	}
+};
+
+static FN::SharedFunction get_deform_function(int type)
 {
 	FN::InputParameters inputs;
 	inputs.append(FN::InputParameter("Position", FN::Types::get_fvec3_type()));
@@ -145,6 +153,50 @@ FnFunction FN_get_deform_function(int type)
 	else {
 		fn->add_body(new Deform2());
 	}
+	return fn;
+}
+
+static FN::SharedFunction get_pass_through_float_function()
+{
+	FN::InputParameters inputs = {FN::InputParameter("In", FN::Types::get_float_type())};
+	FN::OutputParameters outputs = {FN::OutputParameter("Out", FN::Types::get_float_type())};
+	auto fn = FN::SharedFunction::New(FN::Signature(inputs, outputs), "Pass Through");
+	fn->add_body(new PassThroughFloat());
+	return fn;
+}
+
+FnFunction FN_get_deform_function(int type)
+{
+	auto fn = get_deform_function(type);
+	BLI::RefCounted<FN::Function> *fn_ref = fn.refcounter();
+	fn_ref->incref();
+	return wrap(fn_ref);
+}
+
+FnFunction FN_get_generated_function()
+{
+	FN::SharedDataFlowGraph graph = FN::SharedDataFlowGraph::New();
+
+	FN::SharedFunction f1 = get_deform_function(0);
+	FN::SharedFunction f2 = get_deform_function(1);
+	FN::SharedFunction pass = get_pass_through_float_function();
+
+	auto n1 = graph->insert(f1);
+	auto n2 = graph->insert(f2);
+	auto npass = graph->insert(pass);
+
+	graph->link(n1->output(0), n2->input(0));
+	graph->link(npass->output(0), n1->input(1));
+	graph->link(npass->output(0), n2->input(1));
+
+	auto fn = FN::function_from_data_flow(graph,
+	{
+		n1->input(0),
+		npass->input(0)
+	},
+	{
+		n2->output(0)
+	});
 
 	BLI::RefCounted<FN::Function> *fn_ref = fn.refcounter();
 	fn_ref->incref();
diff --git a/source/blender/functions/core/core.hpp b/source/blender/functions/core/core.hpp
index d89c156b446..a1a09956cbe 100644
--- a/source/blender/functions/core/core.hpp
+++ b/source/blender/functions/core/core.hpp
@@ -109,7 +109,7 @@ namespace FN {
 
 	class Parameter {
 	public:
-		Parameter(const std::string &name, SharedType &type)
+		Parameter(const std::string &name, const SharedType &type)
 			: m_type(type), m_name(name) {}
 
 		const SharedType &type() const
@@ -123,19 +123,19 @@ namespace FN {
 		}
 
 	private:
-		SharedType m_type;
+		const SharedType m_type;
 		const std::string m_name;
 	};
 
 	class InputParameter : public Parameter {
 	public:
-		InputParameter(const std::string &name, SharedType &type)
+		InputParameter(const std::string &name, const SharedType &type)
 			: Parameter(name, type) {}
 	};
 
 	class OutputParameter : public Parameter {
 	public:
-		OutputParameter(const std::string &name, SharedType &type)
+		OutputParameter(const std::string &name, const SharedType &type)
 			: Parameter(name, type) {}
 	};
 
diff --git a/source/blender/functions/core/data_flow_graph.hpp b/source/blender/functions/core/data_flow_graph.hpp
index 24f74e6722f..8481d5d93b8 100644
--- a/source/blender/functions/core/data_flow_graph.hpp
+++ b/source/blender/functions/core/data_flow_graph.hpp
@@ -3,6 +3,7 @@
 #include "core.hpp"
 
 #include "BLI_small_set.hpp"
+#include "BLI_small_set_vector.hpp"
 
 namespace FN {
 
@@ -16,36 +17,19 @@ namespace FN {
 		static inline Socket Input(const Node *node, uint index);
 		static inline Socket Output(const Node *node, uint index);
 
-		const Node *node() const
-		{
-			return this->m_node;
-		}
+		inline const Node *node() const;
+		inline const DataFlowGraph *graph() const;
 
-		bool is_input() const
-		{
-			return !this->m_is_output;
-		}
-
-		bool is_output() const
-		{
-			return this->m_is_output;
-		}
-
-		uint index() const
-		{
-			return this->m_index;
-		}
+		inline bool is_input() const;
+		inline bool is_output() const;
+		inline uint index() const;
 
 		const SharedType &type() const;
 		std::string name() const;
 
-		friend bool operator==(const Socket &a, const Socket &b)
-		{
-			return (
-				a.m_node == b.m_node &&
-				a.m_is_output == b.m_is_output &&
-				a.m_index == b.m_index);
-		}
+		friend bool operator==(const Socket &a, const Socket &b);
+
+		inline Socket origin() const;
 
 	private:
 		Socket(const Node *node, bool is_output, uint index)
@@ -56,10 +40,14 @@ namespace FN {
 		const uint m_index;
 	};
 
+
+	using SmallSocketVector = SmallVector<Socket>;
+	using SmallSocketSetVector = SmallSetVector<Socket>;
+
 	class Node {
 	public:
-		Node(const SharedFunction &function)
-			: m_function(function) {}
+		Node(const DataFlowGraph *graph, const SharedFunction &function)
+			: m_graph(graph), m_function(function) {}
 
 		Socket input(uint index) const
 		{
@@ -71,6 +59,11 @@ namespace FN {
 			return Socket::Output(this, index);
 		}
 
+		const DataFlowGraph *graph() const
+		{
+			return this->m_graph;
+		}
+
 		const SharedFunction &function() const
 		{
 			return this->m_function;
@@ -82,6 +75,7 @@ namespace FN {
 		}
 
 	private:
+		const DataFlowGraph *m_graph;
 		const SharedFunction m_function;
 	};
 
@@ -150,6 +144,14 @@ namespace FN {
 			return this->m_all_links;
 		}
 
+		Socket get_origin(Socket socket) const
+		{
+			BLI_assert(socket.is_input());
+			auto linked = this->get_linked(socket);
+			BLI_assert(linked.size() == 1);
+			return linked.any();
+		}
+
 	private:
 		SmallMap<Socket, SmallSet<Socket>> m_links;
 		SmallVector<Link> m_all_links;
@@ -169,7 +171,7 @@ namespace FN {
 		const Node *insert(const SharedFunction &function)
 		{
 			BLI_assert(this->can_modify());
-			const Node *node = new Node(function);
+			const Node *node = new Node(this, function);
 			this->m_nodes.add(node);
 			return node;
 		}
@@ -179,8 +181,7 @@ namespace FN {
 			BLI_assert(this->can_modify());
 			BLI_assert(a.node() != b.node());
 			BLI_assert(a.is_input() != b.is_input());
-			BLI_assert(m_nodes.contains(a.node()));
-			BLI_assert(m_nodes.contains(b.node()));
+			BLI_assert(a.graph() == this && b.graph() == this);
 
 			m_links.insert(Link::New(a, b));
 		}
@@ -211,11 +212,16 @@ namespace FN {
 		bool m_frozen = false;
 		SmallSet<const Node *> m_nodes;
 		GraphLinks m_links;
+
+		friend Node;
+		friend Socket;
 	};
 
+	using SharedDataFlowGraph = Shared<DataFlowGraph>;
 
-	/* Some inline functions.
-	 * Those can only come after the declaration of other types. */
+
+	/* Socket Inline Functions
+	 ********************************************** */
 
 	inline Socket Socket::Input(const Node *node, uint index)
 	{
@@ -229,4 +235,42 @@ namespace FN {
 		return Socket(node, true, index);
 	}
 
+	const Node *Socket::node() const
+	{
+		return this->m_node;
+	}
+
+	const DataFlowGraph *Socket::graph() const
+	{
+		return this->node()->graph();
+	}
+
+	bool Socket::is_input() const
+	{
+		return !this->m_is_output;
+	}
+
+	bool Socket::is_output() const
+	{
+		return this->m_is_output;
+	}
+
+	uint Socket::index() const
+	{
+		return this->m_index;
+	}
+
+	inline bool operator==(const Socket &a, const Socket &b)
+	{
+		return (
+			a.m_node == b.m_node &&
+			a.m_is_output == b.m_is_output &&
+			a.m_index == b.m_index);
+	}
+
+	Socket Socket::origin() const
+	{
+		return this->graph()->m_links.get_origin(*this);
+	}
+
 } /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/core/graph_to_function.cpp b/source/blender/functions/core/graph_to_function.cpp
new file mode 100644
index 00000000000..931f376081c
--- /dev/null
+++ b/source/blender/functions/core/graph_to_function.cpp
@@ -0,0 +1,82 @@
+#include "graph_to_function.hpp"
+#include "cpu.hpp"
+
+namespace FN {
+
+	

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list