c++ - 实现 De Boors 算法以查找 B 样条上的点

标签 c++ algorithm math curve bspline

我已经为此工作了几个星期,但一直无法让我的算法正常工作,我束手无策。这是我所取得成就的说明:

enter image description here

如果一切正常,我希望最后会出现一个完美的圆形/椭圆形。

每次添加新的控制点(黄色)时,我的样本点(白色)都会重新计算。在 4 个控制点,一切看起来都很完美,当我在第 1 个控制点上添加第 5 个时,一切看起来还不错,但随后在第 6 个控制点,它开始偏离一边,在第 7 个控制点,它跳到原点!

下面我将发布我的代码,其中 calculateWeightForPointI 包含实际算法。并供引用- here is the information i'm trying to follow.如果有人能帮我看看,我会非常感激。

void updateCurve(const std::vector<glm::vec3>& controls, std::vector<glm::vec3>& samples)
{
    int subCurveOrder = 4; // = k = I want to break my curve into to cubics

    // De boor 1st attempt
    if(controls.size() >= subCurveOrder)
    {
        createKnotVector(subCurveOrder, controls.size()); 
        samples.clear(); 

        for(int steps=0; steps<=20; steps++)
        {
            // use steps to get a 0-1 range value for progression along the curve
                    // then get that value into the range [k-1, n+1]
            // k-1 = subCurveOrder-1
            // n+1 = always the number of total control points

            float t = ( steps / 20.0f ) * ( controls.size() - (subCurveOrder-1) ) + subCurveOrder-1;

            glm::vec3 newPoint(0,0,0);
            for(int i=1; i <= controls.size(); i++)
            {
                float weightForControl = calculateWeightForPointI(i, subCurveOrder, controls.size(), t);
                newPoint += weightForControl * controls.at(i-1);
            }
            samples.push_back(newPoint);
        }
    }

}

    //i = the weight we're looking for, i should go from 1 to n+1, where n+1 is equal to the total number of control points.
    //k = curve order = power/degree +1. eg, to break whole curve into cubics use a curve order of 4
    //cps = number of total control points 
    //t = current step/interp value
float calculateWeightForPointI( int i, int k, int cps, float t )
    {
        //test if we've reached the bottom of the recursive call
        if( k == 1 )
        {
            if( t >= knot(i) && t < knot(i+1) )
                return 1;
            else
                return 0;
        } 

        float numeratorA = ( t - knot(i) );
        float denominatorA = ( knot(i + k-1) - knot(i) );
        float numeratorB = ( knot(i + k) - t );
        float denominatorB = ( knot(i + k) - knot(i + 1) );

        float subweightA = 0;
        float subweightB = 0;

        if( denominatorA != 0 )
            subweightA = numeratorA / denominatorA * calculateWeightForPointI(i, k-1, cps, t);
        if( denominatorB != 0 )
            subweightB = numeratorB / denominatorB * calculateWeightForPointI(i+1, k-1, cps, t);

        return subweightA + subweightB;

    }

    //returns the knot value at the passed in index 
    //if i = 1 and we want Xi then we have to remember to index with i-1
float knot(int indexForKnot)
    {
         // When getting the index for the knot function i remember to subtract 1 from i because of the difference caused by us counting from i=1 to n+1 and indexing a vector from 0
         return knotVector.at(indexForKnot-1);
    }
    //calculate the whole knot vector
void createKnotVector(int curveOrderK, int numControlPoints)
    {
        int knotSize = curveOrderK + numControlPoints;
        for(int count = 0; count < knotSize; count++)
        {
            knotVector.push_back(count);
        }
    } 

最佳答案

您的算法似乎适用于我尝试过的任何输入。您的问题可能是控制点不在应有的位置,或者它们没有正确初始化。看起来有两个控制点,在左下角下方一半的高度。

Correct Wrong

关于c++ - 实现 De Boors 算法以查找 B 样条上的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15944532/

相关文章:

c++ - '=' AND ' !='运算符重载问题

c# - VERSIONINFO - 忽略自定义值

python - Networkx:使用公共(public)函数进行边权重计算

matlab - 如何在matlab中绘制坡度场

math - 就内存而言,在 Julia 中将公式保存为字符串还是表达式(符号)更好

algorithm - 尽可能快地计算矩阵的零空间

c++ - OpenCV(C++)中的背景减法

c++ - 如何在 C++ 中逻辑组织源文件

c++ - 没有元素满足谓词时的 is_partitioned 行为

algorithm - 字符串外部索引的高效存储