[Bf-blender-cvs] [5d96ed364f2] greasepencil-edit-curve: GPencil: Apply GSoC changes

Antonio Vazquez noreply at git.blender.org
Thu Jul 23 17:56:28 CEST 2020


Commit: 5d96ed364f2704cfa32118ba256df306e8ceef92
Author: Antonio Vazquez
Date:   Thu Jul 23 17:54:11 2020 +0200
Branches: greasepencil-edit-curve
https://developer.blender.org/rB5d96ed364f2704cfa32118ba256df306e8ceef92

GPencil: Apply GSoC changes

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

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/blenkernel/BKE_gpencil_curve.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/blenkernel/intern/gpencil_curve.c
M	source/blender/blenkernel/intern/gpencil_geom.c
M	source/blender/blenloader/intern/versioning_290.c
M	source/blender/editors/gpencil/gpencil_edit.c
M	source/blender/editors/gpencil/gpencil_edit_curve.c
M	source/blender/editors/gpencil/gpencil_select.c
M	source/blender/editors/gpencil/gpencil_utils.c
M	source/blender/gpencil_modifiers/intern/MOD_gpencilbuild.c
M	source/blender/makesdna/DNA_gpencil_types.h
M	source/blender/makesrna/intern/rna_gpencil.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 238e0f499dc..22068264cb5 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -6990,6 +6990,7 @@ class VIEW3D_PT_gpencil_curve_edit(Panel):
 
         layout = self.layout
         col = layout.column(align=True)
+        col.prop(gpd, "use_adaptive_curve_resolution")
         col.prop(gpd, "edit_curve_resolution")
         col.prop(gpd, "curve_edit_threshold")
 
diff --git a/source/blender/blenkernel/BKE_gpencil_curve.h b/source/blender/blenkernel/BKE_gpencil_curve.h
index 3527d678355..c374a5b85bb 100644
--- a/source/blender/blenkernel/BKE_gpencil_curve.h
+++ b/source/blender/blenkernel/BKE_gpencil_curve.h
@@ -48,8 +48,11 @@ struct bGPDcurve *BKE_gpencil_stroke_editcurve_generate(struct bGPDstroke *gps,
 void BKE_gpencil_stroke_editcurve_update(struct bGPDstroke *gps, float error_threshold);
 void BKE_gpencil_editcurve_stroke_sync_selection(struct bGPDstroke *gps, struct bGPDcurve *gpc);
 void BKE_gpencil_stroke_editcurve_sync_selection(struct bGPDstroke *gps, struct bGPDcurve *gpc);
-void BKE_gpencil_selected_strokes_editcurve_update(struct bGPdata *gpd);
-void BKE_gpencil_stroke_update_geometry_from_editcurve(struct bGPDstroke *gps);
+void BKE_gpencil_strokes_selected_update_editcurve(struct bGPdata *gpd);
+void BKE_gpencil_strokes_selected_sync_selection_editcurve(struct bGPdata *gpd);
+void BKE_gpencil_stroke_update_geometry_from_editcurve(struct bGPDstroke *gps,
+                                                       const uint resolution,
+                                                       bool is_adaptive);
 void BKE_gpencil_editcurve_recalculate_handles(struct bGPDstroke *gps);
 void BKE_gpencil_editcurve_subdivide(struct bGPDstroke *gps, const int cuts);
 
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 7932be4000f..992efe4bdb5 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -536,6 +536,7 @@ bGPdata *BKE_gpencil_data_addnew(Main *bmain, const char name[])
   gpd->flag |= GP_DATA_VIEWALIGN;
   /* always enable object onion skin switch */
   gpd->flag |= GP_DATA_SHOW_ONIONSKINS;
+
   /* GP object specific settings */
   ARRAY_SET_ITEMS(gpd->line_color, 0.6f, 0.6f, 0.6f, 0.5f);
 
@@ -543,6 +544,8 @@ bGPdata *BKE_gpencil_data_addnew(Main *bmain, const char name[])
 
   gpd->editcurve_resolution = GP_DEFAULT_CURVE_RESOLUTION;
   gpd->curve_edit_threshold = GP_DEFAULT_CURVE_ERROR;
+  /* use adaptive curve resolution by default */
+  gpd->flag |= GP_DATA_CURVE_ADAPTIVE_RESOLUTION;
 
   gpd->zdepth_offset = 0.150f;
 
diff --git a/source/blender/blenkernel/intern/gpencil_curve.c b/source/blender/blenkernel/intern/gpencil_curve.c
index 3bd60746028..70d4b20f751 100644
--- a/source/blender/blenkernel/intern/gpencil_curve.c
+++ b/source/blender/blenkernel/intern/gpencil_curve.c
@@ -53,7 +53,7 @@
 
 #include "DEG_depsgraph_query.h"
 
-#define POINT_DIM 3
+#define COORD_FITTING_INFLUENCE 20.0f
 
 /* Helper: Check materials with same color. */
 static int gpencil_check_same_material_color(Object *ob_gp, float color[4], Material **r_mat)
@@ -401,6 +401,7 @@ static void gpencil_editstroke_deselect_all(bGPDcurve *gpc)
     gpc_pt->flag &= ~GP_CURVE_POINT_SELECT;
     BEZT_DESEL_ALL(bezt);
   }
