[Bf-blender-cvs] [c20e336835d] soc-2021-curves: Cleanup and bug fix

dilithjay noreply at git.blender.org
Thu Aug 19 10:54:46 CEST 2021


Commit: c20e336835d6bf7fc82ccf2e842dcf2c76695322
Author: dilithjay
Date:   Thu Aug 19 14:24:19 2021 +0530
Branches: soc-2021-curves
https://developer.blender.org/rBc20e336835d6bf7fc82ccf2e842dcf2c76695322

Cleanup and bug fix

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

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 1854bb1513d..7f2e0e87af3 100644
--- a/source/blender/editors/curve/editcurve_pen.c
+++ b/source/blender/editors/curve/editcurve_pen.c
@@ -47,23 +47,23 @@
 
 /* Data structure to keep track of details about the cut location */
 typedef struct CutData {
-  /* Index of the last bez triple before the cut. */
+  /* Index of the last BezTriple or BPoint before the cut. */
   int bezt_index, bp_index;
   /* Nurb to which the cut belongs to. */
   Nurb *nurb;
   /* Minimum distance to curve from mouse location. */
   float min_dist;
-  /* Ratio at which the new point divides the curve segment. */
+  /* Fraction of segments after which the new point divides the curve segment. */
   float parameter;
-  /* Whether the cut has any vertices before/after it. */
+  /* Whether the currently identified closest point has any vertices before/after it. */
   bool has_prev, has_next;
-  /* Locations of adjacent vertices. */
+  /* Locations of adjacent vertices and cut location. */
   float prev_loc[3], cut_loc[3], next_loc[3];
-  /* Mouse location as floats. */
+  /* Mouse location in floats. */
   float mval[2];
 } CutData;
 
