tangxu
2022-11-02 05f522e321a742f03bf1e3e26edaeb5147da42f4
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.ComponentModel;
 
namespace IStation.Model
{
 
    /// <summary>
    /// 曲线表达式
    /// </summary>
    [DataContract]
    public class CurveExpress : ICloneable
    {
        #region 构造函数
 
        public CurveExpress() { }
        public CurveExpress(CurveExpress rhs)
        {
            this.FitPow = rhs.FitPow;
            this.FitType = rhs.FitType;
            this.Min = rhs.Min;
            this.Max = rhs.Max;
            this.Index0 = rhs.Index0;
            this.Index1 = rhs.Index1;
            this.Index2 = rhs.Index2;
            this.Index3 = rhs.Index3;
            this.DefinePoints = rhs.DefinePoints?.Select(x => new CurvePoint(x)).ToList();
            this.IsNull = rhs.IsNull;
        }
        public CurveExpress(CurveExpress rhs, double ratio_y)
        {
            this.FitPow = rhs.FitPow;
            this.FitType = rhs.FitType;
            this.Min = rhs.Min;
            this.Max = rhs.Max;
            this.Index0 = rhs.Index0 * ratio_y;
            this.Index1 = rhs.Index1 * ratio_y;
            this.Index2 = rhs.Index2 * ratio_y;
            this.Index3 = rhs.Index3 * ratio_y;
            this.DefinePoints = rhs.DefinePoints?.Select(x => new CurvePoint(x.X, x.Y * ratio_y)).ToList();
            this.IsNull = rhs.IsNull;
        }
        public CurveExpress(string strParas)
        {
            if (string.IsNullOrEmpty(strParas))
            {
                this.IsNull = true;
                return;
            }
            var str_list = strParas.Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
            var count = str_list.Count();
            if (count < 8)
            {
                this.IsNull = true;
                return;
            }
            this.IsNull = false ;
            this.FitPow = Convert.ToInt32(str_list[0]);
            this.FitType = (eCurveFitType)Convert.ToInt32(str_list[1]);
            this.Min = Convert.ToDouble(str_list[2]);
            this.Max = Convert.ToDouble(str_list[3]);
            this.Index0 = Convert.ToDouble(str_list[4]);
            this.Index1 = Convert.ToDouble(str_list[5]);
            this.Index2 = Convert.ToDouble(str_list[6]);
            this.Index3 = Convert.ToDouble(str_list[7]);
            if (count > 8)
            {
                this.DefinePoints = CurvePoint.ToList(str_list[8]);
            }
        }
        public CurveExpress(List<CurvePoint> points)
        {
            ResetCurve(points, eCurveFitType.CubicCurve, false);
        }
        public CurveExpress(List<CurvePoint> points, bool savePoints)
        {
            ResetCurve(points, eCurveFitType.CubicCurve, savePoints);
        }
        public CurveExpress(List<CurvePoint> points, eCurveFitType fitType)
        {
            ResetCurve(points, fitType,false);
        }
        public CurveExpress(List<CurvePoint> points, eCurveFitType fitType, bool savePoints)
        {
            ResetCurve(points, fitType, savePoints);
        }
        public CurveExpress(List<CurvePoint> points, int fitPow)
        {
            ResetCurve(points, fitPow,false);
        }
        public CurveExpress(List<CurvePoint> points, int fitPow, bool savePoints)
        {
            ResetCurve(points, fitPow, savePoints);
        }
 
        /// <summary>
        /// 重置曲线
        /// </summary>
        public virtual void ResetCurve(CurveExpress rhs)
        {
            this.FitPow = rhs.FitPow;
            this.FitType = rhs.FitType;
            this.Min = rhs.Min;
            this.Max = rhs.Max;
            this.Index0 = rhs.Index0;
            this.Index1 = rhs.Index1;
            this.Index2 = rhs.Index2;
            this.Index3 = rhs.Index3;
            this.DefinePoints = rhs.DefinePoints?.Select(x => new CurvePoint(x)).ToList();
            this.IsNull = rhs.IsNull;
        }
 
