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