public bool Equals(Vector3d obj) { return obj.x == this.x && obj.y == this.y && obj.z == this.z; }
/// <summary> /// Obtains the square distance between two points. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="v">Vector3d.</param> /// <returns>Square distance.</returns> public static double SquareDistance(Vector3d u, Vector3d v) { return (u.X - v.X)*(u.X - v.X) + (u.Y - v.Y)*(u.Y - v.Y) + (u.Z - v.Z)*(u.Z - v.Z); }
/// </summary> /// Check if the components of two vectors are approximate equals. /// <param name="obj">Vector3d.</param> /// <param name="threshold">Maximun tolerance.</param> /// <returns>True if the three components are almost equal or false in anyother case.</returns> public bool Equals(Vector3d obj, double threshold) { if (Math.Abs(obj.X - this.x) > threshold) { return false; } if (Math.Abs(obj.Y - this.y) > threshold) { return false; } if (Math.Abs(obj.Z - this.z) > threshold) { return false; } return true; }
/// <summary> /// Obtains the midpoint. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="v">Vector3d.</param> /// <returns>Vector3d.</returns> public static Vector3d MidPoint(Vector3d u, Vector3d v) { return new Vector3d((v.X + u.X)*0.5F, (v.Y + u.Y)*0.5F, (v.Z + u.Z)*0.5F); }
/// <summary> /// Rounds the components of a vector. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="numDigits">Number of significative defcimal digits.</param> /// <returns>Vector3d.</returns> public static Vector3d Round(Vector3d u, int numDigits) { return new Vector3d((Math.Round(u.X, numDigits)), (Math.Round(u.Y, numDigits)), (Math.Round(u.Z, numDigits))); }
/// <summary> /// Obtains the distance between two points. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="v">Vector3d.</param> /// <returns>Distancie.</returns> public static double Distance(Vector3d u, Vector3d v) { return (Math.Sqrt((u.X - v.X)*(u.X - v.X) + (u.Y - v.Y)*(u.Y - v.Y) + (u.Z - v.Z)*(u.Z - v.Z))); }
/// <summary> /// Obtains the dot product of two vectors. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="v">Vector3d.</param> /// <returns>The dot product.</returns> public static double DotProduct(Vector3d u, Vector3d v) { return (u.X*v.X) + (u.Y*v.Y) + (u.Z*v.Z); }
/// <summary> /// Checks if two vectors are perpendicular. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="v">Vector3d.</param> /// <param name="threshold">Tolerance used.</param> /// <returns>True if are penpendicular or false in anyother case.</returns> public static bool ArePerpendicular(Vector3d u, Vector3d v, double threshold) { return MathHelper.IsZero(DotProduct(u, v), threshold); }
/// <summary> /// Obtains the cross product of two vectors. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="v">Vector3d.</param> /// <returns>Vector3d.</returns> public static Vector3d CrossProduct(Vector3d u, Vector3d v) { double a = u.Y*v.Z - u.Z*v.Y; double b = u.Z*v.X - u.X*v.Z; double c = u.X*v.Y - u.Y*v.X; return new Vector3d(a, b, c); }
/// <summary> /// Checks if two vectors are parallel. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="v">Vector3d.</param> /// <param name="threshold">Tolerance used.</param> /// <returns>True if are parallel or false in anyother case.</returns> public static bool AreParallel(Vector3d u, Vector3d v, double threshold) { double a = u.Y*v.Z - u.Z*v.Y; double b = u.Z*v.X - u.X*v.Z; double c = u.X*v.Y - u.Y*v.X; if (! MathHelper.IsZero(a, threshold)) { return false; } if (!MathHelper.IsZero(b, threshold)) { return false; } if (!MathHelper.IsZero(c, threshold)) { return false; } return true; }
/// <summary> /// Obtains the angle between two vectors. /// </summary> /// <param name="u">Vector3d.</param> /// <param name="v">Vector3d.</param> /// <returns>Angle in radians.</returns> public static double AngleBetween(Vector3d u, Vector3d v) { double cos = DotProduct(u, v)/(u.Modulus()*v.Modulus()); if (MathHelper.IsOne(cos)) { return 0; } if (MathHelper.IsOne(-cos)) { return Math.PI; } return Math.Acos(cos); }
/// <summary> /// Gets the rotation matrix from the normal vector (extrusion direction) of an entity. /// </summary> /// <param name="zAxis">Normal vector.</param> /// <returns>Rotation matriz.</returns> public static Matrix3d ArbitraryAxis(Vector3d zAxis) { zAxis.Normalize(); Vector3d wY = Vector3d.UnitY; Vector3d wZ = Vector3d.UnitZ; Vector3d aX; if ((Math.Abs(zAxis.X) < 1/64.0) && (Math.Abs(zAxis.Y) < 1/64.0)) aX = Vector3d.CrossProduct(wY, zAxis); else aX = Vector3d.CrossProduct(wZ, zAxis); aX.Normalize(); Vector3d aY = Vector3d.CrossProduct(zAxis, aX); aY.Normalize(); return new Matrix3d(aX.X, aY.X, zAxis.X, aX.Y, aY.Y, zAxis.Y, aX.Z, aY.Z, zAxis.Z); }
/// <summary> /// Transforms a point list between coordinate systems. /// </summary> /// <param name="points">Points to transform.</param> /// <param name="zAxis">Object normal vector.</param> /// <param name="from">Points coordinate system.</param> /// <param name="to">Coordinate system of the transformed points.</param> /// <returns>Transormed point list.</returns> public static IList<Vector3d> Transform(IList<Vector3d> points, Vector3d zAxis, CoordinateSystem from, CoordinateSystem to) { Matrix3d trans = ArbitraryAxis(zAxis); List<Vector3d> transPoints; if (from == CoordinateSystem.World && to == CoordinateSystem.Object) { transPoints = new List<Vector3d>(); trans = trans.Traspose(); foreach (Vector3d p in points) { transPoints.Add(trans*p); } return transPoints; } if (from == CoordinateSystem.Object && to == CoordinateSystem.World) { transPoints = new List<Vector3d>(); foreach (Vector3d p in points) { transPoints.Add(trans*p); } return transPoints; } return points; }
/// <summary> /// Transforms a point between coordinate systems. /// </summary> /// <param name="point">Point to transform.</param> /// <param name="zAxis">Object normal vector.</param> /// <param name="from">Point coordinate system.</param> /// <param name="to">Coordinate system of the transformed point.</param> /// <returns>Transormed point.</returns> public static Vector3d Transform(Vector3d point, Vector3d zAxis, CoordinateSystem from, CoordinateSystem to) { Matrix3d trans = ArbitraryAxis(zAxis); if (from == CoordinateSystem.World && to == CoordinateSystem.Object) { trans = trans.Traspose(); return trans*point; } if (from == CoordinateSystem.Object && to == CoordinateSystem.World) { return trans*point; } return point; }