source: josm/trunk/src/org/openstreetmap/josm/actions/AlignInLineAction.java@ 6930

Last change on this file since 6930 was 6894, checked in by bastiK, 10 years ago

applied #9605 - Align Nodes in Line moves outer node instead of middle one (patch by Balaitous)

  • Property svn:eol-style set to native
File size: 13.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.List;
14
15import javax.swing.JOptionPane;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.command.Command;
19import org.openstreetmap.josm.command.MoveCommand;
20import org.openstreetmap.josm.command.SequenceCommand;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.gui.Notification;
25import org.openstreetmap.josm.tools.Shortcut;
26
27/**
28 * Aligns all selected nodes into a straight line (useful for
29 * roads that should be straight, but have side roads and
30 * therefore need multiple nodes)
31 *
32 * @author Matthew Newton
33 */
34public final class AlignInLineAction extends JosmAction {
35
36 /**
37 * Constructs a new {@code AlignInLineAction}.
38 */
39 public AlignInLineAction() {
40 super(tr("Align Nodes in Line"), "alignline", tr("Move the selected nodes in to a line."),
41 Shortcut.registerShortcut("tools:alignline", tr("Tool: {0}", tr("Align Nodes in Line")), KeyEvent.VK_L, Shortcut.DIRECT), true);
42 putValue("help", ht("/Action/AlignInLine"));
43 }
44
45 /**
46 * Compute 2 anchor points to align a set of nodes.
47 * If all nodes are part of a same way anchor points are choose farthest relative to this way,
48 * else choose farthest nodes.
49 * @param nodes Nodes to be aligned
50 * @param resultOut Array of size >= 2
51 */
52 private void nodePairFurthestApart(List<Node> nodes, Node[] resultOut) {
53 if(resultOut.length < 2)
54 throw new IllegalArgumentException();
55
56 Node nodea = null;
57 Node nodeb = null;
58
59 // Intersection of all ways referred by each node
60 HashSet<Way> waysRef = null;
61 for(Node n: nodes) {
62 Collection<Way> ref = OsmPrimitive.getFilteredList(n.getReferrers(), Way.class);
63 if(waysRef == null)
64 waysRef = new HashSet<Way>(ref);
65 else
66 waysRef.retainAll(ref);
67 }
68 if(waysRef.size() == 1) {
69 // All nodes are part of the same way. See #9605
70 HashSet<Node> remainNodes = new HashSet<Node>(nodes);
71 Way way = waysRef.iterator().next();
72 for(Node n: way.getNodes()) {
73 if(!remainNodes.contains(n)) continue;
74 if(nodea == null) nodea = n;
75 if(remainNodes.size() == 1) {
76 nodeb = remainNodes.iterator().next();
77 break;
78 }
79 remainNodes.remove(n);
80 }
81 } else {
82 // Find from the selected nodes two that are the furthest apart.
83 // Let's call them A and B.
84 double distance = 0;
85 for (int i = 0; i < nodes.size()-1; i++) {
86 Node n = nodes.get(i);
87 for (int j = i+1; j < nodes.size(); j++) {
88 Node m = nodes.get(j);
89 double dist = Math.sqrt(n.getEastNorth().distance(m.getEastNorth()));
90 if (dist > distance) {
91 nodea = n;
92 nodeb = m;
93 distance = dist;
94 }
95 }
96 }
97 }
98 resultOut[0] = nodea;
99 resultOut[1] = nodeb;
100 }
101
102 private void showWarning() {
103 showWarning(tr("Please select at least three nodes."));
104 }
105
106 private void showWarning(String msg) {
107 new Notification(msg)
108 .setIcon(JOptionPane.INFORMATION_MESSAGE)
109 .show();
110 }
111
112 private static int indexWrap(int size, int i) {
113 i = i % size; // -2 % 5 = -2, -7 % 5 = -2, -5 % 5 = 0
114 if (i < 0) {
115 i = size + i;
116 }
117 return i;
118 }
119 // get the node in w at index i relative to refI
120 private static Node getNodeRelative(Way w, int refI, int i) {
121 int absI = indexWrap(w.getNodesCount(), refI + i);
122 if(w.isClosed() && refI + i < 0) {
123 absI--; // node duplicated in closed ways
124 }
125 return w.getNode(absI);
126 }
127
128 /**
129 * The general algorithm here is to find the two selected nodes
130 * that are furthest apart, and then to align all other selected
131 * nodes onto the straight line between these nodes.
132 */
133
134
135 /**
136 * Operation depends on the selected objects:
137 */
138 @Override
139 public void actionPerformed(ActionEvent e) {
140 if (!isEnabled())
141 return;
142
143 Node[] anchors = new Node[2]; // oh, java I love you so much..
144
145 List<Node> selectedNodes = new ArrayList<Node>(getCurrentDataSet().getSelectedNodes());
146 Collection<Way> selectedWays = getCurrentDataSet().getSelectedWays();
147 List<Node> nodes = new ArrayList<Node>();
148
149 //// Decide what to align based on selection:
150
151 /// Only ways selected -> For each way align their nodes taking care of intersection
152 if(selectedNodes.isEmpty() && !selectedWays.isEmpty()) {
153 alignMultiWay(selectedWays);
154 return;
155 }
156 /// More than 3 nodes selected -> align those nodes
157 else if(selectedNodes.size() >= 3) {
158 nodes.addAll(selectedNodes);
159 // use the nodes furthest apart as anchors
160 nodePairFurthestApart(nodes, anchors);
161 }
162 /// One node selected -> align that node to the relevant neighbors
163 else if (selectedNodes.size() == 1) {
164 Node n = selectedNodes.iterator().next();
165
166 Way w = null;
167 if(selectedWays.size() == 1) {
168 w = selectedWays.iterator().next();
169 if (!w.containsNode(n))
170 // warning
171 return;
172 } else {
173 List<Way> refWays = OsmPrimitive.getFilteredList(n.getReferrers(), Way.class);
174 if (refWays.size() == 1) { // node used in only one way
175 w = refWays.iterator().next();
176 }
177 }
178 if (w == null || w.getNodesCount() < 3)
179 // warning, need at least 3 nodes
180 return;
181
182 // Find anchors
183 int nodeI = w.getNodes().indexOf(n);
184 // End-node in non-circular way selected: align this node with the two neighbors.
185 if ((nodeI == 0 || nodeI == w.getNodesCount()-1) && !w.isClosed()) {
186 int direction = nodeI == 0 ? 1 : -1;
187 anchors[0] = w.getNode(nodeI + direction);
188 anchors[1] = w.getNode(nodeI + direction*2);
189 } else {
190 // o---O---o
191 anchors[0] = getNodeRelative(w, nodeI, 1);
192 anchors[1] = getNodeRelative(w, nodeI, -1);
193 }
194 nodes.add(n);
195 }
196
197 if (anchors[0] == null || anchors[1] == null) {
198 showWarning();
199 return;
200 }
201
202
203 Collection<Command> cmds = new ArrayList<Command>(nodes.size());
204
205 createAlignNodesCommands(anchors, nodes, cmds);
206
207 // Do it!
208 Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Line"), cmds));
209 Main.map.repaint();
210 }
211
212 private void createAlignNodesCommands(Node[] anchors, Collection<Node> nodes, Collection<Command> cmds) {
213 Node nodea = anchors[0];
214 Node nodeb = anchors[1];
215
216 // The anchors are aligned per definition
217 nodes.remove(nodea);
218 nodes.remove(nodeb);
219
220 // Find out co-ords of A and B
221 double ax = nodea.getEastNorth().east();
222 double ay = nodea.getEastNorth().north();
223 double bx = nodeb.getEastNorth().east();
224 double by = nodeb.getEastNorth().north();
225
226 // OK, for each node to move, work out where to move it!
227 for (Node n : nodes) {
228 // Get existing co-ords of node to move
229 double nx = n.getEastNorth().east();
230 double ny = n.getEastNorth().north();
231
232 if (ax == bx) {
233 // Special case if AB is vertical...
234 nx = ax;
235 } else if (ay == by) {
236 // ...or horizontal
237 ny = ay;
238 } else {
239 // Otherwise calculate position by solving y=mx+c
240 double m1 = (by - ay) / (bx - ax);
241 double c1 = ay - (ax * m1);
242 double m2 = (-1) / m1;
243 double c2 = n.getEastNorth().north() - (n.getEastNorth().east() * m2);
244
245 nx = (c2 - c1) / (m1 - m2);
246 ny = (m1 * nx) + c1;
247 }
248 double newX = nx - n.getEastNorth().east();
249 double newY = ny - n.getEastNorth().north();
250 // Add the command to move the node to its new position.
251 cmds.add(new MoveCommand(n, newX, newY));
252 }
253 }
254
255 /**
256 * Align way in case of multiple way #6819
257 * @param ways Collection of way to align
258 */
259 private void alignMultiWay(Collection<Way> ways) {
260 // Collect all nodes and compute line equation
261 HashSet<Node> nodes = new HashSet<Node>();
262 HashMap<Way, Line> lines = new HashMap<Way, Line>();
263 for(Way w: ways) {
264 if(w.firstNode() == w.lastNode()) {
265 showWarning(tr("Can not align a polygon. Abort."));
266 return;
267 }
268 nodes.addAll(w.getNodes());
269 lines.put(w, new Line(w));
270 }
271 Collection<Command> cmds = new ArrayList<Command>(nodes.size());
272 List<Way> referers = new ArrayList<Way>(ways.size());
273 for(Node n: nodes) {
274 referers.clear();
275 for(OsmPrimitive o: n.getReferrers())
276 if(ways.contains(o))
277 referers.add((Way) o);
278 if(referers.size() == 1) {
279 Way way = referers.get(0);
280 if(n == way.firstNode() || n == way.lastNode()) continue;
281 cmds.add(lines.get(way).projectionCommand(n));
282 }
283 else if(referers.size() == 2) {
284 Command cmd = lines.get(referers.get(0)).intersectionCommand(n, lines.get(referers.get(1)));
285 if(cmd == null) {
286 showWarning(tr("Two parallels ways found. Abort."));
287 return;
288 }
289 cmds.add(cmd);
290 }
291 else {
292 showWarning(tr("Intersection of three or more ways can not be solved. Abort."));
293 return;
294 }
295 }
296 Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Line"), cmds));
297 Main.map.repaint();
298 }
299
300 /**
301 * Class that describe a line
302 */
303 private class Line {
304
305 /**
306 * Line equation ax + by + c = 0
307 * Such as a^2 + b^2 = 1, ie (-b, a) is a unit vector of line
308 */
309 private double a, b, c; // Line equation ax+by+c=0
310 /**
311 * (xM, yM) are coordinate of a point of the line
312 */
313 private double xM, yM; // Coordinate of a point of the line
314
315 /**
316 * Init a line equation from a way.
317 * @param way
318 */
319 public Line(Way way) {
320 xM = way.firstNode().getEastNorth().getX();
321 yM = way.firstNode().getEastNorth().getY();
322 double xB = way.lastNode().getEastNorth().getX();
323 double yB = way.lastNode().getEastNorth().getY();
324 a = yB - yM;
325 b = xM - xB;
326 double norm = Math.sqrt(a*a + b*b);
327 if (norm == 0) {
328 norm = 1;
329 }
330 a /= norm;
331 b /= norm;
332 c = -(a*xM + b*yM);
333 }
334
335 /**
336 * Orthogonal projection of a node N along this line.
337 * @param n Node to be projected
338 * @return The command that do the projection of this node
339 */
340 public Command projectionCommand(Node n) {
341 double s = (xM - n.getEastNorth().getX()) * a + (yM - n.getEastNorth().getY()) * b;
342 return new MoveCommand(n, a*s, b*s);
343 }
344
345 /**
346 * Intersection of two line.
347 * @param n Node to move to the intersection
348 * @param other Second line for intersection
349 * @return The command that move the node or null if line are parallels
350 */
351 public Command intersectionCommand(Node n, Line other) {
352 double d = this.a * other.b - other.a * this.b;
353 if(d == 0) return null;
354 double x = (this.b * other.c - other.b * this.c) / d;
355 double y = (other.a * this.c - this.a * other.c) / d;
356 return new MoveCommand(n, x - n.getEastNorth().getX(), y - n.getEastNorth().getY());
357 }
358 }
359
360 @Override
361 protected void updateEnabledState() {
362 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
363 }
364
365 @Override
366 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
367 setEnabled(selection != null && !selection.isEmpty());
368 }
369}
Note: See TracBrowser for help on using the repository browser.