source: josm/trunk/src/org/openstreetmap/josm/gui/MapViewState.java@ 14273

Last change on this file since 14273 was 14273, checked in by stoecker, 6 years ago

fix typos - patch by naoliv - fix #16781 - Thanks a lot

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