source: josm/trunk/src/org/openstreetmap/josm/tools/RotationAngle.java@ 17335

Last change on this file since 17335 was 17333, checked in by Don-vip, 3 years ago

see #20129 - Fix typos and misspellings in the code (patch by gaben)

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.util.List;
5import java.util.Locale;
6
7import org.openstreetmap.josm.data.osm.IPrimitive;
8import org.openstreetmap.josm.data.osm.Node;
9import org.openstreetmap.josm.data.osm.Way;
10
11/**
12 * Determines how an icon is to be rotated depending on the primitive to be displayed.
13 * @since 8199 (creation)
14 * @since 10599 (functional interface)
15 * @since 12756 (moved from {@code gui.util} package)
16 */
17@FunctionalInterface
18public interface RotationAngle {
19
20 /**
21 * The rotation along a way.
22 */
23 final class WayDirectionRotationAngle implements RotationAngle {
24 @Override
25 public double getRotationAngle(IPrimitive p) {
26 if (!(p instanceof Node)) {
27 return 0;
28 }
29 final Node n = (Node) p;
30 final List<Way> ways = n.getParentWays();
31 if (ways.isEmpty()) {
32 return 0;
33 }
34 final Way w = ways.iterator().next();
35 final int idx = w.getNodes().indexOf(n);
36 if (idx == 0) {
37 return -Geometry.getSegmentAngle(n.getEastNorth(), w.getNode(idx + 1).getEastNorth());
38 } else {
39 return -Geometry.getSegmentAngle(w.getNode(idx - 1).getEastNorth(), n.getEastNorth());
40 }
41 }
42
43 @Override
44 public String toString() {
45 return "way-direction";
46 }
47
48 @Override
49 public int hashCode() {
50 return 1;
51 }
52
53 @Override
54 public boolean equals(Object obj) {
55 if (this == obj) {
56 return true;
57 }
58 return obj != null && getClass() == obj.getClass();
59 }
60 }
61
62 /**
63 * A static rotation
64 */
65 final class StaticRotationAngle implements RotationAngle {
66 private final double angle;
67
68 private StaticRotationAngle(double angle) {
69 this.angle = angle;
70 }
71
72 @Override
73 public double getRotationAngle(IPrimitive p) {
74 return angle;
75 }
76
77 @Override
78 public String toString() {
79 return angle + "rad";
80 }
81
82 @Override
83 public int hashCode() {
84 return Double.hashCode(angle);
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92 if (obj == null || getClass() != obj.getClass()) {
93 return false;
94 }
95 StaticRotationAngle other = (StaticRotationAngle) obj;
96 return Double.doubleToLongBits(angle) == Double.doubleToLongBits(other.angle);
97 }
98 }
99
100 /**
101 * A no-rotation angle that always returns 0.
102 * @since 11726
103 */
104 RotationAngle NO_ROTATION = new StaticRotationAngle(0);
105
106 /**
107 * Calculates the rotation angle depending on the primitive to be displayed.
108 * @param p primitive
109 * @return rotation angle
110 * @since 13623 (signature)
111 */
112 double getRotationAngle(IPrimitive p);
113
114 /**
115 * Always returns the fixed {@code angle}.
116 * @param angle angle
117 * @return rotation angle
118 */
119 static RotationAngle buildStaticRotation(final double angle) {
120 return new StaticRotationAngle(angle);
121 }
122
123 /**
124 * Parses the rotation angle from the specified {@code string}.
125 * @param string angle as string
126 * @return rotation angle
127 */
128 static RotationAngle buildStaticRotation(final String string) {
129 try {
130 return buildStaticRotation(parseCardinalRotation(string));
131 } catch (IllegalArgumentException e) {
132 throw new IllegalArgumentException("Invalid string: " + string, e);
133 }
134 }
135
136 /**
137 * Converts an angle given in cardinal directions to radians.
138 * The following values are supported: {@code n}, {@code north}, {@code ne}, {@code northeast},
139 * {@code e}, {@code east}, {@code se}, {@code southeast}, {@code s}, {@code south},
140 * {@code sw}, {@code southwest}, {@code w}, {@code west}, {@code nw}, {@code northwest}.
141 * @param cardinal the angle in cardinal directions
142 * @return the angle in radians
143 */
144 static double parseCardinalRotation(final String cardinal) {
145 switch (cardinal.toLowerCase(Locale.ENGLISH)) {
146 case "n":
147 case "north":
148 return 0; // 0 degree => 0 radian
149 case "ne":
150 case "northeast":
151 return Utils.toRadians(45);
152 case "e":
153 case "east":
154 return Utils.toRadians(90);
155 case "se":
156 case "southeast":
157 return Utils.toRadians(135);
158 case "s":
159 case "south":
160 return Math.PI; // 180 degree
161 case "sw":
162 case "southwest":
163 return Utils.toRadians(225);
164 case "w":
165 case "west":
166 return Utils.toRadians(270);
167 case "nw":
168 case "northwest":
169 return Utils.toRadians(315);
170 default:
171 throw new IllegalArgumentException("Unexpected cardinal direction " + cardinal);
172 }
173 }
174
175 /**
176 * Computes the angle depending on the referencing way segment, or {@code 0} if none exists.
177 * @return rotation angle
178 */
179 static RotationAngle buildWayDirectionRotation() {
180 return new WayDirectionRotationAngle();
181 }
182}
Note: See TracBrowser for help on using the repository browser.