-/* Data required segment altering functionality. */
+/* Data required for segment altering functionality. */
 typedef struct MoveSegmentData {
   /* Nurb being altered. */
   Nurb *nu;
@@ -85,7 +85,7 @@ static void mouse_location_to_worldspace(const int mouse_loc[2],
   mul_m4_v3(imat, r_location);
 }
 
-/* Move the handle of BezTriple to mouse based on the previously added point. */
+/* Move the handle of BezTriple to mouse based on the newly added point. */
 static void move_bezt_handles_to_mouse(BezTriple *bezt,
                                        const bool is_end_point,
                                        const wmEvent *event,
@@ -158,7 +158,9 @@ static void free_up_handles_for_movement(BezTriple *bezt, bool f1, bool f3)
 }
 
 /* Move BezTriple to mouse. */
-static void move_selected_bezt_to_mouse(BezTriple *bezt, ViewContext *vc, const wmEvent *event)
+static void move_selected_bezt_to_mouse(BezTriple *bezt,
+                                        const ViewContext *vc,
+                                        const wmEvent *event)
 {
   /* Get mouse location in 3D space. */
   float location[3];
@@ -192,8 +194,36 @@ static void move_bp_to_mouse(BPoint *bp, const wmEvent *event, const ViewContext
   copy_v3_v3(bp->vec, location);
 }
 
+static int get_nurb_index(ListBase *nurbs, Nurb *nurb)
+{
+  int index = 0;
+  LISTBASE_FOREACH (Nurb *, nu, nurbs) {
+    if (nu == nurb) {
+      return index;
+    }
+    index++;
+  }
+  /* The nurb should've been found by now. */
+  BLI_assert(false);
+  return -1;
+}
+
+static void delete_nurb(Curve *cu, Nurb *nu)
+{
+  EditNurb *editnurb = cu->editnurb;
+  ListBase *nubase = &editnurb->nurbs;
+  const int nuindex = get_nurb_index(nubase, nu);
+  if (cu->actnu == nuindex) {
+    cu->actnu = CU_ACT_NONE;
+  }
+
+  BLI_remlink(nubase, nu);
+  BKE_nurb_free(nu);
+  nu = NULL;
+}
+
 /* Delete given BezTriple from given Nurb. */
-static void delete_bezt_from_nurb(BezTriple *bezt, Nurb *nu)
+static void delete_bezt_from_nurb(const BezTriple *bezt, Nurb *nu)
 {
   BLI_assert(nu->type == CU_BEZIER);
   int index = BKE_curve_nurb_vert_index_get(nu, bezt);
@@ -202,7 +232,7 @@ static void delete_bezt_from_nurb(BezTriple *bezt, Nurb *nu)
 }
 
 /* Delete given BPoint from given Nurb. */
-static void delete_bp_from_nurb(BPoint *bp, Nurb *nu)
+static void delete_bp_from_nurb(const BPoint *bp, Nurb *nu)
 {
   BLI_assert(nu->type == CU_NURBS);
   int index = BKE_curve_nurb_vert_index_get(nu, bp);
@@ -212,7 +242,7 @@ static void delete_bp_from_nurb(BPoint *bp, Nurb *nu)
 
 /* Get the closest point on an edge to a given point based on perpendicular distance. Return true
  * if closest point on curve.  */
-static bool get_closest_point_on_edge(float point[3],
+static bool get_closest_point_on_edge(float r_point[3],
                                       const float pos[2],
                                       const float pos1[3],
                                       const float pos2[3],
@@ -244,24 +274,24 @@ static bool get_closest_point_on_edge(float point[3],
 
     float pos_dif[3];
     sub_v3_v3v3(pos_dif, pos2, pos1);
-    madd_v3_v3v3fl(point, pos1, pos_dif, *factor);
+    madd_v3_v3v3fl(r_point, pos1, pos_dif, *factor);
     return true;
   }
   if (len_manhattan_v2(vec1) < len_manhattan_v2(vec2)) {
-    copy_v3_v3(point, pos1);
+    copy_v3_v3(r_point, pos1);
     return false;
   }
-  copy_v3_v3(point, pos2);
+  copy_v3_v3(r_point, pos2);
   return false;
 }
 
-/* Get closest control point in all nurbs in given ListBase to a given point. */
-static void get_closest_cp_to_point_in_nurbs(ListBase *nurbs,
-                                             Nurb **r_nu,
-                                             BezTriple **r_bezt,
-                                             BPoint **r_bp,
-                                             const float point[2],
-                                             const ViewContext *vc)
+/* Get closest vertex in all nurbs in given ListBase to a given point. */
+static void get_closest_vertex_to_point_in_nurbs(ListBase *nurbs,
+                                                 Nurb **r_nu,
+                                                 BezTriple **r_bezt,
+                                                 BPoint **r_bp,
+                                                 const float point[2],
+                                                 const ViewContext *vc)
 {
   float min_distance_bezt = FLT_MAX;
   float min_distance_bp = FLT_MAX;
@@ -317,13 +347,13 @@ static void get_closest_cp_to_point_in_nurbs(ListBase *nurbs,
 }
 
 /* Update CutData with location of closest vertex on curve. */
-static void update_data_if_nearest_point_in_segment(BezTriple *bezt1,
-                                                    BezTriple *bezt2,
-                                                    Nurb *nu,
-                                                    int index,
-                                                    ViewContext *vc,
-                                                    float screen_co[2],
-                                                    void *op_data)
+static void update_data_if_closest_vertex_in_segment(BezTriple *bezt1,
+                                                     BezTriple *bezt2,
+                                                     Nurb *nu,
+                                                     const int index,
+                                                     const ViewContext *vc,
+                                                     float screen_co[2],
+                                                     void *op_data)
 {
 
   CutData *data = op_data;
@@ -331,7 +361,7 @@ static void update_data_if_nearest_point_in_segment(BezTriple *bezt1,
   float resolu = nu->resolu;
   float *points = MEM_mallocN(sizeof(float[3]) * (resolu + 1), "makeCut_bezier");
 
-  /* Calculate all points on curve. TODO: Get existing . */
+  /* Calculate all points on curve. */
   for (int j = 0; j < 3; j++) {
     BKE_curve_forward_diff_bezier(bezt1->vec[1][j],
                                   bezt1->vec[2][j],
@@ -342,9 +372,7 @@ static void update_data_if_nearest_point_in_segment(BezTriple *bezt1,
                                   sizeof(float[3]));
   }
 
-  /* Calculate angle for middle points */
   for (int k = 0; k <= resolu; k++) {
-    /* Convert point to screen coordinates */
     bool check = ED_view3d_project_float_object(vc->region,
                                                 points + 3 * k,
                                                 screen_co,
@@ -375,8 +403,8 @@ static void update_data_if_nearest_point_in_segment(BezTriple *bezt1,
   MEM_freeN(points);
 }
 
-/* Update the closest point in the data structure. */
-static void update_closest_point_in_data(void *op_data, int resolution, ViewContext *vc)
+/* Update the closest location as cut location in data. */
+static void update_cut_loc_in_data(void *op_data, const ViewContext *vc)
 {
   CutData *data = op_data;
   bool found_min = false;
@@ -398,7 +426,7 @@ static void update_closest_point_in_data(void *op_data, int resolution, ViewCont
         vc->region, point, point_2d, V3D_PROJ_RET_CLIP_BB | V3D_PROJ_RET_CLIP_WIN);
     float dist = len_manhattan_v2v2(point_2d, data->mval);
     data->min_dist = dist;
-    data->parameter += factor / resolution;
+    data->parameter += factor / data->nurb->resolu;
     copy_v3_v3(data->cut_loc, point);
   }
 }
@@ -447,7 +475,7 @@ static void calculate_new_bezier_point(const float point_prev[3],
 }
 
 /* Update the nearest point data for all nurbs. */
-static void update_data_for_all_nurbs(ListBase *nurbs, ViewContext *vc, void *op_data)
+static void update_data_for_all_nurbs(const ListBase *nurbs, const ViewContext *vc, void *op_data)
 {
   CutData *data = op_data;
 
@@ -468,11 +496,11 @@ static void update_data_for_all_nurbs(ListBase *nurbs, ViewContext *vc, void *op
       BezTriple *bezt = NULL;
       for (i = 0; i < nu->pntsu - 1; i++) {
         bezt = &nu->bezt[i];
-        update_data_if_nearest_point_in_segment(bezt, bezt + 1, nu, i, vc, screen_co, data);
+        update_data_if_closest_vertex_in_segment(bezt, bezt + 1, nu, i, vc, screen_co, data);
       }
 
       if (nu->flagu & CU_NURB_CYCLIC && bezt) {
-        update_data_if_nearest_point_in_segment(bezt + 1, nu->bezt, nu, i, vc, screen_co, data);
+        update_data_if_closest_vertex_in_segment(bezt + 1, nu->bezt, nu, i, vc, screen_co, data);
       }
     }
     else if (nu->bp) {
@@ -509,10 +537,10 @@ static void update_data_for_all_nurbs(ListBase *nurbs, ViewContext *vc, void *op
 }
 
 /* Insert a bezt to a nurb at the location specified by op_data. */
-static void add_bezt_to_nurb(Nurb *nu, void *op_data, Curve *cu)
+static void add_bezt_to_nurb(Nurb *nu, const void *op_data, Curve *cu)
 {
   EditNurb *editnurb = cu->editnurb;
-  CutData *data = op_data;
+  const CutData *data = op_data;
 
   BezTriple *bezt1 = (BezTriple *)MEM_mallocN((nu->pntsu + 1) * sizeof(BezT

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list