📄 roadfinder.java
字号:
newIntersection, secondIntersection, road.getRoadClass()); newRoad.setRoadPoints(pointsAfter); return newIntersection; } /** * Join two roads into one. The roads must be the only two roads * at the intersection and must be of the same class. If the roads * are not distinct, then we quietly delete the road and remove * the intersection. The roads might not be distinct if they form * an isolated loop (such as a racetrack). Thus situation is * particularly problematic if the road has no inner points. The * RoadPoints of both roads are concatenated with a new RoadPoint * where the intersection was between them. This code is a little * complicated because the RoadPoints must be assembled in a valid * order. The order used is to start from the other intersection * of the first road to the given intersection and from the given * intersection of the second road to the other intersection of * the second road. */ public void joinRoads(Intersection intersection) { if (intersection.getRoadCount() != 2) throw new IllegalArgumentException("Illegal intersection conversion"); Road road0 = intersection.getRoad(0); Road road1 = intersection.getRoad(1); if (road0 == road1) { roads.remove(road1); intersections.remove(intersection); return; } if (road0.getRoadClass() != road1.getRoadClass()) throw new IllegalArgumentException("Illegal intersection conversion"); intersections.remove(intersection); roads.remove(road1); RoadPoint[] road0Points = road0.getRoadPoints(); RoadPoint[] road1Points = road1.getRoadPoints(); RoadPoint[] innerPoints = new RoadPoint[road0Points.length + road1Points.length + 1]; int j = 0; Intersection firstIntersection; if (intersection == road0.getFirstIntersection()) { firstIntersection = road0.getSecondIntersection(); for (int i = road0Points.length; --i >= 0;) innerPoints[j++] = road0Points[i]; } else { firstIntersection = road0.getFirstIntersection(); System.arraycopy(road0Points, 0, innerPoints, j, road0Points.length); j += road0Points.length; } Intersection otherIntersection = road1.getOtherIntersection(intersection); otherIntersection.removeRoad(road1); road0.setIntersections(firstIntersection, otherIntersection); otherIntersection.addRoad(road0); innerPoints[j++] = new RoadPoint(road0, intersection.getLocation(), this); if (intersection == road1.getFirstIntersection()) { System.arraycopy(road1Points, 0, innerPoints, j, road1Points.length); j += road1Points.length; } else { for (int i = road1Points.length; --i >= 0;) innerPoints[j++] = road1Points[i]; } road0.setRoadPoints(innerPoints); road0.setName(mergeRoadNames(road0.getName(), road1.getName())); } protected String mergeRoadNames(String name0, String name1) { return name0 + "+" + name1; } public RoadClass findRoadClass(Object className) { RoadClass cl_ss = (RoadClass) roadClasses.get(className); if (cl_ss == null) return defaultRoadClass; return cl_ss; } public int findUnusedRoadID() { return roads.findUnusedID(); } /** * Displays a Route between two points on the map. * <p> * * @param start start from start point on map * @param end to end point on map * @param route the Route to travel from start to end * @param segments as side effect, populated with PathSegments * between returned WayPoints * @return List of WayPoints */ public List displayPathOnRoad(Point start, Point end, Route route, List segments) { List newPoints; try { if (route == null) { OMPoint point = new RedPoint(start.x, start.y, 5); toDraw.add(point); point = new RedPoint(end.x, end.y, 5); toDraw.add(point); return null; } if (drawResults) { OMPoint point = new YellowPoint(start.x, start.y, 10); toDraw.add(point); point = new YellowPoint(end.x, end.y, 10); toDraw.add(point); } newPoints = new ArrayList(); populatePointsAndSegments(route, newPoints, segments); if (drawResults) { Point last = null; Point first = null; for (Iterator iter = newPoints.iterator(); iter.hasNext();) { Point pt = (Point) iter.next(); if (last != null) { OMLine line = new BlueLine(last.x, last.y, pt.x, pt.y); toDraw.add(line); } if (first == null) first = pt; last = pt; } // draw line from start to beginning intersection OMLine line = new YellowLine(start.x, start.y, first.x, first.y, 10); toDraw.add(line); line = new YellowLine(last.x, last.y, end.x, end.y, 10); toDraw.add(line); } } catch (Exception e) { logger.warning("Got exception " + e); e.printStackTrace(); return null; } return newPoints; } /** * Finds closest intersection to start and end find path from * start intersection to end intersection * <p> * * This method works on screen coordinates. * * @param start from start point on map * @param end to end point on map * @param segments as side effect, populated with PathSegments * between returned WayPoints * @return List of WayPoints */ public List getPathOnRoad(Point start, Point end, List segments) { List newPoints; try { Route bestRoute = getRouteBetweenPoints(start, end); newPoints = displayPathOnRoad(start, end, bestRoute, segments); } catch (Exception e) { logger.warning("Got exception " + e); e.printStackTrace(); return null; } return newPoints; } /** * a red point for displaying when we can't find a route between * two points */ protected class RedPoint extends OMPoint { public RedPoint(int x, int y, int radius) { super(x, y, radius); } public void render(Graphics g) { setGraphicsColor(g, Color.RED); draw(g); } } /** a blue line to indicate the found route */ protected class BlueLine extends OMLine { int width; public BlueLine(int x, int y, int x2, int y2) { super(x, y, x2, y2); this.width = 5; } public void render(Graphics g) { float[] dash1 = new float[width + 1]; dash1[0] = 10.f; for (int i = 1; i < width; i++) { dash1[i] = 2.0f; } BasicStroke dashed = new BasicStroke(5.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f); ((Graphics2D) g).setStroke(dashed); setGraphicsColor(g, Color.BLUE); draw(g); } } /** * Returns best Route between two points specified by latitude and * longitude. * <p> * * This method works on latitude/longitude coordinates. * * @return Route between two points */ public Route getPathOnRoad(LatLonPoint start, LatLonPoint end) { Intersection startTemp = findClosestIntersection(start); Intersection endTemp = findClosestIntersection(end); Route bestRoute = null; if (startTemp != null && endTemp != null) { if (roadClasses == null) logger.warning("huh? road classes is null???"); bestRoute = Route.getBestRoute(startTemp, endTemp, roadClasses.getBestConvoySpeed(), roadClasses.getWorstConvoySpeed()); } if (bestRoute == null) { if (logger.isLoggable(Level.INFO)) logger.info("no route from " + startTemp + " to " + endTemp); } else { if (logger.isLoggable(Level.INFO)) logger.info("route from " + startTemp + " to " + endTemp + " is " + bestRoute); } // post condition check if (logger.isLoggable(Level.INFO) && bestRoute != null) { float length = 0; for (int i = 0; i < bestRoute.getRoads().length; i++) { Road road = bestRoute.getRoads()[i]; length += road.getLengthInKilometers(); } logger.info("best route from " + bestRoute.getOriginIntersection() + " - start " + start + " to " + bestRoute.getDestinationIntersection() + " - end " + end + " was " + length + " kilometers."); } return bestRoute; } /** * Returns best Route between two points * * This method works on latitude/longitude coordinates. * * @return Route between two points */ public Route getRouteBetweenPoints(Point start, Point end) { return getPathOnRoad(createLatLonPoint(start.x, start.y), createLatLonPoint(end.x, end.y)); } /** * Look in intersection Quad Tree for closest intersection to * point x,y * * @return Intersection closest */ protected Intersection findClosestIntersection(int x, int y) { return findClosestIntersection(createLatLonPoint(x, y)); } /** * Look in intersection Quad Tree for closest intersection to * point at specified latitude and longitude. * <p> * * @return Intersection closest */ protected Intersection findClosestIntersection(LatLonPoint latLon) { Intersection inter = (Intersection) interQuadTree.get(latLon.getLatitude(), latLon.getLongitude()); if (inter == null) logger.warning("no intersection at " + latLon); return inter; } /** * Iterates over route, populating points and segments lists. * Worries about sequence order of from and to points, i.e. end of * one road should be the start of the next. This is not * guaranteed by the route, so we have to check. * * @param bestRoute route to iterate over. * @param newPoints populated with points on the route. * @param segments populated with Segments. */ protected void populatePointsAndSegments(Route bestRoute, List newPoints, List segments) { Projection proj = getProjection(); Intersection origin = bestRoute.getOriginIntersection(); // Intersection dest = bestRoute.getDestinationIntersection(); if (logger.isLoggable(Level.INFO)) logger.info("adding " + bestRoute.roads.length + " new roads."); Road road = null; Intersection from = origin, to = null; Set loopSet = new HashSet(); if (doLoopCheck) loopSet.add(origin);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -