001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.command; 003 004import static org.openstreetmap.josm.tools.I18n.trn; 005 006import java.util.Collection; 007import java.util.Objects; 008 009import org.openstreetmap.josm.data.coor.EastNorth; 010import org.openstreetmap.josm.data.osm.Node; 011import org.openstreetmap.josm.data.osm.OsmPrimitive; 012 013/** 014 * RotateCommand rotates a number of objects around their centre. 015 * 016 * @author Frederik Ramm 017 */ 018public class RotateCommand extends TransformNodesCommand { 019 020 /** 021 * Pivot point 022 */ 023 private final EastNorth pivot; 024 025 /** 026 * angle of rotation starting click to pivot 027 */ 028 private double startAngle; 029 030 /** 031 * computed rotation angle between starting click and current mouse pos 032 */ 033 private double rotationAngle; 034 035 private double deltaAngle; 036 037 /** 038 * Creates a RotateCommand. 039 * Assign the initial object set, compute pivot point and initial rotation angle. 040 * @param objects objects to fetch nodes from 041 * @param currentEN current east/north 042 */ 043 public RotateCommand(Collection<? extends OsmPrimitive> objects, EastNorth currentEN) { 044 super(objects); 045 046 pivot = getNodesCenter(); 047 startAngle = getAngle(currentEN); 048 rotationAngle = 0.0; 049 050 handleEvent(currentEN); 051 } 052 053 /** 054 * Get angle between the horizontal axis and the line formed by the pivot and given point. 055 * @param currentEN current east/north 056 * @return angle between the horizontal axis and the line formed by the pivot and given point 057 **/ 058 protected final double getAngle(EastNorth currentEN) { 059 if (pivot == null) 060 return 0.0; // should never happen by contract 061 return Math.atan2(currentEN.east()-pivot.east(), currentEN.north()-pivot.north()); 062 } 063 064 /** 065 * Compute new rotation angle and transform nodes accordingly. 066 */ 067 @Override 068 public final void handleEvent(EastNorth currentEN) { 069 double currentAngle = getAngle(currentEN); 070 rotationAngle = currentAngle - startAngle + deltaAngle; 071 transformNodes(); 072 } 073 074 /** 075 * Handle a repeated rotation action where the mouse was moved to a different position 076 * see #24695 077 * @param startEN start cursor position of a repeated rotation 078 * @since 19566 079 */ 080 public void handleUpdate(EastNorth startEN) { 081 deltaAngle = rotationAngle; 082 startAngle = getAngle(startEN); 083 } 084 085 /** 086 * Set the rotation angle. 087 * @param rotationAngle The rotate angle 088 */ 089 protected void setRotationAngle(double rotationAngle) { 090 this.rotationAngle = rotationAngle; 091 } 092 093 /** 094 * Returns the rotation angle. 095 * @return The rotation angle 096 */ 097 public double getRotationAngle() { 098 return rotationAngle; 099 } 100 101 /** 102 * Rotate nodes. 103 */ 104 @Override 105 protected void transformNodes() { 106 double cosPhi = Math.cos(rotationAngle); 107 double sinPhi = Math.sin(rotationAngle); 108 for (Node n : nodes) { 109 EastNorth oldEastNorth = oldStates.get(n).getEastNorth(); 110 double x = oldEastNorth.east() - pivot.east(); 111 double y = oldEastNorth.north() - pivot.north(); 112 // CHECKSTYLE.OFF: SingleSpaceSeparator 113 double nx = cosPhi * x + sinPhi * y + pivot.east(); 114 double ny = -sinPhi * x + cosPhi * y + pivot.north(); 115 // CHECKSTYLE.ON: SingleSpaceSeparator 116 n.setEastNorth(new EastNorth(nx, ny)); 117 } 118 } 119 120 @Override 121 public String getDescriptionText() { 122 return trn("Rotate {0} node", "Rotate {0} nodes", nodes.size(), nodes.size()); 123 } 124 125 @Override 126 public int hashCode() { 127 return Objects.hash(super.hashCode(), pivot, startAngle, rotationAngle, deltaAngle); 128 } 129 130 @Override 131 public boolean equals(Object obj) { 132 if (this == obj) return true; 133 if (obj == null || getClass() != obj.getClass()) return false; 134 if (!super.equals(obj)) return false; 135 RotateCommand that = (RotateCommand) obj; 136 return Double.compare(that.startAngle, startAngle) == 0 && 137 Double.compare(that.rotationAngle, rotationAngle) == 0 && 138 Objects.equals(pivot, that.pivot) && 139 Objects.equals(deltaAngle, that.deltaAngle); 140 } 141}