duheng
2025-03-31 be65218617cab71a90a9a05cf488fbb6e206b5c5
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
using Yw.Ahart;
 
namespace Yw.WinFrmUI
{
    public static class PhartPoint2dListExtensions
    {
        public static double? GetX(this List<Yw.Geometry.Point2d> pt_list, Yw.Ahart.eFeatType feat_type, double x)
        {
            if (pt_list == null || !pt_list.Any())
                return default;
            var feat_curve = GetFeatCurve(pt_list, feat_type);
            if (feat_curve == null || feat_curve.IsInvalid())
                return default;
            return feat_curve.GetPointsX(x)?.FirstOrDefault() ?? null;
        }
 
        public static double GetY(this List<Yw.Geometry.Point2d> pt_list, Yw.Ahart.eFeatType feat_type, double x)
        {
            if (pt_list == null || !pt_list.Any())
                return default;
            var feat_curve = GetFeatCurve(pt_list, feat_type);
            if (feat_curve == null || feat_curve.IsInvalid())
                return default;
            return feat_curve.GetPointYUnlimited(x);
        }
 
        public static List<double> GetYList(this List<Yw.Geometry.Point2d> pt_list, Yw.Ahart.eFeatType feat_type, int pt_count = 30)
        {
            if (pt_list == null || !pt_list.Any())
                return default;
            var feat_pt_list = GetPointList(pt_list, feat_type, pt_count);
            if (feat_pt_list == null || !feat_pt_list.Any())
                return default;
            return feat_pt_list.Select(x => x.Y).ToList();
        }
 
        public static List<Yw.Geometry.Point2d> GetPointList(this List<Yw.Geometry.Point2d> pt_list, Yw.Ahart.eFeatType feat_type, int pt_count = 30)
        {
            if (pt_list == null || !pt_list.Any())
                return default;
            var feat_curve = GetFeatCurve(pt_list, feat_type);
            if (feat_curve == null || feat_curve.IsInvalid())
                return default;
            var feat_pt_list = feat_curve.GetPointList(pt_count);
            return feat_pt_list;
        }
 
        public static List<Yw.Geometry.Point2d> GetExpandPointList(this List<Yw.Geometry.Point2d> pt_list, Yw.Ahart.eFeatType feat_type, double minXRatio = 0.9, double maxXRatio = 1.2, int pt_count = 30)
        {
            if (pt_list == null || !pt_list.Any())
                return default;
            var feat_curve = GetFeatCurve(pt_list, feat_type);
            if (feat_curve == null || feat_curve.IsInvalid())
                return default;
            var feat_pt_list = feat_curve.GetPointListByXRatioRange(minXRatio, maxXRatio, pt_count);
            return feat_pt_list;
        }
 
        public static IFeatCurve GetFeatCurve(this List<Yw.Geometry.Point2d> pt_list, Yw.Ahart.eFeatType feat_type)
        {
            if (pt_list == null || !pt_list.Any())
                return default;
            Yw.Ahart.IFeatCurve feat_curve = null;
            switch (feat_type)
            {
                case Ahart.eFeatType.Polynomial: feat_curve = new Yw.Ahart.PolynomialCurve(pt_list); break;
                case Ahart.eFeatType.Cubic: feat_curve = new Yw.Ahart.CubicCurve(pt_list); break;
                case Ahart.eFeatType.Through: feat_curve = new Yw.Ahart.ThroughCurve(pt_list); break;
                case Ahart.eFeatType.Quadratic: feat_curve = new Yw.Ahart.QuadraticCurve(pt_list); break;
                case Ahart.eFeatType.Quartic: feat_curve = new Yw.Ahart.QuarticCurve(pt_list); break;
                default: break;
            }
 
            return feat_curve;
        }
 
