Merge branch 'develop' into 1047_Hosting_Game

This commit is contained in:
Haoming Yin
2017-07-16 21:54:08 +12:00
17 changed files with 79 additions and 142 deletions
@@ -21,9 +21,7 @@ import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Text;
import seng302.fxObjects.BoatAnnotations;
import seng302.fxObjects.BoatGroup;
import seng302.fxObjects.Wake;
import seng302.models.Colors;
import seng302.models.Yacht;
import seng302.models.mark.GateMark;
@@ -38,14 +36,8 @@ import seng302.models.stream.XMLParser;
import seng302.models.stream.XMLParser.RaceXMLObject.Limit;
import seng302.models.stream.XMLParser.RaceXMLObject.Participant;
import seng302.models.stream.packets.BoatPositionPacket;
import seng302.server.simulator.GeoUtility;
import seng302.server.simulator.mark.Position;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.PriorityBlockingQueue;
import seng302.utilities.GeoPoint;
import seng302.utilities.GeoUtility;
/**
* Created by ptg19 on 15/03/17.
@@ -208,8 +200,8 @@ public class CanvasController {
double bearingFromTopLeftToOrigin = Math
.toDegrees(Math.atan2(-topLeftPoint.getX(), topLeftPoint.getY()));
// the top left extreme
Position topLeftPos = new Position(maxLatPoint.getLatitude(), minLonPoint.getLongitude());
Position originPos = GeoUtility
GeoPoint topLeftPos = new GeoPoint(maxLatPoint.getLatitude(), minLonPoint.getLongitude());
GeoPoint originPos = GeoUtility
.getGeoCoordinate(topLeftPos, bearingFromTopLeftToOrigin, distanceFromTopLeftToOrigin);
// distance from origin corner to bottom right corner of the panel
@@ -218,7 +210,7 @@ public class CanvasController {
.pow(PANEL_WIDTH * metersPerPixelX, 2));
double bearingFromOriginToBottomRight = Math
.toDegrees(Math.atan2(PANEL_WIDTH, -PANEL_HEIGHT));
Position bottomRightPos = GeoUtility
GeoPoint bottomRightPos = GeoUtility
.getGeoCoordinate(originPos, bearingFromOriginToBottomRight,
distanceFromOriginToBottomRight);
@@ -27,7 +27,7 @@ import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import javafx.util.StringConverter;
import seng302.GeometryUtils;
import seng302.utilities.GeometryUtils;
import seng302.controllers.annotations.Annotation;
import seng302.controllers.annotations.ImportantAnnotationController;
import seng302.controllers.annotations.ImportantAnnotationDelegate;
@@ -1,26 +1,22 @@
package seng302.fxObjects;
import java.util.ArrayList;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.CacheHint;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import seng302.models.Yacht;
import seng302.GeometryUtils;
import seng302.utilities.GeometryUtils;
import seng302.controllers.CanvasController;
import seng302.models.mark.GateMark;
import seng302.models.mark.Mark;
import seng302.models.mark.SingleMark;
import seng302.models.stream.StreamParser;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
* BoatGroup is a javafx group that by default contains a graphical objects for representing a 2
* dimensional boat. It contains a single polygon for the boat, a group of lines to show it's path,
@@ -12,7 +12,6 @@ import seng302.models.mark.GateMark;
import seng302.models.mark.Mark;
import seng302.models.mark.MarkType;
import seng302.models.mark.SingleMark;
import seng302.GeometryUtils;
/**
* Grouping of javaFX objects needed to represent a Mark on screen.
@@ -1,6 +1,8 @@
package seng302.models.map;
import javafx.geometry.Point2D;
import javafx.scene.image.Image;
import seng302.utilities.GeoPoint;
import javax.net.ssl.HttpsURLConnection;
import java.net.URL;
@@ -65,10 +67,10 @@ public class CanvasMap {
private MapSize getMapSize(int zoom, Boundary boundary) {
double scale = Math.pow(2, zoom);
MapGeo geoSW = new MapGeo(boundary.getSouthLat(), boundary.getWestLng());
MapGeo geoNE = new MapGeo(boundary.getNorthLat(), boundary.getEastLng());
MapPoint pointSW = MercatorProjection.toMapPoint(geoSW);
MapPoint pointNE = MercatorProjection.toMapPoint(geoNE);
GeoPoint geoSW = new GeoPoint(boundary.getSouthLat(), boundary.getWestLng());
GeoPoint geoNE = new GeoPoint(boundary.getNorthLat(), boundary.getEastLng());
Point2D pointSW = MercatorProjection.toMapPoint(geoSW);
Point2D pointNE = MercatorProjection.toMapPoint(geoNE);
return new MapSize(Math.abs(pointNE.getX() - pointSW.getX()) * scale,
Math.abs(pointNE.getY() - pointSW.getY()) * scale);
}
@@ -1,31 +0,0 @@
package seng302.models.map;
/**
* A class represent Geo location (latitude, longitude).
* Created by Haoming on 15/5/2017
*/
class MapGeo {
private double lat, lng;
MapGeo(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
double getLat() {
return lat;
}
void setLat(double lat) {
this.lat = lat;
}
double getLng() {
return lng;
}
void setLng(double lng) {
this.lng = lng;
}
}
@@ -1,31 +0,0 @@
package seng302.models.map;
/**
* A class represent euclidean planar point (x, y)
* Created by Haoming on 15/5/2017
*/
class MapPoint {
private double x, y;
MapPoint(double x, double y) {
this.x = x;
this.y = y;
}
double getX() {
return x;
}
void setX(double x) {
this.x = x;
}
double getY() {
return y;
}
void setY(double y) {
this.y = y;
}
}
@@ -1,5 +1,8 @@
package seng302.models.map;
import javafx.geometry.Point2D;
import seng302.utilities.GeoPoint;
/**
* An utility class useful to convert between Geo locations and Mercator projection
* planar coordinates.
@@ -22,31 +25,31 @@ public class MercatorProjection {
/**
* Projects a Geo Location (lat, lng) on a planar
* @param geo MapGeo (lat, lng) location to be projected
* @return the projection GeoPoint (x, y) on planar
* @param geo GeoPoint (lat, lng) location to be projected
* @return the projection Point2D (x, y) on planar
*/
public static MapPoint toMapPoint(MapGeo geo) {
MapPoint point = new MapPoint(0, 0);
MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
point.setX(origin.getX() + geo.getLng() * pixelsPerLngDegree);
public static Point2D toMapPoint(GeoPoint geo) {
double x, y;
Point2D origin = new Point2D(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
x = (origin.getX() + geo.getLng() * pixelsPerLngDegree);
// NOTE(appleton): Truncating to 0.9999 effectively limits latitude to
// 89.189. This is about a third of a tile past the edge of the world tile.
double sinY = bound(Math.sin(Math.toRadians(geo.getLat())));
point.setY(origin.getY() + 0.5 * Math.log((1 + sinY) / (1 - sinY)) * (-pixelsPerLngRadian));
return point;
y = origin.getY() + 0.5 * Math.log((1 + sinY) / (1 - sinY)) * (-pixelsPerLngRadian);
return new Point2D(x, y);
}
/**
* Converts the planar projection (x, y) back to Geo Location (lat, lng)
* @param point MapPoint (x, y) to be converted back
* @param point Point2D (x, y) to be converted back
* @return the original Geo location converted from the given projection point
*/
public static MapGeo toMapGeo(MapPoint point) {
MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
public static GeoPoint toMapGeo(Point2D point) {
Point2D origin = new Point2D(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
double lng = (point.getX() - origin.getX()) / pixelsPerLngDegree;
double latRadians = (point.getY() - origin.getY()) / (-pixelsPerLngRadian);
double lat = Math.toDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2.0);
return new MapGeo(lat, lng);
return new GeoPoint(lat, lng);
}
}
@@ -1,7 +1,8 @@
package seng302.server.simulator;
import seng302.server.simulator.mark.Corner;
import seng302.server.simulator.mark.Position;
import seng302.utilities.GeoPoint;
import seng302.utilities.GeoUtility;
public class Boat {
@@ -29,8 +30,8 @@ public class Boat {
*/
public void move(double heading, double duration) {
Double distance = speed * duration / 1000000; // convert mm to meter
Position originPos = new Position(lat, lng);
Position newPos = GeoUtility.getGeoCoordinate(originPos, heading, distance);
GeoPoint originPos = new GeoPoint(lat, lng);
GeoPoint newPos = GeoUtility.getGeoCoordinate(originPos, heading, distance);
this.lat = newPos.getLat();
this.lng = newPos.getLng();
}
@@ -2,8 +2,9 @@ package seng302.server.simulator;
import seng302.server.simulator.mark.Corner;
import seng302.server.simulator.mark.Mark;
import seng302.server.simulator.mark.Position;
import seng302.server.simulator.parsers.RaceParser;
import seng302.utilities.GeoPoint;
import seng302.utilities.GeoUtility;
import java.util.List;
import java.util.Observable;
@@ -77,8 +78,8 @@ public class Simulator extends Observable implements Runnable {
boat.move(boat.getLastPassedCorner().getBearingToNextCorner(), duration);
Position boatPos = new Position(boat.getLat(), boat.getLng());
Position lastMarkPos = boat.getLastPassedCorner().getCompoundMark().getMark1();
GeoPoint boatPos = new GeoPoint(boat.getLat(), boat.getLng());
GeoPoint lastMarkPos = boat.getLastPassedCorner().getCompoundMark().getMark1();
double distanceFromLastMark = GeoUtility.getDistance(boatPos, lastMarkPos);
// if a boat passes its heading mark
@@ -94,13 +95,13 @@ public class Simulator extends Observable implements Runnable {
}
// move compensate distance for the mark just passed
Position pos = GeoUtility.getGeoCoordinate(
GeoPoint pos = GeoUtility.getGeoCoordinate(
boat.getLastPassedCorner().getCompoundMark().getMark1(),
boat.getLastPassedCorner().getBearingToNextCorner(),
compensateDistance);
boat.setLat(pos.getLat());
boat.setLng(pos.getLng());
distanceFromLastMark = GeoUtility.getDistance(new Position(boat.getLat(), boat.getLng()),
distanceFromLastMark = GeoUtility.getDistance(new GeoPoint(boat.getLat(), boat.getLng()),
boat.getLastPassedCorner().getCompoundMark().getMark1());
}
}
@@ -1,10 +1,12 @@
package seng302.server.simulator.mark;
import seng302.utilities.GeoPoint;
/**
* An abstract class to represent general marks
* Created by Haoming Yin (hyi25) on 17/3/17.
*/
public class Mark extends Position {
public class Mark extends GeoPoint {
private int seqID;
private String name;
@@ -22,7 +24,7 @@ public class Mark extends Position {
*/
@Override
public String toString() {
return String.format("Mark%d: %s, source: %d, lat: %f, lng: %f", seqID, name, sourceID, lat, lng);
return String.format("Mark%d: %s, source: %d, lat: %f, lng: %f", seqID, name, sourceID, getLat(), getLng());
}
public int getSeqID() {
@@ -1,18 +1,18 @@
package seng302.server.simulator.mark;
package seng302.utilities;
public class Position {
/**
* A class represent Geo location (latitude, longitude).
* Created by Haoming on 15/5/2017
*/
public class GeoPoint {
double lat, lng;
public Position(double lat, double lng) {
public GeoPoint(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
public String toString() {
return String.format("Position at lat:%f lng:%f.", lat, lng);
}
public double getLat() {
return lat;
}
@@ -1,6 +1,4 @@
package seng302.server.simulator;
import seng302.server.simulator.mark.Position;
package seng302.utilities;
public class GeoUtility {
@@ -13,7 +11,7 @@ public class GeoUtility {
* @param p2 second geographical position
* @return the distance in meter between two points in meters
*/
public static Double getDistance(Position p1, Position p2) {
public static Double getDistance(GeoPoint p1, GeoPoint p2) {
double dLat = Math.toRadians(p2.getLat() - p1.getLat());
double dLon = Math.toRadians(p2.getLng() - p1.getLng());
@@ -42,7 +40,7 @@ public class GeoUtility {
* ( Baghdad) to 35°N,135°E ( Osaka), you would start on a heading of 60°
* and end up on a heading of 120°
*/
public static Double getBearing(Position p1, Position p2) {
public static Double getBearing(GeoPoint p1, GeoPoint p2) {
double dLon = Math.toRadians(p2.getLng() - p1.getLng());
@@ -64,7 +62,7 @@ public class GeoUtility {
* @param distance the distance in meter, from original position to the new position
* @return the new position
*/
public static Position getGeoCoordinate(Position origin, Double bearing, Double distance) {
public static GeoPoint getGeoCoordinate(GeoPoint origin, Double bearing, Double distance) {
double b = Math.toRadians(bearing); // bearing to radians
double d = distance / 1000.0; // distance to km
@@ -77,6 +75,6 @@ public class GeoUtility {
+ Math.atan2(Math.sin(b) * Math.sin(d / EARTH_RADIUS) * Math.cos(originLat),
Math.cos(d / EARTH_RADIUS) - Math.sin(originLat) * Math.sin(endLat));
return new Position(Math.toDegrees(endLat), Math.toDegrees(endLng));
return new GeoPoint(Math.toDegrees(endLat), Math.toDegrees(endLng));
}
}
@@ -1,4 +1,4 @@
package seng302;
package seng302.utilities;
import javafx.geometry.Point2D;
+1
View File
@@ -3,6 +3,7 @@ package seng302;
import javafx.geometry.Point2D;
import org.junit.Before;
import org.junit.Test;
import seng302.utilities.GeometryUtils;
import static org.junit.Assert.*;
@@ -1,6 +1,9 @@
package seng302.models.map;
import org.junit.Test;
import seng302.utilities.GeoPoint;
import java.awt.geom.Point2D;
import static org.junit.Assert.*;
@@ -11,30 +14,30 @@ import static org.junit.Assert.*;
public class MercatorProjectionTest {
@Test
public void toMapPoint() throws Exception {
MapGeo geo1 = new MapGeo(12.485394, 19.38947);
MapPoint actualPoint1 = MercatorProjection.toMapPoint(geo1);
MapPoint expectedPoint1 = new MapPoint(141.78806755555556, 119.0503853635612);
GeoPoint geo1 = new GeoPoint(12.485394, 19.38947);
javafx.geometry.Point2D actualPoint1 = MercatorProjection.toMapPoint(geo1);
javafx.geometry.Point2D expectedPoint1 = new javafx.geometry.Point2D(141.78806755555556, 119.0503853635612);
assertEquals(expectedPoint1.getX(), actualPoint1.getX(), 0.0001);
assertEquals(expectedPoint1.getY(), actualPoint1.getY(), 0.0001);
MapGeo geo2 = new MapGeo(77.456432, -23.456462);
MapPoint actualPoint2 = MercatorProjection.toMapPoint(geo2);
MapPoint expectedPoint2 = new MapPoint(111.31984924444444, 38.03143323746788);
GeoPoint geo2 = new GeoPoint(77.456432, -23.456462);
javafx.geometry.Point2D actualPoint2 = MercatorProjection.toMapPoint(geo2);
javafx.geometry.Point2D expectedPoint2 = new javafx.geometry.Point2D(111.31984924444444, 38.03143323746788);
assertEquals(expectedPoint2.getX(), actualPoint2.getX(), 0.0001);
assertEquals(expectedPoint2.getY(), actualPoint2.getY(), 0.0001);
}
@Test
public void toMapGeo() throws Exception {
MapPoint point1 = new MapPoint(123.1234, 25.4565);
MapGeo actualGeo1 = MercatorProjection.toMapGeo(point1);
MapGeo expectedGeo1 = new MapGeo(80.77043127275441, -6.857718749999995);
javafx.geometry.Point2D point1 = new javafx.geometry.Point2D(123.1234, 25.4565);
GeoPoint actualGeo1 = MercatorProjection.toMapGeo(point1);
GeoPoint expectedGeo1 = new GeoPoint(80.77043127275441, -6.857718749999995);
assertEquals(expectedGeo1.getLat(), actualGeo1.getLat(), 0.0001);
assertEquals(expectedGeo1.getLng(), actualGeo1.getLng(), 0.0001);
MapPoint point2 = new MapPoint(1.235, 255.4565);
MapGeo actualGeo2 = MercatorProjection.toMapGeo(point2);
MapGeo expectedGeo2 = new MapGeo(-84.98475532898011, -178.26328125);
javafx.geometry.Point2D point2 = new javafx.geometry.Point2D(1.235, 255.4565);
GeoPoint actualGeo2 = MercatorProjection.toMapGeo(point2);
GeoPoint expectedGeo2 = new GeoPoint(-84.98475532898011, -178.26328125);
assertEquals(expectedGeo2.getLat(), actualGeo2.getLat(), 0.0001);
assertEquals(expectedGeo2.getLng(), actualGeo2.getLng(), 0.0001);
}
@@ -1,7 +1,8 @@
package seng302.server.simulator;
import org.junit.Test;
import seng302.server.simulator.mark.Position;
import seng302.utilities.GeoPoint;
import seng302.utilities.GeoUtility;
import static org.junit.Assert.*;
@@ -11,10 +12,10 @@ import static org.junit.Assert.*;
*/
public class GeoUtilityTest {
private Position p1 = new Position(57.670333, 11.827833);
private Position p2 = new Position(57.671524, 11.844495);
private Position p3 = new Position(57.670822, 11.843392);
private Position p4 = new Position(25.694829, 98.392049);
private GeoPoint p1 = new GeoPoint(57.670333, 11.827833);
private GeoPoint p2 = new GeoPoint(57.671524, 11.844495);
private GeoPoint p3 = new GeoPoint(57.670822, 11.843392);
private GeoPoint p4 = new GeoPoint(25.694829, 98.392049);
private double toleranceRate = 0.01;
@@ -54,7 +55,7 @@ public class GeoUtilityTest {
@Test
public void getGeoCoordinate() throws Exception {
Position expected, actual;
GeoPoint expected, actual;
actual = GeoUtility.getGeoCoordinate(p1, 82.0, 1000.0);
expected = p2;