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-2018 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 
41 // ===========================================================================
42 // type definitions
43 // ===========================================================================
44 typedef std::map<const MSLane*, std::pair<double, double> > LaneCoverageInfo; // also declared in MSLane.h!
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
49 
51 public:
53  LaneStoringVisitor(std::set<std::string>& ids, const PositionVector& shape,
54  const double range, const int domain)
55  : myIDs(ids), myShape(shape), myRange(range), myDomain(domain) {}
56 
59 
61  void add(const MSLane* const l) const;
62 
64  std::set<std::string>& myIDs;
66  const double myRange;
67  const int myDomain;
68 
69 private:
72 
75 };
76 
77 #define LANE_RTREE_QUAL RTree<MSLane*, MSLane, float, 2, LaneStoringVisitor>
78 template<>
79 inline float LANE_RTREE_QUAL::RectSphericalVolume(Rect* a_rect) {
80  ASSERT(a_rect);
81  const float extent0 = a_rect->m_max[0] - a_rect->m_min[0];
82  const float extent1 = a_rect->m_max[1] - a_rect->m_min[1];
83  return .78539816f * (extent0 * extent0 + extent1 * extent1);
84 }
85 
86 template<>
87 inline LANE_RTREE_QUAL::Rect LANE_RTREE_QUAL::CombineRect(Rect* a_rectA, Rect* a_rectB) {
88  ASSERT(a_rectA && a_rectB);
89  Rect newRect;
90  newRect.m_min[0] = rtree_min(a_rectA->m_min[0], a_rectB->m_min[0]);
91  newRect.m_max[0] = rtree_max(a_rectA->m_max[0], a_rectB->m_max[0]);
92  newRect.m_min[1] = rtree_min(a_rectA->m_min[1], a_rectB->m_min[1]);
93  newRect.m_max[1] = rtree_max(a_rectA->m_max[1], a_rectB->m_max[1]);
94  return newRect;
95 }
96 
97 namespace libsumo {
98 
103 class Helper {
104 public:
105  static void subscribe(const int commandId, const std::string& id, const std::vector<int>& variables,
106  const double beginTime, const double endTime, const int contextDomain = 0, const double range = 0.);
107 
108  static void handleSubscriptions(const SUMOTime t);
109 
111  static TraCIPositionVector makeTraCIPositionVector(const PositionVector& positionVector);
112  static TraCIPosition makeTraCIPosition(const Position& position, const bool includeZ = false);
113  static Position makePosition(const TraCIPosition& position);
114 
115  static PositionVector makePositionVector(const TraCIPositionVector& vector);
116  static TraCIColor makeTraCIColor(const RGBColor& color);
117  static RGBColor makeRGBColor(const TraCIColor& color);
118 
119  static MSEdge* getEdge(const std::string& edgeID);
120  static const MSLane* getLaneChecking(const std::string& edgeID, int laneIndex, double pos);
121  static std::pair<MSLane*, double> convertCartesianToRoadMap(Position pos);
122 
123  static void findObjectShape(int domain, const std::string& id, PositionVector& shape);
124 
125  static void collectObjectsInRange(int domain, const PositionVector& shape, double range, std::set<std::string>& into);
126 
133  static void applySubscriptionFilters(const Subscription& s, std::set<std::string>& objIDs);
134 
135  static void setRemoteControlled(MSVehicle* v, Position xyPos, MSLane* l, double pos, double posLat, double angle,
136  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
137 
138  static void setRemoteControlled(MSPerson* p, Position xyPos, MSLane* l, double pos, double posLat, double angle,
139  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
140 
141  static void postProcessRemoteControl();
142 
143  static void cleanup();
144 
145  static void registerVehicleStateListener();
146 
147  static const std::vector<std::string>& getVehicleStateChanges(const MSNet::VehicleState state);
148 
149  static void clearVehicleStates();
150 
153  static bool moveToXYMap(const Position& pos, double maxRouteDistance, bool mayLeaveNetwork, const std::string& origID, const double angle,
154  double speed, const ConstMSEdgeVector& currentRoute, const int routePosition, MSLane* currentLane, double currentLanePos, bool onRoad,
155  double& bestDistance, MSLane** lane, double& lanePos, int& routeOffset, ConstMSEdgeVector& edges);
156 
157  static bool moveToXYMap_matchingRoutePosition(const Position& pos, const std::string& origID,
158  const ConstMSEdgeVector& currentRoute, int routeIndex,
159  double& bestDistance, MSLane** lane, double& lanePos, int& routeOffset);
160 
161  static bool findCloserLane(const MSEdge* edge, const Position& pos, double& bestDistance, MSLane** lane);
162 
163  class LaneUtility {
164  public:
165  LaneUtility(double dist_, double perpendicularDist_, double lanePos_, double angleDiff_, bool ID_,
166  bool onRoute_, bool sameEdge_, const MSEdge* prevEdge_, const MSEdge* nextEdge_) :
167  dist(dist_), perpendicularDist(perpendicularDist_), lanePos(lanePos_), angleDiff(angleDiff_), ID(ID_),
168  onRoute(onRoute_), sameEdge(sameEdge_), prevEdge(prevEdge_), nextEdge(nextEdge_) {}
171 
172  double dist;
174  double lanePos;
175  double angleDiff;
176  bool ID;
177  bool onRoute;
178  bool sameEdge;
179  const MSEdge* prevEdge;
180  const MSEdge* nextEdge;
181  };
183 
185  public:
186  SubscriptionWrapper(VariableWrapper::SubscriptionHandler handler, SubscriptionResults& into, ContextSubscriptionResults& context);
187  void setContext(const std::string& refID);
188  bool wrapDouble(const std::string& objID, const int variable, const double value);
189  bool wrapInt(const std::string& objID, const int variable, const int value);
190  bool wrapString(const std::string& objID, const int variable, const std::string& value);
191  bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
192  bool wrapPosition(const std::string& objID, const int variable, const TraCIPosition& value);
193  bool wrapColor(const std::string& objID, const int variable, const TraCIColor& value);
194  private:
198  private:
201  };
202 
203 private:
204  static void handleSingleSubscription(const Subscription& s);
205 
211  static void fuseLaneCoverage(std::shared_ptr<LaneCoverageInfo> aggregatedLaneCoverage, const std::shared_ptr<LaneCoverageInfo> newLaneCoverage);
212 
213 private:
215  public:
216  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
218  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
219  };
220 
222  static std::vector<Subscription> mySubscriptions;
223 
225  static std::map<int, std::shared_ptr<VariableWrapper> > myWrapper;
226 
229 
231  static std::map<int, NamedRTree*> myObjects;
232 
235 
236  static std::map<std::string, MSVehicle*> myRemoteControlledVehicles;
237  static std::map<std::string, MSPerson*> myRemoteControlledPersons;
238 
240  Helper() = delete;
241 };
242 
243 }
244 
245 
246 #endif
247 
248 /****************************************************************************/
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:199
const MSEdge * prevEdge
Definition: Helper.h:179
const MSEdge * nextEdge
Definition: Helper.h:180
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79
Representation of a subscription.
Definition: Subscription.h:65
long long int SUMOTime
Definition: SUMOTime.h:36
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:525
const int myDomain
Definition: Helper.h:67
LaneUtility(double dist_, double perpendicularDist_, double lanePos_, double angleDiff_, bool ID_, bool onRoute_, bool sameEdge_, const MSEdge *prevEdge_, const MSEdge *nextEdge_)
Definition: Helper.h:165
#define LANE_RTREE_QUAL
Definition: Helper.h:77
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition: Helper.h:218
static LANE_RTREE_QUAL * myLaneTree
A storage of lanes.
Definition: Helper.h:234
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:200
ContextSubscriptionResults myContextResults
Definition: Helper.h:196
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:72
SubscriptionResults & myActiveResults
Definition: Helper.h:197
static std::map< std::string, MSPerson * > myRemoteControlledPersons
Definition: Helper.h:237
static VehicleStateListener myVehicleStateListener
Changes in the states of simulated vehicles.
Definition: Helper.h:228
LaneStoringVisitor & operator=(const LaneStoringVisitor &src)
invalidated assignment operator
LaneStoringVisitor(std::set< std::string > &ids, const PositionVector &shape, const double range, const int domain)
Constructor.
Definition: Helper.h:53
A road/street connecting two junctions.
Definition: MSEdge.h:75
Representation of a vehicle.
Definition: SUMOVehicle.h:60
static std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: Helper.h:222
#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:103
const double myRange
Definition: Helper.h:66
void add(const MSLane *const l) const
Adds the given object to the container.
Definition: Helper.cpp:61
SubscriptionResults myResults
Definition: Helper.h:195
#define rtree_max(a, b)
Definition: RTree.h:21
Definition: Edge.cpp:30
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:494
std::set< std::string > & myIDs
The container.
Definition: Helper.h:64
const PositionVector & myShape
Definition: Helper.h:65
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:225
std::map< const MSLane *, std::pair< double, double > > LaneCoverageInfo
Definition: Helper.h:39
static std::map< int, NamedRTree * > myObjects
A storage of objects.
Definition: Helper.h:231
static std::map< std::string, MSVehicle * > myRemoteControlledVehicles
Definition: Helper.h:236
A 3D-position.
Definition: TraCIDefs.h:107
~LaneStoringVisitor()
Destructor.
Definition: Helper.h:58
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
A list of positions.