Tidied code, added MidPoint to CompoundMark class

Compound Mark class is now constructed with a list of marks.
A mid point is created on its construction for use in Geo Calculations

#story[1124] #pair[wmu16, hyi25]
This commit is contained in:
William Muir
2017-08-10 13:58:32 +12:00
parent abb15f6edf
commit 9c79897e01
6 changed files with 87 additions and 113 deletions
@@ -79,17 +79,18 @@ public class CourseParser extends FileParser {
if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node; Element e = (Element) node;
Integer markID = Integer.valueOf(e.getAttribute("CompoundMarkID")); Integer markID = Integer.valueOf(e.getAttribute("CompoundMarkID"));
String name = e.getAttribute("Name"); String name = e.getAttribute("Name");
CompoundMark cMark = new CompoundMark(markID, name);
NodeList marks = e.getElementsByTagName("Mark"); NodeList marks = e.getElementsByTagName("Mark");
List<Mark> subMarks = new ArrayList<>();
for (int i = 0; i < marks.getLength(); i++) { for (int i = 0; i < marks.getLength(); i++) {
Mark mark = getMark(marks.item(i)); Mark mark = getMark(marks.item(i));
if (mark != null) if (mark != null) {
cMark.addSubMarks(mark); subMarks.add(mark);
} }
return cMark; }
return new CompoundMark(markID, name, subMarks);
} }
System.out.println("Failed to create compound mark."); System.out.println("Failed to create compound mark.");
return null; return null;
+37 -58
View File
@@ -64,8 +64,7 @@ public class Yacht {
//MARK ROUNDING INFO //MARK ROUNDING INFO
private GeoPoint lastLocation; //For purposes of mark rounding calculations private GeoPoint lastLocation; //For purposes of mark rounding calculations
private Boolean hasEnteredRoundingZone; //The distance that the boat must be from the mark to round private Boolean hasEnteredRoundingZone; //The distance that the boat must be from the mark to round
private Boolean hasPassedFirstLine; //The line extrapolated from the next mark to the current mark private Boolean hasPassedLine;
private Boolean hasPassedSecondLine;
private Boolean hasPassedThroughGate; private Boolean hasPassedThroughGate;
private Boolean finishedRace; private Boolean finishedRace;
@@ -93,8 +92,7 @@ public class Yacht {
this.velocity = 0d; //in mms-1 this.velocity = 0d; //in mms-1
this.hasEnteredRoundingZone = false; this.hasEnteredRoundingZone = false;
this.hasPassedFirstLine = false; this.hasPassedLine = false;
this.hasPassedSecondLine = false;
this.hasPassedThroughGate = false; this.hasPassedThroughGate = false;
this.finishedRace = false; this.finishedRace = false;
} }
@@ -193,13 +191,13 @@ public class Yacht {
* @param currentMark The current gate * @param currentMark The current gate
*/ */
private void checkStartLineCrossing(CompoundMark currentMark) { private void checkStartLineCrossing(CompoundMark currentMark) {
Integer crossedLine = GeoUtility.checkCrossedLine(currentMark.getSubMark(1), Mark mark1 = currentMark.getSubMark(1);
currentMark.getSubMark(2), lastLocation, location); Mark mark2 = currentMark.getSubMark(2);
if (crossedLine > 0) {
CompoundMark nextMark = GameState.getMarkOrder().getNextMark(currentMarkSeqID); CompoundMark nextMark = GameState.getMarkOrder().getNextMark(currentMarkSeqID);
Boolean isClockwiseCross = GeoUtility.isClockwise(currentMark.getSubMark(1),
currentMark.getSubMark(2), Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
nextMark.getSubMark(1)); if (crossedLine > 0) {
Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, nextMark.getMidPoint());
if (crossedLine == 2 && isClockwiseCross || crossedLine == 1 && !isClockwiseCross) { if (crossedLine == 2 && isClockwiseCross || crossedLine == 1 && !isClockwiseCross) {
currentMarkSeqID++; currentMarkSeqID++;
logMarkRounding(currentMark); logMarkRounding(currentMark);
@@ -211,49 +209,31 @@ public class Yacht {
/** /**
* This algorithm checks for mark rounding. And increments the currentMarSeqID number attribute * This algorithm checks for mark rounding. And increments the currentMarSeqID number attribute
* of the yacht if so. * of the yacht if so.
* The algorithm works by using the last mark, the current mark, the next mark and the change in * A visual representation of this algorithm can be seen on the Wiki under
* boats location, like so:
* -Condition 1:
* The boat has entered the mark rounding distance
* -Condition 2:
* The boat has passed the line extending from the last mark to the current mark
* -Condition 3:
* The boat has passed the line extending from the next mark to the current mark
*
* A more visual representation of this algorithm can be seen on the Wiki under
* 'mark passing algorithm' * 'mark passing algorithm'
*/ */
private void checkMarkRounding(CompoundMark currentMark) { private void checkMarkRounding(CompoundMark currentMark) {
distanceToCurrentMark = calcDistanceToCurrentMark(); distanceToCurrentMark = calcDistanceToCurrentMark();
CompoundMark nextMark = GameState.getMarkOrder().getNextMark(currentMarkSeqID); GeoPoint nextPoint = GameState.getMarkOrder().getNextMark(currentMarkSeqID).getMidPoint();
CompoundMark prevMark = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID); GeoPoint prevPoint = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID)
.getMidPoint();
GeoPoint midPoint = GeoUtility.getDirtyMidPoint(nextPoint, prevPoint);
//1 TEST FOR ENTERING THE ROUNDING DISTANCE //1 TEST FOR ENTERING THE ROUNDING DISTANCE
if (distanceToCurrentMark < ROUNDING_DISTANCE) { if (distanceToCurrentMark < ROUNDING_DISTANCE) {
hasEnteredRoundingZone = true; hasEnteredRoundingZone = true;
} }
//If the current mark is a gate mark we need to check both its marks for rounding, thus //In case current mark is a gate, loop through all marks just in case
//we loop
for (Mark thisCurrentMark : currentMark.getMarks()) { for (Mark thisCurrentMark : currentMark.getMarks()) {
//2 TEST FOR CROSSING NEXT - CURRENT LINE FIRST if (GeoUtility.isPointInTriangle(lastLocation, location, midPoint, thisCurrentMark)) {
if (GeoUtility hasPassedLine = true;
.isPointInTriangle(lastLocation, location, nextMark.getMarks().get(0),
thisCurrentMark)) {
hasPassedFirstLine = true;
}
//3 TEST FOR CROSSING PREV - CURRENT LINE SECOND
if (GeoUtility
.isPointInTriangle(lastLocation, location, prevMark.getMarks().get(0),
thisCurrentMark)) {
hasPassedSecondLine = true;
} }
} }
if (hasPassedSecondLine && hasPassedFirstLine && hasEnteredRoundingZone) { if (hasPassedLine && hasEnteredRoundingZone) {
currentMarkSeqID++; currentMarkSeqID++;
hasPassedFirstLine = false; hasPassedLine = false;
hasPassedSecondLine = false;
hasEnteredRoundingZone = false; hasEnteredRoundingZone = false;
hasPassedThroughGate = false; hasPassedThroughGate = false;
logMarkRounding(currentMark); logMarkRounding(currentMark);
@@ -267,29 +247,28 @@ public class Yacht {
* @param currentMark The current gate * @param currentMark The current gate
*/ */
private void checkGateRounding(CompoundMark currentMark) { private void checkGateRounding(CompoundMark currentMark) {
Integer crossedLine = GeoUtility.checkCrossedLine(currentMark.getSubMark(1), Mark mark1 = currentMark.getSubMark(1);
currentMark.getSubMark(2), lastLocation, location); Mark mark2 = currentMark.getSubMark(2);
CompoundMark prevMark = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID);
CompoundMark nextMark = GameState.getMarkOrder().getNextMark(currentMarkSeqID);
Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
//We have crossed the line //We have crossed the line
if (crossedLine > 0) { if (crossedLine > 0) {
CompoundMark prevMark = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID); Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
Boolean isClockwiseCross = GeoUtility.isClockwise(currentMark.getSubMark(1),
currentMark.getSubMark(2), //Check we cross the line in the correct direction
prevMark.getSubMark(1));
if (crossedLine == 1 && isClockwiseCross || crossedLine == 2 && !isClockwiseCross) { if (crossedLine == 1 && isClockwiseCross || crossedLine == 2 && !isClockwiseCross) {
hasPassedThroughGate = true; hasPassedThroughGate = true;
} }
} }
Boolean prevMarkSide = GeoUtility.isClockwise(currentMark.getSubMark(1), Boolean prevMarkSide = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
currentMark.getSubMark(2), Boolean nextMarkSide = GeoUtility.isClockwise(mark1, mark2, nextMark.getMidPoint());
GameState.getMarkOrder().getPreviousMark(currentMarkSeqID).getSubMark(1));
Boolean nextMarkSide = GeoUtility.isClockwise(currentMark.getSubMark(1),
currentMark.getSubMark(2),
GameState.getMarkOrder().getNextMark(currentMarkSeqID).getSubMark(1));
if (hasPassedThroughGate) { if (hasPassedThroughGate) {
//Check if we need to round this gate after passing through
if (prevMarkSide == nextMarkSide) { if (prevMarkSide == nextMarkSide) {
checkMarkRounding(currentMark); checkMarkRounding(currentMark);
} else { } else {
@@ -300,18 +279,18 @@ public class Yacht {
} }
/** /**
* If we pass the finish gate in the correct direction // TODO: 8/08/17 wmu16 - do something * If we pass the finish gate in the correct direction
* *
* @param currentMark The current gate * @param currentMark The current gate
*/ */
private void checkFinishLineCrossing(CompoundMark currentMark) { private void checkFinishLineCrossing(CompoundMark currentMark) {
Integer crossedLine = GeoUtility.checkCrossedLine(currentMark.getSubMark(1), Mark mark1 = currentMark.getSubMark(1);
currentMark.getSubMark(2), lastLocation, location); Mark mark2 = currentMark.getSubMark(2);
CompoundMark prevMark = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID);
Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
if (crossedLine > 0) { if (crossedLine > 0) {
CompoundMark previousMark = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID); Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
Boolean isClockwiseCross = GeoUtility.isClockwise(currentMark.getSubMark(1),
currentMark.getSubMark(2),
previousMark.getSubMark(1));
if (crossedLine == 1 && isClockwiseCross || crossedLine == 2 && !isClockwiseCross) { if (crossedLine == 1 && isClockwiseCross || crossedLine == 2 && !isClockwiseCross) {
currentMarkSeqID++; currentMarkSeqID++;
finishedRace = true; finishedRace = true;
@@ -1,8 +1,9 @@
package seng302.model.mark; package seng302.model.mark;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import seng302.model.GeoPoint;
import seng302.utilities.GeoUtility;
public class CompoundMark { public class CompoundMark {
@@ -10,18 +11,17 @@ public class CompoundMark {
private String name; private String name;
private List<Mark> marks = new ArrayList<>(); private List<Mark> marks = new ArrayList<>();
private GeoPoint midPoint;
public CompoundMark(int markID, String name) { public CompoundMark(int markID, String name, List<Mark> marks) {
this.compoundMarkId = markID; this.compoundMarkId = markID;
this.name = name; this.name = name;
}
public void addSubMarks(Mark... marks) {
this.marks.addAll(Arrays.asList(marks));
}
public void addSubMarks(List<Mark> marks) {
this.marks.addAll(marks); this.marks.addAll(marks);
if (marks.size() > 1) {
this.midPoint = GeoUtility.getDirtyMidPoint(marks.get(0), marks.get(1));
} else {
this.midPoint = marks.get(0);
}
} }
/** /**
@@ -68,6 +68,16 @@ public class CompoundMark {
} }
} }
/**
* NOTE: This is a 'dirty' mid point as it is simply calculated as an xy point would be.
* NO CHECKING FOR LAT / LNG WRAPPING IS DONE IN CREATION OF THIS MIDPOINT
*
* @return GeoPoint of the midpoint of the two marks, or the one mark if there is only one
*/
public GeoPoint getMidPoint() {
return midPoint;
}
/** /**
* Returns whether or not this CompoundMark is a Gate. It is generally cleaner to program to a * Returns whether or not this CompoundMark is a Gate. It is generally cleaner to program to a
* specific singleMark or the list of marks. * specific singleMark or the list of marks.
@@ -87,38 +97,6 @@ public class CompoundMark {
return marks; return marks;
} }
// @Override
// public boolean equals(Object other) {
// if (other == null) {
// return false;
// }
//
// if (!(other instanceof Mark)){
// return false;
// }
//
// Mark otherMark = (Mark) other;
//
// if (otherMark.getLat() != getLat() || otherMark.getLongitude() != getLongitude()) {
// return false;
// }
//
// if (otherMark.getCompoundMarkID() != getCompoundMarkID()){
// return false;
// }
//
// if (otherMark.getId() != getId()){
// return false;
// }
//
// if (!otherMark.getName().equals(name)){
// return false;
// }
//
// return true;
// }
@Override @Override
public int hashCode() { public int hashCode() {
int hash = 0; int hash = 0;
@@ -45,6 +45,19 @@ public class GeoUtility {
return (Math.toDegrees(getBearingRad(p1, p2)) + 360.0) % 360.0; return (Math.toDegrees(getBearingRad(p1, p2)) + 360.0) % 360.0;
} }
/**
* WARNING: this function DOES NOT account for wrapping around on lats / longs etc.
* SO BE CAREFUL IN USING THIS FUNCTION
*
* @param p1 GeoPoint 1
* @param p2 GeoPoint 2
* @return GeoPoint midPoint
*/
public static GeoPoint getDirtyMidPoint(GeoPoint p1, GeoPoint p2) {
return new GeoPoint((p1.getLat() + p2.getLat()) / 2, (p1.getLng() + p2.getLng()) / 2);
}
/** /**
* Calculates the angle between to angular co-ordinates on a sphere in radians. * Calculates the angle between to angular co-ordinates on a sphere in radians.
* *
@@ -256,9 +256,9 @@ public class XMLParser {
if (cMarkNode.getNodeName().equals("CompoundMark")) { if (cMarkNode.getNodeName().equals("CompoundMark")) {
cMark = new CompoundMark( cMark = new CompoundMark(
XMLParser.getNodeAttributeInt(cMarkNode, "CompoundMarkID"), XMLParser.getNodeAttributeInt(cMarkNode, "CompoundMarkID"),
XMLParser.getNodeAttributeString(cMarkNode, "Name") XMLParser.getNodeAttributeString(cMarkNode, "Name"),
createMarks(cMarkNode)
); );
cMark.addSubMarks(createMarks(cMarkNode));
allMarks.add(cMark); allMarks.add(cMark);
} }
} }
+6 -3
View File
@@ -2,6 +2,8 @@ package seng302.model;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import seng302.model.mark.CompoundMark; import seng302.model.mark.CompoundMark;
@@ -33,11 +35,12 @@ public class YachtTest {
yacht.setLocation(57.670333, 11.827833); yacht.setLocation(57.670333, 11.827833);
compoundMark = new CompoundMark(0, "HaomingsMark"); List<Mark> subMarks = new ArrayList<>();
Mark subMark1 = new Mark("H", 57.671524, 11.844495, 0); Mark subMark1 = new Mark("H", 57.671524, 11.844495, 0);
Mark subMark2 = new Mark("H", 57.670822, 11.843392, 0); Mark subMark2 = new Mark("H", 57.670822, 11.843392, 0);
compoundMark.addSubMarks(subMark1, subMark2); subMarks.add(subMark1);
subMarks.add(subMark2);
compoundMark = new CompoundMark(0, "HaomingsMark", subMarks);
} }