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

Last change on this file since 1838 was 1750, checked in by Gubaer, 15 years ago

new: replaced global conflict list by conflict list per layer, similar to datasets

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