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 1978442 : 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 1978442 : double distance) :
75 1978442 : Named(id), myNumericalID(numericalID), myLanes(nullptr),
76 1978442 : myLaneChanger(nullptr), myFunction(function), myVaporizationRequests(0),
77 1978442 : myLastFailedInsertionTime(-1),
78 1978442 : myFromJunction(nullptr), myToJunction(nullptr),
79 1978442 : myHaveTransientPermissions(false),
80 1978442 : myOtherTazConnector(nullptr),
81 1978442 : myStreetName(streetName),
82 1978442 : myEdgeType(edgeType),
83 1978442 : myRoutingType(routingType),
84 1978442 : myPriority(priority),
85 1978442 : myDistance(distance),
86 1978442 : myWidth(0.),
87 1978442 : myLength(0.),
88 1978442 : myEmptyTraveltime(0.),
89 1978442 : myTimePenalty(0.),
90 1978442 : myAmDelayed(false),
91 1978442 : myAmRoundabout(false),
92 1978442 : myAmFringe(true),
93 3956884 : myBidiEdge(nullptr)
94 1978442 : { }
95 :
96 :
97 3410130 : MSEdge::~MSEdge() {
98 1962995 : delete myLaneChanger;
99 1962995 : delete myReversedRoutingEdge;
100 1962995 : delete myRailwayRoutingEdge;
101 13225105 : }
102 :
103 :
104 : void
105 1842435 : MSEdge::initialize(const std::vector<MSLane*>* lanes) {
106 : assert(lanes != 0);
107 1842435 : myLanes = std::shared_ptr<const std::vector<MSLane*> >(lanes);
108 1842435 : if (myFunction == SumoXMLEdgeFunc::CONNECTOR) {
109 70006 : myCombinedPermissions = SVCAll;
110 : }
111 3975913 : for (MSLane* const lane : *lanes) {
112 2133478 : lane->setRightSideOnEdge(myWidth, (int)mySublaneSides.size());
113 2133478 : MSLeaderInfo ahead(lane->getWidth());
114 4981821 : for (int j = 0; j < ahead.numSublanes(); ++j) {
115 2848343 : mySublaneSides.push_back(myWidth + j * MSGlobals::gLateralResolution);
116 : }
117 2133478 : myWidth += lane->getWidth();
118 2133478 : }
119 1842435 : }
120 :
121 :
122 1866094 : void MSEdge::recalcCache() {
123 1866094 : if (myLanes->empty()) {
124 : return;
125 : }
126 1866094 : myLength = myLanes->front()->getLength();
127 3732188 : myEmptyTraveltime = myLength / MAX2(getSpeedLimit(), NUMERICAL_EPS);
128 1866094 : if (isNormal() && (MSGlobals::gUseMesoSim || MSGlobals::gTLSPenalty > 0)) {
129 : SUMOTime minorPenalty = 0;
130 187404 : bool haveTLSPenalty = MSGlobals::gTLSPenalty > 0;
131 187404 : if (MSGlobals::gUseMesoSim) {
132 187289 : const MESegment::MesoEdgeType& edgeType = MSNet::getInstance()->getMesoType(getEdgeType());
133 187289 : minorPenalty = edgeType.minorPenalty;
134 187289 : haveTLSPenalty = edgeType.tlsPenalty > 0;
135 : }
136 187404 : if (haveTLSPenalty || minorPenalty > 0) {
137 : // add tls penalties to the minimum travel time
138 : SUMOTime minPenalty = -1;
139 2541 : for (const MSLane* const l : *myLanes) {
140 4084 : for (const MSLink* const link : l->getLinkCont()) {
141 2381 : if (link->getLane()->isWalkingArea() && link->getLaneBefore()->isNormal()) {
142 60 : continue;
143 : }
144 2321 : SUMOTime linkPenalty = link->isTLSControlled() ? link->getMesoTLSPenalty() : (link->havePriority() ? 0 : minorPenalty);
145 2321 : if (minPenalty == -1) {
146 : minPenalty = linkPenalty;
147 : } else {
148 : minPenalty = MIN2(minPenalty, linkPenalty);
149 : }
150 : }
151 : }
152 838 : if (minPenalty > 0) {
153 284 : myEmptyTraveltime += STEPS2TIME(minPenalty);
154 284 : myTimePenalty = STEPS2TIME(minPenalty);
155 : }
156 : }
157 1678690 : } 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 1678634 : } else if (isInternal() && MSGlobals::gUsingInternalLanes) {
170 976400 : const MSLink* link = myLanes->front()->getIncomingLanes()[0].viaLink;
171 976400 : if (!link->isTLSControlled() && !link->havePriority()) {
172 485592 : if (link->isTurnaround()) {
173 184298 : myEmptyTraveltime += MSGlobals::gTurnaroundPenalty;
174 184298 : myTimePenalty = MSGlobals::gTurnaroundPenalty;
175 : } else {
176 301294 : myEmptyTraveltime += MSGlobals::gMinorPenalty;
177 301294 : 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 1769899 : MSEdge::closeBuilding() {
209 3900424 : for (MSLane* const lane : *myLanes) {
210 5060463 : for (MSLink* const link : lane->getLinkCont()) {
211 2929938 : link->initParallelLinks();
212 : MSLane* const toL = link->getLane();
213 : MSLane* const viaL = link->getViaLane();
214 2929938 : if (toL != nullptr) {
215 : MSEdge& to = toL->getEdge();
216 2929938 : if (std::find(mySuccessors.begin(), mySuccessors.end(), &to) == mySuccessors.end()) {
217 2729341 : mySuccessors.push_back(&to);
218 5458682 : myViaSuccessors.push_back(std::make_pair(&to, (viaL == nullptr ? nullptr : &viaL->getEdge())));
219 : }
220 2929938 : if (std::find(to.myPredecessors.begin(), to.myPredecessors.end(), this) == to.myPredecessors.end()) {
221 2729341 : to.myPredecessors.push_back(this);
222 : }
223 2929938 : if (link->getDirection() != LinkDirection::TURN) {
224 2286292 : myAmFringe = false;
225 : }
226 : }
227 2929938 : if (viaL != nullptr) {
228 : MSEdge& to = viaL->getEdge();
229 1021827 : if (std::find(to.myPredecessors.begin(), to.myPredecessors.end(), this) == to.myPredecessors.end()) {
230 940872 : to.myPredecessors.push_back(this);
231 : }
232 : }
233 : }
234 2130525 : lane->checkBufferType();
235 : }
236 1769899 : std::sort(mySuccessors.begin(), mySuccessors.end(), by_id_sorter());
237 1769899 : rebuildAllowedLanes(true);
238 1769899 : recalcCache();
239 :
240 : // extend lookup table for sublane model after all edges are read
241 1769899 : if (myLanes->back()->getOpposite() != nullptr) {
242 8262 : MSLane* opposite = myLanes->back()->getOpposite();
243 8262 : MSLeaderInfo ahead(opposite->getWidth());
244 22542 : for (int j = 0; j < ahead.numSublanes(); ++j) {
245 14280 : mySublaneSides.push_back(myWidth + j * MSGlobals::gLateralResolution);
246 : }
247 8262 : }
248 1769899 : }
249 :
250 :
251 : void
252 1769891 : MSEdge::postLoadInitLaneChanger() {
253 1769891 : if (myLaneChanger != nullptr) {
254 443011 : myLaneChanger->postloadInitLC();
255 : }
256 1769891 : }
257 :
258 : void
259 1769899 : MSEdge::buildLaneChanger() {
260 1769899 : if (!myLanes->empty()) {
261 1769899 : const bool allowChanging = allowsLaneChanging();
262 1769899 : if (MSGlobals::gLateralResolution > 0) {
263 : // may always initiate sublane-change
264 183838 : if (!isInternal() || MSGlobals::gUsingInternalLanes) {
265 183647 : myLaneChanger = new MSLaneChangerSublane(myLanes.get(), allowChanging);
266 : }
267 : } else {
268 1586061 : if (MSGlobals::gLaneChangeDuration > 0) {
269 4981 : myLaneChanger = new MSLaneChanger(myLanes.get(), allowChanging);
270 1581080 : } else if (myLanes->size() > 1 || canChangeToOpposite()) {
271 254391 : myLaneChanger = new MSLaneChanger(myLanes.get(), allowChanging);
272 : }
273 : }
274 : }
275 1769899 : }
276 :
277 :
278 : bool
279 1769899 : MSEdge::allowsLaneChanging() const {
280 1769899 : 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 1484672 : for (const MSLane* const lane : *myLanes) {
284 1018394 : const MSLink* const link = lane->getLogicalPredecessorLane()->getLinkTo(lane);
285 : assert(link != nullptr);
286 : const LinkState state = link->getState();
287 463927 : if ((state == LINKSTATE_MINOR && lane->getBidiLane() == nullptr)
288 554893 : || state == LINKSTATE_EQUAL
289 554893 : || state == LINKSTATE_STOP
290 : || state == LINKSTATE_ALLWAY_STOP
291 1018394 : || state == LINKSTATE_DEADEND) {
292 : return false;
293 : }
294 : }
295 : }
296 : return true;
297 : }
298 :
299 :
300 : void
301 18816118 : MSEdge::addToAllowed(const SVCPermissions permissions, std::shared_ptr<const std::vector<MSLane*> > allowedLanes, AllowedLanesCont& laneCont) const {
302 18816118 : if (!allowedLanes->empty()) {
303 : // recheck whether we had this list to save memory
304 18550176 : for (auto& allowed : laneCont) {
305 17512280 : if (*allowed.second == *allowedLanes) {
306 13180001 : allowed.first |= permissions;
307 : return;
308 : }
309 : }
310 1037896 : laneCont.push_back(std::make_pair(permissions, allowedLanes));
311 : }
312 : }
313 :
314 :
315 : SVCPermissions
316 2963239 : MSEdge::getMesoPermissions(SVCPermissions p, SVCPermissions ignoreIgnored) {
317 2963239 : SVCPermissions ignored = myMesoIgnoredVClasses & ~ignoreIgnored;
318 2963239 : return (p | ignored) == ignored ? 0 : p;
319 : }
320 :
321 :
322 : void
323 1771520 : MSEdge::rebuildAllowedLanes(const bool onInit, bool updateVehicles) {
324 : // rebuild myMinimumPermissions and myCombinedPermissions
325 1771520 : myMinimumPermissions = SVCAll;
326 1771520 : myCombinedPermissions = 0;
327 : bool lanesChangedPermission = false;
328 3904186 : 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 2132666 : SVCPermissions allow = getMesoPermissions(lane->getPermissions(), SVC_PEDESTRIAN);
333 2132666 : myMinimumPermissions &= allow;
334 2132666 : myCombinedPermissions |= allow;
335 2132666 : lanesChangedPermission |= lane->hadPermissionChanges();
336 : }
337 1771520 : if (!onInit && !myHaveTransientPermissions && lanesChangedPermission) {
338 1145 : myHaveTransientPermissions = true;
339 : // backup original structures when first needed
340 1145 : myOrigAllowed = myAllowed;
341 : myOrigAllowedTargets = myAllowedTargets;
342 : myOrigClassesViaSuccessorMap = myClassesViaSuccessorMap;
343 : }
344 : // rebuild myAllowed
345 : myAllowed.clear();
346 1771520 : if (myCombinedPermissions != myMinimumPermissions) {
347 169337 : myAllowed.push_back(std::make_pair(SVC_IGNORING, myLanes));
348 5757458 : for (SVCPermissions vclass = SVC_PRIVATE; vclass <= SUMOVehicleClass_MAX; vclass *= 2) {
349 5588121 : if ((myCombinedPermissions & vclass) == vclass) {
350 : std::shared_ptr<std::vector<MSLane*> > allowedLanes = std::make_shared<std::vector<MSLane*> >();
351 11859747 : for (MSLane* const lane : *myLanes) {
352 8029117 : if (lane->allowsVehicleClass((SUMOVehicleClass)vclass)) {
353 4052589 : allowedLanes->push_back(lane);
354 : }
355 : }
356 7661260 : addToAllowed(vclass, allowedLanes, myAllowed);
357 : }
358 : }
359 : }
360 1771520 : if (onInit) {
361 1769899 : myOriginalMinimumPermissions = myMinimumPermissions;
362 1769899 : myOriginalCombinedPermissions = myCombinedPermissions;
363 : } else {
364 1621 : rebuildAllowedTargets(updateVehicles);
365 4065 : for (MSEdge* pred : myPredecessors) {
366 2444 : if (myHaveTransientPermissions && !pred->myHaveTransientPermissions) {
367 1561 : pred->myOrigAllowed = pred->myAllowed;
368 : pred->myOrigAllowedTargets = pred->myAllowedTargets;
369 : pred->myOrigClassesViaSuccessorMap = pred->myClassesViaSuccessorMap;
370 1561 : pred->myHaveTransientPermissions = true;
371 : }
372 2444 : pred->rebuildAllowedTargets(updateVehicles);
373 : }
374 1621 : 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 1771520 : }
381 :
382 :
383 : void
384 1775100 : MSEdge::rebuildAllowedTargets(const bool updateVehicles) {
385 : myAllowedTargets.clear();
386 4512213 : 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 6241385 : for (MSLane* const lane : *myLanes) {
391 : SVCPermissions combinedTargetPermissions = 0;
392 9951132 : for (const MSLink* const link : lane->getLinkCont()) {
393 6446860 : if (&link->getLane()->getEdge() == target) {
394 2938785 : allLanes->push_back(lane);
395 2938785 : combinedTargetPermissions |= link->getLane()->getPermissions();
396 2938785 : if (link->getViaLane() != nullptr &&
397 1024336 : ((lane->getPermissions() & link->getLane()->getPermissions()) != link->getViaLane()->getPermissions())) {
398 : // custom connection permissions
399 : universalMap = false;
400 : }
401 : }
402 : }
403 3504272 : if (combinedTargetPermissions == 0 || (lane->getPermissions() & combinedTargetPermissions) != lane->getPermissions()) {
404 : universalMap = false;
405 : }
406 : }
407 2737113 : if (universalMap) {
408 2225019 : if (myAllowed.empty()) {
409 : // we have no lane specific permissions
410 4381652 : myAllowedTargets[target].push_back(std::make_pair(myMinimumPermissions, myLanes));
411 : } else {
412 134540 : for (const auto& i : myAllowed) {
413 301041 : addToAllowed(i.first, i.second, myAllowedTargets[target]);
414 : }
415 : }
416 : } else {
417 1024188 : addToAllowed(SVC_IGNORING, allLanes, myAllowedTargets[target]);
418 : // compute the vclass specific mapping
419 17411196 : for (SVCPermissions vclass = SVC_PRIVATE; vclass <= SUMOVehicleClass_MAX; vclass *= 2) {
420 16899102 : if ((myCombinedPermissions & vclass) == vclass) {
421 : std::shared_ptr<std::vector<MSLane*> > allowedLanes = std::make_shared<std::vector<MSLane*> >();
422 44687711 : for (MSLane* const lane : *myLanes) {
423 30314664 : if (lane->allowsVehicleClass((SUMOVehicleClass)vclass)) {
424 58055958 : for (const MSLink* const link : lane->getLinkCont()) {
425 37960398 : if (link->getLane()->allowsVehicleClass((SUMOVehicleClass)vclass) && &link->getLane()->getEdge() == target && (link->getViaLane() == nullptr || link->getViaLane()->allowsVehicleClass((SUMOVehicleClass)vclass))) {
426 10134461 : allowedLanes->push_back(lane);
427 : }
428 : }
429 : }
430 : }
431 43119141 : addToAllowed(vclass, allowedLanes, myAllowedTargets[target]);
432 : }
433 : }
434 : }
435 : }
436 1775100 : if (updateVehicles) {
437 3285 : for (const MSLane* const lane : *myLanes) {
438 1906 : const MSLane::VehCont& vehs = lane->getVehiclesSecure();
439 4337 : for (MSVehicle* veh : vehs) {
440 2431 : veh->updateBestLanes(true);
441 : }
442 1906 : lane->releaseVehicles();
443 : }
444 : }
445 : myClassesSuccessorMap.clear();
446 1775100 : }
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 84971336 : MSEdge::parallelLane(const MSLane* const lane, int offset, bool includeOpposite) const {
464 84971336 : const int resultIndex = lane->getIndex() + offset;
465 84971336 : if (resultIndex >= getNumLanes() && includeOpposite) {
466 20755474 : const MSEdge* opposite = getOppositeEdge();
467 20755474 : if (opposite != nullptr && resultIndex < getNumLanes() + opposite->getNumLanes()) {
468 1378500 : return opposite->getLanes()[opposite->getNumLanes() + getNumLanes() - resultIndex - 1];
469 : }
470 : return nullptr;
471 64215862 : } else if (resultIndex >= (int)myLanes->size() || resultIndex < 0) {
472 : return nullptr;
473 : } else {
474 39106847 : return (*myLanes)[resultIndex];
475 : }
476 : }
477 :
478 :
479 : const std::vector<MSLane*>*
480 76675951 : MSEdge::allowedLanes(const MSEdge& destination, SUMOVehicleClass vclass, bool ignoreTransientPermissions) const {
481 76675951 : const auto& targets = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigAllowedTargets : myAllowedTargets;
482 : AllowedLanesByTarget::const_iterator i = targets.find(&destination);
483 76675951 : if (i != targets.end()) {
484 76651909 : for (const auto& allowed : i->second) {
485 76527696 : 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 274811367 : MSEdge::allowedLanes(SUMOVehicleClass vclass) const {
496 274811367 : if ((myMinimumPermissions & vclass) == vclass) {
497 43861257 : return myLanes.get();
498 : } else {
499 230950110 : if ((myCombinedPermissions & vclass) == vclass) {
500 461896729 : for (const auto& allowed : myAllowed) {
501 461896729 : 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 2176758750 : MSEdge::allowedLanes(SUMOVehicleClass vclass, bool ignoreTransientPermissions) const {
513 2176758750 : const SVCPermissions& minP = ignoreTransientPermissions ? myOriginalMinimumPermissions : myMinimumPermissions;
514 2176758750 : if ((minP & vclass) == vclass) {
515 639582310 : return myLanes.get();
516 : } else {
517 1537176440 : const SVCPermissions comP = ignoreTransientPermissions ? myOriginalCombinedPermissions : myCombinedPermissions;
518 1537176440 : if ((comP & vclass) == vclass) {
519 1537176368 : const AllowedLanesCont& allowedCont = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigAllowed : myAllowed;
520 3080050860 : for (const auto& allowed : allowedCont) {
521 3080050860 : 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 217835604 : MSEdge::getFreeLane(const std::vector<MSLane*>* allowed, const SUMOVehicleClass vclass, double departPos) const {
548 217835604 : if (allowed == nullptr) {
549 184213842 : allowed = allowedLanes(vclass);
550 : }
551 : MSLane* res = nullptr;
552 184213842 : if (allowed != nullptr) {
553 : double largestGap = 0;
554 : MSLane* resByGap = nullptr;
555 : double leastOccupancy = std::numeric_limits<double>::max();
556 460387664 : for (std::vector<MSLane*>::const_iterator i = allowed->begin(); i != allowed->end(); ++i) {
557 242554059 : const double occupancy = (*i)->getBruttoOccupancy();
558 242554059 : if (occupancy < leastOccupancy) {
559 225831471 : res = (*i);
560 : leastOccupancy = occupancy;
561 : }
562 242554059 : const MSVehicle* last = (*i)->getLastFullVehicle();
563 242554059 : const double lastGap = (last != nullptr ? last->getPositionOnLane() : myLength) - departPos;
564 242554059 : if (lastGap > largestGap) {
565 : largestGap = lastGap;
566 58240073 : resByGap = (*i);
567 : }
568 : }
569 217833605 : 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 217835604 : return res;
575 : }
576 :
577 :
578 : MSLane*
579 839357071 : MSEdge::getProbableLane(const std::vector<MSLane*>* allowed, const SUMOVehicleClass vclass, double departPos, double maxSpeed) const {
580 839357071 : 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 1684774665 : for (std::vector<MSLane*>::const_iterator i = allowed->begin(); i != allowed->end(); ++i, aIndex++) {
591 845417594 : const double occupancy = (*i)->getBruttoOccupancy();
592 845417594 : if (occupancy < leastOccupancy) {
593 844692559 : res = (*i);
594 : leastOccupancy = occupancy;
595 : }
596 845417594 : const MSVehicle* last = (*i)->getLastFullVehicle();
597 845417594 : double lastGap = (last != nullptr ? last->getPositionOnLane() : myLength) - departPos;
598 : // never insert to the left of a vehicle with a larger speedFactor
599 845417594 : if (lastGap > largestGap && maxSpeed >= largestSpeed) {
600 : largestGap = lastGap;
601 800212708 : resByGap = (*i);
602 : }
603 845417594 : if (last != nullptr) {
604 843172386 : largestSpeed = MAX2(largestSpeed, getVehicleMaxSpeed(last));
605 : }
606 : }
607 839357071 : 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 839357071 : return res;
613 : }
614 :
615 :
616 : double
617 1058304737 : MSEdge::getDepartPosBound(const MSVehicle& veh, bool upper) const {
618 1058304737 : const SUMOVehicleParameter& pars = veh.getParameter();
619 : double pos = getLength();
620 : // determine the position
621 1058304737 : switch (pars.departPosProcedure) {
622 10036639 : case DepartPosDefinition::GIVEN:
623 10036639 : pos = pars.departPos;
624 10036639 : 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 831230950 : case DepartPosDefinition::BASE:
651 : case DepartPosDefinition::DEFAULT:
652 831230950 : if (!upper) {
653 : pos = 0;
654 : }
655 : break;
656 28 : default:
657 28 : pos = MIN2(pos, veh.getVehicleType().getLength());
658 : break;
659 : }
660 1058304737 : 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 1763669542 : MSEdge::getDepartLane(MSVehicle& veh) const {
678 1763669542 : DepartLaneDefinition dld = veh.getParameter().departLaneProcedure;
679 1763669542 : int departLane = veh.getParameter().departLane;
680 1763669542 : if (dld == DepartLaneDefinition::DEFAULT) {
681 1323130942 : dld = myDefaultDepartLaneDefinition;
682 1323130942 : departLane = myDefaultDepartLane;
683 : }
684 1763669542 : switch (dld) {
685 125051025 : case DepartLaneDefinition::GIVEN:
686 125051025 : if ((int) myLanes->size() <= departLane || !(*myLanes)[departLane]->allowsVehicleClass(veh.getVClass())) {
687 66 : return nullptr;
688 : }
689 125050959 : return (*myLanes)[departLane];
690 90567744 : case DepartLaneDefinition::RANDOM:
691 181135488 : return RandHelper::getRandomFrom(*allowedLanes(veh.getVehicleType().getVehicleClass()));
692 184111253 : case DepartLaneDefinition::FREE:
693 184111253 : 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 867809079 : case DepartLaneDefinition::BEST_FREE:
701 : case DepartLaneDefinition::BEST_PROB: {
702 867809079 : veh.updateBestLanes(false, myLanes->front());
703 867809079 : const std::vector<MSVehicle::LaneQ>& bl = veh.getBestLanes();
704 : double bestLength = -1;
705 2562150826 : for (std::vector<MSVehicle::LaneQ>::const_iterator i = bl.begin(); i != bl.end(); ++i) {
706 1694341747 : 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 867809079 : if (bestLength > BEST_LANE_LOOKAHEAD) {
715 1413161 : departPos = getDepartPosBound(veh);
716 1413161 : bestLength = MIN2(bestLength - departPos, BEST_LANE_LOOKAHEAD);
717 : }
718 867809079 : std::vector<MSLane*>* bestLanes = new std::vector<MSLane*>();
719 2562150826 : for (std::vector<MSVehicle::LaneQ>::const_iterator i = bl.begin(); i != bl.end(); ++i) {
720 1694341747 : if (((*i).length - departPos) >= bestLength) {
721 876487958 : 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 876487946 : } else if ((*i).lane->allowsVehicleClass(veh.getVClass())) {
728 876055996 : bestLanes->push_back((*i).lane);
729 : }
730 : }
731 : }
732 : MSLane* ret = nullptr;
733 867809079 : if (veh.getParameter().departLaneProcedure == DepartLaneDefinition::BEST_FREE) {
734 28452008 : ret = getFreeLane(bestLanes, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
735 : } else {
736 839357071 : ret = getProbableLane(bestLanes, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false), getVehicleMaxSpeed(&veh));
737 : }
738 867809079 : delete bestLanes;
739 867809079 : return ret;
740 : }
741 491159197 : case DepartLaneDefinition::DEFAULT:
742 : case DepartLaneDefinition::FIRST_ALLOWED:
743 491159197 : 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 491902802 : MSEdge::getFirstAllowed(SUMOVehicleClass vClass, bool defaultFirst, int routingMode) const {
756 499059623 : for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
757 498190723 : if ((*i)->allowsVehicleClass(vClass, routingMode)) {
758 491033902 : return *i;
759 : }
760 : }
761 868900 : return defaultFirst && !myLanes->empty() ? myLanes->front() : nullptr;
762 : }
763 :
764 :
765 : bool
766 2761973208 : MSEdge::validateDepartSpeed(SUMOVehicle& v) const {
767 2761973208 : const SUMOVehicleParameter& pars = v.getParameter();
768 2761973208 : const MSVehicleType& type = v.getVehicleType();
769 2761973208 : if (pars.departSpeedProcedure == DepartSpeedDefinition::GIVEN) {
770 : // departSpeed could have been rounded down in the output
771 150719157 : double vMax = getVehicleMaxSpeed(&v) + SPEED_EPS;
772 150719157 : if (pars.departSpeed > vMax) {
773 : // check departLane (getVehicleMaxSpeed checks lane 0)
774 19611 : MSLane* departLane = MSGlobals::gMesoNet ? getDepartLaneMeso(v) : getDepartLane(dynamic_cast<MSVehicle&>(v));
775 19611 : if (departLane != nullptr) {
776 19611 : vMax = departLane->getVehicleMaxSpeed(&v);
777 19611 : 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 19611 : vMax += SPEED_EPS;
783 19611 : if (pars.departSpeed > vMax) {
784 19597 : if (type.getSpeedFactor().getParameter(1) > 0.) {
785 39156 : v.setChosenSpeedFactor(type.computeChosenSpeedDeviation(pars.speedFactor, nullptr, pars.departSpeed / MIN2(getSpeedLimit(), type.getDesiredMaxSpeed() - SPEED_EPS)));
786 19578 : 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 2762434596 : 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 2762415810 : if (isVaporizing() || isTazConnector()
806 5524385107 : || v.getRouteValidity(true, checkOnly) != MSBaseVehicle::ROUTE_VALID) {
807 484152 : return checkOnly;
808 : }
809 2761950265 : const SUMOVehicleParameter& pars = v.getParameter();
810 2761950265 : 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 2761950258 : if (MSGlobals::gUseMesoSim) {
820 524004075 : if (!forceCheck && myLastFailedInsertionTime == time) {
821 : return false;
822 : }
823 : double pos = 0.0;
824 83905867 : switch (pars.departPosProcedure) {
825 595636 : case DepartPosDefinition::GIVEN:
826 595636 : if (pars.departPos >= 0.) {
827 : pos = pars.departPos;
828 : } else {
829 8393 : pos = pars.departPos + getLength();
830 : }
831 595636 : 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 83905867 : MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this, pos);
846 : MEVehicle* veh = static_cast<MEVehicle*>(&v);
847 : int qIdx;
848 83905867 : if (pars.departPosProcedure == DepartPosDefinition::FREE) {
849 486990 : while (segment != nullptr && !result) {
850 448198 : if (checkOnly) {
851 6 : result = segment->hasSpaceFor(veh, time, qIdx, true) == time;
852 : } else {
853 448192 : result = segment->initialise(veh, time);
854 : }
855 : segment = segment->getNextSegment();
856 : }
857 : } else {
858 83867075 : if (checkOnly) {
859 73875974 : result = segment->hasSpaceFor(veh, time, qIdx, true) == time;
860 : } else {
861 9991101 : result = segment->initialise(veh, time);
862 : }
863 : }
864 83905865 : return result;
865 : }
866 2237946183 : if (checkOnly) {
867 617014832 : DepartLaneDefinition dld = v.getParameter().departLaneProcedure;
868 617014832 : if (dld == DepartLaneDefinition::DEFAULT) {
869 561253138 : dld = myDefaultDepartLaneDefinition;
870 : }
871 617014832 : switch (dld) {
872 80181474 : case DepartLaneDefinition::GIVEN:
873 : case DepartLaneDefinition::DEFAULT:
874 : case DepartLaneDefinition::FIRST_ALLOWED: {
875 80181474 : MSLane* insertionLane = getDepartLane(static_cast<MSVehicle&>(v));
876 80181474 : 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 79749524 : const double occupancy = insertionLane->getBruttoOccupancy();
882 79749524 : return (occupancy == 0 || occupancy * myLength + v.getVehicleType().getLengthWithGap() <= myLength ||
883 6025948 : v.getParameter().departProcedure == DepartDefinition::SPLIT);
884 : }
885 536833358 : default:
886 556316459 : for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
887 547164929 : const double occupancy = (*i)->getBruttoOccupancy();
888 547164929 : if (occupancy == 0 || occupancy * myLength + v.getVehicleType().getLengthWithGap() <= myLength ||
889 19483101 : v.getParameter().departProcedure == DepartDefinition::SPLIT) {
890 : return true;
891 : }
892 : }
893 : }
894 : return false;
895 : }
896 1620931351 : MSLane* insertionLane = getDepartLane(static_cast<MSVehicle&>(v));
897 1620931351 : if (insertionLane == nullptr) {
898 : return false;
899 : }
900 :
901 1620062451 : if (!forceCheck) {
902 1620062274 : 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 13533919 : bool success = insertionLane->insertVehicle(static_cast<MSVehicle&>(v));
914 :
915 13533918 : if (!success) {
916 : // constraints may enforce explicit re-ordering so we need to try other vehicles after failure
917 20137066 : if (!insertionLane->hasParameter("insertionOrder" + v.getID())) {
918 10068386 : myFailedInsertionMemory.insert(insertionLane->getIndex());
919 : }
920 : }
921 : return success;
922 : }
923 :
924 :
925 : void
926 42617979 : MSEdge::changeLanes(SUMOTime t) const {
927 42617979 : if (myLaneChanger != nullptr) {
928 42617979 : myLaneChanger->laneChange(t);
929 : }
930 42617979 : }
931 :
932 :
933 : const MSEdge*
934 5304146 : MSEdge::getInternalFollowingEdge(const MSEdge* followerAfterInternal, SUMOVehicleClass vClass) const {
935 : //@todo to be optimized
936 6099464 : for (const MSLane* const l : *myLanes) {
937 8573386 : for (const MSLink* const link : l->getLinkCont()) {
938 7778068 : if (&link->getLane()->getEdge() == followerAfterInternal) {
939 5037956 : if (link->getViaLane() != nullptr) {
940 4004823 : if (link->getViaLane()->allowsVehicleClass(vClass)) {
941 3966318 : return &link->getViaLane()->getEdge();
942 : } else {
943 38505 : continue;
944 : }
945 : } else {
946 : return nullptr; // network without internal links
947 : }
948 : }
949 : }
950 : }
951 : return nullptr;
952 : }
953 :
954 :
955 : double
956 1246322 : MSEdge::getInternalFollowingLengthTo(const MSEdge* followerAfterInternal, SUMOVehicleClass vClass) const {
957 : assert(followerAfterInternal != 0);
958 : assert(!followerAfterInternal->isInternal());
959 : double dist = 0.;
960 1246322 : const MSEdge* edge = getInternalFollowingEdge(followerAfterInternal, vClass);
961 : // Take into account non-internal lengths until next non-internal edge
962 2301854 : while (edge != nullptr && edge->isInternal()) {
963 1055532 : dist += edge->getLength();
964 1055532 : edge = edge->getInternalFollowingEdge(followerAfterInternal, vClass);
965 : }
966 1246322 : return dist;
967 : }
968 :
969 :
970 : const MSEdge*
971 148950 : MSEdge::getNormalBefore() const {
972 : const MSEdge* result = this;
973 158046 : while (result->isInternal() && MSGlobals::gUsingInternalLanes) {
974 : assert(result->getPredecessors().size() == 1);
975 9096 : result = result->getPredecessors().front();
976 : }
977 148950 : return result;
978 : }
979 :
980 : const MSEdge*
981 6166407 : MSEdge::getNormalSuccessor() const {
982 : const MSEdge* result = this;
983 11554538 : while (result->isInternal()) {
984 : assert(result->getSuccessors().size() == 1);
985 5388131 : result = result->getSuccessors().front();
986 : }
987 6166407 : return result;
988 : }
989 :
990 : double
991 191880602 : MSEdge::getMeanSpeed() const {
992 : double v = 0;
993 : double totalNumVehs = 0;
994 191880602 : if (MSGlobals::gUseMesoSim) {
995 253521008 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
996 : const int numVehs = segment->getCarNumber();
997 185783770 : if (numVehs > 0) {
998 25233076 : v += numVehs * segment->getMeanSpeed();
999 25233076 : totalNumVehs += numVehs;
1000 : }
1001 : }
1002 67737238 : if (totalNumVehs == 0) {
1003 58583881 : return getLength() / myEmptyTraveltime; // may include tls-penalty
1004 : }
1005 : } else {
1006 285440827 : for (const MSLane* const lane : *myLanes) {
1007 : int numVehs = lane->getVehicleNumber();
1008 161297463 : if (numVehs == 0) {
1009 : // take speed limit but with lowest possible weight
1010 : numVehs = 1;
1011 : }
1012 161297463 : v += numVehs * lane->getMeanSpeed();
1013 161297463 : totalNumVehs += numVehs;
1014 : }
1015 124143364 : if (myBidiEdge != nullptr) {
1016 8758516 : for (const MSLane* const lane : myBidiEdge->getLanes()) {
1017 4537796 : if (lane->getVehicleNumber() > 0) {
1018 : // do not route across edges which are already occupied in reverse direction
1019 : return 0;
1020 : }
1021 : }
1022 : }
1023 123827284 : if (totalNumVehs == 0) {
1024 0 : return getSpeedLimit();
1025 : }
1026 : }
1027 132980641 : 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 64398 : MSEdge::getCurrentTravelTime(double minSpeed) const {
1066 : assert(minSpeed > 0);
1067 64398 : if (!myAmDelayed) {
1068 44802 : return myEmptyTraveltime;
1069 : }
1070 39192 : 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 1842433 : MSEdge::dictionary(const std::string& id, MSEdge* ptr) {
1082 : const DictType::iterator it = myDict.lower_bound(id);
1083 1842433 : if (it == myDict.end() || it->first != id) {
1084 : // id not in myDict
1085 1842433 : myDict.emplace_hint(it, id, ptr);
1086 3684906 : while (ptr->getNumericalID() >= (int)myEdges.size()) {
1087 1842473 : myEdges.push_back(nullptr);
1088 : }
1089 1842433 : myEdges[ptr->getNumericalID()] = ptr;
1090 1842433 : return true;
1091 : }
1092 : return false;
1093 : }
1094 :
1095 :
1096 : MSEdge*
1097 8693641 : MSEdge::dictionary(const std::string& id) {
1098 : const DictType::iterator it = myDict.find(id);
1099 8693641 : if (it == myDict.end()) {
1100 : return nullptr;
1101 : }
1102 6848540 : return it->second;
1103 : }
1104 :
1105 :
1106 : MSEdge*
1107 2936444 : 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 2936444 : if (myEdges[startIdx] != nullptr && myEdges[startIdx]->getID() == id) {
1110 : return myEdges[startIdx];
1111 : }
1112 1853251 : if (startIdx + 1 < (int)myEdges.size() && myEdges[startIdx + 1] != nullptr && myEdges[startIdx + 1]->getID() == id) {
1113 : return myEdges[startIdx + 1];
1114 : }
1115 609670 : return dictionary(id);
1116 : }
1117 :
1118 :
1119 : const MSEdgeVector&
1120 906782 : MSEdge::getAllEdges() {
1121 906782 : return myEdges;
1122 : }
1123 :
1124 :
1125 : void
1126 42346 : MSEdge::clear() {
1127 1869334 : for (DictType::iterator i = myDict.begin(); i != myDict.end(); ++i) {
1128 1826988 : delete (*i).second;
1129 : }
1130 : myDict.clear();
1131 : myEdges.clear();
1132 42346 : }
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 414280 : MSEdge::parseEdgesList(const std::string& desc, ConstMSEdgeVector& into,
1145 : const std::string& rid) {
1146 414280 : StringTokenizer st(desc);
1147 414280 : parseEdgesList(st.getVector(), into, rid);
1148 414280 : }
1149 :
1150 :
1151 : void
1152 414563 : MSEdge::parseEdgesList(const std::vector<std::string>& desc, ConstMSEdgeVector& into,
1153 : const std::string& rid) {
1154 1570720 : for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
1155 1156206 : const MSEdge* edge = MSEdge::dictionary(*i);
1156 : // check whether the edge exists
1157 1156206 : 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 1156157 : into.push_back(edge);
1162 : }
1163 414514 : }
1164 :
1165 :
1166 : double
1167 1891243 : MSEdge::getDistanceTo(const MSEdge* other, const bool doBoundaryEstimate) const {
1168 : assert(this != other);
1169 1891243 : if (doBoundaryEstimate) {
1170 19288 : return myBoundary.distanceTo2D(other->myBoundary);
1171 : }
1172 1871955 : 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 1871391 : if (other->isTazConnector()) {
1179 5405 : return other->myBoundary.distanceTo2D(getLanes()[0]->getShape()[-1]);
1180 : }
1181 1865986 : return getLanes()[0]->getShape()[-1].distanceTo2D(other->getLanes()[0]->getShape()[0]);
1182 : }
1183 :
1184 :
1185 : const Position
1186 2450 : MSEdge::getStopPosition(const SUMOVehicleParameter::Stop& stop) {
1187 2450 : return MSLane::dictionary(stop.lane)->geometryPositionAtOffset((stop.endPos + stop.startPos) / 2.);
1188 : }
1189 :
1190 :
1191 : double
1192 94011188 : MSEdge::getSpeedLimit() const {
1193 : // @note lanes might have different maximum speeds in theory
1194 94011188 : return myLanes->empty() ? 1 : getLanes()[0]->getSpeedLimit();
1195 : }
1196 :
1197 :
1198 : double
1199 1925264 : MSEdge::getLengthGeometryFactor() const {
1200 1925264 : return myLanes->empty() ? 1 : getLanes()[0]->getLengthGeometryFactor();
1201 : }
1202 :
1203 : double
1204 2068670669 : MSEdge::getVehicleMaxSpeed(const SUMOTrafficObject* const veh) const {
1205 : // @note lanes might have different maximum speeds in theory
1206 2068670669 : return myLanes->empty() ? 1 : getLanes()[0]->getVehicleMaxSpeed(veh);
1207 : }
1208 :
1209 :
1210 : void
1211 201 : MSEdge::setMaxSpeed(const double val, const bool modified, const double jamThreshold) {
1212 : assert(val >= 0);
1213 201 : if (myLanes != nullptr) {
1214 556 : for (MSLane* const lane : *myLanes) {
1215 355 : lane->setMaxSpeed(val, modified, jamThreshold);
1216 : }
1217 : }
1218 201 : }
1219 :
1220 :
1221 : void
1222 3224364 : MSEdge::addTransportable(MSTransportable* t) const {
1223 3224364 : if (t->isPerson()) {
1224 : myPersons.insert(t);
1225 : } else {
1226 : myContainers.insert(t);
1227 : }
1228 3224364 : }
1229 :
1230 : void
1231 8134120 : MSEdge::removeTransportable(MSTransportable* t) const {
1232 8134120 : std::set<MSTransportable*, ComparatorNumericalIdLess>& tc = t->isPerson() ? myPersons : myContainers;
1233 : auto it = tc.find(t);
1234 8134120 : if (it != tc.end()) {
1235 : tc.erase(it);
1236 : }
1237 8134120 : }
1238 :
1239 : std::vector<MSTransportable*>
1240 110758065 : MSEdge::getSortedPersons(SUMOTime timestep, bool includeRiding) const {
1241 110758065 : std::vector<MSTransportable*> result(myPersons.begin(), myPersons.end());
1242 110758065 : if (includeRiding) {
1243 2896155 : for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
1244 2042878 : const MSLane::VehCont& vehs = (*i)->getVehiclesSecure();
1245 3266518 : for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
1246 1223640 : const std::vector<MSTransportable*>& persons = (*j)->getPersons();
1247 1223640 : result.insert(result.end(), persons.begin(), persons.end());
1248 : }
1249 2042878 : (*i)->releaseVehicles();
1250 : }
1251 : }
1252 110758065 : sort(result.begin(), result.end(), transportable_by_position_sorter(timestep));
1253 110758065 : return result;
1254 0 : }
1255 :
1256 :
1257 : std::vector<MSTransportable*>
1258 54037736 : MSEdge::getSortedContainers(SUMOTime timestep, bool /* includeRiding */) const {
1259 54037736 : std::vector<MSTransportable*> result(myContainers.begin(), myContainers.end());
1260 54037736 : sort(result.begin(), result.end(), transportable_by_position_sorter(timestep));
1261 54037736 : return result;
1262 0 : }
1263 :
1264 :
1265 : int
1266 5266882 : MSEdge::transportable_by_position_sorter::operator()(const MSTransportable* const c1, const MSTransportable* const c2) const {
1267 5266882 : const double pos1 = c1->getCurrentStage()->getEdgePos(myTime);
1268 5266882 : const double pos2 = c2->getCurrentStage()->getEdgePos(myTime);
1269 5266882 : if (pos1 != pos2) {
1270 5110066 : return pos1 < pos2;
1271 : }
1272 156816 : return c1->getID() < c2->getID();
1273 : }
1274 :
1275 :
1276 : void
1277 125229 : MSEdge::addSuccessor(MSEdge* edge, const MSEdge* via) {
1278 125229 : mySuccessors.push_back(edge);
1279 125229 : myViaSuccessors.push_back(std::make_pair(edge, via));
1280 125229 : if (isTazConnector() && edge->getFromJunction() != nullptr) {
1281 62611 : myBoundary.add(edge->getFromJunction()->getPosition());
1282 : }
1283 :
1284 125229 : edge->myPredecessors.push_back(this);
1285 125229 : if (edge->isTazConnector() && getToJunction() != nullptr) {
1286 62618 : edge->myBoundary.add(getToJunction()->getPosition());
1287 : }
1288 125229 : }
1289 :
1290 :
1291 : const MSEdgeVector&
1292 78844725 : MSEdge::getSuccessors(SUMOVehicleClass vClass) const {
1293 78844725 : if (vClass == SVC_IGNORING || !MSNet::getInstance()->hasPermissions() || myFunction == SumoXMLEdgeFunc::CONNECTOR) {
1294 78827521 : return mySuccessors;
1295 : }
1296 : #ifdef HAVE_FOX
1297 17204 : ScopedLocker<> lock(mySuccessorMutex, MSGlobals::gNumThreads > 1);
1298 : #endif
1299 : std::map<SUMOVehicleClass, MSEdgeVector>::iterator i = myClassesSuccessorMap.find(vClass);
1300 17204 : if (i == myClassesSuccessorMap.end()) {
1301 : // instantiate vector
1302 2232 : myClassesSuccessorMap[vClass];
1303 : i = myClassesSuccessorMap.find(vClass);
1304 : // this vClass is requested for the first time. rebuild all successors
1305 11480 : for (MSEdgeVector::const_iterator it = mySuccessors.begin(); it != mySuccessors.end(); ++it) {
1306 9248 : if ((*it)->isTazConnector()) {
1307 289 : i->second.push_back(*it);
1308 : } else {
1309 8959 : const std::vector<MSLane*>* allowed = allowedLanes(**it, vClass);
1310 8959 : if (allowed != nullptr && allowed->size() > 0) {
1311 6731 : i->second.push_back(*it);
1312 : }
1313 : }
1314 : }
1315 : }
1316 : // can use cached value
1317 17204 : return i->second;
1318 : }
1319 :
1320 :
1321 : const MSConstEdgePairVector&
1322 197573412 : MSEdge::getViaSuccessors(SUMOVehicleClass vClass, bool ignoreTransientPermissions) const {
1323 197573412 : if (vClass == SVC_IGNORING || !MSNet::getInstance()->hasPermissions() || myFunction == SumoXMLEdgeFunc::CONNECTOR) {
1324 191960645 : return myViaSuccessors;
1325 : }
1326 : #ifdef HAVE_FOX
1327 5612767 : ScopedLocker<> lock(mySuccessorMutex, MSGlobals::gNumThreads > 1);
1328 : #endif
1329 5612767 : auto& viaMap = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigClassesViaSuccessorMap : myClassesViaSuccessorMap;
1330 : auto i = viaMap.find(vClass);
1331 5612767 : if (i != viaMap.end()) {
1332 : // can use cached value
1333 5557934 : return i->second;
1334 : }
1335 : // instantiate vector
1336 54833 : MSConstEdgePairVector& result = viaMap[vClass];
1337 : // this vClass is requested for the first time. rebuild all successors
1338 233420 : for (const auto& viaPair : myViaSuccessors) {
1339 178587 : if (viaPair.first->isTazConnector()) {
1340 6321 : result.push_back(viaPair);
1341 : } else {
1342 172266 : const std::vector<MSLane*>* allowed = allowedLanes(*viaPair.first, vClass, ignoreTransientPermissions);
1343 172266 : if (allowed != nullptr && allowed->size() > 0) {
1344 129462 : result.push_back(viaPair);
1345 : }
1346 : }
1347 : }
1348 : return result;
1349 : }
1350 :
1351 :
1352 : void
1353 1772206 : MSEdge::setJunctions(MSJunction* from, MSJunction* to) {
1354 1772206 : myFromJunction = from;
1355 1772206 : myToJunction = to;
1356 1772206 : if (!isTazConnector()) {
1357 1772206 : myBoundary.add(from->getPosition());
1358 1772206 : myBoundary.add(to->getPosition());
1359 : }
1360 1772206 : }
1361 :
1362 :
1363 : bool
1364 2929039 : MSEdge::canChangeToOpposite() const {
1365 2929039 : return (!myLanes->empty() && myLanes->back()->getOpposite() != nullptr &&
1366 : // do not change on curved internal lanes
1367 : (!isInternal()
1368 5872 : || (MSGlobals::gUsingInternalLanes
1369 5872 : && myLanes->back()->getIncomingLanes()[0].viaLink->getDirection() == LinkDirection::STRAIGHT)));
1370 : }
1371 :
1372 :
1373 : const MSEdge*
1374 22515301 : MSEdge::getOppositeEdge() const {
1375 22515301 : if (!myLanes->empty() && myLanes->back()->getOpposite() != nullptr) {
1376 2988221 : return &(myLanes->back()->getOpposite()->getEdge());
1377 : } else {
1378 19527080 : return nullptr;
1379 : }
1380 : }
1381 :
1382 :
1383 : bool
1384 178 : MSEdge::hasMinorLink() const {
1385 354 : for (const MSLane* const l : *myLanes) {
1386 386 : for (const MSLink* const link : l->getLinkCont()) {
1387 210 : if (!link->havePriority()) {
1388 : return true;
1389 : }
1390 : }
1391 : }
1392 : return false;
1393 : }
1394 :
1395 : bool
1396 209247 : MSEdge::hasChangeProhibitions(SUMOVehicleClass svc, int index) const {
1397 209247 : if (myLanes->size() == 1) {
1398 : return false;
1399 : }
1400 285554 : for (const MSLane* const l : *myLanes) {
1401 193905 : if (l->getIndex() <= index && !l->allowsChangingRight(svc) && l->getIndex() > 0) {
1402 : return true;
1403 193861 : } else if (l->getIndex() >= index && !l->allowsChangingLeft(svc) && l->getIndex() < (int)(myLanes->size() - 1)) {
1404 : return true;
1405 : }
1406 : }
1407 : return false;
1408 : }
1409 :
1410 : void
1411 921604 : MSEdge::checkAndRegisterBiDirEdge(const std::string& bidiID) {
1412 921604 : if (bidiID != "") {
1413 32980 : myBidiEdge = dictionary(bidiID);
1414 32980 : if (myBidiEdge == nullptr) {
1415 0 : WRITE_ERRORF(TL("Bidi-edge '%' does not exist"), bidiID);
1416 : }
1417 32980 : setBidiLanes();
1418 528489 : return;
1419 : }
1420 888624 : if (getFunction() != SumoXMLEdgeFunc::NORMAL) {
1421 : return;
1422 : }
1423 : // legacy networks (no bidi attribute)
1424 393115 : ConstMSEdgeVector candidates = myToJunction->getOutgoing();
1425 3108347 : for (ConstMSEdgeVector::const_iterator it = candidates.begin(); it != candidates.end(); it++) {
1426 2715232 : if ((*it)->getToJunction() == myFromJunction) { //reverse edge
1427 311200 : if (myBidiEdge != nullptr && isSuperposable(*it)) {
1428 0 : WRITE_WARNINGF(TL("Ambiguous superposable edges between junction '%' and '%'."), myToJunction->getID(), myFromJunction->getID());
1429 0 : break;
1430 : }
1431 311200 : if (isSuperposable(*it)) {
1432 26 : myBidiEdge = *it;
1433 26 : setBidiLanes();
1434 : }
1435 : }
1436 : }
1437 393115 : }
1438 :
1439 :
1440 : void
1441 33006 : MSEdge::setBidiLanes() {
1442 : assert(myBidiEdge != nullptr);
1443 33006 : if (getNumLanes() == 1 && myBidiEdge->getNumLanes() == 1) {
1444 : // the other way round is set when this method runs for the bidiEdge
1445 31274 : getLanes()[0]->setBidiLane(myBidiEdge->getLanes()[0]);
1446 : } else {
1447 : // find lanes with matching reversed shapes
1448 : int numBidiLanes = 0;
1449 5262 : for (MSLane* l1 : *myLanes) {
1450 10794 : for (MSLane* l2 : *myBidiEdge->myLanes) {
1451 7264 : if (l1->getShape().reverse().almostSame(l2->getShape(), POSITION_EPS * 2)) {
1452 1786 : l1->setBidiLane(l2);
1453 1786 : numBidiLanes++;
1454 : }
1455 : }
1456 : }
1457 : // warn only once for each pair
1458 1732 : if (numBidiLanes == 0 && getNumericalID() < myBidiEdge->getNumericalID()) {
1459 15 : WRITE_WARNINGF(TL("Edge '%' and bidi edge '%' have no matching bidi lanes"), getID(), myBidiEdge->getID());
1460 : }
1461 : }
1462 33006 : }
1463 :
1464 :
1465 : bool
1466 311200 : MSEdge::isSuperposable(const MSEdge* other) {
1467 311200 : if (other == nullptr || other->getLanes().size() != myLanes->size()) {
1468 : return false;
1469 : }
1470 : std::vector<MSLane*>::const_iterator it1 = myLanes->begin();
1471 : std::vector<MSLane*>::const_reverse_iterator it2 = other->getLanes().rbegin();
1472 : do {
1473 306506 : if ((*it1)->getShape().reverse() != (*it2)->getShape()) {
1474 : return false;
1475 : }
1476 : it1++;
1477 : it2++;
1478 26 : } while (it1 != myLanes->end());
1479 :
1480 : return true;
1481 : }
1482 :
1483 :
1484 : void
1485 73833 : MSEdge::addWaiting(SUMOVehicle* vehicle) const {
1486 : #ifdef HAVE_FOX
1487 73833 : ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
1488 : #endif
1489 73833 : myWaiting.push_back(vehicle);
1490 73833 : }
1491 :
1492 :
1493 : void
1494 65476 : MSEdge::removeWaiting(const SUMOVehicle* vehicle) const {
1495 : #ifdef HAVE_FOX
1496 65476 : ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
1497 : #endif
1498 65476 : std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting.begin(), myWaiting.end(), vehicle);
1499 65476 : if (it != myWaiting.end()) {
1500 64865 : myWaiting.erase(it);
1501 : }
1502 65476 : }
1503 :
1504 :
1505 : SUMOVehicle*
1506 136440 : MSEdge::getWaitingVehicle(MSTransportable* transportable, const double position) const {
1507 : #ifdef HAVE_FOX
1508 136440 : ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
1509 : #endif
1510 136743 : for (SUMOVehicle* const vehicle : myWaiting) {
1511 25402 : if (transportable->isWaitingFor(vehicle)) {
1512 27592 : if (vehicle->isStoppedInRange(position, MSGlobals::gStopTolerance) ||
1513 2280 : (!vehicle->hasDeparted() &&
1514 2071 : (vehicle->getParameter().departProcedure == DepartDefinition::TRIGGERED ||
1515 83 : vehicle->getParameter().departProcedure == DepartDefinition::CONTAINER_TRIGGERED))) {
1516 : return vehicle;
1517 : }
1518 213 : if (!vehicle->isLineStop(position) && vehicle->allowsBoarding(transportable)) {
1519 228 : WRITE_WARNING((transportable->isPerson() ? "Person '" : "Container '")
1520 : + transportable->getID() + "' at edge '" + getID() + "' position " + toString(position) + " cannot use waiting vehicle '"
1521 : + vehicle->getID() + "' at position " + toString(vehicle->getPositionOnLane()) + " because it is too far away.");
1522 : }
1523 : }
1524 : }
1525 : return nullptr;
1526 : }
1527 :
1528 : std::vector<const SUMOVehicle*>
1529 143941 : MSEdge::getVehicles() const {
1530 : std::vector<const SUMOVehicle*> result;
1531 143941 : if (MSGlobals::gUseMesoSim) {
1532 220 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1533 122 : std::vector<const MEVehicle*> segmentVehs = segment->getVehicles();
1534 122 : result.insert(result.end(), segmentVehs.begin(), segmentVehs.end());
1535 122 : }
1536 : } else {
1537 385235 : for (MSLane* lane : getLanes()) {
1538 824519 : for (auto veh : lane->getVehiclesSecure()) {
1539 583176 : result.push_back(veh);
1540 : }
1541 241343 : lane->releaseVehicles();
1542 : }
1543 : }
1544 143941 : return result;
1545 0 : }
1546 :
1547 : int
1548 622627 : MSEdge::getNumDrivingLanes() const {
1549 : int result = 0;
1550 622627 : SVCPermissions filter = SVCAll;
1551 622627 : if ((myCombinedPermissions & ~(SVC_PEDESTRIAN | SVC_WHEELCHAIR)) != 0) {
1552 : filter = ~(SVC_PEDESTRIAN | SVC_WHEELCHAIR);
1553 2050 : } else if ((myCombinedPermissions & (SVC_PEDESTRIAN | SVC_WHEELCHAIR)) != 0) {
1554 : // filter out green verge
1555 : filter = (SVC_PEDESTRIAN | SVC_WHEELCHAIR);
1556 : }
1557 1402613 : for (const MSLane* const l : *myLanes) {
1558 779986 : if ((l->getPermissions() & filter) != 0) {
1559 690617 : result++;
1560 : }
1561 : }
1562 622627 : return result;
1563 : }
1564 :
1565 : int
1566 399 : MSEdge::getVehicleNumber() const {
1567 399 : return (int)getVehicles().size();
1568 : }
1569 :
1570 :
1571 : bool
1572 0 : MSEdge::isEmpty() const {
1573 : /// more efficient than retrieving vehicle number
1574 0 : if (MSGlobals::gUseMesoSim) {
1575 0 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1576 0 : if (segment->getCarNumber() > 0) {
1577 : return false;
1578 : }
1579 : }
1580 : } else {
1581 0 : for (MSLane* lane : getLanes()) {
1582 0 : if (lane->getVehicleNumber() > 0) {
1583 : return false;
1584 : }
1585 : }
1586 : }
1587 : return true;
1588 : }
1589 :
1590 :
1591 : double
1592 14 : MSEdge::getWaitingSeconds() const {
1593 : double wtime = 0;
1594 14 : if (MSGlobals::gUseMesoSim) {
1595 12 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1596 9 : wtime += segment->getWaitingSeconds();
1597 : }
1598 : } else {
1599 34 : for (MSLane* lane : getLanes()) {
1600 23 : wtime += lane->getWaitingSeconds();
1601 : }
1602 : }
1603 14 : return wtime;
1604 : }
1605 :
1606 :
1607 : double
1608 22 : MSEdge::getOccupancy() const {
1609 22 : if (myLanes->size() == 0) {
1610 : return 0;
1611 : }
1612 22 : if (MSGlobals::gUseMesoSim) {
1613 : /// @note MESegment only tracks brutto occupancy so we compute this from sratch
1614 : double sum = 0;
1615 8 : for (const SUMOVehicle* veh : getVehicles()) {
1616 4 : sum += dynamic_cast<const MEVehicle*>(veh)->getVehicleType().getLength();
1617 4 : }
1618 4 : return sum / (myLength * (double)myLanes->size());
1619 : } else {
1620 : double sum = 0;
1621 48 : for (auto lane : getLanes()) {
1622 30 : sum += lane->getNettoOccupancy();
1623 : }
1624 18 : return sum / (double)myLanes->size();
1625 : }
1626 : }
1627 :
1628 :
1629 : double
1630 0 : MSEdge::getFlow() const {
1631 0 : if (myLanes->size() == 0) {
1632 : return 0;
1633 : }
1634 : double flow = 0;
1635 0 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1636 0 : flow += (double) segment->getCarNumber() * segment->getMeanSpeed();
1637 : }
1638 0 : return 3600 * flow / (*myLanes)[0]->getLength();
1639 : }
1640 :
1641 :
1642 : double
1643 0 : MSEdge::getBruttoOccupancy() const {
1644 0 : if (myLanes->size() == 0) {
1645 : return 0;
1646 : }
1647 : double occ = 0;
1648 0 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
1649 0 : occ += segment->getBruttoOccupancy();
1650 : }
1651 0 : return occ / (*myLanes)[0]->getLength() / (double)(myLanes->size());
1652 : }
1653 :
1654 : double
1655 4240 : MSEdge::getTravelTimeAggregated(const MSEdge* const edge, const SUMOVehicle* const veh, double /*time*/) {
1656 4240 : return edge->getLength() / MIN2(MSRoutingEngine::getAssumedSpeed(edge, veh), veh->getMaxSpeed());
1657 : }
1658 :
1659 :
1660 : void
1661 2098 : MSEdge::inferEdgeType() {
1662 : // @note must be called after closeBuilding() to ensure successors and
1663 : // predecessors are set
1664 2098 : if (isInternal() && myEdgeType == "") {
1665 1274 : const std::string typeBefore = getNormalBefore()->getEdgeType();
1666 1274 : if (typeBefore != "") {
1667 694 : const std::string typeAfter = getNormalSuccessor()->getEdgeType();
1668 694 : if (typeBefore == typeAfter) {
1669 : myEdgeType = typeBefore;
1670 314 : } else if (typeAfter != "") {
1671 68 : MSNet* net = MSNet::getInstance();
1672 68 : auto resBefore = net->getRestrictions(typeBefore);
1673 68 : auto resAfter = net->getRestrictions(typeAfter);
1674 68 : if (resBefore != nullptr && resAfter != nullptr) {
1675 : // create new restrictions for this type-combination
1676 96 : myEdgeType = typeBefore + "|" + typeAfter;
1677 48 : if (net->getRestrictions(myEdgeType) == nullptr) {
1678 48 : for (const auto& item : *resBefore) {
1679 24 : const SUMOVehicleClass svc = item.first;
1680 24 : const double speed = item.second;
1681 : const auto it = (*resAfter).find(svc);
1682 24 : if (it != (*resAfter).end()) {
1683 24 : const double speed2 = it->second;
1684 24 : const double newSpeed = (MSNet::getInstance()->hasJunctionHigherSpeeds()
1685 24 : ? MAX2(speed, speed2) : (speed + speed2) / 2);
1686 24 : net->addRestriction(myEdgeType, svc, newSpeed);
1687 : }
1688 : }
1689 : }
1690 : }
1691 : }
1692 : }
1693 : }
1694 2098 : }
1695 :
1696 :
1697 : double
1698 2178 : MSEdge::getDistanceAt(double pos) const {
1699 : // negative values of myDistances indicate descending kilometrage
1700 2178 : return fabs(myDistance + pos);
1701 : }
1702 :
1703 :
1704 : bool
1705 1348 : MSEdge::hasTransientPermissions() const {
1706 1348 : return myHaveTransientPermissions;
1707 : }
1708 :
1709 :
1710 : std::pair<double, SUMOTime>
1711 592702737 : MSEdge::getLastBlocked(int index) const {
1712 592702737 : if (myLaneChanger != nullptr) {
1713 592702737 : return myLaneChanger->getLastBlocked(index);
1714 : }
1715 0 : return std::make_pair(-1, -1);
1716 : }
1717 :
1718 :
1719 : double
1720 2855 : MSEdge::getPreference(const SUMOVTypeParameter& pars) const {
1721 5710 : return MSNet::getInstance()->getPreference(getRoutingType(), pars);
1722 : }
1723 :
1724 :
1725 : void
1726 7032 : MSEdge::clearState() {
1727 : myPersons.clear();
1728 : myContainers.clear();
1729 : myWaiting.clear();
1730 7032 : }
1731 :
1732 :
1733 : const std::map<const MEVehicle*, std::pair<double, int> >&
1734 778080 : MSEdge::getMesoPositions() const {
1735 : assert(MSGlobals::gUseMesoSim);
1736 778080 : if (myLastCacheUpdate < SIMSTEP) {
1737 766099 : myLastCacheUpdate = SIMSTEP;
1738 : auto old = std::move(myCachedMesoPos);
1739 : myCachedMesoPos.clear(); // moved-from map is valid-but-unspecified; make it defined-empty
1740 : int laneIndex = 0;
1741 766099 : const double now = SIMTIME;
1742 1876779 : for (std::vector<MSLane*>::const_iterator msl = myLanes->begin(); msl != myLanes->end(); ++msl, ++laneIndex) {
1743 : // go through the vehicles
1744 : double segmentOffset = 0; // offset at start of current segment
1745 1110680 : for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this);
1746 2362162 : segment != nullptr; segment = segment->getNextSegment()) {
1747 : const double segLength = segment->getLength();
1748 1251482 : const double lanesCovered = segment->numQueues() == 1 ? std::round(segment->getCapacity() / segLength) : 1.;
1749 1251482 : if (laneIndex < segment->numQueues()) {
1750 : // make a copy so we don't have to worry about synchronization
1751 950843 : std::vector<MEVehicle*> queue = segment->getQueue(laneIndex);
1752 950843 : const int queueSize = (int)queue.size();
1753 : SUMOTime earliestExitTime = segment->getQueueBlockTime(laneIndex);
1754 : int overlap = 0;
1755 : double prevPos = std::numeric_limits<double>::max();
1756 2117028 : for (int i = 0; i < queueSize; ++i) {
1757 1166185 : const MEVehicle* const veh = queue[queueSize - i - 1];
1758 : earliestExitTime = MAX2(earliestExitTime, veh->getEventTime());
1759 1166185 : const double vehLength = veh->getVehicleType().getLengthWithGap();
1760 1166185 : double maxPos = segmentOffset + segLength;
1761 : auto it = old.find(veh);
1762 1166185 : const double oldPos = it != old.end() ? it->second.first : 0.; // store the old position to prevent backwards moving vehicles
1763 1166185 : if (i > 0) {
1764 1051818 : earliestExitTime += segment->getMinTauWithVehLength(vehLength, veh->getVehicleType().getCarFollowModel().getHeadwayTime());
1765 1051818 : maxPos = MIN2(maxPos, prevPos - vehLength / lanesCovered);
1766 : }
1767 1166185 : const double entry = veh->getLastEntryTimeSeconds();
1768 : assert(STEPS2TIME(earliestExitTime) > entry);
1769 1166185 : const double pos = MAX2(MIN2(segmentOffset + segLength * (now - entry) / (STEPS2TIME(earliestExitTime) - entry), maxPos), oldPos);
1770 : // check if we overlap with the previous vehicle such that the gui has the chance to add some lateral offset
1771 1166185 : if (overlap == 0 && prevPos - pos < vehLength) {
1772 301802 : overlap = lanesCovered > 1. ? -3 : -1;
1773 : } else {
1774 : overlap = 0;
1775 : }
1776 1166185 : myCachedMesoPos[veh] = std::make_pair(pos, overlap);
1777 : prevPos = pos;
1778 : }
1779 950843 : }
1780 1251482 : segmentOffset += segLength;
1781 : }
1782 : }
1783 : }
1784 778080 : return myCachedMesoPos;
1785 : }
1786 :
1787 :
1788 : /****************************************************************************/
|