Commit d670fb8a authored by Nico Mack's avatar Nico Mack

Added withinBounds method to Point class

Added static ZERO_BOUNDS to Bounds2D class
parent 39e79fcb
...@@ -12,6 +12,8 @@ public class Bounds2D { ...@@ -12,6 +12,8 @@ public class Bounds2D {
public static DecimalFormat format = new DecimalFormat(); public static DecimalFormat format = new DecimalFormat();
public static final Bounds2D ZERO_BOUNDS = new Bounds2D(0, 0, 0, 0);
static { static {
format.setDecimalSeparatorAlwaysShown(false); format.setDecimalSeparatorAlwaysShown(false);
format.setMaximumFractionDigits(2); format.setMaximumFractionDigits(2);
......
...@@ -31,6 +31,9 @@ import lu.list.itis.dkd.dbc.annotation.NonNull; ...@@ -31,6 +31,9 @@ import lu.list.itis.dkd.dbc.annotation.NonNull;
* @version 2.3.0 * @version 2.3.0
*/ */
public class CameraCoordinates extends CoordinateState { public class CameraCoordinates extends CoordinateState {
private static final Bounds2D BOUNDS = new Bounds2D(Calibration.getCameraLeft(), Calibration.getCameraTop(), Calibration.getTableWidth(), Calibration.getTableHeight());
/** /**
* Constructing an instance with the provided points. * Constructing an instance with the provided points.
* *
...@@ -70,11 +73,15 @@ public class CameraCoordinates extends CoordinateState { ...@@ -70,11 +73,15 @@ public class CameraCoordinates extends CoordinateState {
@Deprecated @Deprecated
public void toCamera(@NonNull Point context) { public void toCamera(@NonNull Point context) {
/** /**
* The conversion is naught as the point's coordinates are already given in this reference * The conversion is naught as the point's coordinates are already given in this reference scheme!
* scheme!
*/ */
} }
@Override
public boolean withinBounds(Point context) {
return BOUNDS.includes(context);
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public Point normalize(Point context) { public Point normalize(Point context) {
......
...@@ -32,8 +32,7 @@ import lu.list.itis.dkd.dbc.annotation.NonNull; ...@@ -32,8 +32,7 @@ import lu.list.itis.dkd.dbc.annotation.NonNull;
public abstract class CoordinateState { public abstract class CoordinateState {
/** /**
* normalizes the coordinates of a given point with the respect to its current coordinate * normalizes the coordinates of a given point with the respect to its current coordinate system.
* system.
* *
* @param context * @param context
* point to convert in given coordinate state * point to convert in given coordinate state
...@@ -70,9 +69,9 @@ public abstract class CoordinateState { ...@@ -70,9 +69,9 @@ public abstract class CoordinateState {
} }
/** /**
* Method used to transition coordinates from the current system to table coordinates. By * Method used to transition coordinates from the current system to table coordinates. By default,
* default, unless overridden, the method does nothing. This method is deprecated. Use * unless overridden, the method does nothing. This method is deprecated. Use toCoordinates method
* toCoordinates method instead. * instead.
* *
* @param context * @param context
* The context of the state; the feature the state gives meaning. * The context of the state; the feature the state gives meaning.
...@@ -82,9 +81,9 @@ public abstract class CoordinateState { ...@@ -82,9 +81,9 @@ public abstract class CoordinateState {
public abstract void toTable(@NonNull Point context); public abstract void toTable(@NonNull Point context);
/** /**
* Method used to transition coordinates from the current system to screen coordinates. By * Method used to transition coordinates from the current system to screen coordinates. By default,
* default, unless overridden, the method does nothing. This method is deprecated. Use * unless overridden, the method does nothing. This method is deprecated. Use toCoordinates method
* toCoordinates method instead. * instead.
* *
* @param context * @param context
* The context of the state; the feature the state gives meaning. * The context of the state; the feature the state gives meaning.
...@@ -94,9 +93,9 @@ public abstract class CoordinateState { ...@@ -94,9 +93,9 @@ public abstract class CoordinateState {
public abstract void toScreen(@NonNull Point context); public abstract void toScreen(@NonNull Point context);
/** /**
* Method used to transition coordinates from the current system to camera coordinates. By * Method used to transition coordinates from the current system to camera coordinates. By default,
* default, unless overridden, the method does nothing. This method is deprecated. Use * unless overridden, the method does nothing. This method is deprecated. Use toCoordinates method
* toCoordinates method instead. * instead.
* *
* @param context * @param context
* The context of the state; the feature the state gives meaning. * The context of the state; the feature the state gives meaning.
...@@ -105,6 +104,16 @@ public abstract class CoordinateState { ...@@ -105,6 +104,16 @@ public abstract class CoordinateState {
@Deprecated @Deprecated
public abstract void toCamera(@NonNull Point context); public abstract void toCamera(@NonNull Point context);
/**
* Checks whether the specified point is withing acceptable bounds.
*
* @param context
* @return <code>true</code> if specified point can be represented by this coordinate state,
* <code>false</code> otherwise
*/
public abstract boolean withinBounds(Point context);
/** /**
* Method testing for equality. * Method testing for equality.
* *
......
...@@ -33,6 +33,9 @@ import com.google.common.base.Preconditions; ...@@ -33,6 +33,9 @@ import com.google.common.base.Preconditions;
* @version 2.3.0 * @version 2.3.0
*/ */
public class NormalizedCoordinates extends CoordinateState { public class NormalizedCoordinates extends CoordinateState {
private static final Bounds2D BOUNDS = new Bounds2D(0, 0, 1, 1);
/** /**
* Constructing an instance with the provided points. * Constructing an instance with the provided points.
* *
...@@ -76,6 +79,11 @@ public class NormalizedCoordinates extends CoordinateState { ...@@ -76,6 +79,11 @@ public class NormalizedCoordinates extends CoordinateState {
toCoordinates(context, CameraCoordinates.class); toCoordinates(context, CameraCoordinates.class);
} }
@Override
public boolean withinBounds(Point context) {
return BOUNDS.includes(context);
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public Point normalize(Point context) { public Point normalize(Point context) {
......
...@@ -571,6 +571,17 @@ public class Point extends Float implements Comparable<Point>, KdComparator<Poin ...@@ -571,6 +571,17 @@ public class Point extends Float implements Comparable<Point>, KdComparator<Poin
return comparison; return comparison;
} }
/**
* Checks whether the specified point is within the acceptable bounds of its coordinate state.
*
* @return <code>true</code> if specified point can be represented by this coordinate state,
* <code>false</code> otherwise
*/
public boolean withinBounds() {
return state.withinBounds(this);
}
} }
......
...@@ -86,6 +86,12 @@ public class ScreenCoordinates extends CoordinateState { ...@@ -86,6 +86,12 @@ public class ScreenCoordinates extends CoordinateState {
context.setState(new CameraCoordinates(context, Calibration.screenToCameraX(context.x), Calibration.screenToCameraY(context.y))); context.setState(new CameraCoordinates(context, Calibration.screenToCameraX(context.x), Calibration.screenToCameraY(context.y)));
} }
@Override
public boolean withinBounds(Point context) {
Bounds2D currentBounds = (bounds == null) ? FULL_SCREEN : bounds;
return currentBounds.includes(context);
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public Point normalize(Point context) { public Point normalize(Point context) {
......
...@@ -31,6 +31,9 @@ import lu.list.itis.dkd.dbc.annotation.NonNull; ...@@ -31,6 +31,9 @@ import lu.list.itis.dkd.dbc.annotation.NonNull;
* @version 2.3.0 * @version 2.3.0
*/ */
public class TableCoordinates extends CoordinateState { public class TableCoordinates extends CoordinateState {
private static final Bounds2D BOUNDS = new Bounds2D(0, 0, Calibration.getTableWidth(), Calibration.getTableHeight());
/** /**
* Constructing an instance with the provided points. * Constructing an instance with the provided points.
* *
...@@ -52,8 +55,7 @@ public class TableCoordinates extends CoordinateState { ...@@ -52,8 +55,7 @@ public class TableCoordinates extends CoordinateState {
@Deprecated @Deprecated
public void toTable(@NonNull Point context) { public void toTable(@NonNull Point context) {
/** /**
* The conversion is naught as the point's coordinates are already given in this reference * The conversion is naught as the point's coordinates are already given in this reference scheme!
* scheme!
*/ */
} }
...@@ -75,11 +77,16 @@ public class TableCoordinates extends CoordinateState { ...@@ -75,11 +77,16 @@ public class TableCoordinates extends CoordinateState {
context.setState(new CameraCoordinates(context, Calibration.tableToCameraX(context.x), Calibration.tableToCameraY(context.y))); context.setState(new CameraCoordinates(context, Calibration.tableToCameraX(context.x), Calibration.tableToCameraY(context.y)));
} }
@Override
public boolean withinBounds(Point context) {
return BOUNDS.includes(context);
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public Point normalize(Point context) { public Point normalize(Point context) {
double normx = context.x / Calibration.getTableWidth(); double normx = context.x / BOUNDS.getWidth();
double normy = context.y / Calibration.getTableHeight(); double normy = context.y / BOUNDS.getHeight();
return new Point((float) normx, (float) normy, context.getAngle(), NormalizedCoordinates.class); return new Point((float) normx, (float) normy, context.getAngle(), NormalizedCoordinates.class);
} }
...@@ -90,8 +97,8 @@ public class TableCoordinates extends CoordinateState { ...@@ -90,8 +97,8 @@ public class TableCoordinates extends CoordinateState {
// Preconditions.checkArgument((normalized.x >= 0) && (normalized.x <= 1)); // Preconditions.checkArgument((normalized.x >= 0) && (normalized.x <= 1));
// Preconditions.checkArgument((normalized.y >= 0) && (normalized.y <= 1)); // Preconditions.checkArgument((normalized.y >= 0) && (normalized.y <= 1));
double tblx = (normalized.x * Calibration.getTableWidth()); double tblx = (normalized.x * BOUNDS.getWidth());
double tbly = (normalized.y * Calibration.getTableHeight()); double tbly = (normalized.y * BOUNDS.getHeight());
return new Point((float) tblx, (float) tbly, normalized.getAngle(), this.getClass()); return new Point((float) tblx, (float) tbly, normalized.getAngle(), this.getClass());
} }
......
...@@ -91,6 +91,11 @@ public class GraphCoordinates extends CoordinateState { ...@@ -91,6 +91,11 @@ public class GraphCoordinates extends CoordinateState {
toCoordinates(context, CameraCoordinates.class); toCoordinates(context, CameraCoordinates.class);
} }
@Override
public boolean withinBounds(Point context) {
return bounds.includes(context);
}
@Override @Override
public Point normalize(Point context) { public Point normalize(Point context) {
double normx = ((context.x - bounds.x1) / bounds.getWidth()); double normx = ((context.x - bounds.x1) / bounds.getWidth());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment