private void DrawLines(AngleSimlirityEdge sim, Brush b, double StrokeThickness, double StrokeDashArray) { try { Point fromP = sinks[sim.StartVertextID - 1].CenterLocation; Point toP = sinks[sim.EndVertextID - 1].CenterLocation; Line lin = new Line(); lin.Stroke = b; lin.StrokeThickness = StrokeThickness; lin.StrokeDashArray = new DoubleCollection() { StrokeDashArray }; lin.X1 = fromP.X; lin.Y1 = fromP.Y; lin.X2 = toP.X; lin.Y2 = toP.Y; mycanvas.Children.Add(lin); } catch { } }
/// <summary> /// get the angles for all sinks. /// </summary> private void FindAngles() { int id = 0; for (int i = 0; i < sinks.Count; i++) { Sensor isink = sinks[i]; for (int j = i + 1; j < sinks.Count; j++) { if (i != j) { id += 1; Sensor jsink = sinks[j]; double ang = new Angle().GetAngle(isink.Position, jsink.Position, sourcePointCnterlocation); // string str = " # : " + isink.ID + " :" + sourcePointCnterlocation.ID + " :" + jsink.ID + " >>>" + ang; // Console.WriteLine(str); AngleSimlirityEdge edge = new AngleSimlirityEdge() { Angle = ang, StartVertex = isink, EndVertex = jsink, ID = id }; isink.Simlirities.Add(edge); allSimlirityEdges.Add(edge); } } } }
/// <summary> /// matching: https://en.wikipedia.org/wiki/Matching_(graph_theory) /// </summary> /// <param name="simlirities"></param> private void ComputeClusters1(List<AngleSimlirityEdge> simlirities) { //DrawLines(allSimlirityEdges, Brushes.Black); // complete graph //DrawLines(simlirities, Brushes.Black); // sirnked graph simlirities.Sort(new sorter()); List<Cluster> LocalClusters = new List<Cluster>(); int cid = 0; // more than 2 sinks: if (sinks.Count >= 3) { foreach (AngleSimlirityEdge edge in simlirities) { // both are not clusters bool bothAreClusterd = edge.StartVertex.IsClustered && edge.EndVertex.IsClustered; bool bothNotClustered = !edge.StartVertex.IsClustered && !edge.EndVertex.IsClustered; ; // both the verticies of the the edge are not clusterd. bool justOneClustered = (edge.StartVertex.IsClustered || edge.EndVertex.IsClustered) && !bothAreClusterd; // simi-saturated if (bothNotClustered) // both vertecies of the edge are not clustered. { if (edge.Angle <= angThre) // this value need to be checked maybe you can chek if the angle is less than 45. : matching or the matximum matching { cid++; // both verticies are not clusterd Cluster cluster = new Cluster(); cluster.MinAngle = edge.Angle; cluster.SourceLocation = sourcePointCnterlocation; cluster.Min = edge.StandardScor; cluster.ID = cid; edge.StartVertex.IsClustered = true; edge.EndVertex.IsClustered = true; cluster.Members.Add(edge.StartVertex); cluster.Members.Add(edge.EndVertex); LocalClusters.Add(cluster); // darw: optioanal //DrawLines(edge,Brushes.Black, 4,0.5); // blocks: } } else if (justOneClustered) // simi-saturated { // only one vertext is clusterd. add to cluster. Sensor clusterdVertex; // the one which clusterd Sensor notClusterdVertex; if (edge.StartVertex.IsClustered) { clusterdVertex = edge.StartVertex; notClusterdVertex = edge.EndVertex; } else { clusterdVertex = edge.EndVertex; notClusterdVertex = edge.StartVertex; } Cluster FoundCluster = FindClusterByVertext(clusterdVertex, LocalClusters); // if (FoundCluster != null) { double difAngle1 = Math.Sqrt(Math.Abs(Math.Pow(edge.Angle, 2) - Math.Pow(FoundCluster.MinAngle, 2))); // double difAngle2 = Math.Abs(Math.Pow(edge.Angle, 2) - Math.Pow(FoundCluster.MinAngle, 2)); if (difAngle1 <= angThre) // this should be between 5 and 45. { FoundCluster.Members.Add(notClusterdVertex); notClusterdVertex.IsClustered = true; /* if (FoundCluster.MinAngle > edge.Angle) { FoundCluster.MinAngle = edge.Angle; }*/ // DrawLines(edge, Brushes.Blue, 2,1); } } } else if (bothAreClusterd) { // both are clustered in seperate clusters. Cluster startC = FindClusterByVertext(edge.StartVertex, LocalClusters); Cluster endC = FindClusterByVertext(edge.EndVertex, LocalClusters); if (startC != endC) { if (!startC.IsRed && !endC.IsRed) { double dif2 = Math.Abs(Math.Pow(startC.MinAngle, 2) - Math.Pow(endC.MinAngle, 2)); if (dif2 <= angThre) { startC.Members.AddRange(endC.Members); if (startC.MinAngle > endC.MinAngle) { startC.MinAngle = endC.MinAngle; } endC.ID = -1; startC.IsRed = true; // DrawLines(edge, Brushes.Red, 1, 7); } } } } } } else { // two sinks only if (sinks.Count == 2) { if (simlirities.Count == 1) { AngleSimlirityEdge edge = simlirities[0]; if (edge.Angle <= angThre) { Cluster cluster = new Cluster(); cluster.SourceLocation = sourcePointCnterlocation; cluster.Min = edge.StandardScor; cluster.ID = cid; edge.StartVertex.IsClustered = true; edge.EndVertex.IsClustered = true; cluster.Members.Add(edge.StartVertex); cluster.Members.Add(edge.EndVertex); LocalClusters.Add(cluster); } else { Cluster cluster1 = new Cluster(); cluster1.SourceLocation = sourcePointCnterlocation; cluster1.Min = 0; cluster1.ID = cid; edge.StartVertex.IsClustered = true; cluster1.Members.Add(edge.StartVertex); LocalClusters.Add(cluster1); Cluster cluster2 = new Cluster(); cluster2.SourceLocation = sourcePointCnterlocation; cluster2.Min = 0; cluster2.ID = cid; edge.EndVertex.IsClustered = true; cluster2.Members.Add(edge.EndVertex); LocalClusters.Add(cluster2); } } } } // reorganzied: int clusteredNodesCount = 0; foreach (Cluster c in LocalClusters) { if (c.ID != -1) // removed -1. { clusteredNodesCount += c.Members.Count; // count how many nodes which are clustered. Clusters.Add(c); } } // add the isolated sinks: if (clusteredNodesCount < sinks.Count) { foreach (Sensor sin in sinks) { if (!sin.IsClustered) { Cluster cluster = new Cluster(); cluster.ID = Clusters.Count + 1; cluster.SourceLocation = sourcePointCnterlocation; cluster.Members.Add(sin); Clusters.Add(cluster); } } } // finished: foreach (Sensor sink in sinks) { sink.IsClustered = false; } }