[Bf-blender-cvs] [ad7c91b3463] functions: new compare function

Jacques Lucke noreply at git.blender.org
Mon Jul 8 17:57:37 CEST 2019


Commit: ad7c91b3463db9ac712b38b455c3aea07dbee5a3
Author: Jacques Lucke
Date:   Mon Jul 8 17:34:48 2019 +0200
Branches: functions
https://developer.blender.org/rBad7c91b3463db9ac712b38b455c3aea07dbee5a3

new compare function

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

A	release/scripts/startup/nodes/function_nodes/compare.py
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
A	source/blender/functions/functions/comparisons.cpp
A	source/blender/functions/functions/comparisons.hpp

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

diff --git a/release/scripts/startup/nodes/function_nodes/compare.py b/release/scripts/startup/nodes/function_nodes/compare.py
new file mode 100644
index 00000000000..bb318b413b5
--- /dev/null
+++ b/release/scripts/startup/nodes/function_nodes/compare.py
@@ -0,0 +1,40 @@
+import bpy
+from bpy.props import *
+from .. base import FunctionNode
+from .. socket_builder import SocketBuilder
+
+operation_items = [
+    ("LESS_THAN", "Less Than", "A < B", "", 1),
+]
+
+class CompareNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_CompareNode"
+    bl_label = "Compare"
+
+    search_terms = (
+        ("Less Than", {"operation" : "LESS_THAN"}),
+    )
+
+    operation: EnumProperty(
+        name="Operation",
+        items=operation_items,
+        update=FunctionNode.refresh,
+    )
+
+    use_list__a: SocketBuilder.VectorizedProperty()
+    use_list__b: SocketBuilder.VectorizedProperty()
+
+    def declaration(self, builder: SocketBuilder):
+        builder.vectorized_input(
+            "a", "use_list__a",
+            "A", "A", "Float")
+        builder.vectorized_input(
+            "b", "use_list__b",
+            "B", "B", "Float")
+
+        builder.vectorized_output(
+            "result", ["use_list__a", "use_list__b"],
+            "Result", "Result", "Boolean")
+
+    def draw(self, layout):
+        layout.prop(self, "operation", text="")
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index efca4734456..1b7f11fff28 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -126,6 +126,8 @@ set(SRC
   functions/auto_vectorization.cpp
   functions/ranges.hpp
   functions/ranges.cpp
+  functions/comparisons.hpp
+  functions/comparisons.cpp
 
   frontends/data_flow_nodes/builder.hpp
   frontends/data_flow_nodes/builder.cpp
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index 0f4a21ff8ab..6efb2675d94 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -8,4 +8,5 @@
 #include "functions/simple_conversions.hpp"
 #include "functions/switch.hpp"
 #include "functions/auto_vectorization.hpp"
-#include "functions/ranges.hpp"
\ No newline at end of file
+#include "functions/ranges.hpp"
+#include "functions/comparisons.hpp"
diff --git a/source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp b/source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
index 57a47afda0f..46f254d325b 100644
--- a/source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
@@ -255,6 +255,29 @@ static void INSERT_separate_vector(BTreeGraphBuilder &builder, bNode *bnode)
   builder.insert_matching_function(fn, bnode);
 }
 
+static SharedFunction &get_compare_function(int operation)
+{
+  switch (operation) {
+    case 1:
+      return Functions::GET_FN_less_than_float();
+    default:
+      BLI_assert(false);
+      return *(SharedFunction *)nullptr;
+  }
+}
+
+static void INSERT_compare(BTreeGraphBuilder &builder, bNode *bnode)
+{
+  PointerRNA rna = builder.get_rna(bnode);
+  int operation = RNA_enum_get(&rna, "operation");
+  SharedFunction fn = get_vectorized_function(
+      get_compare_function(operation),
+      rna,
+      {{"use_list__a", Functions::GET_FN_output_float_0()},
+       {"use_list__b", Functions::GET_FN_output_float_0()}});
+  builder.insert_matching_function(fn, bnode);
+}
+
 void register_node_inserters(GraphInserters &inserters)
 {
   inserters.reg_node_function("fn_VectorDistanceNode", Functions::GET_FN_vector_distance);
@@ -273,6 +296,7 @@ void register_node_inserters(GraphInserters &inserters)
   inserters.reg_node_inserter("fn_CallNode", INSERT_call);
   inserters.reg_node_inserter("fn_SwitchNode", INSERT_switch);
   inserters.reg_node_inserter("fn_ListLengthNode", INSERT_list_length);
+  inserters.reg_node_inserter("fn_CompareNode", INSERT_compare);
 }
 
 }  // namespace DataFlowNodes
diff --git a/source/blender/functions/functions/comparisons.cpp b/source/blender/functions/functions/comparisons.cpp
new file mode 100644
index 00000000000..b9bf5396a6c
--- /dev/null
+++ b/source/blender/functions/functions/comparisons.cpp
@@ -0,0 +1,47 @@
+#include "BLI_lazy_init.hpp"
+
+#include "FN_tuple_call.hpp"
+#include "FN_types.hpp"
+
+#include "comparisons.hpp"
+
+namespace FN {
+namespace Functions {
+
+using namespace Types;
+
+template<typename T> class LessThan : public TupleCallBody {
+  void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const
+  {
+    T a = fn_in.get<T>(0);
+    T b = fn_in.get<T>(1);
+    fn_out.set<bool>(0, a < b);
+  }
+};
+
+BLI_LAZY_INIT(SharedFunction, GET_FN_less_than_float)
+{
+  FunctionBuilder builder;
+  builder.add_input("A", GET_TYPE_float());
+  builder.add_input("B", GET_TYPE_float());
+  builder.add_output("A < B", GET_TYPE_bool());
+
+  auto fn = builder.build("Less Than (float)");
+  fn->add_body<LessThan<float>>();
+  return fn;
+}
+
+BLI_LAZY_INIT(SharedFunction, GET_FN_less_than_int32)
+{
+  FunctionBuilder builder;
+  builder.add_input("A", GET_TYPE_int32());
+  builder.add_input("B", GET_TYPE_int32());
+  builder.add_output("A < B", GET_TYPE_bool());
+
+  auto fn = builder.build("Less Than (int32)");
+  fn->add_body<LessThan<int32_t>>();
+  return fn;
+}
+
+}  // namespace Functions
+}  // namespace FN
diff --git a/source/blender/functions/functions/comparisons.hpp b/source/blender/functions/functions/comparisons.hpp
new file mode 100644
index 00000000000..a97cd1ce62b
--- /dev/null
+++ b/source/blender/functions/functions/comparisons.hpp
@@ -0,0 +1,12 @@
+#pragma once
+
+#include "FN_core.hpp"
+
+namespace FN {
+namespace Functions {
+
+SharedFunction &GET_FN_less_than_float();
+SharedFunction &GET_FN_less_than_int32();
+
+}  // namespace Functions
+}  // namespace FN



More information about the Bf-blender-cvs mailing list