public static Segment3?CropBy(this Segment3 @this, Plane plane) { var signedDist = @this.Start.SignedDistanceTo(plane); var t = -signedDist / (plane.Normal * @this.Vector); if (t > 0 && t < 1) // plane intersects @this { if (signedDist > 0) { return(new Segment3(@this.PointAt(t), @this.End)); } else { return(new Segment3(@this.Start, @this.PointAt(t))); } } else // plane doesn't intersect @this { if (signedDist > 0) { return(null); } else { return(@this); } } }
public void IntersectionWithPlaneXY_of_Segment3_with_endpoints_on_the_same_side_of_plane_XY_is_null() { var point1 = new Vector3(-3.141, 2.718, 0.577); var point2 = new Vector3(0.256, -1.234, 0.676); var intersection = new Segment3(point1, point2).IntersectionWithPlaneXY(); Expect(intersection, Is.Null); }
public void Cropping_Segment3_by_plane_produces_null_when_entire_segment_is_on_negative_side() { var segment = new Segment3(new Vector3(3.14, 2.88, -1.77), new Vector3(0.52, -5.08, 1.23)); var point = segment.PointAt(1.256); var plane = new Plane(point, normal: new Vector3(0.66, 1.44, -0.87)); var crop = segment.CropBy(plane); Expect(crop == null); }
public void Distance_of_point_to_vertical_segment_intersecting_points_height_level_equals_distance_of_point_to_intersection_point() { var p = new Vector3(3.141, 2.718, 0.577); var displacement = new Vector2(0.123, 0.256); var q0 = p + new Vector3(displacement, 0.482); var q1 = p + new Vector3(displacement, -1.234); var segment = new Segment3(q0, q1); Expect(p.DistanceTo(segment), Is.EqualTo(displacement.Norm).Within(_tolerance)); }
public void Distance_of_point_to_vertical_segment_below_points_height_level_equals_distance_of_point_to_higher_segment_end() { var p = new Vector3(3.141, 2.718, 0.577); var displacement = new Vector2(0.123, 0.256); var q0 = p + new Vector3(displacement, -0.482); var q1 = p + new Vector3(displacement, -1.234); var segment = new Segment3(q0, q1); Expect(p.DistanceTo(segment), Is.EqualTo(Vector3.Distance(p, q0)).Within(_tolerance)); }
public void Cropping_Segment3_by_plane_produces_original_segment_when_entire_segment_is_on_positive_side() { var segment = new Segment3(new Vector3(3.14, 2.88, -1.77), new Vector3(0.52, -5.08, 1.23)); var point = segment.PointAt(-0.256); var plane = new Plane(point, normal: new Vector3(0.66, 1.44, -0.87)); var crop = segment.CropBy(plane); Expect(crop != null); Expect(Vector3.Distance(crop.Value.Start, segment.Start), Is.LessThan(_tolerance)); Expect(Vector3.Distance(crop.Value.End, segment.End), Is.LessThan(_tolerance)); }
public void IntersectionWithPlaneXY_of_Segment3_with_endpoints_collinear_with_a_point_on_plane_XY_on_different_sides_of_the_plane_equals_to_the_point() { var pointXY = new Vector3(3.141, 2.718, 0); var delta = new Vector3(0.256, 0.777, 0.577); var point1 = pointXY + 0.124 * delta; var point2 = pointXY - 0.369 * delta; var intersection = new Segment3(point1, point2).IntersectionWithPlaneXY(); Expect(intersection, Is.Not.Null); Expect(Vector2.Distance(intersection.Value, pointXY.XY), Is.LessThan(_tolerance)); }
public static double Distance2(Vector3 point, Segment3 segment) { var vector = segment.Vector; var toStart = segment.Start - point; var toEnd = segment.End - point; if ((toStart * vector) * (toEnd * vector) > 0) { return(Math.Min(toStart.Norm2, toEnd.Norm2)); } else { return(toStart.Cross(toEnd).Norm2 / vector.Norm2); } }
public static Vector2?IntersectionWithPlaneXY(this Segment3 @this) { if (@this.Start.Z * @this.End.Z > 0) { return(null); } else { var deltaZ = @this.End.Z - @this.Start.Z; if (Math.Abs(deltaZ) < BasicMath.Epsilon) { return((@this.Start.XY + @this.End.XY) / 2); } return((@this.End.Z * @this.Start.XY - @this.Start.Z * @this.End.XY) / deltaZ); } }
public static Vector2?IntersectionWithPlaneZX(this Segment3 @this) { if (@this.Start.Y * @this.End.Y > 0) { return(null); } else { var deltaY = @this.End.Y - @this.Start.Y; if (Math.Abs(deltaY) < BasicMath.Epsilon) { return((@this.Start.ZX + @this.End.ZX) / 2); } return((@this.End.Y * @this.Start.ZX - @this.Start.Y * @this.End.ZX) / deltaY); } }
public static Vector2?IntersectionWithPlaneYZ(this Segment3 @this) { if (@this.Start.X * @this.End.X > 0) { return(null); } else { var deltaX = @this.End.X - @this.Start.X; if (Math.Abs(deltaX) < BasicMath.Epsilon) { return((@this.Start.YZ + @this.End.YZ) / 2); } return((@this.End.X * @this.Start.YZ - @this.Start.X * @this.End.YZ) / deltaX); } }
public static double DistanceTo(this Vector3 @this, Segment3 segment) { return(Math.Sqrt(Distance2(@this, segment))); }
public static Segment2 ProjectToXY(this Segment3 @this) { return(new Segment2(@this.Start.XY, @this.End.XY)); }
public static Segment3 ApplyInv(this RotoTranslation3 @this, Segment3 segment) { return(new Segment3(@this.ApplyInv(segment.Start), @this.ApplyInv(segment.End))); }