Beispiel #1
0
 private void ApplyGrid(GraphNode[,] grid)
 {
     for (int i = 0; i < _gridWidth; ++i)
     {
         for (int j = 0; j < _gridHeight; ++j)
         {
             GraphNode node = grid[1 + i, 1 + j];
             if (node != null)
             {
                 node.SetData(_GridPos, new Point(i, j));
             }
         }
     }
 }
Beispiel #2
0
        private void ScrambleGridStep(GraphNode[,] newGrid,
                                      int maxShift, int iter, int i, int j)
        {
            GraphNode node1 = newGrid[1 + i, 1 + j];

            if (node1 == null)
            {
                return;
            }

            int col = i;
            int row = j;

            // Choose a new position
            Size dir = CalcLinkDir(node1);

            int newCol = maxShift > 1 ?
                         col + dir.Width * _random.Next(0, maxShift + 1) + _random.Next(-1, 1 + 1) :
                         col + dir.Width * _random.Next(0, 1 + 1);
            int newRow = maxShift > 1 ?
                         row + dir.Height * _random.Next(0, maxShift + 1) + _random.Next(-1, 1 + 1) :
                         row + dir.Height * _random.Next(0, 1 + 1);

            if (newCol < 0)
            {
                newCol = 0;
            }
            if (newCol >= _gridWidth)
            {
                newCol = _gridWidth - 1;
            }
            if (newRow < 0)
            {
                newRow = 0;
            }
            if (newRow >= _gridHeight)
            {
                newRow = _gridHeight - 1;
            }

            // Swap node1 with what's at the new pos
            GraphNode node2 = newGrid[1 + newCol, 1 + newRow];

            // Start & end nodes are static
            INode startNode = _info.StartNode;
            INode endNode   = _info.EndNode;

            if (startNode != null)
            {
                if (node1.Node == startNode ||
                    (node2 != null && node2.Node == startNode))
                {
                    return;
                }
            }

            if (endNode != null)
            {
                if (node1.Node == endNode ||
                    (node2 != null && node2.Node == endNode))
                {
                    return;
                }
            }

            newGrid[1 + newCol, 1 + newRow] = node1;
            newGrid[1 + i, 1 + j]           = node2;

            node1.SetData(_GridPos, new Point(newCol, newRow));
            if (node2 != null)
            {
                node2.SetData(_GridPos, new Point(i, j));
            }
        }
Beispiel #3
0
        private GraphNode[,] InitGrid()
        {
            int nodeCount = _graph.Nodes.Count;

            _gridWidth = _gridHeight = (int)Math.Ceiling(Math.Sqrt(nodeCount)) + 2;

            GraphNode[,] grid = new GraphNode[_gridWidth + 2, _gridHeight + 2];
            GraphNode node0 = _backbone[0] as GraphNode;

            node0.SetData(_GridPos, new Point(0, 0));
            grid[1, 1] = node0;

            int   perRow = Math.Max(1, _backbone.Count / _gridHeight + 1);
            int   curRow = 0;
            float curCol = 0;
            int   c      = 0;

            for (int i = 0; i < _backbone.Count - 1; i++)
            {
                int       nodeCol = (int)curCol + c;
                int       nodeRow = curRow;
                GraphNode node    = _backbone[i] as GraphNode;
                node.SetData(_GridPos, new Point(nodeCol, nodeRow));
                grid[1 + nodeCol, 1 + nodeRow] = node;

                c++;
                if (c == perRow)
                {
                    c       = 0;
                    curCol += ((float)_gridWidth - perRow) / _gridHeight;
                    curRow++;
                }
            }

            c = 0;
            for (int i = 0; i < nodeCount; i++)
            {
                int nodeCol = i % _gridHeight;
                int nodeRow = i / _gridHeight;

                if (grid[1 + nodeCol, 1 + nodeRow] != null)
                {
                    continue;
                }

                while (c < nodeCount && _backbone.Contains(_graph.Nodes[c]))
                {
                    c++;
                }

                if (c == nodeCount)
                {
                    break;
                }

                GraphNode node = _graph.Nodes[c] as GraphNode;
                node.SetData(_GridPos, new Point(nodeCol, nodeRow));
                grid[1 + nodeCol, 1 + nodeRow] = node;

                if (c == nodeCount - 1)
                {
                    break;
                }

                c++;
            }

            GraphNode nodeN = _backbone[_backbone.Count - 1] as GraphNode;

            nodeN.SetData(_GridPos, new Point(_gridWidth - 1, _gridHeight - 1));
            grid[1 + _gridWidth - 1, 1 + _gridHeight - 1] = nodeN;

            return(grid);
        }
Beispiel #4
0
 private void CommitMove(GraphNode node)
 {
     node.SetData(BordLines, _bordNewLocalValue);
 }
