単調スプライン; Monotone Spline
概要
単調スプラインは制御点とその近傍2点から傾きを算出する3次スプラインの一つである。
区間内において常に補間値が単調増加または単調減少する。
2次元以上の曲線を補間するのに向いている。
スプライン補間構成
・スプライン基本クラス
・3次スプライン基本クラス
・制御点とその近傍2点から傾きを算出する3次スプライン基本クラス
・単調スプライン
・Catmull-Romスプライン
・制御点とその近傍4点から傾きを算出する3次スプライン基本クラス
・秋間スプライン
・終端タイプ列挙型
・多次元スプライン補間ジェネリッククラス
ソースコード
namespace SplineInterpolation {
/// <summary>単調スプライン</summary>
public class MonotoneSpline : CubicSplineNeighbor2 {
/// <summary>コンストラクタ</summary>
public MonotoneSpline(EndType type = EndType.Open) : base(type) {
}
/// <summary>制御点における傾き</summary>
protected override double Grad(double vm1, double v0, double vp1) {
double vv = (vm1 - v0) * (v0 - vp1);
return (vv > 0) ? (2 * vv / (vp1 - vm1)) : 0;
}
}
}
実行例
using(StreamWriter stream = new StreamWriter("plot_monotone.txt")) {
stream.WriteLine("x,y");
for(decimal x = -1.0m; x <= sp1.Points; x += 0.01m) {
stream.WriteLine($"{x},{sp1.Value((double)x)}");
}
}
Spline sp2 = new MonotoneSpline(EndType.Close);
sp2.Set(12, 15, 30, 30, 20, 10, 20, 20, 50, 20, 25);
using(StreamWriter stream = new StreamWriter("plot_monotone_close.txt")) {
stream.WriteLine("x,y");
for(decimal x = -5.0m; x <= sp2.Points + 4; x += 0.01m) {
stream.WriteLine($"{x},{sp2.Value((double)x)}");
}
}
var sp3 = new SplineMultiDimension<MonotoneSpline>(2, EndType.Close);
sp3.Set(new Vector(-1, -1), new Vector(-2, 0), new Vector(-1, +1), new Vector(+1, -1), new Vector(+2, 0), new Vector(+1, +1));
using(StreamWriter stream = new StreamWriter("plot_monotone_parametric.txt")) {
stream.WriteLine("x,y");
for(decimal t = 0; t <= sp3.Points; t += 0.01m) {
var v = sp3.Value((double)t);
stream.WriteLine(v);
}
}
関連項目
3次スプライン補間基本クラス
3次スプライン補間基本クラス 単体テスト
秋間スプライン
Catmull-Romスプライン