ejmahler / SplineLibrary

A library to collect many useful spline functions into one place.
BSD 2-Clause "Simplified" License
269 stars 50 forks source link

cubic spline tangent #18

Open baronzzl opened 3 years ago

baronzzl commented 3 years ago

Hello I try to use get cubic spline tangent, then compute normal dir of tangent to offset spline. but it strange as showing follow image: 图片 What may this problem?

ejmahler commented 3 years ago

It would help to have some more information. What is the green line? What is the black line? And what are the red lines?

Could you share the code you used to make this picture?

baronzzl commented 3 years ago

thanks for your reply. green line create from the black line offseting alone normal dir. The red line is normal dir. normal dir caculate from control point's tangent. Cubic b spline is right, show as below, but natural spline is error, show as above. 图片 code as below:

void RadiusCompensate2D(const Point2Ds &_closedSpline, double offset, bool bInner,
    Point2Ds & offsetSpline, Point2Ds *offsetDirs)
{
    TIMED_SCOPE(t, "半径补偿耗时");
    Point2Ds closedSpline = _closedSpline;
    if (closedSpline.size() < 5) return;
    auto spline = LoopingNaturalSpline<Vector2d>(closedSpline, 0.5);
    int count = closedSpline.size();
    Point2Ds rePts(count);
    Point2Ds reDirs(count);
    Point2Ds _offsetPts(count);
    double costhr = cos(M_PI / 6);
    for (int i = 0; i < count; ++i) {
        const Vector2d &tanDir = spline.getTangent(i).tangent.normalized();
        reDirs[i](0) = tanDir(1);
        reDirs[i](1) = -tanDir(0);
        auto _pt = closedSpline[i];
        rePts[i] = _pt + nor2d * offset;
        bool bIn = geo::isInBolygon(rePts[i], closedSpline);
        if (bIn != bInner) {
            reDirs[i]= -reDirs[i];
            rePts[i] = _pt + reDirs[i]* offset;;
        }
    }
    offsetSpline.swap(rePts);
    if(offsetDirs) offsetDirs->swap(reDirs);
}