Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 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 MSStageDriving.cpp
15 : /// @author Melanie Weber
16 : /// @author Andreas Kendziorra
17 : /// @author Michael Behrisch
18 : /// @date Thu, 12 Jun 2014
19 : ///
20 : // A stage performing the travelling by a transport system (cars, public transport)
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <utils/common/StringTokenizer.h>
25 : #include <utils/geom/GeomHelper.h>
26 : #include <utils/vehicle/SUMOVehicleParameter.h>
27 : #include <utils/router/PedestrianRouter.h>
28 : #include <utils/router/IntermodalRouter.h>
29 : #include <microsim/MSEdge.h>
30 : #include <microsim/MSEventControl.h>
31 : #include <microsim/MSLane.h>
32 : #include <microsim/MSNet.h>
33 : #include <microsim/MSStop.h>
34 : #include <microsim/MSInsertionControl.h>
35 : #include <microsim/MSVehicleControl.h>
36 : #include <microsim/MSStoppingPlace.h>
37 : #include <microsim/MSTrainHelper.h>
38 : #include <microsim/MSVehicleType.h>
39 : #include <microsim/devices/MSTransportableDevice.h>
40 : #include <microsim/devices/MSDevice_Taxi.h>
41 : #include <microsim/devices/MSDevice_Transportable.h>
42 : #include <microsim/devices/MSDevice_Tripinfo.h>
43 : #include <microsim/devices/MSDispatch.h>
44 : #include <microsim/transportables/MSTransportableControl.h>
45 : #include <microsim/transportables/MSStageDriving.h>
46 : #include <microsim/transportables/MSPModel.h>
47 : #include <microsim/transportables/MSPerson.h>
48 :
49 :
50 : // ===========================================================================
51 : // method definitions
52 : // ===========================================================================
53 77848 : MSStageDriving::MSStageDriving(const MSEdge* origin, const MSEdge* destination,
54 : MSStoppingPlace* toStop, const double arrivalPos, const double arrivalPosLat,
55 : const std::vector<std::string>& lines, const std::string& group,
56 77848 : const std::string& intendedVeh, SUMOTime intendedDepart) :
57 : MSStage(MSStageType::DRIVING, destination, toStop, arrivalPos, arrivalPosLat, group),
58 77848 : myOrigin(origin),
59 77848 : myLines(lines.begin(), lines.end()),
60 77848 : myVehicle(nullptr),
61 77848 : myVehicleID("NULL"),
62 77848 : myVehicleVClass(SVC_IGNORING),
63 77848 : myVehicleDistance(-1.),
64 77848 : myTimeLoss(-1),
65 77848 : myWaitingPos(-1),
66 77848 : myWaitingSince(-1),
67 77848 : myWaitingEdge(nullptr),
68 77848 : myStopWaitPos(Position::INVALID),
69 77848 : myOriginStop(nullptr),
70 77848 : myIntendedVehicleID(intendedVeh),
71 77848 : myIntendedDepart(intendedDepart),
72 77848 : myReservationCommand(nullptr) {
73 77848 : }
74 :
75 :
76 : MSStage*
77 67671 : MSStageDriving::clone() const {
78 67671 : MSStage* const clon = new MSStageDriving(myOrigin, myDestination, myDestinationStop, myArrivalPos, myArrivalPosLat,
79 135342 : std::vector<std::string>(myLines.begin(), myLines.end()),
80 67671 : myGroup, myIntendedVehicleID, myIntendedDepart);
81 67671 : clon->setParameters(*this);
82 67671 : return clon;
83 : }
84 :
85 :
86 233454 : MSStageDriving::~MSStageDriving() {}
87 :
88 :
89 : void
90 73238 : MSStageDriving::init(MSTransportable* transportable) {
91 146476 : if (hasParameter("earliestPickupTime")) {
92 162 : SUMOTime reservationTime = MSNet::getInstance()->getCurrentTimeStep();
93 324 : if (hasParameter("reservationTime")) {
94 312 : reservationTime = string2time(getParameter("reservationTime"));
95 : }
96 324 : SUMOTime earliestPickupTime = string2time(getParameter("earliestPickupTime"));
97 162 : if (transportable->getNextStage(1) == this) {
98 : // if the ride is the first stage use the departPos (there is a unvisible stop before)
99 144 : myWaitingPos = transportable->getParameter().departPos;
100 : } else {
101 : // else use the middle of the edge, as also used as default for walk's arrivalPos
102 18 : myWaitingPos = myOrigin->getLength() / 2;
103 : }
104 162 : myReservationCommand = new BookReservation(transportable, earliestPickupTime, this);
105 162 : MSNet::getInstance()->getBeginOfTimestepEvents()->addEvent(myReservationCommand, reservationTime);
106 : }
107 73238 : }
108 :
109 :
110 : const MSEdge*
111 114450 : MSStageDriving::getEdge() const {
112 114450 : if (myVehicle != nullptr) {
113 98276 : if (myVehicle->getLane() != nullptr) {
114 85955 : return &myVehicle->getLane()->getEdge();
115 : }
116 12321 : return myVehicle->getEdge();
117 16174 : } else if (myArrived >= 0) {
118 14352 : return myDestination;
119 : } else {
120 1822 : return myWaitingEdge;
121 : }
122 : }
123 :
124 :
125 : const MSEdge*
126 3041 : MSStageDriving::getFromEdge() const {
127 3041 : return myWaitingEdge;
128 : }
129 :
130 :
131 : double
132 343921 : MSStageDriving::getEdgePos(SUMOTime /* now */) const {
133 343921 : if (isWaiting4Vehicle()) {
134 247083 : return myWaitingPos;
135 96838 : } else if (myArrived >= 0) {
136 2522 : return myArrivalPos;
137 : } else {
138 : // vehicle may already have passed the lane (check whether this is correct)
139 94316 : return MIN2(myVehicle->getPositionOnLane(), getEdge()->getLength());
140 : }
141 : }
142 :
143 : int
144 60 : MSStageDriving::getDirection() const {
145 60 : if (isWaiting4Vehicle()) {
146 0 : return MSPModel::UNDEFINED_DIRECTION;
147 60 : } else if (myArrived >= 0) {
148 0 : return MSPModel::UNDEFINED_DIRECTION;
149 : } else {
150 60 : return MSPModel::FORWARD;
151 : }
152 : }
153 :
154 : const MSLane*
155 76 : MSStageDriving::getLane() const {
156 76 : return myVehicle != nullptr ? myVehicle->getLane() : nullptr;
157 : }
158 :
159 :
160 : Position
161 409218 : MSStageDriving::getPosition(SUMOTime /* now */) const {
162 409218 : if (isWaiting4Vehicle()) {
163 : if (myStopWaitPos != Position::INVALID) {
164 143142 : return myStopWaitPos;
165 : }
166 213404 : return getEdgePosition(myWaitingEdge, myWaitingPos,
167 426808 : ROADSIDE_OFFSET * (MSGlobals::gLefthand ? -1 : 1));
168 52672 : } else if (myArrived >= 0) {
169 0 : return getEdgePosition(myDestination, myArrivalPos,
170 0 : ROADSIDE_OFFSET * (MSGlobals::gLefthand ? -1 : 1));
171 : } else {
172 52672 : return myVehicle->getPosition();
173 : }
174 : }
175 :
176 :
177 : double
178 410383 : MSStageDriving::getAngle(SUMOTime /* now */) const {
179 410383 : if (isWaiting4Vehicle()) {
180 713635 : return getEdgeAngle(myWaitingEdge, myWaitingPos) + M_PI / 2. * (MSGlobals::gLefthand ? -1 : 1);
181 53557 : } else if (myArrived >= 0) {
182 0 : return getEdgeAngle(myDestination, myArrivalPos) + M_PI / 2. * (MSGlobals::gLefthand ? -1 : 1);
183 : } else {
184 53557 : MSVehicle* veh = dynamic_cast<MSVehicle*>(myVehicle);
185 53557 : if (veh != nullptr) {
186 47953 : return veh->getAngle();
187 : } else {
188 : return 0;
189 : }
190 : }
191 : }
192 :
193 :
194 : double
195 6606 : MSStageDriving::getDistance() const {
196 6606 : if (myVehicle != nullptr) {
197 : // distance was previously set to driven distance upon embarking
198 481 : return myVehicle->getOdometer() - myVehicleDistance;
199 : }
200 6125 : return myVehicleDistance;
201 : }
202 :
203 :
204 : std::string
205 1698 : MSStageDriving::getStageDescription(const bool isPerson) const {
206 2940 : return isWaiting4Vehicle() ? "waiting for " + joinToString(myLines, ",") : (isPerson ? "driving" : "transport");
207 : }
208 :
209 :
210 : std::string
211 0 : MSStageDriving::getStageSummary(const bool isPerson) const {
212 0 : const std::string dest = (getDestinationStop() == nullptr ?
213 0 : " edge '" + getDestination()->getID() + "'" :
214 0 : " stop '" + getDestinationStop()->getID() + "'" + (
215 0 : getDestinationStop()->getMyName() != "" ? " (" + getDestinationStop()->getMyName() + ")" : ""));
216 0 : const std::string intended = myIntendedVehicleID != "" ?
217 0 : " (vehicle " + myIntendedVehicleID + " at time=" + time2string(myIntendedDepart) + ")" :
218 0 : "";
219 0 : const std::string modeName = isPerson ? "driving" : "transported";
220 0 : return isWaiting4Vehicle() ?
221 0 : "waiting for " + joinToString(myLines, ",") + intended + " then " + modeName + " to " + dest :
222 0 : modeName + " to " + dest;
223 : }
224 :
225 :
226 : void
227 77295 : MSStageDriving::proceed(MSNet* net, MSTransportable* transportable, SUMOTime now, MSStage* previous) {
228 77295 : myOriginStop = (previous->getStageType() == MSStageType::TRIP
229 77295 : ? previous->getOriginStop()
230 : : previous->getDestinationStop());
231 77295 : myWaitingSince = now;
232 77295 : const bool isPerson = transportable->isPerson();
233 77295 : if (transportable->getParameter().departProcedure == DepartDefinition::TRIGGERED
234 77295 : && transportable->getCurrentStageIndex() == 1) {
235 : // we are the first real stage (stage 0 is WAITING_FOR_DEPART)
236 : const std::string vehID = *myLines.begin();
237 1458 : SUMOVehicle* startVeh = net->getVehicleControl().getVehicle(vehID);
238 1458 : if (startVeh == nullptr && net->hasFlow(vehID)) {
239 568 : startVeh = net->getInsertionControl().getLastFlowVehicle(vehID);
240 : }
241 1458 : if (startVeh == nullptr) {
242 0 : throw ProcessError("Vehicle '" + vehID + "' not found for triggered departure of " +
243 0 : (isPerson ? "person" : "container") + " '" + transportable->getID() + "'.");
244 : }
245 1458 : if (transportable->isPerson()) {
246 1436 : const int pCap = startVeh->getVehicleType().getParameter().personCapacity;
247 1436 : if (startVeh->getPersonNumber() >= pCap) {
248 312 : WRITE_WARNING(TLF("Vehicle '%' exceeds personCapacity % when placing triggered person '%', time=%",
249 : startVeh->getID(), pCap, transportable->getID(), time2string(SIMSTEP)));
250 : }
251 : } else {
252 22 : const int cCap = startVeh->getVehicleType().getParameter().containerCapacity;
253 22 : if (startVeh->getContainerNumber() >= cCap) {
254 48 : WRITE_WARNING(TLF("Vehicle '%' exceeds containerCapacity % when placing triggered container '%', time=%",
255 : startVeh->getID(), cCap, transportable->getID(), time2string(SIMSTEP)));
256 : }
257 : }
258 1458 : myDeparted = now;
259 1458 : setVehicle(startVeh);
260 1458 : if (myOriginStop != nullptr) {
261 0 : myOriginStop->removeTransportable(transportable);
262 : }
263 1458 : myWaitingEdge = previous->getEdge();
264 1458 : myStopWaitPos = Position::INVALID;
265 1458 : myWaitingPos = previous->getEdgePos(now);
266 1458 : myVehicle->addTransportable(transportable);
267 : return;
268 : }
269 75837 : if (myOriginStop != nullptr) {
270 : // the arrival stop may have an access point
271 23371 : myWaitingEdge = &myOriginStop->getLane().getEdge();
272 23371 : myStopWaitPos = myOriginStop->getWaitPosition(transportable);
273 23371 : myWaitingPos = myOriginStop->getWaitingPositionOnLane(transportable);
274 : } else {
275 52466 : myWaitingEdge = previous->getEdge();
276 52466 : myStopWaitPos = Position::INVALID;
277 52466 : myWaitingPos = previous->getEdgePos(now);
278 : }
279 50503 : if (myOrigin != nullptr && myOrigin != myWaitingEdge
280 75860 : && (myOriginStop == nullptr || myOriginStop->getAccessPos(myOrigin) < 0)) {
281 : // transfer at junction (rather than access)
282 15 : myWaitingEdge = myOrigin;
283 15 : myWaitingPos = 0;
284 : }
285 75837 : SUMOVehicle* const availableVehicle = myWaitingEdge->getWaitingVehicle(transportable, myWaitingPos);
286 75837 : const bool triggered = availableVehicle != nullptr &&
287 4471 : ((isPerson && availableVehicle->getParameter().departProcedure == DepartDefinition::TRIGGERED) ||
288 311 : (!isPerson && availableVehicle->getParameter().departProcedure == DepartDefinition::CONTAINER_TRIGGERED));
289 2034 : if (triggered && !availableVehicle->hasDeparted()) {
290 2006 : setVehicle(availableVehicle);
291 2006 : if (myOriginStop != nullptr) {
292 0 : myOriginStop->removeTransportable(transportable);
293 : }
294 2006 : myVehicle->addTransportable(transportable);
295 2006 : net->getInsertionControl().add(myVehicle);
296 2006 : net->getVehicleControl().handleTriggeredDepart(myVehicle, false);
297 : } else {
298 73831 : registerWaiting(transportable, now);
299 : }
300 : }
301 :
302 :
303 : void
304 73838 : MSStageDriving::registerWaiting(MSTransportable* transportable, SUMOTime now) {
305 : // check if the ride can be conducted and reserve it
306 73838 : if (MSDevice_Taxi::isReservation(getLines())) {
307 2365 : const MSEdge* to = getDestination();
308 2365 : double toPos = getArrivalPos();
309 2365 : if ((to->getPermissions() & SVC_TAXI) == 0 && getDestinationStop() != nullptr) {
310 : // try to find usable access edge
311 6 : for (const auto& access : getDestinationStop()->getAllAccessPos()) {
312 6 : const MSEdge* accessEdge = &access.lane->getEdge();
313 6 : if ((accessEdge->getPermissions() & SVC_TAXI) != 0) {
314 : to = accessEdge;
315 6 : toPos = access.endPos;
316 6 : break;
317 : }
318 : }
319 : }
320 2365 : if ((myWaitingEdge->getPermissions() & SVC_TAXI) == 0 && myOriginStop != nullptr) {
321 : // try to find usable access edge
322 11 : for (const auto& access : myOriginStop->getAllAccessPos()) {
323 11 : const MSEdge* accessEdge = &access.lane->getEdge();
324 11 : if ((accessEdge->getPermissions() & SVC_TAXI) != 0) {
325 11 : myWaitingEdge = accessEdge;
326 11 : myStopWaitPos = Position::INVALID;
327 11 : myWaitingPos = access.endPos;
328 11 : break;
329 : }
330 : }
331 : }
332 : // Create reservation only if not already created by previous reservationTime
333 2365 : if (myReservationCommand == nullptr) {
334 2203 : MSDevice_Taxi::addReservation(transportable, getLines(), now, now, -1, myWaitingEdge, myWaitingPos, myOriginStop, to, toPos, myDestinationStop, myGroup);
335 : } else {
336 : // update "fromPos" with current (new) value of myWaitingPos
337 162 : MSDevice_Taxi::updateReservationFromPos(transportable, getLines(), myWaitingEdge, myReservationCommand->myWaitingPos, to, toPos, myGroup, myWaitingPos);
338 : }
339 : }
340 73838 : if (transportable->isPerson()) {
341 20860 : MSNet::getInstance()->getPersonControl().addWaiting(myWaitingEdge, transportable);
342 : } else {
343 52978 : MSNet::getInstance()->getContainerControl().addWaiting(myWaitingEdge, transportable);
344 : }
345 73838 : myWaitingEdge->addTransportable(transportable);
346 73838 : }
347 :
348 : SUMOTime
349 6342 : MSStageDriving::getDuration() const {
350 6342 : return myArrived >= 0 ? myArrived - myWaitingSince : SUMOTime_MAX;
351 : }
352 :
353 :
354 : SUMOTime
355 6342 : MSStageDriving::getTravelTime() const {
356 6342 : return myArrived >= 0 ? myArrived - myDeparted : SUMOTime_MAX;
357 : }
358 :
359 :
360 : SUMOTime
361 12684 : MSStageDriving::getWaitingTime() const {
362 12684 : const SUMOTime departed = myDeparted >= 0 ? myDeparted : SIMSTEP;
363 12684 : return myWaitingSince >= 0 ? departed - myWaitingSince : SUMOTime_MAX;
364 : }
365 :
366 :
367 : SUMOTime
368 12417 : MSStageDriving::getTimeLoss(const MSTransportable* /*transportable*/) const {
369 12417 : return myArrived >= 0 ? myTimeLoss : SUMOTime_MAX;
370 : }
371 :
372 : void
373 6342 : MSStageDriving::tripInfoOutput(OutputDevice& os, const MSTransportable* const transportable) const {
374 6342 : const SUMOTime now = MSNet::getInstance()->getCurrentTimeStep();
375 6342 : const SUMOTime waitingTime = getWaitingTime();
376 6342 : const SUMOTime duration = myArrived - myDeparted;
377 6342 : MSDevice_Tripinfo::addRideTransportData(transportable->isPerson(), myVehicleDistance, duration, myVehicleVClass, myVehicleLine, waitingTime);
378 6977 : os.openTag(transportable->isPerson() ? "ride" : "transport");
379 12684 : os.writeAttr("waitingTime", waitingTime != SUMOTime_MAX ? time2string(waitingTime) : "-1");
380 6342 : os.writeAttr("vehicle", myVehicleID);
381 12684 : os.writeAttr("depart", myDeparted >= 0 ? time2string(myDeparted) : "-1");
382 12684 : os.writeAttr("arrival", myArrived >= 0 ? time2string(myArrived) : "-1");
383 12684 : os.writeAttr("arrivalPos", myArrived >= 0 ? toString(getArrivalPos()) : "-1");
384 12951 : os.writeAttr("duration", myArrived >= 0 ? time2string(duration) :
385 267 : (myDeparted >= 0 ? time2string(now - myDeparted) : "-1"));
386 12684 : os.writeAttr("routeLength", myArrived >= 0 || myVehicle != nullptr ? toString(getDistance()) : "-1");
387 12684 : os.writeAttr("timeLoss", myArrived >= 0 ? time2string(getTimeLoss(transportable)) : "-1");
388 6342 : os.closeTag();
389 6342 : }
390 :
391 :
392 : void
393 1257 : MSStageDriving::routeOutput(const bool isPerson, OutputDevice& os, const bool withRouteLength, const MSStage* const previous) const {
394 1341 : os.openTag(isPerson ? SUMO_TAG_RIDE : SUMO_TAG_TRANSPORT);
395 1257 : if (getFromEdge() != nullptr) {
396 1249 : os.writeAttr(SUMO_ATTR_FROM, getFromEdge()->getID());
397 8 : } else if (previous != nullptr && previous->getStageType() == MSStageType::WAITING_FOR_DEPART) {
398 8 : os.writeAttr(SUMO_ATTR_FROM, previous->getEdge()->getID());
399 : }
400 1257 : os.writeAttr(SUMO_ATTR_TO, getDestination()->getID());
401 1257 : std::string comment = "";
402 1257 : if (myDestinationStop != nullptr) {
403 530 : os.writeAttr(toString(myDestinationStop->getElement()), myDestinationStop->getID());
404 530 : if (myDestinationStop->getMyName() != "") {
405 108 : comment = " <!-- " + StringUtils::escapeXML(myDestinationStop->getMyName(), true) + " -->";
406 : }
407 727 : } else if (!unspecifiedArrivalPos()) {
408 696 : os.writeAttr(SUMO_ATTR_ARRIVALPOS, myArrivalPos);
409 : }
410 1257 : os.writeAttr(SUMO_ATTR_LINES, myLines);
411 1257 : if (myIntendedVehicleID != "") {
412 : os.writeAttr(SUMO_ATTR_INTENDED, myIntendedVehicleID);
413 : }
414 1257 : if (myIntendedDepart >= 0) {
415 466 : os.writeAttr(SUMO_ATTR_DEPART, time2string(myIntendedDepart));
416 : }
417 1257 : if (withRouteLength) {
418 24 : os.writeAttr("routeLength", myVehicleDistance);
419 : }
420 2514 : if (OptionsCont::getOptions().getBool("vehroute-output.exit-times")) {
421 54 : os.writeAttr("vehicle", myVehicleID);
422 54 : os.writeAttr(SUMO_ATTR_STARTED, myDeparted >= 0 ? time2string(myDeparted) : "-1");
423 108 : os.writeAttr(SUMO_ATTR_ENDED, myArrived >= 0 ? time2string(myArrived) : "-1");
424 : }
425 2514 : if (OptionsCont::getOptions().getBool("vehroute-output.cost")) {
426 0 : os.writeAttr(SUMO_ATTR_COST, getCosts());
427 : }
428 1257 : os.closeTag(comment);
429 1257 : }
430 :
431 :
432 : bool
433 369660 : MSStageDriving::isWaitingFor(const SUMOVehicle* vehicle) const {
434 : assert(myLines.size() > 0);
435 : return (myLines.count(vehicle->getID()) > 0
436 351086 : || ((myLines.count(vehicle->getParameter().line) > 0
437 563332 : || myLines.count("ANY") > 0) &&
438 : // even if the line matches we still have to check for stops (#14526)
439 297512 : (myDestinationStop == nullptr
440 297512 : ? vehicle->stopsAtEdge(myDestination)
441 112689 : : vehicle->stopsAt(myDestinationStop)))
442 795870 : || MSDevice_Taxi::compatibleLine(vehicle->getParameter().line, *myLines.begin()));
443 : }
444 :
445 :
446 : bool
447 3001072 : MSStageDriving::isWaiting4Vehicle() const {
448 3001072 : return myVehicle == nullptr && myArrived < 0;
449 : }
450 :
451 :
452 : SUMOTime
453 0 : MSStageDriving::getWaitingTime(SUMOTime now) const {
454 0 : return isWaiting4Vehicle() ? now - myWaitingSince : 0;
455 : }
456 :
457 :
458 : double
459 80726 : MSStageDriving::getSpeed() const {
460 80726 : return myVehicle == nullptr ? 0 : myVehicle->getSpeed();
461 : }
462 :
463 :
464 : ConstMSEdgeVector
465 535 : MSStageDriving::getEdges() const {
466 : ConstMSEdgeVector result;
467 535 : result.push_back(getFromEdge());
468 535 : result.push_back(getDestination());
469 535 : return result;
470 0 : }
471 :
472 :
473 : double
474 36451 : MSStageDriving::getArrivalPos() const {
475 36451 : return unspecifiedArrivalPos() ? getDestination()->getLength() : myArrivalPos;
476 : }
477 :
478 :
479 : bool
480 174930 : MSStageDriving::unspecifiedArrivalPos() const {
481 174930 : return myArrivalPos == std::numeric_limits<double>::infinity();
482 : }
483 :
484 :
485 : const std::string
486 12838 : MSStageDriving::setArrived(MSNet* net, MSTransportable* transportable, SUMOTime now, const bool vehicleArrived) {
487 12838 : MSStage::setArrived(net, transportable, now, vehicleArrived);
488 12838 : if (myVehicle != nullptr) {
489 : // distance was previously set to driven distance upon embarking
490 12760 : myVehicleDistance = myVehicle->getOdometer() - myVehicleDistance;
491 12760 : myTimeLoss = myVehicle->getTimeLoss() - myTimeLoss;
492 12760 : if (vehicleArrived) {
493 3929 : myArrivalPos = myVehicle->getArrivalPos();
494 : } else {
495 8831 : myArrivalPos = myVehicle->getPositionOnLane();
496 : }
497 : const MSStoppingPlace* const stop = getDestinationStop();
498 12760 : if (stop != nullptr) {
499 : MSStoppingPlace::AccessExit exit = MSStoppingPlace::AccessExit::PLATFORM;
500 6862 : for (const auto& access : stop->getAllAccessPos()) {
501 2557 : if (access.exit != exit) {
502 : exit = access.exit;
503 : break;
504 : }
505 : }
506 4755 : if (exit != MSStoppingPlace::AccessExit::PLATFORM) {
507 450 : MSVehicle* train = dynamic_cast<MSVehicle*>(myVehicle);
508 450 : if (train != nullptr) {
509 336 : MSTrainHelper trainHelper = MSTrainHelper(train);
510 336 : const MSLane* const lane = myVehicle->getLane();
511 672 : if (OptionsCont::getOptions().getString("pedestrian.model") != "jupedsim") {
512 336 : trainHelper.computeDoorPositions();
513 : const std::vector<MSTrainHelper::Carriage*>& carriages = trainHelper.getCarriages();
514 336 : const int randomCarriageIx = RandHelper::rand(trainHelper.getNumCarriages() - trainHelper.getFirstPassengerCarriage()) + trainHelper.getFirstPassengerCarriage();
515 336 : const MSTrainHelper::Carriage* randomCarriage = carriages[randomCarriageIx];
516 336 : const int randomDoorIx = RandHelper::rand(trainHelper.getCarriageDoors());
517 336 : Position randomDoor = randomCarriage->doorPositions[randomDoorIx];
518 : // Jitter the position before projection because of possible train curvature.
519 : Position direction = randomCarriage->front - randomCarriage->back;
520 336 : direction.norm2D();
521 336 : randomDoor.add(direction * RandHelper::rand(-0.5 * MSTrainHelper::CARRIAGE_DOOR_WIDTH, 0.5 * MSTrainHelper::CARRIAGE_DOOR_WIDTH));
522 : // Project onto the lane.
523 336 : myArrivalPos = lane->getShape().nearest_offset_to_point2D(randomDoor);
524 336 : myArrivalPos = lane->interpolateGeometryPosToLanePos(myArrivalPos);
525 672 : myArrivalPos = MIN2(MAX2(0., myArrivalPos), myVehicle->getEdge()->getLength());
526 : } else {
527 0 : std::vector<Position>& unboardingPositions = static_cast<MSDevice_Transportable*>(train->getDevice(typeid(MSDevice_Transportable)))->getUnboardingPositions();
528 0 : if (unboardingPositions.empty()) {
529 0 : const MSVehicleType* defaultPedestrianType = MSNet::getInstance()->getVehicleControl().getVType(DEFAULT_PEDTYPE_ID, nullptr, true);
530 0 : const double defaultPassengerRadius = MAX2(defaultPedestrianType->getLength(), defaultPedestrianType->getWidth()) / 2.;
531 0 : trainHelper.computeUnboardingPositions(defaultPassengerRadius, unboardingPositions);
532 : }
533 : // Random shuffling of the positions has already been done in the train helper.
534 0 : const Position availableUnboardingPosition = unboardingPositions.back();
535 : unboardingPositions.pop_back();
536 0 : const Position arrivalPos = lane->getShape().transformToVectorCoordinates(availableUnboardingPosition);
537 0 : myArrivalPos = arrivalPos.x();
538 0 : myArrivalPosLat = arrivalPos.y();
539 : }
540 336 : }
541 : }
542 : }
543 : } else {
544 78 : myVehicleDistance = -1.;
545 78 : myTimeLoss = -1;
546 : }
547 12838 : myVehicle = nullptr; // avoid dangling pointer after vehicle arrival
548 12838 : return "";
549 : }
550 :
551 :
552 : void
553 12905 : MSStageDriving::setVehicle(SUMOVehicle* v) {
554 12905 : myVehicle = v;
555 12905 : if (myVehicle != nullptr) {
556 12895 : myVehicleID = v->getID();
557 12895 : myVehicleLine = v->getParameter().line;
558 12895 : myVehicleType = v->getVehicleType().getID();
559 12895 : myVehicleVClass = v->getVClass();
560 12895 : if (myVehicle->hasDeparted()) {
561 9992 : myVehicleDistance = myVehicle->getOdometer();
562 9992 : myTimeLoss = myVehicle->getTimeLoss();
563 : } else {
564 : // it probably got triggered by the person
565 2903 : myVehicleDistance = 0.;
566 2903 : myTimeLoss = 0;
567 : }
568 : }
569 12905 : }
570 :
571 :
572 : void
573 91 : MSStageDriving::abort(MSTransportable* t) {
574 91 : myDestinationStop = nullptr;
575 91 : if (myVehicle != nullptr) {
576 : // jumping out of a moving vehicle!
577 13 : myVehicle->removeTransportable(t);
578 23 : myDestination = myVehicle->getLane() == nullptr ? myVehicle->getEdge() : &myVehicle->getLane()->getEdge();
579 : // myVehicleDistance and myTimeLoss are updated in subsequent call to setArrived
580 : } else {
581 78 : MSTransportableControl& tc = (t->isPerson() ?
582 78 : MSNet::getInstance()->getPersonControl() :
583 0 : MSNet::getInstance()->getContainerControl());
584 78 : tc.abortWaitingForVehicle(t);
585 78 : MSDevice_Taxi::removeReservation(t, getLines(), myWaitingEdge, myWaitingPos, myDestination, getArrivalPos(), myGroup);
586 78 : myDestination = myWaitingEdge;
587 78 : myDestinationStop = myOriginStop;
588 : }
589 91 : }
590 :
591 :
592 : std::string
593 605 : MSStageDriving::getWaitingDescription() const {
594 1815 : return isWaiting4Vehicle() ? ("waiting for " + joinToString(myLines, ",")
595 605 : + " at " + (myOriginStop == nullptr
596 1210 : ? ("edge '" + myWaitingEdge->getID() + "'")
597 1689 : : (toString(myOriginStop->getElement()) + " '" + myOriginStop->getID() + "'"))
598 1210 : ) : "";
599 : }
600 :
601 :
602 : bool
603 75999 : MSStageDriving::canLeaveVehicle(const MSTransportable* t, const SUMOVehicle& veh, const MSStop& stop) {
604 75999 : const MSEdge* stopEdge = stop.getEdge();
605 : bool canLeave = false;
606 75999 : if (t->getDestination() == stopEdge) {
607 : // if this is the last stage, we can use the arrivalPos of the person
608 68876 : const bool unspecifiedAP = unspecifiedArrivalPos() && (
609 46917 : t->getNumRemainingStages() > 1 || !t->getParameter().wasSet(VEHPARS_ARRIVALPOS_SET));
610 68876 : const double arrivalPos = (unspecifiedArrivalPos()
611 68876 : ? SUMOVehicleParameter::interpretEdgePos(t->getParameter().arrivalPos, stopEdge->getLength(),
612 : SUMO_ATTR_ARRIVALPOS, t->getID(), true)
613 18430 : : getArrivalPos());
614 68876 : if (unspecifiedAP || stop.isInRange(arrivalPos, veh.getLength() + MSGlobals::gStopTolerance)) {
615 : canLeave = true;
616 : }
617 : }
618 75999 : if (myDestinationStop != nullptr) {
619 16393 : if (!canLeave) {
620 : // check with more tolerance due to busStop size and also check
621 : // access edges
622 1770 : const double accessPos = myDestinationStop->getAccessPos(veh.getEdge());
623 1770 : if (accessPos >= 0) {
624 519 : double tolerance = veh.getLength() + MSGlobals::gStopTolerance;
625 519 : if (&myDestinationStop->getLane().getEdge() == veh.getEdge()) {
626 : // accessPos is in the middle of the stop
627 271 : tolerance += (myDestinationStop->getEndLanePosition() - myDestinationStop->getBeginLanePosition()) / 2;
628 : }
629 519 : canLeave = stop.isInRange(accessPos, tolerance);
630 : }
631 : }
632 : }
633 75999 : return canLeave;
634 : }
635 :
636 :
637 : void
638 16 : MSStageDriving::saveState(std::ostringstream& out) {
639 16 : const bool hasVehicle = myVehicle != nullptr;
640 48 : out << " " << myWaitingSince << " " << myTimeLoss << " " << myArrived << " " << hasVehicle;
641 16 : if (hasVehicle) {
642 27 : out << " " << myDeparted << " " << myVehicle->getID() << " " << myVehicleDistance;
643 : }
644 16 : }
645 :
646 :
647 : void
648 16 : MSStageDriving::loadState(MSTransportable* transportable, std::istringstream& state) {
649 : bool hasVehicle;
650 16 : state >> myWaitingSince >> myTimeLoss >> myArrived >> hasVehicle;
651 16 : if (hasVehicle) {
652 : std::string vehID;
653 9 : state >> myDeparted >> vehID;
654 9 : SUMOVehicle* startVeh = MSNet::getInstance()->getVehicleControl().getVehicle(vehID);
655 9 : setVehicle(startVeh);
656 9 : myVehicle->addTransportable(transportable);
657 9 : state >> myVehicleDistance;
658 : } else {
659 : // there should always be at least one prior WAITING_FOR_DEPART stage
660 : MSStage* previous = transportable->getNextStage(-1);
661 7 : myOriginStop = (previous->getStageType() == MSStageType::TRIP
662 7 : ? previous->getOriginStop()
663 : : previous->getDestinationStop());
664 7 : if (myOriginStop != nullptr) {
665 : // the arrival stop may have an access point
666 2 : myOriginStop->addTransportable(transportable);
667 2 : myWaitingEdge = &myOriginStop->getLane().getEdge();
668 2 : myStopWaitPos = myOriginStop->getWaitPosition(transportable);
669 2 : myWaitingPos = myOriginStop->getWaitingPositionOnLane(transportable);
670 : } else {
671 5 : myWaitingEdge = previous->getEdge();
672 5 : myStopWaitPos = Position::INVALID;
673 5 : myWaitingPos = previous->getArrivalPos();
674 : }
675 7 : registerWaiting(transportable, SIMSTEP);
676 : }
677 16 : }
678 :
679 : // ---------------------------------------------------------------------------
680 : // MSStageDriving::BookReservation method definitions
681 : // ---------------------------------------------------------------------------
682 : SUMOTime
683 162 : MSStageDriving::BookReservation::execute(SUMOTime currentTime) {
684 486 : MSDevice_Taxi::addReservation(myTransportable, myStage->getLines(), currentTime, currentTime, myEarliestPickupTime,
685 162 : myStage->myOrigin, myStage->myWaitingPos, myStage->myOriginStop, myStage->getDestination(), myStage->getArrivalPos(), myStage->myDestinationStop, myStage->myGroup);
686 : // do not repeat if execution fails
687 162 : return 0;
688 : }
689 :
690 : /****************************************************************************/
|