1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Set;
|
---|
16 |
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.command.Command;
|
---|
21 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
22 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
23 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.gui.Notification;
|
---|
28 | import org.openstreetmap.josm.tools.Geometry;
|
---|
29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Aligns all selected nodes within a circle. (Useful for roundabouts)
|
---|
33 | *
|
---|
34 | * @author Matthew Newton
|
---|
35 | * @author Petr Dlouhý
|
---|
36 | * @author Teemu Koskinen
|
---|
37 | * @author Alain Delplanque
|
---|
38 | *
|
---|
39 | * @since 146
|
---|
40 | */
|
---|
41 | public final class AlignInCircleAction extends JosmAction {
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code AlignInCircleAction}.
|
---|
45 | */
|
---|
46 | public AlignInCircleAction() {
|
---|
47 | super(tr("Align Nodes in Circle"), "aligncircle", tr("Move the selected nodes into a circle."),
|
---|
48 | Shortcut.registerShortcut("tools:aligncircle", tr("Tool: {0}", tr("Align Nodes in Circle")),
|
---|
49 | KeyEvent.VK_O, Shortcut.DIRECT), true);
|
---|
50 | putValue("help", ht("/Action/AlignInCircle"));
|
---|
51 | }
|
---|
52 |
|
---|
53 | private static double distance(EastNorth n, EastNorth m) {
|
---|
54 | double easd, nord;
|
---|
55 | easd = n.east() - m.east();
|
---|
56 | nord = n.north() - m.north();
|
---|
57 | return Math.sqrt(easd * easd + nord * nord);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static class PolarCoor {
|
---|
61 | private double radius;
|
---|
62 | private double angle;
|
---|
63 | private EastNorth origin = new EastNorth(0, 0);
|
---|
64 | private double azimuth = 0;
|
---|
65 |
|
---|
66 | PolarCoor(double radius, double angle) {
|
---|
67 | this(radius, angle, new EastNorth(0, 0), 0);
|
---|
68 | }
|
---|
69 |
|
---|
70 | PolarCoor(double radius, double angle, EastNorth origin, double azimuth) {
|
---|
71 | this.radius = radius;
|
---|
72 | this.angle = angle;
|
---|
73 | this.origin = origin;
|
---|
74 | this.azimuth = azimuth;
|
---|
75 | }
|
---|
76 |
|
---|
77 | PolarCoor(EastNorth en) {
|
---|
78 | this(en, new EastNorth(0, 0), 0);
|
---|
79 | }
|
---|
80 |
|
---|
81 | PolarCoor(EastNorth en, EastNorth origin, double azimuth) {
|
---|
82 | radius = distance(en, origin);
|
---|
83 | angle = Math.atan2(en.north() - origin.north(), en.east() - origin.east());
|
---|
84 | this.origin = origin;
|
---|
85 | this.azimuth = azimuth;
|
---|
86 | }
|
---|
87 |
|
---|
88 | public EastNorth toEastNorth() {
|
---|
89 | return new EastNorth(radius * Math.cos(angle - azimuth) + origin.east(), radius * Math.sin(angle - azimuth)
|
---|
90 | + origin.north());
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Create a MoveCommand to move a node to this PolarCoor.
|
---|
95 | * @param n Node to move
|
---|
96 | * @return new MoveCommand
|
---|
97 | */
|
---|
98 | public MoveCommand createMoveCommand(Node n) {
|
---|
99 | EastNorth en = toEastNorth();
|
---|
100 | return new MoveCommand(n, en.east() - n.getEastNorth().east(), en.north() - n.getEastNorth().north());
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Perform AlignInCircle action.
|
---|
107 | *
|
---|
108 | * A fixed node is a node for which it is forbidden to change the angle relative to center of the circle.
|
---|
109 | * All other nodes are uniformly distributed.
|
---|
110 | *
|
---|
111 | * Case 1: One unclosed way.
|
---|
112 | * --> allow action, and align selected way nodes
|
---|
113 | * If nodes contained by this way are selected, there are fix.
|
---|
114 | * If nodes outside from the way are selected there are ignored.
|
---|
115 | *
|
---|
116 | * Case 2: One or more ways are selected and can be joined into a polygon
|
---|
117 | * --> allow action, and align selected ways nodes
|
---|
118 | * If 1 node outside of way is selected, it became center
|
---|
119 | * If 1 node outside and 1 node inside are selected there define center and radius
|
---|
120 | * If no outside node and 2 inside nodes are selected those 2 nodes define diameter
|
---|
121 | * In all other cases outside nodes are ignored
|
---|
122 | * In all cases, selected nodes are fix, nodes with more than one referrers are fix
|
---|
123 | * (first referrer is the selected way)
|
---|
124 | *
|
---|
125 | * Case 3: Only nodes are selected
|
---|
126 | * --> Align these nodes, all are fix
|
---|
127 | */
|
---|
128 | @Override
|
---|
129 | public void actionPerformed(ActionEvent e) {
|
---|
130 | if (!isEnabled())
|
---|
131 | return;
|
---|
132 |
|
---|
133 | Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
|
---|
134 | List<Node> nodes = new LinkedList<>();
|
---|
135 | // fixNodes: All nodes for which the angle relative to center should not be modified
|
---|
136 | Set<Node> fixNodes = new HashSet<>();
|
---|
137 | List<Way> ways = new LinkedList<>();
|
---|
138 | EastNorth center = null;
|
---|
139 | double radius = 0;
|
---|
140 |
|
---|
141 | for (OsmPrimitive osm : sel) {
|
---|
142 | if (osm instanceof Node) {
|
---|
143 | nodes.add((Node) osm);
|
---|
144 | } else if (osm instanceof Way) {
|
---|
145 | ways.add((Way) osm);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (ways.size() == 1 && ways.get(0).firstNode() != ways.get(0).lastNode()) {
|
---|
150 | // Case 1
|
---|
151 | Way w = ways.get(0);
|
---|
152 | fixNodes.add(w.firstNode());
|
---|
153 | fixNodes.add(w.lastNode());
|
---|
154 | fixNodes.addAll(nodes);
|
---|
155 | fixNodes.addAll(collectNodesWithExternReferers(ways));
|
---|
156 | // Temporary closed way used to reorder nodes
|
---|
157 | Way closedWay = new Way(w);
|
---|
158 | closedWay.addNode(w.firstNode());
|
---|
159 | List<Way> usedWays = new ArrayList<>(1);
|
---|
160 | usedWays.add(closedWay);
|
---|
161 | nodes = collectNodesAnticlockwise(usedWays);
|
---|
162 | } else if (!ways.isEmpty() && checkWaysArePolygon(ways)) {
|
---|
163 | // Case 2
|
---|
164 | List<Node> inside = new ArrayList<>();
|
---|
165 | List<Node> outside = new ArrayList<>();
|
---|
166 |
|
---|
167 | for(Node n: nodes) {
|
---|
168 | boolean isInside = false;
|
---|
169 | for(Way w: ways) {
|
---|
170 | if(w.getNodes().contains(n)) {
|
---|
171 | isInside = true;
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | if(isInside)
|
---|
176 | inside.add(n);
|
---|
177 | else
|
---|
178 | outside.add(n);
|
---|
179 | }
|
---|
180 |
|
---|
181 | if(outside.size() == 1 && inside.isEmpty()) {
|
---|
182 | center = outside.get(0).getEastNorth();
|
---|
183 | } else if(outside.size() == 1 && inside.size() == 1) {
|
---|
184 | center = outside.get(0).getEastNorth();
|
---|
185 | radius = distance(center, inside.get(0).getEastNorth());
|
---|
186 | } else if(inside.size() == 2 && outside.isEmpty()) {
|
---|
187 | // 2 nodes inside, define diameter
|
---|
188 | EastNorth en0 = inside.get(0).getEastNorth();
|
---|
189 | EastNorth en1 = inside.get(1).getEastNorth();
|
---|
190 | center = new EastNorth((en0.east() + en1.east()) / 2, (en0.north() + en1.north()) / 2);
|
---|
191 | radius = distance(en0, en1) / 2;
|
---|
192 | }
|
---|
193 |
|
---|
194 | fixNodes.addAll(inside);
|
---|
195 | fixNodes.addAll(collectNodesWithExternReferers(ways));
|
---|
196 | nodes = collectNodesAnticlockwise(ways);
|
---|
197 | if (nodes.size() < 4) {
|
---|
198 | new Notification(
|
---|
199 | tr("Not enough nodes in selected ways."))
|
---|
200 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
201 | .setDuration(Notification.TIME_SHORT)
|
---|
202 | .show();
|
---|
203 | return;
|
---|
204 | }
|
---|
205 | } else if (ways.isEmpty() && nodes.size() > 3) {
|
---|
206 | // Case 3
|
---|
207 | fixNodes.addAll(nodes);
|
---|
208 | // No need to reorder nodes since all are fix
|
---|
209 | } else {
|
---|
210 | // Invalid action
|
---|
211 | new Notification(
|
---|
212 | tr("Please select at least four nodes."))
|
---|
213 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
214 | .setDuration(Notification.TIME_SHORT)
|
---|
215 | .show();
|
---|
216 | return;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (center == null) {
|
---|
220 | // Compute the center of nodes
|
---|
221 | center = Geometry.getCenter(nodes);
|
---|
222 | if (center == null) {
|
---|
223 | new Notification(tr("Cannot determine center of selected nodes."))
|
---|
224 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
225 | .setDuration(Notification.TIME_SHORT)
|
---|
226 | .show();
|
---|
227 | return;
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | // Now calculate the average distance to each node from the
|
---|
232 | // center. This method is ok as long as distances are short
|
---|
233 | // relative to the distance from the N or S poles.
|
---|
234 | if (radius == 0) {
|
---|
235 | for (Node n : nodes) {
|
---|
236 | radius += distance(center, n.getEastNorth());
|
---|
237 | }
|
---|
238 | radius = radius / nodes.size();
|
---|
239 | }
|
---|
240 |
|
---|
241 | if(!actionAllowed(nodes)) return;
|
---|
242 |
|
---|
243 | Collection<Command> cmds = new LinkedList<>();
|
---|
244 |
|
---|
245 | // Move each node to that distance from the center.
|
---|
246 | // Nodes that are not "fix" will be adjust making regular arcs.
|
---|
247 | int nodeCount = nodes.size();
|
---|
248 | // Search first fixed node
|
---|
249 | int startPosition = 0;
|
---|
250 | for(startPosition = 0; startPosition < nodeCount; startPosition++)
|
---|
251 | if(fixNodes.contains(nodes.get(startPosition % nodeCount))) break;
|
---|
252 | int i = startPosition; // Start position for current arc
|
---|
253 | int j; // End position for current arc
|
---|
254 | while(i < startPosition + nodeCount) {
|
---|
255 | for(j = i + 1; j < startPosition + nodeCount; j++)
|
---|
256 | if(fixNodes.contains(nodes.get(j % nodeCount))) break;
|
---|
257 | Node first = nodes.get(i % nodeCount);
|
---|
258 | PolarCoor pcFirst = new PolarCoor(first.getEastNorth(), center, 0);
|
---|
259 | pcFirst.radius = radius;
|
---|
260 | cmds.add(pcFirst.createMoveCommand(first));
|
---|
261 | if(j > i + 1) {
|
---|
262 | double delta;
|
---|
263 | if(j == i + nodeCount) {
|
---|
264 | delta = 2 * Math.PI / nodeCount;
|
---|
265 | } else {
|
---|
266 | PolarCoor pcLast = new PolarCoor(nodes.get(j % nodeCount).getEastNorth(), center, 0);
|
---|
267 | delta = pcLast.angle - pcFirst.angle;
|
---|
268 | if(delta < 0) // Assume each PolarCoor.angle is in range ]-pi; pi]
|
---|
269 | delta += 2*Math.PI;
|
---|
270 | delta /= j - i;
|
---|
271 | }
|
---|
272 | for(int k = i+1; k < j; k++) {
|
---|
273 | PolarCoor p = new PolarCoor(radius, pcFirst.angle + (k-i)*delta, center, 0);
|
---|
274 | cmds.add(p.createMoveCommand(nodes.get(k % nodeCount)));
|
---|
275 | }
|
---|
276 | }
|
---|
277 | i = j; // Update start point for next iteration
|
---|
278 | }
|
---|
279 |
|
---|
280 | Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Circle"), cmds));
|
---|
281 | Main.map.repaint();
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Collect all nodes with more than one referrer.
|
---|
286 | * @param ways Ways from witch nodes are selected
|
---|
287 | * @return List of nodes with more than one referrer
|
---|
288 | */
|
---|
289 | private List<Node> collectNodesWithExternReferers(List<Way> ways) {
|
---|
290 | List<Node> withReferrers = new ArrayList<>();
|
---|
291 | for(Way w: ways)
|
---|
292 | for(Node n: w.getNodes())
|
---|
293 | if(n.getReferrers().size() > 1)
|
---|
294 | withReferrers.add(n);
|
---|
295 | return withReferrers;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Assuming all ways can be joined into polygon, create an ordered list of node.
|
---|
300 | * @param ways List of ways to be joined
|
---|
301 | * @return Nodes anticlockwise ordered
|
---|
302 | */
|
---|
303 | private List<Node> collectNodesAnticlockwise(List<Way> ways) {
|
---|
304 | List<Node> nodes = new ArrayList<>();
|
---|
305 | Node firstNode = ways.get(0).firstNode();
|
---|
306 | Node lastNode = null;
|
---|
307 | Way lastWay = null;
|
---|
308 | while(firstNode != lastNode) {
|
---|
309 | if(lastNode == null) lastNode = firstNode;
|
---|
310 | for(Way way: ways) {
|
---|
311 | if(way == lastWay) continue;
|
---|
312 | if(way.firstNode() == lastNode) {
|
---|
313 | List<Node> wayNodes = way.getNodes();
|
---|
314 | for(int i = 0; i < wayNodes.size() - 1; i++)
|
---|
315 | nodes.add(wayNodes.get(i));
|
---|
316 | lastNode = way.lastNode();
|
---|
317 | lastWay = way;
|
---|
318 | break;
|
---|
319 | }
|
---|
320 | if(way.lastNode() == lastNode) {
|
---|
321 | List<Node> wayNodes = way.getNodes();
|
---|
322 | for(int i = wayNodes.size() - 1; i > 0; i--)
|
---|
323 | nodes.add(wayNodes.get(i));
|
---|
324 | lastNode = way.firstNode();
|
---|
325 | lastWay = way;
|
---|
326 | break;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 | // Check if nodes are in anticlockwise order
|
---|
331 | int nc = nodes.size();
|
---|
332 | double area = 0;
|
---|
333 | for(int i = 0; i < nc; i++) {
|
---|
334 | EastNorth p1 = nodes.get(i).getEastNorth();
|
---|
335 | EastNorth p2 = nodes.get((i+1) % nc).getEastNorth();
|
---|
336 | area += p1.east()*p2.north() - p2.east()*p1.north();
|
---|
337 | }
|
---|
338 | if(area < 0)
|
---|
339 | Collections.reverse(nodes);
|
---|
340 | return nodes;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Check if one or more nodes are outside of download area
|
---|
345 | * @param nodes Nodes to check
|
---|
346 | * @return true if action can be done
|
---|
347 | */
|
---|
348 | private boolean actionAllowed(Collection<Node> nodes) {
|
---|
349 | boolean outside = false;
|
---|
350 | for(Node n: nodes)
|
---|
351 | if(n.isOutsideDownloadArea()) {
|
---|
352 | outside = true;
|
---|
353 | break;
|
---|
354 | }
|
---|
355 | if(outside)
|
---|
356 | new Notification(
|
---|
357 | tr("One or more nodes involved in this action is outside of the downloaded area."))
|
---|
358 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
359 | .setDuration(Notification.TIME_SHORT)
|
---|
360 | .show();
|
---|
361 | return true;
|
---|
362 | }
|
---|
363 |
|
---|
364 | @Override
|
---|
365 | protected void updateEnabledState() {
|
---|
366 | setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
|
---|
367 | }
|
---|
368 |
|
---|
369 | @Override
|
---|
370 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
371 | setEnabled(selection != null && !selection.isEmpty());
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Determines if ways can be joined into a polygon.
|
---|
376 | * @param ways The ways collection to check
|
---|
377 | * @return true if all ways can be joined into a polygon
|
---|
378 | */
|
---|
379 | protected static boolean checkWaysArePolygon(Collection<Way> ways) {
|
---|
380 | // For each way, nodes strictly between first and last should't be reference by an other way
|
---|
381 | for(Way way: ways) {
|
---|
382 | for(Node node: way.getNodes()) {
|
---|
383 | if(node == way.firstNode() || node == way.lastNode()) continue;
|
---|
384 | for(Way wayOther: ways) {
|
---|
385 | if(way == wayOther) continue;
|
---|
386 | if(node.getReferrers().contains(wayOther)) return false;
|
---|
387 | }
|
---|
388 | }
|
---|
389 | }
|
---|
390 | // Test if ways can be joined
|
---|
391 | Way currentWay = null;
|
---|
392 | Node startNode = null, endNode = null;
|
---|
393 | int used = 0;
|
---|
394 | while(true) {
|
---|
395 | Way nextWay = null;
|
---|
396 | for(Way w: ways) {
|
---|
397 | if(w.firstNode() == w.lastNode()) return ways.size() == 1;
|
---|
398 | if(w == currentWay) continue;
|
---|
399 | if(currentWay == null) {
|
---|
400 | nextWay = w;
|
---|
401 | startNode = w.firstNode();
|
---|
402 | endNode = w.lastNode();
|
---|
403 | break;
|
---|
404 | }
|
---|
405 | if(w.firstNode() == endNode) {
|
---|
406 | nextWay = w;
|
---|
407 | endNode = w.lastNode();
|
---|
408 | break;
|
---|
409 | }
|
---|
410 | if(w.lastNode() == endNode) {
|
---|
411 | nextWay = w;
|
---|
412 | endNode = w.firstNode();
|
---|
413 | break;
|
---|
414 | }
|
---|
415 | }
|
---|
416 | if(nextWay == null) return false;
|
---|
417 | used += 1;
|
---|
418 | currentWay = nextWay;
|
---|
419 | if(endNode == startNode) return used == ways.size();
|
---|
420 | }
|
---|
421 | }
|
---|
422 | }
|
---|