diff options
author | Joshua Leung <aligorith@gmail.com> | 2016-03-27 16:56:43 +0300 |
---|---|---|
committer | Joshua Leung <aligorith@gmail.com> | 2016-03-27 17:21:29 +0300 |
commit | e78a929d68e1479f32cd4ffaaec6debfa91d9fe3 (patch) | |
tree | b9d4d106d90c8a7c2c76f5990411c8c04d367ae0 /source/blender/editors/gpencil | |
parent | a7538b19c6641be5e210a1f8286c5cd4ce945389 (diff) |
GPencil: Code Cleanup - Simplify and clarify the code for subdividing a stroke
Diffstat (limited to 'source/blender/editors/gpencil')
-rw-r--r-- | source/blender/editors/gpencil/gpencil_intern.h | 4 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_paint.c | 16 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_utils.c | 38 |
3 files changed, 28 insertions, 30 deletions
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h index c3fafaae9c3..368da618a1d 100644 --- a/source/blender/editors/gpencil/gpencil_intern.h +++ b/source/blender/editors/gpencil/gpencil_intern.h @@ -137,9 +137,9 @@ void gp_stroke_delete_tagged_points(bGPDframe *gpf, bGPDstroke *gps, bGPDstroke bool gp_smooth_stroke(bGPDstroke *gps, int i, float inf, bool affect_pressure); /** - * Subdivide a stroke + * Subdivide a stroke once, by adding points at the midpoint between each pair of points * \param gps Stroke data - * \param new_totpoints Total number of points + * \param new_totpoints Total number of points (after subdividing) */ void gp_subdivide_stroke(bGPDstroke *gps, const int new_totpoints); diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c index 740fb8e0096..ac949775fd0 100644 --- a/source/blender/editors/gpencil/gpencil_paint.c +++ b/source/blender/editors/gpencil/gpencil_paint.c @@ -572,7 +572,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) bGPDstroke *gps; bGPDspoint *pt; tGPspoint *ptc; - + int i, totelem; /* since strokes are so fine, when using their depth we need a margin otherwise they might get missed */ int depth_margin = (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 4 : 0; @@ -614,6 +614,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) /* allocate enough memory for a continuous array for storage points */ int sublevel = gpl->sublevel; int new_totpoints = gps->totpoints; + for (i = 0; i < sublevel; i++) { /* Avoid error if subdivide is too big (assume totpoints is right) */ if (new_totpoints + (new_totpoints - 1) > GP_STROKE_BUFFER_MAX) { @@ -624,7 +625,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) new_totpoints += new_totpoints - 1; } gps->points = MEM_callocN(sizeof(bGPDspoint) * new_totpoints, "gp_stroke_points"); - + /* set pointer to first non-initialized point */ pt = gps->points + (gps->totpoints - totelem); @@ -745,19 +746,22 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) /* subdivide the stroke */ if (sublevel > 0) { - int sub = gps->totpoints; + int totpoints = gps->totpoints; for (i = 0; i < sublevel; i++) { - sub += sub - 1; - gp_subdivide_stroke(gps, sub); + /* we're adding one new point between each pair of verts on each step */ + totpoints += totpoints - 1; + + gp_subdivide_stroke(gps, totpoints); } } + /* smooth stroke - only if there's something to do */ if (gpl->smooth_drawfac > 0.0f) { for (i = 0; i < gps->totpoints; i++) { gp_smooth_stroke(gps, i, gpl->smooth_drawfac, true); } } - + if (depth_arr) MEM_freeN(depth_arr); } diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c index 30558b00b9f..64cb0b5bb6a 100644 --- a/source/blender/editors/gpencil/gpencil_utils.c +++ b/source/blender/editors/gpencil/gpencil_utils.c @@ -606,40 +606,34 @@ bool gp_smooth_stroke(bGPDstroke *gps, int i, float inf, bool affect_pressure) } /** - * Subdivide a stroke + * Subdivide a stroke once, by adding a point half way between each pair of existing points * \param gps Stroke data - * \param new_totpoints Total number of points + * \param new_totpoints Total number of points (after subdividing) */ void gp_subdivide_stroke(bGPDstroke *gps, const int new_totpoints) { - int i; - /* Subdivide stroke adding a point half way existing points */ - bGPDspoint *pt_a; - bGPDspoint *pt_b; - bGPDspoint *pt_n; - - /* Move points to insert subdivision */ + /* Move points towards end of enlarged points array to leave space for new points */ int y = 1; - for (i = gps->totpoints - 1; i > 0; i--) { - pt_n = &gps->points[i]; - gps->points[new_totpoints - y] = *pt_n; - y = y + 2; + for (int i = gps->totpoints - 1; i > 0; i--) { + gps->points[new_totpoints - y] = gps->points[i]; + y += 2; } /* Create interpolated points */ - for (i = 0; i < new_totpoints - 1; i++) { - pt_a = &gps->points[i]; - pt_n = &gps->points[i + 1]; - pt_b = &gps->points[i + 2]; + for (int i = 0; i < new_totpoints - 1; i += 2) { + bGPDspoint *prev = &gps->points[i]; + bGPDspoint *pt = &gps->points[i + 1]; + bGPDspoint *next = &gps->points[i + 2]; + /* Interpolate all values */ - interp_v3_v3v3(&pt_n->x, &pt_a->x, &pt_b->x, 0.5f); - pt_n->pressure = interpf(pt_a->pressure, pt_b->pressure, 0.5f); - pt_n->time = interpf(pt_a->time, pt_b->time, 0.5f); + interp_v3_v3v3(&pt->x, &prev->x, &next->x, 0.5f); - i++; /* add to loop to jump next pair */ + pt->pressure = interpf(prev->pressure, next->pressure, 0.5f); + pt->time = interpf(prev->time, next->time, 0.5f); } - gps->totpoints = new_totpoints; /* Increase number of points */ + /* Update to new total number of points */ + gps->totpoints = new_totpoints; } /* ******************************************************** */ |