/// <summary> /// Get intersection of segment with plane. /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'. /// </summary> public object IntersectionWith(Plane3d s) { object obj = this.ToRay.IntersectionWith(s); if (obj == null) { return(null); } else { if (object.ReferenceEquals(obj.GetType(), typeof(Ray3d))) { return(this); } else { Ray3d r = new Ray3d(this.P2, new Vector3d(this.P2, this.P1)); object obj2 = r.IntersectionWith(s); if (obj2 == null) { return(null); } else { return((Point3d)obj2); } } } }
/// <summary> /// Translate ray by a vector /// </summary> public Ray3d Translate(Vector3d v) { Ray3d l = this.Copy(); l.Point = l.Point.Translate(v); return(l); }
/// <summary> /// Shortest distance between sphere and ray /// </summary> public double DistanceTo(Ray3d r) { if (this.Center.ProjectionTo(r.ToLine).BelongsTo(r)) { return(this.DistanceTo(r.ToLine)); } else { return(this.DistanceTo(r.Point)); } }
/// <summary> /// Get intersection of ray with other ray. /// Returns 'null' (no intersection) or object of type 'Point3d', 'Segment3d' or 'Ray3d'. /// </summary> public object IntersectionWith(Ray3d r) { if (this == r) { return(this); } if (this.Point.BelongsTo(r) && this.IsParallelTo(r)) { if (r.Point.BelongsTo(this)) { // Two rays are collinear and opposite if (this.Point == r.Point) { return(this.Point); } else { return(new Segment3d(this.Point, r.Point)); } } else { return(this); } } if (r.Point.BelongsTo(this) && r.IsParallelTo(this)) { return(r); } object obj = this.ToLine.IntersectionWith(r.ToLine); if (obj == null) { return(null); } else { Point3d p = (Point3d)obj; if (p.BelongsTo(this) && p.BelongsTo(r)) { return(p); } else { return(null); } } }
/// <summary> /// <para>Test if point is located in the epsilon neighborhood of the ray.</para> /// <para>Epsilon neighborhood is defined by a GeometRi3D.Tolerance property.</para> /// </summary> public bool BelongsTo(Ray3d l) { if (this == l.Point) { return(true); } if (this.DistanceTo(l.ToLine) <= GeometRi3D.Tolerance) { Point3d proj = this.ProjectionTo(l.ToLine); if (new Vector3d(l.Point, proj).AngleTo(l.Direction) < PI / 4) { return(true); } } return(false); }
/// <summary> /// Check if point belongs to the ray /// </summary> /// <param name="l"></param> /// <returns>True, if the point belongs to the ray</returns> public bool BelongsTo(Ray3d l) { if (this == l.Point) { return(true); } else { Vector3d v = new Vector3d(l.Point, this); if (l.Direction.Normalized == v.Normalized) { return(true); } else { return(false); } } }
/// <summary> /// Returns shortest distance from segment to ray /// </summary> public double DistanceTo(Ray3d r) { if (this.ToVector.IsParallelTo(r.Direction)) { return(this.ToLine.DistanceTo(r.ToLine)); } if (this.ToLine.PerpendicularTo(r.ToLine).BelongsTo(r) && r.ToLine.PerpendicularTo(this.ToLine).BelongsTo(this)) { return(this.ToLine.DistanceTo(r.ToLine)); } double d1 = double.PositiveInfinity; double d2 = double.PositiveInfinity; double d3 = double.PositiveInfinity; bool flag = false; if (r.Point.ProjectionTo(this.ToLine).BelongsTo(this)) { d1 = r.Point.DistanceTo(this.ToLine); flag = true; } if (this.P1.ProjectionTo(r.ToLine).BelongsTo(r)) { d2 = this.P1.DistanceTo(r.ToLine); flag = true; } if (this.P2.ProjectionTo(r.ToLine).BelongsTo(r)) { d3 = this.P2.DistanceTo(r.ToLine); flag = true; } if (flag) { return(Min(d1, Min(d2, d3))); } return(Min(this.P1.DistanceTo(r.Point), this.P2.DistanceTo(r.Point))); }
/// <summary> /// Returns shortest distance from segment to ray /// </summary> public double DistanceTo(Ray3d r) { Point3d p1 = this.ToLine.PerpendicularTo(r.ToLine); bool b1 = p1.BelongsTo(r); Point3d p2 = r.ToLine.PerpendicularTo(this.ToLine); bool b2 = p2.BelongsTo(this); if (this.ToLine.PerpendicularTo(r.ToLine).BelongsTo(r) && r.ToLine.PerpendicularTo(this.ToLine).BelongsTo(this)) { return(this.ToLine.DistanceTo(r.ToLine)); } double d1 = double.PositiveInfinity; double d2 = double.PositiveInfinity; double d3 = double.PositiveInfinity; bool flag = false; if (r.Point.ProjectionTo(this.ToLine).BelongsTo(this)) { d1 = r.Point.DistanceTo(this.ToLine); flag = true; } if (this.P1.ProjectionTo(r.ToLine).BelongsTo(r)) { d2 = this.P1.DistanceTo(r.ToLine); flag = true; } if (this.P2.ProjectionTo(r.ToLine).BelongsTo(r)) { d3 = this.P2.DistanceTo(r.ToLine); flag = true; } if (flag) { return(Min(d1, Min(d2, d3))); } return(Min(this.P1.DistanceTo(r.Point), this.P2.DistanceTo(r.Point))); }
/// <summary> /// Get intersection of ray with triangle. /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'. /// </summary> public object IntersectionWith(Ray3d r) { // Relative tolerance ================================ if (!GeometRi3D.UseAbsoluteTolerance) { double tol = GeometRi3D.Tolerance; GeometRi3D.Tolerance = tol * Max(AB, Max(BC, AC)); GeometRi3D.UseAbsoluteTolerance = true; object result = this.IntersectionWith(r); GeometRi3D.UseAbsoluteTolerance = false; GeometRi3D.Tolerance = tol; return(result); } //==================================================== object obj = this.IntersectionWith(r.ToLine); if (obj == null) { return(null); } else if (obj.GetType() == typeof(Point3d)) { Point3d p = (Point3d)obj; if (p.BelongsTo(r)) { return(p); } else { return(null); } } else { return(r.IntersectionWith((Segment3d)obj)); } }
/// <summary> /// Determines whether two objects are equal. /// </summary> public override bool Equals(object obj) { if (obj == null || (!object.ReferenceEquals(this.GetType(), obj.GetType()))) { return(false); } Ray3d r = (Ray3d)obj; if (GeometRi3D.UseAbsoluteTolerance) { return(this.Point == r.Point && Abs(this.Direction.Normalized * r.Direction.Normalized - 1) < GeometRi3D.Tolerance); } else { double tol = GeometRi3D.Tolerance; GeometRi3D.Tolerance = tol * this.Point.DistanceTo(this.Point.Coord.Origin); GeometRi3D.UseAbsoluteTolerance = true; bool res1 = this.Point == r.Point; GeometRi3D.UseAbsoluteTolerance = false; GeometRi3D.Tolerance = tol; bool res2 = Abs(this.Direction.Normalized * r.Direction.Normalized - 1) < GeometRi3D.Tolerance; return(res1 && res2); } }
/// <summary> /// Get intersection of ray with box. /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'. /// </summary> public object IntersectionWith(Ray3d r) { return(_line_intersection(r.ToLine, 0.0, double.PositiveInfinity)); }
/// <summary> /// Intersection of circle with ray. /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'. /// </summary> public object IntersectionWith(Ray3d r) { Ellipse e = this.ToEllipse; return(e.IntersectionWith(r)); }
/// <summary> /// Shortest distance between line and ray /// </summary> public double DistanceTo(Ray3d r) { return(r.DistanceTo(this)); }
/// <summary> /// Get intersection of segment with ray. /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'. /// </summary> public object IntersectionWith(Ray3d r) { return(r.IntersectionWith(this)); }