Beispiel #1
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;
}
/*
 *  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;
}