3차의 Hermite spline을 쓰는 interpolation에서 입력 데이터 점에서의 기울기 값을 수정하면 입력 데이터의 monotonic 한 성질을 그대로 유지하는 보간을 할 수 있다. 주어진 데이터가
Hermite 기저 함수의 정의는 아래와 같다:
이를 보이기 위해 3차 보간함수를

보간 spline이 단조성(monotonicity)을 가지도록 하려면 각 입력점에서 기울기 값(
이것만으로는 단조성을 보증을 할 수 없으므로 다음 조건을 추가로 부여해야 됨이 관련 논문에서 증명이 되었다.
아래의 코드는 위키피디아에 제시된 알고리즘을 구현한 것이다. (입력 데이터가 한 방향으로 monotonic 할 때만 동작함)
참고: http://en.wikipedia.org/wiki/Monotone_cubic_interpolation
std::vector<double> estimateTangents(const std::vector<CfPt>& P) {
// slopes;
std::vector<double> delta(P.size());
for (int k = 0; k < delta.size()-1; k++)
delta[k] = double(P[k+1].y - P[k].y) / (P[k+1].x - P[k].x) ;
// average tangent at data points;
std::vector<double> m(P.size());
m.front() = delta.front();
m.back() = delta[m.size()-2];
for (int k = 1; k < m.size()-1; k++)
m[k] = (delta[k-1] + delta[k]) / 2; //error corrected.
for (int k = 0; k < m.size()-1; k++) {
if (delta[k] == 0) m[k] = m[k+1] = 0;
else {
double alpha = m[k] / delta[k];
double beta = m[k+1] / delta[k];
double epsilon = hypot(alpha, beta);
if (epsilon > 3) {
double tau = 3 * delta[k] / epsilon ;
m[k] = tau * alpha;
m[k+1] = tau * beta;
}
}
}
return m;
};
std::vector<CfPt> monotoneCubicSplineInterp(const std::vector<CfPt>& points) {
if (points.size() <= 2) return points;
// tangent table;
std::vector<double> m = estimateTangents(points);
// hermite spline;
#define STEPS 12
std::vector<CfPt> curve;
curve.push_back(points.front()); //add first point;
for (int i = 0; i < points.size()-1; i++) {
double h = points[i+1].x - points[i].x;
for (int k = 1; k <= STEPS; k++) {
double t = double(k) / STEPS; // 0 < t <= 1;
double tt = t*t, ttt = tt*t;
double h00 = 2 * ttt - 3 * tt + 1;
double h10 = ttt - 2 * tt + t;
double h01 = -2 * ttt + 3 * tt;
double h11 = ttt - tt;
double yf = h00 * points[i].y + h10 * h * m[i]
+ h01 * points[i+1].y + h11 * h * m[i+1];
curve.push_back(CfPt(points[i].x + t * h, yf));
}
}
return curve;
};
Natural cubic spline과의 비교:

Natural Cubic Spline
구간
kipl.tistory.com
'Computational Geometry' 카테고리의 다른 글
Chain Hull (2) | 2012.09.16 |
---|---|
Quick Hull (2) | 2012.09.16 |
Brute Force Convex Hull Algorithm (0) | 2012.09.06 |
Curvature of a Curve (0) | 2012.08.07 |
Douglas-Peucker Algorithm (0) | 2012.05.03 |