        /// <summary>
        /// 重置曲线
        /// </summary>
        public virtual void ResetCurve(List<CurvePoint> points, eCurveFitType fitType, bool savePoints = false )
        {
            var express = FitCurveHelper.BuildCurveExpress(points, fitType);
            if (express == null)
            {
                this.IsNull = true;
                return;
            }
            if (express.IsNull)
            {
                this.IsNull = true;
                return;
            }
            this.FitType = express.FitType;
            this.FitPow = express.FitPow;
            this.Min = express.Min;
            this.Max = express.Max;
            this.Index0 = express.Index0;
            this.Index1 = express.Index1;
            this.Index2 = express.Index2;
            this.Index3 = express.Index3;
            this.IsNull = express.IsNull;
            if (savePoints)
            {
                this.DefinePoints = express.DefinePoints;
            }
            else if (fitType == eCurveFitType.ThroughPoint)
            {
                this.DefinePoints = express.DefinePoints;
            }
        }
 
        /// <summary>
        /// 重置曲线
        /// </summary>
        public virtual void ResetCurve(List<CurvePoint> points, int fitPow, bool savePoints = false )
        {
            var fitType = GetFitType(fitPow);
            this.ResetCurve(points, fitType, savePoints);
        }
 
 
        #endregion
 
        #region 静态值
 
        public const string Separator = "#";
 
        #endregion
 
        #region 属性
 
        /// <summary>
        /// 拟合次数
        /// </summary>
        [DataMember]
        public int FitPow
        {
            get { return fitPow; }
            set { fitPow = value; }
        }
        protected int fitPow = 3;
 
        /// <summary>
        /// 拟合类型
        /// </summary>
        [DataMember]
        public eCurveFitType FitType
        {
            get { return fitType; }
            set { fitType = value; }
        }
        protected eCurveFitType fitType = eCurveFitType.CubicCurve;
 
        /// <summary>
        /// 最大值
        /// </summary>
        [DataMember]
        public double Max
        {
            get { return max; }
            set { max = value; }
        }
        protected double max;
 
        /// <summary>
        /// 最小值
        /// </summary>
        [DataMember]
        public double Min
        {
            get { return min; }
            set { min = value; }
        }
        protected double min;
 
        /// <summary>
        /// 0次系数
        /// </summary>
        [DataMember]
        public double Index0
        {
            get { return index0; }
            set { index0 = value; }
        }
        protected double index0;
 
        /// <summary>
        /// 1次系数
        /// </summary>
        [DataMember]
        public double Index1
        {
            get { return index1; }
            set { index1 = value; }
        }
        protected double index1;
 
        /// <summary>
        /// 2次系数
        /// </summary>
        [DataMember]
        public double Index2
        {
            get { return index2; }
            set { index2 = value; }
        }
        protected double index2;
 
        /// <summary>
        /// 3次系数
        /// </summary>
        [DataMember]
        public double Index3
        {
            get { return index3; }
            set { index3 = value; }
        }
        protected double index3;
 
        /// <summary>
        /// 定义点
        /// </summary>
        [DataMember]
        public List<CurvePoint> DefinePoints
        {
            get { return definePoints; }
            set { definePoints = value; }
        }
        protected List<CurvePoint> definePoints = null;
 
