using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#pragma warning disable CS8601 // 引用类型赋值可能为 null。
namespace IStation.Model
{
///
/// 样条曲线(二次拟合):使用Math.Net
///
public class TwinRelateCurve2M : IFitCurvePoint
{
public TwinRelateCurve2M(CurveExpress express)
{
_express = express;
}
public TwinRelateCurve2M(List points)
{
if (points == null || points.Count < 3)
{
throw new Exception("出现数值计算问题, 无法解出二次拟合曲线!");
}
_express = BuildCurveExpress(points);
}
//double[] _res = null;
private CurveExpress _express = null;
///
/// 创建曲线表达式
///
public static Model.CurveExpress? BuildCurveExpress(List points)
{
if (points == null || points.Count() < 3)
return default;
points = points.OrderBy(x => x.X).ToList();
CurveExpress express = new CurveExpress();
express.FitPow = 2;
express.FitType = eCurveFitType.ConicCurve;
express.DefinePoints = points.Select(x => new CurvePoint(x)).ToList();
express.Min = points.First().X;
express.Max = points.Last().X;
var X = (from x in points select x.X).ToArray();
var Y = (from x in points select x.Y).ToArray();
var res = IStation.Numerics.Fit.Polynomial(X, Y, 2);
//0次方
express.Index0 = res[0];
//1次方
express.Index1 = res[1];
//2次方
express.Index2 = res[2];
//3次方
express.Index3 = 0;// res[3];
return express;
}
///
/// 获取拟合点Y
///
public double GetFitPointY(double x)
{
if (_express == null)
return default;
return _express.Index2 * x * x + _express.Index1 * x + _express.Index0;
}
///
/// 获取拟合点列表
///
public List GetFitPoints(int pointNumber)
{
if (_express == null)
return default;
return GetFitPointsByRange(_express.Min, _express.Max, pointNumber);
}
///
/// 通过区间获取拟合点列表
///
public List GetFitPointsByRange(double x_min, double x_max, int pointNumber)
{
if (_express == null)
return default;
if (pointNumber < 1)
pointNumber = 12;
if (pointNumber > 10000)
pointNumber = 10000;
double space = (x_max - x_min) / (pointNumber - 1);
if (space < 0.0001)
return default;
var points = new List();
for (int i = 0; i < pointNumber; i++)
{
double x = space * i + x_min;
double y = GetFitPointY(x);
points.Add(new CurvePoint(x, y));
}
return points;
}
///
/// 通过区间获取拟合点列表
///
public static List GetFitPointsByRange(CurveExpress express, double x_min, double x_max, int pointNumber)
{
if (express == null)
return default;
return new TwinRelateCurve2M(express).GetFitPointsByRange(x_min, x_max, pointNumber);
}
///
/// 获取拟合点列表
///
public static List GetFitPoints(CurveExpress express, int pointNumber)
{
if (express == null)
return default;
return GetFitPointsByRange(express, express.Min, express.Max, pointNumber);
}
///
/// 获取拟合点Y
///
public static double GetFitPointY(CurveExpress express, double x)
{
if (express == null)
return default;
if (express.IsNull)
return default;
var s = new TwinRelateCurve2M(express);
return s.GetFitPointY(x);
}
///
/// 获取拟合点Y
///
public static double GetFitPointY(List points, double x)
{
if (points == null || points.Count < 3)
return default;
var s = new TwinRelateCurve2M(points);
return s.GetFitPointY(x);
}
}
}