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

Last change on this file since 13731 was 10663, checked in by Don-vip, 8 years ago

fix #13223 - Minor command class fixes (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7import java.util.Objects;
8
9import org.openstreetmap.josm.data.coor.EastNorth;
10import org.openstreetmap.josm.data.osm.Node;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12
13/**
14 * RotateCommand rotates a number of objects around their centre.
15 *
16 * @author Frederik Ramm
17 */
18public class RotateCommand extends TransformNodesCommand {
19
20 /**
21 * Pivot point
22 */
23 private final EastNorth pivot;
24
25 /**
26 * angle of rotation starting click to pivot
27 */
28 private final double startAngle;
29
30 /**
31 * computed rotation angle between starting click and current mouse pos
32 */
33 private double rotationAngle;
34
35 /**
36 * Creates a RotateCommand.
37 * Assign the initial object set, compute pivot point and inital rotation angle.
38 * @param objects objects to fetch nodes from
39 * @param currentEN cuurent eats/north
40 */
41 public RotateCommand(Collection<? extends OsmPrimitive> objects, EastNorth currentEN) {
42 super(objects);
43
44 pivot = getNodesCenter();
45 startAngle = getAngle(currentEN);
46 rotationAngle = 0.0;
47
48 handleEvent(currentEN);
49 }
50
51 /**
52 * Get angle between the horizontal axis and the line formed by the pivot and given point.
53 * @param currentEN cuurent eats/north
54 * @return angle between the horizontal axis and the line formed by the pivot and given point
55 **/
56 protected final double getAngle(EastNorth currentEN) {
57 if (pivot == null)
58 return 0.0; // should never happen by contract
59 return Math.atan2(currentEN.east()-pivot.east(), currentEN.north()-pivot.north());
60 }
61
62 /**
63 * Compute new rotation angle and transform nodes accordingly.
64 */
65 @Override
66 public final void handleEvent(EastNorth currentEN) {
67 double currentAngle = getAngle(currentEN);
68 rotationAngle = currentAngle - startAngle;
69 transformNodes();
70 }
71
72 /**
73 * Set the rotation angle.
74 * @param rotationAngle The rotate angle
75 */
76 protected void setRotationAngle(double rotationAngle) {
77 this.rotationAngle = rotationAngle;
78 }
79
80 /**
81 * Rotate nodes.
82 */
83 @Override
84 protected void transformNodes() {
85 double cosPhi = Math.cos(rotationAngle);
86 double sinPhi = Math.sin(rotationAngle);
87 for (Node n : nodes) {
88 EastNorth oldEastNorth = oldStates.get(n).getEastNorth();
89 double x = oldEastNorth.east() - pivot.east();
90 double y = oldEastNorth.north() - pivot.north();
91 // CHECKSTYLE.OFF: SingleSpaceSeparator
92 double nx = cosPhi * x + sinPhi * y + pivot.east();
93 double ny = -sinPhi * x + cosPhi * y + pivot.north();
94 // CHECKSTYLE.ON: SingleSpaceSeparator
95 n.setEastNorth(new EastNorth(nx, ny));
96 }
97 }
98
99 @Override
100 public String getDescriptionText() {
101 return trn("Rotate {0} node", "Rotate {0} nodes", nodes.size(), nodes.size());
102 }
103
104 @Override
105 public int hashCode() {
106 return Objects.hash(super.hashCode(), pivot, startAngle, rotationAngle);
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) return true;
112 if (obj == null || getClass() != obj.getClass()) return false;
113 if (!super.equals(obj)) return false;
114 RotateCommand that = (RotateCommand) obj;
115 return Double.compare(that.startAngle, startAngle) == 0 &&
116 Double.compare(that.rotationAngle, rotationAngle) == 0 &&
117 Objects.equals(pivot, that.pivot);
118 }
119}
Note: See TracBrowser for help on using the repository browser.