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