        /// <summary>
        /// 是否为空
        /// </summary>
        [DataMember]
        public bool IsNull
        {
            get { return isNull; }
            set { isNull = value; }
        }
        protected bool isNull;
 
 
        #endregion
 
 
        /// <summary>
        /// 转化为保存字符串
        /// </summary>
        public string ToDsString()
        {
            var str_list = new List<string>();
            str_list.Add(this.FitPow.ToString());
            str_list.Add(((int)this.FitType).ToString());
            str_list.Add(this.Min.ToString());
            str_list.Add(this.Max.ToString());
            //str_list.Add(this.Index0.ToString());
            //str_list.Add(this.Index1.ToString("0.00000"));
            //str_list.Add(this.Index2.ToString("0.0000000000"));
            //str_list.Add(this.Index3.ToString("0.00000000000000"));
 
            //nsx
            var dc = new DoubleConverter();
            str_list.Add(dc.ConvertToString(this.Index0));
            str_list.Add(dc.ConvertToString(this.Index1));
            str_list.Add(dc.ConvertToString(this.Index2));
            str_list.Add(dc.ConvertToString(this.Index3));
 
            if (this.DefinePoints != null && this.DefinePoints.Count > 0)
            {
                str_list.Add(this.DefinePoints.ToDsString());
            }
            return string.Join(Separator, str_list);
        }
 
        object ICloneable.Clone()
        {
            return Clone();
        }
 
        public CurveExpress Clone()
        {
            return new CurveExpress(this);
        }
 
        /// <summary>
        /// 是否在内部
        /// </summary>
        public bool IsInterior(double x)
        {
            if (x < Min)
                return false;
            if (x > Max)
                return false;
            return true;
        }
 
        /// <summary>
        /// 获取拟合次方
        /// </summary>
        public static int GetFitPow(eCurveFitType fitType)
        {
            switch (fitType)
            {
                case eCurveFitType.CubicCurve: return 3;
                case eCurveFitType.ConicCurve: return 2;
                case eCurveFitType.ConicCurveB0: return 2;
                case eCurveFitType.LinearCurve: return 1;
                case eCurveFitType.ThroughPoint: return 0;
                default: return 3;
            }
        }
 
        /// <summary>
        /// 获取拟合类型
        /// </summary>
        public static eCurveFitType GetFitType(int fitPow)
        {
            switch (fitPow)
            {
                case 3: return eCurveFitType.CubicCurve;
                case 2: return eCurveFitType.ConicCurve;
                case 1: return eCurveFitType.LinearCurve;
                case 0: return eCurveFitType.ThroughPoint;
                default: return eCurveFitType.CubicCurve;
            }
        }
 
        /// <summary>
        /// 获取拟合点
        /// </summary>
        public double GetFitPointY(double x)
        {
            return FitCurveHelper.GetFitPointY(this, x);
        }
 
        /// <summary>
        /// 获取拟合点
        /// </summary>
        public List<CurvePoint> GetFitPoints(int pointNumber = 20)
        {
            if (this.IsNull)
                return default;
            return FitCurveHelper.GetFitPoints(this, pointNumber);
        }
 
        /// <summary>
        /// 通过区间内拟合点
        /// </summary>
        public List<CurvePoint> GetFitPointsByRange(double minX, double maxX, int pointNumber = 20)
        {
            if (this.IsNull)
                return default;
            return FitCurveHelper.GetFitPointsByRange(this, minX, maxX, pointNumber);
        }
 
        /// <summary>
        /// 获取拟合点
        /// </summary>
        public List<CurvePoint> GetFitPointsByExtend(double ratioExtend, int pointNumber = 20)
        {
            if (this.IsNull)
                return default;
            return GetFitPointsByRange(this.min, this.max * ratioExtend, pointNumber);
        }
 
        /// <summary>
        /// 获取第一个拟合点
        /// </summary>
        public CurvePoint GetFirstFitPoint()
        {
            return new CurvePoint(this.Min, GetFitPointY(this.Min));
        }
 
        /// <summary>
        /// 获取最后一个拟合点
        /// </summary>
        public CurvePoint GetLastFitPoint()
        {
            return new CurvePoint(this.Max, GetFitPointY(this.Max));
        }
 
        /// <summary>
        /// 获取零点的Y值
        /// </summary>
        public double GetZeroFitPointY()
        {
            return this.Index0;
        }
 
