source: josm/trunk/src/org/openstreetmap/josm/gui/util/RotationAngle.java@ 11108

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

see #11390 - sonar - squid:S1610 - Java 8: Abstract classes without fields should be converted to interfaces

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import java.util.Locale;
5
6import org.openstreetmap.josm.data.osm.Node;
7import org.openstreetmap.josm.data.osm.OsmPrimitive;
8import org.openstreetmap.josm.data.osm.Way;
9import org.openstreetmap.josm.tools.Geometry;
10import org.openstreetmap.josm.tools.SubclassFilteredCollection;
11import org.openstreetmap.josm.tools.Utils;
12
13/**
14 * Determines how an icon is to be rotated depending on the primitive to be displayed.
15 * @since 8199 (creation)
16 * @since 10599 (functional interface)
17 */
18@FunctionalInterface
19public interface RotationAngle {
20
21 /**
22 * Calculates the rotation angle depending on the primitive to be displayed.
23 * @param p primitive
24 * @return rotation angle
25 */
26 double getRotationAngle(OsmPrimitive p);
27
28 /**
29 * Always returns the fixed {@code angle}.
30 * @param angle angle
31 * @return rotation angle
32 */
33 static RotationAngle buildStaticRotation(final double angle) {
34 return new RotationAngle() {
35 @Override
36 public double getRotationAngle(OsmPrimitive p) {
37 return angle;
38 }
39
40 @Override
41 public String toString() {
42 return angle + "rad";
43 }
44 };
45 }
46
47 /**
48 * Parses the rotation angle from the specified {@code string}.
49 * @param string angle as string
50 * @return rotation angle
51 */
52 static RotationAngle buildStaticRotation(final String string) {
53 try {
54 return buildStaticRotation(parseCardinalRotation(string));
55 } catch (IllegalArgumentException e) {
56 throw new IllegalArgumentException("Invalid string: " + string, e);
57 }
58 }
59
60 /**
61 * Converts an angle diven in cardinal directions to radians.
62 * The following values are supported: {@code n}, {@code north}, {@code ne}, {@code northeast},
63 * {@code e}, {@code east}, {@code se}, {@code southeast}, {@code s}, {@code south},
64 * {@code sw}, {@code southwest}, {@code w}, {@code west}, {@code nw}, {@code northwest}.
65 * @param cardinal the angle in cardinal directions
66 * @return the angle in radians
67 */
68 static double parseCardinalRotation(final String cardinal) {
69 switch (cardinal.toLowerCase(Locale.ENGLISH)) {
70 case "n":
71 case "north":
72 return 0; // 0 degree => 0 radian
73 case "ne":
74 case "northeast":
75 return Math.toRadians(45);
76 case "e":
77 case "east":
78 return Math.toRadians(90);
79 case "se":
80 case "southeast":
81 return Math.toRadians(135);
82 case "s":
83 case "south":
84 return Math.PI; // 180 degree
85 case "sw":
86 case "southwest":
87 return Math.toRadians(225);
88 case "w":
89 case "west":
90 return Math.toRadians(270);
91 case "nw":
92 case "northwest":
93 return Math.toRadians(315);
94 default:
95 throw new IllegalArgumentException("Unexpected cardinal direction " + cardinal);
96 }
97 }
98
99 /**
100 * Computes the angle depending on the referencing way segment, or {@code 0} if none exists.
101 * @return rotation angle
102 */
103 static RotationAngle buildWayDirectionRotation() {
104 return new RotationAngle() {
105 @Override
106 public double getRotationAngle(OsmPrimitive p) {
107 if (!(p instanceof Node)) {
108 return 0;
109 }
110 final Node n = (Node) p;
111 final SubclassFilteredCollection<OsmPrimitive, Way> ways = Utils.filteredCollection(n.getReferrers(), Way.class);
112 if (ways.isEmpty()) {
113 return 0;
114 }
115 final Way w = ways.iterator().next();
116 final int idx = w.getNodes().indexOf(n);
117 if (idx == 0) {
118 return -Geometry.getSegmentAngle(n.getEastNorth(), w.getNode(idx + 1).getEastNorth());
119 } else {
120 return -Geometry.getSegmentAngle(w.getNode(idx - 1).getEastNorth(), n.getEastNorth());
121 }
122 }
123
124 @Override
125 public String toString() {
126 return "way-direction";
127 }
128 };
129 }
130}
Note: See TracBrowser for help on using the repository browser.