三次贝塞尔曲线的x坐标求y,快速Newton-Raphson方法

Getting y from x co-ord for cubic bezier curve, fast Newton-Raphson method

本文关键字:坐标 方法 Newton-Raphson 快速 曲线 三次      更新时间:2023-10-16

给定2D中Bezier曲线(P0,P1,P2,P3)的点,我想找到给定x坐标的y坐标。由于以下限制,该问题得到了很好的定义:

  • P0=(0,0),P3=(1,1)
  • P1=(t,1-t),对于0,1之间的t
  • P2=1-P1(x和y)

考虑到所有的限制,我有以下函数来计算答案我使用Newton-Raphson来计算我想要的点的参数,我知道这很有效,因为我不会让循环完成,直到它完成(在定义的公差范围内)。

我正在使用此功能对图像应用对比度过滤器。对于此0.5,会返回相同的图像,0.0会最大限度地降低对比度,1.0会最大程度地增加对比度。

编辑以下功能已更正,现在可以完美工作。

/*
 * Parameters: p - x co-ord of P1, 
 *             x - x we want to find y for
 *
 * This method is unstable for p ~= 0.5, maybe needs refinement.
 */
#include <iostream>
#include <math.h>
#define ITER_TOL  0.00001
float maths::bezier(float p, float x)
{
    if(p < 0.f || p > 1.f || x < 0.f || x > 1.f)
    {
        std::cerr << "Both parameters must be between 0 and 1, returning dummy value" << std::endl;
        return 0.f;
    }
    //First guess for u
    float u = x;
    //Coefficients of curve (for x and y co-ord)
    float x3 = 6 * p - 2;
    float x2 = 3 - 9 * p;
    float x1 = 3 * p;
    float x0 = -x;
    float y3 = 6 * (1-p) - 2;
    float y2 = 3 - 9 * (1-p);
    float y1 = 3 * (1-p);
    //Newton-Raphson refinement
    for(int i=0; fabs(x3*u*u*u + x2*u*u + x1*u + x0) > ITER_TOL && i<1000; i++)
    {
        u = u - (x3*u*u*u + x2*u*u + x1*u + x0) /
                (3*x3*u*u + 2*x2*u + x1);
        //std::cout << i << ": " << u << std::endl;
        //Deal with non-convergence
        if(i==999)
        {
            std::cerr << "Warning, Newton-Raphson method did not converge in Maths.cpp, returning dummy" << std::endl;
            return 0.f;
        }
    }
    //Calculate y co-ord
    return y3*u*u*u + y2*u*u + y1*u;
}

如果我们设置p=0.5,我们应该得到一条直线,但当我对林空间这样做时绘制这些点,我得到了0.5到1.0之间的弯曲。有人明白为什么会发生这种事吗?如果我能做些什么的话?

我编译了您的代码,注意到循环只运行0或1次迭代。可能是因为fabs在收敛检查中的某个地方丢失了?