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

Last change on this file since 1938 was 1912, checked in by Gubaer, 15 years ago

Updated w.getNodes().contains(..) to w.containsNode(...)

  • Property svn:eol-style set to native
File size: 8.2 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.gui.OptionPaneUtil;
25import org.openstreetmap.josm.tools.Shortcut;
26
27/**
28 * Aligns all selected nodes within a circle. (Useful for roundabouts)
29 *
30 * @author Matthew Newton
31 * @author Petr Dlouhý
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 OptionPaneUtil.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 if (center == null) {
145 // Compute the centroid of nodes
146
147 BigDecimal area = new BigDecimal("0");
148 BigDecimal north = new BigDecimal("0");
149 BigDecimal east = new BigDecimal("0");
150
151 // See http://en.wikipedia.org/w/index.php?title=Centroid&oldid=294224857#Centroid_of_polygon for the equation used here
152 for (int i = 0; i < nodes.size(); i++) {
153 EastNorth n0 = nodes.get(i).getEastNorth();
154 EastNorth n1 = nodes.get((i+1) % nodes.size()).getEastNorth();
155
156 BigDecimal x0 = new BigDecimal(n0.east());
157 BigDecimal y0 = new BigDecimal(n0.north());
158 BigDecimal x1 = new BigDecimal(n1.east());
159 BigDecimal y1 = new BigDecimal(n1.north());
160
161 BigDecimal k = x0.multiply(y1, MathContext.DECIMAL128).subtract(y0.multiply(x1, MathContext.DECIMAL128));
162
163 area = area.add(k, MathContext.DECIMAL128);
164 east = east.add(k.multiply(x0.add(x1, MathContext.DECIMAL128), MathContext.DECIMAL128));
165 north = north.add(k.multiply(y0.add(y1, MathContext.DECIMAL128), MathContext.DECIMAL128));
166
167 }
168
169 BigDecimal d = new BigDecimal("2");
170 area = area.divide(d, MathContext.DECIMAL128);
171 d = new BigDecimal("6");
172 north = north.divide(d.multiply(area, MathContext.DECIMAL128), MathContext.DECIMAL128);
173 east = east.divide(d.multiply(area, MathContext.DECIMAL128), MathContext.DECIMAL128);
174
175 center = new EastNorth(east.doubleValue(), north.doubleValue());
176
177 }
178 // Node "center" now is central to all selected nodes.
179
180 // Now calculate the average distance to each node from the
181 // centre. This method is ok as long as distances are short
182 // relative to the distance from the N or S poles.
183 if (radius == 0) {
184 for (Node n : nodes) {
185 radius += distance(center, n.getEastNorth());
186 }
187 radius = radius / nodes.size();
188 }
189
190 Collection<Command> cmds = new LinkedList<Command>();
191
192 PolarCoor pc;
193
194 if (regular) { // Make a regular polygon
195 double angle = Math.PI * 2 / nodes.size();
196 pc = new PolarCoor(nodes.get(0).getEastNorth(), center, 0);
197
198 if (pc.angle > (new PolarCoor(nodes.get(1).getEastNorth(), center, 0).angle)) {
199 angle *= -1;
200 }
201
202 pc.radius = radius;
203 for (Node n : nodes) {
204 EastNorth no = pc.toEastNorth();
205 cmds.add(new MoveCommand(n, no.east() - n.getEastNorth().east(), no.north() - n.getEastNorth().north()));
206 pc.angle += angle;
207 }
208 } else { // Move each node to that distance from the centre.
209 for (Node n : nodes) {
210 pc = new PolarCoor(n.getEastNorth(), center, 0);
211 pc.radius = radius;
212 EastNorth no = pc.toEastNorth();
213 cmds.add(new MoveCommand(n, no.east() - n.getEastNorth().east(), no.north() - n.getEastNorth().north()));
214 }
215 }
216
217 Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Circle"), cmds));
218 Main.map.repaint();
219 }
220
221 @Override
222 protected void updateEnabledState() {
223 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
224 }
225}
Note: See TracBrowser for help on using the repository browser.