source: josm/trunk/src/org/openstreetmap/josm/command/RotateCommand.java@ 729

Last change on this file since 729 was 630, checked in by framm, 16 years ago
  • make commands able to fail (patch by DH)
  • add Command.getOrig() (patch by DH)
  • add RemoveRelationMemberCommand (patch by DH)
  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1package org.openstreetmap.josm.command;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7import java.util.HashMap;
8import java.util.LinkedList;
9import java.util.Map;
10
11import javax.swing.JLabel;
12import javax.swing.tree.DefaultMutableTreeNode;
13import javax.swing.tree.MutableTreeNode;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.coor.EastNorth;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
21import 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 */
28public 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
66 for (Node n : this.objects) {
67 MoveCommand.OldState os = new MoveCommand.OldState();
68 os.eastNorth = n.eastNorth;
69 os.latlon = n.coor;
70 os.modified = n.modified;
71 oldState.put(n, os);
72 pivot.eastNorth = new EastNorth(pivot.eastNorth.east()+os.eastNorth.east(), pivot.eastNorth.north()+os.eastNorth.north());
73 pivot.coor = Main.proj.eastNorth2latlon(pivot.eastNorth);
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}
Note: See TracBrowser for help on using the repository browser.