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>
31#include <microsim/MSNet.h>
33
34#ifdef _MSC_VER
35#pragma warning(push)
36// mask warnings about int conversion (4267), class layout (4371),
37// inline function (4514 4710 4711), reinterpret_cast (4946) in protobuf and
38// pop/push in different files (5031), missing virtual destructors (5204),
39// overrides (4266), casts (4191), 'this' in initializers (4355) in boost
40// and deprecations in grpc (4996)
41#pragma warning(disable: 4191 4266 4267 4355 4371 4514 4710 4711 4946 4996 5031 5204)
42#endif
43#ifdef HAVE_BOOST
44#if __has_include(<boost/process/v1.hpp>) // Boost 1.86+
45#include <boost/process/v1.hpp>
46namespace bp = boost::process::v1;
47#else
48#include <boost/process.hpp>
49namespace bp = boost::process;
50#endif
51#endif
52#pragma push_macro("TS")
53#pragma push_macro("Min")
54#pragma push_macro("Max")
55#undef TS
56#undef Min
57#undef Max
58#include <grpcpp/grpcpp.h>
59#include "jupedsim.grpc.pb.h"
60#pragma pop_macro("TS")
61#pragma pop_macro("Min")
62#pragma pop_macro("Max")
63#ifdef _MSC_VER
64#pragma warning(pop)
65#endif
66
67// ===========================================================================
68// class declarations
69// ===========================================================================
70class OutputDevice;
71
72
73// ===========================================================================
74// class definitions
75// ===========================================================================
82public:
83 typedef int64_t JPS_StageId;
84 typedef int64_t JPS_AgentId;
85 typedef int64_t JPS_JourneyId;
86
87 MSPModel_JuPedSim(const OptionsCont& oc, MSNet* net);
89
91 void remove(MSTransportableStateAdapter* state) override;
93
94 bool usingShortcuts() override {
96 }
98 bool usingDoors() override {
99 return true;
100 }
101 void registerArrived(const JPS_AgentId agentID);
102 void clearState() override;
103
104 class Event : public Command {
105 public:
106 explicit Event(MSPModel_JuPedSim* model)
107 : myModel(model) { }
108 SUMOTime execute(SUMOTime currentTime) override {
109 return myModel->execute(currentTime);
110 }
111
112 private:
114 };
115
122
123 typedef std::tuple<JPS_StageId, const MSLane*, Position, double> WaypointDesc;
124
125 void polygonChanged(const SUMOPolygon* const poly, const bool added, const bool removed) override;
126
127private:
133 public:
134 PState(MSPerson* person, MSStageMoving* stage, JPS_JourneyId journeyId, JPS_StageId stageId, const std::vector<WaypointDesc>& waypoints);
135 ~PState() override;
136
137 void reinit(MSStageMoving* stage, JPS_JourneyId journeyId, JPS_StageId stageId, const std::vector<WaypointDesc>& waypoints);
138
139 inline Position getPosition(const MSStageMoving&, SUMOTime) const override {
140 return myRemoteXYPos;
141 }
142 void setPosition(const double x, const double y, const double z = 0.);
143
144 inline void setAngle(double angle) {
145 myAngle = angle;
146 }
147
148 inline void setStage(MSStageMoving* const stage) {
149 myStage = stage;
150 }
151
152 inline void setLane(MSLane* lane) {
153 myLane = lane;
154 }
155
156 inline void setLanePosition(double lanePosition) {
157 myEdgePos = lanePosition;
158 }
159 const MSEdge* getNextEdge(const MSStageMoving& stage) const override;
160
161 const MSPModel_JuPedSim::WaypointDesc* getNextWaypoint(const int offset = 0) const;
162
163 inline JPS_AgentId getAgentId() const {
164 return myAgentId;
165 }
166
168 myAgentId = id;
169 myWaitingToEnter = false;
170 }
171
173 bool isFinished() const override {
174 return myWaypoints.empty();
175 }
176
178 return myJourneyId;
179 }
180
183 return myStageId;
184 }
185
187 myWaypoints.erase(myWaypoints.begin());
188 return myWaypoints.empty();
189 }
190
191 private:
195 std::vector<MSPModel_JuPedSim::WaypointDesc> myWaypoints;
197 };
198
199 std::vector<StopWatch<std::chrono::nanoseconds> > myStopWatch;
202
205
208
211
213 const double myExitTolerance;
214
215 std::vector<PState*> myPedestrianStates;
216
218 GEOSGeometry* myGEOSPedestrianNetwork; // Kept because the largest component refers to it.
219
222
224
226 int64_t myJPSGeometry = -1; // Kept because of dynamic geometry switching and JPS_Simulation object.
227
230 std::string myJPSModel;
232#ifdef HAVE_BOOST
233 std::unique_ptr<bp::child> myJuPedSimServer = nullptr;
234#endif
235 std::shared_ptr<grpc::Channel> myGrpcChannel;
236 std::unique_ptr<sumo_jupedsim_api::JuPedSimService::Stub> myGrpcStub;
237
239 struct AreaData {
240 const std::string id;
241 const std::string areaType;
242 sumo_jupedsim_api::Polygon areaBoundary;
244
247 };
248
250 std::vector<std::unique_ptr<AreaData> > myAreas;
251
253 std::vector<SUMOTrafficObject::NumericalID> myAllStoppedTrainIDs;
254
255 std::map<const MSLane*, std::pair<JPS_StageId, JPS_StageId>, ComparatorNumericalIdLess> myCrossingWaits;
256 std::map<JPS_StageId, std::pair<const MSLane*, bool> > myCrossings;
257 std::map<const MSLane*, JPS_StageId, ComparatorNumericalIdLess> myLaneCenters;
258
259 static const int GEOS_QUADRANT_SEGMENTS;
260 static const double GEOS_MITRE_LIMIT;
261 static const double GEOS_MIN_AREA;
262 static const double GEOS_BUFFERED_SEGMENT_WIDTH;
265 static const std::string PEDESTRIAN_NETWORK_ID;
267 static const std::vector<MSPModel_JuPedSim::PState*> noPedestrians;
268
269 void initialize(const OptionsCont& oc);
270 void tryPedestrianInsertion(PState* state, const Position& p);
271 JPS_StageId addWaypoint(const std::string& agentID, WaypointDesc& waypoint, const MSStageMoving* const finalStage);
272 static GEOSGeometry* createGeometryFromCenterLine(PositionVector centerLine, double width, int capStyle);
273 static GEOSGeometry* createGeometryFromShape(PositionVector shape, std::string junctionID = std::string(""), std::string shapeID = std::string(""), bool isInternalShape = false);
274 GEOSGeometry* buildPedestrianNetwork(MSNet* network);
275 static GEOSCoordSequence* convertToGEOSPoints(PositionVector shape);
276 static void convertToJPSPoints(const GEOSGeometry* geometry, sumo_jupedsim_api::Polygon* into);
277 static PositionVector convertToSUMOPoints(const GEOSGeometry* geometry);
278 static double getLinearRingArea(const GEOSGeometry* linearRing);
279 void removePolygonFromDrawing(const std::string& polygonId);
280 void preparePolygonForDrawing(const GEOSGeometry* polygon, const std::string& polygonId, const RGBColor& color);
281 static const GEOSGeometry* getLargestComponent(const GEOSGeometry* polygon, int& nbrComponents, double& maxArea, double& totalArea);
282 int64_t buildJPSGeometryFromGEOSGeometry(const GEOSGeometry* polygon);
283 static void dumpGeometry(const GEOSGeometry* polygon, const std::string& filename, bool useGeoCoordinates = false);
284 static double getRadius(const MSVehicleType& vehType);
285 JPS_StageId addWaitingSet(const MSLane* const crossing, const bool entry);
286
287 template <class Request, class Response>
288 Response
289 callGrpc(grpc::Status(sumo_jupedsim_api::JuPedSimService::Stub::*method)(grpc::ClientContext*, const Request&, Response*),
290 Request& request, const std::string& what, const bool warnOnly = false) {
291 myStopWatch[0].start();
292 grpc::ClientContext context;
293 Response response;
294 request.set_simulation_id(myJPSSimulation);
295 const grpc::Status status = ((*myGrpcStub).*method)(&context, request, &response);
296 myStopWatch[0].stop();
297 if (!status.ok()) {
298 if (!warnOnly) {
299 throw ProcessError(what + status.error_message());
300 }
301 WRITE_WARNING(what + status.error_message());
302 }
303 return response;
304 }
305};
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNING(msg)
Definition MsgHandler.h:298
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:90
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:92
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