001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.event.ActionEvent; 008import java.awt.event.KeyEvent; 009import java.util.ArrayList; 010import java.util.Collection; 011import java.util.LinkedList; 012import java.util.List; 013 014import javax.swing.JOptionPane; 015 016import org.openstreetmap.josm.Main; 017import org.openstreetmap.josm.command.AddCommand; 018import org.openstreetmap.josm.command.ChangeCommand; 019import org.openstreetmap.josm.command.Command; 020import org.openstreetmap.josm.command.DeleteCommand; 021import org.openstreetmap.josm.command.SequenceCommand; 022import org.openstreetmap.josm.data.coor.EastNorth; 023import org.openstreetmap.josm.data.osm.Node; 024import org.openstreetmap.josm.data.osm.OsmPrimitive; 025import org.openstreetmap.josm.data.osm.Way; 026import org.openstreetmap.josm.gui.Notification; 027import org.openstreetmap.josm.tools.Shortcut; 028 029/** 030 * - Create a new circle from two selected nodes or a way with 2 nodes which represent the diameter of the circle. 031 * - Create a new circle from three selected nodes--or a way with 3 nodes. 032 * - Useful for roundabouts 033 * 034 * Note: If a way is selected, it is changed. If nodes are selected a new way is created. 035 * So if you've got a way with nodes it makes a difference between running this on the way or the nodes! 036 * 037 * BTW: Someone might want to implement projection corrections for this... 038 * 039 * @author Henry Loenwind, based on much copy&Paste from other Actions. 040 * @author Sebastian Masch 041 */ 042public final class CreateCircleAction extends JosmAction { 043 044 /** 045 * Constructs a new {@code CreateCircleAction}. 046 */ 047 public CreateCircleAction() { 048 super(tr("Create Circle"), "createcircle", tr("Create a circle from three selected nodes."), 049 Shortcut.registerShortcut("tools:createcircle", tr("Tool: {0}", tr("Create Circle")), 050 KeyEvent.VK_O, Shortcut.SHIFT), true); 051 putValue("help", ht("/Action/CreateCircle")); 052 } 053 054 private double calcang(double xc, double yc, double x, double y) { 055 // calculate the angle from xc|yc to x|y 056 if (xc == x && yc == y) 057 return 0; // actually invalid, but we won't have this case in this context 058 double yd = Math.abs(y - yc); 059 if (yd == 0 && xc < x) 060 return 0; 061 if (yd == 0 && xc > x) 062 return Math.PI; 063 double xd = Math.abs(x - xc); 064 double a = Math.atan2(xd, yd); 065 if (y > yc) { 066 a = Math.PI - a; 067 } 068 if (x < xc) { 069 a = -a; 070 } 071 a = 1.5*Math.PI + a; 072 if (a < 0) { 073 a += 2*Math.PI; 074 } 075 if (a >= 2*Math.PI) { 076 a -= 2*Math.PI; 077 } 078 return a; 079 } 080 081 @Override 082 public void actionPerformed(ActionEvent e) { 083 if (!isEnabled()) 084 return; 085 086 int numberOfNodesInCircle = Main.pref.getInteger("createcircle.nodecount", 8); 087 if (numberOfNodesInCircle < 1) { 088 numberOfNodesInCircle = 1; 089 } else if (numberOfNodesInCircle > 100) { 090 numberOfNodesInCircle = 100; 091 } 092 093 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected(); 094 List<Node> nodes = new LinkedList<Node>(); 095 Way existingWay = null; 096 097 for (OsmPrimitive osm : sel) 098 if (osm instanceof Node) { 099 nodes.add((Node)osm); 100 } 101 102 // special case if no single nodes are selected and exactly one way is: 103 // then use the way's nodes 104 if (nodes.isEmpty() && (sel.size() == 1)) { 105 for (OsmPrimitive osm : sel) 106 if (osm instanceof Way) { 107 existingWay = ((Way)osm); 108 for (Node n : ((Way)osm).getNodes()) 109 { 110 if(!nodes.contains(n)) { 111 nodes.add(n); 112 } 113 } 114 } 115 } 116 117 // now we can start doing things to OSM data 118 Collection<Command> cmds = new LinkedList<Command>(); 119 120 if (nodes.size() == 2) { 121 // diameter: two single nodes needed or a way with two nodes 122 123 Node n1 = nodes.get(0); 124 double x1 = n1.getEastNorth().east(); 125 double y1 = n1.getEastNorth().north(); 126 Node n2 = nodes.get(1); 127 double x2 = n2.getEastNorth().east(); 128 double y2 = n2.getEastNorth().north(); 129 130 // calculate the center (xc/yc) 131 double xc = 0.5 * (x1 + x2); 132 double yc = 0.5 * (y1 + y2); 133 134 // calculate the radius (r) 135 double r = Math.sqrt(Math.pow(xc-x1,2) + Math.pow(yc-y1,2)); 136 137 // find where to put the existing nodes 138 double a1 = calcang(xc, yc, x1, y1); 139 double a2 = calcang(xc, yc, x2, y2); 140 if (a1 < a2) { double at = a1; Node nt = n1; a1 = a2; n1 = n2; a2 = at; n2 = nt; } 141 142 // build a way for the circle 143 List<Node> wayToAdd = new ArrayList<Node>(numberOfNodesInCircle + 1); 144 145 for (int i = 1; i <= numberOfNodesInCircle; i++) { 146 double a = a2 + 2*Math.PI*(1.0 - i/(double)numberOfNodesInCircle); // "1-" to get it clock-wise 147 148 // insert existing nodes if they fit before this new node (999 means "already added this node") 149 if ((a1 < 999) && (a1 > a - 1E-9) && (a1 < a + 1E-9)) { 150 wayToAdd.add(n1); 151 a1 = 999; 152 } 153 else if ((a2 < 999) && (a2 > a - 1E-9) && (a2 < a + 1E-9)) { 154 wayToAdd.add(n2); 155 a2 = 999; 156 } 157 else { 158 // get the position of the new node and insert it 159 double x = xc + r*Math.cos(a); 160 double y = yc + r*Math.sin(a); 161 Node n = new Node(Main.getProjection().eastNorth2latlon(new EastNorth(x,y))); 162 wayToAdd.add(n); 163 cmds.add(new AddCommand(n)); 164 } 165 } 166 wayToAdd.add(wayToAdd.get(0)); // close the circle 167 if (existingWay == null) { 168 Way newWay = new Way(); 169 newWay.setNodes(wayToAdd); 170 cmds.add(new AddCommand(newWay)); 171 } else { 172 Way newWay = new Way(existingWay); 173 newWay.setNodes(wayToAdd); 174 cmds.add(new ChangeCommand(existingWay, newWay)); 175 } 176 177 // the first node may be unused/abandoned if createcircle.nodecount is odd 178 if (a1 < 999) { 179 // if it is, delete it 180 List<OsmPrimitive> parents = n1.getReferrers(); 181 if (parents.isEmpty() || ((parents.size() == 1) && (parents.contains(existingWay)))) { 182 cmds.add(new DeleteCommand(n1)); 183 } 184 } 185 186 } else if (nodes.size() == 3) { 187 // triangle: three single nodes needed or a way with three nodes 188 189 // let's get some shorter names 190 Node n1 = nodes.get(0); 191 double x1 = n1.getEastNorth().east(); 192 double y1 = n1.getEastNorth().north(); 193 Node n2 = nodes.get(1); 194 double x2 = n2.getEastNorth().east(); 195 double y2 = n2.getEastNorth().north(); 196 Node n3 = nodes.get(2); 197 double x3 = n3.getEastNorth().east(); 198 double y3 = n3.getEastNorth().north(); 199 200 // calculate the center (xc/yc) 201 double s = 0.5*((x2 - x3)*(x1 - x3) - (y2 - y3)*(y3 - y1)); 202 double sUnder = (x1 - x2)*(y3 - y1) - (y2 - y1)*(x1 - x3); 203 204 if (sUnder == 0) { 205 new Notification( 206 tr("Those nodes are not in a circle. Aborting.")) 207 .setIcon(JOptionPane.WARNING_MESSAGE) 208 .show(); 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 Node n = new Node(Main.getProjection().eastNorth2latlon(new EastNorth(x,y))); 249 wayToAdd.add(n); 250 cmds.add(new AddCommand(n)); 251 } 252 wayToAdd.add(wayToAdd.get(0)); // close the circle 253 if (existingWay == null) { 254 Way newWay = new Way(); 255 newWay.setNodes(wayToAdd); 256 cmds.add(new AddCommand(newWay)); 257 } else { 258 Way newWay = new Way(existingWay); 259 newWay.setNodes(wayToAdd); 260 cmds.add(new ChangeCommand(existingWay, newWay)); 261 } 262 263 } else { 264 new Notification( 265 tr("Please select exactly two or three nodes or one way with exactly two or three nodes.")) 266 .setIcon(JOptionPane.INFORMATION_MESSAGE) 267 .setDuration(Notification.TIME_LONG) 268 .show(); 269 return; 270 } 271 272 Main.main.undoRedo.add(new SequenceCommand(tr("Create Circle"), cmds)); 273 Main.map.repaint(); 274 } 275 276 @Override 277 protected void updateEnabledState() { 278 if (getCurrentDataSet() == null) { 279 setEnabled(false); 280 } else { 281 updateEnabledState(getCurrentDataSet().getSelected()); 282 } 283 } 284 285 @Override 286 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 287 setEnabled(selection != null && !selection.isEmpty()); 288 } 289}