C# Class SimShift.Map.Splines.CubicSpline

Cubic spline interpolation. Call Fit (or use the corrector constructor) to compute spline coefficients, then Eval to evaluate the spline at other X coordinates.

This is implemented based on the wikipedia article: http://en.wikipedia.org/wiki/Spline_interpolation I'm not sure I have the right to include a copy of the article so the equation numbers referenced in comments will end up being wrong at some point.

This is not optimized, and is not MT safe. This can extrapolate off the ends of the splines. You must provide points in X sort order.

Mostrar archivo Open project: nlhans/SimShift Class Usage Examples

Public Methods

Method Description
Compute ( float x, float y, float xs, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false ) : float[]

Static all-in-one method to fit the splines and evaluate at X coordinates.

CubicSpline ( ) : System

Default ctor.

CubicSpline ( float x, float y, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false ) : System

Construct and call Fit.

Eval ( float x, bool debug = false ) : float[]

Evaluate the spline at the specified x coordinates. This can extrapolate off the ends of the splines. You must provide X's in ascending order. The spline must already be computed before calling this, meaning you must have already called Fit() or FitAndEval().

EvalSlope ( float x, bool debug = false ) : float[]

Evaluate (compute) the slope of the spline at the specified x coordinates. This can extrapolate off the ends of the splines. You must provide X's in ascending order. The spline must already be computed before calling this, meaning you must have already called Fit() or FitAndEval().

Fit ( float x, float y, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false ) : void

Compute spline coefficients for the specified x,y points. This does the "natural spline" style for ends. This can extrapolate off the ends of the splines. You must provide points in X sort order.

FitAndEval ( float x, float y, float xs, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false ) : float[]

Fit x,y and then eval at points xs and return the corresponding y's. This does the "natural spline" style for ends. This can extrapolate off the ends of the splines. You must provide points in X sort order.

FitGeometric ( float x, float y, float directions, int nOutputPoints, float &xs, float &ys ) : void

Fit the input x,y points using a 'geometric' strategy so that y does not have to be a single-valued function of x.

Private Methods

Method Description
CheckAlreadyFitted ( ) : void

Throws if Fit has not been called.

EvalSpline ( float x, int j, bool debug = false ) : float

Evaluate the specified x value using the specified spline.

GetNextXIndex ( float x ) : int

Find where in xOrig the specified x falls, by simultaneous traverse. This allows xs to be less than x[0] and/or greater than x[n-1]. So allows extrapolation. This keeps state, so requires that x be sorted and xs called in ascending order, and is not multi-thread safe.

Method Details

Compute() public static method

Static all-in-one method to fit the splines and evaluate at X coordinates.
public static Compute ( float x, float y, float xs, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false ) : float[]
x float Input. X coordinates to fit.
y float Input. Y coordinates to fit.
xs float Input. X coordinates to evaluate the fitted curve at.
startSlope float Optional slope constraint for the first point. Single.NaN means no constraint.
endSlope float Optional slope constraint for the final point. Single.NaN means no constraint.
debug bool Turn on console output. Default is false.
return float[]

CubicSpline() public method

Default ctor.
public CubicSpline ( ) : System
return System

CubicSpline() public method

Construct and call Fit.
public CubicSpline ( float x, float y, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false ) : System
x float Input. X coordinates to fit.
y float Input. Y coordinates to fit.
startSlope float Optional slope constraint for the first point. Single.NaN means no constraint.
endSlope float Optional slope constraint for the final point. Single.NaN means no constraint.
debug bool Turn on console output. Default is false.
return System

Eval() public method

Evaluate the spline at the specified x coordinates. This can extrapolate off the ends of the splines. You must provide X's in ascending order. The spline must already be computed before calling this, meaning you must have already called Fit() or FitAndEval().
public Eval ( float x, bool debug = false ) : float[]
x float Input. X coordinates to evaluate the fitted curve at.
debug bool Turn on console output. Default is false.
return float[]

EvalSlope() public method

Evaluate (compute) the slope of the spline at the specified x coordinates. This can extrapolate off the ends of the splines. You must provide X's in ascending order. The spline must already be computed before calling this, meaning you must have already called Fit() or FitAndEval().
public EvalSlope ( float x, bool debug = false ) : float[]
x float Input. X coordinates to evaluate the fitted curve at.
debug bool Turn on console output. Default is false.
return float[]

Fit() public method

Compute spline coefficients for the specified x,y points. This does the "natural spline" style for ends. This can extrapolate off the ends of the splines. You must provide points in X sort order.
public Fit ( float x, float y, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false ) : void
x float Input. X coordinates to fit.
y float Input. Y coordinates to fit.
startSlope float Optional slope constraint for the first point. Single.NaN means no constraint.
endSlope float Optional slope constraint for the final point. Single.NaN means no constraint.
debug bool Turn on console output. Default is false.
return void

FitAndEval() public method

Fit x,y and then eval at points xs and return the corresponding y's. This does the "natural spline" style for ends. This can extrapolate off the ends of the splines. You must provide points in X sort order.
public FitAndEval ( float x, float y, float xs, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false ) : float[]
x float Input. X coordinates to fit.
y float Input. Y coordinates to fit.
xs float Input. X coordinates to evaluate the fitted curve at.
startSlope float Optional slope constraint for the first point. Single.NaN means no constraint.
endSlope float Optional slope constraint for the final point. Single.NaN means no constraint.
debug bool Turn on console output. Default is false.
return float[]

FitGeometric() public static method

Fit the input x,y points using a 'geometric' strategy so that y does not have to be a single-valued function of x.
public static FitGeometric ( float x, float y, float directions, int nOutputPoints, float &xs, float &ys ) : void
x float Input x coordinates.
y float Input y coordinates, do not need to be a single-valued function of x.
directions float
nOutputPoints int How many output points to create.
xs float Output (interpolated) x values.
ys float Output (interpolated) y values.
return void