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 MSTransportable.cpp
15 : /// @author Melanie Weber
16 : /// @author Andreas Kendziorra
17 : /// @author Michael Behrisch
18 : /// @date Thu, 12 Jun 2014
19 : ///
20 : // The common superclass for modelling transportable objects like persons and containers
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 <libsumo/TraCIConstants.h>
30 : #include <microsim/MSEdge.h>
31 : #include <microsim/MSLane.h>
32 : #include <microsim/MSNet.h>
33 : #include <microsim/MSEventControl.h>
34 : #include <microsim/MSStoppingPlace.h>
35 : #include <microsim/MSVehicleControl.h>
36 : #include <microsim/devices/MSTransportableDevice.h>
37 : #include <microsim/transportables/MSTransportableControl.h>
38 : #include <microsim/transportables/MSPerson.h>
39 : #include <microsim/transportables/MSStageDriving.h>
40 : #include <microsim/transportables/MSStageTrip.h>
41 : #include <microsim/transportables/MSStageWaiting.h>
42 : #include <microsim/transportables/MSTransportable.h>
43 :
44 : SUMOTrafficObject::NumericalID MSTransportable::myCurrentNumericalIndex = 0;
45 :
46 : //#define DEBUG_PARKING
47 :
48 : // ===========================================================================
49 : // method definitions
50 : // ===========================================================================
51 530721 : MSTransportable::MSTransportable(const SUMOVehicleParameter* pars, MSVehicleType* vtype, MSTransportablePlan* plan, const bool isPerson) :
52 : SUMOTrafficObject(pars->id),
53 530721 : myParameter(pars), myVType(vtype), myPlan(plan),
54 530721 : myAmPerson(isPerson),
55 530721 : myNumericalID(myCurrentNumericalIndex++),
56 530721 : myRandomSeed(RandHelper::murmur3_32(pars->id, RandHelper::getSeed())) {
57 530721 : myStep = myPlan->begin();
58 : // init devices
59 530721 : MSDevice::buildTransportableDevices(*this, myDevices);
60 1705257 : for (MSStage* const stage : * myPlan) {
61 1174536 : stage->init(this);
62 : }
63 530721 : }
64 :
65 :
66 727609 : MSTransportable::~MSTransportable() {
67 530670 : if (myStep != myPlan->end() && getCurrentStageType() == MSStageType::DRIVING) {
68 65107 : MSStageDriving* const stage = dynamic_cast<MSStageDriving*>(*myStep);
69 65107 : if (stage->getVehicle() != nullptr) {
70 158 : stage->getVehicle()->removeTransportable(this);
71 64949 : } else if (stage->getOriginStop() != nullptr) {
72 19004 : stage->getOriginStop()->removeTransportable(this);
73 : }
74 : }
75 530670 : if (myPlan != nullptr) {
76 1884588 : for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
77 1353918 : delete *i;
78 : }
79 530670 : delete myPlan;
80 530670 : myPlan = nullptr;
81 : }
82 533593 : for (MSTransportableDevice* dev : myDevices) {
83 2923 : delete dev;
84 : }
85 530670 : delete myParameter;
86 530670 : if (myVType->isVehicleSpecific()) {
87 180 : MSNet::getInstance()->getVehicleControl().removeVType(myVType);
88 : }
89 727609 : }
90 :
91 : SumoRNG*
92 56 : MSTransportable::getRNG() const {
93 56 : return getEdge()->getLanes()[0]->getRNG();
94 : }
95 :
96 : int
97 7230 : MSTransportable::getRNGIndex() const {
98 7230 : const std::vector<MSLane*>& lanes = getEdge()->getLanes();
99 7230 : return lanes.empty() ? 0 : lanes[0]->getRNGIndex();
100 : }
101 :
102 : bool
103 1237076 : MSTransportable::proceed(MSNet* net, SUMOTime time, const bool vehicleArrived) {
104 1237076 : MSStage* const prior = *myStep;
105 1237076 : const std::string& error = prior->setArrived(net, this, time, vehicleArrived);
106 : // must be done before increasing myStep to avoid invalid state for rendering
107 1237076 : prior->getEdge()->removeTransportable(this);
108 : myStep++;
109 1237076 : if (error != "") {
110 56 : throw ProcessError(error);
111 : }
112 : /* We need to check whether an access stage is needed (or maybe even two).
113 : The general scheme is: If the prior stage ended at a stop and the next stage
114 : starts at an edge which is not the one the stop is at, but the stop has an access to it
115 : we need an access stage. The same is true if prior ends at an edge, the next stage
116 : is allowed to start at any stop the edge has access to.
117 : If we start at a stop or end at a stop no access is needed.
118 : */
119 : bool accessToStop = false;
120 1237020 : if (prior->getStageType() == MSStageType::WALKING || prior->getStageType() == MSStageType::DRIVING) {
121 295834 : accessToStop = checkAccess(prior);
122 941186 : } else if (prior->getStageType() == MSStageType::WAITING_FOR_DEPART) {
123 512235 : for (MSTransportableDevice* const dev : myDevices) {
124 2474 : dev->notifyEnter(*this, MSMoveReminder::NOTIFICATION_DEPARTED, nullptr);
125 : }
126 : }
127 1237020 : if (!accessToStop && (myStep == myPlan->end()
128 798960 : || ((*myStep)->getStageType() != MSStageType::DRIVING
129 660316 : && (*myStep)->getStageType() != MSStageType::TRIP))) {
130 929202 : MSStoppingPlace* priorStop = prior->getStageType() == MSStageType::TRIP ? prior->getOriginStop() : prior->getDestinationStop();
131 : // a trip might resolve to DRIVING so we would have to stay at the stop
132 : // if a trip resolves to something else, this step will do stop removal
133 929202 : if (priorStop != nullptr) {
134 292222 : priorStop->removeTransportable(this);
135 : }
136 : }
137 1237020 : if (myStep != myPlan->end()) {
138 801055 : if ((*myStep)->getStageType() == MSStageType::WALKING && (prior->getStageType() != MSStageType::ACCESS || prior->getDestination() != (*myStep)->getFromEdge())) {
139 230626 : checkAccess(prior, false);
140 570429 : } else if ((*myStep)->getStageType() == MSStageType::WAITING && prior->getStageType() == MSStageType::WAITING && prior->getDestination() != (*myStep)->getFromEdge()) {
141 22 : checkAccess(prior, false);
142 : }
143 801055 : (*myStep)->proceed(net, this, time, prior);
144 : return true;
145 : } else {
146 435965 : MSNet::getInstance()->getPersonControl().addArrived();
147 435965 : return false;
148 : }
149 : }
150 :
151 :
152 : void
153 0 : MSTransportable::setID(const std::string& /*newID*/) {
154 0 : throw ProcessError(TL("Changing a transportable ID is not permitted"));
155 : }
156 :
157 : SUMOTime
158 0 : MSTransportable::getDesiredDepart() const {
159 0 : return myParameter->depart;
160 : }
161 :
162 : void
163 644109 : MSTransportable::setDeparted(SUMOTime now) {
164 644109 : (*myStep)->setDeparted(now);
165 644109 : }
166 :
167 : SUMOTime
168 122 : MSTransportable::getDeparture() const {
169 122 : for (const MSStage* const stage : *myPlan) {
170 122 : if (stage->getDeparted() >= 0) {
171 122 : return stage->getDeparted();
172 : }
173 : }
174 : return -1;
175 : }
176 :
177 : const MSEdge*
178 0 : MSTransportable::getCurrentEdge() const {
179 0 : const MSLane* lane = getLane();
180 0 : return lane == nullptr ? getEdge() : &lane->getEdge();
181 : }
182 :
183 : double
184 9835227 : MSTransportable::getEdgePos() const {
185 9835227 : return (*myStep)->getEdgePos(MSNet::getInstance()->getCurrentTimeStep());
186 : }
187 :
188 : double
189 4776 : MSTransportable::getBackPositionOnLane(const MSLane* /*lane*/) const {
190 4776 : return getEdgePos() - getVehicleType().getLength();
191 : }
192 :
193 : int
194 395282 : MSTransportable::getDirection() const {
195 395282 : return (*myStep)->getDirection();
196 : }
197 :
198 : Position
199 10241197 : MSTransportable::getPosition() const {
200 10241197 : return (*myStep)->getPosition(MSNet::getInstance()->getCurrentTimeStep());
201 : }
202 :
203 : double
204 5620143 : MSTransportable::getAngle() const {
205 5620143 : return (*myStep)->getAngle(MSNet::getInstance()->getCurrentTimeStep());
206 : }
207 :
208 : double
209 612 : MSTransportable::getWaitingSeconds() const {
210 612 : return STEPS2TIME((*myStep)->getWaitingTime());
211 : }
212 :
213 : double
214 3654527 : MSTransportable::getSpeed() const {
215 3654527 : return (*myStep)->getSpeed();
216 : }
217 :
218 :
219 : void
220 57662 : MSTransportable::tripInfoOutput(OutputDevice& os) const {
221 57662 : SUMOTime departure = myPlan->front()->getDeparted();
222 116172 : os.openTag(isPerson() ? "personinfo" : "containerinfo");
223 57662 : os.writeAttr(SUMO_ATTR_ID, getID());
224 57662 : os.writeAttr(SUMO_ATTR_DEPART, departure >= 0 ? time2string(departure) : "-1");
225 57662 : os.writeAttr(SUMO_ATTR_TYPE, getVehicleType().getID());
226 57662 : if (isPerson()) {
227 56814 : os.writeAttr(SUMO_ATTR_SPEEDFACTOR, getChosenSpeedFactor());
228 : }
229 : SUMOTime duration = 0;
230 : SUMOTime waitingTime = 0;
231 : SUMOTime timeLoss = 0;
232 : SUMOTime travelTime = 0;
233 : bool durationOK = true;
234 : bool waitingTimeOK = true;
235 : bool timeLossOK = true;
236 : bool travelTimeOK = true;
237 193114 : for (MSStage* const i : *myPlan) {
238 135452 : SUMOTime t = i->getDuration();
239 135452 : if (t != SUMOTime_MAX) {
240 134746 : duration += t;
241 : } else {
242 : durationOK = false;
243 : }
244 135452 : t = i->getTotalWaitingTime();
245 135452 : if (t != SUMOTime_MAX) {
246 135417 : waitingTime += t;
247 : } else {
248 : waitingTimeOK = false;
249 : }
250 135452 : t = i->getTimeLoss(this);
251 135452 : if (t != SUMOTime_MAX) {
252 135115 : timeLoss += t;
253 : } else {
254 : timeLossOK = false;
255 : }
256 135452 : t = i->getTravelTime();
257 135452 : if (t != SUMOTime_MAX) {
258 134748 : travelTime += t;
259 : } else {
260 : travelTimeOK = false;
261 : }
262 : }
263 57662 : os.writeAttr(SUMO_ATTR_DURATION, durationOK ? time2string(duration) : "-1");
264 57662 : os.writeAttr(SUMO_ATTR_WAITINGTIME, waitingTimeOK ? time2string(waitingTime) : "-1");
265 57662 : os.writeAttr(SUMO_ATTR_TIMELOSS, timeLossOK ? time2string(timeLoss) : "-1");
266 57662 : os.writeAttr(SUMO_ATTR_TRAVELTIME, travelTimeOK ? time2string(travelTime) : "-1");
267 193114 : for (MSStage* const i : *myPlan) {
268 135452 : i->tripInfoOutput(os, this);
269 : }
270 57662 : os.closeTag();
271 57662 : }
272 :
273 :
274 : void
275 3910 : MSTransportable::routeOutput(OutputDevice& os, const bool withRouteLength) const {
276 : const std::string typeID = (
277 7744 : (isPerson() && getVehicleType().getID() == DEFAULT_PEDTYPE_ID)
278 7744 : || (isContainer() && getVehicleType().getID() == DEFAULT_CONTAINERTYPE_ID)) ? "" : getVehicleType().getID();
279 3986 : myParameter->write(os, OptionsCont::getOptions(), isPerson() ? SUMO_TAG_PERSON : SUMO_TAG_CONTAINER, typeID);
280 3910 : if (hasArrived()) {
281 7516 : os.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
282 : }
283 : const MSStage* previous = nullptr;
284 3910 : const bool withTiming = OptionsCont::getOptions().getBool("vehroute-output.exit-times");
285 15251 : for (const MSStage* const stage : *myPlan) {
286 11341 : stage->routeOutput(myAmPerson, os, withRouteLength, previous, withTiming);
287 : previous = stage;
288 : }
289 3910 : myParameter->writeParams(os);
290 3910 : os.closeTag();
291 3910 : os.lf();
292 3910 : }
293 :
294 :
295 : void
296 32 : MSTransportable::setAbortWaiting(const SUMOTime timeout) {
297 32 : if (timeout < 0 && myAbortCommand != nullptr) {
298 : myAbortCommand->deschedule();
299 16 : myAbortCommand = nullptr;
300 16 : return;
301 : }
302 16 : myAbortCommand = new WrappingCommand<MSTransportable>(this, &MSTransportable::abortStage);
303 16 : MSNet::getInstance()->getBeginOfTimestepEvents()->addEvent(myAbortCommand, SIMSTEP + timeout);
304 : }
305 :
306 :
307 : SUMOTime
308 16 : MSTransportable::abortStage(SUMOTime step) {
309 48 : WRITE_WARNINGF(TL("Teleporting % '%'; waited too long, from edge '%', time=%."),
310 : isPerson() ? "person" : "container", getID(), (*myStep)->getEdge()->getID(), time2string(step));
311 16 : MSTransportableControl& tc = isPerson() ? MSNet::getInstance()->getPersonControl() : MSNet::getInstance()->getContainerControl();
312 : tc.registerTeleportAbortWait();
313 16 : (*myStep)->abort(this);
314 16 : if (!proceed(MSNet::getInstance(), step)) {
315 8 : tc.erase(this);
316 : }
317 16 : return 0;
318 : }
319 :
320 :
321 : void
322 175287 : MSTransportable::appendStage(MSStage* stage, int next) {
323 : // myStep is invalidated upon modifying myPlan
324 175287 : const int stepIndex = (int)(myStep - myPlan->begin());
325 175287 : if (next < 0) {
326 397 : myPlan->push_back(stage);
327 : } else {
328 174890 : if (stepIndex + next > (int)myPlan->size()) {
329 0 : throw ProcessError("invalid index '" + toString(next) + "' for inserting new stage into plan of '" + getID() + "'");
330 : }
331 174890 : myPlan->insert(myPlan->begin() + stepIndex + next, stage);
332 : }
333 175287 : myStep = myPlan->begin() + stepIndex;
334 175287 : }
335 :
336 :
337 : void
338 2138 : MSTransportable::removeStage(int next, bool stayInSim) {
339 : assert(myStep + next < myPlan->end());
340 : assert(next >= 0);
341 2138 : if (next > 0) {
342 : // myStep is invalidated upon modifying myPlan
343 84 : int stepIndex = (int)(myStep - myPlan->begin());
344 84 : delete *(myStep + next);
345 84 : myPlan->erase(myStep + next);
346 84 : myStep = myPlan->begin() + stepIndex;
347 : } else {
348 2054 : if (myStep + 1 == myPlan->end() && stayInSim) {
349 : // stay in the simulation until the start of simStep to allow appending new stages (at the correct position)
350 172 : appendStage(new MSStageWaiting(getEdge(), nullptr, 0, 0, getEdgePos(), "last stage removed", false));
351 1968 : } else if (myStep + 1 != myPlan->end()) {
352 3562 : (*(myStep + 1))->setOrigin(getEdge(), getEdge() == getFromEdge() ? (*myStep)->getOriginStop() : nullptr, getEdgePos());
353 : }
354 2054 : (*myStep)->abort(this);
355 2054 : if (!proceed(MSNet::getInstance(), SIMSTEP)) {
356 187 : MSNet::getInstance()->getPersonControl().erase(this);
357 1867 : } else if (myPlan->front()->getDeparted() < 0) {
358 46 : myPlan->front()->setDeparted(SIMSTEP);
359 : }
360 : }
361 2138 : }
362 :
363 :
364 : void
365 15 : MSTransportable::setSpeed(double speed) {
366 40 : for (MSTransportablePlan::const_iterator i = myStep; i != myPlan->end(); ++i) {
367 25 : (*i)->setSpeed(speed);
368 : }
369 15 : getSingularType().setMaxSpeed(speed);
370 15 : }
371 :
372 :
373 : bool
374 0 : MSTransportable::replaceRoute(ConstMSRoutePtr newRoute, const std::string& /* info */, bool /* onInit */, int /* offset */, bool /* addRouteStops */, bool /* removeStops */, std::string* /* msgReturn */) {
375 0 : if (isPerson()) {
376 0 : static_cast<MSPerson*>(this)->replaceWalk(newRoute->getEdges(), getPositionOnLane(), 0, 1);
377 0 : return true;
378 : }
379 : return false;
380 : }
381 :
382 :
383 : bool
384 118 : MSTransportable::reroute(SUMOTime t, const std::string& /* info */, MSTransportableRouter& router, const bool /* onInit */, const bool /* withTaz */, const bool /* silent */, const MSEdge* /* sink */) {
385 : MSStageTrip* trip = getCurrentStage()->getTrip();
386 118 : if (trip == nullptr) {
387 : // TODO this should be possible after factoring out MSStageTrip::reroute
388 : return false;
389 : }
390 118 : if (getCurrentStage()->getVehicle() != nullptr) {
391 : // TODO rerouting during a ride still needs to be implemented
392 : return false;
393 : }
394 : // find the final stage of the trip
395 : int tripEndOffset = -1;
396 94 : for (int i = getNumRemainingStages() - 1; i >= 0; i--) {
397 94 : if (getNextStage(i)->getTrip() == trip) {
398 : tripEndOffset = i;
399 : break;
400 : }
401 : }
402 : std::vector<MSStage*> stages;
403 94 : MSStageWaiting start(getEdge(), getCurrentStage()->getOriginStop(), -1, t, getEdgePos(), "start", true);
404 94 : if (trip->reroute(t, router, this, &start, getEdge(), getRerouteDestination(), stages) == "") {
405 : // check whether the new plan actually differs
406 175 : while (tripEndOffset >= 0 && !stages.empty() && stages.back()->equals(*getNextStage(tripEndOffset))) {
407 81 : delete stages.back();
408 : stages.pop_back();
409 81 : tripEndOffset--;
410 : }
411 : bool abortCurrent = true;
412 : // check whether the future route of the current stage is identical to the route
413 94 : if (!stages.empty() && stages.front()->isWalk() && getCurrentStage()->isWalk()) {
414 : // TODO this check should be done for rides as well
415 : MSStageMoving* s = static_cast<MSStageMoving*>(getCurrentStage());
416 57 : int routeIndex = (int)(s->getRouteStep() - s->getRoute().begin());
417 57 : ConstMSEdgeVector oldEdges = s->getEdges();
418 : oldEdges.erase(oldEdges.begin(), oldEdges.begin() + routeIndex);
419 57 : ConstMSEdgeVector newEdges = stages.front()->getEdges();
420 57 : if (newEdges == oldEdges) {
421 57 : delete stages.front();
422 : stages.erase(stages.begin());
423 : abortCurrent = false;
424 : }
425 57 : }
426 94 : if (stages.empty()) {
427 : return false;
428 : }
429 : // remove future stages of the trip
430 14 : for (int i = tripEndOffset; i >= 1; i--) {
431 7 : removeStage(i);
432 : }
433 : // insert new stages of the rerouting
434 : int idx = 1;
435 14 : for (MSStage* stage : stages) {
436 7 : appendStage(stage, idx++);
437 : }
438 7 : if (abortCurrent) {
439 0 : removeStage(0);
440 : }
441 7 : return true;
442 : }
443 : return false;
444 94 : }
445 :
446 :
447 : void
448 197 : MSTransportable::replaceVehicleType(const MSVehicleType* type) {
449 197 : const SUMOVehicleClass oldVClass = myVType->getVehicleClass();
450 197 : if (myVType->isVehicleSpecific()) {
451 1 : MSNet::getInstance()->getVehicleControl().removeVType(myVType);
452 : }
453 197 : if (isPerson()
454 197 : && type->getVehicleClass() != oldVClass
455 10 : && type->getVehicleClass() != SVC_PEDESTRIAN
456 202 : && !type->getParameter().wasSet(VTYPEPARS_VEHICLECLASS_SET)) {
457 15 : WRITE_WARNINGF(TL("Person '%' receives type '%' which implicitly uses unsuitable vClass '%'."), getID(), type->getID(), toString(type->getVehicleClass()));
458 : }
459 197 : myVType = type;
460 197 : }
461 :
462 :
463 : MSVehicleType&
464 203 : MSTransportable::getSingularType() {
465 203 : if (myVType->isVehicleSpecific()) {
466 : return *const_cast<MSVehicleType*>(myVType);
467 : }
468 362 : MSVehicleType* type = myVType->buildSingularType(myVType->getID() + "@" + getID());
469 181 : replaceVehicleType(type);
470 181 : return *type;
471 : }
472 :
473 :
474 : PositionVector
475 1099196 : MSTransportable::getBoundingBox() const {
476 1099196 : PositionVector centerLine;
477 1099196 : const Position p = getPosition();
478 1099196 : const double angle = getAngle();
479 1099196 : const double length = getVehicleType().getLength();
480 1099196 : const Position back = p + Position(-cos(angle) * length, -sin(angle) * length);
481 1099196 : centerLine.push_back(p);
482 1099196 : centerLine.push_back(back);
483 1099196 : centerLine.move2side(0.5 * getVehicleType().getWidth());
484 : PositionVector result = centerLine;
485 1099196 : centerLine.move2side(-getVehicleType().getWidth());
486 1099196 : result.append(centerLine.reverse(), POSITION_EPS);
487 : //std::cout << " transp=" << getID() << " p=" << p << " angle=" << GeomHelper::naviDegree(angle) << " back=" << back << " result=" << result << "\n";
488 1099196 : return result;
489 1099196 : }
490 :
491 :
492 : std::string
493 0 : MSTransportable::getStageSummary(int stageIndex) const {
494 : assert(stageIndex < (int)myPlan->size());
495 : assert(stageIndex >= 0);
496 0 : return (*myPlan)[stageIndex]->getStageSummary(myAmPerson);
497 : }
498 :
499 :
500 : const std::set<SUMOTrafficObject::NumericalID>
501 16 : MSTransportable::getUpcomingEdgeIDs() const {
502 : std::set<SUMOTrafficObject::NumericalID> result;
503 40 : for (auto step = myStep; step != myPlan->end(); ++step) {
504 104 : for (const MSEdge* const e : (*step)->getEdges()) {
505 80 : if (e != nullptr) {
506 : // catch incomplete rides
507 72 : result.insert(e->getNumericalID());
508 : }
509 24 : }
510 : }
511 16 : return result;
512 : }
513 :
514 :
515 : bool
516 4371354 : MSTransportable::hasArrived() const {
517 4371354 : return myStep == myPlan->end();
518 : }
519 :
520 : bool
521 527876 : MSTransportable::hasDeparted() const {
522 527876 : return myPlan->size() > 0 && (myPlan->front()->getDeparted() >= 0 || myStep > myPlan->begin());
523 : }
524 :
525 :
526 : void
527 52 : MSTransportable::rerouteParkingArea(MSStoppingPlace* orig, MSStoppingPlace* replacement) {
528 : // check whether the transportable was riding to the orignal stop
529 : // @note: parkingArea can currently not be set as myDestinationStop so we
530 : // check for stops on the edge instead
531 : #ifdef DEBUG_PARKING
532 : std::cout << SIMTIME << " person=" << getID() << " rerouteParkingArea orig=" << orig->getID() << " replacement=" << replacement->getID() << "\n";
533 : #endif
534 : assert(getCurrentStageType() == MSStageType::DRIVING);
535 52 : if (!myAmPerson) {
536 0 : WRITE_WARNING(TL("parkingAreaReroute not supported for containers"));
537 0 : return;
538 : }
539 52 : if (getDestination() == &orig->getLane().getEdge()) {
540 52 : MSStageDriving* const stage = dynamic_cast<MSStageDriving*>(*myStep);
541 : assert(stage != 0);
542 : assert(stage->getVehicle() != 0);
543 : // adapt plan
544 52 : stage->setDestination(&replacement->getLane().getEdge(), replacement);
545 52 : stage->setArrivalPos((replacement->getBeginLanePosition() + replacement->getEndLanePosition()) / 2);
546 : #ifdef DEBUG_PARKING
547 : std::cout << " set ride destination\n";
548 : #endif
549 52 : if (myStep + 1 == myPlan->end()) {
550 : return;
551 : }
552 : // if the next step is a walk, adapt the route
553 52 : MSStage* nextStage = *(myStep + 1);
554 52 : if (nextStage->getStageType() == MSStageType::TRIP) {
555 40 : dynamic_cast<MSStageTrip*>(nextStage)->setOrigin(stage->getDestination(), stage->getDestinationStop(), stage->getArrivalPos());
556 : #ifdef DEBUG_PARKING
557 : std::cout << " set subsequent trip origin\n";
558 : #endif
559 12 : } else if (nextStage->getStageType() == MSStageType::WALKING) {
560 : #ifdef DEBUG_PARKING
561 : std::cout << " replace subsequent walk with a trip\n";
562 : #endif
563 : MSStageTrip* newStage = new MSStageTrip(stage->getDestination(), nullptr, nextStage->getDestination(),
564 6 : nextStage->getDestinationStop(), -1, 0, "", -1, 1, getID(), 0, true, nextStage->getArrivalPos());
565 6 : removeStage(1);
566 6 : appendStage(newStage, 1);
567 6 : } else if (nextStage->getStageType() == MSStageType::WAITING) {
568 : #ifdef DEBUG_PARKING
569 : std::cout << " add subsequent walk to reach stop\n";
570 : std::cout << " arrivalPos=" << nextStage->getArrivalPos() << "\n";
571 : #endif
572 : MSStageTrip* newStage = new MSStageTrip(stage->getDestination(), nullptr, nextStage->getDestination(),
573 6 : nextStage->getDestinationStop(), -1, 0, "", -1, 1, getID(), 0, true, nextStage->getArrivalPos());
574 6 : appendStage(newStage, 1);
575 : }
576 : // if the plan contains another ride with the same vehicle from the same
577 : // parking area, adapt the preceeding walk to end at the replacement
578 106 : for (auto it = myStep + 2; it != myPlan->end(); it++) {
579 66 : MSStage* const futureStage = *it;
580 66 : MSStage* const prevStage = *(it - 1);
581 66 : if (futureStage->getStageType() == MSStageType::DRIVING) {
582 : MSStageDriving* const ds = static_cast<MSStageDriving*>(futureStage);
583 : // ride origin is set implicitly from the walk destination
584 12 : ds->setOrigin(nullptr, nullptr, -1);
585 : if (ds->getLines() == stage->getLines()
586 12 : && prevStage->getDestination() == &orig->getLane().getEdge()) {
587 12 : if (prevStage->getStageType() == MSStageType::TRIP) {
588 6 : dynamic_cast<MSStageTrip*>(prevStage)->setDestination(stage->getDestination(), replacement);
589 : #ifdef DEBUG_PARKING
590 : std::cout << " replace later trip before ride (" << (it - myPlan->begin()) << ")\n";
591 : #endif
592 6 : } else if (prevStage->getStageType() == MSStageType::WALKING) {
593 : #ifdef DEBUG_PARKING
594 : std::cout << " replace later walk before ride (" << (it - myPlan->begin()) << ")\n";
595 : #endif
596 6 : MSStageTrip* newStage = new MSStageTrip(prevStage->getFromEdge(), nullptr, stage->getDestination(),
597 12 : replacement, -1, 0, "", -1, 1, getID(), 0, true, stage->getArrivalPos());
598 6 : int prevStageRelIndex = (int)(it - 1 - myStep);
599 6 : removeStage(prevStageRelIndex);
600 6 : appendStage(newStage, prevStageRelIndex);
601 : }
602 : break;
603 : }
604 : }
605 : }
606 : }
607 : }
608 :
609 :
610 : MSDevice*
611 1899766 : MSTransportable::getDevice(const std::type_info& type) const {
612 2112250 : for (MSTransportableDevice* const dev : myDevices) {
613 1987880 : if (typeid(*dev) == type) {
614 : return dev;
615 : }
616 : }
617 : return nullptr;
618 : }
619 :
620 :
621 : void
622 10 : MSTransportable::setJunctionModelParameter(const std::string& key, const std::string& value) {
623 15 : if (key == toString(SUMO_ATTR_JM_IGNORE_IDS) || key == toString(SUMO_ATTR_JM_IGNORE_TYPES)) {
624 5 : getParameter().parametersSet |= VEHPARS_JUNCTIONMODEL_PARAMS_SET;
625 5 : const_cast<SUMOVehicleParameter&>(getParameter()).setParameter(key, value);
626 : // checked in MSLink::ignoreFoe
627 : } else {
628 15 : throw InvalidArgument(getObjectType() + " '" + getID() + "' does not support junctionModel parameter '" + key + "'");
629 : }
630 5 : }
631 :
632 :
633 : double
634 0 : MSTransportable::getSlope() const {
635 0 : const MSEdge* edge = getEdge();
636 0 : const double ep = getEdgePos();
637 0 : const double gp = edge->getLanes()[0]->interpolateLanePosToGeometryPos(ep);
638 0 : return edge->getLanes()[0]->getShape().slopeDegreeAtOffset(gp);
639 : }
640 :
641 :
642 : SUMOTime
643 9716 : MSTransportable::getWaitingTime(const bool /* accumulated */) const {
644 9716 : return (*myStep)->getWaitingTime();
645 : }
646 :
647 :
648 : double
649 107536835 : MSTransportable::getMaxSpeed() const {
650 107536835 : return MIN2(getVehicleType().getMaxSpeed(), getVehicleType().getDesiredMaxSpeed() * getChosenSpeedFactor());
651 : }
652 :
653 :
654 : SUMOVehicleClass
655 2417070 : MSTransportable::getVClass() const {
656 2417070 : return getVehicleType().getVehicleClass();
657 : }
658 :
659 :
660 : int
661 0 : MSTransportable::getRoutingMode() const {
662 : /// @todo: allow configuring routing mode
663 0 : return libsumo::ROUTING_MODE_DEFAULT;
664 : }
665 :
666 :
667 : void
668 0 : MSTransportable::setRoutingMode(int value) {
669 : /// @todo: allow configuring routing mode
670 : UNUSED_PARAMETER(value);
671 0 : }
672 :
673 : void
674 215 : MSTransportable::saveState(OutputDevice& out) {
675 : // this saves lots of departParameters which are only needed for transportables that did not yet depart
676 : // the parameters may hold the name of a vTypeDistribution but we are interested in the actual type
677 215 : const SUMOTime desiredDepart = myParameter->depart;
678 215 : if (myPlan->front()->getDeparted() >= 0) {
679 : // this is only relevant in the context of delayed departure (max-num-persons)
680 130 : const_cast<SUMOVehicleParameter*>(myParameter)->depart = myPlan->front()->getDeparted();
681 : }
682 215 : myParameter->write(out, OptionsCont::getOptions(), myAmPerson ? SUMO_TAG_PERSON : SUMO_TAG_CONTAINER, getVehicleType().getID());
683 215 : const_cast<SUMOVehicleParameter*>(myParameter)->depart = desiredDepart;
684 215 : if (!myParameter->wasSet(VEHPARS_SPEEDFACTOR_SET)) {
685 215 : out.setPrecision(MAX2(gPrecisionRandom, gPrecision));
686 215 : out.writeAttr(SUMO_ATTR_SPEEDFACTOR, getChosenSpeedFactor());
687 215 : out.setPrecision(gPrecision);
688 : }
689 215 : int stepIdx = (int)(myStep - myPlan->begin());
690 385 : for (auto it = myPlan->begin(); it != myStep; ++it) {
691 170 : const MSStageType st = (*it)->getStageType();
692 170 : if (st == MSStageType::TRIP || st == MSStageType::ACCESS) {
693 30 : stepIdx--;
694 : }
695 : }
696 215 : const bool isAccess = (*myStep)->getStageType() == MSStageType::ACCESS;
697 215 : std::ostringstream state;
698 645 : state << myParameter->parametersSet << " " << (isAccess ? toString(stepIdx - 0.5, 1) : toString(stepIdx));
699 215 : (*myStep)->saveState(state, this);
700 215 : out.writeAttr(SUMO_ATTR_STATE, state.str());
701 : const MSStage* previous = nullptr;
702 691 : for (const MSStage* const stage : *myPlan) {
703 476 : const bool routeLength = stage->getDeparted() >= 0 && stage->getStageType() == MSStageType::DRIVING;
704 476 : const bool withTiming = stage->getDeparted() >= 0;
705 476 : stage->routeOutput(myAmPerson, out, routeLength, previous, withTiming, true);
706 : previous = stage;
707 : }
708 215 : out.closeTag();
709 215 : }
710 :
711 :
712 : void
713 219 : MSTransportable::loadState(const std::string& state) {
714 219 : std::istringstream iss(state);
715 : double step;
716 219 : iss >> myParameter->parametersSet >> step;
717 219 : myPlan->front()->setDeparted(myParameter->depart);
718 219 : if (step != floor(step)) {
719 : // we are in an access stage
720 2 : int priorIndex = (int)(step - 0.5);
721 2 : MSStage* prior = *(myPlan->begin() + priorIndex);
722 2 : myStep = myPlan->begin() + priorIndex + 1;
723 : bool waitAtStop = prior->getDestinationStop() != nullptr
724 2 : && &prior->getDestinationStop()->getLane().getEdge() != prior->getDestination();
725 2 : checkAccess(prior, waitAtStop);
726 : //std::cout << " step=" << step << " i=" << getCurrentStageIndex() << " stage=" << getStageSummary(true) << "\n";
727 : } else {
728 217 : myStep = myPlan->begin() + (int)step;
729 : }
730 219 : (*myStep)->loadState(this, iss);
731 229 : for (int i = 1; i < step; i++) {
732 10 : if ((*myPlan)[i]->getStageType() == MSStageType::DRIVING) {
733 4 : dynamic_cast<MSStageDriving*>((*myPlan)[i])->setWaitingSince((*myPlan)[i - 1]->getArrived());
734 : }
735 : }
736 219 : }
737 :
738 :
739 : /****************************************************************************/
|