2013-04-11 15 views
9

私は数週間この作業を続けてきましたが、アルゴリズムが正しく機能することができず、私の知恵が終わりました。ここで私は達成しているかの実例です:すべてが働いていた場合はDe-Boorsアルゴリズムを実装してBスプラインの点を見つけるアルゴリズム

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); 
     } 
    } 
+0

http://chi3x10.wordpress.com/2009/10/18/de-boor-algorithm-in-c/あなたは – Saqlain

+0

Bスプラインは、凸包性を示す少しの助けを得ることができます。連続する各コントロールポイントから線を引くと、凸多角形になりますか?いくつかのエッジが交差するように見えます。 –

+0

@BrettHale私はまったくフォローしていないのだろうか?私は現時点では2Dで作業していますが、白い点で定義されたB-スレーンカーブのエッジのどれもが交差しているようには見えません。それは意図的なもので、円を描こうとすると、第1、第2、第3、第5、第6、第7の点が重なる。私はこれを解決するために時間を割いてくれてありがとう、私は本当に苦労しています。 – Holly

答えて

2

あなたのアルゴリズムは、私が試したどんな入力に対してもうまくいくようです。コントロールポイントが想定されていない場所、またはコントロールポイントが正しく初期化されていない可能性があります。左下には高さの半分の2つのコントロールポイントがあるようです。

Correct Wrong

関連する問題