Beispiel #5
0
		private void Dummify()
		{
			ArrayList links = new ArrayList(_graph.Links);
			foreach (GraphLink link in links)
			{
				GraphNode o = link.Origin;
				GraphNode d = link.Destination;

				int oLayer = (int)o.GetData(_Layer);
				int dLayer = (int)d.GetData(_Layer);
				double oPos = (double)o.GetData(_GridPosition);
				double dPos = (double)d.GetData(_GridPosition);

				double step = (dPos - oPos) / Math.Abs(dLayer - oLayer);

				GraphNode p = o;
				if (oLayer - dLayer > 1)
				{
					for (int i = oLayer - 1; i > dLayer; i--)
					{
						GraphNode newNode = new GraphNode(o.Bounds);
						ArrayList layer = _layers[i] as ArrayList;

						double pos = (i - dLayer) * step + oPos;

						if (pos > layer.Count)
							pos = layer.Count;

						// Check if origin and dest are both last
						if (oPos >= (_layers[oLayer] as ArrayList).Count - 1 &&
							dPos >= (_layers[dLayer] as ArrayList).Count - 1)
							pos = layer.Count;

						// Check if origin and destination are both first
						else if (oPos == 0 && dPos == 0)
							pos = 0;

						newNode.SetData(_Layer, i);
						newNode.SetData(_UBaryCenter, 0.0);
						newNode.SetData(_DBaryCenter, 0.0);
						newNode.SetData(_ULinkCount, 0);
						newNode.SetData(_DLinkCount, 0);
						newNode.SetData(_GridPosition, pos);
						newNode.SetData(_Dummy, true);
						layer.Insert((int)pos, newNode);

						// Translate rightwards nodes' positions
						for (int r = (int)pos + 1; r < layer.Count; r++)
						{
							GraphNode node = layer[r] as GraphNode;
							node.SetData(_GridPosition,
								(double)node.GetData(_GridPosition) + 1);
						}

						GraphLink newLink = new GraphLink(p, newNode);
						newLink.SetData(_DummificationLevel, 0);
						p = newNode;

						// Add the new node and the new link to the graph
						_graph.Nodes.Add(newNode);
						_graph.Links.Add(newLink);
					}

					// Set the origin of the real arrow to the last dummy
					link.Origin = p;
					link.SetData(_DummificationLevel, oLayer - dLayer - 1);
				}

				if (oLayer - dLayer < -1)
				{
					for (int i = oLayer + 1; i < dLayer; i++)
					{
						GraphNode newNode = new GraphNode(o.Bounds);
						ArrayList layer = _layers[i] as ArrayList;

						double pos = (i - oLayer) * step + oPos;

						if (pos > layer.Count)
							pos = layer.Count;

						// Check if origin and dest are both last
						if (oPos >= (_layers[oLayer] as ArrayList).Count - 1 &&
							dPos >= (_layers[dLayer] as ArrayList).Count - 1)
							pos = layer.Count;

						// Check if origin and destination are both first
						else if (oPos == 0 && dPos == 0)
							pos = 0;

						newNode.SetData(_Layer, i);
						newNode.SetData(_UBaryCenter, 0.0);
						newNode.SetData(_DBaryCenter, 0.0);
						newNode.SetData(_ULinkCount, 0);
						newNode.SetData(_DLinkCount, 0);
						newNode.SetData(_GridPosition, pos);
						newNode.SetData(_Dummy, true);
						layer.Insert((int)pos, newNode);

						// Translate rightwards nodes' positions
						for (int r = (int)pos + 1; r < layer.Count; r++)
						{
							GraphNode node = layer[r] as GraphNode;
							node.SetData(_GridPosition,
								(double)node.GetData(_GridPosition) + 1);
						}

						GraphLink newLink = new GraphLink(p, newNode);
						newLink.SetData(_DummificationLevel, 0);
						p = newNode;

						// Add the new node and the new link to the graph
						_graph.Nodes.Add(newNode);
						_graph.Links.Add(newLink);
					}

					// Set the origin of the real arrow to the last dummy
					link.Origin = p;
					link.SetData(_DummificationLevel, dLayer - oLayer - 1);
				}
			}
		}
Beispiel #6
0
		private bool MoveLeft(GraphNode node, ArrayList layer, double priority)
		{
			int index = layer.IndexOf(node);
			if (index == 0)
			{
				// This is the last node in the layer - 
				// so we can move to the left without troubles
				node.SetData(_GridPosition, (double)node.GetData(_GridPosition) - 0.5);
				return true;
			}

			GraphNode leftNode = layer[index - 1] as GraphNode;

			double leftNodePriority = ((double)leftNode.GetData(_UPriority) +
				(double)leftNode.GetData(_DPriority)) / 2;

			// Check if there is space
			// between the left and the current node
			if ((double)leftNode.GetData(_GridPosition) <
				(double)node.GetData(_GridPosition) - 1)
			{
				node.SetData(_GridPosition, (double)node.GetData(_GridPosition) - 0.5);
				return true;
			}

			// We have reached a node with higher priority - 
			// no movement is allowed
			if (leftNodePriority > priority ||
				Math.Abs(leftNodePriority - priority) < double.Epsilon)
				return false;

			// The left node has lower priority - try to move it
			if (MoveLeft(leftNode, layer, priority))
			{
				node.SetData(_GridPosition, (double)node.GetData(_GridPosition) - 0.5);
				return true;
			}

			return false;
		}
Beispiel #7
0
		private void CommitMove(GraphNode node)
		{
			node.SetData(BordLines, _bordNewLocalValue);
		}