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