Commit 0d27fecb authored by Nico Mack's avatar Nico Mack

Major overhaul of CoordinateState handling and extension

parent 03e135bb
......@@ -22,6 +22,8 @@ package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.dbc.annotation.NonNull;
import com.google.common.base.Preconditions;
/**
* Class implementing a {@link CoordinateState} for camera coordinates. The class features methods
* to transition to all other states.
......@@ -49,6 +51,7 @@ public class CameraCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toTable(@NonNull Point context) {
context.setState(new TableCoordinates(context, Calibration.cameraToTableX(context.x), Calibration.cameraToTableY(context.y)));
}
......@@ -57,6 +60,7 @@ public class CameraCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toScreen(@NonNull Point context) {
context.setState(new ScreenCoordinates(context, Calibration.cameraToScreenX(context.x), Calibration.cameraToScreenY(context.y)));
}
......@@ -65,10 +69,32 @@ public class CameraCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toCamera(@NonNull Point context) {
/**
* The conversion is naught as the point's coordinates are already given in this reference
* scheme!
*/
}
/** {@inheritDoc} */
@Override
public Point normalize(Point context) {
double normx = (context.x - Calibration.getCameraLeft()) / Calibration.getCameraWidth();
double normy = (context.y - Calibration.getCameraTop()) / Calibration.getCameraHeight();
return new Point((float) normx, (float) normy, context.getAngle(), NormalizedCoordinates.class);
}
/** {@inheritDoc} */
@Override
public Point transform(Point normalized) {
Preconditions.checkArgument((normalized.x >= 0) && (normalized.x <= 1));
Preconditions.checkArgument((normalized.y >= 0) && (normalized.y <= 1));
double camx = Calibration.getCameraLeft() + (normalized.x * Calibration.getCameraWidth());
double camy = Calibration.getCameraTop() + (normalized.y * Calibration.getCameraHeight());
return new Point((float) camx, (float) camy, normalized.getAngle(), this.getClass());
}
}
\ No newline at end of file
......@@ -30,31 +30,77 @@ import lu.list.itis.dkd.dbc.annotation.NonNull;
* @version 2.3.0
*/
public abstract class CoordinateState {
/**
* normalizes the coordinates of a given point with the respect to its current coordinate
* system.
*
* @param context
* point to convert in given coordinate state
* @return point with normalized coordinates, i.e. without coordinate state
*/
public abstract Point normalize(Point context);
/**
* transforms a normalized point to this coordinate system.
*
* @param normalized
* specifies a normalized point to be converted
* @return a point expressed in this coordinate system.
*/
public abstract Point transform(Point normalized);
/**
* Method used to transition coordinates from the current system to the specified one.
*
* @param context
* specifies the point to be converted
* @param targetStateClass
* specifies the target coordinate state
*/
public void toCoordinates(@NonNull Point context, Class<? extends CoordinateState> targetStateClass) {
Point normalized = this.normalize(context);
context.setState(CoordinateStateFactory.newInstance(targetStateClass, context, normalized.x, normalized.y));
Point transformed = context.getState().transform(normalized);
context.x = transformed.x;
context.y = transformed.y;
}
/**
* Method used to transition coordinates from the current system to table coordinates. By
* default, unless overridden, the method does nothing.
* default, unless overridden, the method does nothing. This method is deprecated. Use
* toCoordinates method instead.
*
* @param context
* The context of the state; the feature the state gives meaning.
* @see #toCoordinates
*/
@Deprecated
public abstract void toTable(@NonNull Point context);
/**
* Method used to transition coordinates from the current system to screen coordinates. By
* default, unless overridden, the method does nothing.
* default, unless overridden, the method does nothing. This method is deprecated. Use
* toCoordinates method instead.
*
* @param context
* The context of the state; the feature the state gives meaning.
* @see #toCoordinates
*/
@Deprecated
public abstract void toScreen(@NonNull Point context);
/**
* Method used to transition coordinates from the current system to camera coordinates. By
* default, unless overridden, the method does nothing.
* default, unless overridden, the method does nothing. This method is deprecated. Use
* toCoordinates method instead.
*
* @param context
* The context of the state; the feature the state gives meaning.
* @see #toCoordinates
*/
@Deprecated
public abstract void toCamera(@NonNull Point context);
/**
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP is free software: you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation, version 3 of the
* License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Constructor;
/**
* @author mack
* @since [major].[minor]
* @version [major].[minor].[micro]
*/
public class CoordinateStateFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(CoordinateStateFactory.class.getSimpleName());
/**
* Creates and initializes a new instance of the specified coordinate state class.
*
* @param stateClass
* specifies the coordinate state class to instantiate
* @param context
* specifies the point to be represented in the given coordinate state
* @param x
* coordinate to be assigned to context
* @param y
* coordinate to be assigned to context
* @return a new instance of the specified coordinate state.
*/
public static CoordinateState newInstance(Class<? extends CoordinateState> stateClass, Point context, double x, double y) {
CoordinateState instance = null;
try {
Constructor<? extends CoordinateState> constructor = stateClass.getConstructor(new Class[] {Point.class, float.class, float.class});
instance = constructor.newInstance(new Object[] {context, (float) x, (float) y});
} catch (Exception exception) {
LOGGER.error("Failed to instantiate coordinate state of class {}!", stateClass.getName(), exception); //$NON-NLS-1$
}
return instance;
}
}
/**
* Copyright Luxembourg Institute of Science and Technology, 2016.
*
* This file is part of TULIP.
*
* TULIP is licensed under a dual-licensing scheme. For non-commercial purposes, the LGPL version 3,
* as stated below, is applicable. For all commercial purposes TULIP is licensed under a LIST
* proprietary license. Please contact LIST at tto@list.lu to obtain a commercial license.
*
* For all non-commercial purposes, TULIP is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, version 3 of the License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.dbc.annotation.NonNull;
import com.google.common.base.Preconditions;
/**
* Class implementing a {@link CoordinateState} for camera coordinates. The class features methods
* to transition to all other states.
*
* @author Eric Tobias [eric.tobias@list.lu]
* @since 1.0
* @version 2.3.0
*/
public class NormalizedCoordinates extends CoordinateState {
/**
* Constructing an instance with the provided points.
*
* @param context
* The context of the state; the feature the state gives meaning.
* @param x
* The X coordinate of the feature containing this state.
* @param y
* The Y coordinate of the feature containing this state.
*/
public NormalizedCoordinates(@NonNull Point context, float x, float y) {
context.setLocation(x, y);
}
/**
* {@inheritDoc}
*/
@Override
@Deprecated
public void toTable(@NonNull Point context) {
toCoordinates(context, TableCoordinates.class);
}
/**
* {@inheritDoc}
*/
@Override
@Deprecated
public void toScreen(@NonNull Point context) {
toCoordinates(context, ScreenCoordinates.class);
}
/**
* {@inheritDoc}
*/
@Override
@Deprecated
public void toCamera(@NonNull Point context) {
toCoordinates(context, CameraCoordinates.class);
}
/** {@inheritDoc} */
@Override
public Point normalize(Point context) {
return context.clone();
}
/** {@inheritDoc} */
@Override
public Point transform(Point normalized) {
Preconditions.checkArgument((normalized.x >= 0) && (normalized.x <= 1));
Preconditions.checkArgument((normalized.y >= 0) && (normalized.y <= 1));
return normalized.clone();
}
}
\ No newline at end of file
......@@ -102,20 +102,23 @@ public class Point extends Float implements KdComparator<Point> {
super(0, 0);
angle = 0;
switch (initialState.getSimpleName()) {
case "CameraCoordinates": //$NON-NLS-1$
state = new CameraCoordinates(this, x, y);
break;
case "ScreenCoordinates": //$NON-NLS-1$
state = new ScreenCoordinates(this, x, y);
break;
case "TableCoordinates": //$NON-NLS-1$
state = new TableCoordinates(this, x, y);
break;
default:
state = new CameraCoordinates(this, x, y);
break;
}
// switch (initialState.getSimpleName()) {
// case "CameraCoordinates": //$NON-NLS-1$
// state = new CameraCoordinates(this, x, y);
// break;
// case "ScreenCoordinates": //$NON-NLS-1$
// state = new ScreenCoordinates(this, x, y);
// break;
// case "TableCoordinates": //$NON-NLS-1$
// state = new TableCoordinates(this, x, y);
// break;
// default:
// state = new CameraCoordinates(this, x, y);
// break;
// }
state = CoordinateStateFactory.newInstance(initialState, this, x, y);
}
/**
......@@ -171,20 +174,22 @@ public class Point extends Float implements KdComparator<Point> {
super(x, y);
this.angle = angle;
switch (initialState.getSimpleName()) {
case "CameraCoordinates": //$NON-NLS-1$
state = new CameraCoordinates(this, x, y);
break;
case "ScreenCoordinates": //$NON-NLS-1$
state = new ScreenCoordinates(this, x, y);
break;
case "TableCoordinates": //$NON-NLS-1$
state = new TableCoordinates(this, x, y);
break;
default:
state = new CameraCoordinates(this, x, y);
break;
}
// switch (initialState.getSimpleName()) {
// case "CameraCoordinates": //$NON-NLS-1$
// state = new CameraCoordinates(this, x, y);
// break;
// case "ScreenCoordinates": //$NON-NLS-1$
// state = new ScreenCoordinates(this, x, y);
// break;
// case "TableCoordinates": //$NON-NLS-1$
// state = new TableCoordinates(this, x, y);
// break;
// default:
// state = new CameraCoordinates(this, x, y);
// break;
// }
state = CoordinateStateFactory.newInstance(initialState, this, x, y);
}
/**
......@@ -388,6 +393,16 @@ public class Point extends Float implements KdComparator<Point> {
return this;
}
/**
* @param targetStateClass
* @return
*/
public Point toCoordinates(Class<? extends CoordinateState> targetStateClass) {
state.toCoordinates(this, targetStateClass);
return this;
}
@Override
public Point clone() {
Point clone = new Point(x, y, angle, rotationDirection);
......@@ -418,20 +433,22 @@ public class Point extends Float implements KdComparator<Point> {
return clone();
}
switch (state.getClass().getSimpleName()) {
case "TableCoordinates": //$NON-NLS-1$
point.toTableCoordinates();
break;
case "ScreenCoordinates": //$NON-NLS-1$
point.toScreenCoordinates();
break;
case "CameraCoordinates": //$NON-NLS-1$
point.toCameraCoordinates();
break;
default:
logger.error("This should never happen! State: {}!", state.toString()); //$NON-NLS-1$
break;
}
// switch (state.getClass().getSimpleName()) {
// case "TableCoordinates": //$NON-NLS-1$
// point.toTableCoordinates();
// break;
// case "ScreenCoordinates": //$NON-NLS-1$
// point.toScreenCoordinates();
// break;
// case "CameraCoordinates": //$NON-NLS-1$
// point.toCameraCoordinates();
// break;
// default:
// logger.error("This should never happen! State: {}!", state.toString()); //$NON-NLS-1$
// break;
// }
point.getState().toCoordinates(point, this.state.getClass());
Point clone = clone();
clone.setLocation(clone.x + point.x, clone.y + point.y);
......
......@@ -22,6 +22,8 @@ package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.dbc.annotation.NonNull;
import com.google.common.base.Preconditions;
/**
* Class implementing a {@link CoordinateState} for screen coordinates. The class features methods
* to transition to all other states.
......@@ -49,6 +51,7 @@ public class ScreenCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toTable(@NonNull Point context) {
context.setState(new TableCoordinates(context, Calibration.screenToTableX(context.x), Calibration.screenToTableY(context.y)));
}
......@@ -57,6 +60,7 @@ public class ScreenCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toScreen(@NonNull Point context) {
/**
* The conversion is naught as the point's coordinates are already given in this reference
......@@ -68,7 +72,32 @@ public class ScreenCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toCamera(@NonNull Point context) {
context.setState(new CameraCoordinates(context, Calibration.screenToCameraX(context.x), Calibration.screenToCameraY(context.y)));
}
/** {@inheritDoc} */
@Override
public Point normalize(Point context) {
double normx = (context.x - Calibration.getScreenLeft()) / Calibration.getScreenWidth();
double normy = (context.y - Calibration.getScreenTop()) / Calibration.getScreenHeight();
return new Point((float) normx, (float) normy, context.getAngle(), NormalizedCoordinates.class);
}
/** {@inheritDoc} */
@Override
public Point transform(Point normalized) {
Preconditions.checkArgument((normalized.x >= 0) && (normalized.x <= 1));
Preconditions.checkArgument((normalized.y >= 0) && (normalized.y <= 1));
double screenx = Calibration.getScreenLeft() + (normalized.x * Calibration.getScreenWidth());
double screeny = Calibration.getScreenTop() + (normalized.y * Calibration.getScreenHeight());
return new Point((float) screenx, (float) screeny, normalized.getAngle(), this.getClass());
}
}
\ No newline at end of file
......@@ -22,6 +22,8 @@ package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.dbc.annotation.NonNull;
import com.google.common.base.Preconditions;
/**
* Class implementing a {@link CoordinateState} for table coordinates. The class features methods to
* transition to all other states.
......@@ -49,6 +51,7 @@ public class TableCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toTable(@NonNull Point context) {
/**
* The conversion is naught as the point's coordinates are already given in this reference
......@@ -60,6 +63,7 @@ public class TableCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toScreen(@NonNull Point context) {
context.setState(new ScreenCoordinates(context, Calibration.tableToScreenX(context.x), Calibration.tableToScreenY(context.y)));
}
......@@ -68,7 +72,31 @@ public class TableCoordinates extends CoordinateState {
* {@inheritDoc}
*/
@Override
@Deprecated
public void toCamera(@NonNull Point context) {
context.setState(new CameraCoordinates(context, Calibration.tableToCameraX(context.x), Calibration.tableToCameraY(context.y)));
}
/** {@inheritDoc} */
@Override
public Point normalize(Point context) {
double normx = context.x / Calibration.getTableWidth();
double normy = context.y / Calibration.getTableHeight();
return new Point((float) normx, (float) normy, context.getAngle(), NormalizedCoordinates.class);
}
/** {@inheritDoc} */
@Override
public Point transform(Point normalized) {
Preconditions.checkArgument((normalized.x >= 0) && (normalized.x <= 1));
Preconditions.checkArgument((normalized.y >= 0) && (normalized.y <= 1));
double tblx = (normalized.x * Calibration.getTableWidth());
double tbly = (normalized.y * Calibration.getTableHeight());
return new Point((float) tblx, (float) tbly, normalized.getAngle(), this.getClass());
}
}
\ No newline at end of file
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