SUMO - Simulation of Urban MObility
TraCIServerAPI_Junction.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // APIs for getting/setting junction values via TraCI
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2009-2016 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 
35 #ifndef NO_TRACI
36 
37 #include "TraCIConstants.h"
38 #include <microsim/MSNet.h>
39 #include <microsim/MSJunction.h>
41 #include <microsim/MSNet.h>
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 bool
54  tcpip::Storage& outputStorage) {
55  // variable
56  int variable = inputStorage.readUnsignedByte();
57  std::string id = inputStorage.readString();
58  // check variable
59  if (variable != ID_LIST && variable != VAR_POSITION && variable != ID_COUNT && variable != VAR_SHAPE) {
60  return server.writeErrorStatusCmd(CMD_GET_JUNCTION_VARIABLE, "Get Junction Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
61  }
62  // begin response building
63  tcpip::Storage tempMsg;
64  // response-code, variableID, objectID
66  tempMsg.writeUnsignedByte(variable);
67  tempMsg.writeString(id);
68  if (variable == ID_LIST) {
69  std::vector<std::string> ids;
72  tempMsg.writeStringList(ids);
73  } else if (variable == ID_COUNT) {
74  std::vector<std::string> ids;
77  tempMsg.writeInt((int) ids.size());
78  } else {
80  if (j == 0) {
81  return server.writeErrorStatusCmd(CMD_GET_JUNCTION_VARIABLE, "Junction '" + id + "' is not known", outputStorage);
82  }
83  switch (variable) {
84  case ID_LIST:
85  break;
86  case VAR_POSITION:
88  tempMsg.writeDouble(j->getPosition().x());
89  tempMsg.writeDouble(j->getPosition().y());
90  break;
91  case VAR_SHAPE:
93  tempMsg.writeUnsignedByte(MIN2(255, (int)j->getShape().size()));
94  for (int iPoint = 0; iPoint < MIN2(255, (int)j->getShape().size()); ++iPoint) {
95  tempMsg.writeDouble(j->getShape()[iPoint].x());
96  tempMsg.writeDouble(j->getShape()[iPoint].y());
97  }
98  break;
99 
100  default:
101  break;
102  }
103  }
104  server.writeStatusCmd(CMD_GET_JUNCTION_VARIABLE, RTYPE_OK, "", outputStorage);
105  server.writeResponseWithLength(outputStorage, tempMsg);
106  return true;
107 }
108 
109 bool
110 TraCIServerAPI_Junction::getPosition(const std::string& id, Position& p) {
112  if (j == 0) {
113  return false;
114  }
115  p = j->getPosition();
116  return true;
117 }
118 
119 
120 NamedRTree*
122  NamedRTree* t = new NamedRTree();
123  const std::map<std::string, MSJunction*>& junctions = MSNet::getInstance()->getJunctionControl().getMyMap();
124  for (std::map<std::string, MSJunction*>::const_iterator i = junctions.begin(); i != junctions.end(); ++i) {
125  Boundary b = (*i).second->getShape().getBoxBoundary();
126  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
127  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
128  t->Insert(cmin, cmax, (*i).second);
129  }
130  return t;
131 }
132 
133 #endif
134 
135 
136 /****************************************************************************/
137 
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
#define POSITION_2D
#define VAR_POSITION
static NamedRTree * getTree()
Returns a tree filled with junction instances.
The base class for an intersection.
Definition: MSJunction.h:64
#define RTYPE_OK
T get(const std::string &id) const
Retrieves an item.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa9: Get Junction Variable)
#define TYPE_POLYGON
A RT-tree for efficient storing of SUMO&#39;s Named objects.
Definition: NamedRTree.h:72
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:142
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
#define TYPE_STRINGLIST
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:148
virtual void writeUnsignedByte(int)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
#define VAR_SHAPE
void insertIDs(std::vector< std::string > &into) const
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:130
virtual void writeInt(int)
virtual int readUnsignedByte()
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
const IDMap & getMyMap() const
virtual void writeStringList(const std::vector< std::string > &s)
T MIN2(T a, T b)
Definition: StdDefs.h:69
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
const Position & getPosition() const
Definition: MSJunction.cpp:67
virtual std::string readString()
#define CMD_GET_JUNCTION_VARIABLE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:73
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:136
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
static bool getPosition(const std::string &id, Position &p)
Returns the named junction&#39;s position.
#define RESPONSE_GET_JUNCTION_VARIABLE
virtual void writeDouble(double)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
MSJunctionControl & getJunctionControl()
Returns the junctions control.
Definition: MSNet.h:390
#define ID_COUNT
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
#define TYPE_INTEGER
#define ID_LIST
const PositionVector & getShape() const
Returns this junction&#39;s shape.
Definition: MSJunction.h:93