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-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 
35 #ifndef NO_TRACI
36 
38 #include "TraCIConstants.h"
39 #include "lib/TraCI_Junction.h"
40 #include "TraCIServer.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 bool
49  tcpip::Storage& outputStorage) {
50  // variable
51  int variable = inputStorage.readUnsignedByte();
52  std::string id = inputStorage.readString();
53  // check variable
54  if (variable != ID_LIST && variable != VAR_POSITION && variable != ID_COUNT && variable != VAR_SHAPE) {
55  return server.writeErrorStatusCmd(CMD_GET_JUNCTION_VARIABLE, "Get Junction Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
56  }
57  // begin response building
58  tcpip::Storage tempMsg;
59  // response-code, variableID, objectID
61  tempMsg.writeUnsignedByte(variable);
62  tempMsg.writeString(id);
63 
64  try {
65  switch (variable) {
66  case ID_LIST:
69  break;
70  case ID_COUNT:
73  break;
74  case VAR_POSITION: {
77  tempMsg.writeDouble(p.x);
78  tempMsg.writeDouble(p.y);
79  break;
80  }
81  case VAR_SHAPE: {
84  tempMsg.writeUnsignedByte(MIN2(255, (int) shp.size()));
85  for (int iPoint = 0; iPoint < MIN2(255, (int) shp.size()); ++iPoint) {
86  tempMsg.writeDouble(shp[iPoint].x);
87  tempMsg.writeDouble(shp[iPoint].y);
88  }
89  break;
90  }
91  default:
92  break;
93 
94  }
95  } catch (TraCIException& e) {
96  return server.writeErrorStatusCmd(CMD_GET_JUNCTION_VARIABLE, e.what(), outputStorage);
97  }
98  server.writeStatusCmd(CMD_GET_JUNCTION_VARIABLE, RTYPE_OK, "", outputStorage);
99  server.writeResponseWithLength(outputStorage, tempMsg);
100 
101  return true;
102 }
103 
104 bool
105 TraCIServerAPI_Junction::getPosition(const std::string& id, Position& p) {
107  if (j == 0) {
108  return false;
109  }
110  p = j->getPosition();
111  return true;
112 }
113 
114 
115 NamedRTree*
117  NamedRTree* t = new NamedRTree();
118  const std::map<std::string, MSJunction*>& junctions = MSNet::getInstance()->getJunctionControl().getMyMap();
119  for (std::map<std::string, MSJunction*>::const_iterator i = junctions.begin(); i != junctions.end(); ++i) {
120  Boundary b = (*i).second->getShape().getBoxBoundary();
121  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
122  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
123  t->Insert(cmin, cmax, (*i).second);
124  }
125  return t;
126 }
127 
128 #endif
129 
130 
131 /****************************************************************************/
132 
double x
Definition: TraCIDefs.h:72
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
#define POSITION_2D
#define VAR_POSITION
static NamedRTree * getTree()
Returns a tree filled with junction instances.
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
The base class for an intersection.
Definition: MSJunction.h:64
#define RTYPE_OK
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
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
#define TYPE_STRINGLIST
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
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
virtual void writeInt(int)
virtual int readUnsignedByte()
static int getIDCount()
A 3D-position.
Definition: TraCIDefs.h:71
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
static std::vector< std::string > getIDList()
virtual void writeStringList(const std::vector< std::string > &s)
T MIN2(T a, T b)
Definition: StdDefs.h:68
virtual std::string readString()
#define CMD_GET_JUNCTION_VARIABLE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
const IDMap & getMyMap() const
Returns a reference to the internal map.
virtual void writeString(const std::string &s)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
static bool getPosition(const std::string &id, Position &p)
Returns the named junction&#39;s position.
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
#define RESPONSE_GET_JUNCTION_VARIABLE
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
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.
double y
Definition: TraCIDefs.h:72
MSJunctionControl & getJunctionControl()
Returns the junctions control.
Definition: MSNet.h:388
static TraCIPositionVector getShape(const std::string &junctionID)
#define ID_COUNT
#define TYPE_INTEGER
#define ID_LIST
static TraCIPosition getPosition(const std::string &junctionID)
A list of positions.
static MSJunction * getJunction(const std::string &id)
const Position & getPosition() const
Definition: MSJunction.cpp:63