source: josm/trunk/src/org/openstreetmap/josm/actions/AlignInCircleAction.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: 9.3 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.math.BigDecimal;
10import java.math.MathContext;
11import java.util.Collection;
12import java.util.HashSet;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.Set;
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;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.OsmPrimitive;
26import org.openstreetmap.josm.data.osm.Way;
27import org.openstreetmap.josm.tools.Geometry;
28import org.openstreetmap.josm.tools.Shortcut;
29
30/**
31 * Aligns all selected nodes within a circle. (Useful for roundabouts)
32 *
33 * @author Matthew Newton
34 * @author Petr Dlouhý
35 * @author Teemu Koskinen
36 */
37public final class AlignInCircleAction extends JosmAction {
38
39 public AlignInCircleAction() {
40 super(tr("Align Nodes in Circle"), "aligncircle", tr("Move the selected nodes into a circle."),
41 Shortcut.registerShortcut("tools:aligncircle", tr("Tool: {0}", tr("Align Nodes in Circle")),
42 KeyEvent.VK_O, Shortcut.DIRECT), true);
43 putValue("help", ht("/Action/AlignInCircle"));
44 }
45
46 public double distance(EastNorth n, EastNorth m) {
47 double easd, nord;
48 easd = n.east() - m.east();
49 nord = n.north() - m.north();
50 return Math.sqrt(easd * easd + nord * nord);
51 }
52
53 public class PolarCoor {
54 double radius;
55 double angle;
56 EastNorth origin = new EastNorth(0, 0);
57 double azimuth = 0;
58
59 PolarCoor(double radius, double angle) {
60 this(radius, angle, new EastNorth(0, 0), 0);
61 }
62
63 PolarCoor(double radius, double angle, EastNorth origin, double azimuth) {
64 this.radius = radius;
65 this.angle = angle;
66 this.origin = origin;
67 this.azimuth = azimuth;
68 }
69
70 PolarCoor(EastNorth en) {
71 this(en, new EastNorth(0, 0), 0);
72 }
73
74 PolarCoor(EastNorth en, EastNorth origin, double azimuth) {
75 radius = distance(en, origin);
76 angle = Math.atan2(en.north() - origin.north(), en.east() - origin.east());
77 this.origin = origin;
78 this.azimuth = azimuth;
79 }
80
81 public EastNorth toEastNorth() {
82 return new EastNorth(radius * Math.cos(angle - azimuth) + origin.east(), radius * Math.sin(angle - azimuth)
83 + origin.north());
84 }
85 }
86
87 @Override
88 public void actionPerformed(ActionEvent e) {
89 if (!isEnabled())
90 return;
91
92 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
93 List<Node> nodes = new LinkedList<Node>();
94 List<Way> ways = new LinkedList<Way>();
95 EastNorth center = null;
96 double radius = 0;
97 boolean regular = false;
98
99 for (OsmPrimitive osm : sel) {
100 if (osm instanceof Node) {
101 nodes.add((Node) osm);
102 } else if (osm instanceof Way) {
103 ways.add((Way) osm);
104 }
105 }
106
107 // special case if no single nodes are selected and exactly one way is:
108 // then use the way's nodes
109 if ((nodes.size() <= 2) && (ways.size() == 1)) {
110 Way way = ways.get(0);
111
112 // some more special combinations:
113 // When is selected node that is part of the way, then make a regular polygon, selected
114 // node doesn't move.
115 // I haven't got better idea, how to activate that function.
116 //
117 // When one way and one node is selected, set center to position of that node.
118 // When one more node, part of the way, is selected, set the radius equal to the
119 // distance between two nodes.
120 if (nodes.size() > 0) {
121 if (nodes.size() == 1 && way.containsNode(nodes.get(0)) && allowRegularPolygon(way.getNodes())) {
122 regular = true;
123 } else if (nodes.size() >= 2) {
124 center = nodes.get(way.containsNode(nodes.get(0)) ? 1 : 0).getEastNorth();
125 if (nodes.size() == 2) {
126 radius = distance(nodes.get(0).getEastNorth(), nodes.get(1).getEastNorth());
127 }
128 }
129 nodes.clear();
130 }
131
132 for (Node n : way.getNodes()) {
133 if (!nodes.contains(n)) {
134 nodes.add(n);
135 }
136 }
137 }
138
139 if (nodes.size() < 4) {
140 JOptionPane.showMessageDialog(
141 Main.parent,
142 tr("Please select at least four nodes."),
143 tr("Information"),
144 JOptionPane.INFORMATION_MESSAGE
145 );
146 return;
147 }
148
149 // Reorder the nodes if they didn't come from a single way
150 if (ways.size() != 1) {
151 // First calculate the average point
152
153 BigDecimal east = new BigDecimal(0);
154 BigDecimal north = new BigDecimal(0);
155
156 for (Node n : nodes) {
157 BigDecimal x = new BigDecimal(n.getEastNorth().east());
158 BigDecimal y = new BigDecimal(n.getEastNorth().north());
159 east = east.add(x, MathContext.DECIMAL128);
160 north = north.add(y, MathContext.DECIMAL128);
161 }
162 BigDecimal nodesSize = new BigDecimal(nodes.size());
163 east = east.divide(nodesSize, MathContext.DECIMAL128);
164 north = north.divide(nodesSize, MathContext.DECIMAL128);
165
166 EastNorth average = new EastNorth(east.doubleValue(), north.doubleValue());
167 List<Node> newNodes = new LinkedList<Node>();
168
169 // Then reorder them based on heading from the average point
170 while (!nodes.isEmpty()) {
171 double maxHeading = -1.0;
172 Node maxNode = null;
173 for (Node n : nodes) {
174 double heading = average.heading(n.getEastNorth());
175 if (heading > maxHeading) {
176 maxHeading = heading;
177 maxNode = n;
178 }
179 }
180 newNodes.add(maxNode);
181 nodes.remove(maxNode);
182 }
183
184 nodes = newNodes;
185 }
186
187 if (center == null) {
188 // Compute the centroid of nodes
189 center = Geometry.getCentroid(nodes);
190 }
191 // Node "center" now is central to all selected nodes.
192
193 // Now calculate the average distance to each node from the
194 // centre. This method is ok as long as distances are short
195 // relative to the distance from the N or S poles.
196 if (radius == 0) {
197 for (Node n : nodes) {
198 radius += distance(center, n.getEastNorth());
199 }
200 radius = radius / nodes.size();
201 }
202
203 Collection<Command> cmds = new LinkedList<Command>();
204
205 PolarCoor pc;
206
207 if (regular) { // Make a regular polygon
208 double angle = Math.PI * 2 / nodes.size();
209 pc = new PolarCoor(nodes.get(0).getEastNorth(), center, 0);
210
211 if (pc.angle > (new PolarCoor(nodes.get(1).getEastNorth(), center, 0).angle)) {
212 angle *= -1;
213 }
214
215 pc.radius = radius;
216 for (Node n : nodes) {
217 EastNorth no = pc.toEastNorth();
218 cmds.add(new MoveCommand(n, no.east() - n.getEastNorth().east(), no.north() - n.getEastNorth().north()));
219 pc.angle += angle;
220 }
221 } else { // Move each node to that distance from the centre.
222 for (Node n : nodes) {
223 pc = new PolarCoor(n.getEastNorth(), center, 0);
224 pc.radius = radius;
225 EastNorth no = pc.toEastNorth();
226 cmds.add(new MoveCommand(n, no.east() - n.getEastNorth().east(), no.north() - n.getEastNorth().north()));
227 }
228 }
229
230 Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Circle"), cmds));
231 Main.map.repaint();
232 }
233
234 @Override
235 protected void updateEnabledState() {
236 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
237 }
238
239 @Override
240 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
241 setEnabled(selection != null && !selection.isEmpty());
242 }
243
244 /**
245 * Determines if a regular polygon is allowed to be created with the given nodes collection.
246 * @param nodes The nodes collection to check.
247 * @return true if all nodes in the given collection are referred by the same object, and no other one (see #8431)
248 */
249 protected boolean allowRegularPolygon(Collection<Node> nodes) {
250 Set<OsmPrimitive> allReferrers = new HashSet<OsmPrimitive>();
251 for (Node n : nodes) {
252 List<OsmPrimitive> referrers = n.getReferrers();
253 if (referrers.size() > 1 || (allReferrers.addAll(referrers) && allReferrers.size() > 1)) {
254 return false;
255 }
256 }
257 return true;
258 }
259}
Note: See TracBrowser for help on using the repository browser.