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