namespace Yw.WinFrmUI.Phart
|
{
|
public static class Point2dListExtensions
|
{
|
|
/// <summary>
|
/// 是否完全相同
|
/// </summary>
|
public static bool IsEqualValueX(this List<Yw.Geometry.Point2d> pt_list, List<Yw.Geometry.Point2d> comparer_pt_list)
|
{
|
if (pt_list == null || pt_list.Count() == 0)
|
{
|
if (comparer_pt_list == null || comparer_pt_list.Count() == 0)
|
{
|
return true;
|
}
|
|
return false;
|
}
|
|
if (comparer_pt_list == null || comparer_pt_list.Count() == 0)
|
{
|
if (pt_list == null || pt_list.Count() == 0)
|
{
|
return true;
|
}
|
|
return false;
|
}
|
|
if (pt_list.Count != comparer_pt_list.Count)
|
{
|
return false;
|
}
|
|
for (int i = 0; i < pt_list.Count; i++)
|
{
|
if (Math.Abs(pt_list[i].X - comparer_pt_list[i].X) > 0.001)
|
{
|
return false;
|
}
|
}
|
|
return true;
|
}
|
|
|
/// <summary>
|
/// 修改曲线0点位置的值,曲线值会根据比例逐渐变化,直到fixPtX,不再变化
|
/// </summary>
|
public static List<Yw.Geometry.Point2d> AmendByZeroPointY(this List<Yw.Geometry.Point2d> pt_list, double fix_point_x, double zero_point_y,Yw.Ahart.eFeatType feat_type= Ahart.eFeatType.Cubic)
|
{
|
if (pt_list == null || pt_list.Count < 3)
|
return null;
|
|
var fit_pt_list = new List<Yw.Geometry.Point2d>();
|
|
//计算差距
|
double zeroDis = pt_list.First().Y - zero_point_y;
|
for (int i = 0; i < pt_list.Count; i++)
|
{
|
double dis = 0;
|
if (pt_list[i].X <= fix_point_x)
|
{
|
dis = zeroDis * (1 - pt_list[i].X / fix_point_x);
|
}
|
else
|
{
|
dis = 0;
|
}
|
|
fit_pt_list.Add(new Yw.Geometry.Point2d(pt_list[i].X, pt_list[i].Y - dis));
|
}
|
var new_pt_list = new List<Yw.Geometry.Point2d>();
|
switch (feat_type)
|
{
|
case Ahart.eFeatType.Polynomial:
|
{
|
new_pt_list=new Yw.Geometry.PolynomialSpline2d(fit_pt_list).GetPointList();
|
}
|
break;
|
case Ahart.eFeatType.Cubic:
|
{
|
new_pt_list = new Yw.Geometry.CubicSpline2d(fit_pt_list).GetPointList();
|
}
|
break;
|
case Ahart.eFeatType.Through2d:
|
{
|
new_pt_list = new Yw.Geometry.ThroughSpline2d(fit_pt_list).GetPointList();
|
}
|
break;
|
}
|
return new_pt_list;
|
}
|
|
|
/// <summary>
|
///
|
/// </summary>
|
public static List<Yw.Geometry.Point2d> GetFitPointList(this List<Yw.Geometry.Point2d> pt_list)
|
{
|
if (pt_list == null || !pt_list.Any())
|
return default;
|
|
var cubic_spline = new Yw.Geometry.CubicSpline2d(pt_list);
|
return cubic_spline.GetPointList(pt_list.Count);
|
}
|
|
|
/// <summary>
|
///
|
/// </summary>
|
public static List<Yw.Geometry.Point2d> GetExpandFitPointList(this List<Yw.Geometry.Point2d> pt_list, double expand_ratio)
|
{
|
if (pt_list == null || !pt_list.Any())
|
return default;
|
|
var cubic_spline = new Yw.Geometry.CubicSpline2d(pt_list);
|
cubic_spline.MaxX = cubic_spline.MaxX * expand_ratio;
|
return cubic_spline.GetPointList(pt_list.Count);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
public static List<Yw.Geometry.Point2d> GetFitPointList(this List<Yw.Geometry.Point2d> pt_list, int point_count)
|
{
|
if (pt_list == null || !pt_list.Any())
|
return default;
|
|
var cubic_spline = new Yw.Geometry.CubicSpline2d(pt_list);
|
return cubic_spline.GetPointList(point_count);
|
}
|
|
/// <summary>
|
/// 计算总长度
|
/// </summary>
|
public static double TotalLength(this IEnumerable<Yw.Geometry.Point2d> srcList)
|
{
|
if (srcList == null || srcList.Count() < 2)
|
return default;
|
var list = srcList.ToList();
|
double length = 0;
|
for (int i = 1; i < list.Count(); i++)
|
{
|
double len = Math.Sqrt((list[i].X - list[i - 1].X) * (list[i].X - list[i - 1].X) + (list[i].Y - list[i - 1].Y) * (list[i].Y - list[i - 1].Y));
|
length += len;
|
}
|
return length;
|
}
|
|
//public static List<Yw.Geometry.Point2d> GetInterPointsX(this List<Yw.Geometry.Point2d> pt_list, double y)
|
//{
|
// if (pt_list == null || pt_list.Count < 2)
|
// {
|
// return default;
|
// }
|
// var list = new List<Yw.Geometry.Point2d>();
|
// for (int i = 0; i < pt_list.Count - 1; i++)
|
// {
|
// if ((y >= pt_list[i].Y && y <= pt_list[i + 1].Y) || (y <= pt_list[i].Y && y >= pt_list[i + 1].Y))
|
// {//直线插值
|
// double x;
|
// if (Math.Abs(pt_list[i].Y - pt_list[i + 1].Y) < 0.01)
|
// x = (pt_list[i].X + pt_list[i + 1].X) * 0.5;
|
// else
|
// x = pt_list[i].X + (pt_list[i + 1].X - pt_list[i].X) * (y - pt_list[i].Y) / (pt_list[i + 1].Y - pt_list[i].Y);
|
|
// list.Add(new Yw.Geometry.Point2d(x, y));
|
// }
|
// }
|
// return list;
|
//}
|
|
|
/// <summary>
|
/// 根据周长度计算点位置,返回空表示超过总长
|
/// </summary>
|
/// <param name="length"></param>
|
/// <returns></returns>
|
public static Yw.Geometry.Point2d GetPosiByLength(this List<Yw.Geometry.Point2d> srcList,double length_from_start)
|
{
|
if (length_from_start < 0.0001)
|
return srcList.FirstOrDefault();
|
double length = 0;
|
for (int i = 1; i < srcList.Count; i++)
|
{
|
double len_line = Math.Sqrt((srcList[i].X - srcList[i - 1].X) * (srcList[i].X - srcList[i - 1].X) + (srcList[i].Y - srcList[i - 1].Y) * (srcList[i].Y - srcList[i - 1].Y));
|
length += len_line;
|
if (length >= length_from_start)
|
{
|
Yw.Geometry.Point2d extPt = new Yw.Geometry.Point2d();
|
extPt.X = (srcList[i - 1].X - srcList[i].X) * (length - length_from_start) / len_line + srcList[i].X;
|
extPt.Y = (srcList[i - 1].Y - srcList[i].Y) * (length - length_from_start) / len_line + srcList[i].Y;
|
return extPt;
|
}
|
}
|
if (length >= length_from_start * 0.9999)
|
{
|
return srcList.LastOrDefault();
|
}
|
return null;
|
}
|
|
/// <summary>
|
/// 获取Y最小的点的序号
|
/// </summary>
|
/// <returns></returns>
|
public static int GetPointIndexOfMinY(this List<Yw.Geometry.Point2d> srcList )
|
{
|
if (srcList.Count() == 0)
|
return 0;
|
if (srcList.Count() == 1)
|
return 0;
|
var minY = (from x in srcList select x.Y).Min();
|
var pt = (from x in srcList where x.Y == minY select x).FirstOrDefault();
|
if (pt == null)
|
return 0;
|
else
|
return srcList.IndexOf(pt);
|
}
|
}
|
}
|