using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; namespace Eventech.Model { //比ThroughCurve多一个参数 [Serializable] public class ThroughParaCurve : ThroughCurve, ICloneable { public ThroughParaCurve() { } public ThroughParaCurve(string strPara) { if (string.IsNullOrEmpty(strPara)) { isNull = true; return; } string[] strParas = strPara.Split(','); if (strParas.Count() < 5) { isNull = true; return; } DispTension = float.Parse(strParas[0]); IsClosed = bool.Parse(strParas[1]); CurvePara = Convert.ToDouble(strParas[2]); int iPtCount = Convert.ToInt32(strParas[3]); if (iPtCount == 0) { isNull = true; return; } PointInfo = new Eventech.Model.FeatPointList(); for (int i = 0; i < iPtCount; i++) {//可能只有一个点 PointInfo.Add(new Eventech.Model.FeatPoint() { X = Convert.ToDouble(strParas[4 + i * 2]), Y = Convert.ToDouble(strParas[4 + 2 * i + 1]) }); } } public ThroughParaCurve(ThroughParaCurve rhs) { this.DispTension = rhs.DispTension; this.IsClosed = rhs.IsClosed; this.CurvePara = rhs.CurvePara; this.PointInfo = rhs.PointInfo.Clone(); } public ThroughParaCurve(double para, List curveInfo, bool isClosed=false, float tension=0.5f) { CurvePara = para; PointInfo = new Eventech.Model.FeatPointList(curveInfo); IsClosed = isClosed; DispTension = tension; } [DataMember] public double CurvePara { get { return _curvePara; } set { _curvePara = value; } } public double _curvePara = -1; public new ThroughParaCurve Clone() { return new ThroughParaCurve(this); } object ICloneable.Clone() { return Clone(); } public new string ToDsString() { return ToDsString(this); } public static string ToDsString(ThroughParaCurve curve) { if (curve == null || curve.PointInfo == null) return null; StringBuilder sb = new StringBuilder(); sb.Append(curve.DispTension); sb.Append(","); sb.Append(curve.IsClosed); sb.Append(","); sb.Append(curve.CurvePara); sb.Append(","); sb.Append(curve.PointInfo.Count); foreach (var point in curve.PointInfo) {//必须用,分割,//可能只有一个点 sb.AppendFormat(",{0},{1}", point.X, point.Y); } return sb.ToString(); } public static ThroughParaCurve ToParameter(string strPara) { ThroughParaCurve curve = new ThroughParaCurve(strPara); if (curve.IsNull) return null; return curve; } public class Comparer : IComparer { public Comparer() { } #region IComparer 成员 private double ignoreDis = 0.001; int IComparer.Compare(ThroughParaCurve obj1, ThroughParaCurve obj2) { if (Math.Abs(obj1.CurvePara - obj2.CurvePara) < ignoreDis) {//参数一样 就比较Y的平均值 double y1_arv = (from x in obj1.PointInfo select x.Y).Average(); double y2_arv = (from x in obj2.PointInfo select x.Y).Average(); if (Math.Abs(y1_arv - y2_arv)<0.01) return 0; else if (y1_arv > y2_arv) return 1; else return -1; } else if (obj1.CurvePara > obj2.CurvePara) { return 1; } else { return -1; } } #endregion } //比较相同:主要用与LIST的Contains方法和Distinct public class EqualComparer : IEqualityComparer { private double ignoreDis = 0.001; public EqualComparer() { ignoreDis = 0.001; } public EqualComparer(double dis) { ignoreDis = dis; } public bool Equals(ThroughParaCurve lhs, ThroughParaCurve rhs) { if (Math.Abs(lhs.CurvePara - rhs.CurvePara) < ignoreDis) return true; else return false; } public int GetHashCode(ThroughParaCurve obj) { //return obj.X.GetHashCode() + obj.Y.GetHashCode(); return 0; } } } }