+  gpc->flag &= ~GP_CURVE_SELECT;
 }
 
 /**
@@ -470,15 +471,27 @@ void BKE_gpencil_convert_curve(Main *bmain,
  */
 bGPDcurve *BKE_gpencil_stroke_editcurve_generate(bGPDstroke *gps, float error_threshold)
 {
+#define POINT_DIM 9
   if (gps->totpoints < 1) {
     return NULL;
   }
 
   float *points = MEM_callocN(sizeof(float) * gps->totpoints * POINT_DIM, __func__);
+  float diag_length = len_v3v3(gps->boundbox_min, gps->boundbox_max);
+  float tmp_vec[3];
+
   for (int i = 0; i < gps->totpoints; i++) {
     bGPDspoint *pt = &gps->points[i];
-    float *to = &points[i * POINT_DIM];
-    copy_v3_v3(to, &pt->x);
+    int row = i * POINT_DIM;
+
+    /* normalize coordinate to 0..1 */
+    sub_v3_v3v3(tmp_vec, &pt->x, gps->boundbox_min);
+    mul_v3_v3fl(&points[row], tmp_vec, COORD_FITTING_INFLUENCE / diag_length);
+    points[row + 3] = pt->pressure / diag_length;
+
+    /* strength and color are already normalized */
+    points[row + 4] = pt->strength / diag_length;
+    mul_v4_v4fl(&points[row + 5], pt->vert_color, 1.0f / diag_length);
   }
 
   uint calc_flag = CURVE_FIT_CALC_HIGH_QUALIY;
@@ -486,40 +499,50 @@ bGPDcurve *BKE_gpencil_stroke_editcurve_generate(bGPDstroke *gps, float error_th
     calc_flag |= CURVE_FIT_CALC_CYCLIC;
   }
 
+  /* TODO: make this a parameter */
+  float corner_angle = M_PI_2;
+
   float *r_cubic_array = NULL;
   unsigned int r_cubic_array_len = 0;
   unsigned int *r_cubic_orig_index = NULL;
   unsigned int *r_corners_index_array = NULL;
   unsigned int r_corners_index_len = 0;
-  int r = curve_fit_cubic_to_points_fl(points,
-                                       gps->totpoints,
-                                       POINT_DIM,
-                                       error_threshold,
-                                       calc_flag,
-                                       NULL,
-                                       0,
-                                       &r_cubic_array,
-                                       &r_cubic_array_len,
-                                       &r_cubic_orig_index,
-                                       &r_corners_index_array,
-                                       &r_corners_index_len);
+  int r = curve_fit_cubic_to_points_refit_fl(points,
+                                             gps->totpoints,
+                                             POINT_DIM,
+                                             error_threshold,
+                                             calc_flag,
+                                             NULL,
+                                             0,
+                                             corner_angle,
+                                             &r_cubic_array,
+                                             &r_cubic_array_len,
+                                             &r_cubic_orig_index,
+                                             &r_corners_index_array,
+                                             &r_corners_index_len);
 
   if (r != 0 || r_cubic_array_len < 1) {
     return NULL;
   }
 
+  uint curve_point_size = 3 * POINT_DIM;
+
   bGPDcurve *editcurve = BKE_gpencil_stroke_editcurve_new(r_cubic_array_len);
 
   for (int i = 0; i < r_cubic_array_len; i++) {
     bGPDcurve_point *cpt = &editcurve->curve_points[i];
     BezTriple *bezt = &cpt->bezt;
-    bGPDspoint *orig_pt = &gps->points[r_cubic_orig_index[i]];
+    float *curve_point = &r_cubic_array[i * curve_point_size];
+
     for (int j = 0; j < 3; j++) {
-      copy_v3_v3(bezt->vec[j], &r_cubic_array[i * 3 * POINT_DIM + j * 3]);
+      float *bez = &curve_point[j * POINT_DIM];
+      madd_v3_v3v3fl(bezt->vec[j], gps->boundbox_min, bez, diag_length / COORD_FITTING_INFLUENCE);
     }
-    cpt->pressure = orig_pt->pressure;
-    cpt->strength = orig_pt->strength;
-    copy_v4_v4(cpt->vert_color, orig_pt->vert_color);
+
+    float *ctrl_point = &curve_point[1 * POINT_DIM];
+    cpt->pressure = ctrl_point[3] * diag_length;
+    cpt->strength = ctrl_point[4] * diag_length;
+    mul_v4_v4fl(cpt->vert_color, &ctrl_point[5], diag_length);
 
     /* default handle type */
     bezt->h1 |= HD_ALIGN;
@@ -539,6 +562,7 @@ bGPDcurve *BKE_gpencil_stroke_editcurve_generate(bGPDstroke *gps, float error_th
     free(r_cubic_orig_index);
   }
 
+#undef POINT_DIM
   return editcurve;
 }
 
@@ -660,10 +684,11 @@ void BKE_gpencil_stroke_editcurve_sync_selection(bGPDstroke *gps, bGPDcurve *gpc
 static void gpencil_interpolate_fl_from_to(
     float from, float to, float *point_offset, int it, int stride)
 {
-  /* linear interpolation */
+  /* smooth interpolation */
   float *r = point_offset;
   for (int i = 0; i <= it; i++) {
     float fac = (float)i / (float)it;
+    fac = 3.0f * fac * fac - 2.0f * fac * fac * fac;  // smooth
     *r = interpf(to, from, fac);
     r = POINTER_OFFSET(r, stride);
   }
@@ -672,16 +697,31 @@ static void gpencil_interpolate_fl_from_to(
 static void gpencil_interpolate_v4_from_to(
     float from[4], float to[4], float *point_offset, int it, int stride)
 {
-  /* linear interpolation */
+  /* smooth interpolation */
   float *r = point_offset;
   for (int i = 0; i <= it; i++) {
     float fac = (float)i / (float)it;
+    fac = 3.0f * fac * fac - 2.0f * fac * fac * fac;  // smooth
     interp_v4_v4v4(r, from, to, fac);
     r = POINTER_OFFSET(r, stride);
   }
 }
 
-static void gpencil_calculate_stroke_points_curve_point(
+static float gpencil_approximate_curve_segment_arclength(bGPDcurve_point *cpt_start,
+                                                         bGPDcurve_point *cpt_end)
+{
+  BezTriple *bezt_start = &cpt_start->bezt;
+  BezTriple *bezt_end = &cpt_end->bezt;
+
+  float chord_len = len_v3v3(bezt_start->vec[1], bezt_end->vec[1]);
+  float net_len = len_v3v3(bezt_start->vec[1], bezt_start->vec[2]);
+  net_len += len_v3v3(bezt_start->vec[2], bezt_end->vec[0]);
+  net_len += len_v3v3(bezt_end->vec[0], bezt_end->vec[1]);
+
+  return (chord_len + net_len) / 2.0f;
+}
+
+static void gpencil_calculate_stroke_points_curve_segment(
     bGPDcurve_point *cpt, bGPDcurve_point *cpt_next, float *points_offset, int resolu, int stride)
 {
   /* sample points on all 3 axis between two curve points */
@@ -713,51 +753,141 @@ static void gpencil_calculate_stroke_points_curve_point(
                                  stride);
 }
 
-/**
- * Recalculate stroke points with the editcurve of the stroke.
- */
-void BKE_gpencil_stroke_update_geometry_from_editcurve(bGPDstroke *gps)
+static float *gpencil_stroke_points_from_editcurve_adaptive_resolu(
+    bGPDcurve_point *curve_point_array,
+    int curve_point_array_len,
+    int resolution,
+    bool is_cyclic,
+    int *r_points_len)
 {
-  if (gps == NULL || gps->editcurve == NULL) {
-    return;
+  /* One stride contains: x, y, z, pressure, strength, Vr, Vg, Vb, Vmix_fa

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list