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

Last change on this file since 2322 was 2256, checked in by Gubaer, 15 years ago

Removed inefficient DataSet:getSelected() when responding to fireSelectionChanged() in JOSM actions, see thread on dev
Still uses DataSet:getSelected() when responding to layer change events, this is less critical.

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