Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libsumo/Junction.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2017-2025 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
/****************************************************************************/
14
/// @file Junction.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Mario Krumnow
17
/// @author Jakob Erdmann
18
/// @author Michael Behrisch
19
/// @author Robert Hilbrich
20
/// @date 30.05.2012
21
///
22
// C++ TraCI client API implementation
23
/****************************************************************************/
24
#include <config.h>
25
26
#include <utils/shapes/PointOfInterest.h>
27
#include <utils/shapes/ShapeContainer.h>
28
#include <microsim/MSNet.h>
29
#include <microsim/MSEdge.h>
30
#include <microsim/MSJunctionControl.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;
51
MSNet::getInstance()->getJunctionControl().insertIDs(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
94
MSJunction*
95
Junction::getJunction(const std::string& id) {
96
MSJunction* j = MSNet::getInstance()->getJunctionControl().get(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
110
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(Junction)
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
119
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(Junction, JUNCTION)
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));
166
case libsumo::VAR_PARAMETER:
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));
173
case libsumo::VAR_PARAMETER_WITH_KEY:
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
/****************************************************************************/
186
187