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