1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import java.awt.Point;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
14 | import org.openstreetmap.josm.gui.MapView;
|
---|
15 | import org.openstreetmap.josm.tools.Geometry;
|
---|
16 | import org.openstreetmap.josm.tools.Pair;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * This static class contains functions used to find target way, node to move or
|
---|
20 | * segment to divide.
|
---|
21 | *
|
---|
22 | * @author Alexander Kachkaev <alexander@kachkaev.ru>, 2011
|
---|
23 | */
|
---|
24 | final class ImproveWayAccuracyHelper {
|
---|
25 |
|
---|
26 | private ImproveWayAccuracyHelper() {
|
---|
27 | // Hide default constructor for utils classes
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Finds the way to work on. If the mouse is on the node, extracts one of
|
---|
32 | * the ways containing it. If the mouse is on the way, simply returns it.
|
---|
33 | *
|
---|
34 | * @param mv the current map view
|
---|
35 | * @param p the cursor position
|
---|
36 | * @return {@code Way} or {@code null} in case there is nothing under the cursor.
|
---|
37 | */
|
---|
38 | public static Way findWay(MapView mv, Point p) {
|
---|
39 | if (mv == null || p == null) {
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 |
|
---|
43 | Node node = mv.getNearestNode(p, OsmPrimitive.isSelectablePredicate);
|
---|
44 | Way candidate = null;
|
---|
45 |
|
---|
46 | if (node != null) {
|
---|
47 | final Collection<OsmPrimitive> candidates = node.getReferrers();
|
---|
48 | for (OsmPrimitive refferer : candidates) {
|
---|
49 | if (refferer instanceof Way) {
|
---|
50 | candidate = (Way) refferer;
|
---|
51 | break;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | if (candidate != null) {
|
---|
55 | return candidate;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | return Main.map.mapView.getNearestWay(p, OsmPrimitive.isSelectablePredicate);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Returns the nearest node to cursor. All nodes that are “behind” segments
|
---|
64 | * are neglected. This is to avoid way self-intersection after moving the
|
---|
65 | * candidateNode to a new place.
|
---|
66 | *
|
---|
67 | * @param mv the current map view
|
---|
68 | * @param w the way to check
|
---|
69 | * @param p the cursor position
|
---|
70 | * @return nearest node to cursor
|
---|
71 | */
|
---|
72 | public static Node findCandidateNode(MapView mv, Way w, Point p) {
|
---|
73 | if (mv == null || w == null || p == null) {
|
---|
74 | return null;
|
---|
75 | }
|
---|
76 |
|
---|
77 | EastNorth pEN = mv.getEastNorth(p.x, p.y);
|
---|
78 |
|
---|
79 | Double bestDistance = Double.MAX_VALUE;
|
---|
80 | Double currentDistance;
|
---|
81 | List<Pair<Node, Node>> wpps = w.getNodePairs(false);
|
---|
82 |
|
---|
83 | Node result = null;
|
---|
84 |
|
---|
85 | mainLoop:
|
---|
86 | for (Node n : w.getNodes()) {
|
---|
87 | EastNorth nEN = n.getEastNorth();
|
---|
88 | currentDistance = pEN.distance(nEN);
|
---|
89 |
|
---|
90 | if (currentDistance < bestDistance) {
|
---|
91 | // Making sure this candidate is not behind any segment.
|
---|
92 | for (Pair<Node, Node> wpp : wpps) {
|
---|
93 | if (!wpp.a.equals(n)
|
---|
94 | && !wpp.b.equals(n)
|
---|
95 | && Geometry.getSegmentSegmentIntersection(
|
---|
96 | wpp.a.getEastNorth(), wpp.b.getEastNorth(),
|
---|
97 | pEN, nEN) != null) {
|
---|
98 | continue mainLoop;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | result = n;
|
---|
102 | bestDistance = currentDistance;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | return result;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Returns the nearest way segment to cursor. The distance to segment ab is
|
---|
111 | * the length of altitude from p to ab (say, c) or the minimum distance from
|
---|
112 | * p to a or b if c is out of ab.
|
---|
113 | *
|
---|
114 | * The priority is given to segments where c is in ab. Otherwise, a segment
|
---|
115 | * with the largest angle apb is chosen.
|
---|
116 | *
|
---|
117 | * @param mv the current map view
|
---|
118 | * @param w the way to check
|
---|
119 | * @param p the cursor position
|
---|
120 | * @return nearest way segment to cursor
|
---|
121 | */
|
---|
122 | public static WaySegment findCandidateSegment(MapView mv, Way w, Point p) {
|
---|
123 | if (mv == null || w == null || p == null) {
|
---|
124 | return null;
|
---|
125 | }
|
---|
126 |
|
---|
127 | EastNorth pEN = mv.getEastNorth(p.x, p.y);
|
---|
128 |
|
---|
129 | Double currentDistance;
|
---|
130 | Double currentAngle;
|
---|
131 | Double bestDistance = Double.MAX_VALUE;
|
---|
132 | Double bestAngle = 0.0;
|
---|
133 |
|
---|
134 | int candidate = -1;
|
---|
135 |
|
---|
136 | List<Pair<Node, Node>> wpps = w.getNodePairs(true);
|
---|
137 |
|
---|
138 | int i = -1;
|
---|
139 | for (Pair<Node, Node> wpp : wpps) {
|
---|
140 | ++i;
|
---|
141 |
|
---|
142 | EastNorth a = wpp.a.getEastNorth();
|
---|
143 | EastNorth b = wpp.b.getEastNorth();
|
---|
144 |
|
---|
145 | // Finding intersection of the segment with its altitude from p
|
---|
146 | EastNorth altitudeIntersection = Geometry.closestPointToSegment(a, b, pEN);
|
---|
147 | currentDistance = pEN.distance(altitudeIntersection);
|
---|
148 |
|
---|
149 | if (!altitudeIntersection.equals(a) && !altitudeIntersection.equals(b)) {
|
---|
150 | // If the segment intersects with the altitude from p,
|
---|
151 | // make an angle too big to let this candidate win any others
|
---|
152 | // having the same distance.
|
---|
153 | currentAngle = Double.MAX_VALUE;
|
---|
154 | } else {
|
---|
155 | // Otherwise measure the angle
|
---|
156 | currentAngle = Math.abs(Geometry.getCornerAngle(a, pEN, b));
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (currentDistance < bestDistance
|
---|
160 | || (currentAngle > bestAngle && currentDistance < bestDistance * 1.0001 /*
|
---|
161 | * equality
|
---|
162 | */)) {
|
---|
163 | candidate = i;
|
---|
164 | bestAngle = currentAngle;
|
---|
165 | bestDistance = currentDistance;
|
---|
166 | }
|
---|
167 |
|
---|
168 | }
|
---|
169 | return candidate != -1 ? new WaySegment(w, candidate) : null;
|
---|
170 | }
|
---|
171 | }
|
---|