SUMO - Simulation of Urban MObility
TraCI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // C++ TraCI client API implementation
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
35 #include <utils/geom/Position.h>
36 #include <utils/common/RGBColor.h>
39 #include <utils/xml/XMLSubSys.h>
40 #include <microsim/MSEdge.h>
41 #include <microsim/MSEdgeControl.h>
42 #include <microsim/MSFrame.h>
43 #include <microsim/MSLane.h>
45 #include <netload/NLBuilder.h>
46 #include "TraCI.h"
47 
48 
49 // ===========================================================================
50 // member definitions
51 // ===========================================================================
52 /* void
53 TraCI::connect(const std::string& host, int port) {
54 }*/
55 
56 void
57 TraCI::load(const std::vector<std::string>& args) {
58  XMLSubSys::init(); // this may be not good for multiple loads
59  OptionsIO::setArgs(args);
61 }
62 
63 
64 void
66  if (time == 0) {
68  } else {
69  while (MSNet::getInstance()->getCurrentTimeStep() < time) {
71  }
72  }
73 }
74 
75 
76 void
78 }
79 
80 /* void
81 TraCI::subscribe(int domID, const std::string& objID, SUMOTime beginTime, SUMOTime endTime, const std::vector<int>& vars) const {
82 }
83 
84 void
85 TraCI::subscribeContext(int domID, const std::string& objID, SUMOTime beginTime, SUMOTime endTime, int domain, double range, const std::vector<
86  int>& vars) const {
87 } */
88 
91  return mySubscribedValues;
92 }
93 
94 const TraCI::TraCIValues&
95 TraCI::getSubscriptionResults(const std::string& objID) const {
96  if (mySubscribedValues.find(objID) != mySubscribedValues.end()) {
97  return mySubscribedValues.find(objID)->second;
98  } else {
99  throw; // Something?
100  }
101 }
102 
106 }
107 
109 TraCI::getContextSubscriptionResults(const std::string& objID) const {
110  if (mySubscribedContextValues.find(objID) != mySubscribedContextValues.end()) {
111  return mySubscribedContextValues.find(objID)->second;
112  } else {
113  throw; // Something?
114  }
115 }
116 
117 
121  for (int i = 0; i < (int)positionVector.size(); ++i) {
122  tp.push_back(makeTraCIPosition(positionVector[i]));
123  }
124  return tp;
125 }
126 
127 
130  PositionVector pv;
131  for (int i = 0; i < (int)vector.size(); i++) {
132  pv.push_back(Position(vector[i].x, vector[i].y));
133  }
134  return pv;
135 }
136 
137 
140  TraCIColor tc;
141  tc.a = color.alpha();
142  tc.b = color.blue();
143  tc.g = color.green();
144  tc.r = color.red();
145  return tc;
146 }
147 
148 RGBColor
150  return RGBColor((unsigned char)c.r, (unsigned char)c.g, (unsigned char)c.b, (unsigned char)c.a);
151 }
152 
153 
156  TraCIPosition p;
157  p.x = position.x();
158  p.y = position.y();
159  p.z = position.z();
160  return p;
161 }
162 
163 Position
165  Position p;
166  p.set(tpos.x, tpos.y, tpos.z);
167  return p;
168 }
169 
170 MSEdge*
171 TraCI::getEdge(const std::string& edgeID) {
172  MSEdge* edge = MSEdge::dictionary(edgeID);
173  if (edge == 0) {
174  throw TraCIException("Referenced edge '" + edgeID + "' is not known.");
175  }
176  return edge;
177 }
178 
179 const MSLane*
180 TraCI::getLaneChecking(const std::string& edgeID, int laneIndex, double pos) {
181  const MSEdge* edge = MSEdge::dictionary(edgeID);
182  if (edge == 0) {
183  throw TraCIException("Unknown edge " + edgeID);
184  }
185  if (laneIndex < 0 || laneIndex >= (int)edge->getLanes().size()) {
186  throw TraCIException("Invalid lane index for " + edgeID);
187  }
188  const MSLane* lane = edge->getLanes()[laneIndex];
189  if (pos < 0 || pos > lane->getLength()) {
190  throw TraCIException("Position on lane invalid");
191  }
192  return lane;
193 }
194 
195 std::pair<MSLane*, double>
198  std::pair<MSLane*, double> result;
199  std::vector<std::string> allEdgeIds;
200  double minDistance = std::numeric_limits<double>::max();
201 
202  allEdgeIds = MSNet::getInstance()->getEdgeControl().getEdgeNames();
203  for (std::vector<std::string>::iterator itId = allEdgeIds.begin(); itId != allEdgeIds.end(); itId++) {
204  const std::vector<MSLane*>& allLanes = MSEdge::dictionary((*itId))->getLanes();
205  for (std::vector<MSLane*>::const_iterator itLane = allLanes.begin(); itLane != allLanes.end(); itLane++) {
206  const double newDistance = (*itLane)->getShape().distance2D(pos);
207  if (newDistance < minDistance) {
208  minDistance = newDistance;
209  result.first = (*itLane);
210  }
211  }
212  }
213  // @todo this may be a place where 3D is required but 2D is delivered
214  result.second = result.first->getShape().nearest_offset_to_point2D(pos, false);
215  return result;
216 }
217 
218 
219 
220 /****************************************************************************/
double x
Definition: TraCIDefs.h:71
double z() const
Returns the z-position.
Definition: Position.h:72
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:53
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:167
std::vector< std::string > getEdgeNames() const
Returns the list of names of all known edges.
double x() const
Returns the x-position.
Definition: Position.h:62
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: TraCI.cpp:139
static MSEdge * getEdge(const std::string &edgeID)
Definition: TraCI.cpp:171
unsigned char g
Definition: TraCIDefs.h:78
const SubscribedValues & getSubscriptionResults() const
Definition: TraCI.cpp:90
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:483
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
static void load(const std::vector< std::string > &args)
load a simulation with the given arguments
Definition: TraCI.cpp:57
unsigned char r
Definition: TraCIDefs.h:78
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:744
void set(double x, double y)
set positions x and y
Definition: Position.h:92
unsigned char a
Definition: TraCIDefs.h:78
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:61
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:82
double z
Definition: TraCIDefs.h:71
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::map< std::string, SubscribedValues > SubscribedContextValues
Definition: TraCI.h:78
std::map< std::string, TraCIValues > SubscribedValues
Definition: TraCI.h:77
static const MSLane * getLaneChecking(const std::string &edgeID, int laneIndex, double pos)
Definition: TraCI.cpp:180
A 3D-position.
Definition: TraCIDefs.h:70
static PositionVector makePositionVector(const TraCIPositionVector &vector)
Definition: TraCI.cpp:129
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
void close()
Connects to the specified SUMO server.
Definition: TraCI.cpp:77
A list of positions.
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:89
double y() const
Returns the y-position.
Definition: Position.h:67
std::map< int, TraCIValue > TraCIValues
{object->{variable->value}}
Definition: TraCI.h:76
const SubscribedContextValues & getContextSubscriptionResults() const
Definition: TraCI.cpp:104
static std::pair< MSLane *, double > convertCartesianToRoadMap(Position pos)
Definition: TraCI.cpp:196
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: TraCI.cpp:119
A color.
Definition: TraCIDefs.h:77
SubscribedContextValues mySubscribedContextValues
Definition: TraCI.h:110
static Position makePosition(const TraCIPosition &position)
Definition: TraCI.cpp:164
unsigned char b
Definition: TraCIDefs.h:78
static TraCIPosition makeTraCIPosition(const Position &position)
Definition: TraCI.cpp:155
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:75
MSEdgeControl & getEdgeControl()
Returns the edge control.
Definition: MSNet.h:349
long long int SUMOTime
Definition: TraCIDefs.h:51
double y
Definition: TraCIDefs.h:71
void simulationStep()
Performs a single simulation step.
Definition: MSNet.cpp:437
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: TraCI.cpp:149
static MSNet * init()
Definition: NLBuilder.cpp:226
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:68
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
static void simulationStep(const SUMOTime time=0)
Advances by one step (or up to the given time)
Definition: TraCI.cpp:65
A list of positions.
SubscribedValues mySubscribedValues
Definition: TraCI.h:109