Eclipse SUMO - Simulation of Urban MObility
libsumo/Junction.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2017-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
22 // C++ TraCI client API implementation
23 /****************************************************************************/
24 #include <config.h>
25 
28 #include <microsim/MSNet.h>
29 #include <microsim/MSEdge.h>
31 #include <libsumo/TraCIConstants.h>
32 #include "Helper.h"
33 #include "Junction.h"
34 
35 
36 namespace libsumo {
37 // ===========================================================================
38 // static member initializations
39 // ===========================================================================
40 SubscriptionResults Junction::mySubscriptionResults;
41 ContextSubscriptionResults Junction::myContextSubscriptionResults;
42 NamedRTree* Junction::myTree(nullptr);
43 
44 
45 // ===========================================================================
46 // member definitions
47 // ===========================================================================
48 std::vector<std::string>
49 Junction::getIDList() {
50  std::vector<std::string> ids;
52  return ids;
53 }
54 
55 
56 int
57 Junction::getIDCount() {
58  return (int)getIDList().size();
59 }
60 
61 
62 TraCIPosition
63 Junction::getPosition(const std::string& junctionID, const bool includeZ) {
64  return Helper::makeTraCIPosition(getJunction(junctionID)->getPosition(), includeZ);
65 }
66 
67 
68 TraCIPositionVector
69 Junction::getShape(const std::string& junctionID) {
70  return Helper::makeTraCIPositionVector(getJunction(junctionID)->getShape());
71 }
72 
73 
74 const std::vector<std::string>
75 Junction::getIncomingEdges(const std::string& junctionID) {
76  std::vector<std::string> result;
77  for (const MSEdge* edge : getJunction(junctionID)->getIncoming()) {
78  result.push_back(edge->getID());
79  }
80  return result;
81 }
82 
83 
84 const std::vector<std::string>
85 Junction::getOutgoingEdges(const std::string& junctionID) {
86  std::vector<std::string> result;
87  for (const MSEdge* edge : getJunction(junctionID)->getOutgoing()) {
88  result.push_back(edge->getID());
89  }
90  return result;
91 }
92 
93 
95 Junction::getJunction(const std::string& id) {
97  if (j == nullptr) {
98  throw TraCIException("Junction '" + id + "' is not known");
99  }
100  return j;
101 }
102 
103 
104 std::string
105 Junction::getParameter(const std::string& junctionID, const std::string& param) {
106  return getJunction(junctionID)->getParameter(param, "");
107 }
108 
109 
111 
112 
113 void
114 Junction::setParameter(const std::string& junctionID, const std::string& name, const std::string& value) {
115  getJunction(junctionID)->setParameter(name, value);
116 }
117 
118 
120 
121 
122 NamedRTree*
123 Junction::getTree() {
124  if (myTree == nullptr) {
125  myTree = new NamedRTree();
126  for (const auto& i : MSNet::getInstance()->getJunctionControl()) {
127  Boundary b = i.second->getShape().getBoxBoundary();
128  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
129  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
130  myTree->Insert(cmin, cmax, i.second);
131  }
132  }
133  return myTree;
134 }
135 
136 void
137 Junction::cleanup() {
138  delete myTree;
139  myTree = nullptr;
140 }
141 
142 void
143 Junction::storeShape(const std::string& id, PositionVector& shape) {
144  shape.push_back(getJunction(id)->getPosition());
145 }
146 
147 
148 std::shared_ptr<VariableWrapper>
149 Junction::makeWrapper() {
150  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
151 }
152 
153 
154 bool
155 Junction::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
156  switch (variable) {
157  case TRACI_ID_LIST:
158  return wrapper->wrapStringList(objID, variable, getIDList());
159  case ID_COUNT:
160  return wrapper->wrapInt(objID, variable, getIDCount());
161  case VAR_POSITION:
162  case VAR_POSITION3D:
163  return wrapper->wrapPosition(objID, variable, getPosition(objID, variable == VAR_POSITION3D));
164  case VAR_SHAPE:
165  return wrapper->wrapPositionVector(objID, variable, getShape(objID));
167  paramData->readUnsignedByte();
168  return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
169  case INCOMING_EDGES:
170  return wrapper->wrapStringList(objID, variable, getIncomingEdges(objID));
171  case OUTGOING_EDGES:
172  return wrapper->wrapStringList(objID, variable, getOutgoingEdges(objID));
174  paramData->readUnsignedByte();
175  return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
176  default:
177  return false;
178  }
179 }
180 
181 
182 }
183 
184 
185 /****************************************************************************/
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition: TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:123
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:130
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:118
PositionVector getShape(const bool closeShape) const
get position vector (shape) based on this boundary
Definition: Boundary.cpp:423
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:136
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:124
C++ TraCI client API implementation.
Definition: Junction.h:34
A road/street connecting two junctions.
Definition: MSEdge.h:77
The base class for an intersection.
Definition: MSJunction.h:58
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:182
MSJunctionControl & getJunctionControl()
Returns the junctions control.
Definition: MSNet.h:461
T get(const std::string &id) const
Retrieves an item.
void insertIDs(std::vector< std::string > &into) const
A RT-tree for efficient storing of SUMO's Named objects.
Definition: NamedRTree.h:61
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
A list of positions.
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
static TraCIPosition makeTraCIPosition(const Position &position, const bool includeZ=false)
Definition: Helper.cpp:377
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:337
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:338
TRACI_CONST int VAR_POSITION
TRACI_CONST int OUTGOING_EDGES
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:337
TRACI_CONST int VAR_SHAPE
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_POSITION3D
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int INCOMING_EDGES