/** * Add a new destination Node to the graph. * The new point is added to the heuristic as target and to the graph * together with edges to its nearest neighbors and they back to the new * node. * @param newPoint new location to add to the graph * @param isCity flag indicating if the new location is a city, which means the coordinates * may not be located in the sea area. */ public void addDestinationPoint(Point2D newPoint, boolean isCity) { addDestinationPointInternal(newPoint, isCity); } @VisibleForTesting
@PostConstruct private void init(){ clientServerEventBus.register(this); try { initImage(); } catch (IOException e) { logger.error("Failed to create black and white image from the map", e); } } @Subscribe
for (int y = 0; y < height; y += CHECK_DISTANCE) { Point2D p = getPoint(x, y); if (isOnSea(p)) { if (!graph.containsNode(p)) { graph.addNodeInternal(p); Point2D p2 = getPoint(x + CHECK_DISTANCE, y); addEdges(p, p2, false, false); Point2D p2 = getPoint(x, y + CHECK_DISTANCE); addEdges(p, p2, false, false); Point2D p2 = getPoint(x + CHECK_DISTANCE, y + CHECK_DISTANCE); addEdges(p, p2, false, false); Point2D p2 = getPoint(x + CHECK_DISTANCE, y - CHECK_DISTANCE); addEdges(p, p2, false, false); if (p.distance(city.getCoordinates()) < DIAG_CHECK_DISTANCE) { Point2D p2 = city.getCoordinates(); addEdges(p, p2, true, false);
private void addEdges(Point2D from, Point2D to, boolean isCity, boolean initial) { if (isCity || isOnSea(to)) { if (!graph.containsNode(to)) { if (initial) { graph.addNodeInternal(to); } else { graph.addNode(to, true); } } heuristicProvider.getHeuristic(); double weight = calculateWeight(from, to); if (initial) { graph.addEdgeInternal(from, to, weight); } else { graph.addEdge(from, to, weight); } weight = calculateWeight(to, from); if (initial) { graph.addEdgeInternal(to, from, weight); } else { graph.addEdge(to, from, weight); } } }
@Override protected double calculateWeight(Point2D from, Point2D to) { if (from.equals(to)) { return 0; } double distance = from.distance(to); try { int landPixels = imageService.countLandPixels(to, CHECK_DISTANCE*2, getSegments(from, to)); int tangentialLandPixels = imageService.countLandPixels(to, (int) Math.round(DIAG_CHECK_DISTANCE), getTangentialSegments(from, to)); double recastLandPixels = landPixels/ (9.0 * 3); double recastTangentialPixels = tangentialLandPixels / (15.0); return distance + recastLandPixels + recastTangentialPixels; } catch (IOException e) { logger.error("Failed to count the land pixels near "+to, e); } return distance; }
private void ensureSetup(Point2D source, Point2D destination) { if (!graph.containsNode(source)) { boolean isCity = false; for (ICity city : map.getCities()) { if (source.equals(city.getCoordinates())) { isCity = true; break; } } // graphProvider.addSourcePoint(source, isCity); graphProvider.addDestinationPoint(source, isCity); } if (!graph.containsNode(destination)) { boolean isCity = false; for (ICity city : map.getCities()) { if (destination.equals(city.getCoordinates())) { isCity = true; break; } } graphProvider.addDestinationPoint(destination, isCity); } graphProvider.getGraph(); }
} else { final Point2D destination = new Point2D(unscaledX, unscaledY); if (aStarGraphService.isOnSea(destination)) { path = seafaringService.travelTo(vessel, destination);
@VisibleForTesting void addSourcePointInternal(Point2D source, boolean isCity) { synchronized (lock) { if (!heuristic.containsKey(source)) { heuristicProvider.addSourceNodeToHeuristic(source); } if (graph.containsNode(source)) { return; } List<Point2D> nearest = new ArrayList<>(); for (Point2D node : graph) { double distance = node.distance(source); if (distance <= DIAG_CHECK_DISTANCE) { nearest.add(node); } } graph.addNode(source, false); for (Point2D point : nearest) { addEdges(source, point, isCity, false); } } } @Override
graph = graphProvider.getGraph(); Thread.yield();
@VisibleForTesting void addDestinationPointInternal(Point2D newPoint, boolean isCity) { synchronized (lock) { if (!heuristic.containsKey(newPoint)) { heuristicProvider.addSourceNodeToHeuristic(newPoint); // once reached there we must leave again. heuristicProvider.addTargetNodeToHeuristic(newPoint); heuristicProvider.getHeuristic(); } Preconditions.checkArgument(heuristic.containsKey(newPoint), "Destination not part of the heuristic: "+newPoint); if (graph.containsNode(newPoint)) { return; } List<Point2D> nearest = new ArrayList<>(); for (Point2D node : graph) { double distance = node.distance(newPoint); if (distance <= DIAG_CHECK_DISTANCE) { nearest.add(node); } } graph.addNode(newPoint, true); for (Point2D point : nearest) { addEdges(newPoint, point, isCity, false); } } } @VisibleForTesting