Tôi đã làm việc này trong vài tuần nhưng đã không thể làm cho thuật toán của tôi hoạt động đúng và tôi đang ở cuối trí thông minh của mình. Dưới đây là một minh họa về những gì tôi đã đạt được:Thực hiện thuật toán De Boors cho việc tìm kiếm các điểm trên B-spline
Nếu mọi thứ đã được làm việc tôi mong chờ một vòng tròn hoàn hảo/hình bầu dục ở cuối.
Điểm mẫu của tôi (màu trắng) được tính toán lại mỗi lần một điểm điều khiển mới (màu vàng) được thêm vào. Tại 4 điểm kiểm soát mọi thứ trông hoàn hảo, một lần nữa khi tôi thêm một thứ 5 trên đầu trang của những điều đầu tiên nhìn ổn, nhưng sau đó vào ngày 6 nó bắt đầu đi quá bên và vào ngày 7 nó nhảy lên nguồn gốc!
Dưới đây tôi sẽ đăng mã của mình, trong đó calculateWeightForPointI
chứa thuật toán thực tế. Và để tham khảo- here is the information i'm trying to follow. Tôi sẽ rất tuyệt vời nếu ai đó có thể tìm kiếm tôi.
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);
}
}
http://chi3x10.wordpress.com/2009/10/18/de-boor-algorithm-in-c/ bạn có thể nhận được sự giúp đỡ – Saqlain
B-splines triển lãm bất động sản thân lồi . Nếu bạn vẽ một đường từ mỗi điểm điều khiển liên tiếp, bạn có kết thúc với một đa giác lồi không? Có vẻ như một số cạnh cắt nhau. –
@BrettHale Tôi sợ tôi không thực sự theo dõi? Tôi chỉ làm việc trong 2D tại thời điểm này nhưng không có cạnh của đường cong B-sline của tôi (như được xác định bởi các chấm trắng) dường như giao nhau .. Oh chờ đợi chúng ta đang nói về các điểm kiểm soát chồng chéo? Đó là cố ý, các điểm thứ 5, thứ 6 và thứ 7 trùng nhau vào ngày 1, 2 và 3 khi cố gắng vẽ một vòng tròn. Cảm ơn bạn đã dành thời gian giúp tôi giải quyết vấn đề này Tôi thực sự đang gặp khó khăn. – Holly