[Bf-blender-cvs] [88daf0fdf6e] greasepencil-edit-curve: GPencil: Apply all GSOC changes

Antonio Vazquez noreply at git.blender.org
Tue Jun 9 19:35:24 CEST 2020


Commit: 88daf0fdf6ec7a45932ad45c3cce95ca8a54362d
Author: Antonio Vazquez
Date:   Tue Jun 9 19:35:16 2020 +0200
Branches: greasepencil-edit-curve
https://developer.blender.org/rB88daf0fdf6ec7a45932ad45c3cce95ca8a54362d

GPencil:  Apply all GSOC changes

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

M	source/blender/blenkernel/BKE_gpencil.h
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/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/editors/include/ED_gpencil.h
M	source/blender/makesdna/DNA_gpencil_types.h
M	source/blender/makesrna/intern/rna_gpencil.c

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

diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index 3cf2a6dd259..f9d1290910f 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -159,6 +159,8 @@ struct bGPDstroke *BKE_gpencil_stroke_add_existing_style(struct bGPDframe *gpf,
                                                          int totpoints,
                                                          short thickness);
 
+struct bGPDcurve *BKE_gpencil_stroke_editcurve_new(int tot_curve_points);
+
 /* Stroke and Fill - Alpha Visibility Threshold */
 #define GPENCIL_ALPHA_OPACITY_THRESH 0.001f
 #define GPENCIL_STRENGTH_MIN 0.003f
