1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.LinkedList;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
16 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.data.osm.Way;
|
---|
20 | import org.openstreetmap.josm.tools.ShortCut;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Aligns all selected nodes into a straight line (useful for
|
---|
24 | * roads that should be straight, but have side roads and
|
---|
25 | * therefore need multiple nodes)
|
---|
26 | *
|
---|
27 | * @author Matthew Newton
|
---|
28 | */
|
---|
29 | public final class AlignInLineAction extends JosmAction {
|
---|
30 |
|
---|
31 | public AlignInLineAction() {
|
---|
32 | super(tr("Align Nodes in Line"), "alignline", tr("Move the selected nodes onto a line."),
|
---|
33 | ShortCut.registerShortCut("tools:alignline", tr("Tool: {0}", tr("Align Nodes in Line")), KeyEvent.VK_L, ShortCut.GROUP_EDIT), true);
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * The general algorithm here is to find the two selected nodes
|
---|
38 | * that are furthest apart, and then to align all other selected
|
---|
39 | * nodes onto the straight line between these nodes.
|
---|
40 | */
|
---|
41 | public void actionPerformed(ActionEvent e) {
|
---|
42 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
43 | Collection<Node> nodes = new LinkedList<Node>();
|
---|
44 | Collection<Node> itnodes = new LinkedList<Node>();
|
---|
45 | for (OsmPrimitive osm : sel)
|
---|
46 | if (osm instanceof Node) {
|
---|
47 | nodes.add((Node)osm);
|
---|
48 | itnodes.add((Node)osm);
|
---|
49 | }
|
---|
50 | // special case if no single nodes are selected and exactly one way is:
|
---|
51 | // then use the way's nodes
|
---|
52 | if ((nodes.size() == 0) && (sel.size() == 1))
|
---|
53 | for (OsmPrimitive osm : sel)
|
---|
54 | if (osm instanceof Way) {
|
---|
55 | nodes.addAll(((Way)osm).nodes);
|
---|
56 | itnodes.addAll(((Way)osm).nodes);
|
---|
57 | }
|
---|
58 | if (nodes.size() < 3) {
|
---|
59 | JOptionPane.showMessageDialog(Main.parent, tr("Please select at least three nodes."));
|
---|
60 | return;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // Find from the selected nodes two that are the furthest apart.
|
---|
64 | // Let's call them A and B.
|
---|
65 | double distance = 0;
|
---|
66 |
|
---|
67 | Node nodea = null;
|
---|
68 | Node nodeb = null;
|
---|
69 |
|
---|
70 | for (Node n : nodes) {
|
---|
71 | itnodes.remove(n);
|
---|
72 | for (Node m : itnodes) {
|
---|
73 | double dist = Math.sqrt(n.eastNorth.distance(m.eastNorth));
|
---|
74 | if (dist > distance) {
|
---|
75 | nodea = n;
|
---|
76 | nodeb = m;
|
---|
77 | distance = dist;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | // Remove the nodes A and B from the list of nodes to move
|
---|
83 | nodes.remove(nodea);
|
---|
84 | nodes.remove(nodeb);
|
---|
85 |
|
---|
86 | // Find out co-ords of A and B
|
---|
87 | double ax = nodea.eastNorth.east();
|
---|
88 | double ay = nodea.eastNorth.north();
|
---|
89 | double bx = nodeb.eastNorth.east();
|
---|
90 | double by = nodeb.eastNorth.north();
|
---|
91 |
|
---|
92 | // A list of commands to do
|
---|
93 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
94 |
|
---|
95 | // OK, for each node to move, work out where to move it!
|
---|
96 | for (Node n : nodes) {
|
---|
97 | // Get existing co-ords of node to move
|
---|
98 | double nx = n.eastNorth.east();
|
---|
99 | double ny = n.eastNorth.north();
|
---|
100 |
|
---|
101 | if (ax == bx) {
|
---|
102 | // Special case if AB is vertical...
|
---|
103 | nx = ax;
|
---|
104 | } else if (ay == by) {
|
---|
105 | // ...or horizontal
|
---|
106 | ny = ay;
|
---|
107 | } else {
|
---|
108 | // Otherwise calculate position by solving y=mx+c
|
---|
109 | double m1 = (by - ay) / (bx - ax);
|
---|
110 | double c1 = ay - (ax * m1);
|
---|
111 | double m2 = (-1) / m1;
|
---|
112 | double c2 = n.eastNorth.north() - (n.eastNorth.east() * m2);
|
---|
113 |
|
---|
114 | nx = (c2 - c1) / (m1 - m2);
|
---|
115 | ny = (m1 * nx) + c1;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // Add the command to move the node to its new position.
|
---|
119 | cmds.add(new MoveCommand(n, nx - n.eastNorth.east(), ny - n.eastNorth.north() ));
|
---|
120 | }
|
---|
121 |
|
---|
122 | // Do it!
|
---|
123 | Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Line"), cmds));
|
---|
124 | Main.map.repaint();
|
---|
125 | }
|
---|
126 | }
|
---|