Eclipse SUMO - Simulation of Urban MObility
Helper.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-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 /****************************************************************************/
16 // C++ TraCI client API implementation
17 /****************************************************************************/
18 #ifndef Helper_h
19 #define Helper_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <vector>
28 #include <memory>
29 #include <libsumo/Subscription.h>
30 #include <microsim/MSNet.h>
31 
32 // ===========================================================================
33 // class declarations
34 // ===========================================================================
35 class Position;
36 class PositionVector;
37 class RGBColor;
38 class MSEdge;
39 class MSPerson;
40 class MSVehicle;
42 
43 
44 // ===========================================================================
45 // type definitions
46 // ===========================================================================
47 typedef std::map<const MSLane*, std::pair<double, double> > LaneCoverageInfo; // also declared in MSLane.h!
48 
49 // ===========================================================================
50 // class definitions
51 // ===========================================================================
52 
54 public:
56  LaneStoringVisitor(std::set<std::string>& ids, const PositionVector& shape,
57  const double range, const int domain)
58  : myIDs(ids), myShape(shape), myRange(range), myDomain(domain) {}
59 
62 
64  void add(const MSLane* const l) const;
65 
67  std::set<std::string>& myIDs;
69  const double myRange;
70  const int myDomain;
71 
72 private:
75 
78 };
79 
80 #define LANE_RTREE_QUAL RTree<MSLane*, MSLane, float, 2, LaneStoringVisitor>
81 template<>
82 inline float LANE_RTREE_QUAL::RectSphericalVolume(Rect* a_rect) {
83  ASSERT(a_rect);
84  const float extent0 = a_rect->m_max[0] - a_rect->m_min[0];
85  const float extent1 = a_rect->m_max[1] - a_rect->m_min[1];
86  return .78539816f * (extent0 * extent0 + extent1 * extent1);
87 }
88 
89 template<>
90 inline LANE_RTREE_QUAL::Rect LANE_RTREE_QUAL::CombineRect(Rect* a_rectA, Rect* a_rectB) {
91  ASSERT(a_rectA && a_rectB);
92  Rect newRect;
93  newRect.m_min[0] = rtree_min(a_rectA->m_min[0], a_rectB->m_min[0]);
94  newRect.m_max[0] = rtree_max(a_rectA->m_max[0], a_rectB->m_max[0]);
95  newRect.m_min[1] = rtree_min(a_rectA->m_min[1], a_rectB->m_min[1]);
96  newRect.m_max[1] = rtree_max(a_rectA->m_max[1], a_rectB->m_max[1]);
97  return newRect;
98 }
99 
100 namespace libsumo {
101 
106 class Helper {
107 public:
108  static void subscribe(const int commandId, const std::string& id, const std::vector<int>& variables,
109  const double beginTime, const double endTime, const int contextDomain = 0, const double range = 0.);
110 
111  static void handleSubscriptions(const SUMOTime t);
112 
114  static TraCIPositionVector makeTraCIPositionVector(const PositionVector& positionVector);
115  static TraCIPosition makeTraCIPosition(const Position& position, const bool includeZ = false);
116  static Position makePosition(const TraCIPosition& position);
117 
118  static PositionVector makePositionVector(const TraCIPositionVector& vector);
119  static TraCIColor makeTraCIColor(const RGBColor& color);
120  static RGBColor makeRGBColor(const TraCIColor& color);
121 
122  static MSEdge* getEdge(const std::string& edgeID);
123  static const MSLane* getLaneChecking(const std::string& edgeID, int laneIndex, double pos);
124  static std::pair<MSLane*, double> convertCartesianToRoadMap(const Position& pos, const SUMOVehicleClass vClass);
125 
126  static MSVehicle* getVehicle(const std::string& id);
127  static const MSVehicleType& getVehicleType(const std::string& vehicleID);
128 
129  static void findObjectShape(int domain, const std::string& id, PositionVector& shape);
130 
131  static void collectObjectsInRange(int domain, const PositionVector& shape, double range, std::set<std::string>& into);
132 
139  static void applySubscriptionFilters(const Subscription& s, std::set<std::string>& objIDs);
140 
141  static void setRemoteControlled(MSVehicle* v, Position xyPos, MSLane* l, double pos, double posLat, double angle,
142  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
143 
144  static void setRemoteControlled(MSPerson* p, Position xyPos, MSLane* l, double pos, double posLat, double angle,
145  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
146 
147  static void postProcessRemoteControl();
148 
149  static void cleanup();
150 
151  static void registerVehicleStateListener();
152 
153  static const std::vector<std::string>& getVehicleStateChanges(const MSNet::VehicleState state);
154 
155  static void clearVehicleStates();
156 
159  static bool moveToXYMap(const Position& pos, double maxRouteDistance, bool mayLeaveNetwork, const std::string& origID, const double angle,
160  double speed, const ConstMSEdgeVector& currentRoute, const int routePosition, MSLane* currentLane, double currentLanePos, bool onRoad,
161  SUMOVehicleClass vClass,
162  double& bestDistance, MSLane** lane, double& lanePos, int& routeOffset, ConstMSEdgeVector& edges);
163 
164  static bool moveToXYMap_matchingRoutePosition(const Position& pos, const std::string& origID,
165  const ConstMSEdgeVector& currentRoute, int routeIndex,
166  SUMOVehicleClass vClass,
167  double& bestDistance, MSLane** lane, double& lanePos, int& routeOffset);
168 
169  static bool findCloserLane(const MSEdge* edge, const Position& pos, SUMOVehicleClass vClass, double& bestDistance, MSLane** lane);
170 
171  class LaneUtility {
172  public:
173  LaneUtility(double dist_, double perpendicularDist_, double lanePos_, double angleDiff_, bool ID_,
174  bool onRoute_, bool sameEdge_, const MSEdge* prevEdge_, const MSEdge* nextEdge_) :
175  dist(dist_), perpendicularDist(perpendicularDist_), lanePos(lanePos_), angleDiff(angleDiff_), ID(ID_),
176  onRoute(onRoute_), sameEdge(sameEdge_), prevEdge(prevEdge_), nextEdge(nextEdge_) {}
179 
180  double dist;
182  double lanePos;
183  double angleDiff;
184  bool ID;
185  bool onRoute;
186  bool sameEdge;
187  const MSEdge* prevEdge;
188  const MSEdge* nextEdge;
189  };
191 
192  class SubscriptionWrapper final : public VariableWrapper {
193  public:
194  SubscriptionWrapper(VariableWrapper::SubscriptionHandler handler, SubscriptionResults& into, ContextSubscriptionResults& context);
195  void setContext(const std::string& refID);
196  void clear();
197  bool wrapDouble(const std::string& objID, const int variable, const double value);
198  bool wrapInt(const std::string& objID, const int variable, const int value);
199  bool wrapString(const std::string& objID, const int variable, const std::string& value);
200  bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
201  bool wrapPosition(const std::string& objID, const int variable, const TraCIPosition& value);
202  bool wrapColor(const std::string& objID, const int variable, const TraCIColor& value);
203  bool wrapRoadPosition(const std::string& objID, const int variable, const TraCIRoadPosition& value);
204  private:
208  private:
211  };
212 
213 private:
214  static void handleSingleSubscription(const Subscription& s);
215 
221  static void fuseLaneCoverage(std::shared_ptr<LaneCoverageInfo> aggregatedLaneCoverage, const std::shared_ptr<LaneCoverageInfo> newLaneCoverage);
222 
223 private:
225  public:
226  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
228  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
229  };
230 
232  static std::vector<Subscription> mySubscriptions;
233 
235  static std::map<int, std::shared_ptr<VariableWrapper> > myWrapper;
236 
239 
241  static std::map<int, NamedRTree*> myObjects;
242 
245 
246  static std::map<std::string, MSVehicle*> myRemoteControlledVehicles;
247  static std::map<std::string, MSPerson*> myRemoteControlledPersons;
248 
250  Helper() = delete;
251 };
252 
253 }
254 
255 
256 #endif
257 
258 /****************************************************************************/
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:204
const MSEdge * prevEdge
Definition: Helper.h:187
const MSEdge * nextEdge
Definition: Helper.h:188
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
Representation of a subscription.
Definition: Subscription.h:65
long long int SUMOTime
Definition: SUMOTime.h:35
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:567
const int myDomain
Definition: Helper.h:70
LaneUtility(double dist_, double perpendicularDist_, double lanePos_, double angleDiff_, bool ID_, bool onRoute_, bool sameEdge_, const MSEdge *prevEdge_, const MSEdge *nextEdge_)
Definition: Helper.h:173
An edgeId, position and laneIndex.
Definition: TraCIDefs.h:122
#define LANE_RTREE_QUAL
Definition: Helper.h:80
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
ContextSubscriptionResults & myContextResults
Definition: Helper.h:206
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition: Helper.h:228
static LANE_RTREE_QUAL * myLaneTree
A storage of lanes.
Definition: Helper.h:244
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:205
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:73
static std::map< std::string, MSPerson * > myRemoteControlledPersons
Definition: Helper.h:247
static VehicleStateListener myVehicleStateListener
Changes in the states of simulated vehicles.
Definition: Helper.h:238
SubscriptionResults & myResults
Definition: Helper.h:205
The car-following model and parameter.
Definition: MSVehicleType.h:66
LaneStoringVisitor & operator=(const LaneStoringVisitor &src)
invalidated assignment operator
SubscriptionResults * myActiveResults
Definition: Helper.h:207
LaneStoringVisitor(std::set< std::string > &ids, const PositionVector &shape, const double range, const int domain)
Constructor.
Definition: Helper.h:56
A road/street connecting two junctions.
Definition: MSEdge.h:76
Representation of a vehicle.
Definition: SUMOVehicle.h:61
static std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: Helper.h:232
#define ASSERT
Definition: RTree.h:12
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
#define rtree_min(a, b)
Definition: RTree.h:20
A list of positions.
C++ TraCI client API implementation.
Definition: Helper.h:106
const double myRange
Definition: Helper.h:69
void add(const MSLane *const l) const
Adds the given object to the container.
Definition: Helper.cpp:63
#define rtree_max(a, b)
Definition: RTree.h:21
Definition: Edge.cpp:30
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:536
std::set< std::string > & myIDs
The container.
Definition: Helper.h:67
const PositionVector & myShape
Definition: Helper.h:68
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:235
std::map< const MSLane *, std::pair< double, double > > LaneCoverageInfo
Definition: Helper.h:41
static std::map< int, NamedRTree * > myObjects
A storage of objects.
Definition: Helper.h:241
static std::map< std::string, MSVehicle * > myRemoteControlledVehicles
Definition: Helper.h:246
A 3D-position.
Definition: TraCIDefs.h:110
~LaneStoringVisitor()
Destructor.
Definition: Helper.h:61
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
A list of positions.