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

Last change on this file since 6085 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

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