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

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

fix #13306 - Make map paint code use double coordinates (patch by michael2402) - gsoc-core

  • Property svn:eol-style set to native
File size: 20.5 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 10826
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 10826
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 10826
48 */
49 public static final int OUTSIDE_LEFT = 3;
50
51 /**
52 * A flag indicating that the point is outside to the right of the map view.
53 * @since 10826
54 */
55 public static final int OUTSIDE_RIGHT = 4;
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 10826
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 10826
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 width of the view on the Screen;
222 * @return The width of the view component in screen pixel.
223 */
224 public double getViewWidth() {
225 return viewWidth;
226 }
227
228 /**
229 * Gets the height of the view on the Screen;
230 * @return The height of the view component in screen pixel.
231 */
232 public double getViewHeight() {
233 return viewHeight;
234 }
235
236 /**
237 * Gets the current projection used for the MapView.
238 * @return The projection.
239 */
240 public Projection getProjection() {
241 return projecting.getBaseProjection();
242 }
243
244 /**
245 * Creates an affine transform that is used to convert the east/north coordinates to view coordinates.
246 * @return The affine transform. It should not be changed.
247 * @since 10375
248 */
249 public AffineTransform getAffineTransform() {
250 return new AffineTransform(1.0 / scale, 0.0, 0.0, -1.0 / scale, -topLeft.east() / scale,
251 topLeft.north() / scale);
252 }
253
254 public Area getArea(Bounds bounds) {
255 Path2D area = new Path2D.Double();
256 bounds.visitEdge(getProjection(), latlon -> {
257 MapViewPoint point = getPointFor(latlon);
258 if (area.getCurrentPoint() == null) {
259 area.moveTo(point.getInViewX(), point.getInViewY());
260 } else {
261 area.lineTo(point.getInViewX(), point.getInViewY());
262 }
263 });
264 area.closePath();
265 return new Area(area);
266 }
267
268 /**
269 * Creates a new state that is the same as the current state except for that it is using a new center.
270 * @param newCenter The new center coordinate.
271 * @return The new state.
272 * @since 10375
273 */
274 public MapViewState usingCenter(EastNorth newCenter) {
275 return movedTo(getCenter(), newCenter);
276 }
277
278 /**
279 * @param mapViewPoint The reference point.
280 * @param newEastNorthThere The east/north coordinate that should be there.
281 * @return The new state.
282 * @since 10375
283 */
284 public MapViewState movedTo(MapViewPoint mapViewPoint, EastNorth newEastNorthThere) {
285 EastNorth delta = newEastNorthThere.subtract(mapViewPoint.getEastNorth());
286 if (delta.distanceSq(0, 0) < .1e-20) {
287 return this;
288 } else {
289 return new MapViewState(topLeft.add(delta), this);
290 }
291 }
292
293 /**
294 * Creates a new state that is the same as the current state except for that it is using a new scale.
295 * @param newScale The new scale to use.
296 * @return The new state.
297 * @since 10375
298 */
299 public MapViewState usingScale(double newScale) {
300 return new MapViewState(newScale, this);
301 }
302
303 /**
304 * Creates a new state that is the same as the current state except for that it is using the location of the given component.
305 * <p>
306 * The view is moved so that the center is the same as the old center.
307 * @param positon The new location to use.
308 * @return The new state.
309 * @since 10375
310 */
311 public MapViewState usingLocation(JComponent positon) {
312 EastNorth center = this.getCenter().getEastNorth();
313 return new MapViewState(positon, this).usingCenter(center);
314 }
315
316 /**
317 * Creates a state that uses the projection.
318 * @param projection The projection to use.
319 * @return The new state.
320 * @since 10486
321 */
322 public MapViewState usingProjection(Projection projection) {
323 if (projection.equals(this.projecting)) {
324 return this;
325 } else {
326 return new MapViewState(projection, this);
327 }
328 }
329
330 /**
331 * 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
332 * before the view was added to the hirarchy.
333 * @param width The view width
334 * @param height The view height
335 * @return The state
336 * @since 10375
337 */
338 public static MapViewState createDefaultState(int width, int height) {
339 Projection projection = Main.getProjection();
340 double scale = projection.getDefaultZoomInPPD();
341 MapViewState state = new MapViewState(projection, width, height, scale, new EastNorth(0, 0));
342 EastNorth center = calculateDefaultCenter();
343 return state.movedTo(state.getCenter(), center);
344 }
345
346 private static EastNorth calculateDefaultCenter() {
347 Bounds b = DownloadDialog.getSavedDownloadBounds();
348 if (b == null) {
349 b = Main.getProjection().getWorldBoundsLatLon();
350 }
351 return Main.getProjection().latlon2eastNorth(b.getCenter());
352 }
353
354 /**
355 * A class representing a point in the map view. It allows to convert between the different coordinate systems.
356 * @author Michael Zangl
357 */
358 public abstract class MapViewPoint {
359
360 /**
361 * Get this point in view coordinates.
362 * @return The point in view coordinates.
363 */
364 public Point2D getInView() {
365 return new Point2D.Double(getInViewX(), getInViewY());
366 }
367
368 /**
369 * Get the x coordinate in view space without creating an intermediate object.
370 * @return The x coordinate
371 * @since 10826
372 */
373 public abstract double getInViewX();
374
375 /**
376 * Get the y coordinate in view space without creating an intermediate object.
377 * @return The y coordinate
378 * @since 10826
379 */
380 public abstract double getInViewY();
381
382 /**
383 * Convert this point to window coordinates.
384 * @return The point in window coordinates.
385 */
386 public Point2D getInWindow() {
387 return getUsingCorner(topLeftInWindow);
388 }
389
390 /**
391 * Convert this point to screen coordinates.
392 * @return The point in screen coordinates.
393 */
394 public Point2D getOnScreen() {
395 return getUsingCorner(topLeftOnScreen);
396 }
397
398 private Double getUsingCorner(Point corner) {
399 return new Point2D.Double(corner.getX() + getInViewX(), corner.getY() + getInViewY());
400 }
401
402 /**
403 * Gets the {@link EastNorth} coordinate of this point.
404 * @return The east/north coordinate.
405 */
406 public EastNorth getEastNorth() {
407 return new EastNorth(topLeft.east() + getInViewX() * scale, topLeft.north() - getInViewY() * scale);
408 }
409
410 /**
411 * Create a rectangle from this to the other point.
412 * @param other The other point. Needs to be of the same {@link MapViewState}
413 * @return A rectangle.
414 */
415 public MapViewRectangle rectTo(MapViewPoint other) {
416 return new MapViewRectangle(this, other);
417 }
418
419 /**
420 * Gets the current position in LatLon coordinates according to the current projection.
421 * @return The positon as LatLon.
422 * @see #getLatLonClamped()
423 */
424 public LatLon getLatLon() {
425 return projecting.getBaseProjection().eastNorth2latlon(getEastNorth());
426 }
427
428 /**
429 * Gets the latlon coordinate clamped to the current world area.
430 * @return The lat/lon coordinate
431 * @since 10805
432 */
433 public LatLon getLatLonClamped() {
434 return projecting.eastNorth2latlonClamped(getEastNorth());
435 }
436
437 /**
438 * Add the given offset to this point
439 * @param en The offset in east/north space.
440 * @return The new point
441 * @since 10651
442 */
443 public MapViewPoint add(EastNorth en) {
444 return new MapViewEastNorthPoint(getEastNorth().add(en));
445 }
446
447 /**
448 * Check if this point is inside the view bounds.
449 *
450 * This is the case iff <code>getOutsideRectangleFlags(getViewArea())</code> returns no flags
451 * @return true if it is.
452 * @since 10826
453 */
454 public boolean isInView() {
455 return inRange(getInViewX(), 0, getViewWidth()) && inRange(getInViewY(), 0, getViewHeight());
456 }
457
458 private boolean inRange(double val, int min, double max) {
459 return val >= min && val < max;
460 }
461
462 /**
463 * Gets the direction in which this point is outside of the given view rectangle.
464 * @param rect The rectangle to check agains.
465 * @return The direction in which it is outside of the view, as OUTSIDE_... flags.
466 * @since 10826
467 */
468 public int getOutsideRectangleFlags(MapViewRectangle rect) {
469 Rectangle2D bounds = rect.getInView();
470 int flags = 0;
471 if (getInViewX() < bounds.getMinX()) {
472 flags |= OUTSIDE_LEFT;
473 } else if (getInViewX() > bounds.getMaxX()) {
474 flags |= OUTSIDE_RIGHT;
475 }
476 if (getInViewY() < bounds.getMinY()) {
477 flags |= OUTSIDE_TOP;
478 } else if (getInViewY() > bounds.getMaxY()) {
479 flags |= OUTSIDE_BOTTOM;
480 }
481
482 return flags;
483 }
484
485 /**
486 * Gets the sum of the x/y view distances between the points. |x1 - x2| + |y1 - y2|
487 * @param p2 The other point
488 * @return The norm
489 * @since 10826
490 */
491 public double oneNormInView(MapViewPoint p2) {
492 return Math.abs(getInViewX() - p2.getInViewX()) + Math.abs(getInViewY()) - p2.getInViewY();
493 }
494
495 /**
496 * Gets the squared distance between this point and an other point.
497 * @param p2 The other point
498 * @return The squared distance.
499 * @since 10826
500 */
501 public double distanceToInViewSq(MapViewPoint p2) {
502 double dx = getInViewX() - p2.getInViewX();
503 double dy = getInViewY() - p2.getInViewY();
504 return dx * dx + dy * dy;
505 }
506
507 /**
508 * Gets the distance between this point and an other point.
509 * @param p2 The other point
510 * @return The distance.
511 * @since 10826
512 */
513 public double distanceToInView(MapViewPoint p2) {
514 return Math.sqrt(distanceToInViewSq(p2));
515 }
516 }
517
518 private class MapViewViewPoint extends MapViewPoint {
519 private final double x;
520 private final double y;
521
522 MapViewViewPoint(double x, double y) {
523 this.x = x;
524 this.y = y;
525 }
526
527 @Override
528 public double getInViewX() {
529 return x;
530 }
531
532 @Override
533 public double getInViewY() {
534 return y;
535 }
536
537 @Override
538 public String toString() {
539 return "MapViewViewPoint [x=" + x + ", y=" + y + ']';
540 }
541 }
542
543 private class MapViewEastNorthPoint extends MapViewPoint {
544
545 private final EastNorth eastNorth;
546
547 MapViewEastNorthPoint(EastNorth eastNorth) {
548 this.eastNorth = eastNorth;
549 }
550
551 @Override
552 public double getInViewX() {
553 return (eastNorth.east() - topLeft.east()) / scale;
554 }
555
556 @Override
557 public double getInViewY() {
558 return (topLeft.north() - eastNorth.north()) / scale;
559 }
560
561 @Override
562 public EastNorth getEastNorth() {
563 return eastNorth;
564 }
565
566 @Override
567 public String toString() {
568 return "MapViewEastNorthPoint [eastNorth=" + eastNorth + ']';
569 }
570 }
571
572 /**
573 * A rectangle on the MapView. It is rectangular in screen / EastNorth space.
574 * @author Michael Zangl
575 */
576 public class MapViewRectangle {
577 private final MapViewPoint p1;
578 private final MapViewPoint p2;
579
580 /**
581 * Create a new MapViewRectangle
582 * @param p1 The first point to use
583 * @param p2 The second point to use.
584 */
585 MapViewRectangle(MapViewPoint p1, MapViewPoint p2) {
586 this.p1 = p1;
587 this.p2 = p2;
588 }
589
590 /**
591 * Gets the projection bounds for this rectangle.
592 * @return The projection bounds.
593 */
594 public ProjectionBounds getProjectionBounds() {
595 ProjectionBounds b = new ProjectionBounds(p1.getEastNorth());
596 b.extend(p2.getEastNorth());
597 return b;
598 }
599
600 /**
601 * Gets a rough estimate of the bounds by assuming lat/lon are parallel to x/y.
602 * @return The bounds computed by converting the corners of this rectangle.
603 * @see #getLatLonBoundsBox()
604 */
605 public Bounds getCornerBounds() {
606 Bounds b = new Bounds(p1.getLatLon());
607 b.extend(p2.getLatLon());
608 return b;
609 }
610
611 /**
612 * Gets the real bounds that enclose this rectangle.
613 * This is computed respecting that the borders of this rectangle may not be a straignt line in latlon coordinates.
614 * @return The bounds.
615 * @since 10458
616 */
617 public Bounds getLatLonBoundsBox() {
618 // TODO @michael2402: Use hillclimb.
619 return projecting.getBaseProjection().getLatLonBoundsBox(getProjectionBounds());
620 }
621
622 /**
623 * Gets this rectangle on the screen.
624 * @return The rectangle.
625 * @since 10651
626 */
627 public Rectangle2D getInView() {
628 double x1 = p1.getInViewX();
629 double y1 = p1.getInViewY();
630 double x2 = p2.getInViewX();
631 double y2 = p2.getInViewY();
632 return new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
633 }
634
635 /**
636 * Check if the rectangle intersects the map view area.
637 * @return <code>true</code> if it intersects.
638 * @since 10826
639 */
640 public boolean isInView() {
641 return getInView().intersects(getViewArea().getInView());
642 }
643 }
644
645}
Note: See TracBrowser for help on using the repository browser.