        /// <summary>
        /// 转换为Model
        /// </summary>
        public static CurveExpress ToModel(string strParas)
        {
            if (string.IsNullOrEmpty(strParas))
                return default;
            var express = new CurveExpress(strParas);
            if (express == null)
                return default;
            return express;
        }
 
        /// <summary>
        /// 是否相等
        /// </summary>
        public bool IsEquals(CurveExpress rhs)
        {
            if (rhs == null)
                return false;
            if (rhs.ToDsString().Equals(this.ToDsString()))
                return true;
            else
                return false;
        }
 
        /// <summary>
        /// 是否为Zero表达式
        /// </summary>
        public bool IsZeroExpress()
        {
            if (this.Index0 != 0)
                return false;
            if (this.Index1 != 0)
                return false;
            if (this.Index2 != 0)
                return false;
            if (this.Index3 != 0)
                return false;
            return true;
        }
 
        /// <summary>
        /// 通过 X转换
        /// </summary>
        public CurveExpress TransferX(double ratioX)
        {
            var fitPoints = this.GetFitPoints();
            fitPoints = fitPoints?.Select(x => new CurvePoint(x.X * ratioX, x.Y)).ToList();
            if (fitPoints == null || fitPoints.Count < 1)
                return default;
            var exp = new CurveExpress(fitPoints, this.FitType);
            var definePoints = this.DefinePoints?.Select(x => new CurvePoint(x.X * ratioX, x.Y)).ToList();
            if (definePoints != null && definePoints.Count > 0)
                exp.DefinePoints = definePoints;
            return exp;
        }
 
        /// <summary>
        /// 相似换算(speed_ratio = 新的转速 除以 老的转速)
        /// </summary>
        public CurveExpress SimuBySpeed(eFeatCurveType featType, double speed_ratio, int pointNumber = 20)
        {
            double space = (this.Max - this.Min) / (pointNumber - 1);
            switch (featType)
            {
                case eFeatCurveType.QH:
                    {
                        List<CurvePoint> pointInfo60 = new List<CurvePoint>();
 
                        for (int i = 0; i < pointNumber; i++)
                        {
                            double q50 = space * i + this.Min;
                            double q60 = q50 * speed_ratio;
                            double Y50 = GetFitPointY(q50);
                            pointInfo60.Add(new CurvePoint() { X = q60, Y = Y50 * speed_ratio * speed_ratio });
                        }
 
                        var new_curve = new CurveExpress(pointInfo60, this.FitType);
                        new_curve.DefinePoints = this.DefinePoints?.Select(x => new CurvePoint() { X = x.X * speed_ratio, Y = x.Y * speed_ratio * speed_ratio }).ToList();
                        return new_curve;
                    }
                case eFeatCurveType.QE:
                    {
                        List<CurvePoint> pointInfo60 = new List<CurvePoint>();
 
                        for (int i = 0; i < pointNumber; i++)
                        {
                            double q50 = space * i + this.Min;
                            double q60 = q50 * speed_ratio;
                            double Y50 = GetFitPointY(q50);
                            pointInfo60.Add(new CurvePoint() { X = q60, Y = Y50 });
                        }
 
                        var new_curve = new CurveExpress(pointInfo60, this.FitType);
                        return new_curve;
                    }
                case eFeatCurveType.QP:
                    {
                        List<CurvePoint> pointInfo60 = new List<CurvePoint>();
 
                        for (int i = 0; i < pointNumber; i++)
                        {
                            double q50 = space * i + this.Min;
                            double q60 = q50 * speed_ratio;
                            double Y50 = GetFitPointY(q50);
                            pointInfo60.Add(new CurvePoint() { X = q60, Y = Y50 * speed_ratio * speed_ratio * speed_ratio });
                        }
 
                        var new_curve = new CurveExpress(pointInfo60);
                        new_curve.DefinePoints = this.DefinePoints?.Select(x => new CurvePoint() { X = x.X * speed_ratio, Y = x.Y * speed_ratio * speed_ratio * speed_ratio }).ToList();
                        return new_curve;
                    }
                default:
                    {
                        List<CurvePoint> pointInfo60 = new List<CurvePoint>();
 
                        for (int i = 0; i < pointNumber; i++)
                        {
                            double q50 = space * i + this.Min;
                            double q60 = q50 * speed_ratio;
                            double Y50 = GetFitPointY(q50);
                            pointInfo60.Add(new CurvePoint() { X = q60, Y = Y50 });
                        }
 
                        var new_curve = new CurveExpress(pointInfo60);
                        new_curve.DefinePoints = this.DefinePoints?.Select(x => new CurvePoint() { X = x.X * speed_ratio, Y = x.Y }).ToList();
                        return new_curve;
                    }
            }
        }
 
