CurveCollection.cs 11.9 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace FLY.Simulation
{
    public class CurveCollection : Misc.ISaveToXml
    {
        public static string EXT_NAME = ".curve";
        public static string DEFAULT_FILE = "default" + EXT_NAME;

        #region 数据文件需要保存的数据项
        public class Curve : Misc.ISaveToXml, INotifyPropertyChanged
        {
            private int _value;
            /// <summary>
            /// 面密度
            /// </summary>
            public int Value
            {
                get { return _value; }
                set
                {
                    _value = value;
                    NotifyPropertyChanged("Value");
                }
            }
            private int _ad;

            public int AD
            {
                get { return _ad; }
                set
                {
                    _ad = value;
                    NotifyPropertyChanged("AD");
                }
            }

            private int _revisedAD;
            /// <summary>
            /// 校正后的AD值
            /// </summary>
            public int RevisedAD
            {
                get { return _revisedAD; }
                set
                {
                    _revisedAD = value;
                    NotifyPropertyChanged("RevisedAD");
                }
            }

            #region ISaveToXml 成员

            public string[] GetSavePropertyNames()
            {
                return new string[]{
                    "Value",
                    "AD"};
            }

            #endregion

            #region INotifyPropertyChanged 成员
            protected void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;

            #endregion
        }
        public class ExChange : Misc.ISaveToXml
        {
            public int OrgAD { get; set; }
            public int CurrAD { get; set; }

            public string[] GetSavePropertyNames()
            {
                return new string[]{
                    "OrgAD",
                    "CurrAD"};
            }
        }

        /// <summary>
        /// 输入的曲线
        /// </summary>
        private ObservableCollection<Curve> mCurves = new ObservableCollection<Curve>();
        public ObservableCollection<Curve> Curves
        {
            get
            {
                return mCurves;
            }
        }
        #endregion

        /// <summary>
        /// 真实样品校正点
        /// </summary>
        private ObservableCollection<ExChange> mRevisingCurves = new ObservableCollection<ExChange>();
        public ObservableCollection<ExChange> RevisingCurves
        {
            get
            {
                return mRevisingCurves;
            }
        }
        public CurveCollection()
        {
            Misc.SaveToXmlHepler.Regist(typeof(Curve));
            Misc.SaveToXmlHepler.Regist(typeof(ExChange));

            //Curves.Add(new Curve() { AD = 57564, Value = 0 });
            //Curves.Add(new Curve() { AD = 30850, Value = 8800 });
            //Curves.Add(new Curve() { AD = 19000, Value = 17600 });
            //Curves.Add(new Curve() { AD = 12528, Value = 26400 });
            //Curves.Add(new Curve() { AD = 8409, Value = 35200 });
            //Curves.Add(new Curve() { AD = 5650, Value = 44000 });
            //Curves.Add(new Curve() { AD = 3779, Value = 52800 });
            //Curves.Add(new Curve() { AD = 2513, Value = 61600 });
            //Curves.Add(new Curve() { AD = 1660, Value = 70400 });


            Curves.Add(new Curve() { Value = 0, AD = 50300 });
            Curves.Add(new Curve() { Value = 1900, AD= 46505});
            Curves.Add(new Curve() { Value = 3800, AD=42999});
            Curves.Add(new Curve() { Value = 5800, AD=39698});
            Curves.Add(new Curve() { Value = 7800, AD=36646});
            Curves.Add(new Curve() { Value = 9800, AD=33894});
            Curves.Add(new Curve() { Value = 11800, AD=31229});
            Curves.Add(new Curve() { Value = 13700, AD=28934});
            Curves.Add(new Curve() { Value = 15600, AD=26818});
            Curves.Add(new Curve() { Value = 17500, AD=24908});
            Curves.Add(new Curve() { Value = 19400, AD=23220});
            Curves.Add(new Curve() { Value = 21300, AD=21558});
            Curves.Add(new Curve() { Value = 23200, AD=20057});
            Curves.Add(new Curve() { Value = 25100, AD=18606});
            Curves.Add(new Curve() { Value = 27100, AD=17283});
            Curves.Add(new Curve() { Value = 29000, AD = 16035 });

            //foreach (Curve c in Curves) 
            //{
            //    c.AD = (int)(c.AD * 58881.0 / 50300);
            //}

           
        }

        public void Clear()
        {
            Curves.Clear();
            ClearExChange();
        }
        #region CURVE
        //ad值从大到小排列
        public void ModifyPoint(int ad, int value)
        {
            var cs = from c in Curves where c.AD == ad select c;
            if (cs.Count() > 0)
            {
                cs.First().Value = value;
            }
            else
            {
                Curves.Add(new Curve() { AD = ad, Value = value });
                Curves.OrderByDescending(c => c.AD);
            }
        }
        public bool DeletePoint(int ad)
        {
            var cs = from c in Curves where c.AD == ad select c;
            if (cs.Count() > 0)
            {
                Curves.Remove(cs.First());
                return true;
            }
            return false;
        }
        #endregion
        #region ExChange

        public void ModifyExChange(int org_ad, int curr_ad)
        {
            //index=0 zero, index=1 sample
            var cs = from c in RevisingCurves where c.OrgAD == org_ad select c;
            if (cs.Count() > 0)
            {
                cs.First().CurrAD = curr_ad;
            }
            else
            {
                RevisingCurves.Add(new ExChange() { OrgAD = org_ad, CurrAD = curr_ad });
                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;
            }
        }

        public bool ReviseCurve()
        {
            if (RevisingCurves.Count < 1)//没数据
            {
                foreach (Curve c in Curves)
                {
                    c.RevisedAD = c.AD;
                }
                return false;
            }

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

                foreach (Curve c in Curves)
                {
                    c.RevisedAD = (int)(c.AD * ux);
                }
                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;

                //int k;
                for (int i = 0; i < Curves.Count; i++)
                {
                    //k = j;
                    //while ((m_curves[i].Value > m_revisingCurves[j].Value) && (j < m_revisingCurves.Count - 1)) j++;
                    //if (k != j)
                    //{
                    //    adh = m_revisingCurves[j-1].OrgAD;
                    //    adl = m_revisingCurves[j].OrgAD;
                    //    uh = ((double)m_revisingCurves[j-1].CurrAD) / adh;
                    //    ul = ((double)m_revisingCurves[j].CurrAD) / adl;
                    //}
                    double ux = uh - (adh - Curves[i].AD) * (uh - ul) / (adh - adl);

                    Curves[i].RevisedAD = (int)(Curves[i].AD * ux);
                }

                return true;
            }
        }

        public int Value2AD(int value, AD2ValueFlag flag)
        {
            int n = Curves.Count;
            int i;
            int ad;
            double tmp, tmp1;

            if (Curves.Count <= 0)
                return Misc.MyBase.NULL_VALUE;

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

            if (flag == AD2ValueFlag.NoRevised)
            {
                tmp1 = (double)Curves[i - 1].AD / Curves[i].AD;
                ad = (int)(Curves[i - 1].AD * Math.Exp(tmp * Math.Log(tmp1, Math.E)));
            }
            else
            {
                tmp1 = (double)Curves[i - 1].RevisedAD / Curves[i].RevisedAD;
                ad = (int)(Curves[i - 1].RevisedAD * Math.Exp(tmp * Math.Log(tmp1, Math.E)));
            }
            return ad;
        }
        public enum AD2ValueFlag
        {
            /// <summary>
            /// 修正后的曲线
            /// </summary>
            Revised,
            /// <summary>
            /// 原始曲线
            /// </summary>
            NoRevised
        }
        public int AD2Value(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);

            }
            return thick;
        }

        public int GetZeroAD()
        {
            if (RevisingCurves.Count < 1)
                return Curves[0].AD;
            else
                return Curves[0].RevisedAD;
        }


        bool IsCurveFile(string file)
        {
            if (file.Length < EXT_NAME.Length) return false;

            if (file.Substring(file.Length - EXT_NAME.Length).CompareTo(EXT_NAME) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        #region ISaveToXml 成员

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

        #endregion
    }
}