[Bf-blender-cvs] [40d815dff65] master: Cleanup: Further split of node link Bezier calculation function

Hans Goudey noreply at git.blender.org
Fri Sep 2 22:44:22 CEST 2022


Commit: 40d815dff6515834c1bce40c0a34cc80b39655d5
Author: Hans Goudey
Date:   Fri Sep 2 15:25:34 2022 -0500
Branches: master
https://developer.blender.org/rB40d815dff6515834c1bce40c0a34cc80b39655d5

Cleanup: Further split of node link Bezier calculation function

Now dragged handles are handled separately, and the function returns a
statically sized array by value. The functions are also renamed to be
more consistent with curve naming elsewhere in Blender.

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

M	source/blender/editors/space_node/drawnode.cc
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/drawnode.cc b/source/blender/editors/space_node/drawnode.cc
index 541c82be2f3..cec33f4b3e2 100644
--- a/source/blender/editors/space_node/drawnode.cc
+++ b/source/blender/editors/space_node/drawnode.cc
@@ -1585,53 +1585,19 @@ void draw_nodespace_back_pix(const bContext &C,
   GPU_matrix_pop();
 }
 
-bool node_link_bezier_handles(const SpaceNode *snode,
-                              const bNodeLink &link,
-                              std::array<float2, 4> &points)
+static float2 socket_link_connection_location(const bNodeSocket &socket, const bNodeLink &link)
 {
-  float2 cursor = {0.0f, 0.0f};
-
-  /* this function can be called with snode null (via cut_links_intersect) */
-  /* XXX map snode->runtime->cursor back to view space */
-  if (snode) {
-    cursor = snode->runtime->cursor * UI_DPI_FAC;
-  }
-
-  /* in v0 and v3 we put begin/end points */
-  if (link.fromsock) {
-    points[0].x = link.fromsock->locx;
-    points[0].y = link.fromsock->locy;
-    if (link.fromsock->flag & SOCK_MULTI_INPUT) {
-      points[0] = node_link_calculate_multi_input_position(
-          {link.fromsock->locx, link.fromsock->locy},
-          link.fromsock->total_inputs - 1,
-          link.fromsock->total_inputs);
-    }
-  }
-  else {
-    if (snode == nullptr) {
-      return false;
-    }
-    points[0] = cursor;
-  }
-  if (link.tosock) {
-    points[3].x = link.tosock->locx;
-    points[3].y = link.tosock->locy;
-    if (!(link.tonode->flag & NODE_HIDDEN) && link.tosock->flag & SOCK_MULTI_INPUT) {
-      points[3] = node_link_calculate_multi_input_position({link.tosock->locx, link.tosock->locy},
-                                                           link.multi_input_socket_index,
-                                                           link.tosock->total_inputs);
-    }
-  }
-  else {
-    if (snode == nullptr) {
-      return false;
-    }
-    points[3] = cursor;
+  const float2 socket_location(socket.locx, socket.locy);
+  if (socket.flag & SOCK_MULTI_INPUT && socket.in_out == SOCK_IN) {
+    return node_link_calculate_multi_input_position(
+        socket_location, link.multi_input_socket_index, socket.total_inputs);
   }
+  return socket_location;
+}
 
+static void calculate_inner_link_bezier_points(std::array<float2, 4> &points)
+{
   const int curving = UI_GetThemeValueType(TH_NODE_CURVING, SPACE_NODE);
-
   if (curving == 0) {
     /* Straight line: align all points. */
     points[1] = math::interpolate(points[0], points[3], 1.0f / 3.0f);
@@ -1646,8 +1612,15 @@ bool node_link_bezier_handles(const SpaceNode *snode,
     points[2].x = points[3].x - dist;
     points[2].y = points[3].y;
   }
+}
 
-  return true;
+std::array<float2, 4> node_link_bezier_points(const bNodeLink &link)
+{
+  std::array<float2, 4> points;
+  points[0] = socket_link_connection_location(*link.fromsock, link);
+  points[3] = socket_link_connection_location(*link.tosock, link);
+  calculate_inner_link_bezier_points(points);
+  return points;
 }
 
 static bool node_link_draw_is_visible(const View2D &v2d, const std::array<float2, 4> &points)
@@ -1661,15 +1634,11 @@ static bool node_link_draw_is_visible(const View2D &v2d, const std::array<float2
   return true;
 }
 
-bool node_link_bezier_points(const SpaceNode *snode,
-                             const bNodeLink &link,
-                             float coord_array[][2],
-                             const int resol)
+bool node_link_bezier_points_evaluated(const bNodeLink &link,
+                                       float coord_array[][2],
+                                       const int resol)
 {
-  std::array<float2, 4> points;
-  if (!node_link_bezier_handles(snode, link, points)) {
-    return false;
-  }
+  const std::array<float2, 4> points = node_link_bezier_points(link);
 
   /* always do all three, to prevent data hanging around */
   BKE_curve_forward_diff_bezier(points[0].x,
@@ -2182,10 +2151,7 @@ void node_draw_link_bezier(const bContext &C,
                            const int th_col3,
                            const bool selected)
 {
-  std::array<float2, 4> points;
-  if (!node_link_bezier_handles(&snode, link, points)) {
-    return;
-  }
+  const std::array<float2, 4> points = node_link_bezier_points(link);
   if (!node_link_draw_is_visible(v2d, points)) {
     return;
   }
@@ -2241,6 +2207,17 @@ void node_draw_link(const bContext &C,
   node_draw_link_bezier(C, v2d, snode, link, th_col1, th_col2, th_col3, selected);
 }
 
+static std::array<float2, 4> node_link_bezier_points_dragged(const SpaceNode &snode,
+                                                             const bNodeLink &link)
+{
+  const float2 cursor = snode.runtime->cursor * UI_DPI_FAC;
+  std::array<float2, 4> points;
+  points[0] = link.fromsock ? socket_link_connection_location(*link.fromsock, link) : cursor;
+  points[3] = link.tosock ? socket_link_connection_location(*link.tosock, link) : cursor;
+  calculate_inner_link_bezier_points(points);
+  return points;
+}
+
 void node_draw_link_dragged(const bContext &C,
                             const View2D &v2d,
                             const SpaceNode &snode,
@@ -2250,10 +2227,7 @@ void node_draw_link_dragged(const bContext &C,
     return;
   }
 
-  std::array<float2, 4> points;
-  if (!node_link_bezier_handles(&snode, link, points)) {
-    return;
-  }
+  const std::array<float2, 4> points = node_link_bezier_points_dragged(snode, link);
 
   const NodeLinkDrawConfig draw_config = nodelink_get_draw_config(
       C, v2d, snode, link, TH_ACTIVE, TH_ACTIVE, TH_WIRE, true);
diff --git a/source/blender/editors/space_node/node_add.cc b/source/blender/editors/space_node/node_add.cc
index fcbb45a48f4..37db3296ec9 100644
--- a/source/blender/editors/space_node/node_add.cc
+++ b/source/blender/editors/space_node/node_add.cc
@@ -108,7 +108,7 @@ static bool add_reroute_intersect_check(const bNodeLink &link,
 {
   float coord_array[NODE_LINK_RESOL + 1][2];
 
-  if (node_link_bezier_points(nullptr, link, coord_array, NODE_LINK_RESOL)) {
+  if (node_link_bezier_points_evaluated(link, coord_array, NODE_LINK_RESOL)) {
     for (int i = 0; i < tot - 1; i++) {
       for (int b = 0; b < NODE_LINK_RESOL; b++) {
         if (isect_seg_seg_v2_point(
diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh
index 52e6521370e..5b376618912 100644
--- a/source/blender/editors/space_node/node_intern.hh
+++ b/source/blender/editors/space_node/node_intern.hh
@@ -229,16 +229,12 @@ void node_draw_link_bezier(const bContext &C,
                            int th_col2,
                            int th_col3,
                            bool selected);
-bool node_link_bezier_points(const SpaceNode *snode,
-                             const bNodeLink &link,
-                             float coord_array[][2],
-                             int resol);
+bool node_link_bezier_points_evaluated(const bNodeLink &link, float coord_array[][2], int resol);
 /**
  * Return quadratic beziers points for a given nodelink.
  */
-bool node_link_bezier_handles(const SpaceNode *snode,
-                              const bNodeLink &ink,
-                              std::array<float2, 4> &points);
+std::array<float2, 4> node_link_bezier_points(const bNodeLink &link);
+
 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 aac05fb1d25..c1f18e16d70 100644
--- a/source/blender/editors/space_node/node_relationships.cc
+++ b/source/blender/editors/space_node/node_relationships.cc
@@ -137,8 +137,7 @@ static void pick_input_link_by_link_intersect(const bContext &C,
   LISTBASE_FOREACH (bNodeLink *, link, &snode->edittree->links) {
     if (link->tosock == socket) {
       /* Test if the cursor is near a link. */
-      std::array<float2, 4> points;
-      node_link_bezier_handles(snode, *link, points);
+      const std::array<float2, 4> points = node_link_bezier_points(*link);
 
       std::array<float2, NODE_LINK_RESOL + 1> data;
       BKE_curve_forward_diff_bezier(points[0].x,
@@ -1325,7 +1324,7 @@ static bool node_links_intersect(bNodeLink &link, const float mcoords[][2], int
 {
   float coord_array[NODE_LINK_RESOL + 1][2];
 
-  if (node_link_bezier_points(nullptr, link, coord_array, NODE_LINK_RESOL)) {
+  if (node_link_bezier_points_evaluated(link, coord_array, NODE_LINK_RESOL)) {
     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], coord_array[b], coord_array[b + 1]) > 0) {
@@ -1965,7 +1964,7 @@ void ED_node_link_intersect_test(ScrArea *area, int test)
       continue;
     }
 
-    if (node_link_bezier_points(nullptr, *link, coord_array, NODE_LINK_RESOL)) {
+    if (node_link_bezier_points_evaluated(*link, coord_array, NODE_LINK_RESOL)) {
       float dist = FLT_MAX;
 
       /* loop over link coords to find shortest dist to



More information about the Bf-blender-cvs mailing list