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