[Bf-blender-cvs] [93fc7e4c3db] greasepencil-object: GPencil: New operator merge by distance (WIP)
Antonio Vazquez
noreply at git.blender.org
Wed Aug 7 13:11:30 CEST 2019
Commit: 93fc7e4c3dba7ab5c62d3de9c7767f62668c4649
Author: Antonio Vazquez
Date: Wed Aug 7 13:11:21 2019 +0200
Branches: greasepencil-object
https://developer.blender.org/rB93fc7e4c3dba7ab5c62d3de9c7767f62668c4649
GPencil: New operator merge by distance (WIP)
This operator joins points separated by a distance lower than factor. The point is moved to the middle location, except extremes that keep extreme location.
===================================================================
M release/scripts/startup/bl_ui/properties_grease_pencil_common.py
M source/blender/blenkernel/BKE_gpencil.h
M source/blender/blenkernel/intern/gpencil.c
M source/blender/editors/gpencil/gpencil_edit.c
M source/blender/editors/gpencil/gpencil_intern.h
M source/blender/editors/gpencil/gpencil_ops.c
===================================================================
diff --git a/release/scripts/startup/bl_ui/properties_grease_pencil_common.py b/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
index 1e5aac4cc0d..bef4f395233 100644
--- a/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
+++ b/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
@@ -606,6 +606,7 @@ class GPENCIL_MT_cleanup(Menu):
def draw(self, _context):
layout = self.layout
+ layout.operator("gpencil.stroke_merge_by_distance", text="Merge by Distance")
layout.operator("gpencil.frame_clean_loose", text="Loose Points")
layout.separator()
diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index affa1034a78..6533c596ca8 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -274,6 +274,10 @@ void BKE_gpencil_simplify_stroke(struct bGPDstroke *gps, float factor);
void BKE_gpencil_simplify_fixed(struct bGPDstroke *gps);
void BKE_gpencil_subdivide(struct bGPDstroke *gps, int level, int flag);
bool BKE_gpencil_trim_stroke(struct bGPDstroke *gps);
+void BKE_gpencil_merge_distance_stroke(struct bGPDframe *gpf,
+ struct bGPDstroke *gps,
+ const float threshold,
+ const bool use_unselected);
void BKE_gpencil_stroke_2d_flat(const struct bGPDspoint *points,
int totpoints,
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 7acb167dd46..82f1217a88d 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -2720,6 +2720,84 @@ void BKE_gpencil_dissolve_points(bGPDframe *gpf, bGPDstroke *gps, const short ta
}
}
+/* Merge by distance ------------------------------------- */
+/* Reduce a series of points when the distance is below a threshold.
+ * Special case for first and last points (both are keeped) for other points,
+ * the merge point is interpolated between points.
+ *
+ */
+void BKE_gpencil_merge_distance_stroke(bGPDframe *gpf,
+ bGPDstroke *gps,
+ const float threshold,
+ const bool use_unselected)
+{
+ float tagged = false;
+ /* Use square distance to speed up loop */
+ const float th_square = threshold * threshold;
+ /* Need to have something to merge. */
+ if (gps->totpoints < 2) {
+ return;
+ }
+ int i = 0;
+ int step = 1;
+ while ((i < gps->totpoints - 1) && (i + step < gps->totpoints)) {
+ bGPDspoint *pt = &gps->points[i];
+ if (pt->flag & GP_SPOINT_TAG) {
+ i++;
+ step = 1;
+ continue;
+ }
+ bGPDspoint *pt_next = &gps->points[i + step];
+ /* Do not recalc tagged points. */
+ if (pt_next->flag & GP_SPOINT_TAG) {
+ step++;
+ continue;
+ }
+ /* Check if contiguous points are selected. */
+ if (!use_unselected) {
+ if (((pt->flag & GP_SPOINT_SELECT) == 0) || ((pt_next->flag & GP_SPOINT_SELECT) == 0)) {
+ i++;
+ step = 1;
+ continue;
+ }
+ }
+ float len_square = len_squared_v3v3(&pt->x, &pt_next->x);
+ if (len_square <= th_square) {
+ tagged = true;
+ /* Interpolated factor depends if extremes or mid points. By default mid point. */
+ float factor = 0.5f;
+ /* For first point use, 1st extreme. */
+ if (i == 0) {
+ factor = 0.0f;
+ }
+ /* For last point use, 2nd extreme. */
+ else if (i == gps->totpoints - 2) {
+ factor = 1.0f;
+ }
+ /* Move first point to interpolated point. */
+ interp_v3_v3v3(&pt->x, &pt->x, &pt_next->x, factor);
+ if (i != gps->totpoints - 1) {
+ /* Tag second point for delete. */
+ pt_next->flag |= GP_SPOINT_TAG;
+ }
+ else {
+ pt->flag |= GP_SPOINT_TAG;
+ }
+ /* Jump to next pair of points, keeping first point segment equals.*/
+ step++;
+ }
+ else {
+ /* Analyze next point. */
+ i++;
+ step = 1;
+ }
+ }
+ /* Dissolve tagged points */
+ if (tagged) {
+ BKE_gpencil_dissolve_points(gpf, gps, GP_SPOINT_TAG);
+ }
+}
+
/* Helper function to check materials with same color */
static int gpencil_check_same_material_color(Object *ob_gp, float color[4], Material *r_mat)
{
diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c
index a9ba86de0a7..86ad6bc326a 100644
--- a/source/blender/editors/gpencil/gpencil_edit.c
+++ b/source/blender/editors/gpencil/gpencil_edit.c
@@ -4581,3 +4581,73 @@ bool ED_object_gpencil_exit(struct Main *bmain, Object *ob)
}
return ok;
}
+
+/* ** merge by distance *** */
+bool gp_merge_by_distance_poll(bContext *C)
+{
+ Object *ob = CTX_data_active_object(C);
+ if (ob == NULL) {
+ return false;
+ }
+ bGPdata *gpd = (bGPdata *)ob->data;
+ if (gpd == NULL) {
+ return false;
+ }
+
+ bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
+
+ return ((gpl != NULL) && (ob->mode == OB_MODE_EDIT_GPENCIL));
+}
+
+static int gp_merge_by_distance_exec(bContext *C, wmOperator *op)
+{
+ Object *ob = CTX_data_active_object(C);
+ bGPdata *gpd = (bGPdata *)ob->data;
+ const float threshold = RNA_float_get(op->ptr, "threshold");
+ const bool unselected = RNA_boolean_get(op->ptr, "use_unselected");
+
+ /* sanity checks */
+ if (ELEM(NULL, gpd)) {
+ return OPERATOR_CANCELLED;
+ }
+
+ /* Go through each editable selected stroke */
+ GP_EDITABLE_STROKES_BEGIN (gpstroke_iter, C, gpl, gps) {
+ if (gps->flag & GP_STROKE_SELECT) {
+ BKE_gpencil_merge_distance_stroke(gpf_, gps, threshold, unselected);
+ }
+ }
+ GP_EDITABLE_STROKES_END(gpstroke_iter);
+
+ /* notifiers */
+ DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
+ WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+
+ return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_stroke_merge_by_distance(wmOperatorType *ot)
+{
+ PropertyRNA *prop;
+
+ /* identifiers */
+ ot->name = "Merge by Distance";
+ ot->idname = "GPENCIL_OT_stroke_merge_by_distance";
+ ot->description = "Merge points by distance";
+
+ /* api callbacks */
+ ot->exec = gp_merge_by_distance_exec;
+ ot->poll = gp_merge_by_distance_poll;
+
+ /* flags */
+ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+ /* properties */
+ prop = RNA_def_float(ot->srna, "threshold", 0.001f, 0.0f, 100.0f, "Threshold", "", 0.0f, 100.0f);
+ /* avoid re-using last var */
+ RNA_def_property_flag(prop, PROP_SKIP_SAVE);
+
+ prop = RNA_def_boolean(
+ ot->srna, "use_unselected", 0, "Unselected", "Use whole stroke, not only selected points");
+ RNA_def_property_flag(prop, PROP_SKIP_SAVE);
+}
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index f9f4e31f5ce..e620804f15c 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -491,6 +491,7 @@ void GPENCIL_OT_stroke_smooth(struct wmOperatorType *ot);
void GPENCIL_OT_stroke_merge(struct wmOperatorType *ot);
void GPENCIL_OT_stroke_cutter(struct wmOperatorType *ot);
void GPENCIL_OT_stroke_trim(struct wmOperatorType *ot);
+void GPENCIL_OT_stroke_merge_by_distance(struct wmOperatorType *ot);
void GPENCIL_OT_brush_presets_create(struct wmOperatorType *ot);
diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c
index b22f96274b3..8cd525043ee 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -319,6 +319,7 @@ void ED_operatortypes_gpencil(void)
WM_operatortype_append(GPENCIL_OT_stroke_merge);
WM_operatortype_append(GPENCIL_OT_stroke_cutter);
WM_operatortype_append(GPENCIL_OT_stroke_trim);
+ WM_operatortype_append(GPENCIL_OT_stroke_merge_by_distance);
WM_operatortype_append(GPENCIL_OT_brush_presets_create);
More information about the Bf-blender-cvs
mailing list