Search in sources :

Example 26 with MapNode

use of aimax.osm.data.entities.MapNode in project aima-java by aimacode.

the class MapViewPopup method actionPerformed.

@Override
public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == entityInfoMenuItem) {
        MapNode mNode = pane.getRenderer().getNextNode(x, y);
        if (mNode != null)
            pane.showMapEntityInfoDialog(mNode, pane.isDebugModeEnabled());
    } else if (ae.getSource() == clearMenuItem) {
        pane.getMap().clearMarkersAndTracks();
    } else if (ae.getSource() == createMarkerMenuItem) {
        PositionPanel panel = new PositionPanel();
        int res = JOptionPane.showConfirmDialog(pane, panel, "Specify a Position", JOptionPane.OK_CANCEL_OPTION);
        if (res == JOptionPane.OK_OPTION) {
            float lat = panel.getLat();
            float lon = panel.getLon();
            if (!Float.isNaN(lat) && !Float.isNaN(lon)) {
                pane.getMap().addMarker(lat, lon);
                pane.adjustToCenter(lat, lon);
            }
        }
    } else if (ae.getSource() == removeMarkerMenuItem) {
        pane.removeNearestMarker(x, y);
    } else if (ae.getSource() == loadMarkersMenuItem) {
        XMLDecoder decoder = null;
        try {
            File xmlFile = null;
            if (getFileChooser().showDialog(pane, "Load Markers") == JFileChooser.APPROVE_OPTION) {
                xmlFile = getFileChooser().getSelectedFile();
                if (!xmlFile.getPath().contains("."))
                    xmlFile = new File(xmlFile.getPath() + ".xml");
            }
            if (xmlFile != null && xmlFile.exists()) {
                pane.getMap().clearMarkersAndTracks();
                decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(xmlFile)));
                int size = (Integer) decoder.readObject();
                for (int i = 0; i < size; i++) {
                    WritablePosition pos = (WritablePosition) decoder.readObject();
                    pane.getMap().addMarker(pos.getLat(), pos.getLon());
                }
                pane.fireMapViewEvent(new MapViewEvent(pane, MapViewEvent.Type.MARKER_ADDED));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (decoder != null)
                decoder.close();
        }
    } else if (ae.getSource() == saveMarkersMenuItem) {
        XMLEncoder encoder = null;
        try {
            File xmlFile = null;
            if (getFileChooser().showDialog(pane, "Save Markers") == JFileChooser.APPROVE_OPTION) {
                xmlFile = getFileChooser().getSelectedFile();
                if (!xmlFile.getPath().contains("."))
                    xmlFile = new File(xmlFile.getPath() + ".xml");
                encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(xmlFile)));
                encoder.writeObject(pane.getMap().getMarkers().size());
                for (MapNode node : pane.getMap().getMarkers()) encoder.writeObject(new WritablePosition(node));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (encoder != null)
                encoder.close();
        }
    } else if (ae.getSource() == functionsMenuItem) {
        JOptionPane.showMessageDialog(pane, MapViewPane.FUNCTION_DESCRIPTION.split("\\|"), "Function Description", JOptionPane.INFORMATION_MESSAGE);
    } else if (ae.getSource() == debugMenuItem) {
        pane.enableDebugMode(debugMenuItem.isSelected());
    }
}
Also used : MapViewEvent(aimax.osm.viewer.MapViewEvent) MapNode(aimax.osm.data.entities.MapNode) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLEncoder(java.beans.XMLEncoder) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 27 with MapNode

use of aimax.osm.data.entities.MapNode in project aima-java by aimacode.

the class OsmAgentController method initAgents.

/** Creates new agents and adds them to the current environment. */
protected void initAgents(MessageLogger logger) {
    List<MapNode> markers = map.getOsmMap().getMarkers();
    if (markers.size() < 2) {
        logger.log("Error: Please set two markers with mouse-left.");
        return;
    }
    String[] locs = new String[markers.size()];
    for (int i = 0; i < markers.size(); i++) {
        MapNode node = markers.get(i);
        Point2D pt = new Point2D(node.getLon(), node.getLat());
        locs[i] = map.getNearestLocation(pt);
    }
    MapAgentFrame.SelectionState state = frame.getSelection();
    heuristic = createHeuristic(state.getIndex(MapAgentFrame.HEURISTIC_SEL), locs[1]);
    search = SearchFactory.getInstance().createSearch(state.getIndex(MapAgentFrame.SEARCH_SEL), state.getIndex(MapAgentFrame.Q_SEARCH_IMPL_SEL), heuristic);
    Agent agent = null;
    switch(state.getIndex(MapAgentFrame.AGENT_SEL)) {
        case 0:
            agent = new SimpleMapAgent(map, env, search, new String[] { locs[1] });
            break;
        case 1:
            Problem<String, MoveToAction> p = new BidirectionalMapProblem(map, null, locs[1]);
            OnlineSearchProblem<String, MoveToAction> osp = new GeneralProblem<>(null, p::getActions, null, p::testGoal, p::getStepCosts);
            agent = new LRTAStarAgent<>(osp, MapFunctions.createPerceptToStateFunction(), s -> heuristic.applyAsDouble(new Node<>(s)));
            break;
    }
    env.addAgent(agent, locs[0]);
}
Also used : DecimalFormat(java.text.DecimalFormat) MessageLogger(aima.gui.swing.framework.MessageLogger) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) LRTAStarAgent(aima.core.search.online.LRTAStarAgent) MapAdapter(aimax.osm.routing.MapAdapter) SearchForActions(aima.core.search.framework.SearchForActions) MapAgentFrame(aima.gui.swing.applications.agent.map.MapAgentFrame) ArrayList(java.util.ArrayList) Point2D(aima.core.util.math.geom.shapes.Point2D) AgentAppController(aima.gui.swing.framework.AgentAppController) List(java.util.List) Node(aima.core.search.framework.Node) OnlineSearchProblem(aima.core.search.framework.problem.OnlineSearchProblem) MapWayAttFilter(aimax.osm.data.MapWayAttFilter) ToDoubleFunction(java.util.function.ToDoubleFunction) MapNode(aimax.osm.data.entities.MapNode) Agent(aima.core.agent.Agent) SimulationThread(aima.gui.swing.framework.SimulationThread) aima.core.environment.map(aima.core.environment.map) Problem(aima.core.search.framework.problem.Problem) SearchFactory(aima.gui.util.SearchFactory) LRTAStarAgent(aima.core.search.online.LRTAStarAgent) Agent(aima.core.agent.Agent) MapNode(aimax.osm.data.entities.MapNode) aima.core.environment.map(aima.core.environment.map) Point2D(aima.core.util.math.geom.shapes.Point2D) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) MapAgentFrame(aima.gui.swing.applications.agent.map.MapAgentFrame)

