/// <summary> /// Initializes coordinate system using origin point and two double arrays. /// </summary> /// <param name="p">Origin of the coordinate system.</param> /// <param name="d1">Vector oriented along the X axis.</param> /// <param name="d2">Vector in the XY plane.</param> /// <param name="name">Name of the coordinate system.</param> public Coord3d(Point3d p, double[] d1, double[] d2, string name = "") { Vector3d v1 = new Vector3d(d1); Vector3d v2 = new Vector3d(d2); if (v1.IsParallelTo(v2)) { throw new Exception("Vectors are parallel"); } v1 = v1.Normalized; Vector3d v3 = v1.Cross(v2).Normalized; v2 = v3.Cross(v1).Normalized; _origin = p.ConvertToGlobal(); _axes = new Matrix3d(v1, v2, v3); if ((!string.IsNullOrEmpty(name))) { _name = name; } else { _name = "Coord " + count.ToString(); } count += 1; }
/// <summary> /// Initializes coordinate system using three points. /// </summary> /// <param name="p1">Origin of the coordinate system.</param> /// <param name="p2">Point on the X axis.</param> /// <param name="p3">Point on the XY plane.</param> /// <param name="name">Name of the coordinate system.</param> public Coord3d(Point3d p1, Point3d p2, Point3d p3, string name = "") { Vector3d v1 = new Vector3d(p1, p2); Vector3d v2 = new Vector3d(p1, p3); if (v1.IsParallelTo(v2)) { throw new Exception("Points are collinear"); } v1 = v1.ConvertToGlobal().Normalized; Vector3d v3 = v1.Cross(v2).Normalized; v2 = v3.Cross(v1).Normalized; _origin = p1.ConvertToGlobal(); _axes = new Matrix3d(v1, v2, v3); if ((!string.IsNullOrEmpty(name))) { _name = name; } else { _name = "Coord " + count.ToString(); } count += 1; }
/// <summary> /// Point on axis aligned box (including interior points) closest to target point "p". /// </summary> public Point3d AABBClosestPoint(Point3d p) { p = p.ConvertToGlobal(); double x = GeometRi3D.Clamp(p.X, this._center.X - _lx / 2, this._center.X + _lx / 2); double y = GeometRi3D.Clamp(p.Y, this._center.Y - _ly / 2, this._center.Y + _ly / 2); double z = GeometRi3D.Clamp(p.Z, this._center.Z - _lz / 2, this._center.Z + _lz / 2); return(new Point3d(x, y, z)); }
/// <summary> /// Convert point to reference coordinate system /// </summary> public Point3d ConvertTo(Coord3d coord) { Point3d p = this.Copy(); p = p.ConvertToGlobal(); if (coord == null || object.ReferenceEquals(coord, Coord3d.GlobalCS)) { return(p); } p = coord.Axes * (p - coord.Origin); p._coord = coord; return(p); }
/// <summary> /// Initializes coordinate system using origin point and transformation matrix. /// </summary> /// <param name="p">Origin of the coordinate system.</param> /// <param name="m">Transformation matrix (in row format).</param> /// <param name="name">Name of the coordinate system.</param> public Coord3d(Point3d p, Matrix3d m, string name = "") { if (!m.IsOrthogonal) { throw new ArgumentException("The matrix is not orthogonal"); } _origin = p.ConvertToGlobal(); _axes = m.Copy(); if ((!string.IsNullOrEmpty(name))) { _name = name; } else { _name = "Coord " + count.ToString(); } count += 1; }