private static DirectionGraph CreateSuperGraph(DirectionGraph graph) { Point[] centres = new Point[DIRECTIONNUM]; #region initcentres for (int i = 0; i < DIRECTIONNUM; i++) { centres[i] = new Point(0, 0); } #endregion int[] number = new int[DIRECTIONNUM]; //numbers of buckets int[] nodeweights = new int[DIRECTIONNUM]; DirectionGraph supergraph = new DirectionGraph(); double[,] edgeweights = new double[DIRECTIONNUM, DIRECTIONNUM]; DirectionNode node1, node2; for (int i = 0; i < graph.Nodes.Count; i++) { node1 = graph.Nodes[i]; int dir1 = node1.direction; centres[dir1] += node1.gravitycenter; number[dir1]++; nodeweights[dir1] += node1.Weight; for (int j = i + 1; j < graph.Nodes.Count; j++) { node2 = graph.Nodes[j]; int dir2 = node2.direction; if (dir1 == dir2) continue; edgeweights[dir1, dir2] += graph.perimeterarray[i, j]; edgeweights[dir2, dir1] = edgeweights[dir1, dir2]; } } for (byte i = 0; i < DIRECTIONNUM; i++) { supergraph.add(new DirectionNode(i)); if (number[i] == 0) supergraph.Nodes[i].gravitycenter = new Point(0, 0); else supergraph.Nodes[i].gravitycenter = centres[i] / number[i]; supergraph.Nodes[i].Weight = nodeweights[i]; } for (int i = 0; i < DIRECTIONNUM; i++) { for (int j = 0; j < i; j++) { double dist = Distance(supergraph.Nodes[i].gravitycenter, supergraph.Nodes[j].gravitycenter); edgeweights[i, j] += dist; edgeweights[j, i] += dist; } } supergraph.weightarray = edgeweights; return supergraph; }
private static DirectionGraph CreateSuperGraph(DirectionGraph graph) { Point[] centres = new Point[DIRECTIONNUM]; #region initcentres for (int i = 0; i < DIRECTIONNUM; i++) { centres[i] = new Point(0, 0); } #endregion int[] number = new int[DIRECTIONNUM]; //numbers of buckets int[] nodeweights = new int[DIRECTIONNUM]; DirectionGraph supergraph = new DirectionGraph(); double[,] edgeweights = new double[DIRECTIONNUM, DIRECTIONNUM]; DirectionNode node1, node2; for (int i = 0; i < graph.Nodes.Count; i++) { node1 = graph.Nodes[i]; int dir1 = node1.direction; centres[dir1] += node1.gravitycenter; number[dir1]++; nodeweights[dir1] += node1.Weight; for (int j = i + 1; j < graph.Nodes.Count; j++) { node2 = graph.Nodes[j]; int dir2 = node2.direction; if (dir1 == dir2) { continue; } edgeweights[dir1, dir2] += graph.perimeterarray[i, j]; edgeweights[dir2, dir1] = edgeweights[dir1, dir2]; } } for (byte i = 0; i < DIRECTIONNUM; i++) { supergraph.add(new DirectionNode(i)); if (number[i] == 0) { supergraph.Nodes[i].gravitycenter = new Point(0, 0); } else { supergraph.Nodes[i].gravitycenter = centres[i] / number[i]; } supergraph.Nodes[i].Weight = nodeweights[i]; } for (int i = 0; i < DIRECTIONNUM; i++) { for (int j = 0; j < i; j++) { double dist = Distance(supergraph.Nodes[i].gravitycenter, supergraph.Nodes[j].gravitycenter); edgeweights[i, j] += dist; edgeweights[j, i] += dist; } } supergraph.weightarray = edgeweights; return(supergraph); }
private static DirectionGraph CreateGraph(DirectionalImage directions) { DirectionGraph graph = new DirectionGraph(); int width = directions.arr.GetLength(0); int height = directions.arr.GetLength(1); DirectionNode[,] tempnodes = new DirectionNode[width, height]; DirectionNode dirnode; #region First line and first column if (directions.arr[0, 0] != 9) { dirnode = new DirectionNode(); dirnode.direction = directions.arr[0, 0]; dirnode.add(new Point(0, 0)); graph.add(dirnode); tempnodes[0, 0] = dirnode; } for (int i = 1; i < width; i++) { byte curr = directions.arr[i, 0]; if (curr == 9) continue; byte l = directions.arr[i - 1, 0]; if (curr == l) { tempnodes[i, 0] = tempnodes[i - 1, 0]; tempnodes[i - 1, 0].add(new Point(i, 0)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(i, 0)); graph.add(dirnode); tempnodes[i, 0] = dirnode; } } int a = 1; for (int j = 1; j < height; j++) { byte curr = directions.arr[0, j]; if (curr == 9) continue; byte u = directions.arr[0, j - 1]; if (curr == u) { tempnodes[0, j] = tempnodes[0, j - 1]; tempnodes[0, j - 1].add(new Point(0, j)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(0, j)); graph.add(dirnode); tempnodes[0, j] = dirnode; } } #endregion int b = 0; #region Main for (int j = 1; j < directions.arr.GetLength(1); j++) { for (int i = 1; i < directions.arr.GetLength(0); i++) { byte curr = directions.arr[i, j]; if (curr == 9) continue; byte l = directions.arr[i - 1, j]; byte u = directions.arr[i, j - 1]; if (curr == l && curr == u && tempnodes[i, j - 1] != tempnodes[i - 1, j]) { DirectionNode input, output; if (tempnodes[i - 1, j].Weight > tempnodes[i, j - 1].Weight) { input = tempnodes[i, j - 1]; output = tempnodes[i - 1, j]; } else { output = tempnodes[i, j - 1]; input = tempnodes[i - 1, j]; } foreach (Point point in input.PointList) { tempnodes[point.x, point.y] = output; output.add(point); } tempnodes[i, j] = output; input.PointList.Clear(); output.add(new Point(i, j)); } else if (curr == l) { tempnodes[i, j] = tempnodes[i - 1, j]; tempnodes[i - 1, j].add(new Point(i, j)); } else if (curr == u) { tempnodes[i, j] = tempnodes[i, j - 1]; tempnodes[i, j - 1].add(new Point(i, j)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(i, j)); graph.add(dirnode); tempnodes[i, j] = dirnode; } } } #endregion DirectionGraph endgraph = new DirectionGraph(); foreach (DirectionNode node in graph.Nodes) { if (node.Weight >= MINNOISECOUNT) endgraph.add(node); } #region computeweights foreach (DirectionNode node in endgraph.Nodes) { node.ComputeCenter(); } #endregion FindNeighbours(endgraph, tempnodes); //tempnodes - array with references to DirectionNodes return endgraph; }
private static DirectionGraph CreateGraph(DirectionalImage directions) { DirectionGraph graph = new DirectionGraph(); int width = directions.arr.GetLength(0); int height = directions.arr.GetLength(1); DirectionNode[,] tempnodes = new DirectionNode[width, height]; DirectionNode dirnode; #region First line and first column if (directions.arr[0, 0] != 9) { dirnode = new DirectionNode(); dirnode.direction = directions.arr[0, 0]; dirnode.add(new Point(0, 0)); graph.add(dirnode); tempnodes[0, 0] = dirnode; } for (int i = 1; i < width; i++) { byte curr = directions.arr[i, 0]; if (curr == 9) { continue; } byte l = directions.arr[i - 1, 0]; if (curr == l) { tempnodes[i, 0] = tempnodes[i - 1, 0]; tempnodes[i - 1, 0].add(new Point(i, 0)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(i, 0)); graph.add(dirnode); tempnodes[i, 0] = dirnode; } } int a = 1; for (int j = 1; j < height; j++) { byte curr = directions.arr[0, j]; if (curr == 9) { continue; } byte u = directions.arr[0, j - 1]; if (curr == u) { tempnodes[0, j] = tempnodes[0, j - 1]; tempnodes[0, j - 1].add(new Point(0, j)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(0, j)); graph.add(dirnode); tempnodes[0, j] = dirnode; } } #endregion int b = 0; #region Main for (int j = 1; j < directions.arr.GetLength(1); j++) { for (int i = 1; i < directions.arr.GetLength(0); i++) { byte curr = directions.arr[i, j]; if (curr == 9) { continue; } byte l = directions.arr[i - 1, j]; byte u = directions.arr[i, j - 1]; if (curr == l && curr == u && tempnodes[i, j - 1] != tempnodes[i - 1, j]) { DirectionNode input, output; if (tempnodes[i - 1, j].Weight > tempnodes[i, j - 1].Weight) { input = tempnodes[i, j - 1]; output = tempnodes[i - 1, j]; } else { output = tempnodes[i, j - 1]; input = tempnodes[i - 1, j]; } foreach (Point point in input.PointList) { tempnodes[point.x, point.y] = output; output.add(point); } tempnodes[i, j] = output; input.PointList.Clear(); output.add(new Point(i, j)); } else if (curr == l) { tempnodes[i, j] = tempnodes[i - 1, j]; tempnodes[i - 1, j].add(new Point(i, j)); } else if (curr == u) { tempnodes[i, j] = tempnodes[i, j - 1]; tempnodes[i, j - 1].add(new Point(i, j)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(i, j)); graph.add(dirnode); tempnodes[i, j] = dirnode; } } } #endregion DirectionGraph endgraph = new DirectionGraph(); foreach (DirectionNode node in graph.Nodes) { if (node.Weight >= MINNOISECOUNT) { endgraph.add(node); } } #region computeweights foreach (DirectionNode node in endgraph.Nodes) { node.ComputeCenter(); } #endregion FindNeighbours(endgraph, tempnodes); //tempnodes - array with references to DirectionNodes return(endgraph); }