Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2014-2025 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 MSPModel_NonInteracting.cpp
15 : /// @author Jakob Erdmann
16 : /// @date Mon, 13 Jan 2014
17 : ///
18 : // The pedestrian following model (prototype)
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <cmath>
23 : #include <algorithm>
24 : #include <utils/common/RandHelper.h>
25 : #include <utils/geom/GeomHelper.h>
26 : #include <utils/options/OptionsCont.h>
27 : #include <utils/router/IntermodalNetwork.h>
28 : #include <microsim/MSGlobals.h>
29 : #include <microsim/MSNet.h>
30 : #include <microsim/MSEdge.h>
31 : #include <microsim/MSLane.h>
32 : #include <microsim/MSJunction.h>
33 : #include <microsim/MSEventControl.h>
34 : #include "MSPModel_NonInteracting.h"
35 :
36 :
37 : // ===========================================================================
38 : // static members
39 : // ===========================================================================
40 : const double MSPModel_NonInteracting::CState::LATERAL_OFFSET(0);
41 :
42 :
43 : // ===========================================================================
44 : // MSPModel_NonInteracting method definitions
45 : // ===========================================================================
46 8238 : MSPModel_NonInteracting::MSPModel_NonInteracting(const OptionsCont& oc, MSNet* net) :
47 8238 : myNet(net),
48 8238 : myNumActivePedestrians(0) {
49 : assert(myNet != 0);
50 : UNUSED_PARAMETER(oc);
51 8238 : }
52 :
53 :
54 16424 : MSPModel_NonInteracting::~MSPModel_NonInteracting() {
55 16424 : }
56 :
57 :
58 : MSTransportableStateAdapter*
59 154957 : MSPModel_NonInteracting::add(MSTransportable* transportable, MSStageMoving* stage, SUMOTime now) {
60 154957 : myNumActivePedestrians++;
61 154957 : MoveToNextEdge* const cmd = new MoveToNextEdge(transportable, *stage, this);
62 154957 : PState* const state = transportable->isPerson() ? new PState(cmd) : new CState(cmd);
63 154957 : myNet->getBeginOfTimestepEvents()->addEvent(cmd, now + state->computeDuration(nullptr, *stage, now));
64 154957 : return state;
65 : }
66 :
67 :
68 : MSTransportableStateAdapter*
69 10 : MSPModel_NonInteracting::loadState(MSTransportable* transportable, MSStageMoving* stage, std::istringstream& in) {
70 10 : myNumActivePedestrians++;
71 10 : MoveToNextEdge* const cmd = new MoveToNextEdge(transportable, *stage, this);
72 10 : PState* const state = transportable->isPerson() ? new PState(cmd, &in) : new CState(cmd, &in);
73 10 : myNet->getBeginOfTimestepEvents()->addEvent(cmd, state->getEventTime());
74 10 : return state;
75 : }
76 :
77 : void
78 8213 : MSPModel_NonInteracting::clearState() {
79 8213 : myNumActivePedestrians = 0;
80 8213 : }
81 :
82 : void
83 430 : MSPModel_NonInteracting::remove(MSTransportableStateAdapter* state) {
84 430 : myNumActivePedestrians--;
85 430 : dynamic_cast<PState*>(state)->getCommand()->abortWalk();
86 430 : }
87 :
88 :
89 : // ---------------------------------------------------------------------------
90 : // MSPModel_NonInteracting::MoveToNextEdge method definitions
91 : // ---------------------------------------------------------------------------
92 : SUMOTime
93 165438 : MSPModel_NonInteracting::MoveToNextEdge::execute(SUMOTime currentTime) {
94 165438 : if (myTransportable == nullptr) {
95 : return 0; // descheduled
96 : }
97 165012 : const MSEdge* old = myParent.getEdge();
98 165012 : const bool arrived = myParent.moveToNextEdge(myTransportable, currentTime, myParent.getPState()->getDirection());
99 165012 : if (arrived) {
100 152467 : myModel->registerArrived();
101 152467 : return 0;
102 : }
103 12545 : myParent.activateEntryReminders(myTransportable);
104 12545 : return static_cast<PState*>(myParent.getPState())->computeDuration(old, myParent, currentTime);
105 : }
106 :
107 :
108 : // ---------------------------------------------------------------------------
109 : // MSPModel_NonInteracting::PState method definitions
110 : // ---------------------------------------------------------------------------
111 154967 : MSPModel_NonInteracting::PState::PState(MoveToNextEdge* cmd, std::istringstream* in) : myCommand(cmd) {
112 154967 : if (in != nullptr) {
113 10 : (*in) >> myLastEntryTime >> myCurrentDuration;
114 : }
115 154967 : }
116 :
117 :
118 : SUMOTime
119 36712 : MSPModel_NonInteracting::PState::computeDuration(const MSEdge* prev, const MSStageMoving& stage, SUMOTime currentTime) {
120 36712 : myLastEntryTime = currentTime;
121 36712 : const MSEdge* edge = stage.getEdge();
122 36712 : const MSEdge* next = stage.getNextRouteEdge();
123 36712 : int dir = UNDEFINED_DIRECTION;
124 36712 : if (prev == nullptr) {
125 24167 : myCurrentBeginPos = stage.getDepartPos();
126 : } else {
127 : // default to FORWARD if not connected
128 12545 : dir = (edge->getToJunction() == prev->getToJunction() || edge->getToJunction() == prev->getFromJunction()) ? BACKWARD : FORWARD;
129 15116 : myCurrentBeginPos = dir == FORWARD ? 0 : edge->getLength();
130 : }
131 36712 : if (next == nullptr) {
132 23541 : myCurrentEndPos = stage.getArrivalPos();
133 : } else {
134 13171 : if (dir == UNDEFINED_DIRECTION) {
135 : // default to FORWARD if not connected
136 9028 : dir = (edge->getFromJunction() == next->getFromJunction() || edge->getFromJunction() == next->getToJunction()) ? BACKWARD : FORWARD;
137 : }
138 23826 : myCurrentEndPos = dir == FORWARD ? edge->getLength() : 0;
139 : }
140 : // ensure that a result > 0 is returned even if the walk ends immediately
141 : // adding 0.5ms is done to ensure proper rounding
142 36712 : myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPos - myCurrentBeginPos) / stage.getMaxSpeed(myCommand->getTransportable())));
143 : //std::cout << std::setprecision(8) << SIMTIME << " curBeg=" << myCurrentBeginPos << " curEnd=" << myCurrentEndPos << " speed=" << stage.getMaxSpeed(myCommand->getTransportable()) << " dur=" << myCurrentDuration << "\n";
144 : // round to the next timestep to avoid systematic higher walking speed
145 36712 : if ((myCurrentDuration % DELTA_T) > 0) {
146 30425 : myCurrentDuration += DELTA_T;
147 : }
148 36712 : return myCurrentDuration;
149 : }
150 :
151 :
152 : double
153 955249 : MSPModel_NonInteracting::PState::getEdgePos(SUMOTime now) const {
154 : //std::cout << SIMTIME << " lastEntryTime=" << myLastEntryTime << " pos=" << (myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime)) << "\n";
155 955249 : return myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / (double)myCurrentDuration * (double)(now - myLastEntryTime);
156 : }
157 :
158 : int
159 165012 : MSPModel_NonInteracting::PState::getDirection() const {
160 165012 : if (myCurrentBeginPos == myCurrentEndPos) {
161 2300 : return UNDEFINED_DIRECTION;
162 : } else {
163 162712 : return myCurrentBeginPos < myCurrentEndPos ? FORWARD : BACKWARD;
164 : }
165 : }
166 :
167 :
168 : Position
169 193975 : MSPModel_NonInteracting::PState::getPosition(const MSStageMoving& stage, SUMOTime now) const {
170 193975 : const MSLane* lane = getSidewalk<MSEdge, MSLane>(stage.getEdge());
171 193975 : if (lane == nullptr) {
172 : //std::string error = "Pedestrian '" + myCommand->myPerson->getID() + "' could not find sidewalk on edge '" + state.getEdge()->getID() + "', time="
173 : // + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".";
174 : //if (!OptionsCont::getOptions().getBool("ignore-route-errors")) {
175 : // throw ProcessError(error);
176 : //}
177 183 : lane = stage.getEdge()->getLanes().front();
178 : }
179 194158 : const double lateral_offset = (lane->allowsVehicleClass(SVC_PEDESTRIAN) ? 0 : SIDEWALK_OFFSET
180 183 : * (MSGlobals::gLefthand ? -1 : 1));
181 193975 : return stage.getLanePosition(lane, getEdgePos(now), lateral_offset);
182 : }
183 :
184 :
185 : double
186 120393 : MSPModel_NonInteracting::PState::getAngle(const MSStageMoving& stage, SUMOTime now) const {
187 : //std::cout << SIMTIME << " rawAngle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) << " angle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? 180 : 0) << "\n";
188 120393 : double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(now)) + (myCurrentEndPos < myCurrentBeginPos ? M_PI : 0);
189 120393 : if (angle > M_PI) {
190 3728 : angle -= 2 * M_PI;
191 : }
192 120393 : return angle;
193 : }
194 :
195 :
196 : double
197 185153 : MSPModel_NonInteracting::PState::getSpeed(const MSStageMoving& stage) const {
198 185153 : return stage.getMaxSpeed(myCommand->getTransportable());
199 : }
200 :
201 :
202 : const MSEdge*
203 1 : MSPModel_NonInteracting::PState::getNextEdge(const MSStageMoving& stage) const {
204 1 : return stage.getNextRouteEdge();
205 : }
206 :
207 :
208 : void
209 10 : MSPModel_NonInteracting::PState::saveState(std::ostringstream& out) {
210 20 : out << " " << myLastEntryTime << " " << myCurrentDuration;
211 10 : }
212 :
213 :
214 : // ---------------------------------------------------------------------------
215 : // MSPModel_NonInteracting::CState method definitions
216 : // ---------------------------------------------------------------------------
217 130790 : MSPModel_NonInteracting::CState::CState(MoveToNextEdge* cmd, std::istringstream* in) : PState(cmd, in) {
218 130790 : }
219 :
220 :
221 : Position
222 68 : MSPModel_NonInteracting::CState::getPosition(const MSStageMoving& stage, SUMOTime now) const {
223 : const double dist = myCurrentBeginPosition.distanceTo2D(myCurrentEndPosition); //distance between begin and end position of this tranship stage
224 68 : double pos = MIN2(STEPS2TIME(now - myLastEntryTime) * stage.getMaxSpeed(), dist); //the container shall not go beyond its end position
225 68 : return PositionVector::positionAtOffset2D(myCurrentBeginPosition, myCurrentEndPosition, pos, 0);
226 : }
227 :
228 :
229 : double
230 68 : MSPModel_NonInteracting::CState::getAngle(const MSStageMoving& stage, SUMOTime now) const {
231 68 : double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(now)) + (myCurrentEndPos < myCurrentBeginPos ? 1.5 * M_PI : 0.5 * M_PI);
232 68 : if (angle > M_PI) {
233 15 : angle -= 2 * M_PI;
234 : }
235 68 : return angle;
236 : }
237 :
238 :
239 : SUMOTime
240 130790 : MSPModel_NonInteracting::CState::computeDuration(const MSEdge* /* prev */, const MSStageMoving& stage, SUMOTime currentTime) {
241 130790 : myLastEntryTime = currentTime;
242 :
243 130790 : myCurrentBeginPos = stage.getDepartPos();
244 130790 : myCurrentEndPos = stage.getArrivalPos();
245 :
246 130790 : const MSLane* fromLane = stage.getFromEdge()->getLanes().front(); //the lane the container starts from during its tranship stage
247 130790 : myCurrentBeginPosition = stage.getLanePosition(fromLane, myCurrentBeginPos, LATERAL_OFFSET);
248 130790 : const MSLane* toLane = stage.getEdges().back()->getLanes().front(); //the lane the container ends during its tranship stage
249 130790 : myCurrentEndPosition = stage.getLanePosition(toLane, myCurrentEndPos, LATERAL_OFFSET);
250 :
251 130790 : myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPosition.distanceTo(myCurrentBeginPosition)) / stage.getMaxSpeed()));
252 130790 : return myCurrentDuration;
253 : }
254 :
255 :
256 : /****************************************************************************/
|