source: josm/trunk/src/org/openstreetmap/josm/data/osm/WaySegment.java@ 6619

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

fix #9523 - validator: Only raise "Way contains segment twice" error if at least two nodes are found twice

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.awt.geom.Line2D;
5
6/**
7 * A segment consisting of 2 consecutive nodes out of a way.
8 */
9public 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.getId() + ", lowerIndex=" + lowerIndex + "]";
97 }
98}
Note: See TracBrowser for help on using the repository browser.