@maptalks/traffic 0.101.0 → 0.101.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,919 +0,0 @@
1
- /*!
2
- * @maptalks/traffic v0.98.6
3
- * LICENSE : UNLICENSED
4
- * (c) 2016-2024 maptalks.org
5
- */
6
- import * as t from "maptalks";
7
-
8
- import * as s from "@turf/turf";
9
-
10
- import { GLTFLayer as i, MultiGLTFMarker as h } from "@maptalks/gltf-layer";
11
-
12
- let n = 0;
13
-
14
- function e(t, s, i) {
15
- return t.length !== +t.length && (t = Object.values(t)), t[o(t.length - 1)];
16
- }
17
-
18
- function r(t) {
19
- const s = ++n + "";
20
- return t ? t + s : s;
21
- }
22
-
23
- function o(t, s) {
24
- return s || (s = t, t = 0), t + Math.floor(Math.random() * (s - t + 1));
25
- }
26
-
27
- class u {
28
- constructor(t, s) {
29
- this.source = t, this.target = s;
30
- }
31
- get vector() {
32
- return this.target.subtract(this.source);
33
- }
34
- get length() {
35
- return this.vector.length;
36
- }
37
- get direction() {
38
- return this.vector.direction;
39
- }
40
- get center() {
41
- return this.getPoint(.5);
42
- }
43
- split(t, s) {
44
- let i, h, n, e, r, o;
45
- const u = s ? function() {
46
- r = [];
47
- for (let s = e = t - 1; e <= 0 ? s <= 0 : s >= 0; e <= 0 ? s++ : s--) r.push(s);
48
- return r;
49
- }.apply(this) : function() {
50
- o = [];
51
- for (let s = 0, i = t - 1; 0 <= i ? s <= i : s >= i; 0 <= i ? s++ : s--) o.push(s);
52
- return o;
53
- }.apply(this), l = [];
54
- for (h = 0, n = u.length; h < n; h++) i = u[h], l.push(this.subsegment(i / t, (i + 1) / t));
55
- return l;
56
- }
57
- getPoint(t) {
58
- return this.source.add(this.vector.mult(t));
59
- }
60
- subsegment(t, s) {
61
- const i = this.vector, h = this.source.add(i.mult(t)), n = this.source.add(i.mult(s));
62
- return new u(h, n);
63
- }
64
- }
65
-
66
- class l {
67
- constructor(t, s, i, h) {
68
- this.A = t, this.B = s, this.O = i, this.Q = h, this.AO = new u(this.A, this.O),
69
- this.OQ = new u(this.O, this.Q), this.QB = new u(this.Q, this.B), this.len = null;
70
- }
71
- get length() {
72
- let t, s, i, h, n;
73
- if (null == this.len) for (i = 10, h = null, this.len = 0, t = n = 0; n <= 10; t = ++n) s = this.getPoint(t / 10),
74
- h && (this.len += s.subtract(h).length), h = s;
75
- return this.len;
76
- }
77
- getPoint(t) {
78
- const s = this.AO.getPoint(t), i = this.OQ.getPoint(t), h = this.QB.getPoint(t), n = new u(s, i).getPoint(t), e = new u(i, h).getPoint(t);
79
- return new u(n, e).getPoint(t);
80
- }
81
- getDirection(t) {
82
- const s = this.AO.getPoint(t), i = this.OQ.getPoint(t), h = this.QB.getPoint(t), n = new u(s, i).getPoint(t), e = new u(i, h).getPoint(t);
83
- return new u(n, e).direction;
84
- }
85
- }
86
-
87
- class c {
88
- constructor(t, s, i) {
89
- this.id = r("laneposition"), this.free = !0, this.car = t, this.position = i, this.tmpLane = s;
90
- }
91
- get lane() {
92
- return this.tmpLane;
93
- }
94
- set lane(t) {
95
- this.release(), this.tmpLane = t;
96
- }
97
- get relativePosition() {
98
- return this.position / this.lane.length;
99
- }
100
- get nextCarDistance() {
101
- let t, s;
102
- const i = this.getNext();
103
- return i ? (s = i.position - i.car.length / 2, t = this.position + this.car.length / 2,
104
- {
105
- car: i.car,
106
- distance: s - t
107
- }) : {
108
- car: null,
109
- distance: 1 / 0
110
- };
111
- }
112
- acquire() {
113
- let t;
114
- return null != (null != (t = this.lane) ? t.addCarPosition : void 0) ? (this.free = !1,
115
- this.lane.addCarPosition(this)) : null;
116
- }
117
- release() {
118
- let t;
119
- return !this.free && (null != (t = this.lane) ? t.removeCar : void 0) ? (this.free = !0,
120
- this.lane.removeCar(this)) : null;
121
- }
122
- getNext() {
123
- return this.lane && !this.free ? this.lane.getNext(this) : null;
124
- }
125
- }
126
-
127
- class a {
128
- constructor(t, s, i) {
129
- this.isChangingLanes = !1, this.car = t, this.current = new c(t, s, i || 0), this.current.acquire(),
130
- this.next = new c(t), this.temp = new c(t);
131
- }
132
- get lane() {
133
- return this.temp.lane || this.current.lane;
134
- }
135
- get absolutePosition() {
136
- return null !== this.temp.lane && void 0 !== this.temp.lane ? this.temp.position : this.current.position;
137
- }
138
- get relativePosition() {
139
- return this.absolutePosition / this.lane.length;
140
- }
141
- get direction() {
142
- return this.lane.getDirection();
143
- }
144
- get coords() {
145
- return this.lane.getPoint(this.relativePosition);
146
- }
147
- get nextCarDistance() {
148
- const t = this.current.nextCarDistance, s = this.next.nextCarDistance;
149
- return t.distance < s.distance ? t : s;
150
- }
151
- get distanceToStopLine() {
152
- return this.canEnterIntersection() ? 1 / 0 : this.getDistanceToIntersection();
153
- }
154
- get nextIntersection() {
155
- return this.current.lane.road.target;
156
- }
157
- get previousIntersection() {
158
- return this.current.lane.road.source;
159
- }
160
- isValidTurn() {
161
- const t = this.car.nextLane, s = this.current.lane, i = s.getTurnDirection(t);
162
- if (3 === i) throw Error("no U-turns are allowed");
163
- if (0 === i && !s.isLeftmost) throw Error("no left turns from this lane");
164
- if (2 === i && !s.isRightmost) throw Error("no right turns from this lane");
165
- return !0;
166
- }
167
- canEnterIntersection() {
168
- const t = this.car.nextLane, s = this.current.lane;
169
- if (!t) return !0;
170
- const i = this.nextIntersection, h = s.getTurnDirection(t), n = s.road.targetSideId;
171
- return i.controlSignals.state[n][h];
172
- }
173
- getDistanceToIntersection() {
174
- const t = this.current.lane.length - this.car.length / 2 - this.current.position;
175
- return this.isChangingLanes ? 1 / 0 : Math.max(t, 0);
176
- }
177
- timeToMakeTurn(t) {
178
- return void 0 === t && (t = 0), this.getDistanceToIntersection() <= t;
179
- }
180
- moveForward(t) {
181
- let s, i;
182
- t = Math.max(t, 0), this.current.position += t, this.next.position += t, this.temp.position += t,
183
- this.timeToMakeTurn() && this.canEnterIntersection() && this.isValidTurn() && this.t(this.car.popNextLane(), 0);
184
- const h = this.temp.position / (null != (s = this.temp.lane) ? s.length : void 0), n = 2 * this.car.length;
185
- return this.isChangingLanes && this.temp.position > n && !this.current.free && this.current.release(),
186
- this.isChangingLanes && this.next.free && this.temp.position + n > (null != (i = this.temp.lane) ? i.length : void 0) && this.next.acquire(),
187
- this.isChangingLanes && h >= 1 && this.i(), !this.current.lane || this.isChangingLanes || this.car.nextLane ? null : this.car.pickNextLane();
188
- }
189
- changeLane(t) {
190
- if (this.isChangingLanes) throw Error("already changing lane");
191
- if (null == t) throw Error("no next lane");
192
- if (t === this.lane) throw Error("next lane == current lane");
193
- if (this.lane.road !== t.road) throw Error("not neighbouring lanes");
194
- const s = this.current.position + 3 * this.car.length;
195
- return this.t(t, s);
196
- }
197
- h() {
198
- const t = this.current.lane.getPoint(this.current.relativePosition), s = this.next.lane.getPoint(this.next.relativePosition), i = s.subtract(t).length, h = this.current.lane.middleLine.vector.normalized.mult(.3 * i), n = t.add(h), e = this.next.lane.middleLine.vector.normalized.mult(.3 * i), r = s.subtract(e);
199
- return new l(t, s, n, r);
200
- }
201
- o() {
202
- return this.h();
203
- }
204
- t(t, s) {
205
- if (this.isChangingLanes) throw Error("already changing lane");
206
- if (null == t) return 0;
207
- this.isChangingLanes = !0, this.next.lane = t, this.next.position = s;
208
- const i = this.o();
209
- return this.temp.lane = i, this.temp.position = 0, this.next.position -= this.temp.lane.length;
210
- }
211
- i() {
212
- if (!this.isChangingLanes) throw Error("no lane changing is going on");
213
- return this.isChangingLanes = !1, this.current.lane = this.next.lane, this.current.position = this.next.position || 0,
214
- this.current.acquire(), this.next.lane = null, this.next.position = NaN, this.temp.lane = null,
215
- this.temp.position = NaN, this.current.lane;
216
- }
217
- release() {
218
- let t, s, i;
219
- return null != (t = this.current) && t.release(), null != (s = this.next) && s.release(),
220
- null != (i = this.temp) ? i.release() : void 0;
221
- }
222
- }
223
-
224
- const f = [];
225
-
226
- class d {
227
- constructor(t, s) {
228
- this.type = o(f.length - 1), this.id = r("car"), this.color = (300 + 240 * Math.random() | 0) % 360,
229
- this.preferedLane = null, this.width = 2, this.length = 2, this.tmpSpeed = 0, this.maxSpeed = 30,
230
- this.s0 = 2, this.timeHeadway = 1.5, this.maxAcceleration = 1, this.maxDeceleration = 3,
231
- this.alive = !0, this.trajectory = new a(this, t, s);
232
- }
233
- get coords() {
234
- return this.trajectory.coords;
235
- }
236
- get speed() {
237
- return this.tmpSpeed;
238
- }
239
- set speed(t) {
240
- t < 0 && (t = 0), t > this.maxSpeed && (t = this.maxSpeed), this.tmpSpeed = t;
241
- }
242
- get direction() {
243
- return this.trajectory.direction;
244
- }
245
- release() {
246
- return this.trajectory.release();
247
- }
248
- getAcceleration() {
249
- let t;
250
- const s = this.trajectory.nextCarDistance, i = Math.max(s.distance, 0), h = this.maxAcceleration, n = this.maxDeceleration, e = this.speed - (null != (t = s.car) ? t.speed : void 0) || 0, r = Math.pow(this.speed / this.maxSpeed, 4), o = this.s0, u = this.speed * this.timeHeadway, l = o + u + this.speed * e / (2 * Math.sqrt(h * n)), c = Math.pow(l / i, 2), a = 1 + u + Math.pow(this.speed, 2) / (2 * n), f = 1 - r - c - Math.pow(a / this.trajectory.distanceToStopLine, 2);
251
- return this.maxAcceleration * f;
252
- }
253
- move(t) {
254
- const s = this.getAcceleration();
255
- if (this.speed += s * t, !this.trajectory.isChangingLanes && this.nextLane) {
256
- const t = this.trajectory.current.lane, s = t.getTurnDirection(this.nextLane), i = function() {
257
- switch (s) {
258
- case 0:
259
- return t.leftmostAdjacent;
260
-
261
- case 2:
262
- return t.rightmostAdjacent;
263
-
264
- default:
265
- return t;
266
- }
267
- }();
268
- i !== t && this.trajectory.changeLane(i);
269
- }
270
- const i = this.speed * t + .5 * s * Math.pow(t, 2);
271
- return this.trajectory.nextCarDistance.distance < i && console.log("bad IDM"), this.trajectory.timeToMakeTurn(i) && null == this.nextLane ? (this.alive = !1,
272
- this.alive) : this.trajectory.moveForward(i);
273
- }
274
- pickNextRoad() {
275
- const t = this.trajectory.nextIntersection, s = this.trajectory.current.lane, i = t.roads.filter((function(t) {
276
- return t.target !== s.road.source;
277
- }));
278
- if (0 === i.length) return null;
279
- return e(i);
280
- }
281
- pickNextLane() {
282
- if (this.nextLane) throw Error("next lane is already chosen");
283
- this.nextLane = null;
284
- const t = this.pickNextRoad();
285
- if (!t) return null;
286
- const s = this.trajectory.current.lane.road.getTurnDirection(t), i = function() {
287
- switch (s) {
288
- case 0:
289
- return t.lanesNumber - 1;
290
-
291
- case 1:
292
- return o(0, t.lanesNumber - 1);
293
-
294
- case 2:
295
- return 0;
296
- }
297
- return null;
298
- }();
299
- return this.nextLane = t.lanes[i], this.nextLane;
300
- }
301
- popNextLane() {
302
- const t = this.nextLane;
303
- return this.nextLane = null, this.preferedLane = null, t;
304
- }
305
- }
306
-
307
- class g {
308
- constructor(t) {
309
- this.flipMultiplier = .4 * Math.random() - .2 + 1, this.time = 0, this.stateNum = 0,
310
- this.lightsFlipInterval = 20, this.states = [ [ "L", "", "L", "" ], [ "FR", "", "FR", "" ], [ "", "L", "", "L" ], [ "", "FR", "", "FR" ] ],
311
- this.intersection = t;
312
- }
313
- get flipInterval() {
314
- return this.flipMultiplier * this.lightsFlipInterval;
315
- }
316
- get state() {
317
- let t = this.states[this.stateNum % this.states.length];
318
- this.intersection.roads.length <= 2 && (t = [ "LFR", "LFR", "LFR", "LFR" ]);
319
- const s = [];
320
- for (let i = 0, h = t.length; i < h; i++) {
321
- const h = t[i];
322
- s.push(this.decode(h));
323
- }
324
- return s;
325
- }
326
- decode(t) {
327
- const s = [ 0, 0, 0 ], i = [].indexOf || function(t) {
328
- for (let s = 0, i = this.length; s < i; s++) if (s in this && this[s] === t) return s;
329
- return -1;
330
- };
331
- return i.call(t, "L") >= 0 && (s[0] = 1), i.call(t, "F") >= 0 && (s[1] = 1), i.call(t, "R") >= 0 && (s[2] = 1),
332
- s;
333
- }
334
- flip() {
335
- return this.stateNum += 1;
336
- }
337
- onTick(t) {
338
- return this.time += t, this.time > this.flipInterval ? (this.flip(), this.time -= this.flipInterval) : null;
339
- }
340
- }
341
-
342
- class w {
343
- constructor(t, s) {
344
- this.x = 0, this.y = 0, t && (this.x = t), s && (this.y = s);
345
- }
346
- get length() {
347
- return Math.sqrt(this.x * this.x + this.y * this.y);
348
- }
349
- get direction() {
350
- return Math.atan2(this.y, this.x);
351
- }
352
- get normalized() {
353
- return new w(this.x / this.length, this.y / this.length);
354
- }
355
- add(t) {
356
- return new w(this.x + t.x, this.y + t.y);
357
- }
358
- subtract(t) {
359
- return new w(this.x - t.x, this.y - t.y);
360
- }
361
- mult(t) {
362
- return new w(this.x * t, this.y * t);
363
- }
364
- divide(t) {
365
- return new w(this.x / t, this.y / t);
366
- }
367
- }
368
-
369
- class p {
370
- constructor(t, s, i, h) {
371
- this.x = 0, this.y = 0, this.width = 0, this.height = 0, this.x = t || 0, this.y = s || 0,
372
- this.width = i || 0, this.height = h || 0;
373
- }
374
- static copy(t) {
375
- return new p(t.x, t.y, t.width, t.height);
376
- }
377
- toJSON() {
378
- return Object.assign({}, this);
379
- }
380
- area() {
381
- return this.width * this.height;
382
- }
383
- left(t) {
384
- return void 0 !== t && (this.x = t), this.x;
385
- }
386
- right(t) {
387
- return void 0 !== t && (this.x = t - this.width), this.x + this.width;
388
- }
389
- top(t) {
390
- return void 0 !== t && (this.y = t), this.y;
391
- }
392
- bottom(t) {
393
- return void 0 !== t && (this.y = t - this.height), this.y + this.height;
394
- }
395
- center(t) {
396
- return void 0 !== t && (this.x = t.x - this.width / 2, this.y = t.y - this.height / 2),
397
- new w(this.x + this.width / 2, this.y + this.height / 2);
398
- }
399
- containsPoint(t) {
400
- let s, i;
401
- return this.left() <= (s = t.x) && s <= this.right() && this.top() <= (i = t.y) && i <= this.bottom();
402
- }
403
- containsRect(t) {
404
- return this.left() <= t.left() && t.right() <= this.right() && this.top() <= t.top() && t.bottom() <= this.bottom();
405
- }
406
- getVertices() {
407
- return [ new w(this.left(), this.top()), new w(this.right(), this.top()), new w(this.right(), this.bottom()), new w(this.left(), this.bottom()) ];
408
- }
409
- getSide(t) {
410
- const s = this.getVertices();
411
- return new u(s[t], s[(t + 1) % 4]);
412
- }
413
- getSectorId(t) {
414
- const s = t.subtract(this.center());
415
- if (s.y <= 0 && Math.abs(s.x) <= Math.abs(s.y)) return 0;
416
- if (s.x >= 0 && Math.abs(s.x) >= Math.abs(s.y)) return 1;
417
- if (s.y >= 0 && Math.abs(s.x) <= Math.abs(s.y)) return 2;
418
- if (s.x <= 0 && Math.abs(s.x) >= Math.abs(s.y)) return 3;
419
- throw new Error("algorithm error");
420
- }
421
- getSector(t) {
422
- return this.getSide(this.getSectorId(t));
423
- }
424
- }
425
-
426
- class m {
427
- constructor(t) {
428
- this.id = r("intersection"), this.rect = t, this.roads = [], this.inRoads = [],
429
- this.controlSignals = new g(this);
430
- }
431
- static copy(t) {
432
- t.rect = p.copy(t.rect);
433
- const s = Object.create(m.prototype);
434
- return Object.assign(s, t), s.roads = [], s.inRoads = [], s.controlSignals = new g(s),
435
- s;
436
- }
437
- toJSON() {
438
- return {
439
- id: this.id,
440
- rect: this.rect
441
- };
442
- }
443
- update() {
444
- let t, s, i, h, n;
445
- const e = this.roads;
446
- for (s = 0, h = e.length; s < h; s++) t = e[s], t.update();
447
- const r = this.inRoads, o = [];
448
- for (i = 0, n = r.length; i < n; i++) t = r[i], o.push(t.update());
449
- return o;
450
- }
451
- }
452
-
453
- class v {
454
- constructor(t, s) {
455
- let i, h, n;
456
- if (this.objects = {}, this.factory = t, s && s.objects) for (i in n = s.objects,
457
- n) h = n[i], this.factory instanceof d || (this.objects[i] = this.factory.copy(h));
458
- }
459
- get length() {
460
- return Object.keys(this.objects).length;
461
- }
462
- toJSON() {
463
- return this.objects;
464
- }
465
- get(t) {
466
- return this.objects[t];
467
- }
468
- put(t) {
469
- return this.objects[t.id] = t;
470
- }
471
- pop(t) {
472
- let s;
473
- const i = null !== (s = t.id) ? s : t, h = this.objects[i];
474
- return h instanceof d && h.release(), delete this.objects[i], h;
475
- }
476
- all() {
477
- return this.objects;
478
- }
479
- clear() {
480
- return this.objects = {};
481
- }
482
- }
483
-
484
- class M {
485
- constructor(t, s, i) {
486
- this.leftmostAdjacent = null, this.rightmostAdjacent = null, this.carsPositions = {},
487
- this.sourceSegment = t, this.targetSegment = s, this.road = i, this.update();
488
- }
489
- get sourceSideId() {
490
- return this.road.sourceSideId;
491
- }
492
- get targetSideId() {
493
- return this.road.targetSideId;
494
- }
495
- get isRightmost() {
496
- return this === this.rightmostAdjacent;
497
- }
498
- get isLeftmost() {
499
- return this === this.leftmostAdjacent;
500
- }
501
- get leftBorder() {
502
- return new u(this.sourceSegment.source, this.targetSegment.target);
503
- }
504
- get rightBorder() {
505
- return new u(this.sourceSegment.target, this.targetSegment.source);
506
- }
507
- toJSON() {
508
- const t = Object.assign({}, this);
509
- return delete t.carsPositions, t;
510
- }
511
- update() {
512
- return this.middleLine = new u(this.sourceSegment.center, this.targetSegment.center),
513
- this.length = this.middleLine.length, this.direction = this.middleLine.direction;
514
- }
515
- getTurnDirection(t) {
516
- return t ? this.road.getTurnDirection(t.road) : 1;
517
- }
518
- getDirection() {
519
- return this.direction;
520
- }
521
- getPoint(t) {
522
- return this.middleLine.getPoint(t);
523
- }
524
- addCarPosition(t) {
525
- if (t.id in this.carsPositions) throw Error("car is already here");
526
- return this.carsPositions[t.id] = t;
527
- }
528
- removeCar(t) {
529
- if (!(t.id in this.carsPositions)) throw Error("removing unknown car");
530
- return delete this.carsPositions[t.id];
531
- }
532
- getNext(t) {
533
- if (t.lane !== this) throw Error("car is on other lane");
534
- let s = null, i = 1 / 0;
535
- const h = this.carsPositions;
536
- for (const n in h) {
537
- const e = h[n], r = e.position - t.position;
538
- !e.free && 0 < r && r < i && (i = r, s = e);
539
- }
540
- return s;
541
- }
542
- }
543
-
544
- class b {
545
- constructor(t, s, i) {
546
- this.id = r("road"), this.lanes = [], this.lanesNumber = null, this.source = t,
547
- this.target = s, this.gridSize = i || 32, this.update();
548
- }
549
- get length() {
550
- return this.targetSide.target.subtract(this.sourceSide.source).length;
551
- }
552
- get leftmostLane() {
553
- return this.lanes[this.lanesNumber - 1];
554
- }
555
- get rightmostLane() {
556
- return this.lanes[0];
557
- }
558
- copy(t) {
559
- const s = Object.create(b.prototype);
560
- return Object.assign(s, t), null == s.lanes && (s.lanes = []), s;
561
- }
562
- toJSON() {
563
- return {
564
- id: this.id,
565
- source: this.source.id,
566
- target: this.target.id
567
- };
568
- }
569
- getTurnDirection(t) {
570
- if (this.target !== t.source) throw Error("invalid roads");
571
- const s = this.targetSideId;
572
- return (t.sourceSideId - s - 1 + 8) % 4;
573
- }
574
- update() {
575
- let t, s, i, h, n, e;
576
- if (!this.source || !this.target) throw Error("incomplete road");
577
- this.sourceSideId = this.source.rect.getSectorId(this.target.rect.center()), this.sourceSide = this.source.rect.getSide(this.sourceSideId).subsegment(.5, 1),
578
- this.targetSideId = this.target.rect.getSectorId(this.source.rect.center()), this.targetSide = this.target.rect.getSide(this.targetSideId).subsegment(0, .5),
579
- this.lanesNumber = 0 | Math.min(this.sourceSide.length, this.targetSide.length),
580
- this.lanesNumber = Math.max(2, this.lanesNumber / this.gridSize | 0);
581
- const r = this.sourceSide.split(this.lanesNumber, !0), o = this.targetSide.split(this.lanesNumber);
582
- if (null == this.lanes || this.lanes.length < this.lanesNumber) for (null == this.lanes && (this.lanes = []),
583
- t = i = 0, n = this.lanesNumber - 1; 0 <= n ? i <= n : i >= n; t = 0 <= n ? ++i : --i) null == (s = this.lanes)[t] && (s[t] = new M(r[t], o[t], this));
584
- const u = [];
585
- for (t = h = 0, e = this.lanesNumber - 1; 0 <= e ? h <= e : h >= e; t = 0 <= e ? ++h : --h) this.lanes[t].sourceSegment = r[t],
586
- this.lanes[t].targetSegment = o[t], this.lanes[t].leftAdjacent = this.lanes[t + 1],
587
- this.lanes[t].rightAdjacent = this.lanes[t - 1], this.lanes[t].leftmostAdjacent = this.lanes[this.lanesNumber - 1],
588
- this.lanes[t].rightmostAdjacent = this.lanes[0], u.push(this.lanes[t].update());
589
- return u;
590
- }
591
- }
592
-
593
- class L {
594
- constructor(t) {
595
- this.toRemove = [], this.carsList = {}, this.u = 0, this.l = 0, this.p = new i("traffic"),
596
- this.m = 5, this.v = "stop", this.M = [], this.L = {}, this.options = t || {}, this.carsNumber = this.options.carsNumber || 10,
597
- this.gridSize = this.options.gridSize || 32, this.set();
598
- }
599
- run() {
600
- this.v = "running", this.C();
601
- }
602
- stop() {
603
- this.v = "stop";
604
- }
605
- remove() {
606
- this.N && cancelAnimationFrame(this.N), this.p.remove(), delete this.map;
607
- }
608
- setSymbols(t) {
609
- this.M = t;
610
- for (let t = 0; t < this.M.length; t++) {
611
- const s = this.M[t];
612
- this.L[s.url] = new h([], {
613
- symbol: s
614
- }).addTo(this.p);
615
- }
616
- }
617
- C() {
618
- if ("running" !== this.v) return;
619
- const t = Date.now();
620
- this.u = t;
621
- const s = Date.now();
622
- let i, h = s - this.l || 0;
623
- h > 100 && (h = 100), this.l = s, this.onTick(this.m * h / 1e3);
624
- let n = this.toRemove.length;
625
- for (;n--; ) this.I(this.toRemove[n]);
626
- this.clearTmpRemove();
627
- const e = this.cars.all();
628
- for (i in e) this.R(e[i]);
629
- this.N = window.requestAnimationFrame(this.C.bind(this));
630
- }
631
- S() {
632
- for (const t in this.L) this.L[t].removeData();
633
- for (const t in this.carsList) {
634
- const s = this.carsList[t], i = s.url;
635
- this.L[i].addData(s);
636
- }
637
- }
638
- R(s) {
639
- const i = s.id.substring(3);
640
- if (this.carsList[i]) {
641
- if (!s) return;
642
- const h = s.coords, n = s.direction, e = this.map, r = e.pointAtResToCoordinate(new t.Point(h.x / this.gridSize, h.y / this.gridSize), e.getGLRes()), o = n / Math.PI * 180 + 90;
643
- this.carsList[i].coordinates = r, this.carsList[i].rotation = [ 0, 0, o ];
644
- const u = this.T(i), l = this.carsList[i].url;
645
- u > -1 ? this.L[l].updateData(u, this.carsList[i]) : this.L[l].addData(this.carsList[i]);
646
- } else {
647
- const t = this.M[Math.floor(Math.random() * this.M.length)];
648
- this.carsList[i] = {
649
- coordinates: null,
650
- direction: null,
651
- url: t.url
652
- }, this.showHideCar(this.carsList[i], "show");
653
- }
654
- }
655
- showHideCar(s, i, h) {
656
- const n = this;
657
- t.animation.Animation.animate({
658
- symbol: {
659
- transparent: 1
660
- }
661
- }, {
662
- duration: 1e3,
663
- easing: "out"
664
- }, (function(t) {
665
- if ("running" === t.state.playState) {
666
- let e = 0;
667
- "show" === i ? (e = t.styles.symbol.transparent, s.color = [ 1, 1, 1, e ]) : "hide" === i && (e = 1 - t.styles.symbol.transparent,
668
- s.color = [ 1, 1, 1, e ], e < .01 && setTimeout((function() {
669
- const t = n.T(h);
670
- if (!n.carsList[h]) return;
671
- const s = n.carsList[h].url;
672
- t > -1 && n.L[s].removeData(t), delete n.carsList[h];
673
- }), 100));
674
- }
675
- })).play();
676
- }
677
- T(t) {
678
- const s = this.carsList[t];
679
- if (!s) return -1;
680
- const i = s.url;
681
- return this.L[i].getAllData().indexOf(this.carsList[t]);
682
- }
683
- I(t) {
684
- const s = t.substring(3);
685
- null != this.carsList[s] && this.showHideCar(this.carsList[s], "hide", s);
686
- }
687
- get instantSpeed() {
688
- const t = Object.values(this.cars.all()).map((t => t.speed));
689
- return 0 === t.length ? 0 : t.reduce((function(t, s) {
690
- return t + s;
691
- })) / t.length;
692
- }
693
- get map() {
694
- const t = this.j;
695
- return t ? t.getMap() : null;
696
- }
697
- addTo(t) {
698
- this.j = t, this.p.addTo(t);
699
- }
700
- set(t) {
701
- return null == t && (t = {}), this.intersections = new v(m, t.intersections), this.roads = new v(b, t.roads),
702
- this.cars = new v(d, t.cars), this.carsNumber = 0;
703
- }
704
- save() {
705
- const t = Object.assign({}, this);
706
- return delete t.cars, window.localStorage.world = JSON.stringify(t);
707
- }
708
- setCarNumber(t) {
709
- this.carsNumber = t;
710
- }
711
- getCarNumber() {
712
- return this.carsNumber;
713
- }
714
- load() {
715
- let t, s, i, h;
716
- if (t = window.localStorage.world, t = t && JSON.parse(t), null == t) return null;
717
- this.clear(), this.carsNumber = t.carsNumber || 0;
718
- const n = t.intersections;
719
- for (s in n) i = n[s], this.addIntersection(m.copy(i));
720
- const e = t.roads, r = [];
721
- for (s in e) h = e[s], h = (new b).copy(h), h.source = this.getIntersection(h.source),
722
- h.target = this.getIntersection(h.target), r.push(this.addRoad(h));
723
- return r;
724
- }
725
- intersectSegment(t, i) {
726
- for (let h = 0; h < i.length - 1; h++) {
727
- const n = [ i[h], i[h + 1] ], e = s.lineString(n), r = s.lineIntersect(t, e);
728
- if (r.features.length) return {
729
- intersectIndex: h,
730
- coord: r.features[0].geometry.coordinates
731
- };
732
- }
733
- return null;
734
- }
735
- intersection(t, i, h, n, e, r) {
736
- const o = s.lineString(t);
737
- for (let t = i + 1; t < n.length; t++) {
738
- const s = n[t], u = this.intersectSegment(o, s);
739
- if (!u) continue;
740
- const l = {};
741
- l[i] = {
742
- pre: h
743
- }, l[t] = {
744
- pre: u.intersectIndex + r[t]
745
- };
746
- const c = {
747
- inLines: l,
748
- coordinate: this.map.coordinateToPointAtRes({
749
- x: u.coord[0],
750
- y: u.coord[1]
751
- }, this.map.getGLRes())
752
- };
753
- e.push(c);
754
- }
755
- }
756
- generateTraffic(t) {
757
- if (!this.map) return;
758
- const s = {};
759
- let i = 0;
760
- for (let h = 0; h < t.length; h++) s[h] = i, i += t[h].length;
761
- const h = [];
762
- let n = 0;
763
- for (let i = 0; i < t.length; i++) {
764
- const e = t[i];
765
- for (let r = 0; r < e.length - 1; r++) {
766
- const o = [ e[r], e[r + 1] ];
767
- this.intersection(o, i, n + r, t, h, s);
768
- }
769
- n += e.length;
770
- }
771
- const e = [];
772
- let r = 0;
773
- const o = this.map;
774
- for (let s = 0; s < t.length; s++) {
775
- const i = t[s];
776
- for (let t = 0; t < i.length; t++) {
777
- const h = o.coordinateToPointAtRes({
778
- x: i[t][0],
779
- y: i[t][1]
780
- }, o.getGLRes()), n = {};
781
- 0 === t ? n[s] = {
782
- pre: null
783
- } : (i.length, n[s] = {
784
- pre: t - 1 + r
785
- });
786
- const u = {
787
- inLines: n,
788
- coordinate: h
789
- };
790
- e.push(u);
791
- }
792
- r += i.length;
793
- }
794
- for (let t = 0; t < e.length; t++) {
795
- const s = e[t];
796
- this.comparePrePoint(s, t, h);
797
- }
798
- const u = this.gridSize, l = [];
799
- for (let t = 0; t < e.length; t++) {
800
- const s = e[t], i = s.coordinate.x, h = s.coordinate.y, n = new p(i * u - .5, h * u - .5, 1, 1), r = new m(n);
801
- r.connectRoads = s.roads, l.push(r), this.addIntersection(r);
802
- }
803
- const c = [];
804
- for (let t = 0; t < h.length; t++) {
805
- const s = h[t], i = s.coordinate.x, n = s.coordinate.y, e = new p(i * u - .5, n * u - .5, 1, 1), r = new m(e);
806
- r.connectRoads = s.roads, c.push(r), this.addIntersection(r);
807
- }
808
- for (let t = 0; t < l.length; t++) {
809
- const s = l[t], i = s.connectRoads;
810
- if (i) for (let t = 0; t < i.length; t++) {
811
- const h = i[t];
812
- if (null != h.pre) {
813
- let t = null;
814
- const i = h.type;
815
- "line" === i ? t = l[h.pre] : "line-interact" === i && (t = c[h.pre]), this.addRoad(new b(s, t)),
816
- this.addRoad(new b(t, s));
817
- }
818
- }
819
- }
820
- for (let t = 0; t < c.length; t++) {
821
- const s = c[t], i = s.connectRoads;
822
- if (i) for (let t = 0; t < i.length; t++) {
823
- const h = i[t];
824
- if (null != h.pre) {
825
- let t = null;
826
- const i = h.type;
827
- "line" === i ? t = l[h.pre] : "line-interact" === i && (t = c[h.pre]), this.addRoad(new b(s, t)),
828
- this.addRoad(new b(t, s));
829
- }
830
- }
831
- }
832
- }
833
- comparePrePoint(t, s, i) {
834
- const h = this.getPre(t);
835
- if (h) {
836
- const {pre: n, lineIndex: e} = h;
837
- for (let h = 0; h < i.length; h++) {
838
- const r = i[h];
839
- r.inLines[e] && r.inLines[e].pre === n && (t.roads = [ {
840
- pre: h,
841
- type: "line-interact"
842
- } ], r.roads ? r.roads.push({
843
- pre: s - 1,
844
- type: "line"
845
- }) : r.roads = [ {
846
- pre: s - 1,
847
- type: "line"
848
- } ]);
849
- }
850
- t.roads || (t.roads = [ {
851
- pre: s - 1,
852
- type: "line"
853
- } ]);
854
- }
855
- }
856
- getPre(t) {
857
- for (const s in t.inLines) if (null !== t.inLines[s].pre) return {
858
- pre: t.inLines[s].pre,
859
- lineIndex: s
860
- };
861
- return null;
862
- }
863
- clear() {
864
- return this.set({});
865
- }
866
- onTick(t) {
867
- let s, i, h;
868
- if (t > 1) throw Error("delta > 1");
869
- this.refreshCars();
870
- const n = this.intersections.all();
871
- for (i in n) h = n[i], h.controlSignals.onTick(t);
872
- const e = this.cars.all(), r = [];
873
- for (i in e) s = e[i], s.move(t), s.alive ? r.push(void 0) : r.push(this.removeCar(s));
874
- return r;
875
- }
876
- refreshCars() {
877
- return this.cars.length < this.carsNumber && this.addRandomCar(), this.cars.length > this.carsNumber ? this.removeRandomCar() : null;
878
- }
879
- addRoad(t) {
880
- return this.roads.put(t), t.source.roads.push(t), t.target.inRoads.push(t), t.update();
881
- }
882
- getRoad(t) {
883
- return this.roads.get(t);
884
- }
885
- addCar(t) {
886
- return this.cars.put(t);
887
- }
888
- getCar(t) {
889
- return this.cars.get(t);
890
- }
891
- removeCar(t) {
892
- return this.toRemove.push(t.id), this.cars.pop(t);
893
- }
894
- clearTmpRemove() {
895
- this.toRemove = [];
896
- }
897
- addIntersection(t) {
898
- return this.intersections.put(t);
899
- }
900
- getIntersection(t) {
901
- return this.intersections.get(t);
902
- }
903
- addRandomCar() {
904
- let t;
905
- const s = e(this.roads.all());
906
- return null != s && (t = e(s.lanes), null != t) ? this.addCar(new d(t)) : null;
907
- }
908
- removeRandomCar() {
909
- const t = e(this.cars.all());
910
- return null != t ? this.removeCar(t) : null;
911
- }
912
- }
913
-
914
- "undefined" != typeof window && window.maptalks && (window.maptalks.TrafficScene = L);
915
-
916
- export { L as TrafficScene };
917
-
918
- "undefined" != typeof console && console.log("@maptalks/traffic v0.98.6");
919
- //# sourceMappingURL=maptalks.traffic.es.js.map