diff --git a/source/blender/blenkernel/BKE_gpencil_curve.h b/source/blender/blenkernel/BKE_gpencil_curve.h
index cf6f9074bda..f62be42858a 100644
--- a/source/blender/blenkernel/BKE_gpencil_curve.h
+++ b/source/blender/blenkernel/BKE_gpencil_curve.h
@@ -31,6 +31,9 @@ extern "C" {
 struct Main;
 struct Object;
 struct Scene;
+struct bGPdata;
+struct bGPDstroke;
+struct bGPDcurve;
 
 void BKE_gpencil_convert_curve(struct Main *bmain,
                                struct Scene *scene,
@@ -40,6 +43,10 @@ void BKE_gpencil_convert_curve(struct Main *bmain,
                                const bool use_collections,
                                const bool only_stroke);
 
+struct bGPDcurve *BKE_gpencil_stroke_editcurve_generate(struct bGPDstroke *gps);
+void BKE_gpencil_stroke_editcurve_update(struct bGPDstroke *gps);
+void BKE_gpencil_selected_strokes_editcurve_update(struct bGPdata *gpd);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 2677c5a4110..e11cd74bb01 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -189,6 +189,7 @@ void BKE_gpencil_free_stroke_editcurve(bGPDstroke *gps)
   MEM_freeN(editcurve->curve_points);
   MEM_freeN(editcurve->point_index_array);
   MEM_freeN(editcurve);
+  gps->editcurve = NULL;
 }
 
 /* free stroke, doesn't unlink from any listbase */
@@ -622,6 +623,17 @@ bGPDstroke *BKE_gpencil_stroke_add_existing_style(
   return gps;
 }
 
+bGPDcurve *BKE_gpencil_stroke_editcurve_new(int tot_curve_points)
+{
+  bGPDcurve *new_gp_curve = (bGPDcurve *)MEM_callocN(sizeof(bGPDcurve), __func__);
+  new_gp_curve->tot_curve_points = tot_curve_points;
+  new_gp_curve->curve_points = (BezTriple *)MEM_callocN(sizeof(BezTriple) * tot_curve_points,
+                                                        __func__);
+  new_gp_curve->point_index_array = (int *)MEM_callocN(sizeof(int) * tot_curve_points, __func__);
+
+  return new_gp_curve;
+}
+
 /* ************************************************** */
 /* Data Duplication */
 
diff --git a/source/blender/blenkernel/intern/gpencil_curve.c b/source/blender/blenkernel/intern/gpencil_curve.c
index 8299943cc49..1c3ae16a2e5 100644
--- a/source/blender/blenkernel/intern/gpencil_curve.c
+++ b/source/blender/blenkernel/intern/gpencil_curve.c
@@ -39,6 +39,7 @@
 #include "DNA_gpencil_types.h"
 
 #include "BKE_collection.h"
+#include "BKE_context.h"
 #include "BKE_curve.h"
 #include "BKE_gpencil.h"
 #include "BKE_gpencil_curve.h"
@@ -47,8 +48,12 @@
 #include "BKE_material.h"
 #include "BKE_object.h"
 
+#include "curve_fit_nd.h"
+
 #include "DEG_depsgraph_query.h"
 
+#define POINT_DIM 3
+
 /* Helper: Check materials with same color. */
 static int gpencil_check_same_material_color(Object *ob_gp, float color[4], Material **r_mat)
 {
@@ -447,4 +452,114 @@ void BKE_gpencil_convert_curve(Main *bmain,
   DEG_id_tag_update(&gpd->id, ID_RECALC_GEOMETRY | ID_RECALC_COPY_ON_WRITE);
 }
 
+/**
+ * Creates a bGPDcurve by doing a cubic curve fitting on the grease pencil stroke points.
+ */
+bGPDcurve *BKE_gpencil_stroke_editcurve_generate(bGPDstroke *gps)
+{
+  if (gps->totpoints < 1) {
+    return NULL;
+  }
+
+  float *points = MEM_callocN(sizeof(float) * gps->totpoints * POINT_DIM, __func__);
+  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);
+  }
+
+  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,
+                                       0.1f,
+                                       CURVE_FIT_CALC_HIGH_QUALIY,
+                                       NULL,
+                                       0,
+                                       &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;
+  }
+
+  bGPDcurve *editcurve = BKE_gpencil_stroke_editcurve_new(r_cubic_array_len);
+
+  for (int i = 0; i < r_cubic_array_len; i++) {
+    BezTriple *bezt = &editcurve->curve_points[i];
+    for (int j = 0; j < 3; j++) {
+      copy_v3_v3(bezt->vec[j], &r_cubic_array[i * 3 * POINT_DIM + j * 3]);
+    }
+    editcurve->point_index_array[i] = r_cubic_orig_index[i];
+  }
+
+  MEM_freeN(points);
+  if (r_cubic_array) {
+    free(r_cubic_array);
+  }
+  if (r_corners_index_array) {
+    free(r_corners_index_array);
+  }
+  if (r_cubic_orig_index) {
+    free(r_cubic_orig_index);
+  }
+
+  return editcurve;
+}
+
+/**
+ * Updates the editcurve for a stroke.
+ */
+void BKE_gpencil_stroke_editcurve_update(bGPDstroke *gps)
+{
+  if (gps == NULL || gps->totpoints < 0 || gps->editcurve != NULL) {
+    return;
+  }
+
+  if (gps->editcurve != NULL) {
+    BKE_gpencil_free_stroke_editcurve(gps);
+  }
+
+  bGPDcurve *editcurve = BKE_gpencil_stroke_editcurve_generate(gps);
+  if (editcurve == NULL) {
+    return;
+  }
+  gps->editcurve = editcurve;
+}
+
+void BKE_gpencil_selected_strokes_editcurve_update(bGPdata *gpd)
+{
+  if (gpd == NULL) {
+    return;
+  }
+
+  const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
+
+  for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
+    if (!BKE_gpencil_layer_is_editable(gpl)) {
+      continue;
+    }
+    bGPDframe *init_gpf = (is_multiedit) ? gpl->frames.first : gpl->actframe;
+    for (bGPDframe *gpf = init_gpf; gpf; gpf = gpf->next) {
+      if ((gpf == gpl->actframe) || ((gpf->flag & GP_FRAME_SELECT) && is_multiedit)) {
+        for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
+          /* skip deselected stroke */
+          if (!(gps->flag & GP_STROKE_SELECT)) {
+            continue;
+          }
+
+          BKE_gpencil_stroke_editcurve_update(gps);
+        }
+      }
+    }
+  }
+}
+
 /** \} */
