using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.Security.Permissions; namespace Hydro.Model { [DataContract] public partial class Point : ICloneable { [DataMember] public double X { get { return x; } set { x = value; } } [DataMember] public double Y { get { return y; } set { y = value; } } [DataMember] public double Z { get { return z; } set { z = value; } } protected double x; protected double y; protected double z; #region 构造函数 public Point() { } public Point(double x, double y,double z) { this.x = x; this.y = y; this.z = z; } public Point (Point2d rhs) { this.X = rhs.X; this.Y = rhs.Y; } public Point(Vector rhs) { this.X = rhs.X; this.Y = rhs.Y; this.z = rhs.Z; } public Point(double x, double y) { this.x = x; this.y = y; } public Point(int x, int y) { this.x = x; this.y = y; } public Point(decimal x, decimal y) { this.x = Convert.ToDouble(x); this.y = Convert.ToDouble(y); } public Point(Point rhs) { this.X = rhs.X; this.Y = rhs.Y; this.Z = rhs.Z; } public Point(string strPara) { if (string.IsNullOrEmpty(strPara)) return; string[] strPara_split_array = strPara.Split(','); if (strPara_split_array.Count() < 3) return; X = Convert.ToDouble(strPara_split_array[0]); Y = Convert.ToDouble(strPara_split_array[1]); Z = Convert.ToDouble(strPara_split_array[2]); } public static Point ToParameter(string strPara) { if (string.IsNullOrEmpty(strPara)) return null; string[] strPara_split_array = strPara.Split(','); if (strPara_split_array.Count() < 3) return null; Point pt = new Point(); pt.X = Convert.ToDouble(strPara_split_array[0]); pt.Y = Convert.ToDouble(strPara_split_array[1]); pt.Z = Convert.ToDouble(strPara_split_array[2]); return pt; } #endregion #region ToString public override string ToString() { return string.Format("X:{0},Y:{1},Z:{2}", X, Y,Z); } #endregion #region Clone public Point Clone() //对外提供一个创建自身的浅表副本的能力 { return new Point(x, y); } object ICloneable.Clone() { return this.Clone(); } public override bool Equals(object obj) { Point rhs = obj as Point; return this.X == rhs.X && this.Y == rhs.Y; } public static Point Copy(Point pt) { if (pt == null) return null; return new Point(pt); } public static List Copy(List list) { if (list == null) return null; List newList = new List(); foreach (var pt in list) newList.Add(new Point(pt)); return newList; } #endregion /// /// Return the HashCode from the base class. /// /// public override int GetHashCode() { return base.GetHashCode(); } /// /// Missing values are represented internally using . /// public const double Missing = Double.MaxValue; /// /// The default format to be used for displaying model values via the /// method. /// public const string DefaultFormat = "N2"; #region Serialization public const int schema = 11; protected Point(SerializationInfo info, StreamingContext context) { //int sch = info.GetInt32("schema"); Z = info.GetDouble("Z"); X = info.GetDouble("X"); Y = info.GetDouble("Y"); } [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)] public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("schema", schema); info.AddValue("X", X); info.AddValue("Y", Y); info.AddValue("Z", Z); } public string ToDsString() { return ToDsString(this); } public static string ToDsString(Point point) { return string.Format("{0},{1},{2}", point.X, point.Y, point.Z); } #endregion #region Properties /// /// Readonly value that determines if either the X or the Y /// coordinate in this PointPair is a missing value. /// /// true if either value is missing public bool IsMissing() { return this.X == Point.Missing || this.Y == Point.Missing || this.Z == Point.Missing; } /// /// Readonly value that determines if either the X or the Y /// coordinate in this PointPair is an invalid (not plotable) value. /// It is considered invalid if it is missing (equal to System.Double.MaxWholeAxis), /// Infinity, or NaN. /// /// true if either value is invalid public bool IsInvalid() { return this.X == Point.Missing || this.Y == Point.Missing || this.Z == Point.Missing || Double.IsInfinity(this.X) || Double.IsInfinity(this.Y) || Double.IsInfinity(this.Z) || Double.IsNaN(this.X) || Double.IsNaN(this.Y) || Double.IsNaN(this.Z); } /// /// static method to determine if the specified model value is invalid. /// /// The value is considered invalid if it is , /// , /// or . /// The value to be checked for validity. /// true if the value is invalid, false otherwise public static bool IsValueInvalid(double value) { return (value == Point.Missing || Double.IsInfinity(value) || Double.IsNaN(value)); } #endregion public enum eSortType { X, Y, Z } public enum eCompareType { X, Y, Z, ALL } //排序与比较 public class Comparer : IComparer { private Hydro.Model.Point.eSortType sortType; public Comparer(Hydro.Model.Point.eSortType type) { sortType = type; } #region IComparer 成员 int IComparer.Compare(Hydro.Model.Point obj1, Hydro.Model.Point obj2) { if (sortType == Hydro.Model.Point.eSortType.X) { if (Math.Abs(obj1.X - obj2.X) < 0.00001) { return 0; } else if (obj1.X > obj2.X) { return 1; } else { return -1; } } else if (sortType == Hydro.Model.Point.eSortType.Z) { if (Math.Abs(obj1.Z - obj2.Z) < 0.00001) { return 0; } else if (obj1.Z > obj2.Z) { return 1; } else { return -1; } } else { if (Math.Abs(obj1.Y - obj2.Y) < 0.00001) { return 0; } else if (obj1.Y > obj2.Y) { return 1; } else { return -1; } } } #endregion } //比较相同:主要用与LIST的Contains方法和Distinct public class EqualComparer : IEqualityComparer { private double ignoreDis = 0.00001; public Hydro.Model.Point.eCompareType CompareType = eCompareType.X;//比较类型 public EqualComparer() { ignoreDis = 0.00001; } public EqualComparer(double dis, Hydro.Model.Point.eCompareType compareType) { ignoreDis = dis; CompareType = compareType; } public bool Equals(Hydro.Model.Point lhs, Hydro.Model.Point rhs) { switch (CompareType) { case eCompareType.X: if (Math.Abs(lhs.X - rhs.X) < ignoreDis) return true; break; case eCompareType.Y: if (Math.Abs(lhs.Y - rhs.Y) < ignoreDis) return true; break; case eCompareType.Z: if (Math.Abs(lhs.Z - rhs.Z) < ignoreDis) return true; break; case eCompareType.ALL: if (Math.Abs(lhs.X - rhs.X) < ignoreDis && Math.Abs(lhs.Y - rhs.Y) < ignoreDis && Math.Abs(lhs.Z - rhs.Z) < ignoreDis) return true; break; } return false; } public int GetHashCode(Hydro.Model.Point obj) { //return obj.X.GetHashCode() + obj.Y.GetHashCode(); return 0; } } } }