source: josm/trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java@ 13193

Last change on this file since 13193 was 13123, checked in by Don-vip, 7 years ago

javadoc

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