static void gp_stroke_to_path(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu, float minmax_weights[2], const float rad_fac, bool stitch, const bool add_start_point, const bool add_end_point, tGpTimingData *gtd) { bGPDspoint *pt; Nurb *nu = (curnu) ? *curnu : NULL; BPoint *bp, *prev_bp = NULL; const bool do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE); const int add_start_end_points = (add_start_point ? 1 : 0) + (add_end_point ? 1 : 0); int i, old_nbp = 0; /* create new 'nurb' or extend current one within the curve */ if (nu) { old_nbp = nu->pntsu; /* If stitch, the first point of this stroke is already present in current nu. * Else, we have to add two additional points to make the zero-radius link between strokes. */ BKE_nurb_points_add(nu, gps->totpoints + (stitch ? -1 : 2) + add_start_end_points); } else { nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_path(nurb)"); nu->pntsu = gps->totpoints + add_start_end_points; nu->pntsv = 1; nu->orderu = 2; /* point-to-point! */ nu->type = CU_NURBS; nu->flagu = CU_NURB_ENDPOINT; nu->resolu = cu->resolu; nu->resolv = cu->resolv; nu->knotsu = NULL; nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * nu->pntsu, "bpoints"); stitch = false; /* Security! */ } if (do_gtd) { gp_timing_data_set_nbr(gtd, nu->pntsu); } /* If needed, make the link between both strokes with two zero-radius additional points */ /* About "zero-radius" point interpolations: * - If we have at least two points in current curve (most common case), we linearly extrapolate * the last segment to get the first point (p1) position and timing. * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point * with the first point of the current stroke. * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated * if it exists, else (if the stroke is a single point), linear interpolation with last curve point... */ if (curnu && !stitch && old_nbp) { float p1[3], p2[3], p[3], next_p[3]; float dt1 = 0.0f, dt2 = 0.0f; BLI_assert(gps->prev != NULL); prev_bp = NULL; if ((old_nbp > 1) && (gps->prev->totpoints > 1)) { /* Only use last curve segment if previous stroke was not a single-point one! */ prev_bp = &nu->bp[old_nbp - 2]; } bp = &nu->bp[old_nbp - 1]; /* First point */ gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect); if (prev_bp) { interp_v3_v3v3(p1, bp->vec, prev_bp->vec, -GAP_DFAC); if (do_gtd) { const int idx = gps->prev->totpoints - 1; dt1 = interpf(gps->prev->points[idx - 1].time, gps->prev->points[idx].time, -GAP_DFAC); } } else { interp_v3_v3v3(p1, bp->vec, p, GAP_DFAC); if (do_gtd) { dt1 = interpf(gps->inittime - gps->prev->inittime, 0.0f, GAP_DFAC); } } bp++; gp_stroke_to_path_add_point(gtd, bp, p1, (bp - 1)->vec, do_gtd, gps->prev->inittime, dt1, 0.0f, rad_fac, minmax_weights); /* Second point */ /* Note dt2 is always negative, which marks the gap. */ if (gps->totpoints > 1) { gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect); interp_v3_v3v3(p2, p, next_p, -GAP_DFAC); if (do_gtd) { dt2 = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); } } else { interp_v3_v3v3(p2, p, bp->vec, GAP_DFAC); if (do_gtd) { dt2 = interpf(gps->prev->inittime - gps->inittime, 0.0f, GAP_DFAC); } } bp++; gp_stroke_to_path_add_point(gtd, bp, p2, p1, do_gtd, gps->inittime, dt2, 0.0f, rad_fac, minmax_weights); old_nbp += 2; } else if (add_start_point) { float p[3], next_p[3]; float dt = 0.0f; gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect); if (gps->totpoints > 1) { gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect); interp_v3_v3v3(p, p, next_p, -GAP_DFAC); if (do_gtd) { dt = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); } } else { p[0] -= GAP_DFAC; /* Rather arbitrary... */ dt = -GAP_DFAC; /* Rather arbitrary too! */ } bp = &nu->bp[old_nbp]; /* Note we can't give anything else than 0.0 as time here, since a negative one (which would be expected value) * would not work (it would be *before* gtd->inittime, which is not supported currently). */ gp_stroke_to_path_add_point(gtd, bp, p, p, do_gtd, gps->inittime, dt, 0.0f, rad_fac, minmax_weights); old_nbp++; } if (old_nbp) { prev_bp = &nu->bp[old_nbp - 1]; } /* add points */ for (i = (stitch) ? 1 : 0, pt = &gps->points[(stitch) ? 1 : 0], bp = &nu->bp[old_nbp]; i < gps->totpoints; i++, pt++, bp++) { float p[3]; float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC; /* get coordinates to add at */ gp_strokepoint_convertcoords(C, gps, pt, p, subrect); gp_stroke_to_path_add_point(gtd, bp, p, (prev_bp) ? prev_bp->vec : p, do_gtd, gps->inittime, pt->time, width, rad_fac, minmax_weights); prev_bp = bp; } if (add_end_point) { float p[3]; float dt = 0.0f; if (gps->totpoints > 1) { interp_v3_v3v3(p, prev_bp->vec, (prev_bp - 1)->vec, -GAP_DFAC); if (do_gtd) { const int idx = gps->totpoints - 1; dt = interpf(gps->points[idx - 1].time, gps->points[idx].time, -GAP_DFAC); } } else { copy_v3_v3(p, prev_bp->vec); p[0] += GAP_DFAC; /* Rather arbitrary... */ dt = GAP_DFAC; /* Rather arbitrary too! */ } /* Note bp has already been incremented in main loop above, so it points to the right place. */ gp_stroke_to_path_add_point(gtd, bp, p, prev_bp->vec, do_gtd, gps->inittime, dt, 0.0f, rad_fac, minmax_weights); } /* add nurb to curve */ if (!curnu || !*curnu) { BLI_addtail(&cu->nurb, nu); } if (curnu) { *curnu = nu; } BKE_nurb_knot_calc_u(nu); }
/* convert stroke to 3d path */ static void gp_stroke_to_path(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu, float minmax_weights[2], float rad_fac, int stitch, tGpTimingData *gtd) { bGPDspoint *pt; Nurb *nu = (curnu) ? *curnu : NULL; BPoint *bp, *prev_bp = NULL; const int do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE); int i, old_nbp = 0; /* create new 'nurb' or extend current one within the curve */ if (nu) { old_nbp = nu->pntsu; /* If stitch, the first point of this stroke is already present in current nu. * Else, we have to add to additional points to make the zero-radius link between strokes. */ BKE_nurb_points_add(nu, gps->totpoints + (stitch ? -1 : 2)); } else { nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_path(nurb)"); nu->pntsu = gps->totpoints; nu->pntsv = 1; nu->orderu = 2; /* point-to-point! */ nu->type = CU_NURBS; nu->flagu = CU_NURB_ENDPOINT; nu->resolu = cu->resolu; nu->resolv = cu->resolv; nu->knotsu = NULL; nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * nu->pntsu, "bpoints"); stitch = FALSE; /* Security! */ } if (do_gtd) { _gp_timing_data_set_nbr(gtd, nu->pntsu); } /* If needed, make the link between both strokes with two zero-radius additional points */ /* About "zero-radius" point interpolations: * - If we have at least two points in current curve (most common case), we linearly extrapolate * the last segment to get the first point (p1) position and timing. * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point * with the first point of the current stroke. * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated * if it exists, else (if the stroke is a single point), linear interpolation with last curve point... */ if (curnu && !stitch && old_nbp) { float p1[3], p2[3], p[3], next_p[3]; float delta_time; prev_bp = NULL; if ((old_nbp > 1) && gps->prev && (gps->prev->totpoints > 1)) { /* Only use last curve segment if previous stroke was not a single-point one! */ prev_bp = nu->bp + old_nbp - 2; } bp = nu->bp + old_nbp - 1; /* XXX We do this twice... Not sure it's worth to bother about this! */ gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect); if (prev_bp) { interp_v3_v3v3(p1, prev_bp->vec, bp->vec, 1.0f + GAP_DFAC); } else { interp_v3_v3v3(p1, bp->vec, p, GAP_DFAC); } if (gps->totpoints > 1) { /* XXX We do this twice... Not sure it's worth to bother about this! */ gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect); interp_v3_v3v3(p2, p, next_p, -GAP_DFAC); } else { interp_v3_v3v3(p2, p, bp->vec, GAP_DFAC); } /* First point */ bp++; copy_v3_v3(bp->vec, p1); bp->vec[3] = 1.0f; bp->f1 = SELECT; minmax_weights[0] = bp->radius = bp->weight = 0.0f; if (do_gtd) { if (prev_bp) { delta_time = gtd->tot_time + (gtd->tot_time - gtd->times[gtd->cur_point - 1]) * GAP_DFAC; } else { delta_time = gtd->tot_time + (((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC; } gp_timing_data_add_point(gtd, gtd->inittime, delta_time, len_v3v3((bp - 1)->vec, p1)); } /* Second point */ bp++; copy_v3_v3(bp->vec, p2); bp->vec[3] = 1.0f; bp->f1 = SELECT; minmax_weights[0] = bp->radius = bp->weight = 0.0f; if (do_gtd) { /* This negative delta_time marks the gap! */ if (gps->totpoints > 1) { delta_time = ((gps->points + 1)->time - gps->points->time) * -GAP_DFAC; } else { delta_time = -(((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC; } gp_timing_data_add_point(gtd, gps->inittime, delta_time, len_v3v3(p1, p2)); } old_nbp += 2; } if (old_nbp && do_gtd) { prev_bp = nu->bp + old_nbp - 1; } /* add points */ for (i = (stitch) ? 1 : 0, pt = gps->points + ((stitch) ? 1 : 0), bp = nu->bp + old_nbp; i < gps->totpoints; i++, pt++, bp++) { float p3d[3]; float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC; /* get coordinates to add at */ gp_strokepoint_convertcoords(C, gps, pt, p3d, subrect); copy_v3_v3(bp->vec, p3d); bp->vec[3] = 1.0f; /* set settings */ bp->f1 = SELECT; bp->radius = width * rad_fac; bp->weight = width; CLAMP(bp->weight, 0.0f, 1.0f); if (bp->weight < minmax_weights[0]) { minmax_weights[0] = bp->weight; } else if (bp->weight > minmax_weights[1]) { minmax_weights[1] = bp->weight; } /* Update timing data */ if (do_gtd) { gp_timing_data_add_point(gtd, gps->inittime, pt->time, (prev_bp) ? len_v3v3(prev_bp->vec, p3d) : 0.0f); } prev_bp = bp; } /* add nurb to curve */ if (!curnu || !*curnu) { BLI_addtail(&cu->nurb, nu); } if (curnu) { *curnu = nu; } BKE_nurb_knot_calc_u(nu); }