diff --git a/source/blender/editors/gpencil/gpencil_edit_curve.c b/source/blender/editors/gpencil/gpencil_edit_curve.c
index 016e80c42bb..03b2e5fb0bb 100644
--- a/source/blender/editors/gpencil/gpencil_edit_curve.c
+++ b/source/blender/editors/gpencil/gpencil_edit_curve.c
@@ -35,6 +35,7 @@
 
 #include "BKE_context.h"
 #include "BKE_gpencil.h"
+#include "BKE_gpencil_curve.h"
 
 #include "BLI_listbase.h"
 #include "BLI_math.h"
@@ -55,33 +56,12 @@
 /** \name Test Operator for curve editing
  * \{ */
 
-static bGPDcurve *create_example_gp_curve(int num_points)
-{
-  bGPDcurve *new_gp_curve = (bGPDcurve *)MEM_callocN(sizeof(bGPDcurve), __func__);
-  new_gp_curve->tot_curve_points = num_points;
-  new_gp_curve->curve_points = (BezTriple *)MEM_callocN(sizeof(BezTriple) * num_points, __func__);
-  new_gp_curve->point_index_array = (int *)MEM_callocN(sizeof(int) * num_points, __func__);
-
-  /* We just write some recognizable data to the BezTriple */
-  for (int i = 0; i < num_points; ++i) {
-    BezTriple *bezt = &new_gp_curve->curve_points[i];
-    for (int j = 0; j < 3; ++j) {
-      copy_v3_fl3(bezt->vec[j], i, j, i * j);
-    }
-    bezt->radius = 1.0f;
-    bezt->weight = 2.0f;
-
-    new_gp_curve->point_index_array[i] = i;
-  }
-  return new_gp_curve;
-}
-
 static int gp_write_stroke_curve_data_exec(bContext *C, wmOperator *op)
 {
   Object *ob = CTX_data_active_object(C);
   bGPdata *gpd = ob->data;
 
-  int num_points = RNA_int_get(op->ptr, "num_points");
+  // int num_points = RNA_int_get(op->ptr, "num_points");
 
   if (ELEM(NULL, gpd)) {
     return OPERATOR_CANCELLED;
@@ -98,7 +78,8 @@ static int gp_write_stroke_curve_data_exec(bContext *C, wmOperator *op)
       if (gps->editcurve != NULL) {
         BKE_gpencil_free_stroke_editcurve(gps);
       }
-      gps->editcurve = create_example_gp_curve(num_points);
+      BKE_gpencil_stroke_editcurve_update(gps);
+      gps->flag |= GP_STROKE_CURVE_MODE;
     }
   }
 
@@ -127,8 +108,8 @@ void GPENCIL_OT_write_sample_stroke_curve_data(wmOperatorType *ot)
   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
   /* properties */
-  prop = RNA_def_int(
-      ot->srna, "num_points", 2, 0, 100, "Curve points", "Number of test curve points", 0, 100);
+  // prop = RNA_def_int(
+  //     ot->srna, "num_points", 2, 0, 100, "Curve points", "Number of test curve points", 0, 100);
 }
 
 /** \} */
\ No newline at end of file
diff --git a/source/blender/editors/gpencil/gpencil_select.c b/source/blender/editors/gpencil/gpencil_select.c
index c41b2993a80..9b361ff2dea 100644
--- a/source/blender/editors/gpencil/gpencil_select.c
+++ b/source/blender/editors/gpencil/gpencil_select.c
@@ -172,7 +172,12 @@ static int gpencil_select_all_exec(bContext *C, wmOperator *op)
     }
   }
 
-  ED_gpencil_select_toggle_all(C, action);
+  if (GPENCIL_CURVE_EDIT_SESSIONS_ON(gpd)) {
+    ED_gpencil_select_curve_toggle_all(C, action);
+  }
+  else {
+    ED_gpencil_select_toggle_all(C, action);
+  }
 
   /* updates */
   DEG_id_tag_update(&gpd->id, ID_RECALC_GEOMETRY);
diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c
index 876fa7c9874..2aaa9a1e9c2 100644
--- a/source/blender/editors/gpencil/gpen

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list