Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2014-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 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 10370 : MSPModel_NonInteracting::MSPModel_NonInteracting(const OptionsCont& oc, MSNet* net) :
47 10370 : myNet(net),
48 10370 : myNumActivePedestrians(0) {
49 : assert(myNet != 0);
50 : UNUSED_PARAMETER(oc);
51 10370 : }
52 :
53 :
54 20700 : MSPModel_NonInteracting::~MSPModel_NonInteracting() {
55 20700 : }
56 :
57 :
58 : MSTransportableStateAdapter*
59 160256 : MSPModel_NonInteracting::add(MSTransportable* transportable, MSStageMoving* stage, SUMOTime now) {
60 160256 : myNumActivePedestrians++;
61 160256 : MoveToNextEdge* const cmd = new MoveToNextEdge(transportable, *stage, this);
62 160256 : PState* const state = transportable->isPerson() ? new PState(cmd) : new CState(cmd);
63 160256 : myNet->getBeginOfTimestepEvents()->addEvent(cmd, now + state->computeDuration(nullptr, *stage, now));
64 160256 : return state;
65 : }
66 :
67 :
68 : MSTransportableStateAdapter*
69 17 : MSPModel_NonInteracting::loadState(MSTransportable* transportable, MSStageMoving* stage, std::istringstream& in) {
70 17 : myNumActivePedestrians++;
71 17 : MoveToNextEdge* const cmd = new MoveToNextEdge(transportable, *stage, this);
72 17 : PState* const state = transportable->isPerson() ? new PState(cmd, &in) : new CState(cmd, &in);
73 17 : myNet->getBeginOfTimestepEvents()->addEvent(cmd, state->getEventTime());
74 17 : return state;
75 : }
76 :
77 : void
78 10346 : MSPModel_NonInteracting::clearState() {
79 10346 : myNumActivePedestrians = 0;
80 10346 : }
81 :
82 : void
83 434 : MSPModel_NonInteracting::remove(MSTransportableStateAdapter* state) {
84 434 : myNumActivePedestrians--;
85 434 : dynamic_cast<PState*>(state)->getCommand()->abortWalk();
86 434 : }
87 :
88 :
89 : // ---------------------------------------------------------------------------
90 : // MSPModel_NonInteracting::MoveToNextEdge method definitions
91 : // ---------------------------------------------------------------------------
92 : SUMOTime
93 1390899 : MSPModel_NonInteracting::MoveToNextEdge::execute(SUMOTime currentTime) {
94 1390899 : if (myTransportable == nullptr) {
95 : return 0; // descheduled
96 : }
97 1390471 : const MSEdge* old = myParent.getEdge();
98 1390471 : const bool arrived = myParent.moveToNextEdge(myTransportable, currentTime, myParent.getPState()->getDirection());
99 1390471 : if (arrived) {
100 159453 : myModel->registerArrived();
101 159453 : return 0;
102 : }
103 1231018 : SUMOTime nextDuration = static_cast<PState*>(myParent.getPState())->computeDuration(old, myParent, currentTime);
104 : // first compute position on the new edge
105 1231018 : myParent.activateEntryReminders(myTransportable);
106 1231018 : if (myTransportable == nullptr) {
107 : return 0; // descheduled by rerouter on new edge
108 : }
109 : return nextDuration;
110 : }
111 :
112 :
113 : // ---------------------------------------------------------------------------
114 : // MSPModel_NonInteracting::PState method definitions
115 : // ---------------------------------------------------------------------------
116 160273 : MSPModel_NonInteracting::PState::PState(MoveToNextEdge* cmd, std::istringstream* in) : myCommand(cmd) {
117 160273 : if (in != nullptr) {
118 17 : (*in) >> myLastEntryTime >> myCurrentDuration;
119 : }
120 160273 : }
121 :
122 :
123 : SUMOTime
124 1255958 : MSPModel_NonInteracting::PState::computeDuration(const MSEdge* prev, const MSStageMoving& stage, SUMOTime currentTime) {
125 1255958 : myLastEntryTime = currentTime;
126 1255958 : const MSEdge* edge = stage.getEdge();
127 1255958 : const MSEdge* next = stage.getNextRouteEdge();
128 1255958 : int dir = UNDEFINED_DIRECTION;
129 1255958 : if (prev == nullptr) {
130 24940 : myCurrentBeginPos = stage.getDepartPos();
131 : } else {
132 : // default to FORWARD if not connected
133 1231018 : dir = (edge->getToJunction() == prev->getToJunction() || edge->getToJunction() == prev->getFromJunction()) ? BACKWARD : FORWARD;
134 2451635 : myCurrentBeginPos = dir == FORWARD ? 0 : edge->getLength();
135 : }
136 1255958 : if (next == nullptr) {
137 1241955 : myCurrentEndPos = stage.getArrivalPos();
138 : } else {
139 14003 : if (dir == UNDEFINED_DIRECTION) {
140 : // default to FORWARD if not connected
141 9780 : dir = (edge->getFromJunction() == next->getFromJunction() || edge->getFromJunction() == next->getToJunction()) ? BACKWARD : FORWARD;
142 : }
143 25042 : myCurrentEndPos = dir == FORWARD ? edge->getLength() : 0;
144 : }
145 : // ensure that a result > 0 is returned even if the walk ends immediately
146 : // adding 0.5ms is done to ensure proper rounding
147 1255958 : myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPos - myCurrentBeginPos) / stage.getMaxSpeed(myCommand->getTransportable())));
148 : //std::cout << std::setprecision(8) << SIMTIME << " curBeg=" << myCurrentBeginPos << " curEnd=" << myCurrentEndPos << " speed=" << stage.getMaxSpeed(myCommand->getTransportable()) << " dur=" << myCurrentDuration << "\n";
149 : // round to the next timestep to avoid systematic higher walking speed
150 1255958 : if ((myCurrentDuration % DELTA_T) > 0) {
151 1249660 : myCurrentDuration += DELTA_T;
152 : }
153 1255958 : return myCurrentDuration;
154 : }
155 :
156 :
157 : double
158 11974433 : MSPModel_NonInteracting::PState::getEdgePos(SUMOTime now) const {
159 : //std::cout << SIMTIME << " lastEntryTime=" << myLastEntryTime << " pos=" << (myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime)) << "\n";
160 11974433 : return myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / (double)myCurrentDuration * (double)(now - myLastEntryTime);
161 : }
162 :
163 : int
164 1390471 : MSPModel_NonInteracting::PState::getDirection() const {
165 1390471 : if (myCurrentBeginPos == myCurrentEndPos) {
166 2659 : return UNDEFINED_DIRECTION;
167 : } else {
168 1387812 : return myCurrentBeginPos < myCurrentEndPos ? FORWARD : BACKWARD;
169 : }
170 : }
171 :
172 :
173 : Position
174 5110227 : MSPModel_NonInteracting::PState::getPosition(const MSStageMoving& stage, SUMOTime now) const {
175 5110227 : const MSLane* lane = getSidewalk<MSEdge, MSLane>(stage.getEdge());
176 5110227 : if (lane == nullptr) {
177 : //std::string error = "Pedestrian '" + myCommand->myPerson->getID() + "' could not find sidewalk on edge '" + state.getEdge()->getID() + "', time="
178 : // + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".";
179 : //if (!OptionsCont::getOptions().getBool("ignore-route-errors")) {
180 : // throw ProcessError(error);
181 : //}
182 171 : lane = stage.getEdge()->getLanes().front();
183 : }
184 5110398 : const double lateral_offset = (lane->allowsVehicleClass(SVC_PEDESTRIAN) ? 0 : SIDEWALK_OFFSET
185 171 : * (MSGlobals::gLefthand ? -1 : 1));
186 5110227 : return stage.getLanePosition(lane, getEdgePos(now), lateral_offset);
187 : }
188 :
189 :
190 : double
191 2558085 : MSPModel_NonInteracting::PState::getAngle(const MSStageMoving& stage, SUMOTime now) const {
192 : //std::cout << SIMTIME << " rawAngle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) << " angle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? 180 : 0) << "\n";
193 2558085 : double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(now)) + (myCurrentEndPos < myCurrentBeginPos ? M_PI : 0);
194 2558085 : if (angle > M_PI) {
195 4325 : angle -= 2 * M_PI;
196 : }
197 2558085 : return angle;
198 : }
199 :
200 :
201 : double
202 2740933 : MSPModel_NonInteracting::PState::getSpeed(const MSStageMoving& stage) const {
203 2740933 : return stage.getMaxSpeed(myCommand->getTransportable());
204 : }
205 :
206 :
207 : const MSEdge*
208 3 : MSPModel_NonInteracting::PState::getNextEdge(const MSStageMoving& stage) const {
209 3 : return stage.getNextRouteEdge();
210 : }
211 :
212 :
213 : void
214 17 : MSPModel_NonInteracting::PState::saveState(std::ostringstream& out) {
215 34 : out << " " << myLastEntryTime << " " << myCurrentDuration;
216 17 : }
217 :
218 :
219 : // ---------------------------------------------------------------------------
220 : // MSPModel_NonInteracting::CState method definitions
221 : // ---------------------------------------------------------------------------
222 135316 : MSPModel_NonInteracting::CState::CState(MoveToNextEdge* cmd, std::istringstream* in) : PState(cmd, in) {
223 135316 : }
224 :
225 :
226 : Position
227 67 : MSPModel_NonInteracting::CState::getPosition(const MSStageMoving& stage, SUMOTime now) const {
228 : const double dist = myCurrentBeginPosition.distanceTo2D(myCurrentEndPosition); //distance between begin and end position of this tranship stage
229 67 : double pos = MIN2(STEPS2TIME(now - myLastEntryTime) * stage.getMaxSpeed(), dist); //the container shall not go beyond its end position
230 67 : return PositionVector::positionAtOffset2D(myCurrentBeginPosition, myCurrentEndPosition, pos, 0);
231 : }
232 :
233 :
234 : double
235 67 : MSPModel_NonInteracting::CState::getAngle(const MSStageMoving& stage, SUMOTime now) const {
236 67 : double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(now)) + (myCurrentEndPos < myCurrentBeginPos ? 1.5 * M_PI : 0.5 * M_PI);
237 67 : if (angle > M_PI) {
238 19 : angle -= 2 * M_PI;
239 : }
240 67 : return angle;
241 : }
242 :
243 :
244 : SUMOTime
245 135316 : MSPModel_NonInteracting::CState::computeDuration(const MSEdge* /* prev */, const MSStageMoving& stage, SUMOTime currentTime) {
246 135316 : myLastEntryTime = currentTime;
247 :
248 135316 : myCurrentBeginPos = stage.getDepartPos();
249 135316 : myCurrentEndPos = stage.getArrivalPos();
250 :
251 135316 : const MSLane* fromLane = stage.getFromEdge()->getLanes().front(); //the lane the container starts from during its tranship stage
252 135316 : myCurrentBeginPosition = stage.getLanePosition(fromLane, myCurrentBeginPos, LATERAL_OFFSET);
253 135316 : const MSLane* toLane = stage.getEdges().back()->getLanes().front(); //the lane the container ends during its tranship stage
254 135316 : myCurrentEndPosition = stage.getLanePosition(toLane, myCurrentEndPos, LATERAL_OFFSET);
255 :
256 135316 : myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPosition.distanceTo(myCurrentBeginPosition)) / stage.getMaxSpeed()));
257 135316 : return myCurrentDuration;
258 : }
259 :
260 :
261 : /****************************************************************************/
|