1 | package org.openstreetmap.josm.gui.util;
|
---|
2 |
|
---|
3 | import java.util.regex.Matcher;
|
---|
4 | import java.util.regex.Pattern;
|
---|
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 | * Calculates the rotation angle depending on the primitive to displayed.
|
---|
19 | */
|
---|
20 | public abstract double getRotationAngle(OsmPrimitive p);
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Always returns the fixed {@code angle}.
|
---|
24 | */
|
---|
25 | public static RotationAngle buildStaticRotation(final double angle) {
|
---|
26 | return new RotationAngle() {
|
---|
27 | @Override
|
---|
28 | public double getRotationAngle(OsmPrimitive p) {
|
---|
29 | return angle;
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public String toString() {
|
---|
34 | return angle + "rad";
|
---|
35 | }
|
---|
36 | };
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Parses the rotation angle from the specified {@code string}.
|
---|
41 | */
|
---|
42 | public static RotationAngle buildStaticRotation(final String string) {
|
---|
43 | final Pattern radiansPattern = Pattern.compile("(\\d+\\.?\\d*)\\s*(rad)?");
|
---|
44 | final Pattern degreesPattern = Pattern.compile("(\\d+\\.?\\d*)\\s*(deg|°)");
|
---|
45 | final double value;
|
---|
46 | Matcher matcher;
|
---|
47 | if ((matcher = radiansPattern.matcher(string)).matches()) {
|
---|
48 | value = Double.parseDouble(matcher.group(1));
|
---|
49 | } else if ((matcher = degreesPattern.matcher(string)).matches()) {
|
---|
50 | value = Math.toRadians(Double.parseDouble(matcher.group(1)));
|
---|
51 | } else {
|
---|
52 | try {
|
---|
53 | value = parseCardinalRotation(string);
|
---|
54 | } catch (IllegalArgumentException ignore) {
|
---|
55 | throw new IllegalArgumentException("Invalid string: " + string);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return buildStaticRotation(value);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Converts an angle diven in cardinal directions to radians.
|
---|
63 | * The following values are supported: {@code n}, {@code north}, {@code ne}, {@code northeast},
|
---|
64 | * {@code e}, {@code east}, {@code se}, {@code southeast}, {@code s}, {@code south},
|
---|
65 | * {@code sw}, {@code southwest}, {@code w}, {@code west}, {@code nw}, {@code northwest}.
|
---|
66 | * @param cardinal the angle in cardinal directions
|
---|
67 | * @return the angle in radians
|
---|
68 | */
|
---|
69 | public static double parseCardinalRotation(final String cardinal) {
|
---|
70 | switch (cardinal.toLowerCase()) {
|
---|
71 | case "n":
|
---|
72 | case "north":
|
---|
73 | return Math.toRadians(0);
|
---|
74 | case "ne":
|
---|
75 | case "northeast":
|
---|
76 | return Math.toRadians(45);
|
---|
77 | case "e":
|
---|
78 | case "east":
|
---|
79 | return Math.toRadians(90);
|
---|
80 | case "se":
|
---|
81 | case "southeast":
|
---|
82 | return Math.toRadians(135);
|
---|
83 | case "s":
|
---|
84 | case "south":
|
---|
85 | return Math.toRadians(180);
|
---|
86 | case "sw":
|
---|
87 | case "southwest":
|
---|
88 | return Math.toRadians(225);
|
---|
89 | case "w":
|
---|
90 | case "west":
|
---|
91 | return Math.toRadians(270);
|
---|
92 | case "nw":
|
---|
93 | case "northwest":
|
---|
94 | return Math.toRadians(315);
|
---|
95 | default:
|
---|
96 | throw new IllegalArgumentException("Unexpected cardinal direction " + cardinal);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Computes the angle depending on the referencing way segment, or {@code 0} if none exists.
|
---|
102 | */
|
---|
103 | public 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 | }
|
---|