/***************************************************/ private static PolyCurve Fillet(this ICurve curve1, ICurve curve2, bool tangentExtensions, bool keepCurve1StartPoint, bool keepCurve2StartPoint, double tolerance = Tolerance.Distance) { //TODO: //Write a proper fillet method, test and make it public if (!((curve1 is Line || curve1 is Arc) && (curve2 is Line || curve2 is Arc))) //for now works only with combinations of lines and arcs { Reflection.Compute.RecordError("Private method fillet is implemented only for PolyCurves consisting of Lines or Arcs."); return(null); } List <PolyCurve> joinCurves = Compute.IJoin(new List <ICurve> { curve1, curve2 }, tolerance).ToList(); if (joinCurves.Count == 1) { return(joinCurves[0]); } List <ICurve> resultCurves = new List <ICurve>(); bool C1SP = keepCurve1StartPoint; bool C2SP = keepCurve2StartPoint; List <Point> intersections = curve1.ICurveIntersections(curve2, tolerance); if (intersections.Count > 2) { Reflection.Compute.RecordError("Invalid number of intersections between curves. Two lines/arcs can have no more than two intersections."); } else if (intersections.Count == 2 || intersections.Count == 1) { Point intersection = intersections[0]; if (intersections.Count == 2) { double maxdist = double.MaxValue; if (C1SP) { foreach (Point p in intersections) { double dist = p.SquareDistance(curve1.IStartPoint()); if (dist < maxdist) { intersection = p; maxdist = dist; } } } else { foreach (Point p in intersections) { double dist = p.SquareDistance(curve1.IEndPoint()); if (dist < maxdist) { intersection = p; maxdist = dist; } } } } else { intersection = intersections[0]; } if (C1SP && C2SP) { resultCurves.AddRange(curve1.ExtendToPoint(curve1.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(curve2.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves[1] = resultCurves[1].IFlip(); } else if (C1SP && !C2SP) { resultCurves.AddRange(curve1.ExtendToPoint(curve1.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(intersection, curve2.IEndPoint(), tangentExtensions, tolerance)); } else if (!C1SP && C2SP) { resultCurves.AddRange(curve1.ExtendToPoint(intersection, curve1.IEndPoint(), tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(curve2.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves[0] = resultCurves[0].IFlip(); resultCurves[1] = resultCurves[1].IFlip(); } else { resultCurves.AddRange(curve1.ExtendToPoint(intersection, curve1.IEndPoint(), tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(intersection, curve2.IEndPoint(), tangentExtensions, tolerance)); resultCurves[0] = resultCurves[0].IFlip(); } } else { if (curve1 is Line && curve2 is Line) { Point intersection = (curve1 as Line).LineIntersection(curve2 as Line, true, tolerance); if (C1SP && C2SP) { if ((curve1.IStartPoint().Distance((curve2 as Line), true) < curve1.IEndPoint().Distance((curve2 as Line), true) && !intersection.IIsOnCurve(curve1)) || (curve2.IStartPoint().Distance((curve1 as Line), true) < curve2.IEndPoint().Distance((curve1 as Line), true) && !intersection.IIsOnCurve(curve2))) { Reflection.Compute.RecordWarning("Couldn't provide correct fillet for given input"); return(null); } resultCurves.AddRange(curve1.ExtendToPoint(curve1.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(curve2.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves[1] = resultCurves[1].IFlip(); } else if (C1SP && !C2SP) { if ((curve1.IStartPoint().Distance((curve2 as Line), true) < curve1.IEndPoint().Distance((curve2 as Line), true) && !intersection.IIsOnCurve(curve1)) || (curve2.IStartPoint().Distance((curve1 as Line), true) > curve2.IEndPoint().Distance((curve1 as Line), true) && !intersection.IIsOnCurve(curve2))) { Reflection.Compute.RecordWarning("Couldn't provide correct fillet for given input"); return(null); } resultCurves.AddRange(curve1.ExtendToPoint(curve1.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(intersection, curve2.IEndPoint(), tangentExtensions, tolerance)); } else if (!C1SP && C2SP) { if ((curve1.IStartPoint().Distance((curve2 as Line), true) > curve1.IEndPoint().Distance((curve2 as Line), true) && !intersection.IIsOnCurve(curve1)) || (curve2.IStartPoint().Distance((curve1 as Line), true) < curve2.IEndPoint().Distance((curve1 as Line), true) && !intersection.IIsOnCurve(curve2))) { Reflection.Compute.RecordWarning("Couldn't provide correct fillet for given input"); return(null); } resultCurves.AddRange(curve1.ExtendToPoint(intersection, curve1.IEndPoint(), tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(curve2.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves[0] = resultCurves[0].IFlip(); resultCurves[1] = resultCurves[1].IFlip(); } else { if ((curve1.IStartPoint().Distance((curve2 as Line), true) > curve1.IEndPoint().Distance((curve2 as Line), true) && !intersection.IIsOnCurve(curve1)) || (curve2.IStartPoint().Distance((curve1 as Line), true) > curve2.IEndPoint().Distance((curve1 as Line), true) && !intersection.IIsOnCurve(curve2))) { Reflection.Compute.RecordWarning("Couldn't provide correct fillet for given input"); return(null); } resultCurves.AddRange(curve1.ExtendToPoint(intersection, curve1.IEndPoint(), tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(intersection, curve2.IEndPoint(), tangentExtensions, tolerance)); resultCurves[0] = resultCurves[0].IFlip(); } } else { Point intersection; if (!tangentExtensions) { PolyCurve pCurve1 = new PolyCurve(); PolyCurve pCurve2 = new PolyCurve(); if (curve1 is Arc) { pCurve1.Curves.Add(new Circle { Centre = (curve1 as Arc).Centre(), Normal = (curve1 as Arc).Normal(), Radius = (curve1 as Arc).Radius }); } else { pCurve1.Curves.Add(new Line { Start = (curve1 as Line).Start, End = (curve1 as Line).End, Infinite = true }); } if (curve2 is Arc) { pCurve2.Curves.Add(new Circle { Centre = (curve2 as Arc).Centre(), Normal = (curve2 as Arc).Normal(), Radius = (curve2 as Arc).Radius }); } else { pCurve2.Curves.Add(new Line { Start = (curve2 as Line).Start, End = (curve2 as Line).End, Infinite = true }); } List <Point> curveIntersections = pCurve1.CurveIntersections(pCurve2, tolerance); if (curveIntersections.Count == 0) { Reflection.Compute.RecordError("Curves' extensions do not intersect"); return(null); } if (C1SP && C2SP) { intersection = curveIntersections.ClosestPoint(curve1.IEndPoint()); resultCurves.AddRange(curve1.ExtendToPoint(curve1.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(curve2.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves[1] = resultCurves[1].IFlip(); } else if (C1SP && !C2SP) { intersection = curveIntersections.ClosestPoint(curve1.IEndPoint()); resultCurves.AddRange(curve1.ExtendToPoint(curve1.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(intersection, curve2.IEndPoint(), tangentExtensions, tolerance)); } else if (!C1SP && C2SP) { intersection = curveIntersections.ClosestPoint(curve1.IStartPoint()); resultCurves.AddRange(curve1.ExtendToPoint(intersection, curve1.IEndPoint(), tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(curve2.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves[0] = resultCurves[0].IFlip(); resultCurves[1] = resultCurves[1].IFlip(); } else { intersection = curveIntersections.ClosestPoint(curve1.IStartPoint()); resultCurves.AddRange(curve1.ExtendToPoint(intersection, curve1.IEndPoint(), tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(intersection, curve2.IEndPoint(), tangentExtensions, tolerance)); resultCurves[0] = resultCurves[0].IFlip(); } } else { if (C1SP && C2SP) { Line tanLine1 = Create.Line(curve1.IEndPoint(), curve1.IEndDir() / tolerance); tanLine1.Infinite = false; PolyCurve pCurve1 = new PolyCurve { Curves = { curve1, tanLine1 } }; Line tanLine2 = Create.Line(curve2.IEndPoint(), curve2.IEndDir() / tolerance); tanLine2.Infinite = false; PolyCurve pCurve2 = new PolyCurve { Curves = { curve2, tanLine2 } }; List <Point> curveIntersecions = pCurve1.CurveIntersections(pCurve2, tolerance); if (curveIntersecions.Count > 0) { intersection = curveIntersecions.ClosestPoint(curve1.IEndPoint()); } else { Reflection.Compute.RecordWarning("Couldn't create fillet"); return(null); } PolyCurve subResult1 = new PolyCurve { Curves = curve1.ExtendToPoint(curve1.IStartPoint(), intersection, tangentExtensions, tolerance).ToList() }; PolyCurve subResult2 = new PolyCurve { Curves = curve2.ExtendToPoint(curve2.IStartPoint(), intersection, tangentExtensions, tolerance).ToList() }; subResult2 = subResult2.Flip(); resultCurves.AddRange(subResult1.Curves); resultCurves.AddRange(subResult2.Curves); } else if (C1SP && !C2SP) { Line tanLine1 = Create.Line(curve1.IEndPoint(), curve1.IEndDir() / tolerance); tanLine1.Infinite = false; PolyCurve pCurve1 = new PolyCurve { Curves = { curve1, tanLine1 } }; Line tanLine2 = Create.Line(curve2.IStartPoint(), curve2.IStartDir().Reverse() / tolerance); tanLine2.Infinite = false; PolyCurve pCurve2 = new PolyCurve { Curves = { tanLine2, curve2 } }; List <Point> curveIntersecions = pCurve1.ICurveIntersections(pCurve2, tolerance); if (curveIntersecions.Count > 0) { intersection = curveIntersecions.ClosestPoint(curve1.IEndPoint()); } else { Reflection.Compute.RecordWarning("Couldn't create fillet"); return(null); } resultCurves.AddRange(curve1.ExtendToPoint(curve1.IStartPoint(), intersection, tangentExtensions, tolerance)); resultCurves.AddRange(curve2.ExtendToPoint(intersection, curve2.IEndPoint(), tangentExtensions, tolerance)); } else if (!C1SP && C2SP) { Line tanLine1 = Create.Line(curve1.IStartPoint(), curve1.IStartDir().Reverse() / tolerance); tanLine1.Infinite = false; PolyCurve pCurve1 = new PolyCurve { Curves = { tanLine1, curve1 } }; Line tanLine2 = Create.Line(curve2.IEndPoint(), curve2.IEndDir() / tolerance); tanLine2.Infinite = false; PolyCurve pCurve2 = new PolyCurve { Curves = { curve2, tanLine2 } }; List <Point> curveIntersecions = pCurve1.ICurveIntersections(pCurve2, tolerance); if (curveIntersecions.Count > 0) { intersection = curveIntersecions.ClosestPoint(curve1.IStartPoint()); } else { Reflection.Compute.RecordWarning("Couldn't create fillet"); return(null); } PolyCurve subResult1 = new PolyCurve { Curves = curve1.ExtendToPoint(intersection, curve1.IEndPoint(), tangentExtensions, tolerance).ToList() }; PolyCurve subResult2 = new PolyCurve { Curves = curve2.ExtendToPoint(curve2.IStartPoint(), intersection, tangentExtensions, tolerance).ToList() }; subResult1 = subResult1.Flip(); subResult2 = subResult2.Flip(); resultCurves.AddRange(subResult1.Curves); resultCurves.AddRange(subResult2.Curves); } else { Line tanLine1 = Create.Line(curve1.IStartPoint(), curve1.IStartDir().Reverse() / tolerance); tanLine1.Infinite = false; PolyCurve pCurve1 = new PolyCurve { Curves = { tanLine1, curve1 } }; Line tanLine2 = Create.Line(curve2.IStartPoint(), curve2.IStartDir().Reverse() / tolerance); tanLine2.Infinite = false; PolyCurve pCurve2 = new PolyCurve { Curves = { tanLine2, curve2 } }; List <Point> curveIntersecions = pCurve1.ICurveIntersections(pCurve2, tolerance); if (curveIntersecions.Count > 0) { intersection = curveIntersecions.ClosestPoint(curve1.IStartPoint()); } else { Reflection.Compute.RecordWarning("Couldn't create fillet"); return(null); } PolyCurve subResult1 = new PolyCurve { Curves = curve1.ExtendToPoint(intersection, curve1.IEndPoint(), tangentExtensions, tolerance).ToList() }; PolyCurve subResult2 = new PolyCurve { Curves = curve2.ExtendToPoint(intersection, curve2.IEndPoint(), tangentExtensions, tolerance).ToList() }; subResult1 = subResult1.Flip(); resultCurves.AddRange(subResult1.Curves); resultCurves.AddRange(subResult2.Curves); } } } } for (int i = resultCurves.Count - 1; i >= 0; i--) { if (resultCurves[i] is PolyCurve) { PolyCurve temp = (PolyCurve)resultCurves[i]; resultCurves.RemoveAt(i); resultCurves.InsertRange(i, temp.Curves); } } PolyCurve result = new PolyCurve { Curves = resultCurves }; return(result); }