Example #1
0
vw::Vector2 tangent(PointContour c, PointContour::iterator p) {
    vw::Vector2 t;
    PointContour::iterator b = c.begin();
    PointContour::iterator e = --(c.end());
    if (p == b) {
        t = *(++p) - *(--p);
    } else if (p == e) {
        t = *(--p) - *(++p);
    } else {
        vw::Vector2 v1, v2;
        v1 = *(--p) - *(++p);
        v2 = *p - *(++p);
        t = vw::Vector2((v1[0] + v2[0])/2.0, (v1[1] + v2[1])/2.0);
    }
    normalize(t);
    return t;
}
/*
 *  FitCurve :
 *      Fit a Bezier curve to a set of digitized points
 */
BezierContour FitCurve(PointContour &contour, double error)
{
    Vector2 tHat1, tHat2;   /*  Unit tangent vectors at endpoints */
    PointContour::iterator iter;
    BezierContour bezContour;
    int i = 0;
    int nPts = contour.size();

    Point2 *d = (Point2 *)malloc(nPts * sizeof(Point2));
    for (iter = contour.begin(); iter != contour.end(); ++iter) {
        Point2 a;
        a.x = (*iter)[0]; a.y = (*iter)[1];
        d[i++] = a;
    }

    tHat1 = ComputeLeftTangent(d, 0);
    tHat2 = ComputeRightTangent(d, nPts - 1);
    FitCubic(d, 0, nPts - 1, tHat1, tHat2, error, bezContour);
    free(d);
    return bezContour;
}
Example #3
0
inline bool closed(PointContour& c) {
    return c.front() == c.back();
}
Example #4
0
bool join(PointContour& c1, PointContour& c2) {
    ContourPoint c1_front = c1.front();
    ContourPoint c1_back = c1.back();
    ContourPoint c2_front = c2.front();
    ContourPoint c2_back = c2.back();

    if (c1_back == c2_front) {
        c1.splice(c1.end(), c2);
    }
    else if (c1_front == c2_back) {
        c1.splice(c1.begin(), c2);
    }
    else if (c1_back == c2_back) {
        if (c2.size() < c1.size()) {
            c2.reverse();
            c1.splice(c1.end(), c2);
        }
        else {
            c1.reverse();
            c1.splice(c1.begin(), c2);
        }
    }
    else if (c1_front == c2_front) {
        if (c2.size() < c1.size()) {
            c2.reverse();
            c1.splice(c1.begin(), c2);
        }
        else {
            c1.reverse();
            c1.splice(c1.end(), c2);
        }
    }
    else
        return false;

    return true;
}