source: josm/trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java@ 4310

Last change on this file since 4310 was 4126, checked in by bastiK, 13 years ago

memory optimizations for Node & WayPoint (Patch by Gubaer, modified)

The field 'proj' in CachedLatLon is a waste of memory. For the 2 classes where this has the greatest impact, the cache for the projected coordinates is replaced by 2 simple double fields (east & north). On projection change, they have to be invalidated explicitly. This is handled by the DataSet & the GpxLayer.

  • Property svn:eol-style set to native
File size: 46.3 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5
6import java.awt.Cursor;
7import java.awt.Point;
8import java.awt.Rectangle;
9import java.awt.geom.Point2D;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.Date;
14import java.util.HashSet;
15import java.util.LinkedHashMap;
16import java.util.LinkedList;
17import java.util.List;
18import java.util.Locale;
19import java.util.Map;
20import java.util.Set;
21import java.util.Stack;
22import java.util.TreeMap;
23import java.util.concurrent.CopyOnWriteArrayList;
24
25import javax.swing.JComponent;
26
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.data.Bounds;
29import org.openstreetmap.josm.data.ProjectionBounds;
30import org.openstreetmap.josm.data.coor.CachedLatLon;
31import org.openstreetmap.josm.data.coor.EastNorth;
32import org.openstreetmap.josm.data.coor.LatLon;
33import org.openstreetmap.josm.data.osm.BBox;
34import org.openstreetmap.josm.data.osm.DataSet;
35import org.openstreetmap.josm.data.osm.Node;
36import org.openstreetmap.josm.data.osm.OsmPrimitive;
37import org.openstreetmap.josm.data.osm.Way;
38import org.openstreetmap.josm.data.osm.WaySegment;
39import org.openstreetmap.josm.data.preferences.IntegerProperty;
40import org.openstreetmap.josm.data.projection.Projection;
41import org.openstreetmap.josm.data.projection.Projections;
42import org.openstreetmap.josm.gui.help.Helpful;
43import org.openstreetmap.josm.gui.preferences.ProjectionPreference;
44import org.openstreetmap.josm.tools.Predicate;
45
46/**
47 * An component that can be navigated by a mapmover. Used as map view and for the
48 * zoomer in the download dialog.
49 *
50 * @author imi
51 */
52public class NavigatableComponent extends JComponent implements Helpful {
53
54 /**
55 * Interface to notify listeners of the change of the zoom area.
56 */
57 public interface ZoomChangeListener {
58 void zoomChanged();
59 }
60
61 public static final IntegerProperty PROP_SNAP_DISTANCE = new IntegerProperty("mappaint.node.snap-distance", 10);
62
63 /**
64 * the zoom listeners
65 */
66 private static final CopyOnWriteArrayList<ZoomChangeListener> zoomChangeListeners = new CopyOnWriteArrayList<ZoomChangeListener>();
67
68 /**
69 * Removes a zoom change listener
70 *
71 * @param listener the listener. Ignored if null or already absent
72 */
73 public static void removeZoomChangeListener(NavigatableComponent.ZoomChangeListener listener) {
74 zoomChangeListeners.remove(listener);
75 }
76
77 /**
78 * Adds a zoom change listener
79 *
80 * @param listener the listener. Ignored if null or already registered.
81 */
82 public static void addZoomChangeListener(NavigatableComponent.ZoomChangeListener listener) {
83 if (listener != null) {
84 zoomChangeListeners.addIfAbsent(listener);
85 }
86 }
87
88 protected static void fireZoomChanged() {
89 for (ZoomChangeListener l : zoomChangeListeners) {
90 l.zoomChanged();
91 }
92 }
93
94 /**
95 * The scale factor in x or y-units per pixel. This means, if scale = 10,
96 * every physical pixel on screen are 10 x or 10 y units in the
97 * northing/easting space of the projection.
98 */
99 private double scale = Main.getProjection().getDefaultZoomInPPD();
100 /**
101 * Center n/e coordinate of the desired screen center.
102 */
103 protected EastNorth center = calculateDefaultCenter();
104
105 public NavigatableComponent() {
106 setLayout(null);
107 }
108
109 protected DataSet getCurrentDataSet() {
110 return Main.main.getCurrentDataSet();
111 }
112
113 private EastNorth calculateDefaultCenter() {
114 Bounds b = Main.getProjection().getWorldBoundsLatLon();
115 double lat = (b.getMax().lat() + b.getMin().lat())/2;
116 double lon = (b.getMax().lon() + b.getMin().lon())/2;
117
118 return Main.getProjection().latlon2eastNorth(new LatLon(lat, lon));
119 }
120
121 public static String getDistText(double dist) {
122 return getSystemOfMeasurement().getDistText(dist);
123 }
124
125 public String getDist100PixelText()
126 {
127 return getDistText(getDist100Pixel());
128 }
129
130 public double getDist100Pixel()
131 {
132 int w = getWidth()/2;
133 int h = getHeight()/2;
134 LatLon ll1 = getLatLon(w-50,h);
135 LatLon ll2 = getLatLon(w+50,h);
136 return ll1.greatCircleDistance(ll2);
137 }
138
139 /**
140 * @return Returns the center point. A copy is returned, so users cannot
141 * change the center by accessing the return value. Use zoomTo instead.
142 */
143 public EastNorth getCenter() {
144 return center;
145 }
146
147 /**
148 * @param x X-Pixelposition to get coordinate from
149 * @param y Y-Pixelposition to get coordinate from
150 *
151 * @return Geographic coordinates from a specific pixel coordination
152 * on the screen.
153 */
154 public EastNorth getEastNorth(int x, int y) {
155 return new EastNorth(
156 center.east() + (x - getWidth()/2.0)*scale,
157 center.north() - (y - getHeight()/2.0)*scale);
158 }
159
160 public ProjectionBounds getProjectionBounds() {
161 return new ProjectionBounds(
162 new EastNorth(
163 center.east() - getWidth()/2.0*scale,
164 center.north() - getHeight()/2.0*scale),
165 new EastNorth(
166 center.east() + getWidth()/2.0*scale,
167 center.north() + getHeight()/2.0*scale));
168 }
169
170 /* FIXME: replace with better method - used by MapSlider */
171 public ProjectionBounds getMaxProjectionBounds() {
172 Bounds b = getProjection().getWorldBoundsLatLon();
173 return new ProjectionBounds(getProjection().latlon2eastNorth(b.getMin()),
174 getProjection().latlon2eastNorth(b.getMax()));
175 }
176
177 /* FIXME: replace with better method - used by Main to reset Bounds when projection changes, don't use otherwise */
178 public Bounds getRealBounds() {
179 return new Bounds(
180 getProjection().eastNorth2latlon(new EastNorth(
181 center.east() - getWidth()/2.0*scale,
182 center.north() - getHeight()/2.0*scale)),
183 getProjection().eastNorth2latlon(new EastNorth(
184 center.east() + getWidth()/2.0*scale,
185 center.north() + getHeight()/2.0*scale)));
186 }
187
188 /**
189 * @param x X-Pixelposition to get coordinate from
190 * @param y Y-Pixelposition to get coordinate from
191 *
192 * @return Geographic unprojected coordinates from a specific pixel coordination
193 * on the screen.
194 */
195 public LatLon getLatLon(int x, int y) {
196 return getProjection().eastNorth2latlon(getEastNorth(x, y));
197 }
198
199 public LatLon getLatLon(double x, double y) {
200 return getLatLon((int)x, (int)y);
201 }
202
203 /**
204 * @param r
205 * @return Minimum bounds that will cover rectangle
206 */
207 public Bounds getLatLonBounds(Rectangle r) {
208 // TODO Maybe this should be (optional) method of Projection implementation
209 EastNorth p1 = getEastNorth(r.x, r.y);
210 EastNorth p2 = getEastNorth(r.x + r.width, r.y + r.height);
211
212 Bounds result = new Bounds(Main.getProjection().eastNorth2latlon(p1));
213
214 double eastMin = Math.min(p1.east(), p2.east());
215 double eastMax = Math.max(p1.east(), p2.east());
216 double northMin = Math.min(p1.north(), p2.north());
217 double northMax = Math.max(p1.north(), p2.north());
218 double deltaEast = (eastMax - eastMin) / 10;
219 double deltaNorth = (northMax - northMin) / 10;
220
221 for (int i=0; i < 10; i++) {
222 result.extend(Main.getProjection().eastNorth2latlon(new EastNorth(eastMin + i * deltaEast, northMin)));
223 result.extend(Main.getProjection().eastNorth2latlon(new EastNorth(eastMin + i * deltaEast, northMax)));
224 result.extend(Main.getProjection().eastNorth2latlon(new EastNorth(eastMin, northMin + i * deltaNorth)));
225 result.extend(Main.getProjection().eastNorth2latlon(new EastNorth(eastMax, northMin + i * deltaNorth)));
226 }
227
228 return result;
229 }
230
231 /**
232 * Return the point on the screen where this Coordinate would be.
233 * @param p The point, where this geopoint would be drawn.
234 * @return The point on screen where "point" would be drawn, relative
235 * to the own top/left.
236 */
237 public Point2D getPoint2D(EastNorth p) {
238 if (null == p)
239 return new Point();
240 double x = (p.east()-center.east())/scale + getWidth()/2;
241 double y = (center.north()-p.north())/scale + getHeight()/2;
242 return new Point2D.Double(x, y);
243 }
244
245 public Point2D getPoint2D(LatLon latlon) {
246 if (latlon == null)
247 return new Point();
248 else if (latlon instanceof CachedLatLon)
249 return getPoint2D(((CachedLatLon)latlon).getEastNorth());
250 else
251 return getPoint2D(getProjection().latlon2eastNorth(latlon));
252 }
253
254 public Point2D getPoint2D(Node n) {
255 return getPoint2D(n.getEastNorth());
256 }
257
258 // looses precision, may overflow (depends on p and current scale)
259 //@Deprecated
260 public Point getPoint(EastNorth p) {
261 Point2D d = getPoint2D(p);
262 return new Point((int) d.getX(), (int) d.getY());
263 }
264
265 // looses precision, may overflow (depends on p and current scale)
266 //@Deprecated
267 public Point getPoint(LatLon latlon) {
268 Point2D d = getPoint2D(latlon);
269 return new Point((int) d.getX(), (int) d.getY());
270 }
271
272 // looses precision, may overflow (depends on p and current scale)
273 //@Deprecated
274 public Point getPoint(Node n) {
275 Point2D d = getPoint2D(n);
276 return new Point((int) d.getX(), (int) d.getY());
277 }
278
279 /**
280 * Zoom to the given coordinate.
281 * @param newCenter The center x-value (easting) to zoom to.
282 * @param scale The scale to use.
283 */
284 public void zoomTo(EastNorth newCenter, double newScale) {
285 Bounds b = getProjection().getWorldBoundsLatLon();
286 LatLon cl = Projections.inverseProject(newCenter);
287 boolean changed = false;
288 double lat = cl.lat();
289 double lon = cl.lon();
290 if(lat < b.getMin().lat()) {changed = true; lat = b.getMin().lat(); }
291 else if(lat > b.getMax().lat()) {changed = true; lat = b.getMax().lat(); }
292 if(lon < b.getMin().lon()) {changed = true; lon = b.getMin().lon(); }
293 else if(lon > b.getMax().lon()) {changed = true; lon = b.getMax().lon(); }
294 if(changed) {
295 newCenter = Projections.project(new LatLon(lat,lon));
296 }
297 int width = getWidth()/2;
298 int height = getHeight()/2;
299 LatLon l1 = new LatLon(b.getMin().lat(), lon);
300 LatLon l2 = new LatLon(b.getMax().lat(), lon);
301 EastNorth e1 = getProjection().latlon2eastNorth(l1);
302 EastNorth e2 = getProjection().latlon2eastNorth(l2);
303 double d = e2.north() - e1.north();
304 if(d < height*newScale)
305 {
306 double newScaleH = d/height;
307 e1 = getProjection().latlon2eastNorth(new LatLon(lat, b.getMin().lon()));
308 e2 = getProjection().latlon2eastNorth(new LatLon(lat, b.getMax().lon()));
309 d = e2.east() - e1.east();
310 if(d < width*newScale) {
311 newScale = Math.max(newScaleH, d/width);
312 }
313 }
314 else
315 {
316 d = d/(l1.greatCircleDistance(l2)*height*10);
317 if(newScale < d) {
318 newScale = d;
319 }
320 }
321
322 if (!newCenter.equals(center) || (scale != newScale)) {
323 pushZoomUndo(center, scale);
324 zoomNoUndoTo(newCenter, newScale);
325 }
326 }
327
328 /**
329 * Zoom to the given coordinate without adding to the zoom undo buffer.
330 * @param newCenter The center x-value (easting) to zoom to.
331 * @param scale The scale to use.
332 */
333 private void zoomNoUndoTo(EastNorth newCenter, double newScale) {
334 if (!newCenter.equals(center)) {
335 EastNorth oldCenter = center;
336 center = newCenter;
337 firePropertyChange("center", oldCenter, newCenter);
338 }
339 if (scale != newScale) {
340 double oldScale = scale;
341 scale = newScale;
342 firePropertyChange("scale", oldScale, newScale);
343 }
344
345 repaint();
346 fireZoomChanged();
347 }
348
349 public void zoomTo(EastNorth newCenter) {
350 zoomTo(newCenter, scale);
351 }
352
353 public void zoomTo(LatLon newCenter) {
354 zoomTo(Projections.project(newCenter));
355 }
356
357 public void smoothScrollTo(LatLon newCenter) {
358 smoothScrollTo(Projections.project(newCenter));
359 }
360
361 /**
362 * Create a thread that moves the viewport to the given center in an
363 * animated fashion.
364 */
365 public void smoothScrollTo(EastNorth newCenter) {
366 // fixme make these configurable.
367 final int fps = 20; // animation frames per second
368 final int speed = 1500; // milliseconds for full-screen-width pan
369 if (!newCenter.equals(center)) {
370 final EastNorth oldCenter = center;
371 final double distance = newCenter.distance(oldCenter) / scale;
372 final double milliseconds = distance / getWidth() * speed;
373 final double frames = milliseconds * fps / 1000;
374 final EastNorth finalNewCenter = newCenter;
375
376 new Thread(
377 new Runnable() {
378 public void run() {
379 for (int i=0; i<frames; i++)
380 {
381 // fixme - not use zoom history here
382 zoomTo(oldCenter.interpolate(finalNewCenter, (i+1) / frames));
383 try { Thread.sleep(1000 / fps); } catch (InterruptedException ex) { };
384 }
385 }
386 }
387 ).start();
388 }
389 }
390
391 public void zoomToFactor(double x, double y, double factor) {
392 double newScale = scale*factor;
393 // New center position so that point under the mouse pointer stays the same place as it was before zooming
394 // You will get the formula by simplifying this expression: newCenter = oldCenter + mouseCoordinatesInNewZoom - mouseCoordinatesInOldZoom
395 zoomTo(new EastNorth(
396 center.east() - (x - getWidth()/2.0) * (newScale - scale),
397 center.north() + (y - getHeight()/2.0) * (newScale - scale)),
398 newScale);
399 }
400
401 public void zoomToFactor(EastNorth newCenter, double factor) {
402 zoomTo(newCenter, scale*factor);
403 }
404
405 public void zoomToFactor(double factor) {
406 zoomTo(center, scale*factor);
407 }
408
409 public void zoomTo(ProjectionBounds box) {
410 // -20 to leave some border
411 int w = getWidth()-20;
412 if (w < 20) {
413 w = 20;
414 }
415 int h = getHeight()-20;
416 if (h < 20) {
417 h = 20;
418 }
419
420 double scaleX = (box.maxEast-box.minEast)/w;
421 double scaleY = (box.maxNorth-box.minNorth)/h;
422 double newScale = Math.max(scaleX, scaleY);
423
424 zoomTo(box.getCenter(), newScale);
425 }
426
427 public void zoomTo(Bounds box) {
428 zoomTo(new ProjectionBounds(getProjection().latlon2eastNorth(box.getMin()),
429 getProjection().latlon2eastNorth(box.getMax())));
430 }
431
432 private class ZoomData {
433 LatLon center;
434 double scale;
435
436 public ZoomData(EastNorth center, double scale) {
437 this.center = Projections.inverseProject(center);
438 this.scale = scale;
439 }
440
441 public EastNorth getCenterEastNorth() {
442 return getProjection().latlon2eastNorth(center);
443 }
444
445 public double getScale() {
446 return scale;
447 }
448 }
449
450 private Stack<ZoomData> zoomUndoBuffer = new Stack<ZoomData>();
451 private Stack<ZoomData> zoomRedoBuffer = new Stack<ZoomData>();
452 private Date zoomTimestamp = new Date();
453
454 private void pushZoomUndo(EastNorth center, double scale) {
455 Date now = new Date();
456 if ((now.getTime() - zoomTimestamp.getTime()) > (Main.pref.getDouble("zoom.undo.delay", 1.0) * 1000)) {
457 zoomUndoBuffer.push(new ZoomData(center, scale));
458 if (zoomUndoBuffer.size() > Main.pref.getInteger("zoom.undo.max", 50)) {
459 zoomUndoBuffer.remove(0);
460 }
461 zoomRedoBuffer.clear();
462 }
463 zoomTimestamp = now;
464 }
465
466 public void zoomPrevious() {
467 if (!zoomUndoBuffer.isEmpty()) {
468 ZoomData zoom = zoomUndoBuffer.pop();
469 zoomRedoBuffer.push(new ZoomData(center, scale));
470 zoomNoUndoTo(zoom.getCenterEastNorth(), zoom.getScale());
471 }
472 }
473
474 public void zoomNext() {
475 if (!zoomRedoBuffer.isEmpty()) {
476 ZoomData zoom = zoomRedoBuffer.pop();
477 zoomUndoBuffer.push(new ZoomData(center, scale));
478 zoomNoUndoTo(zoom.getCenterEastNorth(), zoom.getScale());
479 }
480 }
481
482 public boolean hasZoomUndoEntries() {
483 return !zoomUndoBuffer.isEmpty();
484 }
485
486 public boolean hasZoomRedoEntries() {
487 return !zoomRedoBuffer.isEmpty();
488 }
489
490 private BBox getBBox(Point p, int snapDistance) {
491 return new BBox(getLatLon(p.x - snapDistance, p.y - snapDistance),
492 getLatLon(p.x + snapDistance, p.y + snapDistance));
493 }
494
495 /**
496 * The *result* does not depend on the current map selection state,
497 * neither does the result *order*.
498 * It solely depends on the distance to point p.
499 *
500 * @return a sorted map with the keys representing the distance of
501 * their associated nodes to point p.
502 */
503 private Map<Double, List<Node>> getNearestNodesImpl(Point p,
504 Predicate<OsmPrimitive> predicate) {
505 TreeMap<Double, List<Node>> nearestMap = new TreeMap<Double, List<Node>>();
506 DataSet ds = getCurrentDataSet();
507
508 if (ds != null) {
509 double dist, snapDistanceSq = PROP_SNAP_DISTANCE.get();
510 snapDistanceSq *= snapDistanceSq;
511
512 for (Node n : ds.searchNodes(getBBox(p, PROP_SNAP_DISTANCE.get()))) {
513 if (predicate.evaluate(n)
514 && (dist = getPoint2D(n).distanceSq(p)) < snapDistanceSq)
515 {
516 List<Node> nlist;
517 if (nearestMap.containsKey(dist)) {
518 nlist = nearestMap.get(dist);
519 } else {
520 nlist = new LinkedList<Node>();
521 nearestMap.put(dist, nlist);
522 }
523 nlist.add(n);
524 }
525 }
526 }
527
528 return nearestMap;
529 }
530
531 /**
532 * The *result* does not depend on the current map selection state,
533 * neither does the result *order*.
534 * It solely depends on the distance to point p.
535 *
536 * @return All nodes nearest to point p that are in a belt from
537 * dist(nearest) to dist(nearest)+4px around p and
538 * that are not in ignore.
539 *
540 * @param p the point for which to search the nearest segment.
541 * @param ignore a collection of nodes which are not to be returned.
542 * @param predicate the returned objects have to fulfill certain properties.
543 */
544 public final List<Node> getNearestNodes(Point p,
545 Collection<Node> ignore, Predicate<OsmPrimitive> predicate) {
546 List<Node> nearestList = Collections.emptyList();
547
548 if (ignore == null) {
549 ignore = Collections.emptySet();
550 }
551
552 Map<Double, List<Node>> nlists = getNearestNodesImpl(p, predicate);
553 if (!nlists.isEmpty()) {
554 Double minDistSq = null;
555 List<Node> nlist;
556 for (Double distSq : nlists.keySet()) {
557 nlist = nlists.get(distSq);
558
559 // filter nodes to be ignored before determining minDistSq..
560 nlist.removeAll(ignore);
561 if (minDistSq == null) {
562 if (!nlist.isEmpty()) {
563 minDistSq = distSq;
564 nearestList = new ArrayList<Node>();
565 nearestList.addAll(nlist);
566 }
567 } else {
568 if (distSq-minDistSq < (4)*(4)) {
569 nearestList.addAll(nlist);
570 }
571 }
572 }
573 }
574
575 return nearestList;
576 }
577
578 /**
579 * The *result* does not depend on the current map selection state,
580 * neither does the result *order*.
581 * It solely depends on the distance to point p.
582 *
583 * @return All nodes nearest to point p that are in a belt from
584 * dist(nearest) to dist(nearest)+4px around p.
585 * @see #getNearestNodes(Point, Collection, Predicate)
586 *
587 * @param p the point for which to search the nearest segment.
588 * @param predicate the returned objects have to fulfill certain properties.
589 */
590 public final List<Node> getNearestNodes(Point p, Predicate<OsmPrimitive> predicate) {
591 return getNearestNodes(p, null, predicate);
592 }
593
594 /**
595 * The *result* depends on the current map selection state IF use_selected is true.
596 *
597 * If more than one node within node.snap-distance pixels is found,
598 * the nearest node selected is returned IF use_selected is true.
599 *
600 * Else the nearest new/id=0 node within about the same distance
601 * as the true nearest node is returned.
602 *
603 * If no such node is found either, the true nearest
604 * node to p is returned.
605 *
606 * Finally, if a node is not found at all, null is returned.
607 *
608 * @return A node within snap-distance to point p,
609 * that is chosen by the algorithm described.
610 *
611 * @param p the screen point
612 * @param predicate this parameter imposes a condition on the returned object, e.g.
613 * give the nearest node that is tagged.
614 */
615 public final Node getNearestNode(Point p, Predicate<OsmPrimitive> predicate, boolean use_selected) {
616 Node n = null;
617
618 Map<Double, List<Node>> nlists = getNearestNodesImpl(p, predicate);
619 if (!nlists.isEmpty()) {
620 Node ntsel = null, ntnew = null;
621 double minDistSq = nlists.keySet().iterator().next();
622
623 for (Double distSq : nlists.keySet()) {
624 for (Node nd : nlists.get(distSq)) {
625 // find the nearest selected node
626 if (ntsel == null && nd.isSelected()) {
627 ntsel = nd;
628 // if there are multiple nearest nodes, prefer the one
629 // that is selected. This is required in order to drag
630 // the selected node if multiple nodes have the same
631 // coordinates (e.g. after unglue)
632 use_selected |= (distSq == minDistSq);
633 }
634 // find the nearest newest node that is within about the same
635 // distance as the true nearest node
636 if (ntnew == null && nd.isNew() && (distSq-minDistSq < 1)) {
637 ntnew = nd;
638 }
639 }
640 }
641
642 // take nearest selected, nearest new or true nearest node to p, in that order
643 n = (ntsel != null && use_selected) ? ntsel
644 : (ntnew != null) ? ntnew
645 : nlists.values().iterator().next().get(0);
646 }
647 return n;
648 }
649
650 /**
651 * Convenience method to {@link #getNearestNode(Point, Predicate, boolean)}.
652 *
653 * @return The nearest node to point p.
654 */
655 public final Node getNearestNode(Point p, Predicate<OsmPrimitive> predicate) {
656 return getNearestNode(p, predicate, true);
657 }
658
659 @Deprecated
660 public final Node getNearestNode(Point p) {
661 return getNearestNode(p, OsmPrimitive.isUsablePredicate);
662 }
663
664 /**
665 * The *result* does not depend on the current map selection state,
666 * neither does the result *order*.
667 * It solely depends on the distance to point p.
668 *
669 * @return a sorted map with the keys representing the perpendicular
670 * distance of their associated way segments to point p.
671 */
672 private Map<Double, List<WaySegment>> getNearestWaySegmentsImpl(Point p,
673 Predicate<OsmPrimitive> predicate) {
674 Map<Double, List<WaySegment>> nearestMap = new TreeMap<Double, List<WaySegment>>();
675 DataSet ds = getCurrentDataSet();
676
677 if (ds != null) {
678 double snapDistanceSq = Main.pref.getInteger("mappaint.segment.snap-distance", 10);
679 snapDistanceSq *= snapDistanceSq;
680
681 for (Way w : ds.searchWays(getBBox(p, Main.pref.getInteger("mappaint.segment.snap-distance", 10)))) {
682 if (!predicate.evaluate(w)) {
683 continue;
684 }
685 Node lastN = null;
686 int i = -2;
687 for (Node n : w.getNodes()) {
688 i++;
689 if (n.isDeleted() || n.isIncomplete()) { //FIXME: This shouldn't happen, raise exception?
690 continue;
691 }
692 if (lastN == null) {
693 lastN = n;
694 continue;
695 }
696
697 Point2D A = getPoint2D(lastN);
698 Point2D B = getPoint2D(n);
699 double c = A.distanceSq(B);
700 double a = p.distanceSq(B);
701 double b = p.distanceSq(A);
702
703 /* perpendicular distance squared
704 * loose some precision to account for possible deviations in the calculation above
705 * e.g. if identical (A and B) come about reversed in another way, values may differ
706 * -- zero out least significant 32 dual digits of mantissa..
707 */
708 double perDistSq = Double.longBitsToDouble(
709 Double.doubleToLongBits( a - (a - b + c) * (a - b + c) / 4 / c )
710 >> 32 << 32); // resolution in numbers with large exponent not needed here..
711
712 if (perDistSq < snapDistanceSq && a < c + snapDistanceSq && b < c + snapDistanceSq) {
713 //System.err.println(Double.toHexString(perDistSq));
714
715 List<WaySegment> wslist;
716 if (nearestMap.containsKey(perDistSq)) {
717 wslist = nearestMap.get(perDistSq);
718 } else {
719 wslist = new LinkedList<WaySegment>();
720 nearestMap.put(perDistSq, wslist);
721 }
722 wslist.add(new WaySegment(w, i));
723 }
724
725 lastN = n;
726 }
727 }
728 }
729
730 return nearestMap;
731 }
732
733 /**
734 * The result *order* depends on the current map selection state.
735 * Segments within 10px of p are searched and sorted by their distance to @param p,
736 * then, within groups of equally distant segments, prefer those that are selected.
737 *
738 * @return all segments within 10px of p that are not in ignore,
739 * sorted by their perpendicular distance.
740 *
741 * @param p the point for which to search the nearest segments.
742 * @param ignore a collection of segments which are not to be returned.
743 * @param predicate the returned objects have to fulfill certain properties.
744 */
745 public final List<WaySegment> getNearestWaySegments(Point p,
746 Collection<WaySegment> ignore, Predicate<OsmPrimitive> predicate) {
747 List<WaySegment> nearestList = new ArrayList<WaySegment>();
748 List<WaySegment> unselected = new LinkedList<WaySegment>();
749
750 for (List<WaySegment> wss : getNearestWaySegmentsImpl(p, predicate).values()) {
751 // put selected waysegs within each distance group first
752 // makes the order of nearestList dependent on current selection state
753 for (WaySegment ws : wss) {
754 (ws.way.isSelected() ? nearestList : unselected).add(ws);
755 }
756 nearestList.addAll(unselected);
757 unselected.clear();
758 }
759 if (ignore != null) {
760 nearestList.removeAll(ignore);
761 }
762
763 return nearestList;
764 }
765
766 /**
767 * The result *order* depends on the current map selection state.
768 *
769 * @return all segments within 10px of p, sorted by their perpendicular distance.
770 * @see #getNearestWaySegments(Point, Collection, Predicate)
771 *
772 * @param p the point for which to search the nearest segments.
773 * @param predicate the returned objects have to fulfill certain properties.
774 */
775 public final List<WaySegment> getNearestWaySegments(Point p, Predicate<OsmPrimitive> predicate) {
776 return getNearestWaySegments(p, null, predicate);
777 }
778
779 /**
780 * The *result* depends on the current map selection state IF use_selected is true.
781 *
782 * @return The nearest way segment to point p,
783 * and, depending on use_selected, prefers a selected way segment, if found.
784 * @see #getNearestWaySegments(Point, Collection, Predicate)
785 *
786 * @param p the point for which to search the nearest segment.
787 * @param predicate the returned object has to fulfill certain properties.
788 * @param use_selected whether selected way segments should be preferred.
789 */
790 public final WaySegment getNearestWaySegment(Point p, Predicate<OsmPrimitive> predicate, boolean use_selected) {
791 WaySegment wayseg = null, ntsel = null;
792
793 for (List<WaySegment> wslist : getNearestWaySegmentsImpl(p, predicate).values()) {
794 if (wayseg != null && ntsel != null) {
795 break;
796 }
797 for (WaySegment ws : wslist) {
798 if (wayseg == null) {
799 wayseg = ws;
800 }
801 if (ntsel == null && ws.way.isSelected()) {
802 ntsel = ws;
803 }
804 }
805 }
806
807 return (ntsel != null && use_selected) ? ntsel : wayseg;
808 }
809
810 /**
811 * Convenience method to {@link #getNearestWaySegment(Point, Predicate, boolean)}.
812 *
813 * @return The nearest way segment to point p.
814 */
815 public final WaySegment getNearestWaySegment(Point p, Predicate<OsmPrimitive> predicate) {
816 return getNearestWaySegment(p, predicate, true);
817 }
818
819 /**
820 * The *result* does not depend on the current map selection state,
821 * neither does the result *order*.
822 * It solely depends on the perpendicular distance to point p.
823 *
824 * @return all nearest ways to the screen point given that are not in ignore.
825 * @see #getNearestWaySegments(Point, Collection, Predicate)
826 *
827 * @param p the point for which to search the nearest ways.
828 * @param ignore a collection of ways which are not to be returned.
829 * @param predicate the returned object has to fulfill certain properties.
830 */
831 public final List<Way> getNearestWays(Point p,
832 Collection<Way> ignore, Predicate<OsmPrimitive> predicate) {
833 List<Way> nearestList = new ArrayList<Way>();
834 Set<Way> wset = new HashSet<Way>();
835
836 for (List<WaySegment> wss : getNearestWaySegmentsImpl(p, predicate).values()) {
837 for (WaySegment ws : wss) {
838 if (wset.add(ws.way)) {
839 nearestList.add(ws.way);
840 }
841 }
842 }
843 if (ignore != null) {
844 nearestList.removeAll(ignore);
845 }
846
847 return nearestList;
848 }
849
850 /**
851 * The *result* does not depend on the current map selection state,
852 * neither does the result *order*.
853 * It solely depends on the perpendicular distance to point p.
854 *
855 * @return all nearest ways to the screen point given.
856 * @see #getNearestWays(Point, Collection, Predicate)
857 *
858 * @param p the point for which to search the nearest ways.
859 * @param predicate the returned object has to fulfill certain properties.
860 */
861 public final List<Way> getNearestWays(Point p, Predicate<OsmPrimitive> predicate) {
862 return getNearestWays(p, null, predicate);
863 }
864
865 /**
866 * The *result* depends on the current map selection state.
867 *
868 * @return The nearest way to point p,
869 * prefer a selected way if there are multiple nearest.
870 * @see #getNearestWaySegment(Point, Collection, Predicate)
871 *
872 * @param p the point for which to search the nearest segment.
873 * @param predicate the returned object has to fulfill certain properties.
874 */
875 public final Way getNearestWay(Point p, Predicate<OsmPrimitive> predicate) {
876 WaySegment nearestWaySeg = getNearestWaySegment(p, predicate);
877 return (nearestWaySeg == null) ? null : nearestWaySeg.way;
878 }
879
880 @Deprecated
881 public final Way getNearestWay(Point p) {
882 return getNearestWay(p, OsmPrimitive.isUsablePredicate);
883 }
884
885 /**
886 * The *result* does not depend on the current map selection state,
887 * neither does the result *order*.
888 * It solely depends on the distance to point p.
889 *
890 * First, nodes will be searched. If there are nodes within BBox found,
891 * return a collection of those nodes only.
892 *
893 * If no nodes are found, search for nearest ways. If there are ways
894 * within BBox found, return a collection of those ways only.
895 *
896 * If nothing is found, return an empty collection.
897 *
898 * @return Primitives nearest to the given screen point that are not in ignore.
899 * @see #getNearestNodes(Point, Collection, Predicate)
900 * @see #getNearestWays(Point, Collection, Predicate)
901 *
902 * @param p The point on screen.
903 * @param ignore a collection of ways which are not to be returned.
904 * @param predicate the returned object has to fulfill certain properties.
905 */
906 public final List<OsmPrimitive> getNearestNodesOrWays(Point p,
907 Collection<OsmPrimitive> ignore, Predicate<OsmPrimitive> predicate) {
908 List<OsmPrimitive> nearestList = Collections.emptyList();
909 OsmPrimitive osm = getNearestNodeOrWay(p, predicate, false);
910
911 if (osm != null) {
912 if (osm instanceof Node) {
913 nearestList = new ArrayList<OsmPrimitive>(getNearestNodes(p, predicate));
914 } else if (osm instanceof Way) {
915 nearestList = new ArrayList<OsmPrimitive>(getNearestWays(p, predicate));
916 }
917 if (ignore != null) {
918 nearestList.removeAll(ignore);
919 }
920 }
921
922 return nearestList;
923 }
924
925 /**
926 * The *result* does not depend on the current map selection state,
927 * neither does the result *order*.
928 * It solely depends on the distance to point p.
929 *
930 * @return Primitives nearest to the given screen point.
931 * @see #getNearests(Point, Collection, Predicate)
932 *
933 * @param p The point on screen.
934 * @param predicate the returned object has to fulfill certain properties.
935 */
936 public final List<OsmPrimitive> getNearestNodesOrWays(Point p, Predicate<OsmPrimitive> predicate) {
937 return getNearestNodesOrWays(p, null, predicate);
938 }
939
940 /**
941 * This is used as a helper routine to {@link #getNearestNodeOrWay(Point, Predicate, boolean)}
942 * It decides, whether to yield the node to be tested or look for further (way) candidates.
943 *
944 * @return true, if the node fulfills the properties of the function body
945 *
946 * @param osm node to check
947 * @param p point clicked
948 * @param use_selected whether to prefer selected nodes
949 */
950 private boolean isPrecedenceNode(Node osm, Point p, boolean use_selected) {
951 boolean ret = false;
952
953 if (osm != null) {
954 ret |= !(p.distanceSq(getPoint2D(osm)) > (4)*(4));
955 ret |= osm.isTagged();
956 if (use_selected) {
957 ret |= osm.isSelected();
958 }
959 }
960
961 return ret;
962 }
963
964 /**
965 * The *result* depends on the current map selection state IF use_selected is true.
966 *
967 * IF use_selected is true, use {@link #getNearestNode(Point, Predicate)} to find
968 * the nearest, selected node. If not found, try {@link #getNearestWaySegment(Point, Predicate)}
969 * to find the nearest selected way.
970 *
971 * IF use_selected is false, or if no selected primitive was found, do the following.
972 *
973 * If the nearest node found is within 4px of p, simply take it.
974 * Else, find the nearest way segment. Then, if p is closer to its
975 * middle than to the node, take the way segment, else take the node.
976 *
977 * Finally, if no nearest primitive is found at all, return null.
978 *
979 * @return A primitive within snap-distance to point p,
980 * that is chosen by the algorithm described.
981 * @see getNearestNode(Point, Predicate)
982 * @see getNearestNodesImpl(Point, Predicate)
983 * @see getNearestWay(Point, Predicate)
984 *
985 * @param p The point on screen.
986 * @param predicate the returned object has to fulfill certain properties.
987 * @param use_selected whether to prefer primitives that are currently selected.
988 */
989 public final OsmPrimitive getNearestNodeOrWay(Point p, Predicate<OsmPrimitive> predicate, boolean use_selected) {
990 OsmPrimitive osm = getNearestNode(p, predicate, use_selected);
991 WaySegment ws = null;
992
993 if (!isPrecedenceNode((Node)osm, p, use_selected)) {
994 ws = getNearestWaySegment(p, predicate, use_selected);
995
996 if (ws != null) {
997 if ((ws.way.isSelected() && use_selected) || osm == null) {
998 // either (no _selected_ nearest node found, if desired) or no nearest node was found
999 osm = ws.way;
1000 } else {
1001 int maxWaySegLenSq = 3*PROP_SNAP_DISTANCE.get();
1002 maxWaySegLenSq *= maxWaySegLenSq;
1003
1004 Point2D wp1 = getPoint2D(ws.way.getNode(ws.lowerIndex));
1005 Point2D wp2 = getPoint2D(ws.way.getNode(ws.lowerIndex+1));
1006
1007 // is wayseg shorter than maxWaySegLenSq and
1008 // is p closer to the middle of wayseg than to the nearest node?
1009 if (wp1.distanceSq(wp2) < maxWaySegLenSq &&
1010 p.distanceSq(project(0.5, wp1, wp2)) < p.distanceSq(getPoint2D((Node)osm))) {
1011 osm = ws.way;
1012 }
1013 }
1014 }
1015 }
1016
1017 return osm;
1018 }
1019
1020 @Deprecated
1021 public final OsmPrimitive getNearest(Point p, Predicate<OsmPrimitive> predicate) {
1022 return getNearestNodeOrWay(p, predicate, false);
1023 }
1024
1025 @Deprecated
1026 public final Collection<OsmPrimitive> getNearestCollection(Point p, Predicate<OsmPrimitive> predicate) {
1027 return asColl(getNearest(p, predicate));
1028 }
1029
1030 /**
1031 * @return o as collection of o's type.
1032 */
1033 public static <T> Collection<T> asColl(T o) {
1034 if (o == null)
1035 return Collections.emptySet();
1036 return Collections.singleton(o);
1037 }
1038
1039 public static double perDist(Point2D pt, Point2D a, Point2D b) {
1040 if (pt != null && a != null && b != null) {
1041 double pd = (
1042 (a.getX()-pt.getX())*(b.getX()-a.getX()) -
1043 (a.getY()-pt.getY())*(b.getY()-a.getY()) );
1044 return Math.abs(pd) / a.distance(b);
1045 }
1046 return 0d;
1047 }
1048
1049 /**
1050 *
1051 * @param pt point to project onto (ab)
1052 * @param a root of vector
1053 * @param b vector
1054 * @return point of intersection of line given by (ab)
1055 * with its orthogonal line running through pt
1056 */
1057 public static Point2D project(Point2D pt, Point2D a, Point2D b) {
1058 if (pt != null && a != null && b != null) {
1059 double r = ((
1060 (pt.getX()-a.getX())*(b.getX()-a.getX()) +
1061 (pt.getY()-a.getY())*(b.getY()-a.getY()) )
1062 / a.distanceSq(b));
1063 return project(r, a, b);
1064 }
1065 return null;
1066 }
1067
1068 /**
1069 * if r = 0 returns a, if r=1 returns b,
1070 * if r = 0.5 returns center between a and b, etc..
1071 *
1072 * @param r scale value
1073 * @param a root of vector
1074 * @param b vector
1075 * @return new point at a + r*(ab)
1076 */
1077 public static Point2D project(double r, Point2D a, Point2D b) {
1078 Point2D ret = null;
1079
1080 if (a != null && b != null) {
1081 ret = new Point2D.Double(a.getX() + r*(b.getX()-a.getX()),
1082 a.getY() + r*(b.getY()-a.getY()));
1083 }
1084 return ret;
1085 }
1086
1087 /**
1088 * The *result* does not depend on the current map selection state,
1089 * neither does the result *order*.
1090 * It solely depends on the distance to point p.
1091 *
1092 * @return a list of all objects that are nearest to point p and
1093 * not in ignore or an empty list if nothing was found.
1094 *
1095 * @param p The point on screen.
1096 * @param ignore a collection of ways which are not to be returned.
1097 * @param predicate the returned object has to fulfill certain properties.
1098 */
1099 public final List<OsmPrimitive> getAllNearest(Point p,
1100 Collection<OsmPrimitive> ignore, Predicate<OsmPrimitive> predicate) {
1101 List<OsmPrimitive> nearestList = new ArrayList<OsmPrimitive>();
1102 Set<Way> wset = new HashSet<Way>();
1103
1104 for (List<WaySegment> wss : getNearestWaySegmentsImpl(p, predicate).values()) {
1105 for (WaySegment ws : wss) {
1106 if (wset.add(ws.way)) {
1107 nearestList.add(ws.way);
1108 }
1109 }
1110 }
1111 for (List<Node> nlist : getNearestNodesImpl(p, predicate).values()) {
1112 nearestList.addAll(nlist);
1113 }
1114 if (ignore != null) {
1115 nearestList.removeAll(ignore);
1116 }
1117
1118 return nearestList;
1119 }
1120
1121 /**
1122 * The *result* does not depend on the current map selection state,
1123 * neither does the result *order*.
1124 * It solely depends on the distance to point p.
1125 *
1126 * @return a list of all objects that are nearest to point p
1127 * or an empty list if nothing was found.
1128 * @see #getAllNearest(Point, Collection, Predicate)
1129 *
1130 * @param p The point on screen.
1131 * @param predicate the returned object has to fulfill certain properties.
1132 */
1133 public final List<OsmPrimitive> getAllNearest(Point p, Predicate<OsmPrimitive> predicate) {
1134 return getAllNearest(p, null, predicate);
1135 }
1136
1137 /**
1138 * @return The projection to be used in calculating stuff.
1139 */
1140 public Projection getProjection() {
1141 return Main.getProjection();
1142 }
1143
1144 public String helpTopic() {
1145 String n = getClass().getName();
1146 return n.substring(n.lastIndexOf('.')+1);
1147 }
1148
1149 /**
1150 * Return a ID which is unique as long as viewport dimensions are the same
1151 */
1152 public int getViewID() {
1153 String x = center.east() + "_" + center.north() + "_" + scale + "_" +
1154 getWidth() + "_" + getHeight() + "_" + getProjection().toString();
1155 java.util.zip.CRC32 id = new java.util.zip.CRC32();
1156 id.update(x.getBytes());
1157 return (int)id.getValue();
1158 }
1159
1160 public static SystemOfMeasurement getSystemOfMeasurement() {
1161 SystemOfMeasurement som = SYSTEMS_OF_MEASUREMENT.get(ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get());
1162 if (som == null)
1163 return METRIC_SOM;
1164 return som;
1165 }
1166
1167 public static class SystemOfMeasurement {
1168 public final double aValue;
1169 public final double bValue;
1170 public final String aName;
1171 public final String bName;
1172
1173 /**
1174 * System of measurement. Currently covers only length units.
1175 *
1176 * If a quantity x is given in m (x_m) and in unit a (x_a) then it translates as
1177 * x_a == x_m / aValue
1178 */
1179 public SystemOfMeasurement(double aValue, String aName, double bValue, String bName) {
1180 this.aValue = aValue;
1181 this.aName = aName;
1182 this.bValue = bValue;
1183 this.bName = bName;
1184 }
1185
1186 public String getDistText(double dist) {
1187 double a = dist / aValue;
1188 if (!Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false) && a > bValue / aValue) {
1189 double b = dist / bValue;
1190 return String.format(Locale.US, "%." + (b<10 ? 2 : 1) + "f %s", b, bName);
1191 } else if (a < 0.01)
1192 return "< 0.01 " + aName;
1193 else
1194 return String.format(Locale.US, "%." + (a<10 ? 2 : 1) + "f %s", a, aName);
1195 }
1196 }
1197
1198 public static final SystemOfMeasurement METRIC_SOM = new SystemOfMeasurement(1, "m", 1000, "km");
1199 public static final SystemOfMeasurement CHINESE_SOM = new SystemOfMeasurement(1.0/3.0, "\u5e02\u5c3a" /* chi */, 500, "\u5e02\u91cc" /* li */);
1200 public static final SystemOfMeasurement IMPERIAL_SOM = new SystemOfMeasurement(0.3048, "ft", 1609.344, "mi");
1201
1202 public static Map<String, SystemOfMeasurement> SYSTEMS_OF_MEASUREMENT;
1203 static {
1204 SYSTEMS_OF_MEASUREMENT = new LinkedHashMap<String, SystemOfMeasurement>();
1205 SYSTEMS_OF_MEASUREMENT.put(marktr("Metric"), METRIC_SOM);
1206 SYSTEMS_OF_MEASUREMENT.put(marktr("Chinese"), CHINESE_SOM);
1207 SYSTEMS_OF_MEASUREMENT.put(marktr("Imperial"), IMPERIAL_SOM);
1208 }
1209
1210 private class CursorInfo {
1211 public Cursor cursor;
1212 public Object object;
1213 public CursorInfo(Cursor c, Object o) {
1214 cursor = c;
1215 object = o;
1216 }
1217 }
1218
1219 private LinkedList<CursorInfo> Cursors = new LinkedList<CursorInfo>();
1220 /**
1221 * Set new cursor.
1222 */
1223 public void setNewCursor(Cursor cursor, Object reference) {
1224 if(Cursors.size() > 0) {
1225 CursorInfo l = Cursors.getLast();
1226 if(l != null && l.cursor == cursor && l.object == reference)
1227 return;
1228 stripCursors(reference);
1229 }
1230 Cursors.add(new CursorInfo(cursor, reference));
1231 setCursor(cursor);
1232 }
1233 public void setNewCursor(int cursor, Object reference) {
1234 setNewCursor(Cursor.getPredefinedCursor(cursor), reference);
1235 }
1236 /**
1237 * Remove the new cursor and reset to previous
1238 */
1239 public void resetCursor(Object reference) {
1240 if(Cursors.size() == 0) {
1241 setCursor(null);
1242 return;
1243 }
1244 CursorInfo l = Cursors.getLast();
1245 stripCursors(reference);
1246 if(l != null && l.object == reference) {
1247 if(Cursors.size() == 0) {
1248 setCursor(null);
1249 } else {
1250 setCursor(Cursors.getLast().cursor);
1251 }
1252 }
1253 }
1254
1255 private void stripCursors(Object reference) {
1256 LinkedList<CursorInfo> c = new LinkedList<CursorInfo>();
1257 for(CursorInfo i : Cursors) {
1258 if(i.object != reference) {
1259 c.add(i);
1260 }
1261 }
1262 Cursors = c;
1263 }
1264}
Note: See TracBrowser for help on using the repository browser.