        /// <summary>
        /// 50 到 60
        /// </summary>
        public CurveExpress SimuFrom50To60(eFeatCurveType featType, int pointNumber = 20)
        {
            return SimuBySpeed(featType, 1.2, pointNumber);
        }
 
 
        /// <summary>
        /// 曲线方程:用于显示
        /// </summary>
        public string GetCurveFunction(string xName = "Q")
        {
            //double signValue = 0;
            //int value = 0;
 
            StringBuilder sb = new StringBuilder();
 
            //sb.Append(this.index0.ToString("G"));
            //sb.Append(this.index0.ToString("0.00E+000"));//ToString("G"));
            sb.Append(Math.Abs(this.Index0).ToString("0.00"));//.ToString("G"));
 
            if (this.Index1 > 0)
                sb.Append(" + ");
            else
                sb.Append(" - ");
            //GetFuncScientificValue(1,Math.Abs(index1),ref signValue,ref value);
            //sb.AppendFormat("{0}E{1}",signValue,value);
            sb.Append(Math.Abs(this.Index1).ToString("0.0000"));//.ToString("G"));
            sb.Append(" * ");
            sb.Append(xName);
 
 
            if (this.Index2 > 0)
                sb.Append(" + ");
            else
                sb.Append(" - ");
            //GetFuncScientificValue(2,Math.Abs(index2),ref signValue,ref value);
            //sb.AppendFormat("{0}E{1}",signValue,value);
            //sb.Append(Math.Round(Math.Abs(this.index2),4).ToString("G"));//.ToString("G"));
            sb.Append(Math.Abs(this.Index2).ToString("0.000000000"));//.ToString("G"));
            sb.Append(" * ");
            sb.Append(xName);
            sb.Append("^2 ");
 
 
            if (this.Index3 > 0)
                sb.Append("  +  ");
            else
                sb.Append("  -  ");
            //GetFuncScientificValue(3,Math.Abs(index3),ref signValue,ref value);
            //sb.AppendFormat("{0}E{1}",signValue,value);
            //sb.Append(Math.Abs(this.index3).ToString("G"));//.ToString("G"));
            sb.Append(Math.Abs(this.Index3).ToString("0.000000000000"));//.ToString("G"));
            sb.Append(" * ");
            sb.Append(xName);
            sb.Append("^3 ");
 
            return sb.ToString();
        }
 
  
        private static void GetFuncScientificValue(int FunctionIndex,
            double FunctionCoeff, ref double signValue, ref int indexValue)
        {
            string strList = "";
            if (FunctionIndex == 0)
            {
                strList = FunctionCoeff.ToString("0.000e+000");
            }
            else if (FunctionIndex == 1)
            {
                strList = FunctionCoeff.ToString("0.000000e+000");
            }
            else if (FunctionIndex == 2)
            {
                strList = FunctionCoeff.ToString("#.#########e+000");
            }
            else if (FunctionIndex == 3)
            {
                strList = FunctionCoeff.ToString("#.############e+000");
            }
            string[] st1 = null;
            st1 = strList.Split('e');
            signValue = Convert.ToDouble(st1[0]);
            indexValue = Convert.ToInt32(st1[1]);
        }
 
 
 
 
 
 
 
 
 
 
    }
}