CurveCollection.cs 14.8 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace FLY.Simulation.Casting
{
    public class CurveCollection : ICurveService, Misc.ISaveToXml
    {
        #region 数据文件需要保存的数据项
        CurveCorrectWay correctWay = CurveCorrectWay.OnePointIsScale;
        /// <summary>
        /// AD曲线校正方式
        /// </summary>
        public CurveCorrectWay CorrectWay
        {
            get
            {
                return correctWay;
            }
            set
            {
                if (correctWay != value)
                {
                    correctWay = value;
                    NotifyPropertyChanged("CorrectWay");
                }
            }
        }

        private CurveType flag;
        public CurveType Flag
        {
            get
            {
                return flag;
            }
            set
            {
                if (flag != value)
                {
                    flag = value;
                    NotifyPropertyChanged("Flag");
                }
            }
        }
        /// <summary>
        /// 输入的曲线, 排列顺序,Value 从 小到大
        /// </summary>
        private ObservableCollection<CurveCell> mCurves = new ObservableCollection<CurveCell>();
        public ObservableCollection<CurveCell> Curves
        {
            get
            {
                return mCurves;
            }
        }
        #endregion

        public event ActiveEventHandler ActiveEvent;

        public class ExChange : Misc.ISaveToXml
        {
            static ExChange()
            {
                Misc.SaveToXmlHepler.Regist(typeof(ExChange));
            }
            public int OrgAD { get; set; }
            public int CurrAD { get; set; }

            public string[] GetSavePropertyNames()
            {
                return new string[]{
                    "OrgAD",
                    "CurrAD"};
            }
        }
        /// <summary>
        /// 真实样品校正点
        /// </summary>
        private ObservableCollection<ExChange> mRevisingCurves = new ObservableCollection<ExChange>();
        public ObservableCollection<ExChange> RevisingCurves
        {
            get
            {
                return mRevisingCurves;
            }
        }
        private string param_path = "curve.xml";
        public CurveCollection()
        {
            SetDefault();
            ReviseCurve();
        }
        public CurveCollection(string param_path)
        {
            if (!string.IsNullOrEmpty(param_path))
                this.param_path = param_path;

            SetDefault();
            Load();
            ReviseCurve();
        }
        void SetDefault()
        {
            CorrectWay = CurveCorrectWay.OnePointIsScale;
            Flag = CurveType.E;
            Curves.Add(new CurveCell() { AD = 57564, Value = 0 });
            Curves.Add(new CurveCell() { AD = 30850, Value = 8800 });
            Curves.Add(new CurveCell() { AD = 19000, Value = 17600 });
            Curves.Add(new CurveCell() { AD = 12528, Value = 26400 });
            Curves.Add(new CurveCell() { AD = 8409, Value = 35200 });
            Curves.Add(new CurveCell() { AD = 5650, Value = 44000 });
            Curves.Add(new CurveCell() { AD = 3779, Value = 52800 });
            Curves.Add(new CurveCell() { AD = 2513, Value = 61600 });
            Curves.Add(new CurveCell() { AD = 1660, Value = 70400 });
        }
        public void Apply()
        {
            ClearExChange();
            ReviseCurve();

            if (ActiveEvent != null)
                ActiveEvent(this);
            Save();
        }
        public void Clear()
        {
            Curves.Clear();
            ClearExChange();
        }

        #region ExChange

        public void ModifyExChange(List<ExChange> list)
        {
            RevisingCurves.Clear();
            for (int i = 0; i < list.Count(); i++)
            {
                //检测RevisingCurves 正确性,避免 /0 溢出
                if ((list[i].OrgAD <= 10) || (list[i].CurrAD <= 10))
                {
                    //异常
                    continue;
                }
                var v = from rc in RevisingCurves where rc.OrgAD == list[i].OrgAD select rc;
                if (v.Count() > 0)
                {
                    //异常,重复了
                    continue;
                }
                RevisingCurves.Add(list[i]);
            }
            RevisingCurves.OrderByDescending(c => c.OrgAD);
        }


        public void ClearExChange()
        {
            RevisingCurves.Clear();
        }
        #endregion


        public static bool CheckRevisingPointValid(int ad)
        {
            if (ad < 3)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 根据样品值,修正曲线,只支持2个样品
        /// </summary>
        /// <returns></returns>
        public bool ReviseCurve()
        {
            if (RevisingCurves.Count < 1)//没数据
            {
                foreach (CurveCell c in Curves)
                {
                    c.RevisedAD = c.AD;
                }
                return false;
            }

            if (RevisingCurves.Count == 1)//只有一个校正点
            {
                switch (CorrectWay)
                {
                    case CurveCorrectWay.OnePointIsScale:
                        {
                            double ux = ((double)RevisingCurves[0].CurrAD) / RevisingCurves[0].OrgAD;

                            foreach (CurveCell c in Curves)
                            {
                                c.RevisedAD = (int)(c.AD * ux);
                            }
                        } break;
                    default:
                        //case CurveCorrectWay.OnePointIsOffset:
                        {
                            int x = RevisingCurves[0].CurrAD - RevisingCurves[0].OrgAD;

                            foreach (CurveCell c in Curves)
                            {
                                c.RevisedAD = c.AD + x;
                            }
                        } break;
                }

                return true;
            }
            else
            {
                int adh = RevisingCurves[0].OrgAD;
                int adl = RevisingCurves[1].OrgAD;
                double uh = ((double)RevisingCurves[0].CurrAD) / adh;
                double ul = ((double)RevisingCurves[1].CurrAD) / adl;

                for (int i = 0, j = 1; i < Curves.Count; i++)
                {
                    double ux = uh - (adh - Curves[i].AD) * (uh - ul) / (adh - adl);
                    Curves[i].RevisedAD = (int)(Curves[i].AD * ux);
                }
                return true;
            }
        }

        #region E

        int AD2Value_E(int ad, AD2ValueFlag flag)
        {
            int i;
            int thick;

            if (Curves.Count < 1) return -1;
            if (ad < 0) return -1;
            if (ad == 0) ad = 1;
            if (flag == AD2ValueFlag.NoRevised)
            {
                for (i = 0; i < Curves.Count; i++)
                {
                    if (ad < Curves[i].AD)
                        continue;
                    else
                        break;
                }
            }
            else
            {
                for (i = 0; i < Curves.Count; i++)
                {
                    if (ad < Curves[i].RevisedAD)
                        continue;
                    else
                        break;
                }
            }
            if (i >= Curves.Count) i = Curves.Count - 1;

            if (i == 0) i = 1;
            if (flag == AD2ValueFlag.NoRevised)
            {
                double u;

                u = Math.Log((double)Curves[i - 1].AD / Curves[i].AD, Math.E) * 100 / (Curves[i].Value - Curves[i - 1].Value);
                thick = (int)(Math.Log((double)Curves[i - 1].AD / ad, Math.E) * 100 / u + Curves[i - 1].Value);
            }
            else
            {
                double u;

                u = Math.Log((double)Curves[i - 1].RevisedAD / Curves[i].RevisedAD, Math.E) * 100 / (Curves[i].Value - Curves[i - 1].Value);
                thick = (int)(Math.Log((double)Curves[i - 1].RevisedAD / ad, Math.E) * 100 / u + Curves[i - 1].Value);

            }
            //if (thick < 0)
            //    return 0;

            return thick;
        }

        int Value2AD_E(int value)
        {
            int i;
            int ad;

            if (Curves.Count < 1) return -1;
            if (value < 0)
                value = 0;


            for (i = 0; i < Curves.Count; i++)
            {
                if (value > Curves[i].Value)
                    continue;
                else
                    break;
            }
            
            if (i >= Curves.Count) i = Curves.Count - 1;

            if (i == 0) i = 1;

            double u;

            u = (Math.Log(Curves[i - 1].AD) - Math.Log(Curves[i].AD)) / (Curves[i - 1].Value - Curves[i].Value);
            ad = (int)Math.Exp((value - Curves[i - 1].Value) * u + Math.Log(Curves[i - 1].AD));
            return ad;
        }
        #endregion
        #region 线性
        int AD2Value_Line(int ad, AD2ValueFlag flag)
        {
            int i;
            int thick;

            if (Curves.Count < 2) return -1;
            if (ad < 0) return -1;
            if (ad == 0) ad = 1;
            bool isDescending = true;//降序排列
            if (Curves[0].AD < Curves[1].AD)
                isDescending = false;
            //找 ad0<ad<adi

            if (flag == AD2ValueFlag.NoRevised)
            {
                if (isDescending)//降序排列
                {
                    for (i = 0; i < Curves.Count; i++)
                    {
                        if (ad < Curves[i].AD)
                            continue;
                        else
                            break;
                    }
                }
                else
                {
                    for (i = 0; i < Curves.Count; i++)
                    {
                        if (ad > Curves[i].AD)
                            continue;
                        else
                            break;
                    }
                }
                if (i >= Curves.Count) i = Curves.Count - 1;
                if (i == 0) i = 1;

                double adi_ad0 = Curves[i].AD - Curves[i - 1].AD;
                double vi_v0 = Curves[i].Value - Curves[i - 1].Value;
                double a = vi_v0 / adi_ad0;
                double b = Curves[i - 1].Value - Curves[i - 1].AD * a;

                thick = (int)(ad * a + b);
                return thick;
            }
            else
            {
                if (isDescending)//降序排列
                {
                    for (i = 0; i < Curves.Count; i++)
                    {
                        if (ad < Curves[i].RevisedAD)
                            continue;
                        else
                            break;
                    }
                }
                else
                {
                    for (i = 0; i < Curves.Count; i++)
                    {
                        if (ad > Curves[i].RevisedAD)
                            continue;
                        else
                            break;
                    }
                }
                if (i >= Curves.Count) i = Curves.Count - 1;
                if (i == 0) i = 1;

                double adi_ad0 = Curves[i].RevisedAD - Curves[i - 1].RevisedAD;
                double vi_v0 = Curves[i].Value - Curves[i - 1].Value;
                double a = vi_v0 / adi_ad0;
                double b = Curves[i - 1].Value - Curves[i - 1].RevisedAD * a;

                thick = (int)(ad * a + b);
                return thick;
            }
        }

        int Value2AD_Line(int thick)
        {
            int i;
            int ad;

            if (Curves.Count < 2) return -1;

            bool isDescending = true;//降序排列
            if (Curves[0].Value < Curves[1].Value)
                isDescending = false;
            //找 value0<thick<value1


                if (isDescending)//降序排列
                {
                    for (i = 0; i < Curves.Count; i++)
                    {
                        if (thick < Curves[i].Value)
                            continue;
                        else
                            break;
                    }
                }
                else
                {
                    for (i = 0; i < Curves.Count; i++)
                    {
                        if (thick > Curves[i].Value)
                            continue;
                        else
                            break;
                    }
                }
                if (i >= Curves.Count) i = Curves.Count - 1;
                if (i == 0) i = 1;

                double vi_v0 = Curves[i].Value - Curves[i - 1].Value;
                double adi_ad0 = Curves[i].AD - Curves[i - 1].AD;
                double a = vi_v0 / adi_ad0;
                double b = Curves[i - 1].Value - Curves[i - 1].AD * a;

                ad = (int)((thick - b)/a);
                return ad;
            }
        
        #endregion

        public int AD2Value(int ad, AD2ValueFlag flag)
        {
            switch (Flag)
            {
                case CurveType.Line:
                    return AD2Value_Line(ad, flag);
                default:
                    return AD2Value_E(ad, flag);
            }
        }

        public int ValueAD(int value)
        {
            switch (Flag)
            {
                case CurveType.Line:
                    return Value2AD_Line(value);
                default:
                    return Value2AD_E(value);
            }
        }
        #region ITDParam 成员
        public bool Load()
        {
            bool ret = Misc.SaveToXmlHepler.Load("curve.xml", this);
            RevisingCurves.Clear();
            Curves.OrderBy(c => c.Value);

            return ret;
        }
        public void Save()
        {
            Misc.SaveToXmlHepler.Save("curve.xml", this);
        }
        #endregion

        #region ISaveToXml 成员

        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "CorrectWay",
                "Flag",
                "Curves"};
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }

        #endregion
    }
}