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

Last change on this file since 6215 was 6130, checked in by bastiK, 11 years ago

see #6963 - converted popups to notifications for all actions in the Tools menu (TODO: Simplify Way)

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.List;
12
13import javax.swing.JOptionPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.command.Command;
17import org.openstreetmap.josm.command.MoveCommand;
18import org.openstreetmap.josm.command.SequenceCommand;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.Way;
22import org.openstreetmap.josm.gui.Notification;
23import org.openstreetmap.josm.tools.Shortcut;
24
25/**
26 * Aligns all selected nodes into a straight line (useful for
27 * roads that should be straight, but have side roads and
28 * therefore need multiple nodes)
29 *
30 * @author Matthew Newton
31 */
32public final class AlignInLineAction extends JosmAction {
33
34 public AlignInLineAction() {
35 super(tr("Align Nodes in Line"), "alignline", tr("Move the selected nodes in to a line."),
36 Shortcut.registerShortcut("tools:alignline", tr("Tool: {0}", tr("Align Nodes in Line")), KeyEvent.VK_L, Shortcut.DIRECT), true);
37 putValue("help", ht("/Action/AlignInLine"));
38 }
39
40 // the joy of single return values only...
41 private void nodePairFurthestApart(ArrayList<Node> nodes, Node[] resultOut) {
42 if(resultOut.length < 2)
43 throw new IllegalArgumentException();
44 // Find from the selected nodes two that are the furthest apart.
45 // Let's call them A and B.
46 double distance = 0;
47
48 Node nodea = null;
49 Node nodeb = null;
50
51 for (int i = 0; i < nodes.size()-1; i++) {
52 Node n = nodes.get(i);
53 for (int j = i+1; j < nodes.size(); j++) {
54 Node m = nodes.get(j);
55 double dist = Math.sqrt(n.getEastNorth().distance(m.getEastNorth()));
56 if (dist > distance) {
57 nodea = n;
58 nodeb = m;
59 distance = dist;
60 }
61 }
62 }
63 resultOut[0] = nodea;
64 resultOut[1] = nodeb;
65 }
66
67 private void showWarning() {
68 new Notification(
69 tr("Please select at least three nodes."))
70 .setIcon(JOptionPane.INFORMATION_MESSAGE)
71 .show();
72 }
73
74 private static int indexWrap(int size, int i) {
75 i = i % size; // -2 % 5 = -2, -7 % 5 = -2, -5 % 5 = 0
76 if (i < 0) {
77 i = size + i;
78 }
79 return i;
80 }
81 // get the node in w at index i relative to refI
82 private static Node getNodeRelative(Way w, int refI, int i) {
83 int absI = indexWrap(w.getNodesCount(), refI + i);
84 if(w.isClosed() && refI + i < 0) {
85 absI--; // node duplicated in closed ways
86 }
87 return w.getNode(absI);
88 }
89
90 /**
91 * The general algorithm here is to find the two selected nodes
92 * that are furthest apart, and then to align all other selected
93 * nodes onto the straight line between these nodes.
94 */
95
96
97 /**
98 * Operation depends on the selected objects:
99 */
100 @Override
101 public void actionPerformed(ActionEvent e) {
102 if (!isEnabled())
103 return;
104
105 Node[] anchors = new Node[2]; // oh, java I love you so much..
106
107 List<Node> selectedNodes = new ArrayList<Node>(getCurrentDataSet().getSelectedNodes());
108 Collection<Way> selectedWays = getCurrentDataSet().getSelectedWays();
109 ArrayList<Node> nodes = new ArrayList<Node>();
110
111 //// Decide what to align based on selection:
112
113 /// Only ways selected -> Align their nodes.
114 if ((selectedNodes.isEmpty()) && (selectedWays.size() == 1)) { // TODO: handle multiple ways
115 for (Way way : selectedWays) {
116 nodes.addAll(way.getNodes());
117 }
118 // use the nodes furthest apart as anchors
119 nodePairFurthestApart(nodes, anchors);
120 }
121 /// More than 3 nodes selected -> align those nodes
122 else if(selectedNodes.size() >= 3) {
123 nodes.addAll(selectedNodes);
124 // use the nodes furthest apart as anchors
125 nodePairFurthestApart(nodes, anchors);
126 }
127 /// One node selected -> align that node to the relevant neighbors
128 else if (selectedNodes.size() == 1) {
129 Node n = selectedNodes.iterator().next();
130
131 Way w = null;
132 if(selectedWays.size() == 1) {
133 w = selectedWays.iterator().next();
134 if(w.containsNode(n) == false)
135 // warning
136 return;
137 } else {
138 List<Way> refWays = OsmPrimitive.getFilteredList(n.getReferrers(), Way.class);
139 if (refWays.size() == 1) { // node used in only one way
140 w = refWays.iterator().next();
141 }
142 }
143 if (w == null || w.getNodesCount() < 3)
144 // warning, need at least 3 nodes
145 return;
146
147 // Find anchors
148 int nodeI = w.getNodes().indexOf(n);
149 // End-node in non-circular way selected: align this node with the two neighbors.
150 if ((nodeI == 0 || nodeI == w.getNodesCount()-1) && !w.isClosed()) {
151 int direction = nodeI == 0 ? 1 : -1;
152 anchors[0] = w.getNode(nodeI + direction);
153 anchors[1] = w.getNode(nodeI + direction*2);
154 } else {
155 // o---O---o
156 anchors[0] = getNodeRelative(w, nodeI, 1);
157 anchors[1] = getNodeRelative(w, nodeI, -1);
158 }
159 nodes.add(n);
160 }
161
162 if (anchors[0] == null || anchors[1] == null) {
163 showWarning();
164 return;
165 }
166
167
168 Collection<Command> cmds = new ArrayList<Command>(nodes.size());
169
170 createAlignNodesCommands(anchors, nodes, cmds);
171
172 // Do it!
173 Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Line"), cmds));
174 Main.map.repaint();
175 }
176
177 private void createAlignNodesCommands(Node[] anchors, Collection<Node> nodes, Collection<Command> cmds) {
178 Node nodea = anchors[0];
179 Node nodeb = anchors[1];
180
181 // The anchors are aligned per definition
182 nodes.remove(nodea);
183 nodes.remove(nodeb);
184
185 // Find out co-ords of A and B
186 double ax = nodea.getEastNorth().east();
187 double ay = nodea.getEastNorth().north();
188 double bx = nodeb.getEastNorth().east();
189 double by = nodeb.getEastNorth().north();
190
191 // OK, for each node to move, work out where to move it!
192 for (Node n : nodes) {
193 // Get existing co-ords of node to move
194 double nx = n.getEastNorth().east();
195 double ny = n.getEastNorth().north();
196
197 if (ax == bx) {
198 // Special case if AB is vertical...
199 nx = ax;
200 } else if (ay == by) {
201 // ...or horizontal
202 ny = ay;
203 } else {
204 // Otherwise calculate position by solving y=mx+c
205 double m1 = (by - ay) / (bx - ax);
206 double c1 = ay - (ax * m1);
207 double m2 = (-1) / m1;
208 double c2 = n.getEastNorth().north() - (n.getEastNorth().east() * m2);
209
210 nx = (c2 - c1) / (m1 - m2);
211 ny = (m1 * nx) + c1;
212 }
213 double newX = nx - n.getEastNorth().east();
214 double newY = ny - n.getEastNorth().north();
215 // Add the command to move the node to its new position.
216 cmds.add(new MoveCommand(n, newX, newY));
217 }
218 }
219
220 @Override
221 protected void updateEnabledState() {
222 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
223 }
224
225 @Override
226 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
227 setEnabled(selection != null && !selection.isEmpty());
228 }
229}
Note: See TracBrowser for help on using the repository browser.