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

Last change on this file since 6848 was 6814, checked in by Don-vip, 10 years ago

fix #8234 - remove duplicated/unused icons + javadoc

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