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