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

Last change on this file since 10874 was 10874, checked in by Don-vip, 8 years ago

fix #13412 - Clean up DrawAction, add StrokeProperty (patch by michael2402) - gsoc-core

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