[1090] | 1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[626] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[2323] | 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[626] | 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
[1768] | 9 | import java.math.BigDecimal;
|
---|
| 10 | import java.math.MathContext;
|
---|
[626] | 11 | import java.util.Collection;
|
---|
| 12 | import java.util.LinkedList;
|
---|
[1846] | 13 | import java.util.List;
|
---|
[626] | 14 |
|
---|
| 15 | import javax.swing.JOptionPane;
|
---|
| 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.command.Command;
|
---|
| 19 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
| 20 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
| 21 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[1084] | 25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[626] | 26 |
|
---|
| 27 | /**
|
---|
[655] | 28 | * Aligns all selected nodes within a circle. (Useful for roundabouts)
|
---|
[1169] | 29 | *
|
---|
[626] | 30 | * @author Matthew Newton
|
---|
[1090] | 31 | * @author Petr Dlouhý
|
---|
[1947] | 32 | * @author Teemu Koskinen
|
---|
[626] | 33 | */
|
---|
| 34 | public final class AlignInCircleAction extends JosmAction {
|
---|
| 35 |
|
---|
[1090] | 36 | public AlignInCircleAction() {
|
---|
[1169] | 37 | super(tr("Align Nodes in Circle"), "aligncircle", tr("Move the selected nodes into a circle."),
|
---|
| 38 | Shortcut.registerShortcut("tools:aligncircle", tr("Tool: {0}", tr("Align Nodes in Circle")),
|
---|
[1090] | 39 | KeyEvent.VK_O, Shortcut.GROUP_EDIT), true);
|
---|
[2323] | 40 | putValue("help", ht("/Action/AlignInCircle"));
|
---|
[1090] | 41 | }
|
---|
[626] | 42 |
|
---|
[1090] | 43 | public double distance(EastNorth n, EastNorth m) {
|
---|
| 44 | double easd, nord;
|
---|
| 45 | easd = n.east() - m.east();
|
---|
| 46 | nord = n.north() - m.north();
|
---|
| 47 | return Math.sqrt(easd * easd + nord * nord);
|
---|
| 48 | }
|
---|
[626] | 49 |
|
---|
[1090] | 50 | public class PolarCoor {
|
---|
| 51 | double radius;
|
---|
| 52 | double angle;
|
---|
| 53 | EastNorth origin = new EastNorth(0, 0);
|
---|
| 54 | double azimuth = 0;
|
---|
| 55 |
|
---|
| 56 | PolarCoor(double radius, double angle) {
|
---|
| 57 | this(radius, angle, new EastNorth(0, 0), 0);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | PolarCoor(double radius, double angle, EastNorth origin, double azimuth) {
|
---|
| 61 | this.radius = radius;
|
---|
| 62 | this.angle = angle;
|
---|
| 63 | this.origin = origin;
|
---|
| 64 | this.azimuth = azimuth;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | PolarCoor(EastNorth en) {
|
---|
| 68 | this(en, new EastNorth(0, 0), 0);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | PolarCoor(EastNorth en, EastNorth origin, double azimuth) {
|
---|
| 72 | radius = distance(en, origin);
|
---|
| 73 | angle = Math.atan2(en.north() - origin.north(), en.east() - origin.east());
|
---|
| 74 | this.origin = origin;
|
---|
| 75 | this.azimuth = azimuth;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public EastNorth toEastNorth() {
|
---|
| 79 | return new EastNorth(radius * Math.cos(angle - azimuth) + origin.east(), radius * Math.sin(angle - azimuth)
|
---|
| 80 | + origin.north());
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public void actionPerformed(ActionEvent e) {
|
---|
[1820] | 85 | if (!isEnabled())
|
---|
| 86 | return;
|
---|
| 87 |
|
---|
[1814] | 88 | Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
|
---|
[1846] | 89 | List<Node> nodes = new LinkedList<Node>();
|
---|
| 90 | List<Way> ways = new LinkedList<Way>();
|
---|
[1636] | 91 | EastNorth center = null;
|
---|
[1090] | 92 | double radius = 0;
|
---|
| 93 | boolean regular = false;
|
---|
| 94 |
|
---|
| 95 | for (OsmPrimitive osm : sel) {
|
---|
[1768] | 96 | if (osm instanceof Node) {
|
---|
[1090] | 97 | nodes.add((Node) osm);
|
---|
[1768] | 98 | } else if (osm instanceof Way) {
|
---|
[1090] | 99 | ways.add((Way) osm);
|
---|
[1768] | 100 | }
|
---|
[1090] | 101 | }
|
---|
| 102 |
|
---|
| 103 | // special case if no single nodes are selected and exactly one way is:
|
---|
| 104 | // then use the way's nodes
|
---|
| 105 | if ((nodes.size() <= 2) && (ways.size() == 1)) {
|
---|
[1846] | 106 | Way way = ways.get(0);
|
---|
[1090] | 107 |
|
---|
| 108 | // some more special combinations:
|
---|
| 109 | // When is selected node that is part of the way, then make a regular polygon, selected
|
---|
| 110 | // node doesn't move.
|
---|
| 111 | // I haven't got better idea, how to activate that function.
|
---|
| 112 | //
|
---|
| 113 | // When one way and one node is selected, set center to position of that node.
|
---|
| 114 | // When one more node, part of the way, is selected, set the radius equal to the
|
---|
| 115 | // distance between two nodes.
|
---|
| 116 | if (nodes.size() > 0) {
|
---|
[1912] | 117 | if (nodes.size() == 1 && way.containsNode(nodes.get(0))) {
|
---|
[1090] | 118 | regular = true;
|
---|
| 119 | } else {
|
---|
| 120 |
|
---|
[1912] | 121 | center = nodes.get(way.containsNode(nodes.get(0)) ? 1 : 0).getEastNorth();
|
---|
[1768] | 122 | if (nodes.size() == 2) {
|
---|
[1846] | 123 | radius = distance(nodes.get(0).getEastNorth(), nodes.get(1).getEastNorth());
|
---|
[1768] | 124 | }
|
---|
[1090] | 125 | }
|
---|
| 126 | nodes = new LinkedList<Node>();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[1862] | 129 | for (Node n : way.getNodes()) {
|
---|
[1768] | 130 | if (!nodes.contains(n)) {
|
---|
[1090] | 131 | nodes.add(n);
|
---|
[1768] | 132 | }
|
---|
[1090] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | if (nodes.size() < 4) {
|
---|
[2017] | 137 | JOptionPane.showMessageDialog(
|
---|
[1847] | 138 | Main.parent,
|
---|
| 139 | tr("Please select at least four nodes."),
|
---|
| 140 | tr("Information"),
|
---|
| 141 | JOptionPane.INFORMATION_MESSAGE
|
---|
| 142 | );
|
---|
[1090] | 143 | return;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[1947] | 146 | // Reorder the nodes if they didn't come from a single way
|
---|
| 147 | if (ways.size() != 1) {
|
---|
| 148 | // First calculate the average point
|
---|
| 149 |
|
---|
| 150 | BigDecimal east = new BigDecimal(0);
|
---|
| 151 | BigDecimal north = new BigDecimal(0);
|
---|
| 152 |
|
---|
| 153 | for (Node n : nodes) {
|
---|
| 154 | BigDecimal x = new BigDecimal(n.getEastNorth().east());
|
---|
| 155 | BigDecimal y = new BigDecimal(n.getEastNorth().north());
|
---|
| 156 | east = east.add(x, MathContext.DECIMAL128);
|
---|
| 157 | north = north.add(y, MathContext.DECIMAL128);
|
---|
| 158 | }
|
---|
| 159 | BigDecimal nodesSize = new BigDecimal(nodes.size());
|
---|
| 160 | east = east.divide(nodesSize, MathContext.DECIMAL128);
|
---|
| 161 | north = north.divide(nodesSize, MathContext.DECIMAL128);
|
---|
| 162 |
|
---|
| 163 | EastNorth average = new EastNorth(east.doubleValue(), north.doubleValue());
|
---|
| 164 | List<Node> newNodes = new LinkedList<Node>();
|
---|
| 165 |
|
---|
| 166 | // Then reorder them based on heading from the average point
|
---|
| 167 | while (!nodes.isEmpty()) {
|
---|
| 168 | double maxHeading = -1.0;
|
---|
| 169 | Node maxNode = null;
|
---|
| 170 | for (Node n : nodes) {
|
---|
| 171 | double heading = average.heading(n.getEastNorth());
|
---|
| 172 | if (heading > maxHeading) {
|
---|
| 173 | maxHeading = heading;
|
---|
| 174 | maxNode = n;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | newNodes.add(maxNode);
|
---|
| 178 | nodes.remove(maxNode);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | nodes = newNodes;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[1090] | 184 | if (center == null) {
|
---|
[1713] | 185 | // Compute the centroid of nodes
|
---|
| 186 |
|
---|
[1947] | 187 | BigDecimal area = new BigDecimal(0);
|
---|
| 188 | BigDecimal north = new BigDecimal(0);
|
---|
| 189 | BigDecimal east = new BigDecimal(0);
|
---|
[1768] | 190 |
|
---|
[1713] | 191 | // See http://en.wikipedia.org/w/index.php?title=Centroid&oldid=294224857#Centroid_of_polygon for the equation used here
|
---|
[1768] | 192 | for (int i = 0; i < nodes.size(); i++) {
|
---|
[1846] | 193 | EastNorth n0 = nodes.get(i).getEastNorth();
|
---|
| 194 | EastNorth n1 = nodes.get((i+1) % nodes.size()).getEastNorth();
|
---|
[1713] | 195 |
|
---|
[1768] | 196 | BigDecimal x0 = new BigDecimal(n0.east());
|
---|
| 197 | BigDecimal y0 = new BigDecimal(n0.north());
|
---|
| 198 | BigDecimal x1 = new BigDecimal(n1.east());
|
---|
| 199 | BigDecimal y1 = new BigDecimal(n1.north());
|
---|
| 200 |
|
---|
| 201 | BigDecimal k = x0.multiply(y1, MathContext.DECIMAL128).subtract(y0.multiply(x1, MathContext.DECIMAL128));
|
---|
| 202 |
|
---|
| 203 | area = area.add(k, MathContext.DECIMAL128);
|
---|
| 204 | east = east.add(k.multiply(x0.add(x1, MathContext.DECIMAL128), MathContext.DECIMAL128));
|
---|
| 205 | north = north.add(k.multiply(y0.add(y1, MathContext.DECIMAL128), MathContext.DECIMAL128));
|
---|
| 206 |
|
---|
[1090] | 207 | }
|
---|
[1768] | 208 |
|
---|
[1947] | 209 | BigDecimal d = new BigDecimal(3, MathContext.DECIMAL128); // 1/2 * 6 = 3
|
---|
| 210 | area = area.multiply(d, MathContext.DECIMAL128);
|
---|
| 211 | north = north.divide(area, MathContext.DECIMAL128);
|
---|
| 212 | east = east.divide(area, MathContext.DECIMAL128);
|
---|
[1768] | 213 |
|
---|
| 214 | center = new EastNorth(east.doubleValue(), north.doubleValue());
|
---|
| 215 |
|
---|
[1090] | 216 | }
|
---|
| 217 | // Node "center" now is central to all selected nodes.
|
---|
| 218 |
|
---|
| 219 | // Now calculate the average distance to each node from the
|
---|
| 220 | // centre. This method is ok as long as distances are short
|
---|
| 221 | // relative to the distance from the N or S poles.
|
---|
| 222 | if (radius == 0) {
|
---|
| 223 | for (Node n : nodes) {
|
---|
[1640] | 224 | radius += distance(center, n.getEastNorth());
|
---|
[1090] | 225 | }
|
---|
| 226 | radius = radius / nodes.size();
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
| 230 |
|
---|
| 231 | PolarCoor pc;
|
---|
| 232 |
|
---|
| 233 | if (regular) { // Make a regular polygon
|
---|
| 234 | double angle = Math.PI * 2 / nodes.size();
|
---|
[1846] | 235 | pc = new PolarCoor(nodes.get(0).getEastNorth(), center, 0);
|
---|
[1090] | 236 |
|
---|
[1846] | 237 | if (pc.angle > (new PolarCoor(nodes.get(1).getEastNorth(), center, 0).angle)) {
|
---|
[1090] | 238 | angle *= -1;
|
---|
[1768] | 239 | }
|
---|
[1090] | 240 |
|
---|
| 241 | pc.radius = radius;
|
---|
| 242 | for (Node n : nodes) {
|
---|
| 243 | EastNorth no = pc.toEastNorth();
|
---|
[1640] | 244 | cmds.add(new MoveCommand(n, no.east() - n.getEastNorth().east(), no.north() - n.getEastNorth().north()));
|
---|
[1090] | 245 | pc.angle += angle;
|
---|
| 246 | }
|
---|
| 247 | } else { // Move each node to that distance from the centre.
|
---|
| 248 | for (Node n : nodes) {
|
---|
[1640] | 249 | pc = new PolarCoor(n.getEastNorth(), center, 0);
|
---|
[1090] | 250 | pc.radius = radius;
|
---|
| 251 | EastNorth no = pc.toEastNorth();
|
---|
[1640] | 252 | cmds.add(new MoveCommand(n, no.east() - n.getEastNorth().east(), no.north() - n.getEastNorth().north()));
|
---|
[1090] | 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Circle"), cmds));
|
---|
| 257 | Main.map.repaint();
|
---|
| 258 | }
|
---|
[1820] | 259 |
|
---|
| 260 | @Override
|
---|
| 261 | protected void updateEnabledState() {
|
---|
| 262 | setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
|
---|
| 263 | }
|
---|
[2256] | 264 |
|
---|
| 265 | @Override
|
---|
| 266 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 267 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 268 | }
|
---|
[626] | 269 | }
|
---|