Example 28 with MapNode

use of aimax.osm.data.entities.MapNode in project aima-java by aimacode.

the class DefaultTrack method addNode.

@Override
public void addNode(Position pos) {
    int idx = trkpts.isEmpty() ? 0 : (int) trkpts.get(trkpts.size() - 1).getId() + 1;
    MapNode node = new DefaultMapNode(idx);
    node.setPosition(pos.getLat(), pos.getLon());
    addNode(node);
}
Also used : MapNode(aimax.osm.data.entities.MapNode)

Example 29 with MapNode

use of aimax.osm.data.entities.MapNode in project aima-java by aimacode.

the class OsmAgentBaseApp method simulate.

/** Starts the experiment. */
public void simulate() {
    List<MapNode> markers = map.getOsmMap().getMarkers();
    if (markers.size() < 2) {
        simPaneCtrl.setStatus("Error: Please set at least two markers with mouse-left.");
    } else {
        List<String> locations = new ArrayList<>(markers.size());
        for (MapNode node : markers) {
            Point2D pt = new Point2D(node.getLon(), node.getLat());
            locations.add(map.getNearestLocation(pt));
        }
        Agent agent = createAgent(locations);
        env = createEnvironment();
        env.addEnvironmentView(new TrackUpdater());
        env.addAgent(agent, locations.get(0));
        if (simPaneCtrl.getParam(PARAM_SEARCH).isPresent())
            env.notifyViews("Using " + simPaneCtrl.getParamValue(PARAM_SEARCH));
        while (!env.isDone() && !CancelableThread.currIsCanceled()) {
            env.step();
            simPaneCtrl.waitAfterStep();
        }
        envViewCtrl.notify("");
    // simPaneCtrl.setStatus(search.getMetrics().toString());
    }
}
Also used : MapAgent(aima.core.environment.map.MapAgent) Point2D(aima.core.util.math.geom.shapes.Point2D) ArrayList(java.util.ArrayList) MapNode(aimax.osm.data.entities.MapNode)

Example 30 with MapNode

use of aimax.osm.data.entities.MapNode in project aima-java by aimacode.

the class MapPaneCtrl method handleMouseEvent.

protected void handleMouseEvent(MouseEvent event) {
    if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
        xDrag = event.getX();
        yDrag = event.getY();
        dragActive = false;
    } else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
        adjust(event.getX() - xDrag, event.getY() - yDrag);
        xDrag = event.getX();
        yDrag = event.getY();
        dragActive = true;
    } else if (event.getEventType() == MouseEvent.MOUSE_CLICKED && !dragActive) {
        if (event.getButton() == MouseButton.PRIMARY) {
            CoordTransformer tr = getTransformer();
            getMap().addMarker(tr.lat((int) event.getY()), tr.lon((int) event.getX()));
        } else if (event.getButton() == MouseButton.SECONDARY) {
            getMap().clearMarkersAndTracks();
        } else if (event.getButton() == MouseButton.MIDDLE) {
            MapNode mNode = getRenderer().getNextNode((int) event.getX(), (int) event.getY());
            if (mNode != null)
                showMapEntityInfoDialog(mNode, true);
        }
    }
    // hack...
    currCanvas.requestFocus();
}
Also used : CoordTransformer(aimax.osm.viewer.CoordTransformer) MapNode(aimax.osm.data.entities.MapNode)

Aggregations

MapNode (aimax.osm.data.entities.MapNode)37 ArrayList (java.util.ArrayList)13 Position (aimax.osm.data.Position)12 MapWay (aimax.osm.data.entities.MapWay)6 MapEntity (aimax.osm.data.entities.MapEntity)5 BoundingBox (aimax.osm.data.BoundingBox)4 WayRef (aimax.osm.data.entities.WayRef)4 MapAdapter (aimax.osm.routing.MapAdapter)4 Point2D (aima.core.util.math.geom.shapes.Point2D)3 MapEvent (aimax.osm.data.MapEvent)3 EntityAttribute (aimax.osm.data.entities.EntityAttribute)3 Node (aima.core.search.framework.Node)2 LRTAStarAgent (aima.core.search.online.LRTAStarAgent)2 IOException (java.io.IOException)2 DecimalFormat (java.text.DecimalFormat)2 HashSet (java.util.HashSet)2 Agent (aima.core.agent.Agent)1 aima.core.environment.map (aima.core.environment.map)1 MapAgent (aima.core.environment.map.MapAgent)1 MapEnvironment (aima.core.environment.map.MapEnvironment)1