1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    MSPerson.h
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Sascha Krieg
14 /// @author  Michael Behrisch
15 /// @date    Mon, 9 Jul 2001
16 /// @version $Id$
17 ///
18 // The class for modelling person-movements
19 /****************************************************************************/
20 #ifndef MSPerson_h
21 #define MSPerson_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <string>
30 #include <vector>
31 #include <set>
32 #include <utils/common/SUMOTime.h>
33 #include <utils/common/Command.h>
34 #include <utils/geom/Position.h>
35 #include <utils/geom/PositionVector.h>
36 #include <microsim/MSTransportable.h>
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 class MSNet;
43 class MSEdge;
44 class MSLane;
45 class OutputDevice;
46 class SUMOVehicleParameter;
47 class MSStoppingPlace;
48 class SUMOVehicle;
49 class MSVehicleType;
50 class MSPModel;
51 class PedestrianState;
52 class DummyState;
53 
54 typedef std::vector<const MSEdge*> ConstMSEdgeVector;
55 
56 // ===========================================================================
57 // class definitions
58 // ===========================================================================
59 /**
60   * @class MSPerson
61   *
62   * The class holds a simulated person together with her movement stages
63   */
64 class MSPerson : public MSTransportable {
65 public:
66     /**
67      * A "real" stage performing the walking to an edge
68      * The walking does not need any route as it is not simulated.
69      * Only the duration is needed
70      */
71     class MSPersonStage_Walking : public MSTransportable::Stage {
72     public:
73         /// constructor
74         MSPersonStage_Walking(const std::string& personID, const ConstMSEdgeVector& route, MSStoppingPlace* toStop, SUMOTime walkingTime,
75                               double speed, double departPos, double arrivalPos, double departPosLat);
76 
77         /// destructor
78         ~MSPersonStage_Walking();
79 
80         Stage* clone() const;
81 
82         /// proceeds to the next step
83         virtual void proceed(MSNet* net, MSTransportable* person, SUMOTime now, Stage* previous);
84 
85         /// abort this stage (TraCI)
86         void abort(MSTransportable*);
87 
88         /// sets the walking speed (ignored in other stages)
89         void setSpeed(double speed);
90 
91         /// Returns the current edge
92         const MSEdge* getEdge() const;
93         const MSEdge* getFromEdge() const;
94         double getEdgePos(SUMOTime now) const;
95 
96         ///
97         Position getPosition(SUMOTime now) const;
98 
99         double getAngle(SUMOTime now) const;
100 
101         SUMOTime getWaitingTime(SUMOTime now) const;
102 
103         double getSpeed() const;
104 
105         /// @brief the edges of the current stage
106         ConstMSEdgeVector getEdges() const;
107 
getStageDescription()108         std::string getStageDescription() const {
109             return "walking";
110         }
111         std::string getStageSummary() const;
112 
113         /** @brief Called on writing tripinfo output
114          * @param[in] os The stream to write the information into
115          * @exception IOError not yet implemented
116          */
117         virtual void tripInfoOutput(OutputDevice& os, const MSTransportable* const transportable) const;
118 
119         /** @brief Called on writing vehroute output
120          * @param[in] os The stream to write the information into
121          * @param[in] withRouteLength whether route length shall be written
122          * @exception IOError not yet implemented
123          */
124         virtual void routeOutput(OutputDevice& os, const bool withRouteLength) const;
125 
126         /** @brief Called for writing the events output
127          * @param[in] os The stream to write the information into
128          * @exception IOError not yet implemented
129          */
130         virtual void beginEventOutput(const MSTransportable& p, SUMOTime t, OutputDevice& os) const;
131 
132         /** @brief Called for writing the events output (end of an action)
133          * @param[in] os The stream to write the information into
134          * @exception IOError not yet implemented
135          */
136         virtual void endEventOutput(const MSTransportable& p, SUMOTime t, OutputDevice& os) const;
137 
138         /// @brief move forward and return whether the person arrived
139         bool moveToNextEdge(MSPerson* person, SUMOTime currentTime, MSEdge* nextInternal = nullptr);
140 
141         /// @brief place person on a previously passed edge
142         void setRouteIndex(MSPerson* person, int routeOffset);
143 
144         /// @brief accessors to be used by MSPModel
145         //@{
146         double getMaxSpeed(const MSTransportable* const person) const;
147 
getDepartPos()148         inline double getDepartPos() const {
149             return myDepartPos;
150         }
151 
getDepartPosLat()152         inline double getDepartPosLat() const {
153             return myDepartPosLat;
154         }
155 
getArrivalPos()156         inline double getArrivalPos() const {
157             return myArrivalPos;
158         }
159 
getRouteStep()160         inline const std::vector<const MSEdge*>::iterator getRouteStep() const {
161             return myRouteStep;
162         }
163 
getRouteEdge()164         inline const MSEdge* getRouteEdge() const {
165             return *myRouteStep;
166         }
getNextRouteEdge()167         inline const MSEdge* getNextRouteEdge() const {
168             return myRouteStep == myRoute.end() - 1 ? 0 : *(myRouteStep + 1);
169         }
getRoute()170         inline const ConstMSEdgeVector& getRoute() const {
171             return myRoute;
172         }
173 
getPedestrianState()174         PedestrianState* getPedestrianState() const {
175             return myPedestrianState;
176         }
177         //@}
178 
179 
180     private:
181 
182         /// @brief compute total walking distance
183         double walkDistance() const;
184 
185         /* @brief compute average speed if the total walking duration is given
186          * @note Must be called when the previous stage changes myDepartPos from the default*/
187         double computeAverageSpeed() const;
188 
189 
190     private:
191         /// the time the person is walking
192         SUMOTime myWalkingTime;
193 
194         /// the time the person entered the edge
195         SUMOTime myLastEdgeEntryTime;
196 
197         /// @brief The route of the person
198         ConstMSEdgeVector myRoute;
199 
200 
201         ConstMSEdgeVector::iterator myRouteStep;
202 
203         /// @brief The current internal edge this person is on or 0
204         MSEdge* myCurrentInternalEdge;
205 
206         double myDepartPos;
207         double myDepartPosLat;
208         double mySpeed;
209 
210         /// @brief state that is to be manipulated by MSPModel
211         PedestrianState* myPedestrianState;
212 
213         class arrival_finder {
214         public:
215             /// constructor
arrival_finder(SUMOTime time)216             explicit arrival_finder(SUMOTime time) : myTime(time) {}
217 
218             /// comparison operator
operator()219             bool operator()(double t) const {
220                 return myTime > t;
221             }
222 
223         private:
224             /// the searched arrival time
225             SUMOTime myTime;
226         };
227 
228     private:
229         /// @brief Invalidated copy constructor.
230         MSPersonStage_Walking(const MSPersonStage_Walking&);
231 
232         /// @brief Invalidated assignment operator.
233         MSPersonStage_Walking& operator=(const MSPersonStage_Walking&);
234 
235     };
236 
237     /**
238     * A "real" stage performing the travelling by a transport system
239     * The given route will be chosen. The travel time is computed by the simulation
240     */
241     class MSPersonStage_Driving : public MSTransportable::Stage_Driving {
242     public:
243         /// constructor
244         MSPersonStage_Driving(const MSEdge* destination, MSStoppingPlace* toStop,
245                               const double arrivalPos, const std::vector<std::string>& lines,
246                               const std::string& intendedVeh = "", SUMOTime intendedDepart = -1);
247 
248         /// destructor
249         ~MSPersonStage_Driving();
250 
251         Stage* clone() const;
252 
253         /// proceeds to the next step
254         virtual void proceed(MSNet* net, MSTransportable* person, SUMOTime now, Stage* previous);
255 
256         /// @brief returns the stage description as a string
257         std::string getStageDescription() const;
258         std::string getStageSummary() const;
259 
260         /** @brief Called on writing tripinfo output
261         *
262         * @param[in] os The stream to write the information into
263         * @param[in] transportable The person to write information about
264         * @exception IOError not yet implemented
265         */
266         virtual void tripInfoOutput(OutputDevice& os, const MSTransportable* const transportable) const;
267 
268         /** @brief Called on writing vehroute output
269          *
270          * @param[in] os The stream to write the information into
271          * @param[in] withRouteLength whether route length shall be written
272          * @exception IOError not yet implemented
273          */
274         virtual void routeOutput(OutputDevice& os, const bool withRouteLength) const;
275     };
276 
277     /**
278      * An intermediate stage performing the access from or to public transport as given
279      * by the access elements of the public transport stop. The travel time is computed by the simulation
280      */
281     class MSPersonStage_Access : public MSTransportable::Stage {
282     public:
283         /// constructor
284         MSPersonStage_Access(const MSEdge* destination, MSStoppingPlace* toStop,
285                              const double arrivalPos, const double dist, const bool isExit);
286 
287         /// destructor
288         ~MSPersonStage_Access();
289 
290         Stage* clone() const;
291 
292         /// proceeds to the next step
293         virtual void proceed(MSNet* net, MSTransportable* person, SUMOTime now, Stage* previous);
294 
295         /// @brief returns the stage description as a string
296         std::string getStageDescription() const;
297         std::string getStageSummary() const;
298 
299         Position getPosition(SUMOTime now) const;
300 
301         double getAngle(SUMOTime now) const;
302 
303         /** @brief Called on writing tripinfo output
304         *
305         * @param[in] os The stream to write the information into
306         * @param[in] transportable The person to write information about
307         * @exception IOError not yet implemented
308         */
309         void tripInfoOutput(OutputDevice& os, const MSTransportable* const transportable) const;
310 
311         /// @brief Called on writing vehroute output. Currently does nothing.
routeOutput(OutputDevice &,const bool)312         void routeOutput(OutputDevice&, const bool) const {};
313 
314         /// @brief Called on writing events output (begin of an action). Currently does nothing.
beginEventOutput(const MSTransportable &,SUMOTime,OutputDevice &)315         void beginEventOutput(const MSTransportable&, SUMOTime, OutputDevice&) const {};
316 
317         /// @brief Called on writing events output (end of an action). Currently does nothing.
endEventOutput(const MSTransportable &,SUMOTime,OutputDevice &)318         void endEventOutput(const MSTransportable&, SUMOTime, OutputDevice&) const {};
319 
320     private:
321         class ProceedCmd : public Command {
322         public:
ProceedCmd(MSTransportable * person,MSEdge * edge)323             ProceedCmd(MSTransportable* person, MSEdge* edge) : myPerson(person), myStopEdge(edge) {}
~ProceedCmd()324             ~ProceedCmd() {}
325             SUMOTime execute(SUMOTime currentTime);
326         private:
327             MSTransportable* const myPerson;
328             MSEdge* myStopEdge;
329         private:
330             /// @brief Invalidated assignment operator.
331             ProceedCmd& operator=(const ProceedCmd&);
332         };
333 
334     private:
335         const double myDist;
336         const bool myAmExit;
337         SUMOTime myEstimatedArrival;
338         PositionVector myPath;
339     };
340 
341 public:
342     /// constructor
343     MSPerson(const SUMOVehicleParameter* pars, MSVehicleType* vtype, MSTransportable::MSTransportablePlan* plan, const double speedFactor);
344 
345     /// destructor
346     virtual ~MSPerson();
347 
348     /* @brief proceeds to the next step of the route,
349      * @return Whether the persons plan continues  */
350     bool proceed(MSNet* net, SUMOTime time);
351 
352     /// @brief return the list of internal edges if this person is walking and the pedestrian model allows it
353     const std::string& getNextEdge() const;
354 
355     /// @brief returns the next edge ptr if this person is walking and the pedestrian model allows it
356     const MSEdge* getNextEdgePtr() const;
357 
358     /** @brief Called on writing tripinfo output
359     *
360     * @param[in] os The stream to write the information into
361     * @exception IOError not yet implemented
362     */
363     virtual void tripInfoOutput(OutputDevice& os) const;
364 
365     /** @brief Called on writing vehroute output
366     *
367     * @param[in] os The stream to write the information into
368     * @exception IOError not yet implemented
369     */
370     virtual void routeOutput(OutputDevice& os, const bool withRouteLength) const;
371 
372     /// @brief whether this person is selected in the GUI
isSelected()373     virtual bool isSelected() const {
374         return false;
375     }
376 
getSpeedFactor()377     inline double getSpeedFactor() const {
378         return myChosenSpeedFactor;
379     }
380 
381     /// @brief set new walk and replace the stages with relative indices in the interval [firstIndex, nextIndex[
382     void reroute(ConstMSEdgeVector& newEdges, double departPos, int firstIndex, int nextIndex);
383 
384 
385     /** @class Influencer
386      * @brief Changes the wished person speed and position
387      *
388      * The class is used for passing velocities or positions obtained via TraCI to the person.
389      */
390     class Influencer {
391     public:
392         /// @brief Constructor
393         Influencer();
394 
395 
396         /// @brief Destructor
397         ~Influencer();
398 
399 
400         void setRemoteControlled(Position xyPos, MSLane* l, double pos, double posLat, double angle, int edgeOffset, const ConstMSEdgeVector& route, SUMOTime t);
401 
getLastAccessTimeStep()402         SUMOTime getLastAccessTimeStep() const {
403             return myLastRemoteAccess;
404         }
405 
406         void postProcessRemoteControl(MSPerson* p);
407 
408         bool isRemoteControlled() const;
409 
410         bool isRemoteAffected(SUMOTime t) const;
411 
412     private:
413         Position myRemoteXYPos;
414         MSLane* myRemoteLane;
415         double myRemotePos;
416         double myRemotePosLat;
417         double myRemoteAngle;
418         int myRemoteEdgeOffset;
419         ConstMSEdgeVector myRemoteRoute;
420         SUMOTime myLastRemoteAccess;
421     };
422 
423 
424     /** @brief Returns the velocity/lane influencer
425      *
426      * If no influencer was existing before, one is built, first
427      * @return Reference to this vehicle's speed influencer
428      */
429     Influencer& getInfluencer();
430 
431     const Influencer* getInfluencer() const;
432 
hasInfluencer()433     bool hasInfluencer() const {
434         return myInfluencer != 0;
435     }
436 
437     /// @brief sets position outside the road network
438     void setRemoteState(Position xyPos);
439 
440 private:
441     /// @brief An instance of a speed/position influencing instance; built in "getInfluencer"
442     Influencer* myInfluencer;
443 
444     const double myChosenSpeedFactor;
445 
446     static DummyState myDummyState;
447 
448 private:
449     /// @brief Invalidated copy constructor.
450     MSPerson(const MSPerson&);
451 
452     /// @brief Invalidated assignment operator.
453     MSPerson& operator=(const MSPerson&);
454 
455 };
456 
457 
458 #endif
459 
460 /****************************************************************************/
461