public static float CalculateDistance(Node n1, Node n2) { Point3D p1 = new Point3D(n1.X, n1.Y, n1.Z); Point3D p2 = new Point3D(n2.X, n2.Y, n2.Z); return (float)Math.Sqrt((Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2) + Math.Pow(p2.Z - p1.Z, 2))); }
private void ReadNodes(ref Stream s, HeaderStruct header) { BinaryReader br = new BinaryReader(s); int size = Marshal.SizeOf(typeof(NodeStruct)); int nsize = Marshal.SizeOf(typeof(NeighborStruct)); byte[] buffer = new byte[size]; byte[] neighbor = new byte[nsize]; List<Neighbor> nList; Node n; for (int i = 0; i < header.pathNodeCount; i++) { br.Read(buffer, 0, size); n = new Node(Functions.ByteArrayToStructure<NodeStruct>(buffer)); nList = new List<Neighbor>(); for (int j = 0; j < 50; j++) { br.Read(neighbor, 0, nsize); NeighborStruct neighborData = Functions.ByteArrayToStructure<NeighborStruct>(neighbor); if (neighborData.id == -1) continue; nList.Add(new Neighbor(neighborData)); } n.Neighbors = nList; _nodes.Add(n); } ConnectNodes(); }
private void OnNodeRemoved(Node node,List<Node>neighbors) { if (NodeRemoved != null) { NodeRemoved(this, new PathModifiedEventArgs(node,neighbors)); } }
private void OnNodeAdded(Node node) { if (NodeAdded != null) { NodeAdded(this, new PathModifiedEventArgs(node,new List<Node>())); } }
public void RemoveNode(Node node) { //OnNodeRemoved(node); List<Node> neighbors = new List<Node>(); node.DisconnectAll(); foreach (Node n in Nodes) { if (n.Neighbors.RemoveAll(x => x.Node == node) > 0) { neighbors.Add(n); } } Nodes.Remove(node); UpdateNodes(); OnNodeRemoved(node, neighbors); }
public void NodeTwoWayDisconnect(Node n1, Node n2) { n1.DisconnectNodeTwoWay(n2); }
public void NodeSingleDisconnect(Node n1, Node n2) { n1.DisconnectNode(n2); }
public void ConnectToNodeAsWarp(Node target) { ConnectToNode(target); foreach (Neighbor n in Neighbors) { if (n.Node == target) { n.Warp = true; n.Distance = 0.0f; return; } } }
public ObservableCollection<Node> GetInaccessibleNodes(Node n) { if (n == null) return new ObservableCollection<Node>(); ObservableCollection<Node> unreachableNodes = new ObservableCollection<Node>(); IEnumerable<Node> connectedNodes = n.ConnectedNodes; var query = Nodes.Select(x => x).Where(y => connectedNodes.Contains(y) == false); foreach (Node node in query) { unreachableNodes.Add(node); } return unreachableNodes; }
public void AddNode(Node node) { Nodes.Add(node); UpdateNodes(); OnNodeAdded(node); }
public void UnsubscribeToNode(Node target) { target.PropertyChanged -= NodePropertyChanged; }
public void SubscribeToNode(Node target) { target.PropertyChanged += NodePropertyChanged; }
public void DisconnectNodeTwoWay(Node target) { target.DisconnectNode(this); DisconnectNode(target); }
public void DisconnectNode(Node target) { Neighbors.RemoveAll(x => x.Node == target); //UnsubscribeToNode(target); target.PropertyChanged -= NodePropertyChanged; NotifyPropertyChanged("Neighbors"); }
public void ConnectToNodeTwoWay(Node target) { ConnectToNode(target); target.ConnectToNode(this); }
public ObservableCollection<Node> GetNonNeighbors(Node n) { if (n == null) return new ObservableCollection<Node>(); ObservableCollection<Node> nonNeighbors = new ObservableCollection<Node>(); List<Node> neighborNodes = new List<Node>(); neighborNodes.Add(n); foreach (Neighbor neighbor in n.Neighbors) { neighborNodes.Add(neighbor.Node); } var query = Nodes.Select(x => x).Where(y => neighborNodes.Contains(y) == false); foreach (Node node in query) { nonNeighbors.Add(node); } return nonNeighbors; }
public PathModifiedEventArgs(Node node, List<Node> neighbors) { _node = node; _neighbors = neighbors; }
///<summary> ///Connects this node to the target node - distance calculations are also stored ///</summary> public void ConnectToNode(Node target) { if (this == target) throw new Exception("Cannot connect node to itself"); //return if this target node is already in the neighbors list foreach (Neighbor neighbor in Neighbors) { if (neighbor.Node == target) return; } Neighbors.Add(new Neighbor() { Node = target, Distance = Path.CalculateDistance(this, target) }); //SubscribeToNode(target); target.PropertyChanged += NodePropertyChanged; NotifyPropertyChanged("Neighbors"); }