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

Last change on this file since 8378 was 8378, checked in by Don-vip, 9 years ago

fix copyright/license headers globally

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