source: josm/trunk/src/org/openstreetmap/josm/actions/AlignInCircleAction.java@ 11279

Last change on this file since 11279 was 10763, checked in by Don-vip, 8 years ago

sonar - squid:S2156 - "final" classes should not have "protected" members

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