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 /****************************************************************************/
15 // C++ TraCI client API implementation
16 /****************************************************************************/
17 #ifndef Helper_h
18 #define Helper_h
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <vector>
27 #include <libsumo/Subscription.h>
28 #include <libsumo/TraCIDefs.h>
29 #include <microsim/MSNet.h>
30 
31 
32 // ===========================================================================
33 // class declarations
34 // ===========================================================================
35 class Position;
36 class PositionVector;
37 class RGBColor;
38 class MSEdge;
39 class MSLane;
40 class MSPerson;
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
46 
48 public:
50  LaneStoringVisitor(std::set<std::string>& ids, const PositionVector& shape,
51  const double range, const int domain)
52  : myIDs(ids), myShape(shape), myRange(range), myDomain(domain) {}
53 
56 
58  void add(const MSLane* const l) const;
59 
61  std::set<std::string>& myIDs;
63  const double myRange;
64  const int myDomain;
65 
66 private:
69 
72 };
73 
74 #define LANE_RTREE_QUAL RTree<MSLane*, MSLane, float, 2, LaneStoringVisitor>
75 template<>
76 inline float LANE_RTREE_QUAL::RectSphericalVolume(Rect* a_rect) {
77  ASSERT(a_rect);
78  const float extent0 = a_rect->m_max[0] - a_rect->m_min[0];
79  const float extent1 = a_rect->m_max[1] - a_rect->m_min[1];
80  return .78539816f * (extent0 * extent0 + extent1 * extent1);
81 }
82 
83 template<>
84 inline LANE_RTREE_QUAL::Rect LANE_RTREE_QUAL::CombineRect(Rect* a_rectA, Rect* a_rectB) {
85  ASSERT(a_rectA && a_rectB);
86  Rect newRect;
87  newRect.m_min[0] = rtree_min(a_rectA->m_min[0], a_rectB->m_min[0]);
88  newRect.m_max[0] = rtree_max(a_rectA->m_max[0], a_rectB->m_max[0]);
89  newRect.m_min[1] = rtree_min(a_rectA->m_min[1], a_rectB->m_min[1]);
90  newRect.m_max[1] = rtree_max(a_rectA->m_max[1], a_rectB->m_max[1]);
91  return newRect;
92 }
93 
94 namespace libsumo {
95 
100 class Helper {
101 public:
102  static void subscribe(const int commandId, const std::string& id, const std::vector<int>& variables,
103  const double beginTime, const double endTime, const int contextDomain = 0, const double range = 0.);
104 
105  static void handleSubscriptions(const SUMOTime t);
106 
108  static TraCIPositionVector makeTraCIPositionVector(const PositionVector& positionVector);
109  static TraCIPosition makeTraCIPosition(const Position& position, const bool includeZ = false);
110  static Position makePosition(const TraCIPosition& position);
111 
112  static PositionVector makePositionVector(const TraCIPositionVector& vector);
113  static TraCIColor makeTraCIColor(const RGBColor& color);
114  static RGBColor makeRGBColor(const TraCIColor& color);
115 
116  static MSEdge* getEdge(const std::string& edgeID);
117  static const MSLane* getLaneChecking(const std::string& edgeID, int laneIndex, double pos);
118  static std::pair<MSLane*, double> convertCartesianToRoadMap(Position pos);
119 
120  static void findObjectShape(int domain, const std::string& id, PositionVector& shape);
121 
122  static void collectObjectsInRange(int domain, const PositionVector& shape, double range, std::set<std::string>& into);
123 
124  static void setRemoteControlled(MSVehicle* v, Position xyPos, MSLane* l, double pos, double posLat, double angle,
125  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
126 
127  static void setRemoteControlled(MSPerson* p, Position xyPos, MSLane* l, double pos, double posLat, double angle,
128  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
129 
130  static void postProcessRemoteControl();
131 
132  static void cleanup();
133 
134  static void registerVehicleStateListener();
135 
136  static const std::vector<std::string>& getVehicleStateChanges(const MSNet::VehicleState state);
137 
138  static void clearVehicleStates();
139 
142  static bool moveToXYMap(const Position& pos, double maxRouteDistance, bool mayLeaveNetwork, const std::string& origID, const double angle,
143  double speed, const ConstMSEdgeVector& currentRoute, const int routePosition, MSLane* currentLane, double currentLanePos, bool onRoad,
144  double& bestDistance, MSLane** lane, double& lanePos, int& routeOffset, ConstMSEdgeVector& edges);
145 
146  static bool moveToXYMap_matchingRoutePosition(const Position& pos, const std::string& origID,
147  const ConstMSEdgeVector& currentRoute, int routeIndex,
148  double& bestDistance, MSLane** lane, double& lanePos, int& routeOffset);
149 
150  static bool findCloserLane(const MSEdge* edge, const Position& pos, double& bestDistance, MSLane** lane);
151 
152  class LaneUtility {
153  public:
154  LaneUtility(double dist_, double perpendicularDist_, double lanePos_, double angleDiff_, bool ID_,
155  bool onRoute_, bool sameEdge_, const MSEdge* prevEdge_, const MSEdge* nextEdge_) :
156  dist(dist_), perpendicularDist(perpendicularDist_), lanePos(lanePos_), angleDiff(angleDiff_), ID(ID_),
157  onRoute(onRoute_), sameEdge(sameEdge_), prevEdge(prevEdge_), nextEdge(nextEdge_) {}
160 
161  double dist;
163  double lanePos;
164  double angleDiff;
165  bool ID;
166  bool onRoute;
167  bool sameEdge;
168  const MSEdge* prevEdge;
169  const MSEdge* nextEdge;
170  };
172 
174  public:
175  SubscriptionWrapper(VariableWrapper::SubscriptionHandler handler, SubscriptionResults& into, ContextSubscriptionResults& context);
176  void setContext(const std::string& refID);
177  bool wrapDouble(const std::string& objID, const int variable, const double value);
178  bool wrapInt(const std::string& objID, const int variable, const int value);
179  bool wrapString(const std::string& objID, const int variable, const std::string& value);
180  bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
181  bool wrapPosition(const std::string& objID, const int variable, const TraCIPosition& value);
182  bool wrapColor(const std::string& objID, const int variable, const TraCIColor& value);
183  private:
187  private:
190  };
191 
192 private:
193  static void handleSingleSubscription(const Subscription& s);
194 
195 private:
197  public:
198  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
200  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
201  };
202 
204  static std::vector<Subscription> mySubscriptions;
205 
207  static std::map<int, std::shared_ptr<VariableWrapper> > myWrapper;
208 
211 
213  static std::map<int, NamedRTree*> myObjects;
214 
217 
218  static std::map<std::string, MSVehicle*> myRemoteControlledVehicles;
219  static std::map<std::string, MSPerson*> myRemoteControlledPersons;
220 
222  Helper() = delete;
223 };
224 
225 }
226 
227 
228 #endif
229 
230 /****************************************************************************/
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:199
const MSEdge * prevEdge
Definition: Helper.h:168
const MSEdge * nextEdge
Definition: Helper.h:169
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79
Representation of a subscription.
Definition: Subscription.h:63
long long int SUMOTime
Definition: SUMOTime.h:36
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:518
const int myDomain
Definition: Helper.h:64
LaneUtility(double dist_, double perpendicularDist_, double lanePos_, double angleDiff_, bool ID_, bool onRoute_, bool sameEdge_, const MSEdge *prevEdge_, const MSEdge *nextEdge_)
Definition: Helper.h:154
#define LANE_RTREE_QUAL
Definition: Helper.h:74
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition: Helper.h:200
static LANE_RTREE_QUAL * myLaneTree
A storage of lanes.
Definition: Helper.h:216
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:200
ContextSubscriptionResults myContextResults
Definition: Helper.h:185
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:72
SubscriptionResults & myActiveResults
Definition: Helper.h:186
static std::map< std::string, MSPerson * > myRemoteControlledPersons
Definition: Helper.h:219
static VehicleStateListener myVehicleStateListener
Changes in the states of simulated vehicles.
Definition: Helper.h:210
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:50
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:204
#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:100
const double myRange
Definition: Helper.h:63
void add(const MSLane *const l) const
Adds the given object to the container.
Definition: Helper.cpp:60
SubscriptionResults myResults
Definition: Helper.h:184
#define rtree_max(a, b)
Definition: RTree.h:21
Definition: Edge.cpp:30
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:487
std::set< std::string > & myIDs
The container.
Definition: Helper.h:61
const PositionVector & myShape
Definition: Helper.h:62
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:207
static std::map< int, NamedRTree * > myObjects
A storage of objects.
Definition: Helper.h:213
static std::map< std::string, MSVehicle * > myRemoteControlledVehicles
Definition: Helper.h:218
A 3D-position.
Definition: TraCIDefs.h:107
~LaneStoringVisitor()
Destructor.
Definition: Helper.h:55
Representation of a lane in the micro simulation.
Definition: MSLane.h:71
A list of positions.