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

Last change on this file since 6815 was 6380, checked in by Don-vip, 10 years ago

update license/copyright information

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