[10343] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
[10375] | 4 | import java.awt.Container;
|
---|
[10343] | 5 | import java.awt.Point;
|
---|
[10375] | 6 | import java.awt.geom.AffineTransform;
|
---|
[10806] | 7 | import java.awt.geom.Area;
|
---|
| 8 | import java.awt.geom.Path2D;
|
---|
[10343] | 9 | import java.awt.geom.Point2D;
|
---|
| 10 | import java.awt.geom.Point2D.Double;
|
---|
[10651] | 11 | import java.awt.geom.Rectangle2D;
|
---|
[10875] | 12 | import java.io.Serializable;
|
---|
[11141] | 13 | import java.util.Objects;
|
---|
[11553] | 14 | import java.util.Optional;
|
---|
[10343] | 15 |
|
---|
[10375] | 16 | import javax.swing.JComponent;
|
---|
[10343] | 17 |
|
---|
| 18 | import org.openstreetmap.josm.Main;
|
---|
| 19 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 20 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
| 21 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[12161] | 22 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
[10343] | 23 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[10827] | 24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[10805] | 25 | import org.openstreetmap.josm.data.projection.Projecting;
|
---|
[10343] | 26 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
[10375] | 27 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
---|
[10968] | 28 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[11026] | 29 | import org.openstreetmap.josm.tools.Geometry;
|
---|
[11746] | 30 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
[10586] | 31 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
[10343] | 32 |
|
---|
| 33 | /**
|
---|
| 34 | * This class represents a state of the {@link MapView}.
|
---|
| 35 | * @author Michael Zangl
|
---|
| 36 | * @since 10343
|
---|
| 37 | */
|
---|
[10875] | 38 | public final class MapViewState implements Serializable {
|
---|
[10343] | 39 |
|
---|
[10875] | 40 | private static final long serialVersionUID = 1L;
|
---|
| 41 |
|
---|
[10827] | 42 | /**
|
---|
| 43 | * A flag indicating that the point is outside to the top of the map view.
|
---|
[10829] | 44 | * @since 10827
|
---|
[10827] | 45 | */
|
---|
| 46 | public static final int OUTSIDE_TOP = 1;
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * A flag indicating that the point is outside to the bottom of the map view.
|
---|
[10829] | 50 | * @since 10827
|
---|
[10827] | 51 | */
|
---|
| 52 | public static final int OUTSIDE_BOTTOM = 2;
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * A flag indicating that the point is outside to the left of the map view.
|
---|
[10829] | 56 | * @since 10827
|
---|
[10827] | 57 | */
|
---|
[10867] | 58 | public static final int OUTSIDE_LEFT = 4;
|
---|
[10827] | 59 |
|
---|
| 60 | /**
|
---|
| 61 | * A flag indicating that the point is outside to the right of the map view.
|
---|
[10829] | 62 | * @since 10827
|
---|
[10827] | 63 | */
|
---|
[10867] | 64 | public static final int OUTSIDE_RIGHT = 8;
|
---|
[10827] | 65 |
|
---|
[11026] | 66 | /**
|
---|
| 67 | * Additional pixels outside the view for where to start clipping.
|
---|
| 68 | */
|
---|
| 69 | private static final int CLIP_BOUNDS = 50;
|
---|
| 70 |
|
---|
[10875] | 71 | private final transient Projecting projecting;
|
---|
[10343] | 72 |
|
---|
| 73 | private final int viewWidth;
|
---|
| 74 | private final int viewHeight;
|
---|
| 75 |
|
---|
| 76 | private final double scale;
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Top left {@link EastNorth} coordinate of the view.
|
---|
| 80 | */
|
---|
| 81 | private final EastNorth topLeft;
|
---|
| 82 |
|
---|
[10375] | 83 | private final Point topLeftOnScreen;
|
---|
| 84 | private final Point topLeftInWindow;
|
---|
[10343] | 85 |
|
---|
| 86 | /**
|
---|
[10375] | 87 | * Create a new {@link MapViewState}
|
---|
| 88 | * @param projection The projection to use.
|
---|
| 89 | * @param viewWidth The view width
|
---|
| 90 | * @param viewHeight The view height
|
---|
| 91 | * @param scale The scale to use
|
---|
| 92 | * @param topLeft The top left corner in east/north space.
|
---|
[10968] | 93 | * @param topLeftInWindow The top left point in window
|
---|
| 94 | * @param topLeftOnScreen The top left point on screen
|
---|
[10343] | 95 | */
|
---|
[10968] | 96 | private MapViewState(Projecting projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft,
|
---|
| 97 | Point topLeftInWindow, Point topLeftOnScreen) {
|
---|
| 98 | CheckParameterUtil.ensureParameterNotNull(projection, "projection");
|
---|
| 99 | CheckParameterUtil.ensureParameterNotNull(topLeft, "topLeft");
|
---|
| 100 | CheckParameterUtil.ensureParameterNotNull(topLeftInWindow, "topLeftInWindow");
|
---|
| 101 | CheckParameterUtil.ensureParameterNotNull(topLeftOnScreen, "topLeftOnScreen");
|
---|
| 102 |
|
---|
[10805] | 103 | this.projecting = projection;
|
---|
[10375] | 104 | this.scale = scale;
|
---|
| 105 | this.topLeft = topLeft;
|
---|
[10343] | 106 |
|
---|
[10375] | 107 | this.viewWidth = viewWidth;
|
---|
| 108 | this.viewHeight = viewHeight;
|
---|
[10968] | 109 | this.topLeftInWindow = topLeftInWindow;
|
---|
| 110 | this.topLeftOnScreen = topLeftOnScreen;
|
---|
[10343] | 111 | }
|
---|
| 112 |
|
---|
[10968] | 113 | private MapViewState(Projecting projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft) {
|
---|
| 114 | this(projection, viewWidth, viewHeight, scale, topLeft, new Point(0, 0), new Point(0, 0));
|
---|
| 115 | }
|
---|
[10375] | 116 |
|
---|
[10968] | 117 | private MapViewState(EastNorth topLeft, MapViewState mvs) {
|
---|
| 118 | this(mvs.projecting, mvs.viewWidth, mvs.viewHeight, mvs.scale, topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
|
---|
[10375] | 119 | }
|
---|
| 120 |
|
---|
[10968] | 121 | private MapViewState(double scale, MapViewState mvs) {
|
---|
| 122 | this(mvs.projecting, mvs.viewWidth, mvs.viewHeight, scale, mvs.topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
|
---|
| 123 | }
|
---|
[10375] | 124 |
|
---|
[10968] | 125 | private MapViewState(JComponent position, MapViewState mvs) {
|
---|
| 126 | this(mvs.projecting, position.getWidth(), position.getHeight(), mvs.scale, mvs.topLeft,
|
---|
| 127 | findTopLeftInWindow(position), findTopLeftOnScreen(position));
|
---|
[10375] | 128 | }
|
---|
| 129 |
|
---|
[10968] | 130 | private MapViewState(Projecting projecting, MapViewState mvs) {
|
---|
| 131 | this(projecting, mvs.viewWidth, mvs.viewHeight, mvs.scale, mvs.topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
|
---|
| 132 | }
|
---|
[10375] | 133 |
|
---|
[10968] | 134 | private static Point findTopLeftInWindow(JComponent position) {
|
---|
| 135 | Point result = new Point();
|
---|
| 136 | // better than using swing utils, since this allows us to use the method if no screen is present.
|
---|
[10375] | 137 | Container component = position;
|
---|
| 138 | while (component != null) {
|
---|
[10968] | 139 | result.x += component.getX();
|
---|
| 140 | result.y += component.getY();
|
---|
[10375] | 141 | component = component.getParent();
|
---|
| 142 | }
|
---|
[10968] | 143 | return result;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | private static Point findTopLeftOnScreen(JComponent position) {
|
---|
[10586] | 147 | try {
|
---|
[10968] | 148 | return position.getLocationOnScreen();
|
---|
[11746] | 149 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
[10596] | 150 | throw BugReport.intercept(e).put("position", position).put("parent", position::getParent);
|
---|
[10586] | 151 | }
|
---|
[10375] | 152 | }
|
---|
| 153 |
|
---|
[10343] | 154 | /**
|
---|
[10375] | 155 | * The scale in east/north units per pixel.
|
---|
| 156 | * @return The scale.
|
---|
| 157 | */
|
---|
| 158 | public double getScale() {
|
---|
| 159 | return scale;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
[10343] | 163 | * Gets the MapViewPoint representation for a position in view coordinates.
|
---|
| 164 | * @param x The x coordinate inside the view.
|
---|
| 165 | * @param y The y coordinate inside the view.
|
---|
| 166 | * @return The MapViewPoint.
|
---|
| 167 | */
|
---|
| 168 | public MapViewPoint getForView(double x, double y) {
|
---|
| 169 | return new MapViewViewPoint(x, y);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * Gets the {@link MapViewPoint} for the given {@link EastNorth} coordinate.
|
---|
| 174 | * @param eastNorth the position.
|
---|
| 175 | * @return The point for that position.
|
---|
| 176 | */
|
---|
| 177 | public MapViewPoint getPointFor(EastNorth eastNorth) {
|
---|
| 178 | return new MapViewEastNorthPoint(eastNorth);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
[10651] | 182 | * Gets the {@link MapViewPoint} for the given {@link LatLon} coordinate.
|
---|
[12161] | 183 | * <p>
|
---|
| 184 | * This method exists to not break binary compatibility with old plugins
|
---|
[10651] | 185 | * @param latlon the position
|
---|
| 186 | * @return The point for that position.
|
---|
| 187 | * @since 10651
|
---|
| 188 | */
|
---|
| 189 | public MapViewPoint getPointFor(LatLon latlon) {
|
---|
[12161] | 190 | return getPointFor((ILatLon) latlon);
|
---|
[10651] | 191 | }
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
[12161] | 194 | * Gets the {@link MapViewPoint} for the given {@link LatLon} coordinate.
|
---|
| 195 | * @param latlon the position
|
---|
| 196 | * @return The point for that position.
|
---|
[12162] | 197 | * @since 12161
|
---|
[12161] | 198 | */
|
---|
| 199 | public MapViewPoint getPointFor(ILatLon latlon) {
|
---|
| 200 | try {
|
---|
| 201 | return getPointFor(Optional.ofNullable(latlon.getEastNorth(getProjection()))
|
---|
| 202 | .orElseThrow(IllegalArgumentException::new));
|
---|
| 203 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
| 204 | throw BugReport.intercept(e).put("latlon", latlon);
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
[11991] | 209 | * Gets the {@link MapViewPoint} for the given node.
|
---|
| 210 | * This is faster than {@link #getPointFor(LatLon)} because it uses the node east/north cache.
|
---|
[10827] | 211 | * @param node The node
|
---|
| 212 | * @return The position of that node.
|
---|
[10829] | 213 | * @since 10827
|
---|
[10827] | 214 | */
|
---|
| 215 | public MapViewPoint getPointFor(Node node) {
|
---|
[12161] | 216 | return getPointFor((ILatLon) node);
|
---|
[10827] | 217 | }
|
---|
| 218 |
|
---|
| 219 | /**
|
---|
[10343] | 220 | * Gets a rectangle representing the whole view area.
|
---|
| 221 | * @return The rectangle.
|
---|
| 222 | */
|
---|
| 223 | public MapViewRectangle getViewArea() {
|
---|
| 224 | return getForView(0, 0).rectTo(getForView(viewWidth, viewHeight));
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /**
|
---|
[10458] | 228 | * Gets a rectangle of the view as map view area.
|
---|
| 229 | * @param rectangle The rectangle to get.
|
---|
| 230 | * @return The view area.
|
---|
[10829] | 231 | * @since 10827
|
---|
[10458] | 232 | */
|
---|
[10827] | 233 | public MapViewRectangle getViewArea(Rectangle2D rectangle) {
|
---|
[10458] | 234 | return getForView(rectangle.getMinX(), rectangle.getMinY()).rectTo(getForView(rectangle.getMaxX(), rectangle.getMaxY()));
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /**
|
---|
[10343] | 238 | * Gets the center of the view.
|
---|
| 239 | * @return The center position.
|
---|
| 240 | */
|
---|
| 241 | public MapViewPoint getCenter() {
|
---|
| 242 | return getForView(viewWidth / 2.0, viewHeight / 2.0);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /**
|
---|
| 246 | * Gets the width of the view on the Screen;
|
---|
| 247 | * @return The width of the view component in screen pixel.
|
---|
| 248 | */
|
---|
| 249 | public double getViewWidth() {
|
---|
| 250 | return viewWidth;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /**
|
---|
| 254 | * Gets the height of the view on the Screen;
|
---|
| 255 | * @return The height of the view component in screen pixel.
|
---|
| 256 | */
|
---|
| 257 | public double getViewHeight() {
|
---|
| 258 | return viewHeight;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /**
|
---|
| 262 | * Gets the current projection used for the MapView.
|
---|
| 263 | * @return The projection.
|
---|
[12161] | 264 | * @see #getProjecting()
|
---|
[10343] | 265 | */
|
---|
| 266 | public Projection getProjection() {
|
---|
[10805] | 267 | return projecting.getBaseProjection();
|
---|
[10343] | 268 | }
|
---|
| 269 |
|
---|
| 270 | /**
|
---|
[12161] | 271 | * Gets the current projecting instance that is used to convert between east/north and lat/lon space.
|
---|
| 272 | * @return The projection.
|
---|
[12162] | 273 | * @since 12161
|
---|
[12161] | 274 | */
|
---|
| 275 | public Projecting getProjecting() {
|
---|
| 276 | return projecting;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /**
|
---|
[10375] | 280 | * Creates an affine transform that is used to convert the east/north coordinates to view coordinates.
|
---|
| 281 | * @return The affine transform. It should not be changed.
|
---|
[10399] | 282 | * @since 10375
|
---|
[10375] | 283 | */
|
---|
| 284 | public AffineTransform getAffineTransform() {
|
---|
[10398] | 285 | return new AffineTransform(1.0 / scale, 0.0, 0.0, -1.0 / scale, -topLeft.east() / scale,
|
---|
[10375] | 286 | topLeft.north() / scale);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[10910] | 289 | /**
|
---|
[11026] | 290 | * Gets a rectangle that is several pixel bigger than the view. It is used to define the view clipping.
|
---|
| 291 | * @return The rectangle.
|
---|
| 292 | */
|
---|
| 293 | public MapViewRectangle getViewClipRectangle() {
|
---|
| 294 | return getForView(-CLIP_BOUNDS, -CLIP_BOUNDS).rectTo(getForView(getViewWidth() + CLIP_BOUNDS, getViewHeight() + CLIP_BOUNDS));
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /**
|
---|
[10910] | 298 | * Returns the area for the given bounds.
|
---|
| 299 | * @param bounds bounds
|
---|
| 300 | * @return the area for the given bounds
|
---|
| 301 | */
|
---|
[10806] | 302 | public Area getArea(Bounds bounds) {
|
---|
| 303 | Path2D area = new Path2D.Double();
|
---|
[12818] | 304 | getProjection().visitOutline(bounds, en -> {
|
---|
| 305 | MapViewPoint point = getPointFor(en);
|
---|
[10806] | 306 | if (area.getCurrentPoint() == null) {
|
---|
| 307 | area.moveTo(point.getInViewX(), point.getInViewY());
|
---|
| 308 | } else {
|
---|
| 309 | area.lineTo(point.getInViewX(), point.getInViewY());
|
---|
| 310 | }
|
---|
| 311 | });
|
---|
| 312 | area.closePath();
|
---|
| 313 | return new Area(area);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[10375] | 316 | /**
|
---|
| 317 | * Creates a new state that is the same as the current state except for that it is using a new center.
|
---|
| 318 | * @param newCenter The new center coordinate.
|
---|
| 319 | * @return The new state.
|
---|
[10399] | 320 | * @since 10375
|
---|
[10375] | 321 | */
|
---|
| 322 | public MapViewState usingCenter(EastNorth newCenter) {
|
---|
| 323 | return movedTo(getCenter(), newCenter);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | /**
|
---|
| 327 | * @param mapViewPoint The reference point.
|
---|
| 328 | * @param newEastNorthThere The east/north coordinate that should be there.
|
---|
| 329 | * @return The new state.
|
---|
[10399] | 330 | * @since 10375
|
---|
[10375] | 331 | */
|
---|
| 332 | public MapViewState movedTo(MapViewPoint mapViewPoint, EastNorth newEastNorthThere) {
|
---|
| 333 | EastNorth delta = newEastNorthThere.subtract(mapViewPoint.getEastNorth());
|
---|
[10458] | 334 | if (delta.distanceSq(0, 0) < .1e-20) {
|
---|
[10375] | 335 | return this;
|
---|
| 336 | } else {
|
---|
| 337 | return new MapViewState(topLeft.add(delta), this);
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /**
|
---|
| 342 | * Creates a new state that is the same as the current state except for that it is using a new scale.
|
---|
| 343 | * @param newScale The new scale to use.
|
---|
| 344 | * @return The new state.
|
---|
[10399] | 345 | * @since 10375
|
---|
[10375] | 346 | */
|
---|
| 347 | public MapViewState usingScale(double newScale) {
|
---|
| 348 | return new MapViewState(newScale, this);
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /**
|
---|
| 352 | * Creates a new state that is the same as the current state except for that it is using the location of the given component.
|
---|
| 353 | * <p>
|
---|
| 354 | * The view is moved so that the center is the same as the old center.
|
---|
| 355 | * @param positon The new location to use.
|
---|
| 356 | * @return The new state.
|
---|
[10399] | 357 | * @since 10375
|
---|
[10375] | 358 | */
|
---|
| 359 | public MapViewState usingLocation(JComponent positon) {
|
---|
| 360 | EastNorth center = this.getCenter().getEastNorth();
|
---|
| 361 | return new MapViewState(positon, this).usingCenter(center);
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /**
|
---|
[10486] | 365 | * Creates a state that uses the projection.
|
---|
| 366 | * @param projection The projection to use.
|
---|
| 367 | * @return The new state.
|
---|
| 368 | * @since 10486
|
---|
| 369 | */
|
---|
| 370 | public MapViewState usingProjection(Projection projection) {
|
---|
[10805] | 371 | if (projection.equals(this.projecting)) {
|
---|
[10486] | 372 | return this;
|
---|
| 373 | } else {
|
---|
| 374 | return new MapViewState(projection, this);
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | /**
|
---|
[10375] | 379 | * Create the default {@link MapViewState} object for the given map view. The screen position won't be set so that this method can be used
|
---|
| 380 | * before the view was added to the hirarchy.
|
---|
| 381 | * @param width The view width
|
---|
| 382 | * @param height The view height
|
---|
| 383 | * @return The state
|
---|
[10399] | 384 | * @since 10375
|
---|
[10375] | 385 | */
|
---|
| 386 | public static MapViewState createDefaultState(int width, int height) {
|
---|
| 387 | Projection projection = Main.getProjection();
|
---|
| 388 | double scale = projection.getDefaultZoomInPPD();
|
---|
| 389 | MapViewState state = new MapViewState(projection, width, height, scale, new EastNorth(0, 0));
|
---|
| 390 | EastNorth center = calculateDefaultCenter();
|
---|
| 391 | return state.movedTo(state.getCenter(), center);
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | private static EastNorth calculateDefaultCenter() {
|
---|
[11553] | 395 | Bounds b = Optional.ofNullable(DownloadDialog.getSavedDownloadBounds()).orElseGet(
|
---|
| 396 | () -> Main.getProjection().getWorldBoundsLatLon());
|
---|
[12725] | 397 | return b.getCenter().getEastNorth(Main.getProjection());
|
---|
[10375] | 398 | }
|
---|
| 399 |
|
---|
| 400 | /**
|
---|
[11470] | 401 | * Check if this MapViewState equals another one, disregarding the position
|
---|
| 402 | * of the JOSM window on screen.
|
---|
| 403 | * @param other the other MapViewState
|
---|
| 404 | * @return true if the other MapViewState has the same size, scale, position and projection,
|
---|
| 405 | * false otherwise
|
---|
| 406 | */
|
---|
| 407 | public boolean equalsInWindow(MapViewState other) {
|
---|
| 408 | return other != null &&
|
---|
| 409 | this.viewWidth == other.viewWidth &&
|
---|
| 410 | this.viewHeight == other.viewHeight &&
|
---|
| 411 | this.scale == other.scale &&
|
---|
| 412 | Objects.equals(this.topLeft, other.topLeft) &&
|
---|
| 413 | Objects.equals(this.projecting, other.projecting);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | /**
|
---|
[10343] | 417 | * A class representing a point in the map view. It allows to convert between the different coordinate systems.
|
---|
| 418 | * @author Michael Zangl
|
---|
| 419 | */
|
---|
| 420 | public abstract class MapViewPoint {
|
---|
[12505] | 421 | /**
|
---|
| 422 | * Gets the map view state this path is used for.
|
---|
| 423 | * @return The state.
|
---|
| 424 | * @since 12505
|
---|
| 425 | */
|
---|
| 426 | public MapViewState getMapViewState() {
|
---|
| 427 | return MapViewState.this;
|
---|
| 428 | }
|
---|
[10343] | 429 |
|
---|
| 430 | /**
|
---|
| 431 | * Get this point in view coordinates.
|
---|
| 432 | * @return The point in view coordinates.
|
---|
| 433 | */
|
---|
| 434 | public Point2D getInView() {
|
---|
| 435 | return new Point2D.Double(getInViewX(), getInViewY());
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[10827] | 438 | /**
|
---|
| 439 | * Get the x coordinate in view space without creating an intermediate object.
|
---|
| 440 | * @return The x coordinate
|
---|
[10829] | 441 | * @since 10827
|
---|
[10827] | 442 | */
|
---|
| 443 | public abstract double getInViewX();
|
---|
[10343] | 444 |
|
---|
[10827] | 445 | /**
|
---|
| 446 | * Get the y coordinate in view space without creating an intermediate object.
|
---|
| 447 | * @return The y coordinate
|
---|
[10829] | 448 | * @since 10827
|
---|
[10827] | 449 | */
|
---|
| 450 | public abstract double getInViewY();
|
---|
[10343] | 451 |
|
---|
| 452 | /**
|
---|
| 453 | * Convert this point to window coordinates.
|
---|
| 454 | * @return The point in window coordinates.
|
---|
| 455 | */
|
---|
| 456 | public Point2D getInWindow() {
|
---|
[10375] | 457 | return getUsingCorner(topLeftInWindow);
|
---|
[10343] | 458 | }
|
---|
| 459 |
|
---|
| 460 | /**
|
---|
| 461 | * Convert this point to screen coordinates.
|
---|
| 462 | * @return The point in screen coordinates.
|
---|
| 463 | */
|
---|
| 464 | public Point2D getOnScreen() {
|
---|
[10375] | 465 | return getUsingCorner(topLeftOnScreen);
|
---|
[10343] | 466 | }
|
---|
| 467 |
|
---|
| 468 | private Double getUsingCorner(Point corner) {
|
---|
| 469 | return new Point2D.Double(corner.getX() + getInViewX(), corner.getY() + getInViewY());
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | /**
|
---|
| 473 | * Gets the {@link EastNorth} coordinate of this point.
|
---|
| 474 | * @return The east/north coordinate.
|
---|
| 475 | */
|
---|
| 476 | public EastNorth getEastNorth() {
|
---|
| 477 | return new EastNorth(topLeft.east() + getInViewX() * scale, topLeft.north() - getInViewY() * scale);
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | /**
|
---|
| 481 | * Create a rectangle from this to the other point.
|
---|
| 482 | * @param other The other point. Needs to be of the same {@link MapViewState}
|
---|
| 483 | * @return A rectangle.
|
---|
| 484 | */
|
---|
| 485 | public MapViewRectangle rectTo(MapViewPoint other) {
|
---|
| 486 | return new MapViewRectangle(this, other);
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | /**
|
---|
| 490 | * Gets the current position in LatLon coordinates according to the current projection.
|
---|
| 491 | * @return The positon as LatLon.
|
---|
[10805] | 492 | * @see #getLatLonClamped()
|
---|
[10343] | 493 | */
|
---|
| 494 | public LatLon getLatLon() {
|
---|
[10805] | 495 | return projecting.getBaseProjection().eastNorth2latlon(getEastNorth());
|
---|
[10343] | 496 | }
|
---|
[10651] | 497 |
|
---|
| 498 | /**
|
---|
[10805] | 499 | * Gets the latlon coordinate clamped to the current world area.
|
---|
| 500 | * @return The lat/lon coordinate
|
---|
| 501 | * @since 10805
|
---|
| 502 | */
|
---|
| 503 | public LatLon getLatLonClamped() {
|
---|
| 504 | return projecting.eastNorth2latlonClamped(getEastNorth());
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | /**
|
---|
[10651] | 508 | * Add the given offset to this point
|
---|
| 509 | * @param en The offset in east/north space.
|
---|
| 510 | * @return The new point
|
---|
| 511 | * @since 10651
|
---|
| 512 | */
|
---|
| 513 | public MapViewPoint add(EastNorth en) {
|
---|
| 514 | return new MapViewEastNorthPoint(getEastNorth().add(en));
|
---|
| 515 | }
|
---|
[10827] | 516 |
|
---|
| 517 | /**
|
---|
| 518 | * Check if this point is inside the view bounds.
|
---|
| 519 | *
|
---|
| 520 | * This is the case iff <code>getOutsideRectangleFlags(getViewArea())</code> returns no flags
|
---|
| 521 | * @return true if it is.
|
---|
[10829] | 522 | * @since 10827
|
---|
[10827] | 523 | */
|
---|
| 524 | public boolean isInView() {
|
---|
| 525 | return inRange(getInViewX(), 0, getViewWidth()) && inRange(getInViewY(), 0, getViewHeight());
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | private boolean inRange(double val, int min, double max) {
|
---|
| 529 | return val >= min && val < max;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | /**
|
---|
| 533 | * Gets the direction in which this point is outside of the given view rectangle.
|
---|
| 534 | * @param rect The rectangle to check agains.
|
---|
| 535 | * @return The direction in which it is outside of the view, as OUTSIDE_... flags.
|
---|
[10829] | 536 | * @since 10827
|
---|
[10827] | 537 | */
|
---|
| 538 | public int getOutsideRectangleFlags(MapViewRectangle rect) {
|
---|
| 539 | Rectangle2D bounds = rect.getInView();
|
---|
| 540 | int flags = 0;
|
---|
| 541 | if (getInViewX() < bounds.getMinX()) {
|
---|
| 542 | flags |= OUTSIDE_LEFT;
|
---|
| 543 | } else if (getInViewX() > bounds.getMaxX()) {
|
---|
| 544 | flags |= OUTSIDE_RIGHT;
|
---|
| 545 | }
|
---|
| 546 | if (getInViewY() < bounds.getMinY()) {
|
---|
| 547 | flags |= OUTSIDE_TOP;
|
---|
| 548 | } else if (getInViewY() > bounds.getMaxY()) {
|
---|
| 549 | flags |= OUTSIDE_BOTTOM;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | return flags;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | /**
|
---|
| 556 | * Gets the sum of the x/y view distances between the points. |x1 - x2| + |y1 - y2|
|
---|
| 557 | * @param p2 The other point
|
---|
| 558 | * @return The norm
|
---|
[10829] | 559 | * @since 10827
|
---|
[10827] | 560 | */
|
---|
| 561 | public double oneNormInView(MapViewPoint p2) {
|
---|
[10867] | 562 | return Math.abs(getInViewX() - p2.getInViewX()) + Math.abs(getInViewY() - p2.getInViewY());
|
---|
[10827] | 563 | }
|
---|
| 564 |
|
---|
| 565 | /**
|
---|
| 566 | * Gets the squared distance between this point and an other point.
|
---|
| 567 | * @param p2 The other point
|
---|
| 568 | * @return The squared distance.
|
---|
[10829] | 569 | * @since 10827
|
---|
[10827] | 570 | */
|
---|
| 571 | public double distanceToInViewSq(MapViewPoint p2) {
|
---|
| 572 | double dx = getInViewX() - p2.getInViewX();
|
---|
| 573 | double dy = getInViewY() - p2.getInViewY();
|
---|
| 574 | return dx * dx + dy * dy;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | /**
|
---|
| 578 | * Gets the distance between this point and an other point.
|
---|
| 579 | * @param p2 The other point
|
---|
| 580 | * @return The distance.
|
---|
[10829] | 581 | * @since 10827
|
---|
[10827] | 582 | */
|
---|
| 583 | public double distanceToInView(MapViewPoint p2) {
|
---|
| 584 | return Math.sqrt(distanceToInViewSq(p2));
|
---|
| 585 | }
|
---|
[10874] | 586 |
|
---|
| 587 | /**
|
---|
| 588 | * Do a linear interpolation to the other point
|
---|
| 589 | * @param p1 The other point
|
---|
| 590 | * @param i The interpolation factor. 0 is at the current point, 1 at the other point.
|
---|
| 591 | * @return The new point
|
---|
| 592 | * @since 10874
|
---|
| 593 | */
|
---|
[11144] | 594 | public MapViewPoint interpolate(MapViewPoint p1, double i) {
|
---|
[10874] | 595 | return new MapViewViewPoint((1 - i) * getInViewX() + i * p1.getInViewX(), (1 - i) * getInViewY() + i * p1.getInViewY());
|
---|
| 596 | }
|
---|
[10343] | 597 | }
|
---|
| 598 |
|
---|
| 599 | private class MapViewViewPoint extends MapViewPoint {
|
---|
| 600 | private final double x;
|
---|
| 601 | private final double y;
|
---|
| 602 |
|
---|
| 603 | MapViewViewPoint(double x, double y) {
|
---|
| 604 | this.x = x;
|
---|
| 605 | this.y = y;
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | @Override
|
---|
[10827] | 609 | public double getInViewX() {
|
---|
[10343] | 610 | return x;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | @Override
|
---|
[10827] | 614 | public double getInViewY() {
|
---|
[10343] | 615 | return y;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | @Override
|
---|
| 619 | public String toString() {
|
---|
[10462] | 620 | return "MapViewViewPoint [x=" + x + ", y=" + y + ']';
|
---|
[10343] | 621 | }
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | private class MapViewEastNorthPoint extends MapViewPoint {
|
---|
| 625 |
|
---|
| 626 | private final EastNorth eastNorth;
|
---|
| 627 |
|
---|
| 628 | MapViewEastNorthPoint(EastNorth eastNorth) {
|
---|
[11141] | 629 | this.eastNorth = Objects.requireNonNull(eastNorth, "eastNorth");
|
---|
[10343] | 630 | }
|
---|
| 631 |
|
---|
| 632 | @Override
|
---|
[10827] | 633 | public double getInViewX() {
|
---|
[10343] | 634 | return (eastNorth.east() - topLeft.east()) / scale;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | @Override
|
---|
[10827] | 638 | public double getInViewY() {
|
---|
[10343] | 639 | return (topLeft.north() - eastNorth.north()) / scale;
|
---|
| 640 | }
|
---|
| 641 |
|
---|
| 642 | @Override
|
---|
| 643 | public EastNorth getEastNorth() {
|
---|
| 644 | return eastNorth;
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | @Override
|
---|
| 648 | public String toString() {
|
---|
[10462] | 649 | return "MapViewEastNorthPoint [eastNorth=" + eastNorth + ']';
|
---|
[10343] | 650 | }
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | /**
|
---|
| 654 | * A rectangle on the MapView. It is rectangular in screen / EastNorth space.
|
---|
| 655 | * @author Michael Zangl
|
---|
| 656 | */
|
---|
| 657 | public class MapViewRectangle {
|
---|
| 658 | private final MapViewPoint p1;
|
---|
| 659 | private final MapViewPoint p2;
|
---|
| 660 |
|
---|
| 661 | /**
|
---|
| 662 | * Create a new MapViewRectangle
|
---|
| 663 | * @param p1 The first point to use
|
---|
| 664 | * @param p2 The second point to use.
|
---|
| 665 | */
|
---|
| 666 | MapViewRectangle(MapViewPoint p1, MapViewPoint p2) {
|
---|
| 667 | this.p1 = p1;
|
---|
| 668 | this.p2 = p2;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | /**
|
---|
| 672 | * Gets the projection bounds for this rectangle.
|
---|
| 673 | * @return The projection bounds.
|
---|
| 674 | */
|
---|
| 675 | public ProjectionBounds getProjectionBounds() {
|
---|
| 676 | ProjectionBounds b = new ProjectionBounds(p1.getEastNorth());
|
---|
| 677 | b.extend(p2.getEastNorth());
|
---|
| 678 | return b;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | /**
|
---|
| 682 | * Gets a rough estimate of the bounds by assuming lat/lon are parallel to x/y.
|
---|
| 683 | * @return The bounds computed by converting the corners of this rectangle.
|
---|
[10458] | 684 | * @see #getLatLonBoundsBox()
|
---|
[10343] | 685 | */
|
---|
| 686 | public Bounds getCornerBounds() {
|
---|
| 687 | Bounds b = new Bounds(p1.getLatLon());
|
---|
| 688 | b.extend(p2.getLatLon());
|
---|
| 689 | return b;
|
---|
| 690 | }
|
---|
[10458] | 691 |
|
---|
| 692 | /**
|
---|
[10462] | 693 | * Gets the real bounds that enclose this rectangle.
|
---|
[10458] | 694 | * This is computed respecting that the borders of this rectangle may not be a straignt line in latlon coordinates.
|
---|
| 695 | * @return The bounds.
|
---|
| 696 | * @since 10458
|
---|
| 697 | */
|
---|
| 698 | public Bounds getLatLonBoundsBox() {
|
---|
[10805] | 699 | // TODO @michael2402: Use hillclimb.
|
---|
| 700 | return projecting.getBaseProjection().getLatLonBoundsBox(getProjectionBounds());
|
---|
[10458] | 701 | }
|
---|
[10651] | 702 |
|
---|
| 703 | /**
|
---|
| 704 | * Gets this rectangle on the screen.
|
---|
| 705 | * @return The rectangle.
|
---|
| 706 | * @since 10651
|
---|
| 707 | */
|
---|
| 708 | public Rectangle2D getInView() {
|
---|
| 709 | double x1 = p1.getInViewX();
|
---|
| 710 | double y1 = p1.getInViewY();
|
---|
| 711 | double x2 = p2.getInViewX();
|
---|
| 712 | double y2 = p2.getInViewY();
|
---|
| 713 | return new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
|
---|
| 714 | }
|
---|
[10827] | 715 |
|
---|
| 716 | /**
|
---|
| 717 | * Check if the rectangle intersects the map view area.
|
---|
| 718 | * @return <code>true</code> if it intersects.
|
---|
[10829] | 719 | * @since 10827
|
---|
[10827] | 720 | */
|
---|
| 721 | public boolean isInView() {
|
---|
| 722 | return getInView().intersects(getViewArea().getInView());
|
---|
| 723 | }
|
---|
[11026] | 724 |
|
---|
| 725 | /**
|
---|
| 726 | * Gets the entry point at which a line between start and end enters the current view.
|
---|
| 727 | * @param start The start
|
---|
| 728 | * @param end The end
|
---|
| 729 | * @return The entry point or <code>null</code> if the line does not intersect this view.
|
---|
| 730 | */
|
---|
| 731 | public MapViewPoint getLineEntry(MapViewPoint start, MapViewPoint end) {
|
---|
| 732 | ProjectionBounds bounds = getProjectionBounds();
|
---|
| 733 | if (bounds.contains(start.getEastNorth())) {
|
---|
| 734 | return start;
|
---|
| 735 | }
|
---|
| 736 |
|
---|
| 737 | double dx = end.getEastNorth().east() - start.getEastNorth().east();
|
---|
| 738 | double boundX = dx > 0 ? bounds.minEast : bounds.maxEast;
|
---|
| 739 | EastNorth borderIntersection = Geometry.getSegmentSegmentIntersection(start.getEastNorth(), end.getEastNorth(),
|
---|
| 740 | new EastNorth(boundX, bounds.minNorth),
|
---|
| 741 | new EastNorth(boundX, bounds.maxNorth));
|
---|
| 742 | if (borderIntersection != null) {
|
---|
| 743 | return getPointFor(borderIntersection);
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 | double dy = end.getEastNorth().north() - start.getEastNorth().north();
|
---|
| 747 | double boundY = dy > 0 ? bounds.minNorth : bounds.maxNorth;
|
---|
| 748 | borderIntersection = Geometry.getSegmentSegmentIntersection(start.getEastNorth(), end.getEastNorth(),
|
---|
| 749 | new EastNorth(bounds.minEast, boundY),
|
---|
| 750 | new EastNorth(bounds.maxEast, boundY));
|
---|
| 751 | if (borderIntersection != null) {
|
---|
| 752 | return getPointFor(borderIntersection);
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 | return null;
|
---|
| 756 | }
|
---|
[10343] | 757 | }
|
---|
[10375] | 758 |
|
---|
[10343] | 759 | }
|
---|