1 | package org.openstreetmap.josm.command;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
13 | import javax.swing.tree.MutableTreeNode;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * RotateCommand rotates a number of objects around their centre.
|
---|
25 | *
|
---|
26 | * @author Frederik Ramm <frederik@remote.org>
|
---|
27 | */
|
---|
28 | public class RotateCommand extends Command {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The objects to rotate.
|
---|
32 | */
|
---|
33 | public Collection<Node> objects = new LinkedList<Node>();
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * pivot point
|
---|
37 | */
|
---|
38 | private Node pivot;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * angle of rotation starting click to pivot
|
---|
42 | */
|
---|
43 | private double startAngle;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * computed rotation angle between starting click and current mouse pos
|
---|
47 | */
|
---|
48 | private double rotationAngle;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * List of all old states of the objects.
|
---|
52 | */
|
---|
53 | private Map<Node, MoveCommand.OldState> oldState = new HashMap<Node, MoveCommand.OldState>();
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Creates a RotateCommand.
|
---|
57 | * Assign the initial object set, compute pivot point and rotation angle.
|
---|
58 | * Computation of pivot point is done by the same rules that are used in
|
---|
59 | * the "align nodes in circle" action.
|
---|
60 | */
|
---|
61 | public RotateCommand(Collection<OsmPrimitive> objects, EastNorth start, EastNorth end) {
|
---|
62 |
|
---|
63 | this.objects = AllNodesVisitor.getAllNodes(objects);
|
---|
64 | pivot = new Node(new LatLon(0,0));
|
---|
65 | pivot.eastNorth = new EastNorth(0,0);
|
---|
66 |
|
---|
67 | for (Node n : this.objects) {
|
---|
68 | MoveCommand.OldState os = new MoveCommand.OldState();
|
---|
69 | os.eastNorth = n.eastNorth;
|
---|
70 | os.latlon = n.coor;
|
---|
71 | os.modified = n.modified;
|
---|
72 | oldState.put(n, os);
|
---|
73 | pivot.eastNorth = new EastNorth(pivot.eastNorth.east()+os.eastNorth.east(), pivot.eastNorth.north()+os.eastNorth.north());
|
---|
74 | }
|
---|
75 | pivot.eastNorth = new EastNorth(pivot.eastNorth.east()/this.objects.size(), pivot.eastNorth.north()/this.objects.size());
|
---|
76 | pivot.coor = Main.proj.eastNorth2latlon(pivot.eastNorth);
|
---|
77 |
|
---|
78 | rotationAngle = Math.PI/2;
|
---|
79 | rotateAgain(start, end);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Rotate the same set of objects again, by the angle between given
|
---|
84 | * start and end nodes. Internally this is added to the existing
|
---|
85 | * rotation so a later undo will undo the whole rotation.
|
---|
86 | */
|
---|
87 | public void rotateAgain(EastNorth start, EastNorth end) {
|
---|
88 | // compute angle
|
---|
89 | startAngle = Math.atan2(start.east()-pivot.eastNorth.east(), start.north()-pivot.eastNorth.north());
|
---|
90 | double endAngle = Math.atan2(end.east()-pivot.eastNorth.east(), end.north()-pivot.eastNorth.north());
|
---|
91 | rotationAngle += startAngle - endAngle;
|
---|
92 | rotateNodes(false);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Helper for actually rotationg the nodes.
|
---|
97 | * @param setModified - true if rotated nodes should be flagged "modified"
|
---|
98 | */
|
---|
99 | private void rotateNodes(boolean setModified) {
|
---|
100 | for (Node n : objects) {
|
---|
101 | double cosPhi = Math.cos(rotationAngle);
|
---|
102 | double sinPhi = Math.sin(rotationAngle);
|
---|
103 | EastNorth oldEastNorth = oldState.get(n).eastNorth;
|
---|
104 | double x = oldEastNorth.east() - pivot.eastNorth.east();
|
---|
105 | double y = oldEastNorth.north() - pivot.eastNorth.north();
|
---|
106 | double nx = sinPhi * x + cosPhi * y + pivot.eastNorth.east();
|
---|
107 | double ny = -cosPhi * x + sinPhi * y + pivot.eastNorth.north();
|
---|
108 | n.eastNorth = new EastNorth(nx, ny);
|
---|
109 | n.coor = Main.proj.eastNorth2latlon(n.eastNorth);
|
---|
110 | if (setModified)
|
---|
111 | n.modified = true;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override public boolean executeCommand() {
|
---|
116 | rotateNodes(true);
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override public void undoCommand() {
|
---|
121 | for (Node n : objects) {
|
---|
122 | MoveCommand.OldState os = oldState.get(n);
|
---|
123 | n.eastNorth = os.eastNorth;
|
---|
124 | n.coor = os.latlon;
|
---|
125 | n.modified = os.modified;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
130 | for (OsmPrimitive osm : objects)
|
---|
131 | modified.add(osm);
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override public MutableTreeNode description() {
|
---|
135 | return new DefaultMutableTreeNode(new JLabel(tr("Rotate")+" "+objects.size()+" "+trn("node","nodes",objects.size()), ImageProvider.get("data", "node"), JLabel.HORIZONTAL));
|
---|
136 | }
|
---|
137 | }
|
---|