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