1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.math.BigDecimal;
|
---|
10 | import java.math.MathContext;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Set;
|
---|
16 |
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.command.Command;
|
---|
21 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
22 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
23 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.gui.Notification;
|
---|
28 | import org.openstreetmap.josm.tools.Geometry;
|
---|
29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Aligns all selected nodes within a circle. (Useful for roundabouts)
|
---|
33 | *
|
---|
34 | * @author Matthew Newton
|
---|
35 | * @author Petr Dlouhý
|
---|
36 | * @author Teemu Koskinen
|
---|
37 | */
|
---|
38 | public final class AlignInCircleAction extends JosmAction {
|
---|
39 |
|
---|
40 | public AlignInCircleAction() {
|
---|
41 | super(tr("Align Nodes in Circle"), "aligncircle", tr("Move the selected nodes into a circle."),
|
---|
42 | Shortcut.registerShortcut("tools:aligncircle", tr("Tool: {0}", tr("Align Nodes in Circle")),
|
---|
43 | KeyEvent.VK_O, Shortcut.DIRECT), true);
|
---|
44 | putValue("help", ht("/Action/AlignInCircle"));
|
---|
45 | }
|
---|
46 |
|
---|
47 | public double distance(EastNorth n, EastNorth m) {
|
---|
48 | double easd, nord;
|
---|
49 | easd = n.east() - m.east();
|
---|
50 | nord = n.north() - m.north();
|
---|
51 | return Math.sqrt(easd * easd + nord * nord);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public class PolarCoor {
|
---|
55 | double radius;
|
---|
56 | double angle;
|
---|
57 | EastNorth origin = new EastNorth(0, 0);
|
---|
58 | double azimuth = 0;
|
---|
59 |
|
---|
60 | PolarCoor(double radius, double angle) {
|
---|
61 | this(radius, angle, new EastNorth(0, 0), 0);
|
---|
62 | }
|
---|
63 |
|
---|
64 | PolarCoor(double radius, double angle, EastNorth origin, double azimuth) {
|
---|
65 | this.radius = radius;
|
---|
66 | this.angle = angle;
|
---|
67 | this.origin = origin;
|
---|
68 | this.azimuth = azimuth;
|
---|
69 | }
|
---|
70 |
|
---|
71 | PolarCoor(EastNorth en) {
|
---|
72 | this(en, new EastNorth(0, 0), 0);
|
---|
73 | }
|
---|
74 |
|
---|
75 | PolarCoor(EastNorth en, EastNorth origin, double azimuth) {
|
---|
76 | radius = distance(en, origin);
|
---|
77 | angle = Math.atan2(en.north() - origin.north(), en.east() - origin.east());
|
---|
78 | this.origin = origin;
|
---|
79 | this.azimuth = azimuth;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public EastNorth toEastNorth() {
|
---|
83 | return new EastNorth(radius * Math.cos(angle - azimuth) + origin.east(), radius * Math.sin(angle - azimuth)
|
---|
84 | + origin.north());
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public void actionPerformed(ActionEvent e) {
|
---|
90 | if (!isEnabled())
|
---|
91 | return;
|
---|
92 |
|
---|
93 | Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
|
---|
94 | List<Node> nodes = new LinkedList<Node>();
|
---|
95 | List<Way> ways = new LinkedList<Way>();
|
---|
96 | EastNorth center = null;
|
---|
97 | double radius = 0;
|
---|
98 | boolean regular = false;
|
---|
99 |
|
---|
100 | for (OsmPrimitive osm : sel) {
|
---|
101 | if (osm instanceof Node) {
|
---|
102 | nodes.add((Node) osm);
|
---|
103 | } else if (osm instanceof Way) {
|
---|
104 | ways.add((Way) osm);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | // special case if no single nodes are selected and exactly one way is:
|
---|
109 | // then use the way's nodes
|
---|
110 | if ((nodes.size() <= 2) && (ways.size() == 1)) {
|
---|
111 | Way way = ways.get(0);
|
---|
112 |
|
---|
113 | // some more special combinations:
|
---|
114 | // When is selected node that is part of the way, then make a regular polygon, selected
|
---|
115 | // node doesn't move.
|
---|
116 | // I haven't got better idea, how to activate that function.
|
---|
117 | //
|
---|
118 | // When one way and one node is selected, set center to position of that node.
|
---|
119 | // When one more node, part of the way, is selected, set the radius equal to the
|
---|
120 | // distance between two nodes.
|
---|
121 | if (nodes.size() > 0) {
|
---|
122 | if (nodes.size() == 1 && way.containsNode(nodes.get(0)) && allowRegularPolygon(way.getNodes())) {
|
---|
123 | regular = true;
|
---|
124 | } else if (nodes.size() >= 2) {
|
---|
125 | center = nodes.get(way.containsNode(nodes.get(0)) ? 1 : 0).getEastNorth();
|
---|
126 | if (nodes.size() == 2) {
|
---|
127 | radius = distance(nodes.get(0).getEastNorth(), nodes.get(1).getEastNorth());
|
---|
128 | }
|
---|
129 | }
|
---|
130 | nodes.clear();
|
---|
131 | }
|
---|
132 |
|
---|
133 | for (Node n : way.getNodes()) {
|
---|
134 | if (!nodes.contains(n)) {
|
---|
135 | nodes.add(n);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (nodes.size() < 4) {
|
---|
141 | new Notification(
|
---|
142 | tr("Please select at least four nodes."))
|
---|
143 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
144 | .setDuration(Notification.TIME_SHORT)
|
---|
145 | .show();
|
---|
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 | }
|
---|