GPencil: Code Cleanup - Simplify and clarify the code for subdividing a stroke

This commit is contained in:
Joshua Leung 2016-03-28 02:56:43 +13:00
parent a7538b19c6
commit e78a929d68
3 changed files with 29 additions and 31 deletions

@ -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);

@ -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);
}

@ -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];
/* 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);
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];
i++; /* add to loop to jump next pair */
/* Interpolate all values */
interp_v3_v3v3(&pt->x, &prev->x, &next->x, 0.5f);
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;
}
/* ******************************************************** */