diff --git a/src/de/tud/kom/p2psim/impl/topology/movement/local/RealWorldStreetsMovement.java b/src/de/tud/kom/p2psim/impl/topology/movement/local/RealWorldStreetsMovement.java index 8ef7a653bc514dcafc6eba5e2e8c1b79b43b2fc1..50766cc2971296d42c3ab1ec4f07ee09d22907ae 100644 --- a/src/de/tud/kom/p2psim/impl/topology/movement/local/RealWorldStreetsMovement.java +++ b/src/de/tud/kom/p2psim/impl/topology/movement/local/RealWorldStreetsMovement.java @@ -24,6 +24,7 @@ import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; import com.graphhopper.GraphHopper; import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.util.DistanceCalc2D; import com.graphhopper.util.PointList; import com.graphhopper.util.shapes.GHPoint; import com.graphhopper.util.shapes.GHPoint3D; @@ -38,9 +39,7 @@ import de.tudarmstadt.maki.simonstrator.api.Binder; import de.tudarmstadt.maki.simonstrator.api.Monitor; import de.tudarmstadt.maki.simonstrator.api.Monitor.Level; -import java.util.HashMap; -import java.util.Locale; -import java.util.UUID; +import java.util.*; /** * This movement strategy uses the data from osm and navigates the nodes throught streets to the destination @@ -117,7 +116,6 @@ public class RealWorldStreetsMovement extends AbstractLocalMovementStrategy { if(movementType == null || movementType.equals("") || !this.movementType.contains(movementType)) throw new AssertionError("Invalid movement type: " + movementType); - if(!init) init(); PositionVector newPosition = null; if (destination.distanceTo(comp.getRealPosition()) > getMovementSpeed(comp)) @@ -191,7 +189,6 @@ public class RealWorldStreetsMovement extends AbstractLocalMovementStrategy { * @return The projected position in world-dimensions */ private PositionVector transformGPSWindowToOwnWorld(double lat, double lon) { - VisualizationTopologyView.VisualizationInjector.getWorldX(); double x = worldDimensions.getX() * (lon - lonLeft)/(lonRight - lonLeft); double y = worldDimensions.getY() - worldDimensions.getY() * (lat - latLower)/(latUpper - latLower); x = Math.max(0, x); @@ -200,6 +197,59 @@ public class RealWorldStreetsMovement extends AbstractLocalMovementStrategy { y = Math.min(worldDimensions.getY(), y); return new PositionVector(x, y); } + + /** + * Returns a list of points representing the current route of the component. Points are + * in x / y values of the own world. + * @param ms the component + * @return list of movement points. + */ + public List getMovementPoints(SimLocationActuator ms) + { + List positions = new LinkedList<>(); + PointList pointList = movementPoints.get(ms).getPointList(); + + pointList.forEach(p -> positions.add(new PositionVector(transformGPSWindowToOwnWorld(p.getLat(), p.getLon())))); + + return positions; + } + + /** + * Calculates the length of a route in meters. + * @param start Starting position in own world coordinates (x / y) + * @param destination Destination on own world coordinates (x / y) + * @return the length of the route in meters. + */ + public double calculateRouteLength(PositionVector start, PositionVector destination) + { + PointList pointList; + double[] startPosition = transformOwnWorldWindowToGPS(start.getX(), start.getY()); + double[] destinationPosition = transformOwnWorldWindowToGPS(destination.getX(), destination.getY()); + GHRequest req = new GHRequest(startPosition[0], startPosition[1], destinationPosition[0], destinationPosition[1]). + setWeighting(navigationalType). + setVehicle(movementType). + setLocale(Locale.GERMANY); + GHResponse rsp = hopper.route(req); + + //If the requested point is not in the map data, return -1 + if(rsp.hasErrors()) { + return -1; + } + else { + pointList = rsp.getBest().getPoints(); + return pointList.calcDistance(new DistanceCalc2D()); + } + } + + /** + * Calculates the length of the current route of the SimLocationActuator. + * @param ms the component + * @return the length of the current route + */ + public double getCurrentRouteLength(SimLocationActuator ms) + { + return movementPoints.get(ms).getPointList().calcDistance(new DistanceCalc2D()); + } public void setOsmFileLocation(String osmFileLocation) { this.osmFileLocation = osmFileLocation; diff --git a/src/de/tud/kom/p2psim/impl/topology/movement/modularosm/ModularMultiTypeMovementModel.java b/src/de/tud/kom/p2psim/impl/topology/movement/modularosm/ModularMultiTypeMovementModel.java index 2450c3916ae871ce55154989b0b4444a81116580..ff052ed8f85436325884bbfd12bbf2a231d1d815 100644 --- a/src/de/tud/kom/p2psim/impl/topology/movement/modularosm/ModularMultiTypeMovementModel.java +++ b/src/de/tud/kom/p2psim/impl/topology/movement/modularosm/ModularMultiTypeMovementModel.java @@ -3,6 +3,7 @@ package de.tud.kom.p2psim.impl.topology.movement.modularosm; import de.tud.kom.p2psim.api.topology.Topology; import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator; import de.tud.kom.p2psim.impl.topology.PositionVector; +import de.tud.kom.p2psim.impl.topology.movement.local.RealWorldMovementPoints; import de.tud.kom.p2psim.impl.topology.movement.local.RealWorldStreetsMovement; import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.ITransitionStrategy; import de.tud.kom.p2psim.impl.util.Either; @@ -13,6 +14,7 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Attraction import java.util.HashMap; import java.util.LinkedList; +import java.util.List; /** * This class is meant to be used with the RealWorldStreetsMovement @@ -187,6 +189,22 @@ public class ModularMultiTypeMovementModel extends ModularMovementModel Monitor.log(ModularMultiTypeMovementModel.class, Monitor.Level.DEBUG, String.format("Client %s changed his transition strategy from %s to %s", ms.getHost().getId().toString(), usedStrategy.getClass(), newStrategy.getClass())); } + /** + * Returns a list of points representing the current route of the component. Points are + * in x / y values of the own world. + * @param ms the component + * @return list of movement points. + */ + public List getMovementPoints(SimLocationActuator ms) + { + return ((RealWorldStreetsMovement) localMovementStrategy).getMovementPoints(ms); + } + + public RealWorldStreetsMovement getMovementStrategy() + { + return (RealWorldStreetsMovement) localMovementStrategy; + } + /** * Sets the default {@link ITransitionStrategy} for the specified {@link SimLocationActuator}. * @param ms The SimLocationActuator diff --git a/src/de/tud/kom/p2psim/impl/topology/movement/modularosm/MultiTypeMovementListener.java b/src/de/tud/kom/p2psim/impl/topology/movement/modularosm/MultiTypeMovementListener.java index 7dde3a983c15ab0717dca846839d97228085240b..b4337304518bbc6aba534f2a8749f319af51d816 100644 --- a/src/de/tud/kom/p2psim/impl/topology/movement/modularosm/MultiTypeMovementListener.java +++ b/src/de/tud/kom/p2psim/impl/topology/movement/modularosm/MultiTypeMovementListener.java @@ -3,7 +3,10 @@ package de.tud.kom.p2psim.impl.topology.movement.modularosm; import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator; /** - * Created by Clemens on 13.03.2017. + * Listener for the {@link ModularMultiTypeMovementModel} to get + * informed about important changes in the movement. + * + * @author Clemens Krug */ public interface MultiTypeMovementListener {