[Bf-blender-cvs] [693311adc73] soc-2021-curves: Clean up, refactoring and commenting

Dilith Jayakody noreply at git.blender.org
Thu Feb 24 04:44:08 CET 2022


Commit: 693311adc7385a7bc72ad91d63e883521efe701f
Author: Dilith Jayakody
Date:   Sun Feb 13 16:33:22 2022 +0530
Branches: soc-2021-curves
https://developer.blender.org/rB693311adc7385a7bc72ad91d63e883521efe701f

Clean up, refactoring and commenting

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

M	source/blender/editors/curve/editcurve_pen.c

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

diff --git a/source/blender/editors/curve/editcurve_pen.c b/source/blender/editors/curve/editcurve_pen.c
index dc07ee6edb3..275fa70a31f 100644
--- a/source/blender/editors/curve/editcurve_pen.c
+++ b/source/blender/editors/curve/editcurve_pen.c
@@ -96,6 +96,15 @@ typedef struct CurvePenData {
   bool new_point;
   /* Whether a segment is being altered by click and drag. */
   bool spline_nearby;
+  /* Whether some action was done. Used for select. */
+  bool acted;
+  /* Whether a point was found underneath the mouse. */
+  bool found_point;
+  /* Whether multiple selected points should be moved. */
+  bool multi_point;
+  /* Whether a point has already been selected. */
+  bool selection_made;
+
   /* Whether shortcut for toggling free handles was pressed. */
   bool free_toggle_pressed;
   /* Whether the current handle type of the moved handle is free. */
@@ -106,14 +115,9 @@ typedef struct CurvePenData {
   bool link_handles;
   /* Whether the current state of the handle angle is locked. */
   bool lock_angle;
-  /* Whether some action was done. Used for select. */
-  bool acted;
-  /* Whether a point was found underneath the mouse. */
-  bool found_point;
-  /* Whether multiple selected points should be moved. */
-  bool multi_point;
-  /* Whether a point has already been selected. */
-  bool selection_made;
+  /* Whether the shortcut for moving the entire point is pressed. */
+  bool move_entire_pressed;
+
   /* Data about found point. Used for closing splines. */
   Nurb *nu;
   BezTriple *bezt;
@@ -270,7 +274,7 @@ static void remove_handle_movement_constraints(BezTriple *bezt, const bool f1, c
 }
 
 static void move_bezt_handle_or_vertex_to_location(BezTriple *bezt,
-                                                   const short bezt_idx,
+                                                   const int bezt_idx,
                                                    const float disp_2d[2],
                                                    const bool link_handles,
                                                    const bool lock_angle,
@@ -376,7 +380,6 @@ static bool get_selected_center(const ListBase *nurbs,
 /* Move all selected points by an amount equivalent to the distance moved by mouse. */
 static void move_all_selected_points(ListBase *nurbs,
                                      const bool bezt_only,
-                                     const bool move_entire,
                                      CurvePenData *cpd,
                                      const wmEvent *event,
                                      const ViewContext *vc)
@@ -388,6 +391,7 @@ static void move_all_selected_points(ListBase *nurbs,
 
   const bool link_handles = cpd->link_handles && !cpd->free_toggle;
   const bool lock_angle = cpd->lock_angle;
+  const bool move_entire = cpd->move_entire_pressed;
 
   float distance = 0.0f;
   if (lock_angle) {
@@ -492,12 +496,12 @@ static bool get_closest_vertex_to_point_in_nurbs(const ListBase *nurbs,
   *r_bp = NULL;
 
   float min_dist_bezt = FLT_MAX;
-  float min_dist_bp = FLT_MAX;
-
-  BezTriple *closest_bezt = NULL;
   short closest_handle = 0;
-  BPoint *closest_bp = NULL;
+  BezTriple *closest_bezt = NULL;
   Nurb *closest_bezt_nu = NULL;
+
+  float min_dist_bp = FLT_MAX;
+  BPoint *closest_bp = NULL;
   Nurb *closest_bp_nu = NULL;
 
   LISTBASE_FOREACH (Nurb *, nu, nurbs) {
@@ -506,11 +510,15 @@ static bool get_closest_vertex_to_point_in_nurbs(const ListBase *nurbs,
         BezTriple *bezt = &nu->bezt[i];
         float bezt_vec[2];
         int start = 0, end = 3;
+
+        /* Consider handles only if visible. Else only consider the middle point of the triple. */
         int handle_display = vc->v3d->overlay.handle_display;
         if (handle_display == CURVE_HANDLE_NONE ||
             (handle_display == CURVE_HANDLE_SELECTED && !BEZT_ISSEL_ANY(bezt))) {
           start = 1, end = 2;
         }
+
+        /* Loop over each of the 3 points of the #BezTriple and update data of closest bezt. */
         for (short j = start; j < end; j++) {
           if (worldspace_to_screenspace(bezt->vec[j], vc, bezt_vec)) {
             const float dist = len_manhattan_v2v2(bezt_vec, point);
@@ -528,6 +536,8 @@ static bool get_closest_vertex_to_point_in_nurbs(const ListBase *nurbs,
       for (int i = 0; i < nu->pntsu; i++) {
         BPoint *bp = &nu->bp[i];
         float bp_vec[2];
+
+        /* Update data of closest #BPoint. */
         if (worldspace_to_screenspace(bp->vec, vc, bp_vec)) {
           const float dist = len_manhattan_v2v2(bp_vec, point);
           if (dist < min_dist_bp) {
@@ -540,6 +550,7 @@ static bool get_closest_vertex_to_point_in_nurbs(const ListBase *nurbs,
     }
   }
 
+  /* Assign closest data to the returned variables. */
   const float threshold_dist = ED_view3d_select_dist_px() * sel_dist_mul;
   if (min_dist_bezt < threshold_dist || min_dist_bp < threshold_dist) {
     if (min_dist_bp < min_dist_bezt) {
@@ -588,6 +599,11 @@ static void calculate_new_bezier_point(const float point_prev[3],
   interp_v3_v3v3(new_right_handle, center_point, handle_next, parameter);
 }
 
+static bool is_cyclic(const Nurb *nu)
+{
+  return nu->flagu & CU_NURB_CYCLIC;
+}
+
 /* Insert a #BezTriple to a nurb at the location specified by `op_data`. */
 static void insert_bezt_to_nurb(Nurb *nu, const CutData *data, Curve *cu)
 {
@@ -613,7 +629,7 @@ static void insert_bezt_to_nurb(Nurb *nu, const CutData *data, Curve *cu)
   cu->actnu = get_nurb_index(BKE_curve_editNurbs_get(cu), nu);
 
   BezTriple *next_bezt;
-  if ((nu->flagu & CU_NURB_CYCLIC) && (index == nu->pntsu - 1)) {
+  if (is_cyclic(nu) && (index == nu->pntsu - 1)) {
     next_bezt = bezt1;
   }
   else {
@@ -667,7 +683,7 @@ static void insert_bp_to_nurb(Nurb *nu, const CutData *data, Curve *cu)
   cu->actnu = get_nurb_index(BKE_curve_editNurbs_get(cu), nu);
 
   BPoint *next_bp;
-  if ((nu->flagu & CU_NURB_CYCLIC) && (index == nu->pntsu - 1)) {
+  if (is_cyclic(nu) && (index == nu->pntsu - 1)) {
     next_bp = bp1;
   }
   else {
@@ -686,14 +702,21 @@ static void insert_bp_to_nurb(Nurb *nu, const CutData *data, Curve *cu)
   new_bp->f1 |= SELECT;
 }
 
+/* Update r_min_dist (minimum distance from point to edge), r_min_i (index of closest point on
+ * Nurb), and r_param (the fraction along the edge at which the closest point lies) based on the
+ * edge and the external point.
+ * `point`: External point
+ * `point1` & `point2`: The two ends of the edge
+ * `point_idx`: Index of the control point out of the points on the Nurb
+ * `resolu_idx`: Index of the edge on a Bezier segment (zero for non-Bezier edges) */
 static void get_updated_data_for_edge(const float point[2],
                                       const float point1[2],
                                       const float point2[2],
                                       const int point_idx,
                                       const int resolu_idx,
-                                      float *min_dist,
-                                      int *min_i,
-                                      float *param)
+                                      float *r_min_dist,
+                                      int *r_min_i,
+                                      float *r_param)
 {
   float edge[2], vec1[2], vec2[2];
   sub_v2_v2v2(edge, point1, point2);
@@ -703,42 +726,35 @@ static void get_updated_data_for_edge(const float point[2],
   const float len_vec2 = len_v2(vec2);
   const float dot1 = dot_v2v2(edge, vec1);
   const float dot2 = dot_v2v2(edge, vec2);
+
+  /* Signs of dot products being equal implies that the angles formed with the external point are
+   * either both acute or both obtuse, meaning the external point is closer to a point on the edge
+   * rather than an endpoint. */
   if (dot1 > 0 == dot2 > 0) {
     const float perp_dist = len_vec1 * sinf(angle_v2v2(vec1, edge));
-    if (*min_dist > perp_dist) {
-      *min_dist = perp_dist;
-      *min_i = point_idx;
-      *param = resolu_idx + len_vec1 * cos_v2v2v2(point, point1, point2) / len_v2(edge);
+    if (*r_min_dist > perp_dist) {
+      *r_min_dist = perp_dist;
+      *r_min_i = point_idx;
+      *r_param = resolu_idx + len_vec1 * cos_v2v2v2(point, point1, point2) / len_v2(edge);
     }
   }
   else {
-    if (*min_dist > len_vec2) {
-      *min_dist = len_vec2;
-      *min_i = point_idx;
-      *param = resolu_idx;
+    if (*r_min_dist > len_vec2) {
+      *r_min_dist = len_vec2;
+      *r_min_i = point_idx;
+      *r_param = resolu_idx;
     }
   }
 }
 
-static void get_updated_data_for_bp_edge(const float point[2],
-                                         const float point1[2],
-                                         const float point2[2],
-                                         const int point_idx,
-                                         float *min_dist,
-                                         int *min_i,
-                                         float *param)
-{
-  const int temp_resolu_idx = 0;
-  get_updated_data_for_edge(
-      point, point1, point2, point_idx, temp_resolu_idx, min_dist, min_i, param);
-}
-
+/* Update #CutData for a single Nurb. */
 static void update_cut_data_for_nurb(
     CutData *cd, Nurb *nu, const int resolu, const float point[2], const ViewContext *vc)
 {
   float min_dist = cd->min_dist, param = 0.0f;
   int min_i = 0;
-  const int end = nu->flagu & CU_NURB_CYCLIC ? nu->pntsu : nu->pntsu - 1;
+  const int end = is_cyclic(nu) ? nu->pntsu : nu->pntsu - 1;
+
   if (nu->type == CU_BEZIER) {
     for (int i = 0; i < end; i++) {
       float *points = MEM_mallocN(sizeof(float[3]) * (resolu + 1), __func__);
@@ -787,7 +803,7 @@ static void update_cut_data_for_nurb(
     worldspace_to_screenspace(nu->bp->vec, vc, point1);
     for (int i = 0; i < end; i++) {
       worldspace_to_screenspace((nu->bp + (i + 1) % nu->pntsu)->vec, vc, point2);
-      get_updated_data_for_bp_edge(point, point1, point2, i, &min_dist, &min_i, &param);
+      get_updated_data_for_edge(point, point1, point2, i, 0, &min_dist, &min_i, &param);
       copy_v2_v2(point1, point2);
     }
 
@@ -795,12 +811,12 @@ static void update_cut_data_for_nurb(
       cd->min_dist = min_dist;
       cd->nurb = nu;
       cd->bp_index = min_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list