        public static Yw.Ahart.PerformCurveBase GetPerformCurve(this List<Yw.Geometry.Point2d> pt_list, Yw.Ahart.eCurveType curve_type, Yw.Ahart.eFeatType feat_type)
        {
            if (pt_list == null || !pt_list.Any())
                return default;
            var feat_curve = GetFeatCurve(pt_list, feat_type);
            if (feat_curve == null || feat_curve.IsInvalid())
                return default;
            switch (curve_type)
            {
                case eCurveType.QH:
                    return new Yw.Ahart.CurveQH(feat_type, feat_curve);
                case eCurveType.QP:
                    return new Yw.Ahart.CurveQP(feat_type, feat_curve);
                case eCurveType.QE:
                    return new Yw.Ahart.CurveQE(feat_type, feat_curve);
                case eCurveType.QNPSH:
                    return null;
                case eCurveType.EqualE:
                    return new Yw.Ahart.CurveEqualE(feat_type, feat_curve);
                case eCurveType.EqualP:
                    return new Yw.Ahart.CurveEqualP(feat_type, feat_curve);
                case eCurveType.QL:
                    return new Yw.Ahart.CurveQL(feat_type, feat_curve);
                case eCurveType.OL:
                    return new Yw.Ahart.CurveOL(feat_type, feat_curve);
                case eCurveType.VOL:
                    return new Yw.Ahart.CurveVOL(feat_type, feat_curve);
                default:
                    throw new Exception("曲线类型异常!");
            }
 
        }
 
 
 
        /// <summary>
        /// 计算总长度
        /// </summary>
        public static double TotalLength(this IEnumerable<Yw.Geometry.Point2d> pt_list)
        {
            if (pt_list == null || pt_list.Count() < 2)
                return default;
            var list = pt_list.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;
        }
 
        /// <summary>
        /// 根据周长度计算点位置,返回空表示超过总长
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static Yw.Geometry.Point2d GetPosiByLength(this List<Yw.Geometry.Point2d> pt_list, double length_from_start)
        {
            if (pt_list == null)
                return null;
            if (length_from_start < 0.0001)
                return pt_list.FirstOrDefault();
            double length = 0;
            for (int i = 1; i < pt_list.Count; i++)
            {
                double len_line = Math.Sqrt((pt_list[i].X - pt_list[i - 1].X) * (pt_list[i].X - pt_list[i - 1].X) + (pt_list[i].Y - pt_list[i - 1].Y) * (pt_list[i].Y - pt_list[i - 1].Y));
                length += len_line;
                if (length >= length_from_start)
                {
                    Yw.Geometry.Point2d extPt = new Yw.Geometry.Point2d();
                    extPt.X = (pt_list[i - 1].X - pt_list[i].X) * (length - length_from_start) / len_line + pt_list[i].X;
                    extPt.Y = (pt_list[i - 1].Y - pt_list[i].Y) * (length - length_from_start) / len_line + pt_list[i].Y;
                    return extPt;
                }
            }
            if (length >= length_from_start * 0.9999)
            {
                return pt_list.LastOrDefault();
            }
            return null;
        }
 
        /// <summary>
        /// 获取Y最小的点的序号
        /// </summary>
        /// <returns></returns>
        public static int GetPointIndexOfMinY(this List<Yw.Geometry.Point2d> pt_list)
        {
            if (pt_list == null)
                return 0;
            if (pt_list.Count == 0)
                return 0;
            if (pt_list.Count == 1)
                return 0;
            var minY = (from x in pt_list select x.Y).Min();
            var pt = (from x in pt_list where x.Y == minY select x).FirstOrDefault();
            if (pt == null)
                return 0;
            else
                return pt_list.IndexOf(pt);
        }
 
        public static string ToDbString(this List<Yw.Geometry.Point2d> pt_list, Yw.Ahart.eCurveType curve_type, Yw.Ahart.eFeatType feat_type)
        {
            return PhartGraphHelper.ToDbString(curve_type, feat_type, pt_list);
        }
 
    }
 
 
 
}