1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.vehicular.simulator; |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | import java.util.*; |
---|
11 | |
---|
12 | import vnsim.map.object.*; |
---|
13 | |
---|
14 | |
---|
15 | public class RouteSegment implements java.io.Serializable { |
---|
16 | |
---|
17 | //a segment, identified by a road index and two point indexes |
---|
18 | //(the order of the point indexes does matter - there are two possible ways |
---|
19 | //to look at the same segment, depending on the direction) |
---|
20 | |
---|
21 | /** <code>serialVersionUID</code> */ |
---|
22 | private static final long serialVersionUID = -5883719321862303634L; |
---|
23 | |
---|
24 | public short roadIndex; |
---|
25 | public short pt1; |
---|
26 | public short pt2; |
---|
27 | |
---|
28 | //pt1 and pt2 are point indexes on the road 'roadIndex'; they can be in any order |
---|
29 | //(pt1>pt2 or pt1<pt2) |
---|
30 | |
---|
31 | public RouteSegment(short roadIndex, short pt1, short pt2) { |
---|
32 | this.roadIndex=roadIndex; |
---|
33 | this.pt1=pt1; |
---|
34 | this.pt2=pt2; |
---|
35 | } |
---|
36 | |
---|
37 | public boolean contains(Location loc) { |
---|
38 | if(pt1>=pt2) { |
---|
39 | for(int i=pt2;i<=pt1;i++) { |
---|
40 | Location test=new Location(roadIndex,i); |
---|
41 | if(test.equals(loc)) |
---|
42 | return true; |
---|
43 | } |
---|
44 | } else { |
---|
45 | for(int i=pt1;i<=pt2;i++) { |
---|
46 | Location test=new Location(roadIndex,i); |
---|
47 | if(test.equals(loc)) |
---|
48 | return true; |
---|
49 | } |
---|
50 | } |
---|
51 | return false; |
---|
52 | } |
---|
53 | |
---|
54 | public boolean checkOrder(Location l1, Location l2) { |
---|
55 | //checks whether l1 and l2 appear in the current route segment, in this order |
---|
56 | int state=0; //0=none found; 1=l1 found |
---|
57 | |
---|
58 | if(l1.roadIdx==l2.roadIdx && l1.roadIdx==this.roadIndex) { |
---|
59 | if(pt1>=pt2) { |
---|
60 | if(pt1>=l1.ptIdx && l1.ptIdx>=l2.ptIdx && l2.ptIdx>=pt2) |
---|
61 | return true; |
---|
62 | return false; |
---|
63 | } else { |
---|
64 | if(pt1<=l1.ptIdx && l1.ptIdx<=l2.ptIdx && l2.ptIdx<=pt2) |
---|
65 | return true; |
---|
66 | return false; |
---|
67 | } |
---|
68 | } |
---|
69 | if(l1.equals(l2)) return this.contains(l1); |
---|
70 | |
---|
71 | // else check each point |
---|
72 | if(pt1>=pt2) { |
---|
73 | for(int i=pt1;i>=pt2;i--) { |
---|
74 | Location test=new Location(roadIndex,i); |
---|
75 | if(state==0) { |
---|
76 | if(test.equals(l2)) return false; |
---|
77 | if(test.equals(l1)) state=1; |
---|
78 | } else { |
---|
79 | if(test.equals(l2)) return true; |
---|
80 | } |
---|
81 | } |
---|
82 | } else { |
---|
83 | for(int i=pt1;i<=pt2;i++) { |
---|
84 | Location test=new Location(roadIndex,i); |
---|
85 | if(state==0) { |
---|
86 | if(test.equals(l2)) return false; |
---|
87 | if(test.equals(l1)) state=1; |
---|
88 | } else { |
---|
89 | if(test.equals(l2)) return true; |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | return false; |
---|
94 | } |
---|
95 | |
---|
96 | public double distanceBetween(Location loc) { |
---|
97 | //assume loc is inside the current route segment; get the distance from loc |
---|
98 | //to the end of the current route segment; |
---|
99 | //return -1.0 on error; |
---|
100 | |
---|
101 | Road r=(Road) Globals.map.roads.get(roadIndex); |
---|
102 | if(loc.roadIdx==this.roadIndex) { |
---|
103 | if(pt1>=pt2) { |
---|
104 | return ((r.points.get(loc.ptIdx))).getDistance() |
---|
105 | - |
---|
106 | ((r.points.get(pt2))).getDistance(); |
---|
107 | } else { |
---|
108 | return ((Point)(r.points.get(pt2))).getDistance() |
---|
109 | - |
---|
110 | ((Point)(r.points.get(loc.ptIdx))).getDistance(); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | //else check each point |
---|
115 | if(pt1>=pt2) { |
---|
116 | for(int i=pt2;i<=pt1;i++) { |
---|
117 | Location test=new Location(roadIndex,i); |
---|
118 | if(test.equals(loc)) { |
---|
119 | return ((Point)(r.points.get(i))).getDistance() |
---|
120 | - |
---|
121 | ((Point)(r.points.get(pt2))).getDistance(); |
---|
122 | } |
---|
123 | } |
---|
124 | } else { |
---|
125 | for(int i=pt1;i<=pt2;i++) { |
---|
126 | Location test=new Location(roadIndex,i); |
---|
127 | if(test.equals(loc)) { |
---|
128 | return ((Point)(r.points.get(pt2))).getDistance() |
---|
129 | - |
---|
130 | ((Point)(r.points.get(i))).getDistance(); |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | return -1.0; |
---|
135 | } |
---|
136 | |
---|
137 | public double distanceFrom(Location loc) { |
---|
138 | //assume loc is inside the current segment! |
---|
139 | //calculates the distance between the start of the current route segment and the location loc; |
---|
140 | //return -1.0 on error; |
---|
141 | Road r=(Road) Globals.map.roads.get(roadIndex); |
---|
142 | if(loc.roadIdx==this.roadIndex) { |
---|
143 | if(pt1>=pt2) { |
---|
144 | return ((r.points.get(pt1))).getDistance() |
---|
145 | - |
---|
146 | ((r.points.get(loc.ptIdx))).getDistance(); |
---|
147 | } else { |
---|
148 | return ((r.points.get(loc.ptIdx))).getDistance() |
---|
149 | - |
---|
150 | ((r.points.get(pt1))).getDistance(); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | //else check each point |
---|
155 | if(pt1>=pt2) { |
---|
156 | for(int i=pt2;i<=pt1;i++) { |
---|
157 | Location test=new Location(roadIndex,i); |
---|
158 | if(test.equals(loc)) { |
---|
159 | return ((Point)(r.points.get(pt1))).getDistance() |
---|
160 | - |
---|
161 | ((Point)(r.points.get(i))).getDistance(); |
---|
162 | } |
---|
163 | } |
---|
164 | } else { |
---|
165 | for(int i=pt1;i<=pt2;i++) { |
---|
166 | Location test=new Location(roadIndex,i); |
---|
167 | if(test.equals(loc)) { |
---|
168 | return ((Point)(r.points.get(i))).getDistance() |
---|
169 | - |
---|
170 | ((Point)(r.points.get(pt1))).getDistance(); |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | return -1.0; |
---|
175 | } |
---|
176 | |
---|
177 | public boolean equals(RouteSegment other) { |
---|
178 | if(this.roadIndex==other.roadIndex) { |
---|
179 | if(this.pt1==other.pt1 && this.pt2==other.pt2) |
---|
180 | return true; |
---|
181 | if(this.pt2==other.pt1 && this.pt1==other.pt2) |
---|
182 | return true; |
---|
183 | return false; |
---|
184 | } |
---|
185 | return false; |
---|
186 | } |
---|
187 | |
---|
188 | public String toString() { |
---|
189 | String ret=""; |
---|
190 | //ret=ret+"[Rd "+rdIdx1+" Pt "+ptIdx1+"--Rd "+rdIdx2+" Pt "+ptIdx2; |
---|
191 | try{ |
---|
192 | ret=ret+"[Rd:"+roadIndex+";From="+pt1+";To="+pt2+"]"; |
---|
193 | return ret; |
---|
194 | } catch (Exception ex) { |
---|
195 | ret="EXCEPTION! RouteSegment.toString()!"; |
---|
196 | return ret; |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|