public IEnumerable <Cluster <T> > Clusterize(IEnumerable <Point <T> > points, float groupDistance) { var result = new List <Cluster <T> >(); var processingPoints = points.ToList(); var processedPoints = new HashSet <Point <T> >(); foreach (var processingPoint in processingPoints) { if (processedPoints.Contains(processingPoint)) { continue; } var newGroup = new Cluster <T>(); newGroup.AddPoint(processingPoint); foreach (var point in processingPoints) { if (point == processingPoint || processedPoints.Contains(point)) { continue; } if (Vector2.SqrMagnitude(point.Position - processingPoint.Position) < groupDistance * groupDistance) { newGroup.AddPoint(point); processedPoints.Add(processingPoint); processedPoints.Add(point); } } if (newGroup.Points.Count > 1) { result.Add(newGroup); } } return(result); }
public Partition GetPartition() { LightWeightGraph lwg = (_reassignNodes) ? GetAttackedGraphWithReassignment() : GetAttackedGraph(); //Get our cluster Assignment List <List <int> > componentList = lwg.GetComponents(); //Setup our Clusters List <Cluster> clusterList = new List <Cluster>(); for (int i = 0; i < componentList.Count; i++) { Cluster c = new Cluster(i); foreach (var n in componentList[i]) { c.AddPoint(new ClusteredItem(lwg[n].Label)); } clusterList.Add(c); } String meta = "VAT: \nRemoved Count:" + NumNodesRemoved + "\n" + String.Join(",", _nodeRemovalOrder.GetRange(0, NumNodesRemoved)); return(new Partition(clusterList, g, meta)); }
private void AddToCluster(KMPoint point) { double minDistance = 0; Cluster current = null; if (point == null) { throw new NullReferenceException("point is null"); } foreach (var cluster in _clusters) { double distance = point.GetDistanceTo(cluster.Center); if (current == null || minDistance > distance) { minDistance = distance; current = cluster; } } if (current != null) { current.AddPoint(point); } }
private static HashSet <Cluster> Init(List <Point> list, int k) { HashSet <Cluster> clusters = new HashSet <Cluster>(); HashSet <int> chosenPoint = new HashSet <int>(); Random rand = new Random(); int newPoint = 0; for (int i = 0; i < k; i++) { do { newPoint = rand.Next(0, list.Count); } while (chosenPoint.Contains(newPoint)); chosenPoint.Add(newPoint); var newCluster = new Cluster(); newCluster.AddPoint(list[newPoint]); newCluster.UpdateCentroid(); clusters.Add(newCluster); } int n = 0; foreach (var point in list) { if (point == clusters.FirstOrDefault().Points.FirstOrDefault()) { n = 0; } FindMinDist(point, clusters).AddPoint(point); n++; } return(clusters); }
/// <summary> /// 将ASCDTSubGraph的结果保存到clusters中 /// </summary> private void CreateClusters() { if (m_bLocal == false) { for (int i = 0; i < m_globalSubGraphs.Count; i++) { Cluster cluster = new Cluster(); cluster.SetClusterIndex(i + 1); ASCDTSubGraph subGraph = m_globalSubGraphs[i]; List <ITinNode> tinNodes = subGraph.GetNodeList(); for (int j = 0; j < tinNodes.Count; j++) { IPoint newPoint = new PointClass(); tinNodes[j].QueryAsPoint(newPoint); IZAware pZAware = newPoint as IZAware; pZAware.ZAware = false; cluster.AddPoint(newPoint); } cluster.CreateConvexHull(m_dataInfo.GetSpatialReference()); m_Clusters.AddCluster(cluster); } } else { for (int i = 0; i < m_localSubGraphs.Count; i++) { Cluster cluster = new Cluster(); cluster.SetClusterIndex(i + 1); ASCDTSubGraph subGraph = m_localSubGraphs[i]; List <ITinNode> tinNodes = subGraph.GetNodeList(); for (int j = 0; j < tinNodes.Count; j++) { IPoint newPoint = new PointClass(); tinNodes[j].QueryAsPoint(newPoint); IZAware pZAware = newPoint as IZAware; pZAware.ZAware = false; cluster.AddPoint(newPoint); } cluster.CreateConvexHull(m_dataInfo.GetSpatialReference()); m_Clusters.AddCluster(cluster); } } }
/// <summary> /// 通过核心点对簇进行扩展 /// </summary> /// <param name="cluster"></param> /// <param name="pntIndex"></param> /// <returns></returns> private Cluster ExpandCluster(Cluster cluster, int pntIndex) { cluster.AddPoint(m_DBSCANPnts[pntIndex].GetPoint()); List <DBSCANPoint> neighbors = m_DBSCANPnts[pntIndex].GetNeighborPoints(); for (int i = 0; i < neighbors.Count; i++) { int neighborIndex = GetDBSCANPointIndexByOID(neighbors[i].GetOID()); if (m_DBSCANPnts[neighborIndex].IsVisited() == true) { continue; } m_DBSCANPnts[neighborIndex].SetAsVisited(); //迭代 //边界点跟其邻域内的某个核心点放在同一个簇中 // if (m_DBSCANPnts[neighborIndex].GetPointType() != 0) if (m_DBSCANPnts[neighborIndex].GetPointType() == 1) { ExpandCluster(cluster, neighborIndex); } } return(cluster); }
static void PrintSegmentMap(Map map, int blueThreshold) { BinaryMatrix discovered = new BinaryMatrix(map.Width, map.Height); BinaryMatrix obstacles = map.ScanObstacles(blueThreshold); List <Cluster> clusters = new List <Cluster>(); for (int x = 0; x < map.Width; x++) { for (int y = 0; y < map.Height; y++) { if (!discovered[x, y] && obstacles[x, y]) { discovered[x, y] = true; Cluster cluster = new Cluster(); Queue <XY> queue = new Queue <XY>(); queue.Enqueue(new XY(x, y)); while (queue.Count > 0) { XY point = queue.Dequeue(); cluster.AddPoint(point); foreach (XY neighbor in point.GetNeighbors()) { if ( neighbor.IsInsideBox(map.Box) && !discovered[neighbor] && obstacles[neighbor] ) { discovered[neighbor] = true; queue.Enqueue(neighbor); } } } clusters.Add(cluster); } } } Console.WriteLine("Discovered {0} clusters", clusters.Count); Bitmap clustersMap = new Bitmap(map.Width, map.Height); for (int x = 0; x < map.Width; x++) { for (int y = 0; y < map.Height; y++) { clustersMap.SetPixel(x, y, Color.Black); } } foreach (Cluster cluster in clusters) { for (int x = cluster.Left; x <= cluster.Right; x++) { for (int y = cluster.Top; y <= cluster.Bottom; y++) { clustersMap.SetPixel(x, y, Color.Red); } } } clustersMap.Save(FILE_BASE + "background-segments.jpg"); }