[Bf-blender-cvs] [b60850d3959] master: Cleanup: Deduplicate node link intersection test

Hans Goudey noreply at git.blender.org
Sun Sep 4 06:50:06 CEST 2022


Commit: b60850d3959534b49a2912d1fe775878fbe8a623
Author: Hans Goudey
Date:   Sat Sep 3 14:52:27 2022 -0500
Branches: master
https://developer.blender.org/rBb60850d3959534b49a2912d1fe775878fbe8a623

Cleanup: Deduplicate node link intersection test

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

M	source/blender/editors/space_node/node_add.cc
M	source/blender/editors/space_node/node_intern.hh
M	source/blender/editors/space_node/node_relationships.cc

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

diff --git a/source/blender/editors/space_node/node_add.cc b/source/blender/editors/space_node/node_add.cc
index 79d8b4fe52e..7e46877d0ba 100644
--- a/source/blender/editors/space_node/node_add.cc
+++ b/source/blender/editors/space_node/node_add.cc
@@ -104,7 +104,7 @@ bNode *add_static_node(const bContext &C, int type, const float2 &location)
 /** \name Add Reroute Operator
  * \{ */
 
-static std::optional<float2> path_link_intersection(const bNodeLink &link, const Span<float2> path)
+std::optional<float2> link_path_intersection(const bNodeLink &link, const Span<float2> path)
 {
   std::array<float2, NODE_LINK_RESOL + 1> coords;
   node_link_bezier_points_evaluated(link, coords);
@@ -166,7 +166,7 @@ static int add_reroute_exec(bContext *C, wmOperator *op)
     if (node_link_is_hidden_or_dimmed(region.v2d, *link)) {
       continue;
     }
-    const std::optional<float2> intersection = path_link_intersection(*link, path);
+    const std::optional<float2> intersection = link_path_intersection(*link, path);
     if (!intersection) {
       continue;
     }
diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh
index 92b2b82209b..011b9f6b631 100644
--- a/source/blender/editors/space_node/node_intern.hh
+++ b/source/blender/editors/space_node/node_intern.hh
@@ -236,6 +236,8 @@ void node_draw_link_bezier(const bContext &C,
 void node_link_bezier_points_evaluated(const bNodeLink &link,
                                        std::array<float2, NODE_LINK_RESOL + 1> &coords);
 
+std::optional<float2> link_path_intersection(const bNodeLink &link, Span<float2> path);
+
 void draw_nodespace_back_pix(const bContext &C,
                              ARegion &region,
                              SpaceNode &snode,
diff --git a/source/blender/editors/space_node/node_relationships.cc b/source/blender/editors/space_node/node_relationships.cc
index d06864f4a8b..7d59bb6cb0c 100644
--- a/source/blender/editors/space_node/node_relationships.cc
+++ b/source/blender/editors/space_node/node_relationships.cc
@@ -1291,28 +1291,6 @@ void NODE_OT_link_make(wmOperatorType *ot)
 
 /** \} */
 
-/* -------------------------------------------------------------------- */
-/** \name Node Link Intersect
- * \{ */
-
-static bool node_links_intersect(bNodeLink &link, const float mcoords[][2], int tot)
-{
-  std::array<float2, NODE_LINK_RESOL + 1> coords;
-  node_link_bezier_points_evaluated(link, coords);
-
-  for (int i = 0; i < tot - 1; i++) {
-    for (int b = 0; b < NODE_LINK_RESOL; b++) {
-      if (isect_seg_seg_v2(mcoords[i], mcoords[i + 1], coords[b], coords[b + 1]) > 0) {
-        return true;
-      }
-    }
-  }
-
-  return false;
-}
-
-/** \} */
-
 /* -------------------------------------------------------------------- */
 /** \name Cut Link Operator
  * \{ */
@@ -1321,24 +1299,22 @@ static int cut_links_exec(bContext *C, wmOperator *op)
 {
   Main &bmain = *CTX_data_main(C);
   SpaceNode &snode = *CTX_wm_space_node(C);
-  ARegion &region = *CTX_wm_region(C);
+  const ARegion &region = *CTX_wm_region(C);
 
-  int i = 0;
-  float mcoords[256][2];
+  Vector<float2> path;
   RNA_BEGIN (op->ptr, itemptr, "path") {
-    float loc[2];
-
-    RNA_float_get_array(&itemptr, "loc", loc);
-    UI_view2d_region_to_view(
-        &region.v2d, (int)loc[0], (int)loc[1], &mcoords[i][0], &mcoords[i][1]);
-    i++;
-    if (i >= 256) {
+    float2 loc_region;
+    RNA_float_get_array(&itemptr, "loc", loc_region);
+    float2 loc_view;
+    UI_view2d_region_to_view(&region.v2d, loc_region.x, loc_region.y, &loc_view.x, &loc_view.y);
+    path.append(loc_view);
+    if (path.size() >= 256) {
       break;
     }
   }
   RNA_END;
 
-  if (i == 0) {
+  if (path.is_empty()) {
     return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
   }
 
@@ -1355,7 +1331,7 @@ static int cut_links_exec(bContext *C, wmOperator *op)
       continue;
     }
 
-    if (node_links_intersect(*link, mcoords, i)) {
+    if (link_path_intersection(*link, path)) {
 
       if (!found) {
         /* TODO(sergey): Why did we kill jobs twice? */
@@ -1418,24 +1394,22 @@ static int mute_links_exec(bContext *C, wmOperator *op)
 {
   Main &bmain = *CTX_data_main(C);
   SpaceNode &snode = *CTX_wm_space_node(C);
-  ARegion &region = *CTX_wm_region(C);
+  const ARegion &region = *CTX_wm_region(C);
 
-  int i = 0;
-  float mcoords[256][2];
+  Vector<float2> path;
   RNA_BEGIN (op->ptr, itemptr, "path") {
-    float loc[2];
-
-    RNA_float_get_array(&itemptr, "loc", loc);
-    UI_view2d_region_to_view(
-        &region.v2d, (int)loc[0], (int)loc[1], &mcoords[i][0], &mcoords[i][1]);
-    i++;
-    if (i >= 256) {
+    float2 loc_region;
+    RNA_float_get_array(&itemptr, "loc", loc_region);
+    float2 loc_view;
+    UI_view2d_region_to_view(&region.v2d, loc_region.x, loc_region.y, &loc_view.x, &loc_view.y);
+    path.append(loc_view);
+    if (path.size() >= 256) {
       break;
     }
   }
   RNA_END;
 
-  if (i <= 1) {
+  if (path.is_empty()) {
     return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
   }
 
@@ -1448,7 +1422,7 @@ static int mute_links_exec(bContext *C, wmOperator *op)
       continue;
     }
     link->flag &= ~NODE_LINK_TEST;
-    if (node_links_intersect(*link, mcoords, i)) {
+    if (link_path_intersection(*link, path)) {
       tot++;
     }
   }
@@ -1462,7 +1436,7 @@ static int mute_links_exec(bContext *C, wmOperator *op)
       continue;
     }
 
-    if (node_links_intersect(*link, mcoords, i)) {
+    if (link_path_intersection(*link, path)) {
       nodeMuteLinkToggle(snode.edittree, link);
     }
   }



More information about the Bf-blender-cvs mailing list