-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.ts
440 lines (387 loc) · 16.1 KB
/
map.ts
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/**
* Contains the classes needed to represent a map in a simulation
*/
import { Schema, ArraySchema, type } from '@colyseus/schema';
import { Point2D, BezierCurve, EvaluatablePath, StraightLine } from '../common/math';
import { Agent } from './agent'
export class Vertex extends Schema {
@type(Point2D)
readonly location: Point2D
@type('number')
readonly id: number
@type('boolean')
readonly source: boolean
@type('boolean')
readonly dest: boolean
@type('number')
readonly weight: number
public constructor(init?: Partial<Vertex>) {
super();
Object.assign(this, init);
}
}
export class EdgeIntersect {
// The edge that the intersect was returned from
sourceEdge: Edge
// the edge that sourceEdge intersects with
edge: Edge
// The point of intersection
point: Point2D
public constructor(init?: Partial<EdgeIntersect>) {
Object.assign(this, init);
}
}
export class Edge extends Schema {
@type('number')
readonly source: number;
@type('number')
readonly dest: number;
@type('boolean')
readonly invert: boolean = false;
@type(['number'])
readonly priorities: number[]
@type('number')
readonly ctrlX: number = undefined;
@type('number')
readonly ctrlY: number = undefined;
@type('number')
readonly speed: number = 60;
@type('boolean')
readonly stopOnRed: boolean = true;
readonly sourceVertex: Vertex
readonly destVertex: Vertex
curve: EvaluatablePath
// Derived/mutable parameters
@type('number')
length: number = 0
@type('number')
currentPriority: number = 1;
lastPriority: number = 0;
// Server side properties
intersectPoints: EdgeIntersect[]
lane: Lane
public constructor(source: Vertex, dest: Vertex, invert: boolean, priorities?: number[], ctrlX?: number, ctrlY?: number, speed?: number, stopOnRed?: boolean) {
super();
this.source = source.id;
this.dest = dest.id;
this.sourceVertex = source;
this.destVertex = dest;
this.invert = invert;
if (speed != undefined)
this.speed = speed;
if (stopOnRed != undefined) {
this.stopOnRed = stopOnRed;
}
this.length = this.calculateLength();
if (priorities !== undefined && priorities.length > 0) {
this.priorities = priorities;
this.currentPriority = priorities[0];
}
this.intersectPoints = [];
this.ctrlX = ctrlX;
this.ctrlY = ctrlY;
if ((this.sourceVertex.location.x == this.destVertex.location.x || this.sourceVertex.location.y == this.destVertex.location.y) && (ctrlX == undefined || ctrlY == undefined)) {
this.curve = new StraightLine(this.sourceVertex.location, this.destVertex.location);
} else {
this.curve = new BezierCurve(this.sourceVertex.location, this.destVertex.location, this.invert, this.ctrlX != undefined && this.ctrlY != undefined ?
new Point2D({ x: this.ctrlX, y: this.ctrlY }) : undefined);
}
}
public intersectsWith(edge: Edge): EdgeIntersect {
// Currently, the approximation is using linear segments, will expand to bezier curves in the future
return this.intersectPoints.find(function (ei) { return ei.edge == edge; });
}
public getControlPoint(): Point2D {
return this.ctrlX != undefined && this.ctrlY != undefined ? new Point2D({ x: this.ctrlX, y: this.ctrlY }) : undefined;
}
public calculateIntersection(edge: Edge): Point2D {
if (edge.sourceVertex == this.destVertex || edge.destVertex == this.sourceVertex || edge.sourceVertex == this.sourceVertex || edge.destVertex == this.destVertex) return undefined;
var p: Point2D = new Point2D(this.sourceVertex.location);
var r: Point2D = this.destVertex.location.minus(p);
var q: Point2D = new Point2D(edge.sourceVertex.location);
var s: Point2D = edge.destVertex.location.minus(q);
// TODO find a better aproximation for intersections such as convex hull
if (r.cross(s) != 0) {
var t = q.minus(p).cross(s) / r.cross(s);
var u = q.minus(p).cross(r) / r.cross(s);
if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
// Edge intersects linearly
if (this.isLinear() && edge.isLinear()) {
return p.plus(r.times(t));
} else if (this.isLinear() || edge.isLinear()) {
var linearEdge = this.isLinear() ? this : edge;
var bezierEdge = linearEdge == this ? edge : this;
var bezierPath = new BezierCurve(new Point2D(bezierEdge.sourceVertex.location), new Point2D(bezierEdge.destVertex.location), bezierEdge.invert, bezierEdge.getControlPoint());
var bezierInterval = 0.5;
var p = new Point2D(linearEdge.sourceVertex.location);
var r = new Point2D(linearEdge.destVertex.location.minus(p));
var p1 = new Point2D(bezierEdge.sourceVertex.location);
var p2 = new Point2D(bezierEdge.destVertex.location);
for (var k = 2; k < 40; k++) {
var midpoint = bezierPath.evaluate(bezierInterval);
var s1 = midpoint.minus(p1);
var s2 = midpoint.minus(p2);
// Calculate intersection points
var t1 = p1.minus(p).cross(s1) / r.cross(s1);
var u1 = p1.minus(p).cross(r) / r.cross(s1);
// Two lines
var t2 = p2.minus(p).cross(s2) / r.cross(s2);
var u2 = p2.minus(p).cross(r) / r.cross(s2);
//console.log(t1 + " " + u1 + " - " + t2 + " " + u2);
if (r.cross(s1) != 0 && t1 >= 0 && t1 <= 1 && u1 >= 0 && u1 <= 1) {
bezierInterval -= 1 / Math.pow(2, k);
p2 = midpoint;
} else if (r.cross(s2) != 0 && t2 >= 0 && t2 <= 1 && u2 >= 0 && u2 <= 1) {
bezierInterval += 1 / Math.pow(2, k);
p1 = midpoint;
} else {
throw new Error("Failed to find intersection point on two edges " + JSON.stringify(linearEdge) + " and " + JSON.stringify(bezierEdge));
}
}
var ip = bezierPath.evaluate(bezierInterval);
return ip;
} else {
return undefined;
}
}
}
}
private calculateLength(): number {
var source: Point2D = new Point2D(this.sourceVertex.location);
var dest: Point2D = new Point2D(this.destVertex.location);
if ((source.x == dest.x || source.y == dest.y) && this.getControlPoint() == undefined) {
return source.distance(dest);
} else {
var step = 0.01;
var distance = 0;
var bezierCurve = new BezierCurve(source, dest, this.invert, this.getControlPoint());
var lastPoint = source;
for (var t = step; t <= 1; t += step) {
var nextPoint = bezierCurve.evaluate(t);
distance += lastPoint.distance(nextPoint);
lastPoint = nextPoint;
}
return distance;
}
}
public isLinear(): boolean {
return this.sourceVertex.location.x == this.destVertex.location.x || this.sourceVertex.location.y == this.destVertex.location.y;
}
}
export class Intersection extends Schema {
@type(['number'])
vertexIds: number[] = new ArraySchema<number>()
@type(['number'])
timings: number[] = new ArraySchema<number>()
// Derived/mutable properties
@type('number')
currentIndex: number = 0;
@type('number')
currentTime: number = 0;
vertices: Vertex[] = new ArraySchema<Vertex>()
edges: Edge[] = new ArraySchema<Edge>()
}
export class LaneEntry extends Schema {
@type('number')
source: number
@type('number')
dest: number
edge: Edge
}
export class Lane extends Schema {
@type([LaneEntry])
entries: LaneEntry[] = new ArraySchema<LaneEntry>();
}
export interface PathSegment {
getEphemeralEdge(): Edge;
}
export class LaneChangePathSegment implements PathSegment {
entryEdge: Edge;
exitEdge: Edge;
entryPoint: number;
exitPoint: number;
private ephemeralEdge = undefined;
constructor(entryEdge: Edge, exitEdge: Edge) {
this.entryEdge = entryEdge;
this.exitEdge = exitEdge;
this.entryPoint = 0.9;
this.exitPoint = 1;
}
setPoints(entryPoint: number, exitPoint: number) {
this.entryPoint = entryPoint;
this.exitPoint = exitPoint;
this.setEphemeralEdge();
}
getEphemeralEdge(): Edge {
if (this.ephemeralEdge == undefined) {
this.setEphemeralEdge();
}
return this.ephemeralEdge;
}
private setEphemeralEdge() {
this.ephemeralEdge = new Edge(new Vertex({ location: this.entryEdge.curve.evaluate(this.entryPoint) }),
new Vertex({ location: this.exitEdge.curve.evaluate(this.exitPoint) }), false, [0.1], undefined, undefined, this.exitEdge.speed);
this.ephemeralEdge.curve = new StraightLine(this.ephemeralEdge.sourceVertex.location, this.ephemeralEdge.destVertex.location);
}
}
export class EdgePathSegment implements PathSegment {
edge: Edge;
constructor(edge: Edge) {
this.edge = edge;
}
getEphemeralEdge(): Edge {
return this.edge;
}
}
export class Map extends Schema {
@type('number')
width: number
@type('number')
height: number
@type([Vertex])
vertices: Vertex[] = new ArraySchema<Vertex>()
@type([Edge])
edges: Edge[] = new ArraySchema<Edge>()
@type([Intersection])
intersections: Intersection[] = new ArraySchema<Intersection>()
@type([Agent])
agents: Agent[] = new ArraySchema<Agent>();
@type([Lane])
lanes: Lane[] = new ArraySchema<Lane>();
sources: Vertex[] = [];
destinations: Vertex[] = [];
public findVertexById(id: number): Vertex {
return this.vertices.find(function (vertex) { return vertex.id == id });
}
public findOutgoingEdge(vertex: Vertex): Edge {
return this.edges.find(function (edge) { return edge.sourceVertex == vertex });
}
public getAssignableDestinations(source: Vertex): Vertex[] {
var destinations: Vertex[] = new ArraySchema<Vertex>();
var adjacencyList = this.getAdjacencyList();
var visited: Vertex[] = [];
var next: Vertex[] = [source];
while (next.length != 0) {
var current = next.splice(0, 1)[0];
if (visited.indexOf(current) >= 0) continue;
visited.push(current);
if (current.dest) destinations.push(current);
if (current.id in adjacencyList) {
for (var entry of adjacencyList[current.id]) {
var edge = entry.edge;
next.push(edge.destVertex);
if (edge.lane != undefined) {
for (var lane of edge.lane.entries) {
next.push(lane.edge.destVertex);
}
}
}
}
}
return destinations;
}
/**
* Returns a path in stack order from source to destination
* For example, if source is 1 and dest is 3 and they are linked by vertex 2, the
* resulting array will be [3,2,1], ie. the last item will be the source.
*
* Uses an adaptation of dijkstra's algorithm
* @param source
* @param dest
*/
public getBestPath(source: Vertex, dest: Vertex): PathSegment[] {
var adjacencyList = this.getAdjacencyList();
// Initialization
var queue: Vertex[] = [];
var parent: { [id: number]: Vertex } = {};
var distance: { [id: number]: number; } = {};
for (var vertex of this.vertices) {
queue.push(vertex);
parent[vertex.id] = undefined;
distance[vertex.id] = Infinity;
}
// Start with source
distance[source.id] = 0;
while (queue.length > 0) {
// Find unvisited vertex with minimum weight
var vertex: Vertex = undefined;
for (var v of queue)
if (vertex == undefined || distance[v.id] < distance[vertex.id]) vertex = v;
if (vertex == dest) break; // If we have reached the destination, terminate dijkstra's algorithm
queue.splice(queue.indexOf(vertex), 1); // Remove vertex from list
if (!(vertex.id in adjacencyList)) continue; // Make sure we are part of adjacency list
// Find the neighbours. Lane changes count as neighbours
var neighbours: { id: number, weight: number }[] = [];
for (var entry of adjacencyList[vertex.id]) {
neighbours.push({ id: entry.edge.dest, weight: entry.weight });
if (entry.edge.lane != undefined) {
for (let lane of entry.edge.lane.entries) {
if (lane.edge != entry.edge) {
// If lane change, set weight as minimum of the two edges
var otherWeight = adjacencyList[lane.edge.source].find(a => a.edge == lane.edge).weight;
neighbours.push({ id: lane.edge.dest, weight: Math.min(entry.weight, otherWeight) });
}
}
}
}
// For each neighbour, update distances and parent if needed
for (var neighbour of neighbours) {
var d = distance[vertex.id] + neighbour.weight;
if (d < distance[neighbour.id]) {
distance[neighbour.id] = d;
parent[neighbour.id] = vertex;
}
}
}
// Path in stack order
var path: PathSegment[] = [];
// From the destination to the source, build a path with path segments
var current = dest;
var p = undefined;
while ((p = parent[current.id]) != undefined) {
for (var entry of adjacencyList[p.id]) {
// Find the edge(s) needed and add them to the path
if (entry.edge.dest == current.id) {
path.push(new EdgePathSegment(entry.edge));
break;
} else if (entry.edge.lane != undefined) {
for (var lane of entry.edge.lane.entries) {
if (lane.edge.dest == current.id) {
// Add the lane change
path.push(new EdgePathSegment(lane.edge));
path.push(new LaneChangePathSegment(entry.edge, lane.edge));
path.push(new EdgePathSegment(entry.edge));
break;
}
}
break;
}
}
current = p;
}
return path;
}
/**
* Returns an adjacency list of edges and weights for each edge
*/
public getAdjacencyList(): Record<number, { edge: Edge, weight: number }[]> {
const adjacencyList: Record<number, { edge: Edge, weight: number }[]> = {};
for (const edge of this.edges) {
var speed = edge.speed;
var numAgents = 0;
for (var agent of this.agents) {
if (agent.edge.getEphemeralEdge() == edge) {
speed = Math.min(speed, agent.speed);
numAgents++;
}
}
var weight = (edge.length + numAgents) / (speed + 1);
if (edge.sourceVertex.id in adjacencyList) {
adjacencyList[edge.sourceVertex.id].push({ edge: edge, weight: weight });
} else {
adjacencyList[edge.sourceVertex.id] = [{ edge: edge, weight: weight }];
}
}
return adjacencyList;
}
}