Example #1
0
        public T GetWeight(int nodeAIndex, int nodeBIndex)
        {
            IWeightedGraphNode <T> nodeA = GetByIndex(nodeAIndex);
            IWeightedGraphNode <T> nodeB = GetByIndex(nodeBIndex);

            return(nodeA.GetWeight(nodeB));
        }
Example #2
0
        public void SetWeightSymmetrical(IWeightedGraphNode <T> node, T weight)
        {
            int index = IndexOf((WeightedGraphNode <T>)node);

            if (index < 0)
            {
                throw new ArgumentException("Node is not connected");
            }
            _weights[index] = weight;
            node.SetWeight(this, weight);
        }
Example #3
0
        public T GetWeight(IWeightedGraphNode <T> node)
        {
            int index = IndexOf((WeightedGraphNode <T>)node);

            if (index < 0)
            {
                return(InfinityWeight);
                //throw new ArgumentException("Node is not connected");
            }

            return(_weights[index]);
        }
Example #4
0
        public ICollection <IWeightedGraphNode <T> > GetWeightedGraphNodes()
        {
            IWeightedGraphNode <T>[] nodes = new IWeightedGraphNode <T> [Width * Height];

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    nodes[y * Width + x] = (IWeightedGraphNode <T>)GraphNodes[x, y];
                }
            }

            return(nodes);
        }
Example #5
0
        public void SetWeight(IWeightedGraphNode <T> connection, T weight)
        {
            int connectionIndex = Connections.IndexOf((GraphNode)connection);

            if (connectionIndex == -1)
            {
                Connections.Add((GraphNode)connection);
                _weights.Add(weight);
            }
            else
            {
                _weights[connectionIndex] = weight;
            }
        }
Example #6
0
        public void ConnectNode(IWeightedGraphNode <double> node, NeighbourMode neighbourMode)
        {
            AStarAlgorithm        aStarAlgorithm = new AStarAlgorithm();
            CoordinateTransformer transformer    = new CoordinateTransformer(this, LeftBottom);

            foreach (var transitionNode in _transitionNodes)
            {
                IList <Vector2Int> path = aStarAlgorithm.GetPath(transformer, node.Position - LeftBottom,
                                                                 transitionNode.Position - LeftBottom, neighbourMode);
                if (path != null)
                {
                    node.SetWeight(transitionNode, path.Count);
                    transitionNode.SetWeight(node, path.Count);
                    // Other mode will not be connected
                }
            }
        }
Example #7
0
        public IList <Vector2Int> GetPathSingleLayer(ICellMap mapBase, Vector2Int start, Vector2Int stop, NeighbourMode neighbourMode)
        {
            if (neighbourMode == NeighbourMode.SideOnly)
            {
                GraphNode[,] graphNodes      = GraphGenerator.GetGraph(mapBase, neighbourMode);
                IGraphNode[,] nodesInterface = new IGraphNode[mapBase.Width, mapBase.Height];
                for (int i = 0; i < mapBase.Width; i++)
                {
                    for (int j = 0; j < mapBase.Height; j++)
                    {
                        if (graphNodes[i, j] == null)
                        {
                            continue;
                        }
                        graphNodes[i, j].Data = new GraphData();
                        SetPosition(graphNodes[i, j], new Vector2Int(i, j));
                        nodesInterface[i, j] = graphNodes[i, j];
                    }
                }

                IGraphNode startNode = graphNodes[start.X, start.Y];
                IGraphNode stopNode  = graphNodes[stop.X, stop.Y];


                Graph graph = new Graph(nodesInterface);

                var nodePath = GetPath(graph, startNode, stopNode);
                if (nodePath == null)
                {
                    return(null);
                }
                List <Vector2Int> path = new List <Vector2Int>(nodePath.Count);
                foreach (var node in nodePath)
                {
                    path.Add(node.Position);
                }

                return(path);
            }
            else
            {
                IWeightedGraph <double> graph = GraphGenerator.GetWeightedGraph(mapBase, NeighbourMode.SidesAndDiagonals);
                var nodes = graph.GetWeightedGraphNodes();
                IWeightedGraphNode <double> startNode = null, stopNode = null;
                foreach (var node in nodes)
                {
                    if (node == null)
                    {
                        continue;
                    }
                    node.Data = new GraphData();
                    if (node.Position == start)
                    {
                        startNode = node;
                    }

                    if (node.Position == stop)
                    {
                        stopNode = node;
                    }
                }

                var nodePath = GetPath(graph, startNode, stopNode);
                if (nodePath == null)
                {
                    return(null);
                }
                List <Vector2Int> path = new List <Vector2Int>(nodePath.Count);
                foreach (var node in nodePath)
                {
                    path.Add(node.Position);
                }

                return(path);
            }
        }
Example #8
0
        public IList <IGraphNode> GetPath(IWeightedGraph <double> map, IWeightedGraphNode <double> start, IWeightedGraphNode <double> stop)
        {
            var nodePath = GetPath((IGraph)map, start, stop);

            return(nodePath);
        }
Example #9
0
 private double GetConnectionWeight(IWeightedGraphNode <double> weightedNodeA,
                                    IWeightedGraphNode <double> weightedNodeB)
 {
     return(weightedNodeA.GetWeight(weightedNodeB));
 }
Example #10
0
 public int GetConnectionIndex(IWeightedGraphNode <T> connection)
 {
     return(Connections.IndexOf((GraphNode)connection));
 }
Example #11
0
 public bool Remove(IWeightedGraphNode <T> item)
 {
     return(base.Remove(item));
 }
Example #12
0
 public bool Contains(IWeightedGraphNode <T> item)
 {
     return(base.Contains(item));
 }
Example #13
0
 public void Add(IWeightedGraphNode <T> item)
 {
     base.Add(item);
 }