/// <summary> /// constructor, /// get a user coordinate system /// </summary> /// <param name="origin">origin of user coordinate system</param> /// <param name="xAxis">xAxis of user coordinate system</param> /// <param name="yAxis">yAxis of user coordinate system</param> /// <param name="flag"></param> public UCS(Vector origin, Vector xAxis, Vector yAxis, bool flag) { Vector x2 = xAxis / ~xAxis; Vector y2 = yAxis / ~yAxis; Vector z2 = x2 & y2; if (~z2 < double.Epsilon) { throw new InvalidOperationException(); } if (!flag) { z2 = -z2; } m_origin = origin; m_xAxis = x2; m_yAxis = y2; m_zAxis = z2; }
/// <summary> /// static constructor used to initialize static members /// </summary> static Graphics3DData() { // initialize small angle double angle = RotateAngle; double antiAngle = -RotateAngle; double sin = Math.Sin(angle); double cos = Math.Cos(angle); double antiSin = Math.Sin(antiAngle); double antiCos = Math.Cos(antiAngle); // initialize 6 Rotate UCS Vector origin = new Vector(); RotateXUCS = new UCS(origin, new Vector(1.0, 0.0, 0.0), new Vector(0.0, cos, sin)); RotateAntiXUCS = new UCS(origin, new Vector(1.0, 0.0, 0.0), new Vector(0.0, antiCos, antiSin)); RotateYUCS = new UCS(origin, new Vector(cos, 0.0, -sin), new Vector(0.0, 1.0, 0.0)); RotateAntiYUCS = new UCS(origin, new Vector(antiCos, 0.0, -antiSin), new Vector(0.0, 1.0, 0.0)); RotateZUCS = new UCS(origin, new Vector(cos, sin, 0.0), new Vector(-sin, cos, 0.0)); RotateAntiZUCS = new UCS(origin, new Vector(antiCos, antiSin, 0.0), new Vector(-antiSin, antiCos, 0.0)); // initialize 7 special UCS SpecialUCSs[(int)ViewDirections.IsoMetric] = new UCS(origin, new Vector(-0.408248290463863, 0.408248290463863, 0.816496580927726), new Vector(0.707106781186548, 0.707106781186548, 0.0)); SpecialUCSs[(int)ViewDirections.Top] = new UCS(origin, new Vector(1.0, 0.0, 0.0), new Vector(0.0, 1.0, 0.0)); SpecialUCSs[(int)ViewDirections.Front] = new UCS(origin, new Vector(-1.0, 0.0, 0.0), new Vector(0.0, 0.0, 1.0)); SpecialUCSs[(int)ViewDirections.Left] = new UCS(origin, new Vector(0.0, -1.0, 0.0), new Vector(0.0, 0.0, 1.0)); SpecialUCSs[(int)ViewDirections.Right] = new UCS(origin, new Vector(0.0, 1.0, 0.0), new Vector(0.0, 0.0, 1.0)); SpecialUCSs[(int)ViewDirections.Bottom] = new UCS(origin, new Vector(-1.0, 0.0, 0.0), new Vector(0.0, 1.0, 0.0)); SpecialUCSs[(int)ViewDirections.Back] = new UCS(origin, new Vector(1.0, 0.0, 0.0), new Vector(0.0, 0.0, 1.0)); }
private double m_z; //z coordinate of vector #endregion Fields #region Constructors /// <summary> /// copy constructor /// </summary> public Vector(Vector rhs) { m_x = rhs.X; m_y = rhs.Y; m_z = rhs.Z; }
/// <summary> /// estimate whether two vector are equal /// </summary> /// <param name="lhs">first vector</param> /// <param name="rhs">second vector</param> /// <returns> whether two are equal</returns> private static bool IsEqual(Vector lhs, Vector rhs) { if (lhs.X == rhs.X && lhs.X == rhs.X && lhs.X == rhs.X) { return true; } else { return false; } }
/// <summary> /// get Normal by vector /// </summary> public Vector GetNormal() { Vector direct = new Vector(); double len = GetLength(); if (len < double.Epsilon) { return new Vector(); } direct.X = m_x / len; direct.Y = m_y / len; direct.Z = m_z / len; return direct; }
/// <summary> /// Cross multiply 2 3*3 Matrix /// </summary> /// <param name="m1"></param> /// <param name="m2"></param> /// <returns>result 3*3 Matrix</returns> public static Vector[] MultiCross3X3Matrix(Vector[] m1, Vector[] m2) { Vector[] result = new Vector[3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { switch (j) { case 0: (result[i]).X += (m1[i])[k] * (m2[k])[j]; break; case 1: (result[i]).Y += (m1[i])[k] * (m2[k])[j]; break; case 2: (result[i]).Z += (m1[i])[k] * (m2[k])[j]; break; default: break; } } } } return result; }
/// <summary> /// get angle of two vector /// </summary> /// <param name="lhs">first vector</param> /// <param name="rhs">second vector</param> /// <param name="acuteAngleDesired"> /// indicate whether get the acute angle of two angles between two vectors /// </param> /// <returns> angle of two vector</returns> public static double GetAngleOf2Vectors(Vector lhs, Vector rhs, bool acuteAngleDesired) { double angle = Math.Acos(lhs.GetNormal() * rhs.GetNormal()); if (acuteAngleDesired && angle > Math.PI / 2) { angle = Math.PI - angle; } return angle; }
/// <summary> /// negative of vector /// </summary> /// <param name="lhs">vector</param> /// <returns>negative of vector</returns> public static Vector operator -(Vector lhs) { Vector result = new Vector(lhs); result.X = -lhs.X; result.Y = -lhs.Y; result.Z = -lhs.Z; return result; }
/// <summary> /// subtraction of two vector /// </summary> /// <param name="lhs">first vector</param> /// <param name="rhs">second vector</param> /// <returns>subtraction of two vector</returns> public static Vector operator -(Vector lhs, Vector rhs) { Vector result = new Vector(lhs); result.X -= rhs.X; result.Y -= rhs.Y; result.Z -= rhs.Z; return result; }
/// <summary> /// add two vector /// </summary> /// <param name="lhs">first vector</param> /// <param name="rhs">second vector</param> /// <returns>add two vector</returns> public static Vector operator +(Vector lhs, Vector rhs) { Vector result = new Vector(lhs); result.X += rhs.X; result.Y += rhs.Y; result.Z += rhs.Z; return result; }
/// <summary> /// transform a Vector instance to a Autodesk.Revit.DB.XYZ instance /// </summary> /// <param name="v">transformed Vector</param> /// <returns>result XYZ</returns> public static Autodesk.Revit.DB.XYZ Vector2XYZ(Vector v) { return new Autodesk.Revit.DB.XYZ (v.X, v.Y, v.Z); }
/// <summary> /// The default constructor, /// </summary> public UCS(Vector origin, Vector xAxis, Vector yAxis) : this(origin, xAxis, yAxis, true) { }
/// <summary> /// Transform local coordinate to global coordinate /// </summary> /// <param name="arg">a vector which need to transform</param> public Vector LC2GC(Vector arg) { Vector result = new Vector(); result.X = arg.X * m_xAxis.X + arg.Y * m_yAxis.X + arg.Z * m_zAxis.X + m_origin.X; result.Y = arg.X * m_xAxis.Y + arg.Y * m_yAxis.Y + arg.Z * m_zAxis.Y + m_origin.Y; result.Z = arg.X * m_xAxis.Z + arg.Y * m_yAxis.Z + arg.Z * m_zAxis.Z + m_origin.Z; return result; }
/// <summary> /// Transform global coordinate to local coordinate /// </summary> /// <param name="arg">a vector which need to transform</param> public Vector GC2LC(Vector arg) { Vector result = new Vector(); arg = arg - m_origin; result.X = m_xAxis * arg; result.Y = m_yAxis * arg; result.Z = m_zAxis * arg; return result; }
/// <summary> /// add 2 UCS /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static UCS operator +(UCS lhs, UCS rhs) { Vector origin = lhs.Origin + rhs.Origin; Vector[] left = new Vector[3]; Vector[] right = new Vector[3]; for (int i = 0; i < 3; i++) { left[i] = lhs[i]; right[i] = rhs[i]; } Vector[] basis = Vector.MultiCross3X3Matrix(left, right); return new UCS(origin, basis[0], basis[1]); }