[Bf-blender-cvs] [d66644dd871] functions: cleanup finding input and output nodes of function

Jacques Lucke noreply at git.blender.org
Thu Jul 4 16:46:04 CEST 2019


Commit: d66644dd871259a92efbf647f2e8a72fba03a93c
Author: Jacques Lucke
Date:   Thu Jul 4 13:57:05 2019 +0200
Branches: functions
https://developer.blender.org/rBd66644dd871259a92efbf647f2e8a72fba03a93c

cleanup finding input and output nodes of function

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

M	source/blender/blenkernel/BKE_node_tree.hpp
M	source/blender/blenlib/BLI_array_ref.hpp
M	source/blender/functions/frontends/data_flow_nodes/builder.cpp
M	source/blender/functions/frontends/data_flow_nodes/builder.hpp
M	source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp
M	tests/gtests/blenlib/BLI_array_ref_test.cc

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

diff --git a/source/blender/blenkernel/BKE_node_tree.hpp b/source/blender/blenkernel/BKE_node_tree.hpp
index 961582d6b82..e14230c4c20 100644
--- a/source/blender/blenkernel/BKE_node_tree.hpp
+++ b/source/blender/blenkernel/BKE_node_tree.hpp
@@ -66,6 +66,16 @@ class IndexedNodeTree {
     return rna;
   }
 
+  ArrayRef<bNode *> original_nodes() const
+  {
+    return m_original_nodes;
+  }
+
+  ArrayRef<bNodeLink *> original_links() const
+  {
+    return m_original_links;
+  }
+
   ArrayRef<SingleOriginLink> single_origin_links() const;
   ArrayRef<bNode *> nodes_with_idname(StringRef idname) const;
   ArrayRef<SocketWithNode> linked(bNodeSocket *bsocket) const;
diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp
index b41c2e78782..1143fd636ca 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -230,6 +230,17 @@ template<typename T> class ArrayRef {
     BLI_assert(m_size > 0);
     return m_start[m_size - 1];
   }
+
+  /**
+   * Get element at the given index. If the index is out of range, return the fallback value.
+   */
+  T get(uint index, const T &fallback)
+  {
+    if (index < m_size) {
+      return m_start[index];
+    }
+    return fallback;
+  }
 };
 
 template<typename ArrayT, typename ValueT, ValueT (*GetValue)(ArrayT &item)> class MappedArrayRef {
diff --git a/source/blender/functions/frontends/data_flow_nodes/builder.cpp b/source/blender/functions/frontends/data_flow_nodes/builder.cpp
index 1f912e91cce..0fec6f04ad9 100644
--- a/source/blender/functions/frontends/data_flow_nodes/builder.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/builder.cpp
@@ -209,6 +209,11 @@ bool BTreeGraphBuilder::verify_data_sockets_mapped(struct bNode *bnode) const
           this->check_if_sockets_are_mapped(bnode, bSocketList(&bnode->outputs)));
 }
 
+IndexedNodeTree &BTreeGraphBuilder::indexed_btree() const
+{
+  return m_indexed_btree;
+}
+
 struct bNodeTree *BTreeGraphBuilder::btree() const
 {
   return m_indexed_btree.btree();
diff --git a/source/blender/functions/frontends/data_flow_nodes/builder.hpp b/source/blender/functions/frontends/data_flow_nodes/builder.hpp
index dd3f48a89fe..96ea483f42e 100644
--- a/source/blender/functions/frontends/data_flow_nodes/builder.hpp
+++ b/source/blender/functions/frontends/data_flow_nodes/builder.hpp
@@ -51,6 +51,7 @@ class BTreeGraphBuilder {
   SharedType &type_by_name(const char *data_type) const;
 
   /* Query Node Tree */
+  IndexedNodeTree &indexed_btree() const;
   bNodeTree *btree() const;
   ID *btree_id() const;
 
diff --git a/source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp b/source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp
index 8b38190f337..58dd0ac7c1f 100644
--- a/source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp
@@ -25,26 +25,6 @@ static bool is_reroute_node(const bNode *bnode)
   return STREQ(bnode->idname, "NodeReroute");
 }
 
-static bNode *find_input_node(bNodeTree *btree)
-{
-  for (bNode *bnode : bNodeList(&btree->nodes)) {
-    if (is_input_node(bnode)) {
-      return bnode;
-    }
-  }
-  return nullptr;
-}
-
-static bNode *find_output_node(bNodeTree *btree)
-{
-  for (bNode *bnode : bNodeList(&btree->nodes)) {
-    if (is_output_node(bnode)) {
-      return bnode;
-    }
-  }
-  return nullptr;
-}
-
 static void insert_input_node(BTreeGraphBuilder &builder, bNode *bnode)
 {
   FunctionBuilder fn_builder;
@@ -75,20 +55,9 @@ static void insert_output_node(BTreeGraphBuilder &builder, bNode *bnode)
   builder.map_data_sockets(node, bnode);
 }
 
-struct BSocketLink {
-  bNodeSocket *from;
-  bNodeSocket *to;
-  bNodeLink *optional_source_link;
-
-  BSocketLink(bNodeSocket *from, bNodeSocket *to, bNodeLink *link = nullptr)
-      : from(from), to(to), optional_source_link(link)
-  {
-  }
-};
-
 static bool insert_functions_for_bnodes(BTreeGraphBuilder &builder, GraphInserters &inserters)
 {
-  for (bNode *bnode : bNodeList(&builder.btree()->nodes)) {
+  for (bNode *bnode : builder.indexed_btree().original_nodes()) {
     if (is_input_node(bnode) || is_output_node(bnode)) {
       continue;
     }
@@ -105,7 +74,8 @@ static bool insert_functions_for_bnodes(BTreeGraphBuilder &builder, GraphInserte
 
 static DFGB_SocketVector insert_function_input(BTreeGraphBuilder &builder)
 {
-  bNode *input_bnode = find_input_node(builder.btree());
+  bNode *input_bnode =
+      builder.indexed_btree().nodes_with_idname("fn_FunctionInputNode").get(0, nullptr);
   if (input_bnode == nullptr) {
     return {};
   }
@@ -122,7 +92,8 @@ static DFGB_SocketVector insert_function_input(BTreeGraphBuilder &builder)
 
 static DFGB_SocketVector insert_function_output(BTreeGraphBuilder &builder)
 {
-  bNode *output_bnode = find_output_node(builder.btree());
+  bNode *output_bnode =
+      builder.indexed_btree().nodes_with_idname("fn_FunctionOutputNode").get(0, nullptr);
   if (output_bnode == nullptr) {
     return {};
   }
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index b73dd60fd3f..ace7c3f91f2 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -227,3 +227,14 @@ TEST(array_ref, FirstLast_OneElement)
   EXPECT_EQ(a_ref.first(), 3);
   EXPECT_EQ(a_ref.last(), 3);
 }
+
+TEST(array_ref, Get)
+{
+  std::array<int, 3> a = {5, 6, 7};
+  IntArrayRef a_ref(a);
+  EXPECT_EQ(a_ref.get(0, 42), 5);
+  EXPECT_EQ(a_ref.get(1, 42), 6);
+  EXPECT_EQ(a_ref.get(2, 42), 7);
+  EXPECT_EQ(a_ref.get(3, 42), 42);
+  EXPECT_EQ(a_ref.get(4, 42), 42);
+}



More information about the Bf-blender-cvs mailing list