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 MSPerson.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @author Laura Bieker
19 : /// @date Mon, 9 Jul 2001
20 : ///
21 : // The class for modelling person-movements
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <string>
26 : #include <vector>
27 : #include <utils/iodevices/OutputDevice.h>
28 : #include <utils/options/OptionsCont.h>
29 : #include <utils/common/ToString.h>
30 : #include <utils/common/StringUtils.h>
31 : #include <utils/geom/GeomHelper.h>
32 : #include <utils/router/IntermodalNetwork.h>
33 : #include <microsim/MSNet.h>
34 : #include <microsim/MSEdge.h>
35 : #include <microsim/MSLane.h>
36 : #include <microsim/transportables/MSTransportableControl.h>
37 : #include <microsim/MSInsertionControl.h>
38 : #include <microsim/MSEventControl.h>
39 : #include <microsim/MSVehicle.h>
40 : #include <microsim/MSVehicleControl.h>
41 : #include <microsim/MSStoppingPlace.h>
42 : #include <microsim/MSRouteHandler.h>
43 : #include <microsim/devices/MSDevice_Taxi.h>
44 : #include <microsim/trigger/MSTriggeredRerouter.h>
45 : #include "MSPModel_Striping.h"
46 : #include "MSStageTrip.h"
47 : #include "MSStageWalking.h"
48 : #include "MSPerson.h"
49 :
50 :
51 : // ===========================================================================
52 : // method definitions
53 : // ===========================================================================
54 : /* -------------------------------------------------------------------------
55 : * MSPerson::MSPersonStage_Access - methods
56 : * ----------------------------------------------------------------------- */
57 4269 : MSPerson::MSPersonStage_Access::MSPersonStage_Access(const MSEdge* destination, MSStoppingPlace* toStop,
58 : const double arrivalPos, const double arrivalPosLat, const double dist, const bool isExit,
59 4269 : const Position& startPos, const Position& endPos) :
60 : MSStage(MSStageType::ACCESS, destination, toStop, arrivalPos, arrivalPosLat),
61 8538 : myDist(dist), myAmExit(isExit) {
62 4269 : myPath.push_back(startPos);
63 4269 : myPath.push_back(endPos);
64 4269 : }
65 :
66 :
67 8538 : MSPerson::MSPersonStage_Access::~MSPersonStage_Access() {}
68 :
69 : MSStage*
70 0 : MSPerson::MSPersonStage_Access::clone() const {
71 0 : return new MSPersonStage_Access(myDestination, myDestinationStop, myArrivalPos, myArrivalPosLat, myDist, myAmExit, myPath.front(), myPath.back());
72 : }
73 :
74 : void
75 4267 : MSPerson::MSPersonStage_Access::proceed(MSNet* net, MSTransportable* person, SUMOTime now, MSStage* previous) {
76 4267 : myDeparted = now;
77 4267 : if (myDist >= 0) {
78 4253 : myEstimatedArrival = now + TIME2STEPS(myDist / person->getMaxSpeed());
79 : } else {
80 14 : myEstimatedArrival = now + previous->getJumpDuration();
81 : }
82 : // TODO myEstimatedArrival is not a multiple of DELTA_T here. This might give a problem because the destination position will not be reached precisely
83 4267 : MSEdge* edge = myDestinationStop != nullptr ? &myDestinationStop->getLane().getEdge() : const_cast<MSEdge*>(myDestination);
84 4267 : net->getBeginOfTimestepEvents()->addEvent(new ProceedCmd(person, edge), myEstimatedArrival);
85 4267 : net->getPersonControl().startedAccess();
86 4267 : edge->addTransportable(person);
87 4267 : }
88 :
89 :
90 : std::string
91 19022 : MSPerson::MSPersonStage_Access::getStageDescription(const bool /* isPerson */) const {
92 19022 : return "access";
93 : }
94 :
95 :
96 : std::string
97 0 : MSPerson::MSPersonStage_Access::getStageSummary(const bool /* isPerson */) const {
98 0 : if (myDestination == nullptr) {
99 0 : return ("jump to edge '") + getDestination()->getID() + "'";
100 : } else {
101 0 : return (myAmExit ? "access from stop '" : "access to stop '") + getDestinationStop()->getID() + "'";
102 : }
103 : }
104 :
105 :
106 : Position
107 20036 : MSPerson::MSPersonStage_Access::getPosition(SUMOTime now) const {
108 20036 : return myPath.positionAtOffset(myPath.length() * (double)(now - myDeparted) / (double)(myEstimatedArrival - myDeparted));
109 : }
110 :
111 :
112 : double
113 20036 : MSPerson::MSPersonStage_Access::getAngle(SUMOTime /* now */) const {
114 20036 : return myPath.angleAt2D(0);
115 : }
116 :
117 :
118 : double
119 38044 : MSPerson::MSPersonStage_Access::getSpeed() const {
120 38044 : return myDist / STEPS2TIME(MAX2((SUMOTime)1, myEstimatedArrival - myDeparted));
121 : }
122 :
123 : void
124 2983 : MSPerson::MSPersonStage_Access::tripInfoOutput(OutputDevice& os, const MSTransportable* const) const {
125 5966 : os.openTag("access");
126 2983 : if (getDestinationStop() != nullptr) {
127 2975 : os.writeAttr("stop", getDestinationStop()->getID());
128 : }
129 2983 : os.writeAttr("depart", time2string(myDeparted));
130 2983 : os.writeAttr("arrival", myArrived >= 0 ? time2string(myArrived) : "-1");
131 2983 : os.writeAttr("duration", myArrived > 0 ? time2string(getDuration()) : "-1");
132 2983 : os.writeAttr("routeLength", myDist);
133 2983 : os.closeTag();
134 2983 : }
135 :
136 :
137 : SUMOTime
138 4237 : MSPerson::MSPersonStage_Access::ProceedCmd::execute(SUMOTime currentTime) {
139 4237 : MSNet::getInstance()->getPersonControl().endedAccess();
140 4237 : myStopEdge->removeTransportable(myPerson);
141 4237 : if (!myPerson->proceed(MSNet::getInstance(), currentTime)) {
142 54 : MSNet::getInstance()->getPersonControl().erase(myPerson);
143 : }
144 4237 : return 0;
145 : }
146 :
147 :
148 : void
149 2 : MSPerson::MSPersonStage_Access::saveState(std::ostringstream& out, MSTransportable* /*transportable*/) {
150 4 : out << " " << myDeparted << " " << myEstimatedArrival;
151 2 : }
152 :
153 :
154 : void
155 2 : MSPerson::MSPersonStage_Access::loadState(MSTransportable* person, std::istringstream& state) {
156 2 : state >> myDeparted;
157 2 : state >> myEstimatedArrival;
158 2 : MSNet* net = MSNet::getInstance();
159 2 : MSEdge* edge = myDestinationStop != nullptr ? &myDestinationStop->getLane().getEdge() : const_cast<MSEdge*>(myDestination);
160 2 : net->getBeginOfTimestepEvents()->addEvent(new ProceedCmd(person, edge), myEstimatedArrival);
161 2 : net->getPersonControl().startedAccess();
162 2 : edge->addTransportable(person);
163 2 : }
164 :
165 : /* -------------------------------------------------------------------------
166 : * MSPerson - methods
167 : * ----------------------------------------------------------------------- */
168 272136 : MSPerson::MSPerson(const SUMOVehicleParameter* pars, MSVehicleType* vtype, MSTransportable::MSTransportablePlan* plan, const double speedFactor) :
169 : MSTransportable(pars, vtype, plan, true),
170 272136 : myInfluencer(nullptr),
171 544272 : myChosenSpeedFactor(pars->speedFactor < 0 ? speedFactor : pars->speedFactor),
172 272376 : myTimegapCrossing(getFloatParam("pedestrian.timegap-crossing"))
173 272136 : { }
174 :
175 :
176 514717 : MSPerson::~MSPerson() {
177 272085 : delete myInfluencer;
178 514717 : }
179 :
180 :
181 : bool
182 461968 : MSPerson::checkAccess(const MSStage* const prior, const bool waitAtStop) {
183 : MSStoppingPlace* prevStop = prior->getDestinationStop();
184 461968 : if (!waitAtStop && prior->getStageType() == MSStageType::TRIP) {
185 164545 : prevStop = prior->getOriginStop();
186 : }
187 461968 : if (prevStop != nullptr) {
188 60572 : const MSEdge* const accessEdge = waitAtStop ? prior->getDestination() : (*myStep)->getFromEdge();
189 60572 : const MSStoppingPlace::Access* const access = prevStop->getAccess(accessEdge);
190 60572 : if (access != nullptr) {
191 4255 : const MSLane* const lane = accessEdge->getLanes()[0];
192 4255 : MSStage* newStage = nullptr;
193 4255 : if (waitAtStop) {
194 2092 : const MSEdge* const stopEdge = &prevStop->getLane().getEdge();
195 2092 : const double arrivalAtBs = (prevStop->getBeginLanePosition() + prevStop->getEndLanePosition()) / 2;
196 4184 : newStage = new MSPersonStage_Access(stopEdge, prevStop, arrivalAtBs, 0.0, access->length, false,
197 2092 : lane->geometryPositionAtOffset(access->endPos),
198 4184 : prevStop->getLane().geometryPositionAtOffset(arrivalAtBs));
199 : } else {
200 3876 : const bool useDoors = access->exit == MSStoppingPlace::AccessExit::DOORS ||
201 7302 : (OptionsCont::getOptions().getString("pedestrian.model") != "jupedsim" && access->exit == MSStoppingPlace::AccessExit::CARRIAGE);
202 2163 : if (access->exit == MSStoppingPlace::AccessExit::CARRIAGE) {
203 0 : const double startPos = prior->getStageType() == MSStageType::TRIP ? prior->getEdgePos(0) : prior->getArrivalPos();
204 0 : const double startPosLat = prior->getStageType() == MSStageType::TRIP ? prior->getEdgePosLat(0) : prior->getArrivalPosLat();
205 : // The start and end attributes of the access stage are equal in this case, but we need to compute the arrival position relatively
206 : // to the current lane and not the lane of the previous stage.
207 0 : const Position start = prevStop->getLane().geometryPositionAtOffset(startPos, startPosLat);
208 0 : const Position end = lane->getShape().transformToVectorCoordinates(start);
209 0 : newStage = new MSPersonStage_Access(accessEdge, prevStop, end.x(), -end.y(), access->length, true, start, start);
210 : } else {
211 2163 : const double startPos = prior->getStageType() == MSStageType::TRIP ? prior->getEdgePos(0) : prior->getArrivalPos();
212 2163 : const Position& trainExit = prevStop->getLane().geometryPositionAtOffset(startPos);
213 2163 : const double arrivalPos = useDoors ? lane->getShape().nearest_offset_to_point2D(trainExit) : access->endPos;
214 : Position platformEntry = lane->geometryPositionAtOffset(arrivalPos);
215 2163 : if (useDoors) {
216 : // find the closer side of the platform to enter
217 900 : const double halfWidth = lane->getWidth() / 2. - MAX2(getVehicleType().getLength(), getVehicleType().getWidth()) / 2. - POSITION_EPS;
218 450 : platformEntry = lane->geometryPositionAtOffset(arrivalPos, halfWidth);
219 450 : const Position& plat2 = lane->geometryPositionAtOffset(arrivalPos, -halfWidth);
220 450 : if (trainExit.distanceSquaredTo2D(plat2) < trainExit.distanceSquaredTo2D(platformEntry)) {
221 356 : platformEntry = plat2;
222 : }
223 : }
224 2163 : newStage = new MSPersonStage_Access(accessEdge, prevStop, arrivalPos, 0.0, access->length, true,
225 2163 : trainExit, platformEntry);
226 : }
227 : }
228 4255 : newStage->setTrip(prior->getTrip());
229 4255 : myStep = myPlan->insert(myStep, newStage);
230 : return true;
231 : }
232 : }
233 457713 : if (prior->getJumpDuration() > 0) {
234 : // negative distance indicates jump
235 : MSStage* newStage = new MSPersonStage_Access(getDestination(), nullptr, getArrivalPos(), 0.0, -1, true,
236 28 : prior->getPosition(SIMSTEP), (*myStep)->getPosition(SIMSTEP));
237 14 : myStep = myPlan->insert(myStep, newStage);
238 : return true;
239 : }
240 : return false;
241 : }
242 :
243 :
244 : double
245 8 : MSPerson::getImpatience() const {
246 8 : return MAX2(0., MIN2(1., getVehicleType().getImpatience()
247 8 : + STEPS2TIME((*myStep)->getWaitingTime()) / MSPModel_Striping::MAX_WAIT_TOLERANCE));
248 : }
249 :
250 : const std::string&
251 2633 : MSPerson::getNextEdge() const {
252 : // if (getCurrentStageType() == WALKING) {
253 : // MSStageWalking* walkingStage = dynamic_cast<MSStageWalking*>(*myStep);
254 : // assert(walkingStage != 0);
255 : // const MSEdge* nextEdge = walkingStage->getPedestrianState()->getNextEdge(*walkingStage);
256 : // if (nextEdge != 0) {
257 : // return nextEdge->getID();
258 : // }
259 : // }
260 : // return StringUtils::emptyString;
261 2633 : const MSEdge* nextEdge = getNextEdgePtr();
262 2633 : if (nextEdge != nullptr) {
263 2623 : return nextEdge->getID();
264 : }
265 : return StringUtils::emptyString;
266 : }
267 :
268 :
269 : const MSEdge*
270 166695 : MSPerson::getNextEdgePtr() const {
271 166695 : if (getCurrentStageType() == MSStageType::WALKING) {
272 166695 : MSStageWalking* walkingStage = dynamic_cast<MSStageWalking*>(*myStep);
273 : assert(walkingStage != nullptr);
274 166695 : return walkingStage->getPState()->getNextEdge(*walkingStage);
275 : }
276 : return nullptr;
277 : }
278 :
279 :
280 :
281 : void
282 1507 : MSPerson::replaceWalk(const ConstMSEdgeVector& newEdges, double departPos, int firstIndex, int nextIndex) {
283 : assert(nextIndex > firstIndex);
284 : //std::cout << SIMTIME << " reroute person " << getID()
285 : // << " newEdges=" << toString(newEdges)
286 : // << " firstIndex=" << firstIndex
287 : // << " nextIndex=" << nextIndex
288 : // << " departPos=" << getEdgePos()
289 : // << " arrivalPos=" << getNextStage(nextIndex - 1)->getArrivalPos()
290 : // << "\n";
291 1507 : MSStage* const toBeReplaced = getNextStage(nextIndex - 1);
292 : MSStageWalking* newStage = new MSStageWalking(getID(), newEdges,
293 : toBeReplaced->getDestinationStop(), -1,
294 : -1,
295 : departPos,
296 1507 : toBeReplaced->getArrivalPos(),
297 1507 : MSPModel::UNSPECIFIED_POS_LAT);
298 1507 : appendStage(newStage, nextIndex);
299 : // remove stages in reverse order so that proceed will only be called at the last removal
300 3019 : for (int i = nextIndex - 1; i >= firstIndex; i--) {
301 : //std::cout << " removeStage=" << i << "\n";
302 1512 : removeStage(i);
303 : }
304 1507 : }
305 :
306 :
307 : MSPerson::Influencer&
308 5209097 : MSPerson::getInfluencer() {
309 5209097 : if (myInfluencer == nullptr) {
310 227 : myInfluencer = new Influencer();
311 : }
312 5209097 : return *myInfluencer;
313 : }
314 :
315 :
316 : const MSPerson::Influencer*
317 0 : MSPerson::getInfluencer() const {
318 0 : return myInfluencer;
319 : }
320 :
321 :
322 :
323 : /* -------------------------------------------------------------------------
324 : * methods of MSPerson::Influencer
325 : * ----------------------------------------------------------------------- */
326 227 : MSPerson::Influencer::Influencer() {}
327 :
328 :
329 227 : MSPerson::Influencer::~Influencer() {}
330 :
331 :
332 : void
333 1732038 : MSPerson::Influencer::setRemoteControlled(Position xyPos, MSLane* l, double pos, double posLat, double angle, int edgeOffset, const ConstMSEdgeVector& route, SUMOTime t) {
334 1732038 : myRemoteXYPos = xyPos;
335 1732038 : myRemoteLane = l;
336 1732038 : myRemotePos = pos;
337 1732038 : myRemotePosLat = posLat;
338 1732038 : myRemoteAngle = angle;
339 1732038 : myRemoteEdgeOffset = edgeOffset;
340 1732038 : myRemoteRoute = route;
341 1732038 : myLastRemoteAccess = t;
342 1732038 : }
343 :
344 :
345 : bool
346 1745021 : MSPerson::Influencer::isRemoteControlled() const {
347 1745021 : return myLastRemoteAccess == MSNet::getInstance()->getCurrentTimeStep();
348 : }
349 :
350 :
351 : bool
352 0 : MSPerson::Influencer::isRemoteAffected(SUMOTime t) const {
353 0 : return myLastRemoteAccess >= t - TIME2STEPS(10);
354 : }
355 :
356 :
357 : void
358 1732038 : MSPerson::Influencer::postProcessRemoteControl(MSPerson* p) {
359 : /*
360 : std::cout << SIMTIME << " moveToXY person=" << p->getID()
361 : << " xyPos=" << myRemoteXYPos
362 : << " lane=" << Named::getIDSecure(myRemoteLane)
363 : << " pos=" << myRemotePos
364 : << " posLat=" << myRemotePosLat
365 : << " angle=" << myRemoteAngle
366 : << " eOf=" << myRemoteEdgeOffset
367 : << " route=" << toString(myRemoteRoute)
368 : << " aTime=" << time2string(myLastRemoteAccess)
369 : << "\n";
370 : */
371 1732038 : switch (p->getStageType(0)) {
372 : case MSStageType::WALKING: {
373 1731998 : MSStageWalking* s = dynamic_cast<MSStageWalking*>(p->getCurrentStage());
374 : assert(s != nullptr);
375 1731998 : s->getPState()->moveToXY(p, myRemoteXYPos, myRemoteLane, myRemotePos, myRemotePosLat,
376 1731998 : myRemoteAngle, myRemoteEdgeOffset, myRemoteRoute,
377 : MSNet::getInstance()->getCurrentTimeStep());
378 : }
379 1731998 : break;
380 : default:
381 : break;
382 : }
383 1732038 : }
384 :
385 :
386 : /****************************************************************************/
|