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