Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
Helper.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2012-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/****************************************************************************/
19// C++ TraCI client API implementation
20/****************************************************************************/
21#pragma once
22#include <vector>
23#include <memory>
25#include <microsim/MSLane.h>
26#include <microsim/MSNet.h>
30
31
32// ===========================================================================
33// class declarations
34// ===========================================================================
35class Position;
36class PositionVector;
37class RGBColor;
38class MSEdge;
40class MSPerson;
41class MSVehicle;
42class MSBaseVehicle;
43class MSVehicleType;
44class MSStoppingPlace;
45
46
47// ===========================================================================
48// class definitions
49// ===========================================================================
50namespace libsumo {
51
56class Helper {
57public:
58 static void subscribe(const int commandId, const std::string& id, const std::vector<int>& variables,
59 const double beginTime, const double endTime, const libsumo::TraCIResults& params,
60 const int contextDomain = 0, const double range = 0.);
61
62 static void handleSubscriptions(const SUMOTime t);
63
64 static bool needNewSubscription(libsumo::Subscription& s, std::vector<Subscription>& subscriptions, libsumo::Subscription*& modifiedSubscription);
65
66 static void clearSubscriptions();
67
69
72 static TraCIPosition makeTraCIPosition(const Position& position, const bool includeZ = false);
73 static Position makePosition(const TraCIPosition& position);
74
76 static TraCIColor makeTraCIColor(const RGBColor& color);
77 static RGBColor makeRGBColor(const TraCIColor& color);
78
79 static MSEdge* getEdge(const std::string& edgeID);
80 static const MSLane* getLaneChecking(const std::string& edgeID, int laneIndex, double pos);
81 static std::pair<MSLane*, double> convertCartesianToRoadMap(const Position& pos, const SUMOVehicleClass vClass);
82 static double getDrivingDistance(std::pair<const MSLane*, double>& roadPos1, std::pair<const MSLane*, double>& roadPos2);
83
84 static MSBaseVehicle* getVehicle(const std::string& id);
85 static MSPerson* getPerson(const std::string& id);
86 static SUMOTrafficObject* getTrafficObject(int domain, const std::string& id);
87 static const MSVehicleType& getVehicleType(const std::string& vehicleID);
88 static MSTLLogicControl::TLSLogicVariants& getTLS(const std::string& id);
89 static MSStoppingPlace* getStoppingPlace(const std::string& id, const SumoXMLTag type);
90
91 static SUMOVehicleParameter::Stop buildStopParameters(const std::string& edgeOrStoppingPlaceID,
92 double pos, int laneIndex, double startPos, int flags, double duration, double until);
93
95
96 static void findObjectShape(int domain, const std::string& id, PositionVector& shape);
97
98 static void collectObjectsInRange(int domain, const PositionVector& shape, double range, std::set<const Named*>& into);
99 static void collectObjectIDsInRange(int domain, const PositionVector& shape, double range, std::set<std::string>& into);
100
109 static void applySubscriptionFilters(const Subscription& s, std::set<std::string>& objIDs);
110
122 static void applySubscriptionFilterLanes(const Subscription& s, std::set<const SUMOTrafficObject*>& vehs, std::vector<int>& filterLanes,
123 double downstreamDist, double upstreamDist, bool disregardOppositeDirection);
124
131 static void applySubscriptionFilterTurn(const Subscription& s, std::set<const SUMOTrafficObject*>& vehs);
132
133 static void applySubscriptionFilterFieldOfVision(const Subscription& s, std::set<std::string>& objIDs);
134
145 static void applySubscriptionFilterLateralDistance(const Subscription& s, std::set<const SUMOTrafficObject*>& vehs, double downstreamDist,
146 double upstreamDist, double lateralDist);
147
149 std::set<std::string>& objIDs,
150 std::set<const SUMOTrafficObject*>& vehs,
151 const std::vector<const MSLane*>& lanes,
152 double posOnLane, double posLat, bool isDownstream);
153
154 static void setRemoteControlled(MSVehicle* v, Position xyPos, MSLane* l, double pos, double posLat, double angle,
155 int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
156
157 static void setRemoteControlled(MSPerson* p, Position xyPos, MSLane* l, double pos, double posLat, double angle,
158 int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
159
161 static int postProcessRemoteControl();
162
163 static void cleanup();
164
165 static void registerStateListener();
166
167 static const std::vector<std::string>& getVehicleStateChanges(const MSNet::VehicleState state);
168
169 static const std::vector<std::string>& getTransportableStateChanges(const MSNet::TransportableState state);
170
171 static void clearStateChanges();
172
174
177 static bool moveToXYMap(const Position& pos, double maxRouteDistance, bool mayLeaveNetwork, const std::string& origID,
178 const double angle, double speed, const ConstMSEdgeVector& currentRoute, const int routePosition,
179 const MSLane* currentLane, double currentLanePos, bool onRoad, SUMOVehicleClass vClass, bool setLateralPos,
180 double& bestDistance, MSLane** lane, double& lanePos, int& routeOffset, ConstMSEdgeVector& edges);
181
182 static bool moveToXYMap_matchingRoutePosition(const Position& pos, const std::string& origID,
183 const ConstMSEdgeVector& currentRoute, int routeIndex,
184 SUMOVehicleClass vClass, bool setLateralPos,
185 double& bestDistance, MSLane** lane, double& lanePos, int& routeOffset);
186
187 static bool findCloserLane(const MSEdge* edge, const Position& pos, SUMOVehicleClass vClass, double& bestDistance, MSLane** lane);
188
190 public:
191 LaneUtility(double dist_, double perpendicularDist_, double lanePos_, double angleDiff_, bool ID_,
192 bool onRoute_, bool sameEdge_, const MSEdge* prevEdge_, const MSEdge* nextEdge_) :
193 dist(dist_), perpendicularDist(perpendicularDist_), lanePos(lanePos_), angleDiff(angleDiff_), ID(ID_),
194 onRoute(onRoute_), sameEdge(sameEdge_), prevEdge(prevEdge_), nextEdge(nextEdge_) {}
196
197 double dist;
199 double lanePos;
200 double angleDiff;
201 bool ID;
206 };
208
210 public:
212 void setContext(const std::string* const refID);
213 void clear();
214 bool wrapDouble(const std::string& objID, const int variable, const double value);
215 bool wrapInt(const std::string& objID, const int variable, const int value);
216 bool wrapString(const std::string& objID, const int variable, const std::string& value);
217 bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
218 bool wrapDoubleList(const std::string& objID, const int variable, const std::vector<double>& value);
219 bool wrapPosition(const std::string& objID, const int variable, const TraCIPosition& value);
220 bool wrapPositionVector(const std::string& objID, const int variable, const TraCIPositionVector& value);
221 bool wrapColor(const std::string& objID, const int variable, const TraCIColor& value);
222 bool wrapStringDoublePair(const std::string& objID, const int variable, const std::pair<std::string, double>& value);
223 bool wrapStringPair(const std::string& objID, const int variable, const std::pair<std::string, std::string>& value);
224 void empty(const std::string& objID);
225 private:
229 private:
232 };
233
234private:
235 static void handleSingleSubscription(const Subscription& s);
236
242 static void fuseLaneCoverage(std::shared_ptr<LaneCoverageInfo> aggregatedLaneCoverage, const std::shared_ptr<LaneCoverageInfo> newLaneCoverage);
243
244 static void debugPrint(const SUMOTrafficObject* veh);
245
246private:
248 public:
249 void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
251 std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
252 };
253
255 public:
256 void transportableStateChanged(const MSTransportable* const transportable, MSNet::TransportableState to, const std::string& info = "");
258 std::map<MSNet::TransportableState, std::vector<std::string> > myTransportableStateChanges;
259 };
260
262 static std::vector<Subscription> mySubscriptions;
263
266
268 static std::map<int, std::shared_ptr<VariableWrapper> > myWrapper;
269
272
275
278
279 static std::map<std::string, MSVehicle*> myRemoteControlledVehicles;
280 static std::map<std::string, MSPerson*> myRemoteControlledPersons;
281
283 Helper() = delete;
284};
285
286}
long long int SUMOTime
Definition GUI.h:36
std::vector< const MSEdge * > ConstMSEdgeVector
Definition MSEdge.h:74
#define LANE_RTREE_QUAL
Definition MSLane.h:1793
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
SumoXMLTag
Numbers representing SUMO-XML - element names.
The base class for microscopic and mesoscopic vehicles.
Calibrates the flow on a segment to a specified one.
A road/street connecting two junctions.
Definition MSEdge.h:77
Representation of a lane in the micro simulation.
Definition MSLane.h:84
Interface for objects listening to transportable state changes.
Definition MSNet.h:700
Interface for objects listening to vehicle state changes.
Definition MSNet.h:641
VehicleState
Definition of a vehicle state.
Definition MSNet.h:608
TransportableState
Definition of a transportable state.
Definition MSNet.h:685
A lane area vehicles can halt at.
Storage for all programs of a single tls.
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
The car-following model and parameter.
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
A list of positions.
Representation of a vehicle, person, or container.
Representation of a vehicle.
Definition SUMOVehicle.h:62
Definition of vehicle stop (position and duration)
LaneUtility(double dist_, double perpendicularDist_, double lanePos_, double angleDiff_, bool ID_, bool onRoute_, bool sameEdge_, const MSEdge *prevEdge_, const MSEdge *nextEdge_)
Definition Helper.h:191
const MSEdge * nextEdge
Definition Helper.h:205
const MSEdge * prevEdge
Definition Helper.h:204
SubscriptionResults & myResults
Definition Helper.h:226
bool wrapDouble(const std::string &objID, const int variable, const double value)
Definition Helper.cpp:1867
void empty(const std::string &objID)
Definition Helper.cpp:1944
bool wrapPositionVector(const std::string &objID, const int variable, const TraCIPositionVector &value)
Definition Helper.cpp:1913
bool wrapColor(const std::string &objID, const int variable, const TraCIColor &value)
Definition Helper.cpp:1920
bool wrapInt(const std::string &objID, const int variable, const int value)
Definition Helper.cpp:1874
bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)
Definition Helper.cpp:1888
SubscriptionWrapper & operator=(const SubscriptionWrapper &s)=delete
Invalidated assignment operator.
bool wrapPosition(const std::string &objID, const int variable, const TraCIPosition &value)
Definition Helper.cpp:1906
bool wrapString(const std::string &objID, const int variable, const std::string &value)
Definition Helper.cpp:1881
bool wrapStringPair(const std::string &objID, const int variable, const std::pair< std::string, std::string > &value)
Definition Helper.cpp:1934
bool wrapStringDoublePair(const std::string &objID, const int variable, const std::pair< std::string, double > &value)
Definition Helper.cpp:1927
ContextSubscriptionResults & myContextResults
Definition Helper.h:227
SubscriptionResults * myActiveResults
Definition Helper.h:228
void setContext(const std::string *const refID)
Definition Helper.cpp:1853
bool wrapDoubleList(const std::string &objID, const int variable, const std::vector< double > &value)
Definition Helper.cpp:1897
void transportableStateChanged(const MSTransportable *const transportable, MSNet::TransportableState to, const std::string &info="")
Called if a transportable changes its state.
Definition Helper.cpp:1956
std::map< MSNet::TransportableState, std::vector< std::string > > myTransportableStateChanges
Changes in the states of simulated transportables.
Definition Helper.h:258
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition Helper.h:251
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to, const std::string &info="")
Called if a vehicle changes its state.
Definition Helper.cpp:1950
C++ TraCI client API implementation.
Definition Helper.h:56
static Position makePosition(const TraCIPosition &position)
Definition Helper.cpp:387
static MSEdge * getEdge(const std::string &edgeID)
Definition Helper.cpp:393
static void cleanup()
Definition Helper.cpp:683
static double getDrivingDistance(std::pair< const MSLane *, double > &roadPos1, std::pair< const MSLane *, double > &roadPos2)
Definition Helper.cpp:455
static void collectObjectsInRange(int domain, const PositionVector &shape, double range, std::set< const Named * > &into)
Definition Helper.cpp:820
static MSCalibrator::AspiredState getCalibratorState(const MSCalibrator *c)
Definition Helper.cpp:730
static TraCIPosition makeTraCIPosition(const Position &position, const bool includeZ=false)
Definition Helper.cpp:377
static LANE_RTREE_QUAL * myLaneTree
A lookup tree of lanes.
Definition Helper.h:277
static void applySubscriptionFilterTurn(const Subscription &s, std::set< const SUMOTrafficObject * > &vehs)
Apply the subscription filter "turn": Gather upcoming junctions and vialanes within downstream distan...
Definition Helper.cpp:1168
static void findObjectShape(int domain, const std::string &id, PositionVector &shape)
Definition Helper.cpp:740
static void clearStateChanges()
Definition Helper.cpp:719
static PositionVector makePositionVector(const TraCIPositionVector &vector)
Definition Helper.cpp:347
static void fuseLaneCoverage(std::shared_ptr< LaneCoverageInfo > aggregatedLaneCoverage, const std::shared_ptr< LaneCoverageInfo > newLaneCoverage)
Adds lane coverage information from newLaneCoverage into aggregatedLaneCoverage.
Definition Helper.cpp:319
static bool moveToXYMap_matchingRoutePosition(const Position &pos, const std::string &origID, const ConstMSEdgeVector &currentRoute, int routeIndex, SUMOVehicleClass vClass, bool setLateralPos, double &bestDistance, MSLane **lane, double &lanePos, int &routeOffset)
Definition Helper.cpp:1731
static void debugPrint(const SUMOTrafficObject *veh)
Definition Helper.cpp:97
static MSPerson * getPerson(const std::string &id)
Definition Helper.cpp:491
static void subscribe(const int commandId, const std::string &id, const std::vector< int > &variables, const double beginTime, const double endTime, const libsumo::TraCIResults &params, const int contextDomain=0, const double range=0.)
Definition Helper.cpp:109
Helper()=delete
invalidated standard constructor
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition Helper.cpp:337
static const std::vector< std::string > & getTransportableStateChanges(const MSNet::TransportableState state)
Definition Helper.cpp:713
static std::map< int, std::shared_ptr< VariableWrapper > > myWrapper
Map of commandIds -> their executors; applicable if the executor applies to the method footprint.
Definition Helper.h:268
static void clearSubscriptions()
Definition Helper.cpp:203
static MSBaseVehicle * getVehicle(const std::string &id)
Definition Helper.cpp:477
static void applySubscriptionFilterLateralDistanceSinglePass(const Subscription &s, std::set< std::string > &objIDs, std::set< const SUMOTrafficObject * > &vehs, const std::vector< const MSLane * > &lanes, double posOnLane, double posLat, bool isDownstream)
Definition Helper.cpp:1306
static MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag type)
Definition Helper.cpp:527
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition Helper.cpp:360
static void applySubscriptionFilterFieldOfVision(const Subscription &s, std::set< std::string > &objIDs)
Definition Helper.cpp:1233
static Subscription * myLastContextSubscription
The last context subscription.
Definition Helper.h:265
static TraCINextStopData buildStopData(const SUMOVehicleParameter::Stop &stopPar)
Definition Helper.cpp:643
static void registerStateListener()
Definition Helper.cpp:698
static TransportableStateListener myTransportableStateListener
Changes in the states of simulated transportables.
Definition Helper.h:274
static void setRemoteControlled(MSVehicle *v, Position xyPos, MSLane *l, double pos, double posLat, double angle, int edgeOffset, ConstMSEdgeVector route, SUMOTime t)
Definition Helper.cpp:1383
static int postProcessRemoteControl()
return number of remote-controlled entities
Definition Helper.cpp:1398
static void applySubscriptionFilters(const Subscription &s, std::set< std::string > &objIDs)
Filter the given ID-Set (which was obtained from an R-Tree search) according to the filters set by th...
Definition Helper.cpp:890
static std::map< std::string, MSVehicle * > myRemoteControlledVehicles
Definition Helper.h:279
static const MSVehicleType & getVehicleType(const std::string &vehicleID)
Definition Helper.cpp:512
static bool moveToXYMap(const Position &pos, double maxRouteDistance, bool mayLeaveNetwork, const std::string &origID, const double angle, double speed, const ConstMSEdgeVector &currentRoute, const int routePosition, const MSLane *currentLane, double currentLanePos, bool onRoad, SUMOVehicleClass vClass, bool setLateralPos, double &bestDistance, MSLane **lane, double &lanePos, int &routeOffset, ConstMSEdgeVector &edges)
Definition Helper.cpp:1423
static std::pair< MSLane *, double > convertCartesianToRoadMap(const Position &pos, const SUMOVehicleClass vClass)
Definition Helper.cpp:420
static MSTLLogicControl::TLSLogicVariants & getTLS(const std::string &id)
Definition Helper.cpp:518
static SUMOTrafficObject * getTrafficObject(int domain, const std::string &id)
Definition Helper.cpp:501
static VehicleStateListener myVehicleStateListener
Changes in the states of simulated vehicles.
Definition Helper.h:271
static std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition Helper.h:262
static SUMOVehicleParameter::Stop buildStopParameters(const std::string &edgeOrStoppingPlaceID, double pos, int laneIndex, double startPos, int flags, double duration, double until)
Definition Helper.cpp:537
static void handleSingleSubscription(const Subscription &s)
Definition Helper.cpp:232
static void applySubscriptionFilterLateralDistance(const Subscription &s, std::set< const SUMOTrafficObject * > &vehs, double downstreamDist, double upstreamDist, double lateralDist)
Apply the subscription filter "lateral distance": Only return vehicles within the given lateral dista...
Definition Helper.cpp:1272
static const std::vector< std::string > & getVehicleStateChanges(const MSNet::VehicleState state)
Definition Helper.cpp:707
static void collectObjectIDsInRange(int domain, const PositionVector &shape, double range, std::set< std::string > &into)
Definition Helper.cpp:810
static void handleSubscriptions(const SUMOTime t)
Definition Helper.cpp:153
static Subscription * addSubscriptionFilter(SubscriptionFilterType filter)
Definition Helper.cpp:210
static const MSLane * getLaneChecking(const std::string &edgeID, int laneIndex, double pos)
Definition Helper.cpp:403
static RGBColor makeRGBColor(const TraCIColor &color)
Definition Helper.cpp:371
static void applySubscriptionFilterLanes(const Subscription &s, std::set< const SUMOTrafficObject * > &vehs, std::vector< int > &filterLanes, double downstreamDist, double upstreamDist, bool disregardOppositeDirection)
Apply the subscription filter "lanes": Only return vehicles on list of lanes relative to ego vehicle....
Definition Helper.cpp:1070
static std::map< std::string, MSPerson * > myRemoteControlledPersons
Definition Helper.h:280
static bool needNewSubscription(libsumo::Subscription &s, std::vector< Subscription > &subscriptions, libsumo::Subscription *&modifiedSubscription)
Definition Helper.cpp:178
static bool findCloserLane(const MSEdge *edge, const Position &pos, SUMOVehicleClass vClass, double &bestDistance, MSLane **lane)
Definition Helper.cpp:1685
Representation of a subscription.
bool(* SubscriptionHandler)(const std::string &objID, const int variable, VariableWrapper *wrapper, tcpip::Storage *paramData)
Definition of a method to be called for serving an associated commandID.
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:338
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:337
SubscriptionFilterType
Filter types for context subscriptions.
std::map< int, std::shared_ptr< libsumo::TraCIResult > > TraCIResults
{variable->value}
Definition TraCIDefs.h:335
A 2D or 3D-position, for 2D positions z == INVALID_DOUBLE_VALUE.
Definition TraCIDefs.h:178
A list of positions.
Definition TraCIDefs.h:234