Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2014-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 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 7776 : MSPModel_NonInteracting::MSPModel_NonInteracting(const OptionsCont& oc, MSNet* net) :
47 7776 : myNet(net),
48 7776 : myNumActivePedestrians(0) {
49 : assert(myNet != 0);
50 : UNUSED_PARAMETER(oc);
51 7776 : }
52 :
53 :
54 15512 : MSPModel_NonInteracting::~MSPModel_NonInteracting() {
55 15512 : }
56 :
57 :
58 : MSTransportableStateAdapter*
59 154311 : MSPModel_NonInteracting::add(MSTransportable* transportable, MSStageMoving* stage, SUMOTime now) {
60 154311 : myNumActivePedestrians++;
61 154311 : MoveToNextEdge* const cmd = new MoveToNextEdge(transportable, *stage, this);
62 154311 : PState* const state = transportable->isPerson() ? new PState(cmd) : new CState(cmd);
63 154311 : myNet->getBeginOfTimestepEvents()->addEvent(cmd, now + state->computeDuration(nullptr, *stage, now));
64 154311 : 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 7760 : MSPModel_NonInteracting::clearState() {
79 7760 : myNumActivePedestrians = 0;
80 7760 : }
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 164904 : MSPModel_NonInteracting::MoveToNextEdge::execute(SUMOTime currentTime) {
94 164904 : if (myTransportable == nullptr) {
95 : return 0; // descheduled
96 : }
97 164478 : const MSEdge* old = myParent.getEdge();
98 164478 : const bool arrived = myParent.moveToNextEdge(myTransportable, currentTime, myParent.getPState()->getDirection());
99 164478 : if (arrived) {
100 151936 : myModel->registerArrived();
101 151936 : return 0;
102 : }
103 12542 : myParent.activateEntryReminders(myTransportable);
104 12542 : return static_cast<PState*>(myParent.getPState())->computeDuration(old, myParent, currentTime);
105 : }
106 :
107 :
108 : // ---------------------------------------------------------------------------
109 : // MSPModel_NonInteracting::PState method definitions
110 : // ---------------------------------------------------------------------------
111 154321 : MSPModel_NonInteracting::PState::PState(MoveToNextEdge* cmd, std::istringstream* in) : myCommand(cmd) {
112 154321 : if (in != nullptr) {
113 10 : (*in) >> myLastEntryTime >> myCurrentDuration;
114 : }
115 154321 : }
116 :
117 :
118 : SUMOTime
119 36614 : MSPModel_NonInteracting::PState::computeDuration(const MSEdge* prev, const MSStageMoving& stage, SUMOTime currentTime) {
120 36614 : myLastEntryTime = currentTime;
121 36614 : const MSEdge* edge = stage.getEdge();
122 36614 : const MSEdge* next = stage.getNextRouteEdge();
123 36614 : int dir = UNDEFINED_DIRECTION;
124 36614 : if (prev == nullptr) {
125 24072 : myCurrentBeginPos = stage.getDepartPos();
126 : } else {
127 : // default to FORWARD if not connected
128 12542 : dir = (edge->getToJunction() == prev->getToJunction() || edge->getToJunction() == prev->getFromJunction()) ? BACKWARD : FORWARD;
129 15174 : myCurrentBeginPos = dir == FORWARD ? 0 : edge->getLength();
130 : }
131 36614 : if (next == nullptr) {
132 23545 : myCurrentEndPos = stage.getArrivalPos();
133 : } else {
134 13069 : if (dir == UNDEFINED_DIRECTION) {
135 : // default to FORWARD if not connected
136 8966 : dir = (edge->getFromJunction() == next->getFromJunction() || edge->getFromJunction() == next->getToJunction()) ? BACKWARD : FORWARD;
137 : }
138 23561 : 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 36614 : 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 36614 : if ((myCurrentDuration % DELTA_T) > 0) {
146 30441 : myCurrentDuration += DELTA_T;
147 : }
148 36614 : return myCurrentDuration;
149 : }
150 :
151 :
152 : double
153 946694 : MSPModel_NonInteracting::PState::getEdgePos(SUMOTime now) const {
154 : //std::cout << SIMTIME << " lastEntryTime=" << myLastEntryTime << " pos=" << (myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime)) << "\n";
155 946694 : return myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / (double)myCurrentDuration * (double)(now - myLastEntryTime);
156 : }
157 :
158 : int
159 164478 : MSPModel_NonInteracting::PState::getDirection() const {
160 164478 : if (myCurrentBeginPos == myCurrentEndPos) {
161 2367 : return UNDEFINED_DIRECTION;
162 : } else {
163 162111 : return myCurrentBeginPos < myCurrentEndPos ? FORWARD : BACKWARD;
164 : }
165 : }
166 :
167 :
168 : Position
169 190419 : MSPModel_NonInteracting::PState::getPosition(const MSStageMoving& stage, SUMOTime now) const {
170 190419 : const MSLane* lane = getSidewalk<MSEdge, MSLane>(stage.getEdge());
171 190419 : 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 171 : lane = stage.getEdge()->getLanes().front();
178 : }
179 190590 : const double lateral_offset = (lane->allowsVehicleClass(SVC_PEDESTRIAN) ? 0 : SIDEWALK_OFFSET
180 171 : * (MSGlobals::gLefthand ? -1 : 1));
181 190419 : return stage.getLanePosition(lane, getEdgePos(now), lateral_offset);
182 : }
183 :
184 :
185 : double
186 116837 : 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 116837 : double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(now)) + (myCurrentEndPos < myCurrentBeginPos ? M_PI : 0);
189 116837 : if (angle > M_PI) {
190 3318 : angle -= 2 * M_PI;
191 : }
192 116837 : return angle;
193 : }
194 :
195 :
196 : double
197 184690 : MSPModel_NonInteracting::PState::getSpeed(const MSStageMoving& stage) const {
198 184690 : 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 130239 : MSPModel_NonInteracting::CState::CState(MoveToNextEdge* cmd, std::istringstream* in) : PState(cmd, in) {
218 130239 : }
219 :
220 :
221 : Position
222 72 : 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 72 : double pos = MIN2(STEPS2TIME(now - myLastEntryTime) * stage.getMaxSpeed(), dist); //the container shall not go beyond its end position
225 72 : return PositionVector::positionAtOffset2D(myCurrentBeginPosition, myCurrentEndPosition, pos, 0);
226 : }
227 :
228 :
229 : double
230 72 : MSPModel_NonInteracting::CState::getAngle(const MSStageMoving& stage, SUMOTime now) const {
231 72 : double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(now)) + (myCurrentEndPos < myCurrentBeginPos ? 1.5 * M_PI : 0.5 * M_PI);
232 72 : if (angle > M_PI) {
233 20 : angle -= 2 * M_PI;
234 : }
235 72 : return angle;
236 : }
237 :
238 :
239 : SUMOTime
240 130239 : MSPModel_NonInteracting::CState::computeDuration(const MSEdge* /* prev */, const MSStageMoving& stage, SUMOTime currentTime) {
241 130239 : myLastEntryTime = currentTime;
242 :
243 130239 : myCurrentBeginPos = stage.getDepartPos();
244 130239 : myCurrentEndPos = stage.getArrivalPos();
245 :
246 130239 : const MSLane* fromLane = stage.getFromEdge()->getLanes().front(); //the lane the container starts from during its tranship stage
247 130239 : myCurrentBeginPosition = stage.getLanePosition(fromLane, myCurrentBeginPos, LATERAL_OFFSET);
248 130239 : const MSLane* toLane = stage.getEdges().back()->getLanes().front(); //the lane the container ends during its tranship stage
249 130239 : myCurrentEndPosition = stage.getLanePosition(toLane, myCurrentEndPos, LATERAL_OFFSET);
250 :
251 130239 : myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPosition.distanceTo(myCurrentBeginPosition)) / stage.getMaxSpeed()));
252 130239 : return myCurrentDuration;
253 : }
254 :
255 :
256 : /****************************************************************************/
|