Explorar o código

Bug fixes in symbol layer

- symbol Layer edge Bug fixed
- added buttons for map type to fxml
dominik %!s(int64=8) %!d(string=hai) anos
pai
achega
6b86a09b9f

+ 18 - 16
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/ButtonManager.java

@@ -213,22 +213,12 @@ public final class ButtonManager {
 
 		}
 		
-		try {
-			// load world view 
-			activateWorldView();
-			
-		} catch (IOException e) {
-			
-			// problems with Internet connection, maybe host not reachable, maybe no Internet connection at all
-			GraphDisplayManager.switchActiveGraph();
-			setBorderStyle((Button) arg0.getSource());
-			
-			// show "Connection Error" message
+		// load world view 
+		if(!activateWorldView()){
+			// show "Connection Error" message, because of problems during connecting attempt to server
 			showConnectionErrorMsg();
-			
-			return;
 		}
-
+		
 		GraphDisplayManager.switchActiveGraph();
 		setBorderStyle((Button) arg0.getSource());
 
@@ -251,7 +241,7 @@ public final class ButtonManager {
 	 * Initializes the WorldView, sets data and paints them.
 	 * @throws IOException 
 	 */
-	private static void activateWorldView() throws IOException {
+	private static boolean activateWorldView() {
 
 		// dont show graph and toolbox
 		controller.toolbox.setVisible(false);
@@ -274,7 +264,17 @@ public final class ButtonManager {
 		// show center map Button
 		controller.centerMap.setVisible(true);
 
-		WorldView.loadWorldView();
+		// standard server connection status is true
+		Boolean serverConnection = true;
+		
+		try {
+			
+			WorldView.loadWorldView();
+		} catch (IOException e) {
+			// problems with server connection -> show error message
+			serverConnection = false;
+		}
+		
 
 		MapViewFunctions.checkVBoxChanged();
 
@@ -283,6 +283,8 @@ public final class ButtonManager {
 		// set content to UI Element
 		controller.swingNodeWorldView.setContent(WorldView.internMapViewer);
 		controller.swingNodeWorldView.setVisible(true);
+		
+		return serverConnection;
 	}
 
 	/**

+ 114 - 43
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/CustomMapClickListener.java

@@ -4,12 +4,14 @@ import java.awt.geom.Line2D;
 import java.awt.geom.Point2D;
 import java.util.HashSet;
 
+import org.graphstream.algorithm.Toolkit;
 import org.graphstream.graph.Edge;
 import org.jxmapviewer.JXMapViewer;
 import org.jxmapviewer.input.MapClickListener;
 import org.jxmapviewer.viewer.GeoPosition;
 
 import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
+import de.tu_darmstadt.informatik.tk.scopviz.main.EdgeSelectionHelper;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.GraphDisplayManager;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.PropertiesManager;
@@ -25,7 +27,7 @@ public class CustomMapClickListener extends MapClickListener {
 	 * selected waypoint
 	 */
 	public static CustomWaypoint selectedNode;
-	
+
 	/*
 	 * selected edge
 	 */
@@ -56,15 +58,37 @@ public class CustomMapClickListener extends MapClickListener {
 	@Override
 	public void mapClicked(GeoPosition arg0) {
 
-		Point2D clickedPoint = CustomMapClickListener.viewer.getTileFactory().geoToPixel(arg0, CustomMapClickListener.viewer.getZoom());
+		Point2D clickedPoint = CustomMapClickListener.viewer.getTileFactory().geoToPixel(arg0,
+				CustomMapClickListener.viewer.getZoom());
 		Point2D nodePoint;
 
 		// a waypoint was clicked
 		Boolean wayPointSelected = false;
 
+		wayPointSelected = checkWaypointClicked(clickedPoint, wayPointSelected);
+
+		// no node selected so check if edge selected
+		if (!wayPointSelected) {
+			checkEdgeClicked(clickedPoint);
+		}
+
+	}
+
+	/**
+	 * check if waypoint was clicked in symbolLayer
+	 * 
+	 * @param clickedPoint
+	 *            on map
+	 * @param wayPointSelected
+	 * @return
+	 */
+	public Boolean checkWaypointClicked(Point2D clickedPoint, Boolean wayPointSelected) {
+		Point2D nodePoint;
+
 		for (CustomWaypoint nodeWaypoint : CustomMapClickListener.waypoints) {
 			// transform GeoPosition to point on screen
-			nodePoint = CustomMapClickListener.viewer.getTileFactory().geoToPixel(nodeWaypoint.getPosition(), CustomMapClickListener.viewer.getZoom());
+			nodePoint = CustomMapClickListener.viewer.getTileFactory().geoToPixel(nodeWaypoint.getPosition(),
+					CustomMapClickListener.viewer.getZoom());
 
 			boolean yChecked = false;
 
@@ -93,46 +117,93 @@ public class CustomMapClickListener extends MapClickListener {
 			}
 		}
 
-		// no node selected so check if edge selected
-		if (!wayPointSelected) {
-			for (Edge edge : CustomMapClickListener.edges) {
-				// Get geo Positions of the two nodes that define the edge
-				GeoPosition startPos = new GeoPosition(edge.getNode0().getAttribute("lat"),
-						edge.getNode0().getAttribute("long"));
-				GeoPosition endPos = new GeoPosition(edge.getNode1().getAttribute("lat"),
-						edge.getNode1().getAttribute("long"));
-
-				// convert geo-coordinate to world bitmap pixel
-				Point2D startPoint = viewer.getTileFactory().geoToPixel(startPos, viewer.getZoom());
-				Point2D endPoint = viewer.getTileFactory().geoToPixel(endPos, viewer.getZoom());
-
-				Line2D.Double line = new Line2D.Double(startPoint.getX(), startPoint.getY(), endPoint.getX(),
-						endPoint.getY());
-
-				// Clicked point in 10 pixel range of line
-				if (line.ptLineDist(clickedPoint) < 10) {
-					
-					PropertiesManager.showNewDataSet(edge);
-					
-					deselectAll();
-					
-					if (!edge.hasAttribute("ui.map.selected"))
-						edge.addAttribute("ui.map.selected", true);
-					else
-						edge.changeAttribute("ui.map.selected", true);
-					
-					selectedEdge = edge;
-
-					viewer.repaint();
-					break;
-				} else {
-					if(edge.hasAttribute("ui.map.selected")){
-						edge.changeAttribute("ui.map.selected", false);
-					}
+		return wayPointSelected;
+	}
+
+	/**
+	 * check if edge was clicked in symbolLayer
+	 * 
+	 * @param clickedPoint
+	 */
+	public void checkEdgeClicked(Point2D clickedPoint) {
+
+		// max distance between clicked point and edge to select edge
+		double maxDistance = 10.0;
+
+		Edge result = null;
+
+		for (Edge edge : CustomMapClickListener.edges) {
+			// Get geo Positions of the two nodes that define the edge
+			GeoPosition startPos = new GeoPosition(edge.getNode0().getAttribute("lat"),
+					edge.getNode0().getAttribute("long"));
+			GeoPosition endPos = new GeoPosition(edge.getNode1().getAttribute("lat"),
+					edge.getNode1().getAttribute("long"));
+
+			// convert geo-coordinate to world bitmap pixel
+			Point2D startPoint = viewer.getTileFactory().geoToPixel(startPos, viewer.getZoom());
+			Point2D endPoint = viewer.getTileFactory().geoToPixel(endPos, viewer.getZoom());
+
+			// the actual edge between the points
+			Line2D.Double line = new Line2D.Double(startPoint.getX(), startPoint.getY(), endPoint.getX(),
+					endPoint.getY());
+
+			// distance between nodes
+			double distanceBetweenNodes = EdgeSelectionHelper.distance(startPoint.getX(), startPoint.getY(),
+					endPoint.getX(), endPoint.getY());
+
+			// distance between clicked point and edge
+			double distanceClickedAndEdge = line.ptLineDist(clickedPoint);
+
+			// half pi
+			double HALF_PI = Math.PI / 2;
+
+			// distance of clicked point is in range of edge selection (or is nearer then to previous selected edge)
+			if (distanceClickedAndEdge < maxDistance) {
+
+				// distance start point to clicked point
+				double distanceStartToClicked = EdgeSelectionHelper.distance(startPoint.getX(), startPoint.getY(),
+						clickedPoint.getX(), clickedPoint.getY());
+				
+				// distance end point to clicked point
+				double distanceEndToClicked = EdgeSelectionHelper.distance(endPoint.getX(), endPoint.getY(),
+						clickedPoint.getX(), clickedPoint.getY());
+
+				// square distances
+				double a2 = distanceStartToClicked * distanceStartToClicked;
+				double b2 = distanceEndToClicked * distanceEndToClicked;
+				double c2 = distanceBetweenNodes * distanceBetweenNodes;
+
+				// Calculates the inner angles off the triangle
+				double alpha = Math.acos((b2 + c2 - a2) / (2 * distanceEndToClicked * distanceBetweenNodes));
+				double beta = Math.acos((a2 + c2 - b2) / (2 * distanceStartToClicked * distanceBetweenNodes));
+
+				
+				// Check if the point is actually visually next to the edge by
+				// checking if both inner angles are less than 90°
+				if (alpha <= HALF_PI && beta <= HALF_PI) {
+					maxDistance = distanceClickedAndEdge;
+					result = edge;
 				}
 			}
 		}
 
+		// Clicked point is in range of edge selection
+		if (result != null) {
+
+			PropertiesManager.showNewDataSet(result);
+
+			deselectAll();
+
+			if (!result.hasAttribute("ui.map.selected"))
+				result.addAttribute("ui.map.selected", true);
+			else
+				result.changeAttribute("ui.map.selected", true);
+
+			selectedEdge = result;
+
+			viewer.repaint();
+		}
+
 	}
 
 	/**
@@ -140,16 +211,16 @@ public class CustomMapClickListener extends MapClickListener {
 	 */
 	public static void deselectAll() {
 		if (selectedNode != null) {
-			
+
 			selectedNode.deselect();
 			selectedNode = null;
 		}
-		if(selectedEdge != null){
-			
+		if (selectedEdge != null) {
+
 			selectedEdge.changeAttribute("ui.map.selected", false);
 			selectedEdge = null;
 		}
-		
+
 		viewer.repaint();
 	}
 

+ 39 - 27
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/WorldView.java

@@ -55,7 +55,14 @@ public class WorldView {
 	 * All edges in the WorldView
 	 */
 	public static HashSet<Edge> edges;
+	
+	/*
+	 * All painter in symbolLayer stored in a list
+	 */
+	public static List<Painter<JXMapViewer>> painters;
 
+	
+	
 	/**
 	 * private constructor to avoid instantiation
 	 */
@@ -87,39 +94,29 @@ public class WorldView {
 		// Get GeoPositions of nodes and get all waypoints created
 		MapViewFunctions.fetchGraphData(nodePositions, waypoints, edges);
 
+		if(edgePainter == null)
 		// Create a line for all edges
-		edgePainter = new EdgePainter(edges);
-
-		// Create a waypoint painter that takes all the waypoints
-		waypointPainter = new WaypointPainter<CustomWaypoint>();
-		waypointPainter.setWaypoints(waypoints);
-		waypointPainter.setRenderer(new CustomWaypointRenderer());
-
-		// Create a compound painter that uses all painters
-		List<Painter<JXMapViewer>> painters = new ArrayList<Painter<JXMapViewer>>();
-		painters.add(edgePainter);
-		painters.add(waypointPainter);
-
+			edgePainter = new EdgePainter(edges);
+		
+		if(waypointPainter == null){
+			// Create a waypoint painter that takes all the waypoints
+			waypointPainter = new WaypointPainter<CustomWaypoint>();
+			waypointPainter.setWaypoints(waypoints);
+			waypointPainter.setRenderer(new CustomWaypointRenderer());
+		}
+		
+		if(painters == null){
+			// Create a compound painter that uses all painters
+			painters = new ArrayList<Painter<JXMapViewer>>();
+			painters.add(edgePainter);
+			painters.add(waypointPainter);
+		}
+		
 		CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
 
 		// Create a TileFactoryInfo for OpenStreetMap
 		TileFactoryInfo info = new OSMTileFactoryInfo();
 
-		// try to load OpenStreesMap, when errors occur, throw and handle
-		// Exceptions
-		URL osmWebPage;
-		try {
-			// try to connect to OpenStreetMap server
-			osmWebPage = new URL(info.getBaseURL());
-			URLConnection connection = osmWebPage.openConnection();
-			connection.connect();
-
-		} catch (MalformedURLException e) {
-			// TODO add Dialog with eroor msg and stack trace
-			e.printStackTrace();
-
-		}
-
 		CustomTileFactory tileFactory = new CustomTileFactory(info);
 		if(!internMapViewer.getTileFactory().equals(tileFactory)){
 			internMapViewer.setTileFactory(tileFactory);
@@ -143,6 +140,21 @@ public class WorldView {
 		}
 
 		internMapViewer.repaint();
+		
+		// try to load OpenStreesMap, when errors occur, throw and handle
+		// Exceptions
+		URL osmWebPage;
+		try {
+			// try to connect to OpenStreetMap server
+			osmWebPage = new URL(info.getBaseURL());
+			URLConnection connection = osmWebPage.openConnection();
+			connection.connect();
+
+		} catch (MalformedURLException e) {
+			// TODO add Dialog with eroor msg and stack trace
+			e.printStackTrace();
+
+		}
 	}
 
 }

+ 4 - 0
scopviz/src/main/resources/MainWindow.fxml

@@ -127,6 +127,10 @@
                                     <Button fx:id="zoomIn" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" text="+" textAlignment="JUSTIFY" AnchorPane.bottomAnchor="45.0" AnchorPane.rightAnchor="10.0" />
                                     <Button fx:id="zoomOut" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" text="-" textAlignment="JUSTIFY" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="10.0" />
                                     <Button fx:id="centerMap" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" text="O" textAlignment="JUSTIFY" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="45.0" />
+                                    <Button fx:id="centerMap1" layoutX="10.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" text="D" textAlignment="JUSTIFY" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" />
+                                    <Button fx:id="centerMap11" layoutX="20.0" layoutY="20.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" text="S" textAlignment="JUSTIFY" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="45.0" />
+                                    <Button fx:id="centerMap111" layoutX="30.0" layoutY="30.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" text="S" textAlignment="JUSTIFY" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="80.0" />
+                                    <Button fx:id="centerMap1111" layoutX="40.0" layoutY="40.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" text="S" textAlignment="JUSTIFY" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="115.0" />
                                  </children>
                               </AnchorPane>
                             <AnchorPane SplitPane.resizableWithParent="false">