using System; using System.ComponentModel; using System.Globalization; namespace Deltares.DamPiping { [TypeConverter(typeof(Point3D))] public class Point3D : ExpandableObjectConverter, IComparable { private double x; private double y; private double z; [Description("Set X coordinate")] public double X { get { return this.x; } set { this.x = value; } } [Description("Set Y coordinate")] public double Y { get { return this.y; } set { this.y = value; } } [Description("Set Z coordinate")] public double Z { get { return this.z; } set { this.z = value; } } public Point3D() { this.x = 0.0; this.y = 0.0; this.z = 0.0; } public Point3D(Point3D point) { this.x = point.X; this.y = point.Y; this.z = point.Z; } public Point3D(double aX, double aY, double aZ) { this.x = aX; this.y = aY; this.z = aZ; } public Point3D(double aValue1, Point3D aPoint1, double aValue2, Point3D aPoint2) { this.x = aValue1 * aPoint1.X + aValue2 * aPoint2.X; this.y = aValue1 * aPoint1.Y + aValue2 * aPoint2.Y; this.z = aValue1 * aPoint1.Z + aValue2 * aPoint2.Z; } public Point3D(double aValue1, Point3D aPoint1, double aValue2, Point3D aPoint2, double aValue3, Point3D aPoint3) { this.x = aValue1 * aPoint1.X + aValue2 * aPoint2.X + aValue3 * aPoint3.X; this.y = aValue1 * aPoint1.Y + aValue2 * aPoint2.Y + aValue3 * aPoint3.Y; this.z = aValue1 * aPoint1.Z + aValue2 * aPoint2.Z + aValue3 * aPoint3.Z; } public Point3D(double aValue1, Point3D aPoint1, double aValue2, Point3D aPoint2, double aValue3, Point3D aPoint3, double aValue4, Point3D aPoint4) { this.x = aValue1 * aPoint1.X + aValue2 * aPoint2.X + aValue3 * aPoint3.X + aValue4 * aPoint4.X; this.y = aValue1 * aPoint1.Y + aValue2 * aPoint2.Y + aValue3 * aPoint3.Y + aValue4 * aPoint4.Y; this.z = aValue1 * aPoint1.Z + aValue2 * aPoint2.Z + aValue3 * aPoint3.Z + aValue4 * aPoint4.Z; } public static Point3D operator +(Point3D aPoint1, Point3D aPoint2) { return new Point3D(aPoint1.X + aPoint2.x, aPoint1.Y + aPoint2.y, aPoint1.Z + aPoint2.Z); } public static Point3D operator -(Point3D aPoint1, Point3D aPoint2) { return new Point3D(aPoint1.X - aPoint2.x, aPoint1.Y - aPoint2.y, aPoint1.Z - aPoint2.Z); } public static Point3D operator *(double scale, Point3D point) { return point * scale; } public static Point3D operator *(Point3D point, double scale) { return new Point3D(point.x * scale, point.y * scale, point.z * scale); } public static Point3D operator /(double scale, Point3D point) { return point / scale; } public static Point3D operator /(Point3D point, double scale) { return point * (1.0 / scale); } public bool Compare(Point3D aPoint, double aDistance) { return Math.Pow(this.x - aPoint.X, 2.0) + Math.Pow(this.y - aPoint.Y, 2.0) + Math.Pow(this.z - aPoint.Z, 2.0) <= Math.Pow(aDistance, 2.0); } public void Init(double aX, double aY, double aZ) { this.x = aX; this.y = aY; this.z = aZ; } public void Init(Point3D aPoint) { this.x = aPoint.X; this.y = aPoint.Y; this.z = aPoint.Z; } public void Add(Point3D aPoint) { this.x += aPoint.X; this.y += aPoint.Y; this.z += aPoint.Z; } public void Mul(double aValue) { this.x *= aValue; this.y *= aValue; this.z *= aValue; } public bool FromString(string aString, params char[] aSeperator) { string[] strArray = aString.Split(aSeperator); if (strArray.Length != 3) return false; this.x = Convert.ToDouble(strArray[0]); this.y = Convert.ToDouble(strArray[1]); this.z = Convert.ToDouble(strArray[2]); return true; } public Point2D GetPointXZ() { return new Point2D(this.X, this.Z); } public double LengthSquared() { return this.DotProduct(this); } public double Length() { return Math.Sqrt(this.LengthSquared()); } public double DotProduct(Point3D p) { return this.X * p.X + this.Y * p.Y + this.Z * p.Z; } public Point3D CrossProduct(Point3D p) { return new Point3D(this.Y * p.Z - this.Z * p.Y, this.Z * p.X - this.X * p.Z, this.X * p.Y - this.Y * p.X); } public override bool Equals(object aObject) { Point3D point3D = aObject as Point3D; if (point3D == null || Math.Abs(this.x - point3D.x) >= 0.001 || Math.Abs(this.y - point3D.y) >= 0.001) return false; return Math.Abs(this.z - point3D.z) < 0.001; } public override object ConvertTo(ITypeDescriptorContext aContext, CultureInfo aCulture, object aValue, Type aDestinationType) { return (object)""; } public override string ToString() { return this.X.ToString("F2") + " " + this.Y.ToString("F2") + " " + this.Z.ToString("F2"); } public override int GetHashCode() { return base.GetHashCode(); } public int CompareTo(object obj) { return this.X.CompareTo(((Point3D)obj).X); } } }