Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MSEdge.cpp
15 : /// @author Christian Roessel
16 : /// @author Jakob Erdmann
17 : /// @author Christoph Sommer
18 : /// @author Daniel Krajzewicz
19 : /// @author Laura Bieker
20 : /// @author Michael Behrisch
21 : /// @author Sascha Krieg
22 : /// @date Tue, 06 Mar 2001
23 : ///
24 : // A road/street connecting two junctions
25 : /****************************************************************************/
26 : #include <config.h>
27 :
28 : #include <algorithm>
29 : #include <iostream>
30 : #include <cassert>
31 : #ifdef HAVE_FOX
32 : #include <utils/common/ScopedLocker.h>
33 : #endif
34 : #include <utils/common/StringTokenizer.h>
35 : #include <utils/options/OptionsCont.h>
36 : #include <microsim/devices/MSRoutingEngine.h>
37 : #include <mesosim/MELoop.h>
38 : #include <mesosim/MESegment.h>
39 : #include <mesosim/MEVehicle.h>
40 : #include "MSInsertionControl.h"
41 : #include "MSJunction.h"
42 : #include "MSLane.h"
43 : #include "MSLaneChanger.h"
44 : #include "MSLaneChangerSublane.h"
45 : #include "MSLink.h"
46 : #include "MSGlobals.h"
47 : #include "MSNet.h"
48 : #include "MSVehicle.h"
49 : #include "MSLeaderInfo.h"
50 : #include <microsim/transportables/MSTransportable.h>
51 : #include "MSEdgeWeightsStorage.h"
52 : #include "MSEdge.h"
53 :
54 : #define BEST_LANE_LOOKAHEAD 3000.0
55 :
56 : // ===========================================================================
57 : // static member definitions
58 : // ===========================================================================
59 : MSEdge::DictType MSEdge::myDict;
60 : MSEdgeVector MSEdge::myEdges;
61 : SVCPermissions MSEdge::myMesoIgnoredVClasses(0);
62 : DepartLaneDefinition MSEdge::myDefaultDepartLaneDefinition(DepartLaneDefinition::DEFAULT);
63 : int MSEdge::myDefaultDepartLane(0);
64 :
65 : // ===========================================================================
66 : // member method definitions
67 : // ===========================================================================
68 1998086 : MSEdge::MSEdge(const std::string& id, int numericalID,
69 : const SumoXMLEdgeFunc function,
70 : const std::string& streetName,
71 : const std::string& edgeType,
72 : const std::string& routingType,
73 : int priority,
74 1998086 : double distance) :
75 1998086 : Named(id), myNumericalID(numericalID), myLanes(nullptr),
76 1998086 : myLaneChanger(nullptr), myFunction(function), myVaporizationRequests(0),
77 1998086 : myLastFailedInsertionTime(-1),
78 1998086 : myFromJunction(nullptr), myToJunction(nullptr),
79 1998086 : myHaveTransientPermissions(false),
80 1998086 : myOtherTazConnector(nullptr),
81 1998086 : myStreetName(streetName),
82 1998086 : myEdgeType(edgeType),
83 1998086 : myRoutingType(routingType),
84 1998086 : myPriority(priority),
85 1998086 : myDistance(distance),
86 1998086 : myWidth(0.),
87 1998086 : myLength(0.),
88 1998086 : myEmptyTraveltime(0.),
89 1998086 : myTimePenalty(0.),
90 1998086 : myAmDelayed(false),
91 1998086 : myAmRoundabout(false),
92 1998086 : myAmFringe(true),
93 3996172 : myBidiEdge(nullptr)
94 1998086 : { }
95 :
96 :
97 3441891 : MSEdge::~MSEdge() {
98 1983258 : delete myLaneChanger;
99 1983258 : delete myReversedRoutingEdge;
100 1983258 : delete myRailwayRoutingEdge;
101 13358181 : }
102 :
103 :
104 : void
105 1861242 : MSEdge::initialize(const std::vector<MSLane*>* lanes) {
106 : assert(lanes != 0);
107 1861242 : myLanes = std::shared_ptr<const std::vector<MSLane*> >(lanes);
108 1861242 : if (myFunction == SumoXMLEdgeFunc::CONNECTOR) {
109 70008 : myCombinedPermissions = SVCAll;
110 : }
111 4015157 : for (MSLane* const lane : *lanes) {
112 2153915 : lane->setRightSideOnEdge(myWidth, (int)mySublaneSides.size());
113 2153915 : MSLeaderInfo ahead(lane->getWidth());
114 5025762 : for (int j = 0; j < ahead.numSublanes(); ++j) {
115 2871847 : mySublaneSides.push_back(myWidth + j * MSGlobals::gLateralResolution);
116 : }
117 2153915 : myWidth += lane->getWidth();
118 2153915 : }
119 1861242 : }
120 :
121 :
122 1911531 : void MSEdge::recalcCache() {
123 1911531 : if (myLanes->empty()) {
124 : return;
125 : }
126 1911531 : myLength = myLanes->front()->getLength();
127 3823062 : myEmptyTraveltime = myLength / MAX2(getSpeedLimit(), NUMERICAL_EPS);
128 1911531 : if (isNormal() && (MSGlobals::gUseMesoSim || MSGlobals::gTLSPenalty > 0)) {
129 : SUMOTime minorPenalty = 0;
130 196134 : bool haveTLSPenalty = MSGlobals::gTLSPenalty > 0;
131 196134 : if (MSGlobals::gUseMesoSim) {
132 196019 : const MESegment::MesoEdgeType& edgeType = MSNet::getInstance()->getMesoType(getEdgeType());
133 196019 : minorPenalty = edgeType.minorPenalty;
134 196019 : haveTLSPenalty = edgeType.tlsPenalty > 0;
135 : }
136 196134 : if (haveTLSPenalty || minorPenalty > 0) {
137 : // add tls penalties to the minimum travel time
138 : SUMOTime minPenalty = -1;
139 2555 : for (const MSLane* const l : *myLanes) {
140 4099 : for (const MSLink* const link : l->getLinkCont()) {
141 2387 : if (link->getLane()->isWalkingArea() && link->getLaneBefore()->isNormal()) {
142 60 : continue;
143 : }
144 2327 : SUMOTime linkPenalty = link->isTLSControlled() ? link->getMesoTLSPenalty() : (link->havePriority() ? 0 : minorPenalty);
145 2327 : if (minPenalty == -1) {
146 : minPenalty = linkPenalty;
147 : } else {
148 : minPenalty = MIN2(minPenalty, linkPenalty);
149 : }
150 : }
151 : }
152 843 : if (minPenalty > 0) {
153 285 : myEmptyTraveltime += STEPS2TIME(minPenalty);
154 285 : myTimePenalty = STEPS2TIME(minPenalty);
155 : }
156 : }
157 1715397 : } else if (isCrossing() && MSGlobals::gTLSPenalty > 0) {
158 : // penalties are recorded for the entering link
159 112 : for (const auto& ili : myLanes->front()->getIncomingLanes()) {
160 56 : double penalty = STEPS2TIME(ili.viaLink->getMesoTLSPenalty());
161 56 : if (!ili.viaLink->haveOffPriority()) {
162 0 : penalty = MAX2(penalty, MSGlobals::gMinorPenalty);
163 : }
164 56 : if (penalty > 0) {
165 20 : myEmptyTraveltime += penalty;
166 20 : myTimePenalty = penalty;
167 : }
168 : }
169 1715341 : } else if (isInternal() && MSGlobals::gUsingInternalLanes) {
170 1010727 : const MSLink* link = myLanes->front()->getIncomingLanes()[0].viaLink;
171 1010727 : if (!link->isTLSControlled() && !link->havePriority()) {
172 496737 : if (link->isTurnaround()) {
173 189432 : myEmptyTraveltime += MSGlobals::gTurnaroundPenalty;
174 189432 : myTimePenalty = MSGlobals::gTurnaroundPenalty;
175 : } else {
176 307305 : myEmptyTraveltime += MSGlobals::gMinorPenalty;
177 307305 : myTimePenalty = MSGlobals::gMinorPenalty;
178 : }
179 : }
180 : }
181 : }
182 :
183 :
184 : void
185 42 : MSEdge::resetTAZ(MSJunction* junction) {
186 : mySuccessors.clear();
187 : myPredecessors.clear();
188 728 : for (const MSEdge* edge : junction->getIncoming()) {
189 686 : if (!edge->isInternal()) {
190 126 : MSEdgeVector& succ = const_cast<MSEdgeVector&>(edge->mySuccessors);
191 : MSConstEdgePairVector& succVia = const_cast<MSConstEdgePairVector&>(edge->myViaSuccessors);
192 126 : MSEdgeVector& pred = const_cast<MSEdgeVector&>(edge->myPredecessors);
193 126 : auto it = std::find(succ.begin(), succ.end(), this);
194 126 : auto it2 = std::find(succVia.begin(), succVia.end(), std::make_pair(const_cast<const MSEdge*>(this), (const MSEdge*)nullptr));
195 126 : auto it3 = std::find(pred.begin(), pred.end(), this);
196 126 : if (it != succ.end()) {
197 : succ.erase(it);
198 : succVia.erase(it2);
199 : }
200 126 : if (it3 != pred.end()) {
201 : pred.erase(it3);
202 : }
203 : }
204 : }
205 42 : }
206 :
207 : void
208 1788704 : MSEdge::closeBuilding() {
209 3939666 : for (MSLane* const lane : *myLanes) {
210 5109418 : for (MSLink* const link : lane->getLinkCont()) {
211 2958456 : link->initParallelLinks();
212 : MSLane* const toL = link->getLane();
213 : MSLane* const viaL = link->getViaLane();
214 2958456 : if (toL != nullptr) {
215 : MSEdge& to = toL->getEdge();
216 2958456 : if (std::find(mySuccessors.begin(), mySuccessors.end(), &to) == mySuccessors.end()) {
217 2757260 : mySuccessors.push_back(&to);
218 5514520 : myViaSuccessors.push_back(std::make_pair(&to, (viaL == nullptr ? nullptr : &viaL->getEdge())));
219 : }
220 2958456 : if (std::find(to.myPredecessors.begin(), to.myPredecessors.end(), this) == to.myPredecessors.end()) {
221 2757260 : to.myPredecessors.push_back(this);
222 : }
223 2958456 : if (link->getDirection() != LinkDirection::TURN) {
224 2306306 : myAmFringe = false;
225 : }
226 : }
227 2958456 : if (viaL != nullptr) {
228 : MSEdge& to = viaL->getEdge();
229 1034962 : if (std::find(to.myPredecessors.begin(), to.myPredecessors.end(), this) == to.myPredecessors.end()) {
230 953814 : to.myPredecessors.push_back(this);
231 : }
232 : }
233 : }
234 2150962 : lane->checkBufferType();
235 : }
236 1788704 : std::sort(mySuccessors.begin(), mySuccessors.end(), by_id_sorter());
237 1788704 : rebuildAllowedLanes(true);
238 1788704 : recalcCache();
239 :
240 : // extend lookup table for sublane model after all edges are read
241 1788704 : if (myLanes->back()->getOpposite() != nullptr) {
242 8356 : MSLane* opposite = myLanes->back()->getOpposite();
243 8356 : MSLeaderInfo ahead(opposite->getWidth());
244 22778 : for (int j = 0; j < ahead.numSublanes(); ++j) {
245 14422 : mySublaneSides.push_back(myWidth + j * MSGlobals::gLateralResolution);
246 : }
247 8356 : }
248 1788704 : }
249 :
250 :
251 : void
252 1788696 : MSEdge::postLoadInitLaneChanger() {
253 1788696 : if (myLaneChanger != nullptr) {
254 445167 : myLaneChanger->postloadInitLC();
255 : }
256 1788696 : }
257 :
258 : void
259 1788704 : MSEdge::buildLaneChanger() {
260 1788704 : if (!myLanes->empty()) {
261 1788704 : const bool allowChanging = allowsLaneChanging();
262 1788704 : if (MSGlobals::gLateralResolution > 0) {
263 : // may always initiate sublane-change
264 184758 : if (!isInternal() || MSGlobals::gUsingInternalLanes) {
265 184567 : myLaneChanger = new MSLaneChangerSublane(myLanes.get(), allowChanging);
266 : }
267 : } else {
268 1603946 : if (MSGlobals::gLaneChangeDuration > 0) {
269 4981 : myLaneChanger = new MSLaneChanger(myLanes.get(), allowChanging);
270 1598965 : } else if (myLanes->size() > 1 || canChangeToOpposite()) {
271 255627 : myLaneChanger = new MSLaneChanger(myLanes.get(), allowChanging);
272 : }
273 : }
274 : }
275 1788704 : }
276 :
277 :
278 : bool
279 1788704 : MSEdge::allowsLaneChanging() const {
280 1788704 : if (isInternal() && MSGlobals::gUsingInternalLanes) {
281 : // allow changing only if all links leading to this internal lane have priority
282 : // or they are controlled by a traffic light
283 1506252 : for (const MSLane* const lane : *myLanes) {
284 1031521 : const MSLink* const link = lane->getLogicalPredecessorLane()->getLinkTo(lane);
285 : assert(link != nullptr);
286 : const LinkState state = link->getState();
287 468406 : if ((state == LINKSTATE_MINOR && lane->getBidiLane() == nullptr)
288 563541 : || state == LINKSTATE_EQUAL
289 563541 : || state == LINKSTATE_STOP
290 : || state == LINKSTATE_ALLWAY_STOP
291 1031521 : || state == LINKSTATE_DEADEND) {
292 : return false;
293 : }
294 : }
295 : }
296 : return true;
297 : }
298 :
299 :
300 : void
301 18944703 : MSEdge::addToAllowed(const SVCPermissions permissions, std::shared_ptr<const std::vector<MSLane*> > allowedLanes, AllowedLanesCont& laneCont) const {
302 18944703 : if (!allowedLanes->empty()) {
303 : // recheck whether we had this list to save memory
304 18675553 : for (auto& allowed : laneCont) {
305 17632865 : if (*allowed.second == *allowedLanes) {
306 13281504 : allowed.first |= permissions;
307 : return;
308 : }
309 : }
310 1042688 : laneCont.push_back(std::make_pair(permissions, allowedLanes));
311 : }
312 : }
313 :
314 :
315 : SVCPermissions
316 3005044 : MSEdge::getMesoPermissions(SVCPermissions p, SVCPermissions ignoreIgnored) {
317 3005044 : SVCPermissions ignored = myMesoIgnoredVClasses & ~ignoreIgnored;
318 3005044 : return (p | ignored) == ignored ? 0 : p;
319 : }
320 :
321 :
322 : void
323 1790333 : MSEdge::rebuildAllowedLanes(const bool onInit, bool updateVehicles) {
324 : // rebuild myMinimumPermissions and myCombinedPermissions
325 1790333 : myMinimumPermissions = SVCAll;
326 1790333 : myCombinedPermissions = 0;
327 : bool lanesChangedPermission = false;
328 3943444 : for (MSLane* const lane : *myLanes) {
329 : // same dedicated lanes are ignored in meso to avoid capacity errors.
330 : // Here we have to make sure that vehicles which are set to depart on
331 : // such lanes trigger an error.
332 2153111 : SVCPermissions allow = getMesoPermissions(lane->getPermissions(), SVC_PEDESTRIAN);
333 2153111 : myMinimumPermissions &= allow;
334 2153111 : myCombinedPermissions |= allow;
335 2153111 : lanesChangedPermission |= lane->hadPermissionChanges();
336 : }
337 1790333 : if (!onInit && !myHaveTransientPermissions && lanesChangedPermission) {
338 1149 : myHaveTransientPermissions = true;
339 : // backup original structures when first needed
340 1149 : myOrigAllowed = myAllowed;
341 : myOrigAllowedTargets = myAllowedTargets;
342 : myOrigClassesViaSuccessorMap = myClassesViaSuccessorMap;
343 : }
344 : // rebuild myAllowed
345 : myAllowed.clear();
346 1790333 : if (myCombinedPermissions != myMinimumPermissions) {
347 169949 : myAllowed.push_back(std::make_pair(SVC_IGNORING, myLanes));
348 5778266 : for (SVCPermissions vclass = SVC_PRIVATE; vclass <= SUMOVehicleClass_MAX; vclass *= 2) {
349 5608317 : if ((myCombinedPermissions & vclass) == vclass) {
350 : std::shared_ptr<std::vector<MSLane*> > allowedLanes = std::make_shared<std::vector<MSLane*> >();
351 11915768 : for (MSLane* const lane : *myLanes) {
352 8066621 : if (lane->allowsVehicleClass((SUMOVehicleClass)vclass)) {
353 4071386 : allowedLanes->push_back(lane);
354 : }
355 : }
356 7698294 : addToAllowed(vclass, allowedLanes, myAllowed);
357 : }
358 : }
359 : }
360 1790333 : if (onInit) {
361 1788704 : myOriginalMinimumPermissions = myMinimumPermissions;
362 1788704 : myOriginalCombinedPermissions = myCombinedPermissions;
363 : } else {
364 1629 : rebuildAllowedTargets(updateVehicles);
365 4137 : for (MSEdge* pred : myPredecessors) {
366 2508 : if (myHaveTransientPermissions && !pred->myHaveTransientPermissions) {
367 1593 : pred->myOrigAllowed = pred->myAllowed;
368 : pred->myOrigAllowedTargets = pred->myAllowedTargets;
369 : pred->myOrigClassesViaSuccessorMap = pred->myClassesViaSuccessorMap;
370 1593 : pred->myHaveTransientPermissions = true;
371 : }
372 2508 : pred->rebuildAllowedTargets(updateVehicles);
373 : }
374 1629 : if (MSGlobals::gUseMesoSim) {
375 2897 : for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*this); s != nullptr; s = s->getNextSegment()) {
376 2467 : s->updatePermissions();
377 : }
378 : }
379 : }
380 1790333 : }
381 :
382 :
383 : void
384 1793979 : MSEdge::rebuildAllowedTargets(const bool updateVehicles) {
385 : myAllowedTargets.clear();
386 4559187 : for (const MSEdge* target : mySuccessors) {
387 : bool universalMap = true; // whether the mapping for SVC_IGNORING is also valid for all vehicle classes
388 : std::shared_ptr<std::vector<MSLane*> > allLanes = std::make_shared<std::vector<MSLane*> >();
389 : // compute the mapping for SVC_IGNORING
390 6302078 : for (MSLane* const lane : *myLanes) {
391 : SVCPermissions combinedTargetPermissions = 0;
392 10048744 : for (const MSLink* const link : lane->getLinkCont()) {
393 6511874 : if (&link->getLane()->getEdge() == target) {
394 2967479 : allLanes->push_back(lane);
395 2967479 : combinedTargetPermissions |= link->getLane()->getPermissions();
396 2967479 : if (link->getViaLane() != nullptr &&
397 1037619 : ((lane->getPermissions() & link->getLane()->getPermissions()) != link->getViaLane()->getPermissions())) {
398 : // custom connection permissions
399 : universalMap = false;
400 : }
401 : }
402 : }
403 3536870 : if (combinedTargetPermissions == 0 || (lane->getPermissions() & combinedTargetPermissions) != lane->getPermissions()) {
404 : universalMap = false;
405 : }
406 : }
407 2765208 : if (universalMap) {
408 2249620 : if (myAllowed.empty()) {
409 : // we have no lane specific permissions
410 4430832 : myAllowedTargets[target].push_back(std::make_pair(myMinimumPermissions, myLanes));
411 : } else {
412 134573 : for (const auto& i : myAllowed) {
413 301107 : addToAllowed(i.first, i.second, myAllowedTargets[target]);
414 : }
415 : }
416 : } else {
417 1031176 : addToAllowed(SVC_IGNORING, allLanes, myAllowedTargets[target]);
418 : // compute the vclass specific mapping
419 17529992 : for (SVCPermissions vclass = SVC_PRIVATE; vclass <= SUMOVehicleClass_MAX; vclass *= 2) {
420 17014404 : if ((myCombinedPermissions & vclass) == vclass) {
421 : std::shared_ptr<std::vector<MSLane*> > allowedLanes = std::make_shared<std::vector<MSLane*> >();
422 45019290 : for (MSLane* const lane : *myLanes) {
423 30539691 : if (lane->allowsVehicleClass((SUMOVehicleClass)vclass)) {
424 58544220 : for (const MSLink* const link : lane->getLinkCont()) {
425 38296139 : if (link->getLane()->allowsVehicleClass((SUMOVehicleClass)vclass) && &link->getLane()->getEdge() == target && (link->getViaLane() == nullptr || link->getViaLane()->allowsVehicleClass((SUMOVehicleClass)vclass))) {
426 10219851 : allowedLanes->push_back(lane);
427 : }
428 : }
429 : }
430 : }
431 43438797 : addToAllowed(vclass, allowedLanes, myAllowedTargets[target]);
432 : }
433 : }
434 : }
435 : }
436 1793979 : if (updateVehicles) {
437 3289 : for (const MSLane* const lane : *myLanes) {
438 1908 : const MSLane::VehCont& vehs = lane->getVehiclesSecure();
439 4339 : for (MSVehicle* veh : vehs) {
440 2431 : veh->updateBestLanes(true);
441 : }
442 1908 : lane->releaseVehicles();
443 : }
444 : }
445 : myClassesSuccessorMap.clear();
446 1793979 : }
447 :
448 :
449 : // ------------ Access to the edge's lanes
450 : MSLane*
451 774 : MSEdge::leftLane(const MSLane* const lane) const {
452 774 : return parallelLane(lane, 1);
453 : }
454 :
455 :
456 : MSLane*
457 400 : MSEdge::rightLane(const MSLane* const lane) const {
458 400 : return parallelLane(lane, -1);
459 : }
460 :
461 :
462 : MSLane*
463 85161403 : MSEdge::parallelLane(const MSLane* const lane, int offset, bool includeOpposite) const {
464 85161403 : const int resultIndex = lane->getIndex() + offset;
465 85161403 : if (resultIndex >= getNumLanes() && includeOpposite) {
466 20762090 : const MSEdge* opposite = getOppositeEdge();
467 20762090 : if (opposite != nullptr && resultIndex < getNumLanes() + opposite->getNumLanes()) {
468 1388240 : return opposite->getLanes()[opposite->getNumLanes() + getNumLanes() - resultIndex - 1];
469 : }
470 : return nullptr;
471 64399313 : } else if (resultIndex >= (int)myLanes->size() || resultIndex < 0) {
472 : return nullptr;
473 : } else {
474 39179584 : return (*myLanes)[resultIndex];
475 : }
476 : }
477 :
478 :
479 : const std::vector<MSLane*>*
480 77108991 : MSEdge::allowedLanes(const MSEdge& destination, SUMOVehicleClass vclass, bool ignoreTransientPermissions) const {
481 77108991 : const auto& targets = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigAllowedTargets : myAllowedTargets;
482 : AllowedLanesByTarget::const_iterator i = targets.find(&destination);
483 77108991 : if (i != targets.end()) {
484 77087484 : for (const auto& allowed : i->second) {
485 76962193 : if ((allowed.first & vclass) == vclass) {
486 : return allowed.second.get();
487 : }
488 : }
489 : }
490 : return nullptr;
491 : }
492 :
493 :
494 : const std::vector<MSLane*>*
495 274802157 : MSEdge::allowedLanes(SUMOVehicleClass vclass) const {
496 274802157 : if ((myMinimumPermissions & vclass) == vclass) {
497 43852019 : return myLanes.get();
498 : } else {
499 230950138 : if ((myCombinedPermissions & vclass) == vclass) {
500 461896785 : for (const auto& allowed : myAllowed) {
501 461896785 : if ((allowed.first & vclass) == vclass) {
502 : return allowed.second.get();
503 : }
504 : }
505 : }
506 2007 : return nullptr;
507 : }
508 : }
509 :
510 :
511 : const std::vector<MSLane*>*
512 2175815696 : MSEdge::allowedLanes(SUMOVehicleClass vclass, bool ignoreTransientPermissions) const {
513 2175815696 : const SVCPermissions& minP = ignoreTransientPermissions ? myOriginalMinimumPermissions : myMinimumPermissions;
514 2175815696 : if ((minP & vclass) == vclass) {
515 638638742 : return myLanes.get();
516 : } else {
517 1537176954 : const SVCPermissions comP = ignoreTransientPermissions ? myOriginalCombinedPermissions : myCombinedPermissions;
518 1537176954 : if ((comP & vclass) == vclass) {
519 1537176882 : const AllowedLanesCont& allowedCont = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigAllowed : myAllowed;
520 3080051901 : for (const auto& allowed : allowedCont) {
521 3080051901 : if ((allowed.first & vclass) == vclass) {
522 : return allowed.second.get();
523 : }
524 : }
525 : }
526 72 : return nullptr;
527 : }
528 : }
529 :
530 :
531 : // ------------
532 : SUMOTime
533 433 : MSEdge::incVaporization(SUMOTime) {
534 433 : ++myVaporizationRequests;
535 433 : return 0;
536 : }
537 :
538 :
539 : SUMOTime
540 279 : MSEdge::decVaporization(SUMOTime) {
541 279 : --myVaporizationRequests;
542 279 : return 0;
543 : }
544 :
545 :
546 : MSLane*
547 217893154 : MSEdge::getFreeLane(const std::vector<MSLane*>* allowed, const SUMOVehicleClass vclass, double departPos) const {
548 217893154 : if (allowed == nullptr) {
549 184249189 : allowed = allowedLanes(vclass);
550 : }
551 : MSLane* res = nullptr;
552 184249189 : if (allowed != nullptr) {
553 : double largestGap = 0;
554 : MSLane* resByGap = nullptr;
555 : double leastOccupancy = std::numeric_limits<double>::max();
556 460697625 : for (std::vector<MSLane*>::const_iterator i = allowed->begin(); i != allowed->end(); ++i) {
557 242806470 : const double occupancy = (*i)->getBruttoOccupancy();
558 242806470 : if (occupancy < leastOccupancy) {
559 225986957 : res = (*i);
560 : leastOccupancy = occupancy;
561 : }
562 242806470 : const MSVehicle* last = (*i)->getLastFullVehicle();
563 242806470 : const double lastGap = (last != nullptr ? last->getPositionOnLane() : myLength) - departPos;
564 242806470 : if (lastGap > largestGap) {
565 : largestGap = lastGap;
566 58368628 : resByGap = (*i);
567 : }
568 : }
569 217891155 : if (resByGap != nullptr) {
570 : //if (res != resByGap) std::cout << SIMTIME << " edge=" << getID() << " departPos=" << departPos << " res=" << Named::getIDSecure(res) << " resByGap=" << Named::getIDSecure(resByGap) << " largestGap=" << largestGap << "\n";
571 : res = resByGap;
572 : }
573 : }
574 217893154 : return res;
575 : }
576 :
577 :
578 : MSLane*
579 839407685 : MSEdge::getProbableLane(const std::vector<MSLane*>* allowed, const SUMOVehicleClass vclass, double departPos, double maxSpeed) const {
580 839407685 : if (allowed == nullptr) {
581 0 : allowed = allowedLanes(vclass);
582 : }
583 : MSLane* res = nullptr;
584 0 : if (allowed != nullptr) {
585 : double largestGap = 0;
586 : double largestSpeed = 0;
587 : MSLane* resByGap = nullptr;
588 : double leastOccupancy = std::numeric_limits<double>::max();
589 : int aIndex = 0;
590 1684875893 : for (std::vector<MSLane*>::const_iterator i = allowed->begin(); i != allowed->end(); ++i, aIndex++) {
591 845468208 : const double occupancy = (*i)->getBruttoOccupancy();
592 845468208 : if (occupancy < leastOccupancy) {
593 844743173 : res = (*i);
594 : leastOccupancy = occupancy;
595 : }
596 845468208 : const MSVehicle* last = (*i)->getLastFullVehicle();
597 845468208 : double lastGap = (last != nullptr ? last->getPositionOnLane() : myLength) - departPos;
598 : // never insert to the left of a vehicle with a larger speedFactor
599 845468208 : if (lastGap > largestGap && maxSpeed >= largestSpeed) {
600 : largestGap = lastGap;
601 800263323 : resByGap = (*i);
602 : }
603 845468208 : if (last != nullptr) {
604 843223109 : largestSpeed = MAX2(largestSpeed, getVehicleMaxSpeed(last));
605 : }
606 : }
607 839407685 : if (resByGap != nullptr) {
608 : //if (res != resByGap) std::cout << SIMTIME << " edge=" << getID() << " departPos=" << departPos << " res=" << Named::getIDSecure(res) << " resByGap=" << Named::getIDSecure(resByGap) << " largestGap=" << largestGap << "\n";
609 : res = resByGap;
610 : }
611 : }
612 839407685 : return res;
613 : }
614 :
615 :
616 : double
617 1058413524 : MSEdge::getDepartPosBound(const MSVehicle& veh, bool upper) const {
618 1058413524 : const SUMOVehicleParameter& pars = veh.getParameter();
619 : double pos = getLength();
620 : // determine the position
621 1058413524 : switch (pars.departPosProcedure) {
622 10036631 : case DepartPosDefinition::GIVEN:
623 10036631 : pos = pars.departPos;
624 10036631 : if (pos < 0.) {
625 2860457 : pos += myLength;
626 : }
627 : break;
628 : case DepartPosDefinition::RANDOM:
629 : // could be any position on the edge
630 : break;
631 : case DepartPosDefinition::RANDOM_FREE:
632 : // could be any position on the edge due to multiple random attempts
633 : break;
634 : case DepartPosDefinition::FREE:
635 : // many candidate positions, upper bound could be computed exactly
636 : // with much effort
637 : break;
638 11728858 : case DepartPosDefinition::LAST:
639 11728858 : if (upper) {
640 472996 : for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
641 321540 : MSVehicle* last = (*i)->getLastFullVehicle();
642 321540 : if (last != nullptr) {
643 278498 : pos = MIN2(pos, last->getPositionOnLane());
644 : }
645 : }
646 : } else {
647 : pos = 0;
648 : }
649 : break;
650 831339773 : case DepartPosDefinition::BASE:
651 : case DepartPosDefinition::DEFAULT:
652 831339773 : if (!upper) {
653 : pos = 0;
654 : }
655 : break;
656 28 : default:
657 28 : pos = MIN2(pos, veh.getVehicleType().getLength());
658 : break;
659 : }
660 1058413524 : return pos;
661 : }
662 :
663 :
664 : MSLane*
665 46 : MSEdge::getDepartLaneMeso(SUMOVehicle& veh) const {
666 46 : if (veh.getParameter().departLaneProcedure == DepartLaneDefinition::GIVEN) {
667 3 : if ((int) myLanes->size() <= veh.getParameter().departLane || !(*myLanes)[veh.getParameter().departLane]->allowsVehicleClass(veh.getVehicleType().getVehicleClass())) {
668 0 : return nullptr;
669 : }
670 3 : return (*myLanes)[veh.getParameter().departLane];
671 : }
672 43 : return (*myLanes)[0];
673 : }
674 :
675 :
676 : MSLane*
677 1763694521 : MSEdge::getDepartLane(MSVehicle& veh) const {
678 1763694521 : DepartLaneDefinition dld = veh.getParameter().departLaneProcedure;
679 1763694521 : int departLane = veh.getParameter().departLane;
680 1763694521 : if (dld == DepartLaneDefinition::DEFAULT) {
681 1323091713 : dld = myDefaultDepartLaneDefinition;
682 1323091713 : departLane = myDefaultDepartLane;
683 : }
684 1763694521 : switch (dld) {
685 125049485 : case DepartLaneDefinition::GIVEN:
686 125049485 : if ((int) myLanes->size() <= departLane || !(*myLanes)[departLane]->allowsVehicleClass(veh.getVClass())) {
687 66 : return nullptr;
688 : }
689 125049419 : return (*myLanes)[departLane];
690 90524551 : case DepartLaneDefinition::RANDOM:
691 181049102 : return RandHelper::getRandomFrom(*allowedLanes(veh.getVehicleType().getVehicleClass()));
692 184146651 : case DepartLaneDefinition::FREE:
693 184146651 : return getFreeLane(nullptr, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
694 4971244 : case DepartLaneDefinition::ALLOWED_FREE:
695 4971244 : if (veh.getRoute().size() == 1) {
696 5960 : return getFreeLane(nullptr, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
697 : } else {
698 4965284 : return getFreeLane(allowedLanes(**(veh.getRoute().begin() + 1), veh.getVehicleType().getVehicleClass()), veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
699 : }
700 867882487 : case DepartLaneDefinition::BEST_FREE:
701 : case DepartLaneDefinition::BEST_PROB: {
702 867882487 : veh.updateBestLanes(false, myLanes->front());
703 867882487 : const std::vector<MSVehicle::LaneQ>& bl = veh.getBestLanes();
704 : double bestLength = -1;
705 2562422919 : for (std::vector<MSVehicle::LaneQ>::const_iterator i = bl.begin(); i != bl.end(); ++i) {
706 1694540432 : if ((*i).length > bestLength) {
707 : bestLength = (*i).length;
708 : }
709 : }
710 : // beyond a certain length, all lanes are suitable
711 : // however, we still need to check departPos to avoid unsuitable insertion
712 : // (this is only possible in some cases)
713 : double departPos = 0;
714 867882487 : if (bestLength > BEST_LANE_LOOKAHEAD) {
715 1413142 : departPos = getDepartPosBound(veh);
716 1413142 : bestLength = MIN2(bestLength - departPos, BEST_LANE_LOOKAHEAD);
717 : }
718 867882487 : std::vector<MSLane*>* bestLanes = new std::vector<MSLane*>();
719 2562422919 : for (std::vector<MSVehicle::LaneQ>::const_iterator i = bl.begin(); i != bl.end(); ++i) {
720 1694540432 : if (((*i).length - departPos) >= bestLength) {
721 876684876 : if (isInternal()) {
722 32 : for (MSLane* lane : *myLanes) {
723 20 : if (lane->getNormalSuccessorLane() == (*i).lane && lane->allowsVehicleClass(veh.getVClass()) ) {
724 12 : bestLanes->push_back(lane);
725 : }
726 : }
727 876684864 : } else if ((*i).lane->allowsVehicleClass(veh.getVClass())) {
728 876252914 : bestLanes->push_back((*i).lane);
729 : }
730 : }
731 : }
732 : MSLane* ret = nullptr;
733 867882487 : if (veh.getParameter().departLaneProcedure == DepartLaneDefinition::BEST_FREE) {
734 28474802 : ret = getFreeLane(bestLanes, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
735 : } else {
736 839407685 : ret = getProbableLane(bestLanes, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false), getVehicleMaxSpeed(&veh));
737 : }
738 867882487 : delete bestLanes;
739 867882487 : return ret;
740 : }
741 491120103 : case DepartLaneDefinition::DEFAULT:
742 : case DepartLaneDefinition::FIRST_ALLOWED:
743 491120103 : return getFirstAllowed(veh.getVehicleType().getVehicleClass());
744 : default:
745 : break;
746 : }
747 0 : if (!(*myLanes)[0]->allowsVehicleClass(veh.getVehicleType().getVehicleClass())) {
748 : return nullptr;
749 : }
750 0 : return (*myLanes)[0];
751 : }
752 :
753 :
754 : MSLane*
755 492093932 : MSEdge::getFirstAllowed(SUMOVehicleClass vClass, bool defaultFirst, int routingMode) const {
756 499325210 : for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
757 498456310 : if ((*i)->allowsVehicleClass(vClass, routingMode)) {
758 491225032 : return *i;
759 : }
760 : }
761 868900 : return defaultFirst && !myLanes->empty() ? myLanes->front() : nullptr;
762 : }
763 :
764 :
765 : bool
766 2837760568 : MSEdge::validateDepartSpeed(SUMOVehicle& v) const {
767 2837760568 : const SUMOVehicleParameter& pars = v.getParameter();
768 2837760568 : const MSVehicleType& type = v.getVehicleType();
769 2837760568 : if (pars.departSpeedProcedure == DepartSpeedDefinition::GIVEN) {
770 : // departSpeed could have been rounded down in the output
771 150720273 : double vMax = getVehicleMaxSpeed(&v) + SPEED_EPS;
772 150720273 : if (pars.departSpeed > vMax) {
773 : // check departLane (getVehicleMaxSpeed checks lane 0)
774 19612 : MSLane* departLane = MSGlobals::gMesoNet ? getDepartLaneMeso(v) : getDepartLane(dynamic_cast<MSVehicle&>(v));
775 19612 : if (departLane != nullptr) {
776 19612 : vMax = departLane->getVehicleMaxSpeed(&v);
777 19612 : if (pars.wasSet(VEHPARS_SPEEDFACTOR_SET)) {
778 : // speedFactor could have been rounded down in the output
779 7 : vMax *= (1 + SPEED_EPS);
780 : }
781 : // additive term must come after multiplication!
782 19612 : vMax += SPEED_EPS;
783 19612 : if (pars.departSpeed > vMax) {
784 19598 : if (type.getSpeedFactor().getParameter(1) > 0.) {
785 39158 : v.setChosenSpeedFactor(type.computeChosenSpeedDeviation(pars.speedFactor, nullptr, pars.departSpeed / MIN2(getSpeedLimit(), type.getDesiredMaxSpeed() - SPEED_EPS)));
786 19579 : if (v.getChosenSpeedFactor() > type.getSpeedFactor().getParameter(0) + 2 * type.getSpeedFactor().getParameter(1)) {
787 : // only warn for significant deviation
788 33536 : WRITE_WARNINGF(TL("Choosing new speed factor % for vehicle '%' to match departure speed % (max %)."),
789 : toString(v.getChosenSpeedFactor()), pars.id, pars.departSpeed, vMax);
790 : }
791 : } else {
792 : return false;
793 : }
794 : }
795 : }
796 : }
797 : }
798 : return true;
799 : }
800 :
801 :
802 : bool
803 2838221953 : MSEdge::insertVehicle(SUMOVehicle& v, SUMOTime time, const bool checkOnly, const bool forceCheck) const {
804 : // when vaporizing, no vehicles are inserted, but checking needs to be successful to trigger removal
805 2838203167 : if (isVaporizing() || isTazConnector()
806 5675959821 : || v.getRouteValidity(true, checkOnly) != MSBaseVehicle::ROUTE_VALID) {
807 484152 : return checkOnly;
808 : }
809 2837737622 : const SUMOVehicleParameter& pars = v.getParameter();
810 2837737622 : if (!validateDepartSpeed(v)) {
811 14 : if (MSGlobals::gCheckRoutes) {
812 21 : throw ProcessError(TLF("Departure speed for vehicle '%' is too high for the departure edge '%', time=%.",
813 21 : pars.id, getID(), time2string(time)));
814 : } else {
815 21 : WRITE_WARNINGF(TL("Departure speed for vehicle '%' is too high for the departure edge '%', time=%."),
816 : pars.id, getID(), time2string(time));
817 : }
818 : }
819 2837737615 : if (MSGlobals::gUseMesoSim) {
820 600745695 : if (!forceCheck && myLastFailedInsertionTime == time) {
821 : return false;
822 : }
823 : double pos = 0.0;
824 103674698 : switch (pars.departPosProcedure) {
825 596112 : case DepartPosDefinition::GIVEN:
826 596112 : if (pars.departPos >= 0.) {
827 : pos = pars.departPos;
828 : } else {
829 8393 : pos = pars.departPos + getLength();
830 : }
831 596112 : if (pos < 0 || pos > getLength()) {
832 6 : WRITE_WARNINGF(TL("Invalid departPos % given for vehicle '%', time=%. Inserting at lane end instead."),
833 : pos, v.getID(), time2string(time));
834 : pos = getLength();
835 : }
836 : break;
837 : case DepartPosDefinition::RANDOM:
838 : case DepartPosDefinition::RANDOM_FREE:
839 : pos = RandHelper::rand(getLength());
840 44186 : break;
841 : default:
842 : break;
843 : }
844 : bool result = false;
845 103674698 : MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this, pos);
846 : MEVehicle* veh = static_cast<MEVehicle*>(&v);
847 : int qIdx;
848 103674698 : if (pars.departPosProcedure == DepartPosDefinition::FREE) {
849 486502 : while (segment != nullptr && !result) {
850 447954 : if (checkOnly) {
851 6 : result = segment->hasSpaceFor(veh, time, qIdx, true) == time;
852 : } else {
853 447948 : result = segment->initialise(veh, time);
854 : }
855 : segment = segment->getNextSegment();
856 : }
857 : } else {
858 103636150 : if (checkOnly) {
859 93006963 : result = segment->hasSpaceFor(veh, time, qIdx, true) == time;
860 : } else {
861 10629187 : result = segment->initialise(veh, time);
862 : }
863 : }
864 103674696 : return result;
865 : }
866 2236991920 : if (checkOnly) {
867 616039463 : DepartLaneDefinition dld = v.getParameter().departLaneProcedure;
868 616039463 : if (dld == DepartLaneDefinition::DEFAULT) {
869 561257444 : dld = myDefaultDepartLaneDefinition;
870 : }
871 616039463 : switch (dld) {
872 80186116 : case DepartLaneDefinition::GIVEN:
873 : case DepartLaneDefinition::DEFAULT:
874 : case DepartLaneDefinition::FIRST_ALLOWED: {
875 80186116 : MSLane* insertionLane = getDepartLane(static_cast<MSVehicle&>(v));
876 80186116 : if (insertionLane == nullptr) {
877 1295850 : WRITE_WARNINGF(TL("Could not insert vehicle '%' on any lane of edge '%', time=%."),
878 : v.getID(), getID(), time2string(time));
879 431950 : return false;
880 : }
881 79754166 : const double occupancy = insertionLane->getBruttoOccupancy();
882 79754166 : return (occupancy == 0 || occupancy * myLength + v.getVehicleType().getLengthWithGap() <= myLength ||
883 6031808 : v.getParameter().departProcedure == DepartDefinition::SPLIT);
884 : }
885 535853347 : default:
886 553405048 : for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
887 545212746 : const double occupancy = (*i)->getBruttoOccupancy();
888 545212746 : if (occupancy == 0 || occupancy * myLength + v.getVehicleType().getLengthWithGap() <= myLength ||
889 17551701 : v.getParameter().departProcedure == DepartDefinition::SPLIT) {
890 : return true;
891 : }
892 : }
893 : }
894 : return false;
895 : }
896 1620952457 : MSLane* insertionLane = getDepartLane(static_cast<MSVehicle&>(v));
897 1620952457 : if (insertionLane == nullptr) {
898 : return false;
899 : }
900 :
901 1620083557 : if (!forceCheck) {
902 1620083380 : if (myLastFailedInsertionTime == time) {
903 : if (myFailedInsertionMemory.count(insertionLane->getIndex())) {
904 : // A vehicle was already rejected for the proposed insertionLane in this timestep
905 : return false;
906 : }
907 : } else {
908 : // last rejection occurred in a previous timestep, clear cache
909 : myFailedInsertionMemory.clear();
910 : }
911 : }
912 :
913 13568602 : bool success = insertionLane->insertVehicle(static_cast<MSVehicle&>(v));
914 :
915 13568601 : if (!success) {
916 : // constraints may enforce explicit re-ordering so we need to try other vehicles after failure
917 20191846 : if (!insertionLane->hasParameter("insertionOrder" + v.getID())) {
918 10095776 : myFailedInsertionMemory.insert(insertionLane->getIndex());
919 : }
920 : }
921 : return success;
922 : }
923 :
924 :
925 : void
926 42703779 : MSEdge::changeLanes(SUMOTime t) const {
927 42703779 : if (myLaneChanger != nullptr) {
928 42703779 : myLaneChanger->laneChange(t);
929 : }
930 42703779 : }
931 :
932 :
933 : const MSEdge*
934 5798957 : MSEdge::getInternalFollowingEdge(const MSEdge* followerAfterInternal, SUMOVehicleClass vClass) const {
935 : //@todo to be optimized
936 6618517 : for (const MSLane* const l : *myLanes) {
937 9691407 : for (const MSLink* const link : l->getLinkCont()) {
938 8871847 : if (&link->getLane()->getEdge() == followerAfterInternal) {
939 5534584 : if (link->getViaLane() != nullptr) {
940 4506744 : if (link->getViaLane()->allowsVehicleClass(vClass)) {
941 4466413 : return &link->getViaLane()->getEdge();
942 : } else {
943 40331 : continue;
944 : }
945 : } else {
946 : return nullptr; // network without internal links
947 : }
948 : }
949 : }
950 : }
951 : return nullptr;
952 : }
953 :
954 :
955 : double
956 1241026 : MSEdge::getInternalFollowingLengthTo(const MSEdge* followerAfterInternal, SUMOVehicleClass vClass) const {
957 : assert(followerAfterInternal != 0);
958 : assert(!followerAfterInternal->isInternal());
959 : double dist = 0.;
960 1241026 : const MSEdge* edge = getInternalFollowingEdge(followerAfterInternal, vClass);
961 : // Take into account non-internal lengths until next non-internal edge
962 2289719 : while (edge != nullptr && edge->isInternal()) {
963 1048693 : dist += edge->getLength();
964 1048693 : edge = edge->getInternalFollowingEdge(followerAfterInternal, vClass);
965 : }
966 1241026 : return dist;
967 : }
968 :
969 :
970 : const MSEdge*
971 150656 : MSEdge::getNormalBefore() const {
972 : const MSEdge* result = this;
973 159783 : while (result->isInternal() && MSGlobals::gUsingInternalLanes) {
974 : assert(result->getPredecessors().size() == 1);
975 9127 : result = result->getPredecessors().front();
976 : }
977 150656 : return result;
978 : }
979 :
980 : const MSEdge*
981 6172009 : MSEdge::getNormalSuccessor() const {
982 : const MSEdge* result = this;
983 11568216 : while (result->isInternal()) {
984 : assert(result->getSuccessors().size() == 1);
985 5396207 : result = result->getSuccessors().front();
986 : }
987 6172009 : return result;
988 : }
989 :
990 : double
991 191843425 : MSEdge::getMeanSpeed() const {
992 : double v = 0;
993 : double totalNumVehs = 0;
994 191843425 : if (MSGlobals::gUseMesoSim) {
995 256288183 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
996 : const int numVehs = segment->getCarNumber();
997 187468177 : if (numVehs > 0) {
998 25316301 : v += numVehs * segment->getMeanSpeed();
999 25316301 : totalNumVehs += numVehs;
1000 : }
1001 : }
1002 68820006 : if (totalNumVehs == 0) {
1003 59611622 : return getLength() / myEmptyTraveltime; // may include tls-penalty
1004 : }
1005 : } else {
1006 283357756 : for (const MSLane* const lane : *myLanes) {
1007 : int numVehs = lane->getVehicleNumber();
1008 160334337 : if (numVehs == 0) {
1009 : // take speed limit but with lowest possible weight
1010 : numVehs = 1;
1011 : }
1012 160334337 : v += numVehs * lane->getMeanSpeed();
1013 160334337 : totalNumVehs += numVehs;
1014 : }
1015 123023419 : if (myBidiEdge != nullptr) {
1016 8794392 : for (const MSLane* const lane : myBidiEdge->getLanes()) {
1017 4555826 : if (lane->getVehicleNumber() > 0) {
1018 : // do not route across edges which are already occupied in reverse direction
1019 : return 0;
1020 : }
1021 : }
1022 : }
1023 122707155 : if (totalNumVehs == 0) {
1024 0 : return getSpeedLimit();
1025 : }
1026 : }
1027 131915539 : return v / totalNumVehs;
1028 : }
1029 :
1030 :
1031 : double
1032 8 : MSEdge::getMeanFriction() const {
1033 : double f = 0.;
1034 32 : for (const MSLane* const lane : *myLanes) {
1035 24 : f += lane->getFrictionCoefficient();
1036 : }
1037 8 : if (!myLanes->empty()) {
1038 8 : return f / (double)myLanes->size();
1039 : }
1040 : return 1.;
1041 : }
1042 :
1043 :
1044 : double
1045 1272 : MSEdge::getMeanSpeedBike() const {
1046 1272 : if (MSGlobals::gUseMesoSim) {
1047 : // no separate bicycle speeds in meso
1048 362 : return getMeanSpeed();
1049 : }
1050 : double v = 0;
1051 : double totalNumVehs = 0;
1052 3005 : for (const MSLane* const lane : *myLanes) {
1053 : const int numVehs = lane->getVehicleNumber();
1054 2095 : v += numVehs * lane->getMeanSpeedBike();
1055 2095 : totalNumVehs += numVehs;
1056 : }
1057 910 : if (totalNumVehs == 0) {
1058 455 : return getSpeedLimit();
1059 : }
1060 455 : return v / totalNumVehs;
1061 : }
1062 :
1063 :
1064 : double
1065 64362 : MSEdge::getCurrentTravelTime(double minSpeed) const {
1066 : assert(minSpeed > 0);
1067 64362 : if (!myAmDelayed) {
1068 44800 : return myEmptyTraveltime;
1069 : }
1070 39124 : return getLength() / MAX2(minSpeed, getMeanSpeed());
1071 : }
1072 :
1073 :
1074 : double
1075 0 : MSEdge::getRoutingSpeed() const {
1076 0 : return MSRoutingEngine::getAssumedSpeed(this, nullptr);
1077 : }
1078 :
1079 :
1080 : bool
1081 1861240 : MSEdge::dictionary(const std::string& id, MSEdge* ptr) {
1082 : const DictType::iterator it = myDict.lower_bound(id);
1083 1861240 : if (it == myDict.end() || it->first != id) {
1084 : // id not in myDict
1085 1861240 : myDict.emplace_hint(it, id, ptr);
1086 3722520 : while (ptr->getNumericalID() >= (int)myEdges.size()) {
1087 1861280 : myEdges.push_back(nullptr);
1088 : }
1089 1861240 : myEdges[ptr->getNumericalID()] = ptr;
1090 1861240 : return true;
1091 : }
1092 : return false;
1093 : }
1094 :
1095 :
1096 : MSEdge*
1097 8772914 : MSEdge::dictionary(const std::string& id) {
1098 : const DictType::iterator it = myDict.find(id);
1099 8772914 : if (it == myDict.end()) {
1100 : return nullptr;
1101 : }
1102 6909006 : return it->second;
1103 : }
1104 :
1105 :
1106 : MSEdge*
1107 2964962 : MSEdge::dictionaryHint(const std::string& id, const int startIdx) {
1108 : // this method is mainly useful when parsing connections from the net.xml which are sorted by "from" id
1109 2964962 : if (myEdges[startIdx] != nullptr && myEdges[startIdx]->getID() == id) {
1110 : return myEdges[startIdx];
1111 : }
1112 1872334 : if (startIdx + 1 < (int)myEdges.size() && myEdges[startIdx + 1] != nullptr && myEdges[startIdx + 1]->getID() == id) {
1113 : return myEdges[startIdx + 1];
1114 : }
1115 616701 : return dictionary(id);
1116 : }
1117 :
1118 :
1119 : const MSEdgeVector&
1120 909707 : MSEdge::getAllEdges() {
1121 909707 : return myEdges;
1122 : }
1123 :
1124 :
1125 : void
1126 42634 : MSEdge::clear() {
1127 1889048 : for (DictType::iterator i = myDict.begin(); i != myDict.end(); ++i) {
1128 1846414 : delete (*i).second;
1129 : }
1130 : myDict.clear();
1131 : myEdges.clear();
1132 42634 : }
1133 :
1134 :
1135 : void
1136 285 : MSEdge::insertIDs(std::vector<std::string>& into) {
1137 16016 : for (DictType::iterator i = myDict.begin(); i != myDict.end(); ++i) {
1138 15731 : into.push_back((*i).first);
1139 : }
1140 285 : }
1141 :
1142 :
1143 : void
1144 415827 : MSEdge::parseEdgesList(const std::string& desc, ConstMSEdgeVector& into,
1145 : const std::string& rid) {
1146 415827 : StringTokenizer st(desc);
1147 415827 : parseEdgesList(st.getVector(), into, rid);
1148 415827 : }
1149 :
1150 :
1151 : void
1152 416109 : MSEdge::parseEdgesList(const std::vector<std::string>& desc, ConstMSEdgeVector& into,
1153 : const std::string& rid) {
1154 1572482 : for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
1155 1156422 : const MSEdge* edge = MSEdge::dictionary(*i);
1156 : // check whether the edge exists
1157 1156422 : if (edge == nullptr) {
1158 49 : throw ProcessError("The edge '" + *i + "' within the route " + rid + " is not known."
1159 147 : + "\n The route can not be build.");
1160 : }
1161 1156373 : into.push_back(edge);
1162 : }
1163 416060 : }
1164 :
1165 :
1166 : double
1167 1834667 : MSEdge::getDistanceTo(const MSEdge* other, const bool doBoundaryEstimate) const {
1168 : assert(this != other);
1169 1834667 : if (doBoundaryEstimate) {
1170 19288 : return myBoundary.distanceTo2D(other->myBoundary);
1171 : }
1172 1815379 : if (isTazConnector()) {
1173 564 : if (other->isTazConnector()) {
1174 448 : return myBoundary.distanceTo2D(other->myBoundary);
1175 : }
1176 116 : return myBoundary.distanceTo2D(other->getLanes()[0]->getShape()[0]);
1177 : }
1178 1814815 : if (other->isTazConnector()) {
1179 5405 : return other->myBoundary.distanceTo2D(getLanes()[0]->getShape()[-1]);
1180 : }
1181 1809410 : return getLanes()[0]->getShape()[-1].distanceTo2D(other->getLanes()[0]->getShape()[0]);
1182 : }
1183 :
1184 :
1185 : const Position
1186 2453 : MSEdge::getStopPosition(const SUMOVehicleParameter::Stop& stop) {
1187 2453 : return MSLane::dictionary(stop.lane)->geometryPositionAtOffset((stop.endPos + stop.startPos) / 2.);
1188 : }
1189 :
1190 :
1191 : double
1192 94193224 : MSEdge::getSpeedLimit() const {
1193 : // @note lanes might have different maximum speeds in theory
1194 94193224 : return myLanes->empty() ? 1 : getLanes()[0]->getSpeedLimit();
1195 : }
1196 :
1197 :
1198 : double
1199 848 : MSEdge::getSpeedLimit(SUMOVehicleClass svc) const {
1200 : // @note lanes might have different maximum speeds in theory
1201 848 : return myLanes->empty() ? 1 : getLanes()[0]->getSpeedLimit(svc);
1202 : }
1203 :
1204 :
1205 : double
1206 1941913 : MSEdge::getLengthGeometryFactor() const {
1207 1941913 : return myLanes->empty() ? 1 : getLanes()[0]->getLengthGeometryFactor();
1208 : }
1209 :
1210 : double
1211 2071348030 : MSEdge::getVehicleMaxSpeed(const SUMOTrafficObject* const veh) const {
1212 : // @note lanes might have different maximum speeds in theory
1213 2071348030 : return myLanes->empty() ? 1 : getLanes()[0]->getVehicleMaxSpeed(veh);
1214 : }
1215 :
1216 :
1217 : void
1218 205 : MSEdge::setMaxSpeed(const double val, const bool modified, const double jamThreshold) {
1219 : assert(val >= 0);
1220 205 : if (myLanes != nullptr) {
1221 564 : for (MSLane* const lane : *myLanes) {
1222 359 : lane->setMaxSpeed(val, modified, jamThreshold);
1223 : }
1224 : }
1225 205 : }
1226 :
1227 :
1228 : void
1229 3277640 : MSEdge::addTransportable(MSTransportable* t) const {
1230 3277640 : if (t->isPerson()) {
1231 : myPersons.insert(t);
1232 : } else {
1233 : myContainers.insert(t);
1234 : }
1235 3277640 : }
1236 :
1237 : void
1238 6798091 : MSEdge::removeTransportable(MSTransportable* t) const {
1239 6798091 : std::set<MSTransportable*, ComparatorNumericalIdLess>& tc = t->isPerson() ? myPersons : myContainers;
1240 : auto it = tc.find(t);
1241 6798091 : if (it != tc.end()) {
1242 : tc.erase(it);
1243 : }
1244 6798091 : }
1245 :
1246 : std::vector<MSTransportable*>
1247 113671875 : MSEdge::getSortedPersons(SUMOTime timestep, bool includeRiding) const {
1248 113671875 : std::vector<MSTransportable*> result(myPersons.begin(), myPersons.end());
1249 113671875 : if (includeRiding) {
1250 2896155 : for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
1251 2042878 : const MSLane::VehCont& vehs = (*i)->getVehiclesSecure();
1252 3266518 : for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
1253 1223640 : const std::vector<MSTransportable*>& persons = (*j)->getPersons();
1254 1223640 : result.insert(result.end(), persons.begin(), persons.end());
1255 : }
1256 2042878 : (*i)->releaseVehicles();
1257 : }
1258 : }
1259 113671875 : sort(result.begin(), result.end(), transportable_by_position_sorter(timestep));
1260 113671875 : return result;
1261 0 : }
1262 :
1263 :
1264 : std::vector<MSTransportable*>
1265 34569028 : MSEdge::getSortedContainers(SUMOTime timestep, bool /* includeRiding */) const {
1266 34569028 : std::vector<MSTransportable*> result(myContainers.begin(), myContainers.end());
1267 34569028 : sort(result.begin(), result.end(), transportable_by_position_sorter(timestep));
1268 34569028 : return result;
1269 0 : }
1270 :
1271 :
1272 : int
1273 5268076 : MSEdge::transportable_by_position_sorter::operator()(const MSTransportable* const c1, const MSTransportable* const c2) const {
1274 5268076 : const double pos1 = c1->getCurrentStage()->getEdgePos(myTime);
1275 5268076 : const double pos2 = c2->getCurrentStage()->getEdgePos(myTime);
1276 5268076 : if (pos1 != pos2) {
1277 5110288 : return pos1 < pos2;
1278 : }
1279 157788 : return c1->getID() < c2->getID();
1280 : }
1281 :
1282 :
1283 : void
1284 125245 : MSEdge::addSuccessor(MSEdge* edge, const MSEdge* via) {
1285 125245 : mySuccessors.push_back(edge);
1286 125245 : myViaSuccessors.push_back(std::make_pair(edge, via));
1287 125245 : if (isTazConnector() && edge->getFromJunction() != nullptr) {
1288 62619 : myBoundary.add(edge->getFromJunction()->getPosition());
1289 : }
1290 :
1291 125245 : edge->myPredecessors.push_back(this);
1292 125245 : if (edge->isTazConnector() && getToJunction() != nullptr) {
1293 62626 : edge->myBoundary.add(getToJunction()->getPosition());
1294 : }
1295 125245 : }
1296 :
1297 :
1298 : const MSEdgeVector&
1299 80800966 : MSEdge::getSuccessors(SUMOVehicleClass vClass) const {
1300 80800966 : if (vClass == SVC_IGNORING || !MSNet::getInstance()->hasPermissions() || myFunction == SumoXMLEdgeFunc::CONNECTOR) {
1301 80783763 : return mySuccessors;
1302 : }
1303 : #ifdef HAVE_FOX
1304 17203 : ScopedLocker<> lock(mySuccessorMutex, MSGlobals::gNumThreads > 1);
1305 : #endif
1306 : std::map<SUMOVehicleClass, MSEdgeVector>::iterator i = myClassesSuccessorMap.find(vClass);
1307 17203 : if (i == myClassesSuccessorMap.end()) {
1308 : // instantiate vector
1309 2230 : myClassesSuccessorMap[vClass];
1310 : i = myClassesSuccessorMap.find(vClass);
1311 : // this vClass is requested for the first time. rebuild all successors
1312 11473 : for (MSEdgeVector::const_iterator it = mySuccessors.begin(); it != mySuccessors.end(); ++it) {
1313 9243 : if ((*it)->isTazConnector()) {
1314 288 : i->second.push_back(*it);
1315 : } else {
1316 8955 : const std::vector<MSLane*>* allowed = allowedLanes(**it, vClass);
1317 8955 : if (allowed != nullptr && allowed->size() > 0) {
1318 6729 : i->second.push_back(*it);
1319 : }
1320 : }
1321 : }
1322 : }
1323 : // can use cached value
1324 17203 : return i->second;
1325 : }
1326 :
1327 :
1328 : const MSConstEdgePairVector&
1329 198883729 : MSEdge::getViaSuccessors(SUMOVehicleClass vClass, bool ignoreTransientPermissions) const {
1330 198883729 : if (vClass == SVC_IGNORING || !MSNet::getInstance()->hasPermissions() || myFunction == SumoXMLEdgeFunc::CONNECTOR) {
1331 193019281 : return myViaSuccessors;
1332 : }
1333 : #ifdef HAVE_FOX
1334 5864448 : ScopedLocker<> lock(mySuccessorMutex, MSGlobals::gNumThreads > 1);
1335 : #endif
1336 5864448 : auto& viaMap = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigClassesViaSuccessorMap : myClassesViaSuccessorMap;
1337 : auto i = viaMap.find(vClass);
1338 5864448 : if (i != viaMap.end()) {
1339 : // can use cached value
1340 5809111 : return i->second;
1341 : }
1342 : // instantiate vector
1343 55337 : MSConstEdgePairVector& result = viaMap[vClass];
1344 : // this vClass is requested for the first time. rebuild all successors
1345 235956 : for (const auto& viaPair : myViaSuccessors) {
1346 180619 : if (viaPair.first->isTazConnector()) {
1347 6321 : result.push_back(viaPair);
1348 : } else {
1349 174298 : const std::vector<MSLane*>* allowed = allowedLanes(*viaPair.first, vClass, ignoreTransientPermissions);
1350 174298 : if (allowed != nullptr && allowed->size() > 0) {
1351 130998 : result.push_back(viaPair);
1352 : }
1353 : }
1354 : }
1355 : return result;
1356 : }
1357 :
1358 :
1359 : void
1360 1791011 : MSEdge::setJunctions(MSJunction* from, MSJunction* to) {
1361 1791011 : myFromJunction = from;
1362 1791011 : myToJunction = to;
1363 1791011 : if (!isTazConnector()) {
1364 1791011 : myBoundary.add(from->getPosition());
1365 1791011 : myBoundary.add(to->getPosition());
1366 : }
1367 1791011 : }
1368 :
1369 :
1370 : bool
1371 2951865 : MSEdge::canChangeToOpposite() const {
1372 2951865 : return (!myLanes->empty() && myLanes->back()->getOpposite() != nullptr &&
1373 : // do not change on curved internal lanes
1374 : (!isInternal()
1375 5944 : || (MSGlobals::gUsingInternalLanes
1376 5944 : && myLanes->back()->getIncomingLanes()[0].viaLink->getDirection() == LinkDirection::STRAIGHT)));
1377 : }
1378 :
1379 :
1380 : const MSEdge*
1381 22524671 : MSEdge::getOppositeEdge() const {
1382 22524671 : if (!myLanes->empty() && myLanes->back()->getOpposite() != nullptr) {
1383 2999042 : return &(myLanes->back()->getOpposite()->getEdge());
1384 : } else {
1385 19525629 : return nullptr;
1386 : }
1387 : }
1388 :
1389 :
1390 : bool
1391 178 : MSEdge::hasMinorLink() const {
1392 354 : for (const MSLane* const l : *myLanes) {
1393 386 : for (const MSLink* const link : l->getLinkCont()) {
1394 210 : if (!link->havePriority()) {
1395 : return true;
1396 : }
1397 : }
1398 : }
1399 : return false;
1400 : }
1401 :
1402 : bool
1403 211480 : MSEdge::hasChangeProhibitions(SUMOVehicleClass svc, int index) const {
1404 211480 : if (myLanes->size() == 1) {
1405 : return false;
1406 : }
1407 294329 : for (const MSLane* const l : *myLanes) {
1408 199755 : if (l->getIndex() <= index && !l->allowsChangingRight(svc) && l->getIndex() > 0) {
1409 : return true;
1410 199711 : } else if (l->getIndex() >= index && !l->allowsChangingLeft(svc) && l->getIndex() < (int)(myLanes->size() - 1)) {
1411 : return true;
1412 : }
1413 : }
1414 : return false;
1415 : }
1416 :
1417 : void
1418 922901 : MSEdge::checkAndRegisterBiDirEdge(const std::string& bidiID) {
1419 922901 : if (bidiID != "") {
1420 32980 : myBidiEdge = dictionary(bidiID);
1421 32980 : if (myBidiEdge == nullptr) {
1422 0 : WRITE_ERRORF(TL("Bidi-edge '%' does not exist"), bidiID);
1423 : }
1424 32980 : setBidiLanes();
1425 528234 : return;
1426 : }
1427 889921 : if (getFunction() != SumoXMLEdgeFunc::NORMAL) {
1428 : return;
1429 : }
1430 : // legacy networks (no bidi attribute)
1431 394667 : ConstMSEdgeVector candidates = myToJunction->getOutgoing();
1432 3113103 : for (ConstMSEdgeVector::const_iterator it = candidates.begin(); it != candidates.end(); it++) {
1433 2718436 : if ((*it)->getToJunction() == myFromJunction) { //reverse edge
1434 312332 : if (myBidiEdge != nullptr && isSuperposable(*it)) {
1435 0 : WRITE_WARNINGF(TL("Ambiguous superposable edges between junction '%' and '%'."), myToJunction->getID(), myFromJunction->getID());
1436 0 : break;
1437 : }
1438 312332 : if (isSuperposable(*it)) {
1439 26 : myBidiEdge = *it;
1440 26 : setBidiLanes();
1441 : }
1442 : }
1443 : }
1444 394667 : }
1445 :
1446 :
1447 : void
1448 33006 : MSEdge::setBidiLanes() {
1449 : assert(myBidiEdge != nullptr);
1450 33006 : if (getNumLanes() == 1 && myBidiEdge->getNumLanes() == 1) {
1451 : // the other way round is set when this method runs for the bidiEdge
1452 31274 : getLanes()[0]->setBidiLane(myBidiEdge->getLanes()[0]);
1453 : } else {
1454 : // find lanes with matching reversed shapes
1455 : int numBidiLanes = 0;
1456 5262 : for (MSLane* l1 : *myLanes) {
1457 10794 : for (MSLane* l2 : *myBidiEdge->myLanes) {
1458 7264 : if (l1->getShape().reverse().almostSame(l2->getShape(), POSITION_EPS * 2)) {
1459 1786 : l1->setBidiLane(l2);
1460 1786 : numBidiLanes++;
1461 : }
1462 : }
1463 : }
1464 : // warn only once for each pair
1465 1732 : if (numBidiLanes == 0 && getNumericalID() < myBidiEdge->getNumericalID()) {
1466 15 : WRITE_WARNINGF(TL("Edge '%' and bidi edge '%' have no matching bidi lanes"), getID(), myBidiEdge->getID());
1467 : }
1468 : }
1469 33006 : }
1470 :
1471 :
1472 : bool
1473 312332 : MSEdge::isSuperposable(const MSEdge* other) {
1474 312332 : if (other == nullptr || other->getLanes().size() != myLanes->size()) {
1475 : return false;
1476 : }
1477 : std::vector<MSLane*>::const_iterator it1 = myLanes->begin();
1478 : std::vector<MSLane*>::const_reverse_iterator it2 = other->getLanes().rbegin();
1479 : do {
1480 307638 : if ((*it1)->getShape().reverse() != (*it2)->getShape()) {
1481 : return false;
1482 : }
1483 : it1++;
1484 : it2++;
1485 26 : } while (it1 != myLanes->end());
1486 :
1487 : return true;
1488 : }
1489 :
1490 :
1491 : void
1492 73983 : MSEdge::addWaiting(SUMOVehicle* vehicle) const {
1493 : #ifdef HAVE_FOX
1494 73983 : ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
1495 : #endif
1496 73983 : myWaiting.push_back(vehicle);
1497 73983 : }
1498 :
1499 :
1500 : void
1501 65577 : MSEdge::removeWaiting(const SUMOVehicle* vehicle) const {
1502 : #ifdef HAVE_FOX
1503 65577 : ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
1504 : #endif
1505 65577 : std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting.begin(), myWaiting.end(), vehicle);
1506 65577 : if (it != myWaiting.end()) {
1507 64966 : myWaiting.erase(it);
1508 : }
1509 65577 : }
1510 :
1511 :
1512 : SUMOVehicle*
1513 136563 : MSEdge::getWaitingVehicle(MSTransportable* transportable, const double position) const {
1514 : #ifdef HAVE_FOX
1515 136563 : ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
1516 : #endif
1517 136865 : for (SUMOVehicle* const vehicle : myWaiting) {
1518 25488 : if (transportable->isWaitingFor(vehicle)) {
1519 27682 : if (vehicle->isStoppedInRange(position, MSGlobals::gStopTolerance) ||
1520 2286 : (!vehicle->hasDeparted() &&
1521 2080 : (vehicle->getParameter().departProcedure == DepartDefinition::TRIGGERED ||
1522 83 : vehicle->getParameter().departProcedure == DepartDefinition::CONTAINER_TRIGGERED))) {
1523 : return vehicle;
1524 : }
1525 210 : if (!vehicle->isLineStop(position) && vehicle->allowsBoarding(transportable)) {
1526 228 : WRITE_WARNING((transportable->isPerson() ? "Person '" : "Container '")
1527 : + transportable->getID() + "' at edge '" + getID() + "' position " + toString(position) + " cannot use waiting vehicle '"
1528 : + vehicle->getID() + "' at position " + toString(vehicle->getPositionOnLane()) + " because it is too far away.");
1529 : }
1530 : }
1531 : }
1532 : return nullptr;
1533 : }
1534 :
1535 : std::vector<const SUMOVehicle*>
1536 144438 : MSEdge::getVehicles() const {
1537 : std::vector<const SUMOVehicle*> result;
1538 144438 : if (MSGlobals::gUseMesoSim) {
1539 220 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1540 122 : std::vector<const MEVehicle*> segmentVehs = segment->getVehicles();
1541 122 : result.insert(result.end(), segmentVehs.begin(), segmentVehs.end());
1542 122 : }
1543 : } else {
1544 386648 : for (MSLane* lane : getLanes()) {
1545 830595 : for (auto veh : lane->getVehiclesSecure()) {
1546 588336 : result.push_back(veh);
1547 : }
1548 242259 : lane->releaseVehicles();
1549 : }
1550 : }
1551 144438 : return result;
1552 0 : }
1553 :
1554 : int
1555 627109 : MSEdge::getNumDrivingLanes() const {
1556 : int result = 0;
1557 627109 : SVCPermissions filter = SVCAll;
1558 627109 : if ((myCombinedPermissions & ~(SVC_PEDESTRIAN | SVC_WHEELCHAIR)) != 0) {
1559 : filter = ~(SVC_PEDESTRIAN | SVC_WHEELCHAIR);
1560 3862 : } else if ((myCombinedPermissions & (SVC_PEDESTRIAN | SVC_WHEELCHAIR)) != 0) {
1561 : // filter out green verge
1562 : filter = (SVC_PEDESTRIAN | SVC_WHEELCHAIR);
1563 : }
1564 1413053 : for (const MSLane* const l : *myLanes) {
1565 785944 : if ((l->getPermissions() & filter) != 0) {
1566 695586 : result++;
1567 : }
1568 : }
1569 627109 : return result;
1570 : }
1571 :
1572 : int
1573 399 : MSEdge::getVehicleNumber() const {
1574 399 : return (int)getVehicles().size();
1575 : }
1576 :
1577 :
1578 : bool
1579 0 : MSEdge::isEmpty() const {
1580 : /// more efficient than retrieving vehicle number
1581 0 : if (MSGlobals::gUseMesoSim) {
1582 0 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1583 0 : if (segment->getCarNumber() > 0) {
1584 : return false;
1585 : }
1586 : }
1587 : } else {
1588 0 : for (MSLane* lane : getLanes()) {
1589 0 : if (lane->getVehicleNumber() > 0) {
1590 : return false;
1591 : }
1592 : }
1593 : }
1594 : return true;
1595 : }
1596 :
1597 :
1598 : double
1599 14 : MSEdge::getWaitingSeconds() const {
1600 : double wtime = 0;
1601 14 : if (MSGlobals::gUseMesoSim) {
1602 12 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1603 9 : wtime += segment->getWaitingSeconds();
1604 : }
1605 : } else {
1606 34 : for (MSLane* lane : getLanes()) {
1607 23 : wtime += lane->getWaitingSeconds();
1608 : }
1609 : }
1610 14 : return wtime;
1611 : }
1612 :
1613 :
1614 : double
1615 22 : MSEdge::getOccupancy() const {
1616 22 : if (myLanes->size() == 0) {
1617 : return 0;
1618 : }
1619 22 : if (MSGlobals::gUseMesoSim) {
1620 : /// @note MESegment only tracks brutto occupancy so we compute this from sratch
1621 : double sum = 0;
1622 8 : for (const SUMOVehicle* veh : getVehicles()) {
1623 4 : sum += dynamic_cast<const MEVehicle*>(veh)->getVehicleType().getLength();
1624 4 : }
1625 4 : return sum / (myLength * (double)myLanes->size());
1626 : } else {
1627 : double sum = 0;
1628 48 : for (auto lane : getLanes()) {
1629 30 : sum += lane->getNettoOccupancy();
1630 : }
1631 18 : return sum / (double)myLanes->size();
1632 : }
1633 : }
1634 :
1635 :
1636 : double
1637 0 : MSEdge::getFlow() const {
1638 0 : if (myLanes->size() == 0) {
1639 : return 0;
1640 : }
1641 : double flow = 0;
1642 0 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1643 0 : flow += (double) segment->getCarNumber() * segment->getMeanSpeed();
1644 : }
1645 0 : return 3600 * flow / (*myLanes)[0]->getLength();
1646 : }
1647 :
1648 :
1649 : double
1650 0 : MSEdge::getBruttoOccupancy() const {
1651 0 : if (myLanes->size() == 0) {
1652 : return 0;
1653 : }
1654 : double occ = 0;
1655 0 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1656 0 : occ += segment->getBruttoOccupancy();
1657 : }
1658 0 : return occ / (*myLanes)[0]->getLength() / (double)(myLanes->size());
1659 : }
1660 :
1661 : double
1662 4240 : MSEdge::getTravelTimeAggregated(const MSEdge* const edge, const SUMOVehicle* const veh, double /*time*/) {
1663 4240 : return edge->getLength() / MIN2(MSRoutingEngine::getAssumedSpeed(edge, veh), veh->getMaxSpeed());
1664 : }
1665 :
1666 :
1667 : void
1668 2098 : MSEdge::inferEdgeType() {
1669 : // @note must be called after closeBuilding() to ensure successors and
1670 : // predecessors are set
1671 2098 : if (isInternal() && myEdgeType == "") {
1672 1274 : const std::string typeBefore = getNormalBefore()->getEdgeType();
1673 1274 : if (typeBefore != "") {
1674 694 : const std::string typeAfter = getNormalSuccessor()->getEdgeType();
1675 694 : if (typeBefore == typeAfter) {
1676 : myEdgeType = typeBefore;
1677 314 : } else if (typeAfter != "") {
1678 68 : MSNet* net = MSNet::getInstance();
1679 68 : auto resBefore = net->getRestrictions(typeBefore);
1680 68 : auto resAfter = net->getRestrictions(typeAfter);
1681 68 : if (resBefore != nullptr && resAfter != nullptr) {
1682 : // create new restrictions for this type-combination
1683 96 : myEdgeType = typeBefore + "|" + typeAfter;
1684 48 : if (net->getRestrictions(myEdgeType) == nullptr) {
1685 48 : for (const auto& item : *resBefore) {
1686 24 : const SUMOVehicleClass svc = item.first;
1687 24 : const double speed = item.second;
1688 : const auto it = (*resAfter).find(svc);
1689 24 : if (it != (*resAfter).end()) {
1690 24 : const double speed2 = it->second;
1691 24 : const double newSpeed = (MSNet::getInstance()->hasJunctionHigherSpeeds()
1692 24 : ? MAX2(speed, speed2) : (speed + speed2) / 2);
1693 24 : net->addRestriction(myEdgeType, svc, newSpeed);
1694 : }
1695 : }
1696 : }
1697 : }
1698 : }
1699 : }
1700 : }
1701 2098 : }
1702 :
1703 :
1704 : double
1705 2178 : MSEdge::getDistanceAt(double pos) const {
1706 : // negative values of myDistances indicate descending kilometrage
1707 2178 : return fabs(myDistance + pos);
1708 : }
1709 :
1710 :
1711 : bool
1712 1350 : MSEdge::hasTransientPermissions() const {
1713 1350 : return myHaveTransientPermissions;
1714 : }
1715 :
1716 :
1717 : std::pair<double, SUMOTime>
1718 594285590 : MSEdge::getLastBlocked(int index) const {
1719 594285590 : if (myLaneChanger != nullptr) {
1720 594285590 : return myLaneChanger->getLastBlocked(index);
1721 : }
1722 0 : return std::make_pair(-1, -1);
1723 : }
1724 :
1725 :
1726 : double
1727 2855 : MSEdge::getPreference(const SUMOVTypeParameter& pars) const {
1728 5710 : return MSNet::getInstance()->getPreference(getRoutingType(), pars);
1729 : }
1730 :
1731 :
1732 : void
1733 7032 : MSEdge::clearState() {
1734 : myPersons.clear();
1735 : myContainers.clear();
1736 : myWaiting.clear();
1737 7032 : }
1738 :
1739 :
1740 : const std::map<const MEVehicle*, std::pair<double, int> >&
1741 1084903 : MSEdge::getMesoPositions() const {
1742 : assert(MSGlobals::gUseMesoSim);
1743 1084903 : if (myLastCacheUpdate < SIMSTEP) {
1744 1028041 : myLastCacheUpdate = SIMSTEP;
1745 : auto old = std::move(myCachedMesoPos);
1746 : myCachedMesoPos.clear(); // moved-from map is valid-but-unspecified; make it defined-empty
1747 : int laneIndex = 0;
1748 1028041 : const double now = SIMTIME;
1749 2445970 : for (std::vector<MSLane*>::const_iterator msl = myLanes->begin(); msl != myLanes->end(); ++msl, ++laneIndex) {
1750 : // go through the vehicles
1751 : double segmentOffset = 0; // offset at start of current segment
1752 1417929 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this);
1753 3046255 : segment != nullptr; segment = segment->getNextSegment()) {
1754 : const double segLength = segment->getLength();
1755 1628326 : const double lanesCovered = segment->numQueues() == 1 ? std::round(segment->getCapacity() / segLength) : 1.;
1756 1628326 : if (laneIndex < segment->numQueues()) {
1757 : // make a copy so we don't have to worry about synchronization
1758 1267800 : std::vector<MEVehicle*> queue = segment->getQueue(laneIndex);
1759 1267800 : const int queueSize = (int)queue.size();
1760 : SUMOTime earliestExitTime = segment->getQueueBlockTime(laneIndex);
1761 : int overlap = 0;
1762 : double prevPos = std::numeric_limits<double>::max();
1763 3145801 : for (int i = 0; i < queueSize; ++i) {
1764 1878001 : const MEVehicle* const veh = queue[queueSize - i - 1];
1765 : earliestExitTime = MAX2(earliestExitTime, veh->getEventTime());
1766 1878001 : const double vehLength = veh->getVehicleType().getLengthWithGap();
1767 1878001 : double maxPos = segmentOffset + segLength;
1768 : auto it = old.find(veh);
1769 1878001 : const double oldPos = it != old.end() ? it->second.first : 0.; // store the old position to prevent backwards moving vehicles
1770 1878001 : if (i > 0) {
1771 1715095 : earliestExitTime += segment->getMinTauWithVehLength(vehLength, veh->getVehicleType().getCarFollowModel().getHeadwayTime());
1772 1715095 : maxPos = MIN2(maxPos, prevPos - vehLength / lanesCovered);
1773 : }
1774 1878001 : const double entry = veh->getLastEntryTimeSeconds();
1775 : assert(STEPS2TIME(earliestExitTime) > entry);
1776 1878001 : const double pos = MAX2(MIN2(segmentOffset + segLength * (now - entry) / (STEPS2TIME(earliestExitTime) - entry), maxPos), oldPos);
1777 : // check if we overlap with the previous vehicle such that the gui has the chance to add some lateral offset
1778 1878001 : if (overlap == 0 && prevPos - pos < vehLength) {
1779 548036 : overlap = lanesCovered > 1. ? -3 : -1;
1780 : } else {
1781 : overlap = 0;
1782 : }
1783 1878001 : myCachedMesoPos[veh] = std::make_pair(pos, overlap);
1784 : prevPos = pos;
1785 : }
1786 1267800 : }
1787 1628326 : segmentOffset += segLength;
1788 : }
1789 : }
1790 : }
1791 1084903 : return myCachedMesoPos;
1792 : }
1793 :
1794 :
1795 : /****************************************************************************/
|