/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.vehicular.simulator; import java.util.*; import vnsim.map.object.Cross; import vnsim.map.object.Globals; import vnsim.map.object.Point; import vnsim.map.object.Road; public class RoutingUtils { public static ArrayList getMergedDijkstraPath(Location src, Location dest) { if(src.roadIdx==dest.roadIdx) { ArrayList rs=new ArrayList(); rs.add(new RouteSegment((short)src.roadIdx, (short)src.ptIdx, (short)dest.ptIdx)); return rs; } ArrayList points=dijkstraPath(src, dest); points.add(new Location(dest.roadIdx, dest.ptIdx)); ArrayList ret=mergeRoute(points); //System.out.println("Route after merge: segments="+ret); return ret; } private static ArrayList split(RouteSegment seg) { ArrayList split=new ArrayList (); split.add(seg); Road r=Globals.map.roads.get(seg.roadIndex); boolean done=false; while(!done) { done=true; ArrayList newSplit=new ArrayList (); x:for(int i=0;irs.pt1 )) { newSplit.add(new RouteSegment((short)seg.roadIndex, (short)rs.pt1, (short)ptCross)); newSplit.add(new RouteSegment((short)seg.roadIndex, (short)ptCross, (short)rs.pt2)); done=false; continue x; } if(( rs.pt1>rs.pt2) && (ptCross < rs.pt1 && ptCross>rs.pt2 )) { newSplit.add(new RouteSegment((short)seg.roadIndex, (short)rs.pt1, (short)ptCross)); newSplit.add(new RouteSegment((short)seg.roadIndex, (short)ptCross, (short)rs.pt2)); done=false; continue x; } } newSplit.add(rs); } split=newSplit; } return split; } public static ArrayList splitRoute(ArrayList route) { ArrayList ret=new ArrayList (); for(int i=0;i getUnmergedDijkstraPath(Location src, Location dest) { if(src.roadIdx==dest.roadIdx) { return split(new RouteSegment((short)src.roadIdx, (short)src.ptIdx, (short)dest.ptIdx)); } ArrayList points=dijkstraPath(src, dest); points.add(new Location(dest.roadIdx, dest.ptIdx)); ArrayList ret=new ArrayList(); x:for(int i=0;i dijkstraPath(Location src, Location dest) { //returns a vector of consecutive RouteSegments, the shortest route //between src and dest // System.out.println("Have to compute a route: from "+src+" -- to "+dest); ArrayList routePoints=new ArrayList(); GraphNodeArrayList set=new GraphNodeArrayList(); // Point destPoint=(Point) ((Road)(Globals.map.roads.get(dest.roadIndex))).points.get(dest.pt1); GraphNode sourceGraphNode=new GraphNode(src.roadIdx,src.ptIdx); ArrayList sourceNeighbors=getNeighbors(sourceGraphNode); //add the direct neighbors of the source to the set of nodes; for(int i=0;i rez=new ArrayList(); //find the closest points on the same road which are cross-points //if there is none, take the end points of the road (except when the //node is already an end point); //also check if there are cross-roads in 'node'; Road road=(Road) Globals.map.roads.get(node.roadIndex); int nodeIndex=node.pointIndex; int min=Integer.MAX_VALUE; int max=-1; for(int i=0;imax && idxnodeIndex) min=idx; Point pt=(Point) road.points.get(nodeIndex); if(idx==nodeIndex) { //have to go on the crossing road, to find the closest points; Road crossingRoad=(Road) Globals.map.roads.get(c.getCrossRoadIndex()); Point pt2=null; int index2=-1; //have to find the index of the point on the crossing road for(int j=0;jmaxCross && otherIndexindex2) minCross=otherIndex; } if(minCross==Integer.MAX_VALUE) { //there was no min; have to consider last point of crossing road //only if it is not the last if((index2!=(crossingRoad.points.size()-1))) { NeighborNode n=new NeighborNode(c.getCrossRoadIndex(),crossingRoad.points.size()-1, ((Point)(crossingRoad.points.get(crossingRoad.points.size()-1))).getDistance() - ((Point)(crossingRoad.points.get(index2))).getDistance() ); rez.add(n); } } else { NeighborNode n=new NeighborNode(c.getCrossRoadIndex(),minCross, ((Point)(crossingRoad.points.get(minCross))).getDistance() - ((Point)(crossingRoad.points.get(index2))).getDistance() ); rez.add(n); } if(maxCross==-1) { //there was no max; have to consider first point of crossing road //only if it is not first if(index2!=0) { NeighborNode n=new NeighborNode(c.getCrossRoadIndex(),0, ((Point)( crossingRoad.points.get(index2) )).getDistance() - ((Point)(crossingRoad.points.get(0))).getDistance() ); rez.add(n); } } else { NeighborNode n=new NeighborNode(c.getCrossRoadIndex(),maxCross, ((Point)( crossingRoad.points.get(index2) )).getDistance() - ((Point)(crossingRoad.points.get(maxCross))).getDistance() ); rez.add(n); } } } } if(min==Integer.MAX_VALUE) { //there was no min; have to consider last point of the road //ONLY if I am not the last! if(((road.points.size()-1)!=node.pointIndex)) { NeighborNode n=new NeighborNode(node.roadIndex, road.points.size()-1 , ((Point)(road.points.get(road.points.size()-1))).getDistance() - ((Point)(road.points.get(node.pointIndex))).getDistance() ); rez.add(n); } } else { NeighborNode n=new NeighborNode(node.roadIndex, min , ((Point)(road.points.get(min))).getDistance() - ((Point)(road.points.get(node.pointIndex))).getDistance() ); rez.add(n); } if(max==-1){ //there was no max; have to consider first point of the road //only if I am not first if(node.pointIndex!=0) { NeighborNode n=new NeighborNode(node.roadIndex, 0 , ((Point)(road.points.get(node.pointIndex))).getDistance() - ((Point)(road.points.get(0))).getDistance() ); rez.add(n); } } else { NeighborNode n=new NeighborNode(node.roadIndex, max , ((Point)(road.points.get(node.pointIndex))).getDistance() - ((Point)(road.points.get(max))).getDistance() ); rez.add(n); } return rez; } private static double getDistance(GraphNodeArrayList t, Point p) { //get the distance from the set for the corresponding point; Iterator iter=t.iterator(); while(iter.hasNext()) { GraphNode n=(GraphNode) iter.next(); Point pct=(Point) (((Road)(Globals.map.roads.get(n.roadIndex))).points.get(n.pointIndex)); if(pct.equals(p)) return n.distance; } // System.out.println("ERROR! getDistance() NOT FOUND!\n"); return -1.0; } private static boolean isPointOnRoad(int roadIndex, int pointIndex, int otherRoadIndex) { //[roadIndex,pointIndex] is also situated on "otherRoadIndex" ? Location loc=new Location(roadIndex, pointIndex); for(int i=0;i v; public GraphNodeArrayList(){ v=new ArrayList(); } public void addGraphNode(GraphNode n) { //insertion-sorting add, based on the 'distance' of the graph nodes for(int i=0;i0) { GraphNode n=(GraphNode) v.get(0); v.remove(0); return n; } return null; } public Iterator iterator() { return v.iterator(); } public void setGraphNode(GraphNode n) { //looks through the vector, finds a graph node referring to the same point (by //checking the location of the point), and replaces that graph node; Point pct1 = (Point) (((Road)(Globals.map.roads.get(n.roadIndex))).points.get(n.pointIndex)); for(int i=0;i