-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRoute.pde
More file actions
274 lines (235 loc) · 7.3 KB
/
Copy pathRoute.pde
File metadata and controls
274 lines (235 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* @class Route
* @desc An undirected collection of transport access nodes (Stops)
*/
class Route {
public String[] stops;
public HashMap links;
public String name;
public int busCount = 0;
public float totalLength;
public int nullStops = 0;
public int maxBusCount = 0;
private String originID;
private boolean constructed = false;
public float length;
java.util.List stopList;
public Route(String name) {
this.name = name;
this.links = new HashMap();
this.stopList = new ArrayList();
}
public String[] getStops() {
if(!this.hasConstructedRoute()) this.construct();
return this.stops;
}
public float getRouteLength() {
if(this.totalLength == 0) {
this.totalLength = 0;
for (int i=0; i < this.stops.length-1;i++) {
Stop oStopFrom = (Stop) net.aStop.get(ltrim(this.stops[i]));
Stop oStopTo = (Stop) net.aStop.get(ltrim(this.stops[i+1]));
this.totalLength += oStopFrom.getDistanceTo(oStopTo);
}
}
return this.totalLength;
}
public float getRouteLengthPixels()
{
return this.getRouteLength() * net.getPixelsPerMeter();
}
public void addStops(String[] pieces,Boolean named)
{
int index = named ? 1: 0;
this.stops = new String[pieces.length-index];
if(named) {
this.name = pieces[0];
}
for(int i =index; i < pieces.length;i++) {
this.stops[i-index]= (pieces[i]);
}
this.constructed = true;
}
public int getMaxBusCount(float routeAverage)
{
if(this.maxBusCount == 0) {
this.maxBusCount = min((int)round(routeAverage*1.3),round(2.5*(this.stops.length - this.nullStops)));
}
return this.maxBusCount;
}
private boolean hasConstructedRoute()
{
return (this.constructed);
}
public void printme()
{
print("ROUTE "+this.name+"\n");
for (int t=0; t < this.stops.length-1;t++) {
String s = this.stops[t];
Stop stopx = (Stop) net.aStop.get(ltrim(s));
Point p = stopx.getCoords();
print(ltrim(s)+"," + p.x+","+p.y + "\n");
}
}
/*
* @desc Incrementally build the route when parsing ncol files.
*/
public void addLink(String id, String id2)
{
//try {
if(this.links.containsKey(id)) {
List linkList = (List)this.links.get(id);
linkList.add(id2);
this.links.put(id,linkList);
this.addStop(id);
this.addStop(id2);
} else {
List aList = new ArrayList();
aList.add(id2);
this.addStop(id);
this.addStop(id2);
this.links.put(id,aList);
}
//} catch(Exception o) {
//debug("tried to link invalid stops: " + id + ","+id2,DEBUG_WARN);
//}
}
private void addStop(String stopID)
{
if(!this.stopList.contains(ltrim(stopID))) {
this.stopList.add(stopID);
}
}
/*
* @desc Constructs a route from a set of links (or edges) specified in an ncol style file.
*/
public void construct()
{
try {
this.originID = this.findRouteOrigin();
} catch(Exception o) {
debug(o.getMessage(),DEBUG_WARN);
this.originID = (String)this.stopList.get(0);
}
debug("found route origin " + originID + " for ROUTE ID " + this.name);
this.stopList = new ArrayList();
this.addStop(originID);
this.followRoute(originID);
this.stops = new String[this.stopList.size()]; //reset this
for(int i=0;i<this.stopList.size();i++) {
this.stops[i] = (String)this.stopList.get(i);
}
//this.printStops();
this.constructed = true;
}
public Stop getDestination()
{
return (Stop)net.aStop.get(this.stops[this.stops.length-1]);
}
public Stop getOrigin()
{
return (Stop)net.aStop.get(this.stops[0]);
}
/*
* @desc Recursive path finding algorithm
*/
private void followRoute(String origin)
{
List linkDestinations = (List)this.links.get(origin);
if(linkDestinations == null) return;
String dest = null;
if (linkDestinations.size() == 1 && !this.isOrigin(origin)) return; //base case
for(int i=0; i < linkDestinations.size(); i++) {
dest = (String)linkDestinations.get(i);
//debug("evaluating dest " + dest);
if(!this.stopList.contains(dest)) {
this.addStop(dest);
Stop originStop = (Stop)net.aStop.get(origin);
Stop destStop = (Stop)net.aStop.get(dest);
if(this.name.equals("2")) {
debug("CONSTRUCTING LINK " + originStop.name + "(" + origin + ") - " + destStop.name + "(" + dest + ")");
}
this.followRoute(dest);
}
}
}
private boolean isOrigin(String id)
{
return this.originID.equals(id);
}
/**
* @return Length of trip, in pixels
*/
public double getLength()
{
Stop origin = this.getOrigin();
Stop dest = this.getDestination();
if(origin!= null && dest!= null) {
this.length = sqrt(pow(abs(dest.getCoords().x - origin.getCoords().x),2) + pow(abs(dest.getCoords().y - origin.getCoords().y),2));
return this.length;
} else {
debug("NO ORIGIN " + origin.ID);
return 0.0;
}
}
private void printStops()
{
String out = new String();
for (int i=0;i< this.stops.length;i++) {
Stop s = (Stop)net.aStop.get(ltrim(this.stops[i]));
try {
out += s.name + "(";
out += this.stops[i];
out += "),";
}catch(Exception o) {
}
}
debug(this.name + ": " +out);
}
private boolean containsStop(String stopID)
{
for (int i=0; i < this.stops.length;i++) {
if(this.stops[i].equals(stopID)) return true;
}
return false;
}
private String findRouteOrigin() throws Exception
{
for (int i=0; i < this.stopList.size();i++) {
List stopLinks = (List)this.links.get(this.stopList.get(i));
if(stopLinks != null && stopLinks.size() == 1) return (String) this.stopList.get(i);
}
throw (Exception) new Exception("DID NOT DETECT A ROUTE ORIGIN FOR ROUTE");
}
public void draw() {
float zoomScale = 1.0;
for (int i=0; i < this.stops.length-1;i++) {
Stop oStopFrom = (Stop) net.aStop.get(ltrim(this.stops[i]));
Stop oStopTo = (Stop) net.aStop.get(ltrim(this.stops[i+1]));
if(cp.flags[ControlPanel.NODES] || net.firstRun) {
try {
oStopFrom.draw(zoomScale,net.hasAttribs());
}catch(Exception o) {
continue;
}
}
if(!cp.flags[ControlPanel.ROUTES] && !net.firstRun) continue;
try {
float dim = net.getDimension();
float dimY = net.getYDimension();
float offset = 0;
if(!trueAspectRatio) dimY = dim;
int xFrom = int(map(oStopFrom.easting,MinX,MaxX,offset,dim));
int yFrom = int(map(oStopFrom.northing,MinY,MaxY,offset,dimY));
int xTo = int(map(oStopTo.easting,MinX,MaxX,offset,dim));
int yTo = int(map(oStopTo.northing,MinY,MaxY,offset,dimY));
strokeWeight(1);
stroke(100,100);
if(net.firstRun) stroke(0,0);
float yOffset = (trueAspectRatio) ? -(height-dimY)/2 : net.getYOffset();
line(xFrom + net.getXOffset(),height-yFrom - yOffset,xTo + net.getXOffset(),height-yTo - yOffset);
} catch(Exception o) {
}
}
}
}