Here is the full solution.
// List curve2Ds consists of LineSegment2d & CircularArc2d, NOTHING ELSE!
public void FindIntersections(List<Curve2d> curve2Ds)
{
var intersections = new Dictionary<int, Point2dCollection>();
for (int i =0; i < curve2Ds.Count -1; i++)
{
var curve_i = curve2Ds[i];
if (!intersections.ContainsKey(i))
{
intersections.Add(i, new Point2dCollection());
}
for (int j = i+1; j < curve2Ds.Count; j++)
{
var curve_j = curve2Ds[j];
if (!intersections.ContainsKey(j))
{
intersections.Add(j, new Point2dCollection());
}
if (!curve_i.BoundBlock.IsDisjoint(curve_j.BoundBlock))
{
var curveCurveIntersector = new CurveCurveIntersector2d(curve_i, curve_j);
if (curveCurveIntersector.NumberOfIntersectionPoints > 1)
{
for (int k = 0; k < curveCurveIntersector.NumberOfIntersectionPoints; k++)
{
Func<PointModel> fun = delegate ()
{
return new PointModel(curveCurveIntersector.GetIntersectionPoint(k), Storage.storage.Points.GetUnusedId());
};
var p = (PointModel)Storage.storage.Points.GetOrAdd(curveCurveIntersector.GetIntersectionPoint(k), fun);
p.SetIdMessage("ERROR: " + k);
return;
}
}
else if (curveCurveIntersector.NumberOfIntersectionPoints > 0)
{
var intersectionPoint = curveCurveIntersector.GetIntersectionPoint(0);
if (!curve_i.StartPoint.Equals(intersectionPoint) && !curve_i.EndPoint.Equals(intersectionPoint))
{
var param = curve_i.GetParameterOf(intersectionPoint);
if (!intersections[i].Contains(intersectionPoint))
{
intersections[i].Add(intersectionPoint);
}
}
if (!curve_j.StartPoint.Equals(intersectionPoint) && !curve_j.EndPoint.Equals(intersectionPoint))
{
var param = curve_j.GetParameterOf(intersectionPoint);
if (!intersections[j].Contains(intersectionPoint))
{
intersections[j].Add(intersectionPoint);
}
}
}
}
}
}
// Split all curves at intersections and add them to a new list
var curves = new List<Curve2d>();
PointModel startPointModel = null;
PointModel endPointModel = null;
for (int i = 0; i < curve2Ds.Count; i++)
{
var curve = curve2Ds[i];
var intersects = intersections[i].ToArray();
var sortedIntersects = intersects.OrderBy(p => curve.GetParameterOf(p));
Point2d point = new Point2d();
Func<PointModel> func = delegate ()
{
return new PointModel(point, Storage.storage.Points.GetUnusedId());
};
ed.WriteMessage("\n Intersections for {0} are {1}", i, intersections[i].Count);
foreach (var p in sortedIntersects)
{
var param = curve.GetParameterOf(p);
var splitCurves = curve.GetSplitCurves(param);
ed.WriteMessage("\n Double d = {0}, splitcount = {1}", param, splitCurves.Length); ;
curves.Add(splitCurves[0]);
point = splitCurves[0].StartPoint;
startPointModel = (PointModel)Storage.storage.Points.GetOrAdd(point, func);
point = splitCurves[0].EndPoint;
endPointModel = (PointModel)Storage.storage.Points.GetOrAdd(point, func);
startPointModel.AddConnection(endPointModel, splitCurves[0]);
endPointModel.AddConnection(startPointModel, splitCurves[0]);
curve = splitCurves[1];
};
// Add the last part of the line, or if no intersections found the whole line.
point = curve.StartPoint;
startPointModel = (PointModel)Storage.storage.Points.GetOrAdd(point, func);
point = curve.EndPoint;
endPointModel = (PointModel)Storage.storage.Points.GetOrAdd(point, func);
startPointModel.AddConnection(endPointModel, curve);
endPointModel.AddConnection(startPointModel, curve);
curves.Add(curve);
}
ed.WriteMessage("\n From {0} curves to {1}", curve2Ds.Count,curves.Count);
}