[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1076] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2323] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[1076] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
| 9 | import java.util.ArrayList;
|
---|
[2268] | 10 | import java.util.Arrays;
|
---|
[1076] | 11 | import java.util.Collection;
|
---|
[2596] | 12 | import java.util.Collections;
|
---|
[2323] | 13 | import java.util.HashMap;
|
---|
[2268] | 14 | import java.util.HashSet;
|
---|
[4385] | 15 | import java.util.Iterator;
|
---|
[1076] | 16 | import java.util.LinkedList;
|
---|
[4385] | 17 | import java.util.List;
|
---|
[6317] | 18 | import java.util.Map;
|
---|
[4385] | 19 | import java.util.Set;
|
---|
[1076] | 20 |
|
---|
| 21 | import javax.swing.JOptionPane;
|
---|
| 22 |
|
---|
| 23 | import org.openstreetmap.josm.Main;
|
---|
| 24 | import org.openstreetmap.josm.command.Command;
|
---|
| 25 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
| 26 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
| 27 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[10382] | 28 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[1076] | 29 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 30 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 31 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[1838] | 32 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
[6130] | 33 | import org.openstreetmap.josm.gui.Notification;
|
---|
[1084] | 34 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[1076] | 35 |
|
---|
| 36 | /**
|
---|
[2268] | 37 | * Tools / Orthogonalize
|
---|
[1169] | 38 | *
|
---|
[2268] | 39 | * Align edges of a way so all angles are angles of 90 or 180 degrees.
|
---|
| 40 | * See USAGE String below.
|
---|
[1076] | 41 | */
|
---|
| 42 | public final class OrthogonalizeAction extends JosmAction {
|
---|
[6798] | 43 | private static final String USAGE = tr(
|
---|
[2957] | 44 | "<h3>When one or more ways are selected, the shape is adjusted such, that all angles are 90 or 180 degrees.</h3>"+
|
---|
| 45 | "You can add two nodes to the selection. Then, the direction is fixed by these two reference nodes. "+
|
---|
| 46 | "(Afterwards, you can undo the movement for certain nodes:<br>"+
|
---|
[6316] | 47 | "Select them and press the shortcut for Orthogonalize / Undo. The default is Shift-Q.)");
|
---|
[1076] | 48 |
|
---|
[5736] | 49 | /**
|
---|
[6986] | 50 | * Constructs a new {@code OrthogonalizeAction}.
|
---|
[5736] | 51 | */
|
---|
[1169] | 52 | public OrthogonalizeAction() {
|
---|
[1218] | 53 | super(tr("Orthogonalize Shape"),
|
---|
[1814] | 54 | "ortho",
|
---|
[2957] | 55 | tr("Move nodes so all angles are 90 or 180 degrees"),
|
---|
[1814] | 56 | Shortcut.registerShortcut("tools:orthogonalize", tr("Tool: {0}", tr("Orthogonalize Shape")),
|
---|
| 57 | KeyEvent.VK_Q,
|
---|
[4982] | 58 | Shortcut.DIRECT), true);
|
---|
[3757] | 59 | putValue("help", ht("/Action/OrthogonalizeShape"));
|
---|
[1169] | 60 | }
|
---|
[1076] | 61 |
|
---|
[2268] | 62 | /**
|
---|
| 63 | * excepted deviation from an angle of 0, 90, 180, 360 degrees
|
---|
| 64 | * maximum value: 45 degrees
|
---|
[3019] | 65 | *
|
---|
| 66 | * Current policy is to except just everything, no matter how strange the result would be.
|
---|
[2268] | 67 | */
|
---|
[3019] | 68 | private static final double TOLERANCE1 = Math.toRadians(45.); // within a way
|
---|
| 69 | private static final double TOLERANCE2 = Math.toRadians(45.); // ways relative to each other
|
---|
[1089] | 70 |
|
---|
[2268] | 71 | /**
|
---|
| 72 | * Remember movements, so the user can later undo it for certain nodes
|
---|
| 73 | */
|
---|
[7005] | 74 | private static final Map<Node, EastNorth> rememberMovements = new HashMap<>();
|
---|
[1089] | 75 |
|
---|
[2268] | 76 | /**
|
---|
| 77 | * Undo the previous orthogonalization for certain nodes.
|
---|
| 78 | *
|
---|
| 79 | * This is useful, if the way shares nodes that you don't like to change, e.g. imports or
|
---|
| 80 | * work of another user.
|
---|
| 81 | *
|
---|
| 82 | * This action can be triggered by shortcut only.
|
---|
| 83 | */
|
---|
[2626] | 84 | public static class Undo extends JosmAction {
|
---|
[5736] | 85 | /**
|
---|
| 86 | * Constructor
|
---|
| 87 | */
|
---|
[2268] | 88 | public Undo() {
|
---|
[4942] | 89 | super(tr("Orthogonalize Shape / Undo"), "ortho",
|
---|
[2626] | 90 | tr("Undo orthogonalization for certain nodes"),
|
---|
| 91 | Shortcut.registerShortcut("tools:orthogonalizeUndo", tr("Tool: {0}", tr("Orthogonalize Shape / Undo")),
|
---|
| 92 | KeyEvent.VK_Q,
|
---|
[4982] | 93 | Shortcut.SHIFT),
|
---|
[4942] | 94 | true, "action/orthogonalize/undo", true);
|
---|
[2268] | 95 | }
|
---|
[8510] | 96 |
|
---|
[6084] | 97 | @Override
|
---|
[2268] | 98 | public void actionPerformed(ActionEvent e) {
|
---|
| 99 | if (!isEnabled())
|
---|
| 100 | return;
|
---|
[7005] | 101 | final Collection<Command> commands = new LinkedList<>();
|
---|
[10382] | 102 | final Collection<OsmPrimitive> sel = getLayerManager().getEditDataSet().getSelected();
|
---|
[2268] | 103 | try {
|
---|
| 104 | for (OsmPrimitive p : sel) {
|
---|
[8510] | 105 | if (!(p instanceof Node)) throw new InvalidUserInputException("selected object is not a node");
|
---|
[2268] | 106 | Node n = (Node) p;
|
---|
| 107 | if (rememberMovements.containsKey(n)) {
|
---|
| 108 | EastNorth tmp = rememberMovements.get(n);
|
---|
[8443] | 109 | commands.add(new MoveCommand(n, -tmp.east(), -tmp.north()));
|
---|
[2268] | 110 | rememberMovements.remove(n);
|
---|
| 111 | }
|
---|
[1169] | 112 | }
|
---|
[6093] | 113 | if (!commands.isEmpty()) {
|
---|
[2268] | 114 | Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize / Undo"), commands));
|
---|
[8510] | 115 | } else {
|
---|
| 116 | throw new InvalidUserInputException("Commands are empty");
|
---|
| 117 | }
|
---|
[8342] | 118 | } catch (InvalidUserInputException ex) {
|
---|
[10463] | 119 | Main.debug(ex);
|
---|
[6130] | 120 | new Notification(
|
---|
| 121 | tr("Orthogonalize Shape / Undo<br>"+
|
---|
| 122 | "Please select nodes that were moved by the previous Orthogonalize Shape action!"))
|
---|
| 123 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
| 124 | .show();
|
---|
[1169] | 125 | }
|
---|
[1076] | 126 | }
|
---|
[2268] | 127 | }
|
---|
[1076] | 128 |
|
---|
[6084] | 129 | @Override
|
---|
[2268] | 130 | public void actionPerformed(ActionEvent e) {
|
---|
| 131 | if (!isEnabled())
|
---|
| 132 | return;
|
---|
[4126] | 133 | if ("EPSG:4326".equals(Main.getProjection().toString())) {
|
---|
[1076] | 134 | String msg = tr("<html>You are using the EPSG:4326 projection which might lead<br>" +
|
---|
[1089] | 135 | "to undesirable results when doing rectangular alignments.<br>" +
|
---|
| 136 | "Change your projection to get rid of this warning.<br>" +
|
---|
[2957] | 137 | "Do you want to continue?</html>");
|
---|
[1838] | 138 | if (!ConditionalOptionPaneUtil.showConfirmationDialog(
|
---|
| 139 | "align_rectangular_4326",
|
---|
| 140 | Main.parent,
|
---|
| 141 | msg,
|
---|
| 142 | tr("Warning"),
|
---|
| 143 | JOptionPane.YES_NO_OPTION,
|
---|
| 144 | JOptionPane.QUESTION_MESSAGE,
|
---|
| 145 | JOptionPane.YES_OPTION))
|
---|
[1076] | 146 | return;
|
---|
| 147 | }
|
---|
[1089] | 148 |
|
---|
[10382] | 149 | final Collection<OsmPrimitive> sel = getLayerManager().getEditDataSet().getSelected();
|
---|
[1076] | 150 |
|
---|
[2268] | 151 | try {
|
---|
[9923] | 152 | final SequenceCommand command = orthogonalize(sel);
|
---|
| 153 | Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize"), command));
|
---|
[2268] | 154 | } catch (InvalidUserInputException ex) {
|
---|
[10463] | 155 | Main.debug(ex);
|
---|
[6130] | 156 | String msg;
|
---|
[7012] | 157 | if ("usage".equals(ex.getMessage())) {
|
---|
[6130] | 158 | msg = "<h2>" + tr("Usage") + "</h2>" + USAGE;
|
---|
| 159 | } else {
|
---|
| 160 | msg = ex.getMessage() + "<br><hr><h2>" + tr("Usage") + "</h2>" + USAGE;
|
---|
[1076] | 161 | }
|
---|
[6130] | 162 | new Notification(msg)
|
---|
| 163 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
[8823] | 164 | .setDuration(Notification.TIME_DEFAULT)
|
---|
[6130] | 165 | .show();
|
---|
[2268] | 166 | }
|
---|
| 167 | }
|
---|
[6069] | 168 |
|
---|
[4385] | 169 | /**
|
---|
[9923] | 170 | * Rectifies the selection
|
---|
| 171 | * @param selection the selection which should be rectified
|
---|
| 172 | * @return a rectifying command
|
---|
| 173 | * @throws InvalidUserInputException if the selection is invalid
|
---|
| 174 | */
|
---|
| 175 | static SequenceCommand orthogonalize(Iterable<OsmPrimitive> selection) throws InvalidUserInputException {
|
---|
| 176 | final List<Node> nodeList = new ArrayList<>();
|
---|
| 177 | final List<WayData> wayDataList = new ArrayList<>();
|
---|
| 178 | // collect nodes and ways from the selection
|
---|
| 179 | for (OsmPrimitive p : selection) {
|
---|
| 180 | if (p instanceof Node) {
|
---|
| 181 | nodeList.add((Node) p);
|
---|
| 182 | } else if (p instanceof Way) {
|
---|
[9925] | 183 | wayDataList.add(new WayData(((Way) p).getNodes()));
|
---|
| 184 | } else {
|
---|
[9923] | 185 | throw new InvalidUserInputException(tr("Selection must consist only of ways and nodes."));
|
---|
[9925] | 186 | }
|
---|
[9923] | 187 | }
|
---|
[9948] | 188 | if (wayDataList.isEmpty() && nodeList.size() > 2) {
|
---|
[9925] | 189 | final WayData data = new WayData(nodeList);
|
---|
| 190 | final Collection<Command> commands = orthogonalize(Collections.singletonList(data), Collections.<Node>emptyList());
|
---|
| 191 | return new SequenceCommand(tr("Orthogonalize"), commands);
|
---|
| 192 | } else if (wayDataList.isEmpty()) {
|
---|
[9923] | 193 | throw new InvalidUserInputException("usage");
|
---|
[10378] | 194 | } else {
|
---|
[9923] | 195 | if (nodeList.size() == 2 || nodeList.isEmpty()) {
|
---|
| 196 | OrthogonalizeAction.rememberMovements.clear();
|
---|
| 197 | final Collection<Command> commands = new LinkedList<>();
|
---|
| 198 |
|
---|
| 199 | if (nodeList.size() == 2) { // fixed direction
|
---|
| 200 | commands.addAll(orthogonalize(wayDataList, nodeList));
|
---|
| 201 | } else if (nodeList.isEmpty()) {
|
---|
| 202 | List<List<WayData>> groups = buildGroups(wayDataList);
|
---|
| 203 | for (List<WayData> g: groups) {
|
---|
| 204 | commands.addAll(orthogonalize(g, nodeList));
|
---|
| 205 | }
|
---|
| 206 | } else {
|
---|
| 207 | throw new IllegalStateException();
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | return new SequenceCommand(tr("Orthogonalize"), commands);
|
---|
| 211 |
|
---|
| 212 | } else {
|
---|
| 213 | throw new InvalidUserInputException("usage");
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
[4385] | 219 | * Collect groups of ways with common nodes in order to orthogonalize each group separately.
|
---|
[9230] | 220 | * @param wayDataList list of ways
|
---|
[8931] | 221 | * @return groups of ways with common nodes
|
---|
[4385] | 222 | */
|
---|
[6316] | 223 | private static List<List<WayData>> buildGroups(List<WayData> wayDataList) {
|
---|
[7005] | 224 | List<List<WayData>> groups = new ArrayList<>();
|
---|
| 225 | Set<WayData> remaining = new HashSet<>(wayDataList);
|
---|
[4385] | 226 | while (!remaining.isEmpty()) {
|
---|
[7005] | 227 | List<WayData> group = new ArrayList<>();
|
---|
[4385] | 228 | groups.add(group);
|
---|
| 229 | Iterator<WayData> it = remaining.iterator();
|
---|
| 230 | WayData next = it.next();
|
---|
| 231 | it.remove();
|
---|
[7005] | 232 | extendGroupRec(group, next, new ArrayList<>(remaining));
|
---|
[4385] | 233 | remaining.removeAll(group);
|
---|
| 234 | }
|
---|
| 235 | return groups;
|
---|
| 236 | }
|
---|
[6069] | 237 |
|
---|
[4385] | 238 | private static void extendGroupRec(List<WayData> group, WayData newGroupMember, List<WayData> remaining) {
|
---|
| 239 | group.add(newGroupMember);
|
---|
| 240 | for (int i = 0; i < remaining.size(); ++i) {
|
---|
| 241 | WayData candidate = remaining.get(i);
|
---|
| 242 | if (candidate == null) continue;
|
---|
[9925] | 243 | if (!Collections.disjoint(candidate.wayNodes, newGroupMember.wayNodes)) {
|
---|
[4385] | 244 | remaining.set(i, null);
|
---|
| 245 | extendGroupRec(group, candidate, remaining);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
[1089] | 249 |
|
---|
[2268] | 250 | /**
|
---|
| 251 | *
|
---|
| 252 | * Outline:
|
---|
| 253 | * 1. Find direction of all segments
|
---|
| 254 | * - direction = 0..3 (right,up,left,down)
|
---|
| 255 | * - right is not really right, you may have to turn your screen
|
---|
| 256 | * 2. Find average heading of all segments
|
---|
| 257 | * - heading = angle of a vector in polar coordinates
|
---|
| 258 | * - sum up horizontal segments (those with direction 0 or 2)
|
---|
| 259 | * - sum up vertical segments
|
---|
| 260 | * - turn the vertical sum by 90 degrees and add it to the horizontal sum
|
---|
| 261 | * - get the average heading from this total sum
|
---|
| 262 | * 3. Rotate all nodes by the average heading so that right is really right
|
---|
| 263 | * and all segments are approximately NS or EW.
|
---|
| 264 | * 4. If nodes are connected by a horizontal segment: Replace their y-Coordinate by
|
---|
| 265 | * the mean value of their y-Coordinates.
|
---|
| 266 | * - The same for vertical segments.
|
---|
[2512] | 267 | * 5. Rotate back.
|
---|
[9230] | 268 | * @param wayDataList list of ways
|
---|
| 269 | * @param headingNodes list of heading nodes
|
---|
[8931] | 270 | * @return list of commands to perform
|
---|
[8926] | 271 | * @throws InvalidUserInputException if selected ways have an angle different from 90 or 180 degrees
|
---|
[2268] | 272 | **/
|
---|
[6316] | 273 | private static Collection<Command> orthogonalize(List<WayData> wayDataList, List<Node> headingNodes) throws InvalidUserInputException {
|
---|
[2268] | 274 | // find average heading
|
---|
| 275 | double headingAll;
|
---|
| 276 | try {
|
---|
| 277 | if (headingNodes.isEmpty()) {
|
---|
| 278 | // find directions of the segments and make them consistent between different ways
|
---|
| 279 | wayDataList.get(0).calcDirections(Direction.RIGHT);
|
---|
| 280 | double refHeading = wayDataList.get(0).heading;
|
---|
[8850] | 281 | EastNorth totSum = new EastNorth(0., 0.);
|
---|
[2268] | 282 | for (WayData w : wayDataList) {
|
---|
| 283 | w.calcDirections(Direction.RIGHT);
|
---|
| 284 | int directionOffset = angleToDirectionChange(w.heading - refHeading, TOLERANCE2);
|
---|
| 285 | w.calcDirections(Direction.RIGHT.changeBy(directionOffset));
|
---|
[8850] | 286 | if (angleToDirectionChange(refHeading - w.heading, TOLERANCE2) != 0)
|
---|
| 287 | throw new RuntimeException();
|
---|
[2268] | 288 | totSum = EN.sum(totSum, w.segSum);
|
---|
[1089] | 289 | }
|
---|
[2268] | 290 | headingAll = EN.polar(new EastNorth(0., 0.), totSum);
|
---|
[8342] | 291 | } else {
|
---|
[2268] | 292 | headingAll = EN.polar(headingNodes.get(0).getEastNorth(), headingNodes.get(1).getEastNorth());
|
---|
| 293 | for (WayData w : wayDataList) {
|
---|
| 294 | w.calcDirections(Direction.RIGHT);
|
---|
| 295 | int directionOffset = angleToDirectionChange(w.heading - headingAll, TOLERANCE2);
|
---|
| 296 | w.calcDirections(Direction.RIGHT.changeBy(directionOffset));
|
---|
[1089] | 297 | }
|
---|
[2268] | 298 | }
|
---|
| 299 | } catch (RejectedAngleException ex) {
|
---|
| 300 | throw new InvalidUserInputException(
|
---|
[2888] | 301 | tr("<html>Please make sure all selected ways head in a similar direction<br>"+
|
---|
[6798] | 302 | "or orthogonalize them one by one.</html>"), ex);
|
---|
[2268] | 303 | }
|
---|
[1089] | 304 |
|
---|
[2268] | 305 | // put the nodes of all ways in a set
|
---|
[8338] | 306 | final Set<Node> allNodes = new HashSet<>();
|
---|
[2268] | 307 | for (WayData w : wayDataList) {
|
---|
[9999] | 308 | allNodes.addAll(w.wayNodes);
|
---|
[2268] | 309 | }
|
---|
[1089] | 310 |
|
---|
[2268] | 311 | // the new x and y value for each node
|
---|
[8338] | 312 | final Map<Node, Double> nX = new HashMap<>();
|
---|
| 313 | final Map<Node, Double> nY = new HashMap<>();
|
---|
[1089] | 314 |
|
---|
[2596] | 315 | // calculate the centroid of all nodes
|
---|
[2268] | 316 | // it is used as rotation center
|
---|
| 317 | EastNorth pivot = new EastNorth(0., 0.);
|
---|
| 318 | for (Node n : allNodes) {
|
---|
| 319 | pivot = EN.sum(pivot, n.getEastNorth());
|
---|
| 320 | }
|
---|
| 321 | pivot = new EastNorth(pivot.east() / allNodes.size(), pivot.north() / allNodes.size());
|
---|
[1076] | 322 |
|
---|
[2268] | 323 | // rotate
|
---|
| 324 | for (Node n: allNodes) {
|
---|
[8443] | 325 | EastNorth tmp = EN.rotateCC(pivot, n.getEastNorth(), -headingAll);
|
---|
[2268] | 326 | nX.put(n, tmp.east());
|
---|
| 327 | nY.put(n, tmp.north());
|
---|
| 328 | }
|
---|
[1089] | 329 |
|
---|
[2268] | 330 | // orthogonalize
|
---|
[10001] | 331 | final Direction[] horizontal = {Direction.RIGHT, Direction.LEFT};
|
---|
| 332 | final Direction[] vertical = {Direction.UP, Direction.DOWN};
|
---|
| 333 | final Direction[][] orientations = {horizontal, vertical};
|
---|
| 334 | for (Direction[] orientation : orientations) {
|
---|
[8338] | 335 | final Set<Node> s = new HashSet<>(allNodes);
|
---|
[10001] | 336 | int size = s.size();
|
---|
| 337 | for (int dummy = 0; dummy < size; ++dummy) {
|
---|
[2626] | 338 | if (s.isEmpty()) {
|
---|
| 339 | break;
|
---|
| 340 | }
|
---|
[10001] | 341 | final Node dummyN = s.iterator().next(); // pick arbitrary element of s
|
---|
[1089] | 342 |
|
---|
[10001] | 343 | final Set<Node> cs = new HashSet<>(); // will contain each node that can be reached from dummyN
|
---|
| 344 | cs.add(dummyN); // walking only on horizontal / vertical segments
|
---|
[2268] | 345 |
|
---|
| 346 | boolean somethingHappened = true;
|
---|
| 347 | while (somethingHappened) {
|
---|
| 348 | somethingHappened = false;
|
---|
| 349 | for (WayData w : wayDataList) {
|
---|
[8510] | 350 | for (int i = 0; i < w.nSeg; ++i) {
|
---|
[9925] | 351 | Node n1 = w.wayNodes.get(i);
|
---|
| 352 | Node n2 = w.wayNodes.get(i+1);
|
---|
[2268] | 353 | if (Arrays.asList(orientation).contains(w.segDirections[i])) {
|
---|
[8443] | 354 | if (cs.contains(n1) && !cs.contains(n2)) {
|
---|
[2268] | 355 | cs.add(n2);
|
---|
| 356 | somethingHappened = true;
|
---|
| 357 | }
|
---|
[8443] | 358 | if (cs.contains(n2) && !cs.contains(n1)) {
|
---|
[2268] | 359 | cs.add(n1);
|
---|
| 360 | somethingHappened = true;
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
[1814] | 365 | }
|
---|
[1089] | 366 |
|
---|
[10001] | 367 | final Map<Node, Double> nC = (orientation == horizontal) ? nY : nX;
|
---|
[1089] | 368 |
|
---|
[2268] | 369 | double average = 0;
|
---|
| 370 | for (Node n : cs) {
|
---|
[8850] | 371 | s.remove(n);
|
---|
[2268] | 372 | average += nC.get(n).doubleValue();
|
---|
[1076] | 373 | }
|
---|
[2268] | 374 | average = average / cs.size();
|
---|
[2014] | 375 |
|
---|
[2268] | 376 | // if one of the nodes is a heading node, forget about the average and use its value
|
---|
| 377 | for (Node fn : headingNodes) {
|
---|
| 378 | if (cs.contains(fn)) {
|
---|
| 379 | average = nC.get(fn);
|
---|
[2014] | 380 | }
|
---|
[2268] | 381 | }
|
---|
[2014] | 382 |
|
---|
[5686] | 383 | // At this point, the two heading nodes (if any) are horizontally aligned, i.e. they
|
---|
| 384 | // have the same y coordinate. So in general we shouldn't find them in a vertical string
|
---|
| 385 | // of segments. This can still happen in some pathological cases (see #7889). To avoid
|
---|
| 386 | // both heading nodes collapsing to one point, we simply skip this segment string and
|
---|
| 387 | // don't touch the node coordinates.
|
---|
[10001] | 388 | if (orientation == vertical && headingNodes.size() == 2 && cs.containsAll(headingNodes)) {
|
---|
[5686] | 389 | continue;
|
---|
[2014] | 390 | }
|
---|
[2268] | 391 |
|
---|
| 392 | for (Node n : cs) {
|
---|
[5686] | 393 | nC.put(n, average);
|
---|
[2268] | 394 | }
|
---|
[1169] | 395 | }
|
---|
[2268] | 396 | if (!s.isEmpty()) throw new RuntimeException();
|
---|
[1076] | 397 | }
|
---|
[1089] | 398 |
|
---|
[2268] | 399 | // rotate back and log the change
|
---|
[7005] | 400 | final Collection<Command> commands = new LinkedList<>();
|
---|
[2268] | 401 | for (Node n: allNodes) {
|
---|
| 402 | EastNorth tmp = new EastNorth(nX.get(n), nY.get(n));
|
---|
[7859] | 403 | tmp = EN.rotateCC(pivot, tmp, headingAll);
|
---|
[10378] | 404 | final double dx = tmp.east() - n.getEastNorth().east();
|
---|
[2268] | 405 | final double dy = tmp.north() - n.getEastNorth().north();
|
---|
| 406 | if (headingNodes.contains(n)) { // The heading nodes should not have changed
|
---|
[10001] | 407 | final double epsilon = 1E-6;
|
---|
| 408 | if (Math.abs(dx) > Math.abs(epsilon * tmp.east()) ||
|
---|
| 409 | Math.abs(dy) > Math.abs(epsilon * tmp.east()))
|
---|
[2268] | 410 | throw new AssertionError();
|
---|
[8342] | 411 | } else {
|
---|
[2268] | 412 | OrthogonalizeAction.rememberMovements.put(n, new EastNorth(dx, dy));
|
---|
| 413 | commands.add(new MoveCommand(n, dx, dy));
|
---|
| 414 | }
|
---|
[1076] | 415 | }
|
---|
[2596] | 416 | return commands;
|
---|
[1089] | 417 | }
|
---|
| 418 |
|
---|
[2268] | 419 | /**
|
---|
[9230] | 420 | * Class contains everything we need to know about a single way.
|
---|
[2268] | 421 | */
|
---|
| 422 | private static class WayData {
|
---|
[10250] | 423 | public final List<Node> wayNodes; // The assigned way
|
---|
| 424 | public final int nSeg; // Number of Segments of the Way
|
---|
| 425 | public final int nNode; // Number of Nodes of the Way
|
---|
| 426 | public final Direction[] segDirections; // Direction of the segments
|
---|
[2626] | 427 | // segment i goes from node i to node (i+1)
|
---|
[2268] | 428 | public EastNorth segSum; // (Vector-)sum of all horizontal segments plus the sum of all vertical
|
---|
[6889] | 429 | // segments turned by 90 degrees
|
---|
[2268] | 430 | public double heading; // heading of segSum == approximate heading of the way
|
---|
[8510] | 431 |
|
---|
[9925] | 432 | WayData(List<Node> wayNodes) {
|
---|
| 433 | this.wayNodes = wayNodes;
|
---|
| 434 | this.nNode = wayNodes.size();
|
---|
| 435 | this.nSeg = nNode - 1;
|
---|
[10250] | 436 | this.segDirections = new Direction[nSeg];
|
---|
[2268] | 437 | }
|
---|
[7859] | 438 |
|
---|
[2268] | 439 | /**
|
---|
| 440 | * Estimate the direction of the segments, given the first segment points in the
|
---|
| 441 | * direction <code>pInitialDirection</code>.
|
---|
| 442 | * Then sum up all horizontal / vertical segments to have a good guess for the
|
---|
| 443 | * heading of the entire way.
|
---|
[7859] | 444 | * @param pInitialDirection initial direction
|
---|
[8459] | 445 | * @throws InvalidUserInputException if selected ways have an angle different from 90 or 180 degrees
|
---|
[2268] | 446 | */
|
---|
| 447 | public void calcDirections(Direction pInitialDirection) throws InvalidUserInputException {
|
---|
[9925] | 448 | final EastNorth[] en = new EastNorth[nNode]; // alias: wayNodes.get(i).getEastNorth() ---> en[i]
|
---|
[8510] | 449 | for (int i = 0; i < nNode; i++) {
|
---|
[9925] | 450 | en[i] = wayNodes.get(i).getEastNorth();
|
---|
[2268] | 451 | }
|
---|
| 452 | Direction direction = pInitialDirection;
|
---|
| 453 | segDirections[0] = direction;
|
---|
[8510] | 454 | for (int i = 0; i < nSeg - 1; i++) {
|
---|
| 455 | double h1 = EN.polar(en[i], en[i+1]);
|
---|
| 456 | double h2 = EN.polar(en[i+1], en[i+2]);
|
---|
[2268] | 457 | try {
|
---|
| 458 | direction = direction.changeBy(angleToDirectionChange(h2 - h1, TOLERANCE1));
|
---|
| 459 | } catch (RejectedAngleException ex) {
|
---|
[6798] | 460 | throw new InvalidUserInputException(tr("Please select ways with angles of approximately 90 or 180 degrees."), ex);
|
---|
[2268] | 461 | }
|
---|
| 462 | segDirections[i+1] = direction;
|
---|
| 463 | }
|
---|
[2014] | 464 |
|
---|
[2268] | 465 | // sum up segments
|
---|
[8510] | 466 | EastNorth h = new EastNorth(0., 0.);
|
---|
| 467 | EastNorth v = new EastNorth(0., 0.);
|
---|
[2268] | 468 | for (int i = 0; i < nSeg; ++i) {
|
---|
| 469 | EastNorth segment = EN.diff(en[i+1], en[i]);
|
---|
[10378] | 470 | if (segDirections[i] == Direction.RIGHT) {
|
---|
[8510] | 471 | h = EN.sum(h, segment);
|
---|
[2626] | 472 | } else if (segDirections[i] == Direction.UP) {
|
---|
[8510] | 473 | v = EN.sum(v, segment);
|
---|
[2626] | 474 | } else if (segDirections[i] == Direction.LEFT) {
|
---|
[8510] | 475 | h = EN.diff(h, segment);
|
---|
[2626] | 476 | } else if (segDirections[i] == Direction.DOWN) {
|
---|
[8510] | 477 | v = EN.diff(v, segment);
|
---|
[2626] | 478 | } else throw new IllegalStateException();
|
---|
[2014] | 479 | }
|
---|
[2268] | 480 | // rotate the vertical vector by 90 degrees (clockwise) and add it to the horizontal vector
|
---|
[8443] | 481 | segSum = EN.sum(h, new EastNorth(v.north(), -v.east()));
|
---|
[8510] | 482 | this.heading = EN.polar(new EastNorth(0., 0.), segSum);
|
---|
[2014] | 483 | }
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[2268] | 486 | private enum Direction {
|
---|
| 487 | RIGHT, UP, LEFT, DOWN;
|
---|
| 488 | public Direction changeBy(int directionChange) {
|
---|
| 489 | int tmp = (this.ordinal() + directionChange) % 4;
|
---|
[2626] | 490 | if (tmp < 0) {
|
---|
| 491 | tmp += 4; // the % operator can return negative value
|
---|
| 492 | }
|
---|
[2268] | 493 | return Direction.values()[tmp];
|
---|
[2014] | 494 | }
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[2268] | 497 | /**
|
---|
| 498 | * Make sure angle (up to 2*Pi) is in interval [ 0, 2*Pi ).
|
---|
[9230] | 499 | * @param a angle
|
---|
[8931] | 500 | * @return correct angle
|
---|
[2268] | 501 | */
|
---|
[10748] | 502 | private static double standardAngle0to2PI(double a) {
|
---|
[2626] | 503 | while (a >= 2 * Math.PI) {
|
---|
| 504 | a -= 2 * Math.PI;
|
---|
| 505 | }
|
---|
| 506 | while (a < 0) {
|
---|
| 507 | a += 2 * Math.PI;
|
---|
| 508 | }
|
---|
[2268] | 509 | return a;
|
---|
[1076] | 510 | }
|
---|
| 511 |
|
---|
[2268] | 512 | /**
|
---|
| 513 | * Make sure angle (up to 2*Pi) is in interval ( -Pi, Pi ].
|
---|
[9230] | 514 | * @param a angle
|
---|
[8931] | 515 | * @return correct angle
|
---|
[2268] | 516 | */
|
---|
[10748] | 517 | private static double standardAngleMPItoPI(double a) {
|
---|
[2626] | 518 | while (a > Math.PI) {
|
---|
| 519 | a -= 2 * Math.PI;
|
---|
| 520 | }
|
---|
[8443] | 521 | while (a <= -Math.PI) {
|
---|
[2626] | 522 | a += 2 * Math.PI;
|
---|
| 523 | }
|
---|
[2268] | 524 | return a;
|
---|
[1089] | 525 | }
|
---|
[2268] | 526 |
|
---|
| 527 | /**
|
---|
| 528 | * Class contains some auxiliary functions
|
---|
| 529 | */
|
---|
[6987] | 530 | private static final class EN {
|
---|
[6986] | 531 | private EN() {
|
---|
| 532 | // Hide implicit public constructor for utility class
|
---|
| 533 | }
|
---|
[8510] | 534 |
|
---|
[7859] | 535 | /**
|
---|
| 536 | * Rotate counter-clock-wise.
|
---|
[9230] | 537 | * @param pivot pivot
|
---|
| 538 | * @param en original east/north
|
---|
| 539 | * @param angle angle, in radians
|
---|
[8931] | 540 | * @return new east/north
|
---|
[7859] | 541 | */
|
---|
| 542 | public static EastNorth rotateCC(EastNorth pivot, EastNorth en, double angle) {
|
---|
[2268] | 543 | double cosPhi = Math.cos(angle);
|
---|
| 544 | double sinPhi = Math.sin(angle);
|
---|
| 545 | double x = en.east() - pivot.east();
|
---|
| 546 | double y = en.north() - pivot.north();
|
---|
[10378] | 547 | double nx = cosPhi * x - sinPhi * y + pivot.east();
|
---|
| 548 | double ny = sinPhi * x + cosPhi * y + pivot.north();
|
---|
[2268] | 549 | return new EastNorth(nx, ny);
|
---|
[1814] | 550 | }
|
---|
[8510] | 551 |
|
---|
[2268] | 552 | public static EastNorth sum(EastNorth en1, EastNorth en2) {
|
---|
| 553 | return new EastNorth(en1.east() + en2.east(), en1.north() + en2.north());
|
---|
[1814] | 554 | }
|
---|
[8510] | 555 |
|
---|
[2268] | 556 | public static EastNorth diff(EastNorth en1, EastNorth en2) {
|
---|
| 557 | return new EastNorth(en1.east() - en2.east(), en1.north() - en2.north());
|
---|
| 558 | }
|
---|
[8510] | 559 |
|
---|
[2268] | 560 | public static double polar(EastNorth en1, EastNorth en2) {
|
---|
[10378] | 561 | return Math.atan2(en2.north() - en1.north(), en2.east() - en1.east());
|
---|
[2268] | 562 | }
|
---|
[1089] | 563 | }
|
---|
| 564 |
|
---|
[2268] | 565 | /**
|
---|
| 566 | * Recognize angle to be approximately 0, 90, 180 or 270 degrees.
|
---|
[8931] | 567 | * returns an integral value, corresponding to a counter clockwise turn.
|
---|
[9230] | 568 | * @param a angle, in radians
|
---|
| 569 | * @param deltaMax maximum tolerance, in radians
|
---|
[8931] | 570 | * @return an integral value, corresponding to a counter clockwise turn
|
---|
[8926] | 571 | * @throws RejectedAngleException in case of invalid angle
|
---|
[2268] | 572 | */
|
---|
| 573 | private static int angleToDirectionChange(double a, double deltaMax) throws RejectedAngleException {
|
---|
[10748] | 574 | a = standardAngleMPItoPI(a);
|
---|
[10378] | 575 | double d0 = Math.abs(a);
|
---|
| 576 | double d90 = Math.abs(a - Math.PI / 2);
|
---|
[10001] | 577 | double dm90 = Math.abs(a + Math.PI / 2);
|
---|
[2268] | 578 | int dirChange;
|
---|
[2626] | 579 | if (d0 < deltaMax) {
|
---|
[10378] | 580 | dirChange = 0;
|
---|
[2626] | 581 | } else if (d90 < deltaMax) {
|
---|
[10378] | 582 | dirChange = 1;
|
---|
[10001] | 583 | } else if (dm90 < deltaMax) {
|
---|
[2626] | 584 | dirChange = -1;
|
---|
| 585 | } else {
|
---|
[10748] | 586 | a = standardAngle0to2PI(a);
|
---|
[2268] | 587 | double d180 = Math.abs(a - Math.PI);
|
---|
[2626] | 588 | if (d180 < deltaMax) {
|
---|
| 589 | dirChange = 2;
|
---|
| 590 | } else
|
---|
[2268] | 591 | throw new RejectedAngleException();
|
---|
| 592 | }
|
---|
| 593 | return dirChange;
|
---|
[1089] | 594 | }
|
---|
[1820] | 595 |
|
---|
[2268] | 596 | /**
|
---|
| 597 | * Exception: unsuited user input
|
---|
| 598 | */
|
---|
[9923] | 599 | protected static class InvalidUserInputException extends Exception {
|
---|
[2268] | 600 | InvalidUserInputException(String message) {
|
---|
| 601 | super(message);
|
---|
[2256] | 602 | }
|
---|
[8510] | 603 |
|
---|
[6798] | 604 | InvalidUserInputException(String message, Throwable cause) {
|
---|
| 605 | super(message, cause);
|
---|
| 606 | }
|
---|
[1820] | 607 | }
|
---|
[8510] | 608 |
|
---|
[2268] | 609 | /**
|
---|
| 610 | * Exception: angle cannot be recognized as 0, 90, 180 or 270 degrees
|
---|
| 611 | */
|
---|
[9923] | 612 | protected static class RejectedAngleException extends Exception {
|
---|
[2268] | 613 | RejectedAngleException() {
|
---|
| 614 | super();
|
---|
| 615 | }
|
---|
| 616 | }
|
---|
[2256] | 617 |
|
---|
| 618 | @Override
|
---|
[2268] | 619 | protected void updateEnabledState() {
|
---|
[10382] | 620 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
[10383] | 621 | setEnabled(ds != null && !ds.selectionEmpty());
|
---|
[2256] | 622 | }
|
---|
[9446] | 623 |
|
---|
| 624 | @Override
|
---|
| 625 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 626 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 627 | }
|
---|
[1076] | 628 | }
|
---|