diff options
author | Antonio Vazquez <blendergit@gmail.com> | 2022-09-05 12:02:39 +0300 |
---|---|---|
committer | Antonio Vazquez <blendergit@gmail.com> | 2022-09-05 12:03:12 +0300 |
commit | 38508f511023c60c6c16016f8825bb2ad9fad79f (patch) | |
tree | 0098335d6913af941621598af48b8c367f54c173 | |
parent | 91b1ca5b7b55cde3d344b03a1eba57df139e0f7c (diff) |
GPencil: New BKE function to set stroke start point
This function allows to set the start point for cyclic strokes.
The function is required by a new modifier and operator
that are currently under development.
-rw-r--r-- | source/blender/blenkernel/BKE_gpencil_geom.h | 8 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/gpencil_geom.cc | 104 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_edit.c | 2 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_utils.c | 2 |
4 files changed, 74 insertions, 42 deletions
diff --git a/source/blender/blenkernel/BKE_gpencil_geom.h b/source/blender/blenkernel/BKE_gpencil_geom.h index 33735fddf55..88244ec9d64 100644 --- a/source/blender/blenkernel/BKE_gpencil_geom.h +++ b/source/blender/blenkernel/BKE_gpencil_geom.h @@ -406,7 +406,13 @@ void BKE_gpencil_stroke_join(struct bGPDstroke *gps_a, struct bGPDstroke *gps_b, bool leave_gaps, bool fit_thickness, - bool smooth); + bool smooth, + bool auto_flip); +/** + * Set stroke start point in the selected index. Only works for Cyclic strokes. + * \param start_idx: Index of the point to be the start point. + */ +void BKE_gpencil_stroke_start_set(struct bGPdata *gpd, struct bGPDstroke *gps, int start_idx); /** * Copy the stroke of the frame to all frames selected (except current). */ diff --git a/source/blender/blenkernel/intern/gpencil_geom.cc b/source/blender/blenkernel/intern/gpencil_geom.cc index 2f1049999d2..2cc752dca47 100644 --- a/source/blender/blenkernel/intern/gpencil_geom.cc +++ b/source/blender/blenkernel/intern/gpencil_geom.cc @@ -3415,7 +3415,8 @@ void BKE_gpencil_stroke_join(bGPDstroke *gps_a, bGPDstroke *gps_b, const bool leave_gaps, const bool fit_thickness, - const bool smooth) + const bool smooth, + bool auto_flip) { bGPDspoint point; bGPDspoint *pt; @@ -3432,52 +3433,54 @@ void BKE_gpencil_stroke_join(bGPDstroke *gps_a, return; } - /* define start and end points of each stroke */ - float start_a[3], start_b[3], end_a[3], end_b[3]; - pt = &gps_a->points[0]; - copy_v3_v3(start_a, &pt->x); + if (auto_flip) { + /* define start and end points of each stroke */ + float start_a[3], start_b[3], end_a[3], end_b[3]; + pt = &gps_a->points[0]; + copy_v3_v3(start_a, &pt->x); - pt = &gps_a->points[gps_a->totpoints - 1]; - copy_v3_v3(end_a, &pt->x); + pt = &gps_a->points[gps_a->totpoints - 1]; + copy_v3_v3(end_a, &pt->x); - pt = &gps_b->points[0]; - copy_v3_v3(start_b, &pt->x); + pt = &gps_b->points[0]; + copy_v3_v3(start_b, &pt->x); - pt = &gps_b->points[gps_b->totpoints - 1]; - copy_v3_v3(end_b, &pt->x); + pt = &gps_b->points[gps_b->totpoints - 1]; + copy_v3_v3(end_b, &pt->x); - /* Check if need flip strokes. */ - float dist = len_squared_v3v3(end_a, start_b); - bool flip_a = false; - bool flip_b = false; - float lowest = dist; + /* Check if need flip strokes. */ + float dist = len_squared_v3v3(end_a, start_b); + bool flip_a = false; + bool flip_b = false; + float lowest = dist; - dist = len_squared_v3v3(end_a, end_b); - if (dist < lowest) { - lowest = dist; - flip_a = false; - flip_b = true; - } + dist = len_squared_v3v3(end_a, end_b); + if (dist < lowest) { + lowest = dist; + flip_a = false; + flip_b = true; + } - dist = len_squared_v3v3(start_a, start_b); - if (dist < lowest) { - lowest = dist; - flip_a = true; - flip_b = false; - } + dist = len_squared_v3v3(start_a, start_b); + if (dist < lowest) { + lowest = dist; + flip_a = true; + flip_b = false; + } - dist = len_squared_v3v3(start_a, end_b); - if (dist < lowest) { - lowest = dist; - flip_a = true; - flip_b = true; - } + dist = len_squared_v3v3(start_a, end_b); + if (dist < lowest) { + lowest = dist; + flip_a = true; + flip_b = true; + } - if (flip_a) { - BKE_gpencil_stroke_flip(gps_a); - } - if (flip_b) { - BKE_gpencil_stroke_flip(gps_b); + if (flip_a) { + BKE_gpencil_stroke_flip(gps_a); + } + if (flip_b) { + BKE_gpencil_stroke_flip(gps_b); + } } /* don't visibly link the first and last points? */ @@ -3540,6 +3543,29 @@ void BKE_gpencil_stroke_join(bGPDstroke *gps_a, } } +void BKE_gpencil_stroke_start_set(bGPdata *gpd, bGPDstroke *gps, int start_idx) +{ + if ((start_idx < 1) || (start_idx >= gps->totpoints)) { + return; + } + + /* Only cyclic strokes. */ + if ((gps->flag & GP_STROKE_CYCLIC) == 0) { + return; + } + + bGPDstroke *gps_b = BKE_gpencil_stroke_duplicate(gps, true, false); + BKE_gpencil_stroke_trim_points(gps_b, 0, start_idx - 0); + BKE_gpencil_stroke_trim_points(gps, start_idx, gps->totpoints - 1); + + /* Join both strokes. */ + BKE_gpencil_stroke_join(gps, gps_b, false, false, false, false); + + BKE_gpencil_stroke_geometry_update(gpd, gps); + + BKE_gpencil_free_stroke(gps_b); +} + void BKE_gpencil_stroke_copy_to_keyframes( bGPdata *gpd, bGPDlayer *gpl, bGPDframe *gpf, bGPDstroke *gps, const bool tail) { diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c index b826f033cc0..106f4acd6b7 100644 --- a/source/blender/editors/gpencil/gpencil_edit.c +++ b/source/blender/editors/gpencil/gpencil_edit.c @@ -3650,7 +3650,7 @@ static int gpencil_stroke_join_exec(bContext *C, wmOperator *op) } elem = &strokes_list[i]; /* Join new_stroke and stroke B. */ - BKE_gpencil_stroke_join(gps_new, elem->gps, leave_gaps, true, false); + BKE_gpencil_stroke_join(gps_new, elem->gps, leave_gaps, true, false, true); elem->used = true; } diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c index 7b659511aaa..c905404061c 100644 --- a/source/blender/editors/gpencil/gpencil_utils.c +++ b/source/blender/editors/gpencil/gpencil_utils.c @@ -3189,7 +3189,7 @@ bGPDstroke *ED_gpencil_stroke_join_and_trim( /* Join both strokes. */ int totpoint = gps_final->totpoints; - BKE_gpencil_stroke_join(gps_final, gps, false, true, true); + BKE_gpencil_stroke_join(gps_final, gps, false, true, true, true); /* Select the join points and merge if the distance is very small. */ pt = &gps_final->points[totpoint - 1]; |