| 470 | } |
| 471 | |
| 472 | std::vector<double> BSpline::Builder::knotVectorBuckets(const std::vector<double> &values, unsigned int degree, unsigned int maxSegments) const |
| 473 | { |
| 474 | // Sort and remove duplicates |
| 475 | std::vector<double> unique = extractUniqueSorted(values); |
| 476 | |
| 477 | // The minimum number of samples from which a free knot vector can be created |
| 478 | if (unique.size() < degree+1) |
| 479 | { |
| 480 | std::ostringstream e; |
| 481 | e << "BSpline::Builder::knotVectorBuckets: Only " << unique.size() |
| 482 | << " unique sample points are given. A minimum of degree+1 = " << degree+1 |
| 483 | << " unique points are required to build a B-spline basis of degree " << degree << "."; |
| 484 | throw Exception(e.str()); |
| 485 | } |
| 486 | |
| 487 | // Num internal knots (0 <= ni <= unique.size() - degree - 1) |
| 488 | unsigned int ni = unique.size() - degree - 1; |
| 489 | |
| 490 | // Num segments |
| 491 | unsigned int ns = ni + degree + 1; |
| 492 | |
| 493 | // Limit number of segments |
| 494 | if (ns > maxSegments && maxSegments >= degree + 1) |
| 495 | { |
| 496 | ns = maxSegments; |
| 497 | ni = ns - degree - 1; |
| 498 | } |
| 499 | |
| 500 | // Num knots |
| 501 | // unsigned int nk = ns + degree + 1; |
| 502 | |
| 503 | // Check numbers |
| 504 | if (ni > unique.size() - degree - 1) |
| 505 | throw Exception("BSpline::Builder::knotVectorBuckets: Invalid number of internal knots!"); |
| 506 | |
| 507 | // Compute window sizes |
| 508 | unsigned int w = 0; |
| 509 | if (ni > 0) |
| 510 | w = std::floor(unique.size()/ni); |
| 511 | |
| 512 | // Residual |
| 513 | unsigned int res = unique.size() - w*ni; |
| 514 | |
| 515 | // Create array with window sizes |
| 516 | std::vector<unsigned int> windows(ni, w); |
| 517 | |
| 518 | // Add residual |
| 519 | for (unsigned int i = 0; i < res; ++i) |
| 520 | windows.at(i) += 1; |
| 521 | |
| 522 | // Compute internal knots |
| 523 | std::vector<double> knots(ni, 0); |
| 524 | |
| 525 | // Compute (n-k-2) interior knots using moving average |
| 526 | unsigned int index = 0; |
| 527 | for (unsigned int i = 0; i < ni; ++i) |
| 528 | { |
| 529 | for (unsigned int j = 0; j < windows.at(i); ++j) |