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 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 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)); }