1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.awt.geom.Line2D;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * A segment consisting of 2 consecutive nodes out of a way.
|
---|
8 | */
|
---|
9 | public final class WaySegment implements Comparable<WaySegment> {
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * The way.
|
---|
13 | */
|
---|
14 | public Way way;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * The index of one of the 2 nodes in the way. The other node has the
|
---|
18 | * index <code>lowerIndex + 1</code>.
|
---|
19 | */
|
---|
20 | public int lowerIndex;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Constructs a new {@code WaySegment}.
|
---|
24 | * @param w The way
|
---|
25 | * @param i The node lower index
|
---|
26 | */
|
---|
27 | public WaySegment(Way w, int i) {
|
---|
28 | way = w;
|
---|
29 | lowerIndex = i;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Returns the first node of the way segment.
|
---|
34 | * @return the first node
|
---|
35 | */
|
---|
36 | public Node getFirstNode() {
|
---|
37 | return way.getNode(lowerIndex);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Returns the second (last) node of the way segment.
|
---|
42 | * @return the second node
|
---|
43 | */
|
---|
44 | public Node getSecondNode(){
|
---|
45 | return way.getNode(lowerIndex + 1);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Returns this way segment as complete way.
|
---|
50 | * @return the way segment as {@code Way}
|
---|
51 | */
|
---|
52 | public Way toWay() {
|
---|
53 | Way w = new Way();
|
---|
54 | w.addNode(getFirstNode());
|
---|
55 | w.addNode(getSecondNode());
|
---|
56 | return w;
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public boolean equals(Object o) {
|
---|
61 | return o instanceof WaySegment
|
---|
62 | && ((WaySegment) o).way == way
|
---|
63 | && ((WaySegment) o).lowerIndex == lowerIndex;
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public int hashCode() {
|
---|
68 | return way.hashCode() ^ lowerIndex;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public int compareTo(WaySegment o) {
|
---|
73 | return equals(o) ? 0 : toWay().compareTo(o.toWay());
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Checks whether this segment crosses other segment
|
---|
78 | *
|
---|
79 | * @param s2 The other segment
|
---|
80 | * @return true if both segments crosses
|
---|
81 | */
|
---|
82 | public boolean intersects(WaySegment s2) {
|
---|
83 | if (getFirstNode().equals(s2.getFirstNode()) || getSecondNode().equals(s2.getSecondNode()) ||
|
---|
84 | getFirstNode().equals(s2.getSecondNode()) || getSecondNode().equals(s2.getFirstNode()))
|
---|
85 | return false;
|
---|
86 |
|
---|
87 | return Line2D.linesIntersect(
|
---|
88 | getFirstNode().getEastNorth().east(), getFirstNode().getEastNorth().north(),
|
---|
89 | getSecondNode().getEastNorth().east(), getSecondNode().getEastNorth().north(),
|
---|
90 | s2.getFirstNode().getEastNorth().east(), s2.getFirstNode().getEastNorth().north(),
|
---|
91 | s2.getSecondNode().getEastNorth().east(), s2.getSecondNode().getEastNorth().north());
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public String toString() {
|
---|
96 | return "WaySegment [way=" + way.getUniqueId() + ", lowerIndex=" + lowerIndex + "]";
|
---|
97 | }
|
---|
98 | }
|
---|