|
|
|
|
@ -1,10 +1,12 @@
|
|
|
|
|
package seng302.Model;
|
|
|
|
|
|
|
|
|
|
import javafx.util.Pair;
|
|
|
|
|
import org.geotools.referencing.GeodeticCalculator;
|
|
|
|
|
import org.opengis.geometry.DirectPosition;
|
|
|
|
|
import seng302.Constants;
|
|
|
|
|
|
|
|
|
|
import java.awt.geom.Point2D;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@ -292,5 +294,226 @@ public class GPSCoordinate {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Takes a list of GPS coordinates describing a course boundary, and "shrinks" it inwards by 50m.
|
|
|
|
|
* @param boundary The boundary of course.
|
|
|
|
|
* @return A copy of the course boundary list, shrunk inwards by 50m.
|
|
|
|
|
*/
|
|
|
|
|
public static List<GPSCoordinate> getShrinkBoundary(List<GPSCoordinate> boundary) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double shrinkDistance = 50d;
|
|
|
|
|
List<GPSCoordinate> shrunkBoundary = new ArrayList<>(boundary.size());
|
|
|
|
|
//This is a list of edges that have been shrunk/shifted inwards.
|
|
|
|
|
List<Pair<GPSCoordinate, GPSCoordinate>> shrunkEdges = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//We need to invert some of our opertations depending if the boundary is clockwise or anti-clockwise.
|
|
|
|
|
boolean isClockwise = GPSCoordinate.isClockwisePolygon(boundary);
|
|
|
|
|
double clockwiseScaleFactor = 0;
|
|
|
|
|
|
|
|
|
|
if (isClockwise) {
|
|
|
|
|
clockwiseScaleFactor = 1;
|
|
|
|
|
} else {
|
|
|
|
|
clockwiseScaleFactor = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starting at a vertex, face anti-clockwise along an adjacent edge.
|
|
|
|
|
Replace the edge with a new, parallel edge placed at distance d to the "left" of the old one.
|
|
|
|
|
Repeat for all edges.
|
|
|
|
|
Find the intersections of the new edges to get the new vertices.
|
|
|
|
|
Detect if you've become a crossed polynomial and decide what to do about it. Probably add a new vertex at the crossing-point and get rid of some old ones. I'm not sure whether there's a better way to detect this than just to compare every pair of non-adjacent edges to see if their intersection lies between both pairs of vertices.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//For the first (size-1) adjacent pairs.
|
|
|
|
|
for (int i = 0; i < (boundary.size() - 1); i++) {
|
|
|
|
|
|
|
|
|
|
//Get the points.
|
|
|
|
|
GPSCoordinate firstPoint = boundary.get(i);
|
|
|
|
|
GPSCoordinate secondPoint = boundary.get(i + 1);
|
|
|
|
|
|
|
|
|
|
//Get the bearing between two adjacent points.
|
|
|
|
|
Bearing bearing = GPSCoordinate.calculateBearing(firstPoint, secondPoint);
|
|
|
|
|
|
|
|
|
|
//Calculate angle perpendicular to bearing.
|
|
|
|
|
Bearing perpindicularBearing = Bearing.fromDegrees(bearing.degrees() + (90d * clockwiseScaleFactor));
|
|
|
|
|
|
|
|
|
|
//Translate both first and second point by 50m, using this bearing. These form our inwards shifted edge.
|
|
|
|
|
GPSCoordinate firstPointTranslated = GPSCoordinate.calculateNewPosition(firstPoint, shrinkDistance, Azimuth.fromBearing(perpindicularBearing));
|
|
|
|
|
GPSCoordinate secondPointTranslated = GPSCoordinate.calculateNewPosition(secondPoint, shrinkDistance, Azimuth.fromBearing(perpindicularBearing));
|
|
|
|
|
|
|
|
|
|
//Add edge to list.
|
|
|
|
|
shrunkEdges.add(new Pair<>(firstPointTranslated, secondPointTranslated));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//For the final adjacent pair, between the last and first point.
|
|
|
|
|
//Get the points.
|
|
|
|
|
GPSCoordinate firstPoint = boundary.get(boundary.size() - 1);
|
|
|
|
|
GPSCoordinate secondPoint = boundary.get(0);
|
|
|
|
|
|
|
|
|
|
//Get the bearing between two adjacent points.
|
|
|
|
|
Bearing bearing = GPSCoordinate.calculateBearing(firstPoint, secondPoint);
|
|
|
|
|
|
|
|
|
|
//Calculate angle perpendicular to bearing.
|
|
|
|
|
Bearing perpindicularBearing = Bearing.fromDegrees(bearing.degrees() + (90d * clockwiseScaleFactor));
|
|
|
|
|
|
|
|
|
|
//Translate both first and second point by 50m, using this bearing. These form our inwards shifted edge.
|
|
|
|
|
GPSCoordinate firstPointTranslated = GPSCoordinate.calculateNewPosition(firstPoint, shrinkDistance, Azimuth.fromBearing(perpindicularBearing));
|
|
|
|
|
GPSCoordinate secondPointTranslated = GPSCoordinate.calculateNewPosition(secondPoint, shrinkDistance, Azimuth.fromBearing(perpindicularBearing));
|
|
|
|
|
|
|
|
|
|
//Add edge to list.
|
|
|
|
|
shrunkEdges.add(new Pair<>(firstPointTranslated, secondPointTranslated));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//We now have a list of edges that have been shifted inwards.
|
|
|
|
|
//We need to find the intersections between adjacent vertices in our edge list. E.g., intersection between edge1-right, and edge2-left.
|
|
|
|
|
|
|
|
|
|
//For the first (size-1) adjacent pairs.
|
|
|
|
|
for (int i = 0; i < (shrunkEdges.size() - 1); i++) {
|
|
|
|
|
|
|
|
|
|
//Get the pair of adjacent edges.
|
|
|
|
|
Pair<GPSCoordinate, GPSCoordinate> edge1 = shrunkEdges.get(i);
|
|
|
|
|
Pair<GPSCoordinate, GPSCoordinate> edge2 = shrunkEdges.get(i + 1);
|
|
|
|
|
|
|
|
|
|
//Get the x and y coordinates of first edge.
|
|
|
|
|
double x1 = edge1.getKey().getLongitude();
|
|
|
|
|
double x2 = edge1.getValue().getLongitude();
|
|
|
|
|
double y1 = edge1.getKey().getLatitude();
|
|
|
|
|
double y2 = edge1.getValue().getLatitude();
|
|
|
|
|
|
|
|
|
|
//Get the x and y coordinates of second edge.
|
|
|
|
|
double x3 = edge2.getKey().getLongitude();
|
|
|
|
|
double x4 = edge2.getValue().getLongitude();
|
|
|
|
|
double y3 = edge2.getKey().getLatitude();
|
|
|
|
|
double y4 = edge2.getValue().getLatitude();
|
|
|
|
|
|
|
|
|
|
//Find the equations for both edges.
|
|
|
|
|
// y = a*x + b
|
|
|
|
|
//First equation.
|
|
|
|
|
double a1 = (y2 - y1) / (x2 - x1);
|
|
|
|
|
double b1 = y1 - a1 * x1;
|
|
|
|
|
|
|
|
|
|
//Second equation.
|
|
|
|
|
double a2 = (y4 - y3) / (x4 - x3);
|
|
|
|
|
double b2 = y3 - a2 * x3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Find intersecting x coordinate.
|
|
|
|
|
// a1 * x + b1 = a2 * x + b2
|
|
|
|
|
double x0 = -(b1 - b2) / (a1 - a2);
|
|
|
|
|
//Find intersecting y coordinate.
|
|
|
|
|
double y0 = x0 * a1 + b1;
|
|
|
|
|
|
|
|
|
|
//Add this to shrunk boundary list.
|
|
|
|
|
GPSCoordinate coordinate = new GPSCoordinate(y0, x0);
|
|
|
|
|
shrunkBoundary.add(coordinate);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//For the final adjacent pair, between the last and first point.
|
|
|
|
|
//Get the pair of adjacent edges.
|
|
|
|
|
Pair<GPSCoordinate, GPSCoordinate> edge1 = shrunkEdges.get(shrunkEdges.size() - 1);
|
|
|
|
|
Pair<GPSCoordinate, GPSCoordinate> edge2 = shrunkEdges.get(0);
|
|
|
|
|
|
|
|
|
|
//Get the x and y coordinates of first edge.
|
|
|
|
|
double x1 = edge1.getKey().getLongitude();
|
|
|
|
|
double x2 = edge1.getValue().getLongitude();
|
|
|
|
|
double y1 = edge1.getKey().getLatitude();
|
|
|
|
|
double y2 = edge1.getValue().getLatitude();
|
|
|
|
|
|
|
|
|
|
//Get the x and y coordinates of second edge.
|
|
|
|
|
double x3 = edge2.getKey().getLongitude();
|
|
|
|
|
double x4 = edge2.getValue().getLongitude();
|
|
|
|
|
double y3 = edge2.getKey().getLatitude();
|
|
|
|
|
double y4 = edge2.getValue().getLatitude();
|
|
|
|
|
|
|
|
|
|
//Find the equations for both edges.
|
|
|
|
|
// y = a*x + b
|
|
|
|
|
//First equation.
|
|
|
|
|
double a1 = (y2 - y1) / (x2 - x1);
|
|
|
|
|
double b1 = y1 - a1 * x1;
|
|
|
|
|
|
|
|
|
|
//Second equation.
|
|
|
|
|
double a2 = (y4 - y3) / (x4 - x3);
|
|
|
|
|
double b2 = y3 - a2 * x3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Find intersecting x coordinate.
|
|
|
|
|
// a1 * x + b1 = a2 * x + b2
|
|
|
|
|
double x0 = -(b1 - b2) / (a1 - a2);
|
|
|
|
|
//Find intersecting y coordinate.
|
|
|
|
|
double y0 = x0 * a1 + b1;
|
|
|
|
|
|
|
|
|
|
//Add this to shrunk boundary list.
|
|
|
|
|
GPSCoordinate coordinate = new GPSCoordinate(y0, x0);
|
|
|
|
|
shrunkBoundary.add(coordinate);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return shrunkBoundary;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if a list of coordinates describes a boundary polygon in clockwise or anti-clockwise order.
|
|
|
|
|
* @param boundary The list of coodinates.
|
|
|
|
|
* @return True if clockwise, false if anti-clockwise.
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isClockwisePolygon(List<GPSCoordinate> boundary) {
|
|
|
|
|
|
|
|
|
|
/** From https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order
|
|
|
|
|
* sum all pairs (x2 − x1)(y2 + y1)
|
|
|
|
|
point[0] = (5,0) edge[0]: (6-5)(4+0) = 4
|
|
|
|
|
point[1] = (6,4) edge[1]: (4-6)(5+4) = -18
|
|
|
|
|
point[2] = (4,5) edge[2]: (1-4)(5+5) = -30
|
|
|
|
|
point[3] = (1,5) edge[3]: (1-1)(0+5) = 0
|
|
|
|
|
point[4] = (1,0) edge[4]: (5-1)(0+0) = 0
|
|
|
|
|
---
|
|
|
|
|
-44 counter-clockwise
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
double sum = 0;
|
|
|
|
|
|
|
|
|
|
//For the first (size-1) adjacent pairs.
|
|
|
|
|
for (int i = 0; i < (boundary.size() - 1); i++) {
|
|
|
|
|
|
|
|
|
|
//Get the points.
|
|
|
|
|
GPSCoordinate firstPoint = boundary.get(i);
|
|
|
|
|
GPSCoordinate secondPoint = boundary.get(i + 1);
|
|
|
|
|
|
|
|
|
|
double xDelta = secondPoint.getLongitude() - firstPoint.getLongitude();
|
|
|
|
|
double ySum = secondPoint.getLatitude() + firstPoint.getLatitude();
|
|
|
|
|
|
|
|
|
|
double product = xDelta * ySum;
|
|
|
|
|
|
|
|
|
|
sum += product;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//For the final adjacent pair, between the last and first point.
|
|
|
|
|
//Get the points.
|
|
|
|
|
GPSCoordinate firstPoint = boundary.get(boundary.size() - 1);
|
|
|
|
|
GPSCoordinate secondPoint = boundary.get(0);
|
|
|
|
|
|
|
|
|
|
double xDelta = secondPoint.getLongitude() - firstPoint.getLongitude();
|
|
|
|
|
double ySum = secondPoint.getLatitude() + firstPoint.getLatitude();
|
|
|
|
|
|
|
|
|
|
double product = xDelta * ySum;
|
|
|
|
|
|
|
|
|
|
sum += product;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//sum > 0 is clockwise, sum < 0 is anticlockwise.
|
|
|
|
|
return sum > 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|