private bool FindNeighbor(List <Polygon> polygons, Claster claster, double minSqrDistance) { var massCenterMinDistance = minSqrDistance * _options.MinDistanceCoeff; foreach (var polygon in polygons) { foreach (var polygonInClaster in claster.Polygons) { if (SqrDistance(polygonInClaster.MassCenter, polygon.MassCenter) <= massCenterMinDistance) { if (polygonInClaster.Paths.First().Points.Any(it => { foreach (var point in polygon.Paths.First().Points) { if (SqrDistance(point, it) < minSqrDistance) { return(true); } } return(false); })) { claster.Polygons.Add(polygon); polygons.Remove(polygon); return(true); } } } } return(false); }
public async Task <List <Polygon> > Generalize(List <Polygon> polygons, double minDistance) { var clasters = new List <Claster>(); while (polygons.Any()) { var claster = new Claster(); claster.Polygons.Add(polygons.Last()); polygons.RemoveAt(polygons.Count - 1); while (FindNeighbor(polygons, claster, minDistance)) { } clasters.Add(claster); } return(_generalizationStrategy.Generalize(clasters, minDistance)); }
private IEnumerable <Polygon> MergeClaster(Claster claster, double minDistance) { if (claster.Polygons.Count == 1) { return(claster.Polygons); } var resultList = new List <Polygon>(); var union = claster.Polygons.First(); claster.Polygons.Remove(union); var count = claster.Polygons.Count; var completedCount = 0; while (claster.Polygons.Any()) { var closest = claster.Polygons.OrderBy(it => _vectorGeometry.DistanceSqr(it, union)).First(); claster.Polygons.Remove(closest); var unionResult = _clipper.Union(union, closest); if (unionResult.Count == 2) { resultList.Add(unionResult[1]); _logger.Log($"Oops, looping happened"); } union = unionResult[0]; _logger.Log($"Merged {++completedCount} from {count} polygons"); } resultList.Add(union); return(resultList); }