Shuxia Ning
2024-12-19 b0c978129ba55cf81e8470b6c9326745a5dbc7d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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);
        }
    }
}