source: josm/trunk/src/org/openstreetmap/josm/actions/CreateCircleAction.java@ 6887

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

javadoc fixes for jdk8 compatibility

  • Property svn:eol-style set to native
File size: 11.2 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.util.ArrayList;
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.AddCommand;
18import org.openstreetmap.josm.command.ChangeCommand;
19import org.openstreetmap.josm.command.Command;
20import org.openstreetmap.josm.command.DeleteCommand;
21import org.openstreetmap.josm.command.SequenceCommand;
22import org.openstreetmap.josm.data.coor.EastNorth;
23import org.openstreetmap.josm.data.coor.LatLon;
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.Shortcut;
29
30/**
31 * - Create a new circle from two selected nodes or a way with 2 nodes which represent the diameter of the circle.
32 * - Create a new circle from three selected nodes--or a way with 3 nodes.
33 * - Useful for roundabouts
34 *
35 * Note: If a way is selected, it is changed. If nodes are selected a new way is created.
36 * So if you've got a way with nodes it makes a difference between running this on the way or the nodes!
37 *
38 * BTW: Someone might want to implement projection corrections for this...
39 *
40 * @since 996
41 *
42 * @author Henry Loenwind
43 * @author Sebastian Masch
44 */
45public final class CreateCircleAction extends JosmAction {
46
47 /**
48 * Constructs a new {@code CreateCircleAction}.
49 */
50 public CreateCircleAction() {
51 super(tr("Create Circle"), "aligncircle", tr("Create a circle from three selected nodes."),
52 Shortcut.registerShortcut("tools:createcircle", tr("Tool: {0}", tr("Create Circle")),
53 KeyEvent.VK_O, Shortcut.SHIFT), true, "createcircle", true);
54 putValue("help", ht("/Action/CreateCircle"));
55 }
56
57 private double calcang(double xc, double yc, double x, double y) {
58 // calculate the angle from xc|yc to x|y
59 if (xc == x && yc == y)
60 return 0; // actually invalid, but we won't have this case in this context
61 double yd = Math.abs(y - yc);
62 if (yd == 0 && xc < x)
63 return 0;
64 if (yd == 0 && xc > x)
65 return Math.PI;
66 double xd = Math.abs(x - xc);
67 double a = Math.atan2(xd, yd);
68 if (y > yc) {
69 a = Math.PI - a;
70 }
71 if (x < xc) {
72 a = -a;
73 }
74 a = 1.5*Math.PI + a;
75 if (a < 0) {
76 a += 2*Math.PI;
77 }
78 if (a >= 2*Math.PI) {
79 a -= 2*Math.PI;
80 }
81 return a;
82 }
83
84 @Override
85 public void actionPerformed(ActionEvent e) {
86 if (!isEnabled())
87 return;
88
89 int numberOfNodesInCircle = Main.pref.getInteger("createcircle.nodecount", 8);
90 if (numberOfNodesInCircle < 1) {
91 numberOfNodesInCircle = 1;
92 } else if (numberOfNodesInCircle > 100) {
93 numberOfNodesInCircle = 100;
94 }
95
96 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
97 List<Node> nodes = new LinkedList<Node>();
98 Way existingWay = null;
99
100 for (OsmPrimitive osm : sel)
101 if (osm instanceof Node) {
102 nodes.add((Node)osm);
103 }
104
105 // special case if no single nodes are selected and exactly one way is:
106 // then use the way's nodes
107 if (nodes.isEmpty() && (sel.size() == 1)) {
108 for (OsmPrimitive osm : sel)
109 if (osm instanceof Way) {
110 existingWay = ((Way)osm);
111 for (Node n : ((Way)osm).getNodes())
112 {
113 if(!nodes.contains(n)) {
114 nodes.add(n);
115 }
116 }
117 }
118 }
119
120 // now we can start doing things to OSM data
121 Collection<Command> cmds = new LinkedList<Command>();
122
123 if (nodes.size() == 2) {
124 // diameter: two single nodes needed or a way with two nodes
125
126 Node n1 = nodes.get(0);
127 double x1 = n1.getEastNorth().east();
128 double y1 = n1.getEastNorth().north();
129 Node n2 = nodes.get(1);
130 double x2 = n2.getEastNorth().east();
131 double y2 = n2.getEastNorth().north();
132
133 // calculate the center (xc/yc)
134 double xc = 0.5 * (x1 + x2);
135 double yc = 0.5 * (y1 + y2);
136
137 // calculate the radius (r)
138 double r = Math.sqrt(Math.pow(xc-x1,2) + Math.pow(yc-y1,2));
139
140 // find where to put the existing nodes
141 double a1 = calcang(xc, yc, x1, y1);
142 double a2 = calcang(xc, yc, x2, y2);
143 if (a1 < a2) { double at = a1; Node nt = n1; a1 = a2; n1 = n2; a2 = at; n2 = nt; }
144
145 // build a way for the circle
146 List<Node> wayToAdd = new ArrayList<Node>(numberOfNodesInCircle + 1);
147
148 for (int i = 1; i <= numberOfNodesInCircle; i++) {
149 double a = a2 + 2*Math.PI*(1.0 - i/(double)numberOfNodesInCircle); // "1-" to get it clock-wise
150
151 // insert existing nodes if they fit before this new node (999 means "already added this node")
152 if ((a1 < 999) && (a1 > a - 1E-9) && (a1 < a + 1E-9)) {
153 wayToAdd.add(n1);
154 a1 = 999;
155 }
156 else if ((a2 < 999) && (a2 > a - 1E-9) && (a2 < a + 1E-9)) {
157 wayToAdd.add(n2);
158 a2 = 999;
159 }
160 else {
161 // get the position of the new node and insert it
162 double x = xc + r*Math.cos(a);
163 double y = yc + r*Math.sin(a);
164 Node n = new Node(Main.getProjection().eastNorth2latlon(new EastNorth(x,y)));
165 wayToAdd.add(n);
166 cmds.add(new AddCommand(n));
167 }
168 }
169 wayToAdd.add(wayToAdd.get(0)); // close the circle
170 if (existingWay == null) {
171 Way newWay = new Way();
172 newWay.setNodes(wayToAdd);
173 cmds.add(new AddCommand(newWay));
174 } else {
175 Way newWay = new Way(existingWay);
176 newWay.setNodes(wayToAdd);
177 cmds.add(new ChangeCommand(existingWay, newWay));
178 }
179
180 // the first node may be unused/abandoned if createcircle.nodecount is odd
181 if (a1 < 999) {
182 // if it is, delete it
183 List<OsmPrimitive> parents = n1.getReferrers();
184 if (parents.isEmpty() || ((parents.size() == 1) && (parents.contains(existingWay)))) {
185 cmds.add(new DeleteCommand(n1));
186 }
187 }
188
189 } else if (nodes.size() == 3) {
190 // triangle: three single nodes needed or a way with three nodes
191
192 // let's get some shorter names
193 Node n1 = nodes.get(0);
194 double x1 = n1.getEastNorth().east();
195 double y1 = n1.getEastNorth().north();
196 Node n2 = nodes.get(1);
197 double x2 = n2.getEastNorth().east();
198 double y2 = n2.getEastNorth().north();
199 Node n3 = nodes.get(2);
200 double x3 = n3.getEastNorth().east();
201 double y3 = n3.getEastNorth().north();
202
203 // calculate the center (xc/yc)
204 double s = 0.5*((x2 - x3)*(x1 - x3) - (y2 - y3)*(y3 - y1));
205 double sUnder = (x1 - x2)*(y3 - y1) - (y2 - y1)*(x1 - x3);
206
207 if (sUnder == 0) {
208 notifyNodesNotOnCircle();
209 return;
210 }
211
212 s /= sUnder;
213
214 double xc = 0.5*(x1 + x2) + s*(y2 - y1);
215 double yc = 0.5*(y1 + y2) + s*(x1 - x2);
216
217 // calculate the radius (r)
218 double r = Math.sqrt(Math.pow(xc-x1,2) + Math.pow(yc-y1,2));
219
220 // find where to put the existing nodes
221 double a1 = calcang(xc, yc, x1, y1);
222 double a2 = calcang(xc, yc, x2, y2);
223 double a3 = calcang(xc, yc, x3, y3);
224 if (a1 < a2) { double at = a1; Node nt = n1; a1 = a2; n1 = n2; a2 = at; n2 = nt; }
225 if (a2 < a3) { double at = a2; Node nt = n2; a2 = a3; n2 = n3; a3 = at; n3 = nt; }
226 if (a1 < a2) { double at = a1; Node nt = n1; a1 = a2; n1 = n2; a2 = at; n2 = nt; }
227
228 // build a way for the circle
229 List<Node> wayToAdd = new ArrayList<Node>();
230 for (int i = 1; i <= numberOfNodesInCircle; i++) {
231 double a = 2*Math.PI*(1.0 - i/(double)numberOfNodesInCircle); // "1-" to get it clock-wise
232 // insert existing nodes if they fit before this new node (999 means "already added this node")
233 if (a1 < 999 && a1 > a) {
234 wayToAdd.add(n1);
235 a1 = 999;
236 }
237 if (a2 < 999 && a2 > a) {
238 wayToAdd.add(n2);
239 a2 = 999;
240 }
241 if (a3 < 999 && a3 > a) {
242 wayToAdd.add(n3);
243 a3 = 999;
244 }
245 // get the position of the new node and insert it
246 double x = xc + r*Math.cos(a);
247 double y = yc + r*Math.sin(a);
248 LatLon ll = Main.getProjection().eastNorth2latlon(new EastNorth(x,y));
249 if (ll.isOutSideWorld()) {
250 notifyNodesNotOnCircle();
251 return;
252 }
253 Node n = new Node(ll);
254 wayToAdd.add(n);
255 cmds.add(new AddCommand(n));
256 }
257 wayToAdd.add(wayToAdd.get(0)); // close the circle
258 if (existingWay == null) {
259 Way newWay = new Way();
260 newWay.setNodes(wayToAdd);
261 cmds.add(new AddCommand(newWay));
262 } else {
263 Way newWay = new Way(existingWay);
264 newWay.setNodes(wayToAdd);
265 cmds.add(new ChangeCommand(existingWay, newWay));
266 }
267
268 } else {
269 new Notification(
270 tr("Please select exactly two or three nodes or one way with exactly two or three nodes."))
271 .setIcon(JOptionPane.INFORMATION_MESSAGE)
272 .setDuration(Notification.TIME_LONG)
273 .show();
274 return;
275 }
276
277 Main.main.undoRedo.add(new SequenceCommand(tr("Create Circle"), cmds));
278 Main.map.repaint();
279 }
280
281 private static void notifyNodesNotOnCircle() {
282 new Notification(
283 tr("Those nodes are not in a circle. Aborting."))
284 .setIcon(JOptionPane.WARNING_MESSAGE)
285 .show();
286 }
287
288 @Override
289 protected void updateEnabledState() {
290 if (getCurrentDataSet() == null) {
291 setEnabled(false);
292 } else {
293 updateEnabledState(getCurrentDataSet().getSelected());
294 }
295 }
296
297 @Override
298 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
299 setEnabled(selection != null && !selection.isEmpty());
300 }
301}
Note: See TracBrowser for help on using the repository browser.