SUMO - Simulation of Urban MObility
TraCI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // C++ TraCI client API implementation
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2012-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
36 #include <utils/geom/Position.h>
37 #include <utils/common/RGBColor.h>
40 #include <utils/xml/XMLSubSys.h>
41 #include <microsim/MSEdge.h>
42 #include <microsim/MSEdgeControl.h>
43 #include <microsim/MSFrame.h>
44 #include <microsim/MSLane.h>
46 #include "TraCI.h"
47 
48 // ===========================================================================
49 // static member definitions
50 // ===========================================================================
51 std::vector<std::string> TraCI::myLoadArgs;
52 
53 // ===========================================================================
54 // member definitions
55 // ===========================================================================
56 /* void
57 TraCI::connect(const std::string& host, int port) {
58 }*/
59 
60 void
61 TraCI::load(const std::vector<std::string>& args) {
62  myLoadArgs = args;
63 }
64 
65 void
67 }
68 
69 /* void
70 TraCI::subscribe(int domID, const std::string& objID, SUMOTime beginTime, SUMOTime endTime, const std::vector<int>& vars) const {
71 }
72 
73 void
74 TraCI::subscribeContext(int domID, const std::string& objID, SUMOTime beginTime, SUMOTime endTime, int domain, double range, const std::vector<
75  int>& vars) const {
76 } */
77 
80  return mySubscribedValues;
81 }
82 
83 const TraCI::TraCIValues&
84 TraCI::getSubscriptionResults(const std::string& objID) const {
85  if (mySubscribedValues.find(objID) != mySubscribedValues.end()) {
86  return mySubscribedValues.find(objID)->second;
87  } else {
88  throw; // Something?
89  }
90 }
91 
95 }
96 
98 TraCI::getContextSubscriptionResults(const std::string& objID) const {
99  if (mySubscribedContextValues.find(objID) != mySubscribedContextValues.end()) {
100  return mySubscribedContextValues.find(objID)->second;
101  } else {
102  throw; // Something?
103  }
104 }
105 
106 
110  for (int i = 0; i < (int)positionVector.size(); ++i) {
111  tp.push_back(makeTraCIPosition(positionVector[i]));
112  }
113  return tp;
114 }
115 
116 
119  PositionVector pv;
120  for (int i = 0; i < (int)vector.size(); i++) {
121  pv.push_back(Position(vector[i].x, vector[i].y));
122  }
123  return pv;
124 }
125 
126 
129  TraCIColor tc;
130  tc.a = color.alpha();
131  tc.b = color.blue();
132  tc.g = color.green();
133  tc.r = color.red();
134  return tc;
135 }
136 
137 RGBColor
139  return RGBColor((unsigned char)c.r, (unsigned char)c.g, (unsigned char)c.b, (unsigned char)c.a);
140 }
141 
142 
145  TraCIPosition p;
146  p.x = position.x();
147  p.y = position.y();
148  p.z = position.z();
149  return p;
150 }
151 
152 Position
154  Position p;
155  p.set(tpos.x, tpos.y, tpos.z);
156  return p;
157 }
158 
159 MSEdge*
160 TraCI::getEdge(const std::string& edgeID) {
161  MSEdge* edge = MSEdge::dictionary(edgeID);
162  if (edge == 0) {
163  throw TraCIException("Referenced edge '" + edgeID + "' is not known.");
164  }
165  return edge;
166 }
167 
168 const MSLane*
169 TraCI::getLaneChecking(const std::string& edgeID, int laneIndex, double pos) {
170  const MSEdge* edge = MSEdge::dictionary(edgeID);
171  if (edge == 0) {
172  throw TraCIException("Unknown edge " + edgeID);
173  }
174  if (laneIndex < 0 || laneIndex >= (int)edge->getLanes().size()) {
175  throw TraCIException("Invalid lane index for " + edgeID);
176  }
177  const MSLane* lane = edge->getLanes()[laneIndex];
178  if (pos < 0 || pos > lane->getLength()) {
179  throw TraCIException("Position on lane invalid");
180  }
181  return lane;
182 }
183 
184 std::pair<MSLane*, double>
187  std::pair<MSLane*, double> result;
188  std::vector<std::string> allEdgeIds;
189  double minDistance = std::numeric_limits<double>::max();
190 
191  allEdgeIds = MSNet::getInstance()->getEdgeControl().getEdgeNames();
192  for (std::vector<std::string>::iterator itId = allEdgeIds.begin(); itId != allEdgeIds.end(); itId++) {
193  const std::vector<MSLane*>& allLanes = MSEdge::dictionary((*itId))->getLanes();
194  for (std::vector<MSLane*>::const_iterator itLane = allLanes.begin(); itLane != allLanes.end(); itLane++) {
195  const double newDistance = (*itLane)->getShape().distance2D(pos);
196  if (newDistance < minDistance) {
197  minDistance = newDistance;
198  result.first = (*itLane);
199  }
200  }
201  }
202  // @todo this may be a place where 3D is required but 2D is delivered
203  result.second = result.first->getShape().nearest_offset_to_point2D(pos, false);
204  return result;
205 }
206 
207 
208 
209 /****************************************************************************/
double x
Definition: TraCIDefs.h:72
double z() const
Returns the z-position.
Definition: Position.h:73
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: TraCI.cpp:128
double y() const
Returns the y-position.
Definition: Position.h:68
static MSEdge * getEdge(const std::string &edgeID)
Definition: TraCI.cpp:160
unsigned char g
Definition: TraCIDefs.h:79
double x() const
Returns the x-position.
Definition: Position.h:63
static std::vector< std::string > myLoadArgs
Definition: TraCI.h:117
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:192
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
static void load(const std::vector< std::string > &args)
load a simulation with the given arguments
Definition: TraCI.cpp:61
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:484
unsigned char r
Definition: TraCIDefs.h:79
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:728
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
void set(double x, double y)
set positions x and y
Definition: Position.h:93
unsigned char a
Definition: TraCIDefs.h:79
std::vector< std::string > getEdgeNames() const
Returns the list of names of all known edges.
double z
Definition: TraCIDefs.h:72
A road/street connecting two junctions.
Definition: MSEdge.h:80
#define max(a, b)
Definition: polyfonts.c:65
std::map< std::string, SubscribedValues > SubscribedContextValues
Definition: TraCI.h:79
std::map< std::string, TraCIValues > SubscribedValues
Definition: TraCI.h:78
static const MSLane * getLaneChecking(const std::string &edgeID, int laneIndex, double pos)
Definition: TraCI.cpp:169
A 3D-position.
Definition: TraCIDefs.h:71
static PositionVector makePositionVector(const TraCIPositionVector &vector)
Definition: TraCI.cpp:118
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
void close()
Connects to the specified SUMO server.
Definition: TraCI.cpp:66
A list of positions.
const SubscribedContextValues & getContextSubscriptionResults() const
Definition: TraCI.cpp:93
std::map< int, TraCIValue > TraCIValues
{object->{variable->value}}
Definition: TraCI.h:77
static std::pair< MSLane *, double > convertCartesianToRoadMap(Position pos)
Definition: TraCI.cpp:185
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: TraCI.cpp:108
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
SubscribedContextValues mySubscribedContextValues
Definition: TraCI.h:115
static Position makePosition(const TraCIPosition &position)
Definition: TraCI.cpp:153
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
unsigned char b
Definition: TraCIDefs.h:79
static TraCIPosition makeTraCIPosition(const Position &position)
Definition: TraCI.cpp:144
MSEdgeControl & getEdgeControl()
Returns the edge control.
Definition: MSNet.h:353
double y
Definition: TraCIDefs.h:72
const SubscribedValues & getSubscriptionResults() const
Definition: TraCI.cpp:79
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: TraCI.cpp:138
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
A list of positions.
SubscribedValues mySubscribedValues
Definition: TraCI.h:114