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.org/sumo
3 // Copyright (C) 2017-2022 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>
30 #include <libsumo/TraCIConstants.h>
31 #include "Helper.h"
32 #include "Junction.h"
33 
34 
35 namespace libsumo {
36 // ===========================================================================
37 // static member initializations
38 // ===========================================================================
39 SubscriptionResults Junction::mySubscriptionResults;
40 ContextSubscriptionResults Junction::myContextSubscriptionResults;
41 NamedRTree* Junction::myTree(nullptr);
42 
43 
44 // ===========================================================================
45 // member definitions
46 // ===========================================================================
47 std::vector<std::string>
48 Junction::getIDList() {
49  std::vector<std::string> ids;
51  return ids;
52 }
53 
54 
55 int
56 Junction::getIDCount() {
57  return (int)getIDList().size();
58 }
59 
60 
61 TraCIPosition
62 Junction::getPosition(const std::string& junctionID, const bool includeZ) {
63  return Helper::makeTraCIPosition(getJunction(junctionID)->getPosition(), includeZ);
64 }
65 
66 
67 TraCIPositionVector
68 Junction::getShape(const std::string& junctionID) {
69  return Helper::makeTraCIPositionVector(getJunction(junctionID)->getShape());
70 }
71 
72 
74 Junction::getJunction(const std::string& id) {
76  if (j == nullptr) {
77  throw TraCIException("Junction '" + id + "' is not known");
78  }
79  return j;
80 }
81 
82 
83 std::string
84 Junction::getParameter(const std::string& junctionID, const std::string& param) {
85  return getJunction(junctionID)->getParameter(param, "");
86 }
87 
88 
90 
91 
92 void
93 Junction::setParameter(const std::string& junctionID, const std::string& name, const std::string& value) {
94  getJunction(junctionID)->setParameter(name, value);
95 }
96 
97 
99 
100 
101 NamedRTree*
102 Junction::getTree() {
103  if (myTree == nullptr) {
104  myTree = new NamedRTree();
105  for (const auto& i : MSNet::getInstance()->getJunctionControl()) {
106  Boundary b = i.second->getShape().getBoxBoundary();
107  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
108  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
109  myTree->Insert(cmin, cmax, i.second);
110  }
111  }
112  return myTree;
113 }
114 
115 void
116 Junction::cleanup() {
117  delete myTree;
118  myTree = nullptr;
119 }
120 
121 void
122 Junction::storeShape(const std::string& id, PositionVector& shape) {
123  shape.push_back(getJunction(id)->getPosition());
124 }
125 
126 
127 std::shared_ptr<VariableWrapper>
128 Junction::makeWrapper() {
129  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
130 }
131 
132 
133 bool
134 Junction::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
135  switch (variable) {
136  case TRACI_ID_LIST:
137  return wrapper->wrapStringList(objID, variable, getIDList());
138  case ID_COUNT:
139  return wrapper->wrapInt(objID, variable, getIDCount());
140  case VAR_POSITION:
141  case VAR_POSITION3D:
142  return wrapper->wrapPosition(objID, variable, getPosition(objID, variable == VAR_POSITION3D));
143  case VAR_SHAPE:
144  return wrapper->wrapPositionVector(objID, variable, getShape(objID));
146  paramData->readUnsignedByte();
147  return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
149  paramData->readUnsignedByte();
150  return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
151  default:
152  return false;
153  }
154 }
155 
156 
157 }
158 
159 
160 /****************************************************************************/
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:69
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:115
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:129
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:117
PositionVector getShape(const bool closeShape) const
get position vector (shape) based on this boundary
Definition: Boundary.cpp:387
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:135
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:123
C++ TraCI client API implementation.
Definition: Junction.h:34
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:174
MSJunctionControl & getJunctionControl()
Returns the junctions control.
Definition: MSNet.h:459
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:378
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:338
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:279
TRACI_CONST int VAR_POSITION
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:278
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