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

Last change on this file since 1654 was 1640, checked in by stoecker, 15 years ago

little bit more refactoring of coordinate access - patch by jttt

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