Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSPModel_JuPedSim.h
Go to the documentation of this file.
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/****************************************************************************/
21// The pedestrian following model that can instantiate different pedestrian models
22// that come with the JuPedSim third-party simulation framework.
23/****************************************************************************/
24#pragma once
25#include <config.h>
26#include <vector>
27#include <map>
28#include <geos_c.h>
29#ifdef HAVE_BOOST
30#if __has_include(<boost/process/v1.hpp>) // Boost 1.86+
31#include <boost/process/v1.hpp>
32namespace bp = boost::process::v1;
33#else
34#include <boost/process.hpp>
35namespace bp = boost::process;
36#endif
37#endif
40#include <microsim/MSNet.h>
42
43#ifdef _MSC_VER
44#pragma warning(push)
45// mask warnings about int conversion (4267), class layout (4371),
46// inline function (4514 4710 4711), reinterpret_cast (4946) in protobuf and
47// pop/push in different files (5031), missing virtual destructors (5204) in boost
48#pragma warning(disable: 4267 4371 4514 4710 4711 4946 5031 5204)
49#endif
50#pragma push_macro("TS")
51#pragma push_macro("Min")
52#pragma push_macro("Max")
53#undef TS
54#undef Min
55#undef Max
56#include <grpcpp/grpcpp.h>
57#include "jupedsim.grpc.pb.h"
58#pragma pop_macro("TS")
59#pragma pop_macro("Min")
60#pragma pop_macro("Max")
61#ifdef _MSC_VER
62#pragma warning(pop)
63#endif
64
65// ===========================================================================
66// class declarations
67// ===========================================================================
68class OutputDevice;
69
70
71// ===========================================================================
72// class definitions
73// ===========================================================================
80public:
81 typedef int64_t JPS_StageId;
82 typedef int64_t JPS_AgentId;
83 typedef int64_t JPS_JourneyId;
84
85 MSPModel_JuPedSim(const OptionsCont& oc, MSNet* net);
87
89 void remove(MSTransportableStateAdapter* state) override;
91
92 bool usingShortcuts() override {
94 }
96 bool usingDoors() override {
97 return true;
98 }
99 void registerArrived(const JPS_AgentId agentID);
100 void clearState() override;
101
102 class Event : public Command {
103 public:
104 explicit Event(MSPModel_JuPedSim* model)
105 : myModel(model) { }
106 SUMOTime execute(SUMOTime currentTime) override {
107 return myModel->execute(currentTime);
108 }
109
110 private:
112 };
113
120
121 typedef std::tuple<JPS_StageId, const MSLane*, Position, double> WaypointDesc;
122
123 void polygonChanged(const SUMOPolygon* const poly, const bool added, const bool removed) override;
124
125private:
131 public:
132 PState(MSPerson* person, MSStageMoving* stage, JPS_JourneyId journeyId, JPS_StageId stageId, const std::vector<WaypointDesc>& waypoints);
133 ~PState() override;
134
135 void reinit(MSStageMoving* stage, JPS_JourneyId journeyId, JPS_StageId stageId, const std::vector<WaypointDesc>& waypoints);
136
137 inline Position getPosition(const MSStageMoving&, SUMOTime) const override {
138 return myRemoteXYPos;
139 }
140 void setPosition(const double x, const double y, const double z = 0.);
141
142 inline void setAngle(double angle) {
143 myAngle = angle;
144 }
145
146 inline void setStage(MSStageMoving* const stage) {
147 myStage = stage;
148 }
149
150 inline void setLane(MSLane* lane) {
151 myLane = lane;
152 }
153
154 inline void setLanePosition(double lanePosition) {
155 myEdgePos = lanePosition;
156 }
157 const MSEdge* getNextEdge(const MSStageMoving& stage) const override;
158
159 const MSPModel_JuPedSim::WaypointDesc* getNextWaypoint(const int offset = 0) const;
160
161 inline JPS_AgentId getAgentId() const {
162 return myAgentId;
163 }
164
166 myAgentId = id;
167 myWaitingToEnter = false;
168 }
169
171 bool isFinished() const override {
172 return myWaypoints.empty();
173 }
174
176 return myJourneyId;
177 }
178
181 return myStageId;
182 }
183
185 myWaypoints.erase(myWaypoints.begin());
186 return myWaypoints.empty();
187 }
188
189 private:
193 std::vector<MSPModel_JuPedSim::WaypointDesc> myWaypoints;
195 };
196
197 std::vector<StopWatch<std::chrono::nanoseconds> > myStopWatch;
200
203
206
209
211 const double myExitTolerance;
212
213 std::vector<PState*> myPedestrianStates;
214
216 GEOSGeometry* myGEOSPedestrianNetwork; // Kept because the largest component refers to it.
217
220
222
224 int64_t myJPSGeometry = -1; // Kept because of dynamic geometry switching and JPS_Simulation object.
225
228 std::string myJPSModel;
230#ifdef HAVE_BOOST
231 std::unique_ptr<bp::child> myJuPedSimServer = nullptr;
232#endif
233 std::shared_ptr<grpc::Channel> myGrpcChannel;
234 std::unique_ptr<sumo_jupedsim_api::JuPedSimService::Stub> myGrpcStub;
235
237 struct AreaData {
238 const std::string id;
239 const std::string areaType;
240 sumo_jupedsim_api::Polygon areaBoundary;
242
245 };
246
248 std::vector<std::unique_ptr<AreaData> > myAreas;
249
251 std::vector<SUMOTrafficObject::NumericalID> myAllStoppedTrainIDs;
252
253 std::map<const MSLane*, std::pair<JPS_StageId, JPS_StageId>, ComparatorNumericalIdLess> myCrossingWaits;
254 std::map<JPS_StageId, std::pair<const MSLane*, bool> > myCrossings;
255 std::map<const MSLane*, JPS_StageId, ComparatorNumericalIdLess> myLaneCenters;
256
257 static const int GEOS_QUADRANT_SEGMENTS;
258 static const double GEOS_MITRE_LIMIT;
259 static const double GEOS_MIN_AREA;
260 static const double GEOS_BUFFERED_SEGMENT_WIDTH;
263 static const std::string PEDESTRIAN_NETWORK_ID;
265 static const std::vector<MSPModel_JuPedSim::PState*> noPedestrians;
266
267 void initialize(const OptionsCont& oc);
268 void tryPedestrianInsertion(PState* state, const Position& p);
269 JPS_StageId addWaypoint(const std::string& agentID, WaypointDesc& waypoint, const MSStageMoving* const finalStage);
270 static GEOSGeometry* createGeometryFromCenterLine(PositionVector centerLine, double width, int capStyle);
271 static GEOSGeometry* createGeometryFromShape(PositionVector shape, std::string junctionID = std::string(""), std::string shapeID = std::string(""), bool isInternalShape = false);
272 GEOSGeometry* buildPedestrianNetwork(MSNet* network);
273 static GEOSCoordSequence* convertToGEOSPoints(PositionVector shape);
274 static void convertToJPSPoints(const GEOSGeometry* geometry, sumo_jupedsim_api::Polygon* into);
275 static PositionVector convertToSUMOPoints(const GEOSGeometry* geometry);
276 static double getLinearRingArea(const GEOSGeometry* linearRing);
277 void removePolygonFromDrawing(const std::string& polygonId);
278 void preparePolygonForDrawing(const GEOSGeometry* polygon, const std::string& polygonId, const RGBColor& color);
279 static const GEOSGeometry* getLargestComponent(const GEOSGeometry* polygon, int& nbrComponents, double& maxArea, double& totalArea);
280 int64_t buildJPSGeometryFromGEOSGeometry(const GEOSGeometry* polygon);
281 static void dumpGeometry(const GEOSGeometry* polygon, const std::string& filename, bool useGeoCoordinates = false);
282 static double getRadius(const MSVehicleType& vehType);
283 JPS_StageId addWaitingSet(const MSLane* const crossing, const bool entry);
284
285 template <class Request, class Response>
286 Response
287 callGrpc(grpc::Status(sumo_jupedsim_api::JuPedSimService::Stub::*method)(grpc::ClientContext*, const Request&, Response*),
288 Request& request, const std::string& what, const bool warnOnly = false) {
289 myStopWatch[0].start();
290 grpc::ClientContext context;
291 Response response;
292 request.set_simulation_id(myJPSSimulation);
293 const grpc::Status status = ((*myGrpcStub).*method)(&context, request, &response);
294 myStopWatch[0].stop();
295 if (!status.ok()) {
296 if (!warnOnly) {
297 throw ProcessError(what + status.error_message());
298 }
299 WRITE_WARNING(what + status.error_message());
300 }
301 return response;
302 }
303};
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
Base (microsim) event class.
Definition Command.h:50
A road/street connecting two junctions.
Definition MSEdge.h:78
Representation of a lane in the micro simulation.
Definition MSLane.h:84
The simulated network and simulation perfomer.
Definition MSNet.h:89
The abstract superclass for pedestrian models which actually interact with vehicles.
Container for pedestrian state and individual position update function.
double myEdgePos
the advancement along the current lane
const MSLane * myLane
the current lane of this pedestrian
MSStageMoving * myStage
the current stage of this pedestrian
Position myRemoteXYPos
remote-controlled position
bool myWaitingToEnter
whether the pedestrian is waiting to start its walk
Event(MSPModel_JuPedSim *model)
MSPModel_JuPedSim * myModel
SUMOTime execute(SUMOTime currentTime) override
Executes the command.
Holds pedestrian state and performs updates.
JPS_StageId getStageId() const
first stage of the journey
JPS_AgentId getAgentId() const
void setStage(MSStageMoving *const stage)
bool isFinished() const override
whether the transportable has finished walking
void setAgentId(JPS_AgentId id)
const MSPModel_JuPedSim::WaypointDesc * getNextWaypoint(const int offset=0) const
void setLanePosition(double lanePosition)
JPS_JourneyId myJourneyId
id of the journey, needed for modifying it
std::vector< MSPModel_JuPedSim::WaypointDesc > myWaypoints
const MSEdge * getNextEdge(const MSStageMoving &stage) const override
return the list of internal edges if the transportable is on an intersection
JPS_JourneyId getJourneyId() const
void reinit(MSStageMoving *stage, JPS_JourneyId journeyId, JPS_StageId stageId, const std::vector< WaypointDesc > &waypoints)
Position getPosition(const MSStageMoving &, SUMOTime) const override
return the network coordinate of the transportable
void setPosition(const double x, const double y, const double z=0.)
A pedestrian following model that acts as a proxy for pedestrian models provided by the JuPedSim thir...
bool usingShortcuts() override
whether travel times and distances can reliably be calculated from the network alone
int64_t buildJPSGeometryFromGEOSGeometry(const GEOSGeometry *polygon)
MSTransportableStateAdapter * add(MSTransportable *person, MSStageMoving *stage, SUMOTime now) override
register the given person as a pedestrian
void removePolygonFromDrawing(const std::string &polygonId)
const GEOSGeometry * myGEOSPedestrianNetworkLargestComponent
The GEOS polygon representing the largest (by area) connected component of the pedestrian network.
void preparePolygonForDrawing(const GEOSGeometry *polygon, const std::string &polygonId, const RGBColor &color)
static const RGBColor PEDESTRIAN_NETWORK_CARRIAGES_AND_RAMPS_COLOR
static const double GEOS_MITRE_LIMIT
const double myExitTolerance
Threshold to decide if a pedestrian has ended its journey or not.
std::vector< SUMOTrafficObject::NumericalID > myAllStoppedTrainIDs
Array of stopped trains, used to detect whether to add carriages and ramps to the geometry.
bool usingDoors() override
precise lateral positioning when leaving trains is supported by JuPedSim
void registerArrived(const JPS_AgentId agentID)
static GEOSGeometry * createGeometryFromCenterLine(PositionVector centerLine, double width, int capStyle)
static PositionVector convertToSUMOPoints(const GEOSGeometry *geometry)
static const std::string PEDESTRIAN_NETWORK_CARRIAGES_AND_RAMPS_ID
static const RGBColor PEDESTRIAN_NETWORK_COLOR
static const std::string PEDESTRIAN_NETWORK_ID
std::map< const MSLane *, JPS_StageId, ComparatorNumericalIdLess > myLaneCenters
static GEOSGeometry * createGeometryFromShape(PositionVector shape, std::string junctionID=std::string(""), std::string shapeID=std::string(""), bool isInternalShape=false)
static GEOSCoordSequence * convertToGEOSPoints(PositionVector shape)
int64_t myJPSGeometry
The JPS polygon representing the largest connected component of the pedestrian network.
GEOSGeometry * myGEOSPedestrianNetwork
The GEOS polygon containing all computed connected components of the pedestrian network.
static void convertToJPSPoints(const GEOSGeometry *geometry, sumo_jupedsim_api::Polygon *into)
static void dumpGeometry(const GEOSGeometry *polygon, const std::string &filename, bool useGeoCoordinates=false)
std::map< const MSLane *, std::pair< JPS_StageId, JPS_StageId >, ComparatorNumericalIdLess > myCrossingWaits
const SUMOTime myJPSDeltaT
Timestep used in the JuPedSim simulator.
MSNet *const myNetwork
The network on which the simulation runs.
std::shared_ptr< grpc::Channel > myGrpcChannel
void polygonChanged(const SUMOPolygon *const poly, const bool added, const bool removed) override
static const double GEOS_MIN_AREA
void initialize(const OptionsCont &oc)
GEOSGeometry * buildPedestrianNetwork(MSNet *network)
void remove(MSTransportableStateAdapter *state) override
remove the specified person from the pedestrian simulation
std::vector< StopWatch< std::chrono::nanoseconds > > myStopWatch
std::vector< std::unique_ptr< AreaData > > myAreas
Array of special areas.
static double getLinearRingArea(const GEOSGeometry *linearRing)
std::unique_ptr< sumo_jupedsim_api::JuPedSimService::Stub > myGrpcStub
SumoRNG myRNG
The random number generator for fluctuations on insertion.
std::tuple< JPS_StageId, const MSLane *, Position, double > WaypointDesc
void clearState() override
Resets pedestrians when quick-loading state.
Response callGrpc(grpc::Status(sumo_jupedsim_api::JuPedSimService::Stub::*method)(grpc::ClientContext *, const Request &, Response *), Request &request, const std::string &what, const bool warnOnly=false)
ShapeContainer & myShapeContainer
The shape container used to add polygons to the rendering pipeline.
std::vector< PState * > myPedestrianStates
JPS_StageId addWaitingSet(const MSLane *const crossing, const bool entry)
int64_t myJPSGeometryWithTrainsAndRamps
The JPS polygon representing the largest connected component plus carriages and ramps.
static const int GEOS_QUADRANT_SEGMENTS
static const std::vector< MSPModel_JuPedSim::PState * > noPedestrians
static const GEOSGeometry * getLargestComponent(const GEOSGeometry *polygon, int &nbrComponents, double &maxArea, double &totalArea)
static double getRadius(const MSVehicleType &vehType)
JPS_StageId addWaypoint(const std::string &agentID, WaypointDesc &waypoint, const MSStageMoving *const finalStage)
void tryPedestrianInsertion(PState *state, const Position &p)
SUMOTime execute(SUMOTime time)
static const double GEOS_BUFFERED_SEGMENT_WIDTH
std::map< JPS_StageId, std::pair< const MSLane *, bool > > myCrossings
abstract base class for managing callbacks to retrieve various state information from the model
Definition MSPModel.h:159
The car-following model and parameter.
A storage for options typed value containers)
Definition OptionsCont.h:89
Static storage of an output device and its base (abstract) implementation.
std::map< std::string, std::string > Map
parameters map
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
A list of positions.
Storage for geometrical objects.
Interface for objects which want to be notified about shape updates.
Definition json.hpp:5639
Function-object for stable sorting of objects with numerical ids.
Definition Named.h:38
Structure that keeps data related to vanishing areas (and other types of areas).
const Parameterised::Map & params
SUMOTime lastRemovalTime
The last time a pedestrian was removed in a vanishing area.
sumo_jupedsim_api::Polygon areaBoundary