| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.awt.event.KeyEvent;
|
|---|
| 8 | import java.util.Collection;
|
|---|
| 9 | import java.util.LinkedList;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.JOptionPane;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.Main;
|
|---|
| 14 | import org.openstreetmap.josm.command.Command;
|
|---|
| 15 | import org.openstreetmap.josm.command.AddCommand;
|
|---|
| 16 | import org.openstreetmap.josm.command.ChangeCommand;
|
|---|
| 17 | import org.openstreetmap.josm.command.MoveCommand;
|
|---|
| 18 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 20 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Create a new circle from three selected nodes--or a way with 3 nodes. (Useful for roundabouts)
|
|---|
| 27 | * Note: If a way is selected, it is changed. If nodes are selected a new way is created.
|
|---|
| 28 | * So if you've got a way with 3 nodes it makes a difference between running this on the way or the nodes!
|
|---|
| 29 | * BTW: Someone might want to implement projection corrections for this...
|
|---|
| 30 | *
|
|---|
| 31 | * @author Henry Loenwind, based on much copy&Paste from other Actions.
|
|---|
| 32 | */
|
|---|
| 33 | public final class CreateCircleAction extends JosmAction {
|
|---|
| 34 |
|
|---|
| 35 | public CreateCircleAction() {
|
|---|
| 36 | super(tr("Create Circle"), "createcircle", tr("Create a circle from three selected nodes."), KeyEvent.VK_DEAD_MACRON, 0, true);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | private double calcang(double xc, double yc, double x, double y) {
|
|---|
| 40 | // calculate the angle from xc|yc to x|y
|
|---|
| 41 | if (xc == x && yc == y) {
|
|---|
| 42 | return 0; // actually invalid, but we won't have this case in this context
|
|---|
| 43 | }
|
|---|
| 44 | double yd = Math.abs(y - yc);
|
|---|
| 45 | if (yd == 0 && xc < x) {
|
|---|
| 46 | return 0;
|
|---|
| 47 | }
|
|---|
| 48 | if (yd == 0 && xc > x) {
|
|---|
| 49 | return Math.PI;
|
|---|
| 50 | }
|
|---|
| 51 | double xd = Math.abs(x - xc);
|
|---|
| 52 | double a = Math.atan2(xd, yd);
|
|---|
| 53 | if (y > yc) {
|
|---|
| 54 | a = Math.PI - a;
|
|---|
| 55 | }
|
|---|
| 56 | if (x < xc) {
|
|---|
| 57 | a = -a;
|
|---|
| 58 | }
|
|---|
| 59 | a = 1.5*Math.PI + a;
|
|---|
| 60 | if (a < 0) {
|
|---|
| 61 | a += 2*Math.PI;
|
|---|
| 62 | }
|
|---|
| 63 | if (a >= 2*Math.PI) {
|
|---|
| 64 | a -= 2*Math.PI;
|
|---|
| 65 | }
|
|---|
| 66 | return a;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | public void actionPerformed(ActionEvent e) {
|
|---|
| 70 | int numberOfNodesInCircle = Integer.parseInt(Main.pref.get("createcircle.nodecount", "8"));
|
|---|
| 71 | if (numberOfNodesInCircle < 1) {
|
|---|
| 72 | numberOfNodesInCircle = 1;
|
|---|
| 73 | } else if (numberOfNodesInCircle > 100) {
|
|---|
| 74 | numberOfNodesInCircle = 100;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
|---|
| 78 | Collection<Node> nodes = new LinkedList<Node>();
|
|---|
| 79 | Way existingWay = null;
|
|---|
| 80 |
|
|---|
| 81 | for (OsmPrimitive osm : sel)
|
|---|
| 82 | if (osm instanceof Node)
|
|---|
| 83 | nodes.add((Node)osm);
|
|---|
| 84 |
|
|---|
| 85 | // special case if no single nodes are selected and exactly one way is:
|
|---|
| 86 | // then use the way's nodes
|
|---|
| 87 | if ((nodes.size() == 0) && (sel.size() == 1))
|
|---|
| 88 | for (OsmPrimitive osm : sel)
|
|---|
| 89 | if (osm instanceof Way) {
|
|---|
| 90 | existingWay = ((Way)osm);
|
|---|
| 91 | for (Node n : ((Way)osm).nodes)
|
|---|
| 92 | {
|
|---|
| 93 | if(!nodes.contains(n))
|
|---|
| 94 | nodes.add(n);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if (nodes.size() != 3) {
|
|---|
| 99 | JOptionPane.showMessageDialog(Main.parent, tr("Please select exactly three nodes or one way with exactly three nodes."));
|
|---|
| 100 | return;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | // let's get some shorter names
|
|---|
| 104 | Node n1 = ((Node)nodes.toArray()[0]);
|
|---|
| 105 | double x1 = n1.eastNorth.east();
|
|---|
| 106 | double y1 = n1.eastNorth.north();
|
|---|
| 107 | Node n2 = ((Node)nodes.toArray()[1]);
|
|---|
| 108 | double x2 = n2.eastNorth.east();
|
|---|
| 109 | double y2 = n2.eastNorth.north();
|
|---|
| 110 | Node n3 = ((Node)nodes.toArray()[2]);
|
|---|
| 111 | double x3 = n3.eastNorth.east();
|
|---|
| 112 | double y3 = n3.eastNorth.north();
|
|---|
| 113 |
|
|---|
| 114 | // calculate the center (xc/yc)
|
|---|
| 115 | double s = 0.5*((x2 - x3)*(x1 - x3) - (y2 - y3)*(y3 - y1));
|
|---|
| 116 | double sUnder = (x1 - x2)*(y3 - y1) - (y2 - y1)*(x1 - x3);
|
|---|
| 117 |
|
|---|
| 118 | if (sUnder == 0) {
|
|---|
| 119 | JOptionPane.showMessageDialog(Main.parent, tr("Those nodes are not in a circle."));
|
|---|
| 120 | return;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | s /= sUnder;
|
|---|
| 124 |
|
|---|
| 125 | double xc = 0.5*(x1 + x2) + s*(y2 - y1);
|
|---|
| 126 | double yc = 0.5*(y1 + y2) + s*(x1 - x2);
|
|---|
| 127 |
|
|---|
| 128 | // calculate the radius (r)
|
|---|
| 129 | double r = Math.sqrt(Math.pow(xc-x1,2) + Math.pow(yc-y1,2));
|
|---|
| 130 |
|
|---|
| 131 | // find where to put the existing nodes
|
|---|
| 132 | double a1 = calcang(xc, yc, x1, y1);
|
|---|
| 133 | double a2 = calcang(xc, yc, x2, y2);
|
|---|
| 134 | double a3 = calcang(xc, yc, x3, y3);
|
|---|
| 135 | if (a1 < a2) { double at = a1; Node nt = n1; a1 = a2; n1 = n2; a2 = at; n2 = nt; }
|
|---|
| 136 | if (a2 < a3) { double at = a2; Node nt = n2; a2 = a3; n2 = n3; a3 = at; n3 = nt; }
|
|---|
| 137 | if (a1 < a2) { double at = a1; Node nt = n1; a1 = a2; n1 = n2; a2 = at; n2 = nt; }
|
|---|
| 138 |
|
|---|
| 139 | // now we can start doing thigs to OSM data
|
|---|
| 140 | Collection<Command> cmds = new LinkedList<Command>();
|
|---|
| 141 |
|
|---|
| 142 | // build a way for the circle
|
|---|
| 143 | Way wayToAdd;
|
|---|
| 144 | if (existingWay == null) {
|
|---|
| 145 | wayToAdd = new Way();
|
|---|
| 146 | } else {
|
|---|
| 147 | // re-use existing way if it was selected
|
|---|
| 148 | wayToAdd = new Way(existingWay);
|
|---|
| 149 | wayToAdd.nodes.clear();
|
|---|
| 150 | }
|
|---|
| 151 | for (int i = 1; i <= numberOfNodesInCircle; i++) {
|
|---|
| 152 | double a = 2*Math.PI*(1.0 - i/(double)numberOfNodesInCircle); // "1-" to get it clock-wise
|
|---|
| 153 | // insert existing nodes if they fit before this new node (999 means "already added this node")
|
|---|
| 154 | if (a1 < 999 && a1 > a) {
|
|---|
| 155 | wayToAdd.nodes.add(n1);
|
|---|
| 156 | a1 = 999;
|
|---|
| 157 | }
|
|---|
| 158 | if (a2 < 999 && a2 > a) {
|
|---|
| 159 | wayToAdd.nodes.add(n2);
|
|---|
| 160 | a2 = 999;
|
|---|
| 161 | }
|
|---|
| 162 | if (a3 < 999 && a3 > a) {
|
|---|
| 163 | wayToAdd.nodes.add(n3);
|
|---|
| 164 | a3 = 999;
|
|---|
| 165 | }
|
|---|
| 166 | // get the position of the new node and insert it
|
|---|
| 167 | double x = xc + r*Math.cos(a);
|
|---|
| 168 | double y = yc + r*Math.sin(a);
|
|---|
| 169 | Node n = new Node(Main.proj.eastNorth2latlon(new EastNorth(x,y)));
|
|---|
| 170 | wayToAdd.nodes.add(n);
|
|---|
| 171 | cmds.add(new AddCommand(n));
|
|---|
| 172 | }
|
|---|
| 173 | wayToAdd.nodes.add(wayToAdd.nodes.get(0)); // close the circle
|
|---|
| 174 | if (existingWay == null) {
|
|---|
| 175 | cmds.add(new AddCommand(wayToAdd));
|
|---|
| 176 | } else {
|
|---|
| 177 | cmds.add(new ChangeCommand(existingWay, wayToAdd));
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | Main.main.undoRedo.add(new SequenceCommand(tr("Create Circle"), cmds));
|
|---|
| 181 | Main.map.repaint();
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|