From b60850d3959534b49a2912d1fe775878fbe8a623 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Sat, 3 Sep 2022 14:52:27 -0500 Subject: Cleanup: Deduplicate node link intersection test --- source/blender/editors/space_node/node_add.cc | 4 +- source/blender/editors/space_node/node_intern.hh | 2 + .../editors/space_node/node_relationships.cc | 68 +++++++--------------- 3 files changed, 25 insertions(+), 49 deletions(-) (limited to 'source') 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 path_link_intersection(const bNodeLink &link, const Span path) +std::optional link_path_intersection(const bNodeLink &link, const Span path) { std::array 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 intersection = path_link_intersection(*link, path); + const std::optional 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 &coords); +std::optional link_path_intersection(const bNodeLink &link, Span path); + void draw_nodespace_back_pix(const bContext &C, ARegion ®ion, 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 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 ®ion = *CTX_wm_region(C); + const ARegion ®ion = *CTX_wm_region(C); - int i = 0; - float mcoords[256][2]; + Vector path; RNA_BEGIN (op->ptr, itemptr, "path") { - float loc[2]; - - RNA_float_get_array(&itemptr, "loc", loc); - UI_view2d_region_to_view( - ®ion.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(®ion.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 ®ion = *CTX_wm_region(C); + const ARegion ®ion = *CTX_wm_region(C); - int i = 0; - float mcoords[256][2]; + Vector path; RNA_BEGIN (op->ptr, itemptr, "path") { - float loc[2]; - - RNA_float_get_array(&itemptr, "loc", loc); - UI_view2d_region_to_view( - ®ion.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(®ion.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); } } -- cgit v1.2.3