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