001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import java.util.List; 005import java.util.Locale; 006 007import org.openstreetmap.josm.data.osm.IPrimitive; 008import org.openstreetmap.josm.data.osm.Node; 009import org.openstreetmap.josm.data.osm.Way; 010 011/** 012 * Determines how an icon is to be rotated depending on the primitive to be displayed. 013 * @since 8199 (creation) 014 * @since 10599 (functional interface) 015 * @since 12756 (moved from {@code gui.util} package) 016 */ 017@FunctionalInterface 018public interface RotationAngle { 019 020 /** 021 * The rotation along a way. 022 */ 023 final class WayDirectionRotationAngle implements RotationAngle { 024 @Override 025 public double getRotationAngle(IPrimitive p) { 026 if (!(p instanceof Node)) { 027 return 0; 028 } 029 final Node n = (Node) p; 030 final List<Way> ways = n.getParentWays(); 031 if (ways.isEmpty()) { 032 return 0; 033 } 034 final Way w = ways.iterator().next(); 035 Double angle = getRotationAngleForNodeOnWay(n, w); 036 return angle != null ? angle : 0; 037 } 038 039 /** 040 * Calculates the rotation angle of a node in a way based on the preceding way segment. 041 * If the node is the first node in the given way, the angle of the following way segment is used instead. 042 * @param n the node to get the rotation angle for 043 * @param w the way containing the node 044 * @return the rotation angle in radians or null if the way does not contain the node 045 * @since 18661 046 */ 047 public static Double getRotationAngleForNodeOnWay(Node n, Way w) { 048 final int idx = w.getNodes().indexOf(n); 049 if (idx == -1) { 050 return null; 051 } 052 if (idx == 0) { 053 return -(Geometry.getSegmentAngle(n.getEastNorth(), w.getNode(idx + 1).getEastNorth()) - Math.PI/2); 054 } else { 055 return -(Geometry.getSegmentAngle(w.getNode(idx - 1).getEastNorth(), n.getEastNorth()) - Math.PI/2); 056 } 057 } 058 059 @Override 060 public String toString() { 061 return "way-direction"; 062 } 063 064 @Override 065 public int hashCode() { 066 return 1; 067 } 068 069 @Override 070 public boolean equals(Object obj) { 071 if (this == obj) { 072 return true; 073 } 074 return obj != null && getClass() == obj.getClass(); 075 } 076 } 077 078 /** 079 * A static rotation 080 */ 081 final class StaticRotationAngle implements RotationAngle { 082 private final double angle; 083 084 private StaticRotationAngle(double angle) { 085 this.angle = angle; 086 } 087 088 @Override 089 public double getRotationAngle(IPrimitive p) { 090 return angle; 091 } 092 093 @Override 094 public String toString() { 095 return angle + "rad"; 096 } 097 098 @Override 099 public int hashCode() { 100 return Double.hashCode(angle); 101 } 102 103 @Override 104 public boolean equals(Object obj) { 105 if (this == obj) { 106 return true; 107 } 108 if (obj == null || getClass() != obj.getClass()) { 109 return false; 110 } 111 StaticRotationAngle other = (StaticRotationAngle) obj; 112 return Double.doubleToLongBits(angle) == Double.doubleToLongBits(other.angle); 113 } 114 } 115 116 /** 117 * A no-rotation angle that always returns 0. 118 * @since 11726 119 */ 120 RotationAngle NO_ROTATION = new StaticRotationAngle(0); 121 122 /** 123 * Calculates the rotation angle depending on the primitive to be displayed. 124 * @param p primitive 125 * @return rotation angle in radians, clockwise starting from up/north 126 * @since 13623 (signature) 127 */ 128 double getRotationAngle(IPrimitive p); 129 130 /** 131 * Always returns the fixed {@code angle}. 132 * @param angle angle 133 * @return rotation angle 134 */ 135 static RotationAngle buildStaticRotation(final double angle) { 136 return new StaticRotationAngle(angle); 137 } 138 139 /** 140 * Parses the rotation angle from the specified {@code string}. 141 * @param string angle as string 142 * @return rotation angle 143 */ 144 static RotationAngle buildStaticRotation(final String string) { 145 try { 146 return buildStaticRotation(parseCardinalRotation(string)); 147 } catch (IllegalArgumentException e) { 148 throw new IllegalArgumentException("Invalid string: " + string, e); 149 } 150 } 151 152 /** 153 * Converts an angle given in cardinal directions to radians. 154 * The following values are supported: {@code n}, {@code north}, {@code ne}, {@code northeast}, 155 * {@code e}, {@code east}, {@code se}, {@code southeast}, {@code s}, {@code south}, 156 * {@code sw}, {@code southwest}, {@code w}, {@code west}, {@code nw}, {@code northwest}. 157 * @param cardinal the angle in cardinal directions 158 * @return the angle in radians 159 */ 160 static double parseCardinalRotation(final String cardinal) { 161 switch (cardinal.toLowerCase(Locale.ENGLISH)) { 162 case "n": 163 case "north": 164 return 0; // 0 degree => 0 radian 165 case "ne": 166 case "northeast": 167 return Utils.toRadians(45); 168 case "e": 169 case "east": 170 return Utils.toRadians(90); 171 case "se": 172 case "southeast": 173 return Utils.toRadians(135); 174 case "s": 175 case "south": 176 return Math.PI; // 180 degree 177 case "sw": 178 case "southwest": 179 return Utils.toRadians(225); 180 case "w": 181 case "west": 182 return Utils.toRadians(270); 183 case "nw": 184 case "northwest": 185 return Utils.toRadians(315); 186 default: 187 throw new IllegalArgumentException("Unexpected cardinal direction " + cardinal); 188 } 189 } 190 191 /** 192 * Computes the angle depending on the referencing way segment, or {@code 0} if none exists. 193 * @return rotation angle 194 */ 195 static RotationAngle buildWayDirectionRotation() { 196 return new WayDirectionRotationAngle(); 197 } 198}