1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    Junction.cpp
11 /// @author  Daniel Krajzewicz
12 /// @author  Mario Krumnow
13 /// @author  Jakob Erdmann
14 /// @author  Michael Behrisch
15 /// @author  Robert Hilbrich
16 /// @date    30.05.2012
17 /// @version $Id$
18 ///
19 // C++ TraCI client API implementation
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <utils/shapes/PointOfInterest.h>
29 #include <utils/shapes/ShapeContainer.h>
30 #include <microsim/MSNet.h>
31 #include <microsim/MSJunctionControl.h>
32 #include <libsumo/TraCIConstants.h>
33 #include "Helper.h"
34 #include "Junction.h"
35 
36 
37 namespace libsumo {
38 // ===========================================================================
39 // static member initializations
40 // ===========================================================================
41 SubscriptionResults Junction::mySubscriptionResults;
42 ContextSubscriptionResults Junction::myContextSubscriptionResults;
43 
44 
45 // ===========================================================================
46 // member definitions
47 // ===========================================================================
48 std::vector<std::string>
getIDList()49 Junction::getIDList() {
50     std::vector<std::string> ids;
51     MSNet::getInstance()->getJunctionControl().insertIDs(ids);
52     return ids;
53 }
54 
55 
56 int
getIDCount()57 Junction::getIDCount() {
58     return (int)getIDList().size();
59 }
60 
61 
62 TraCIPosition
getPosition(const std::string & junctionID,const bool includeZ)63 Junction::getPosition(const std::string& junctionID, const bool includeZ) {
64     return Helper::makeTraCIPosition(getJunction(junctionID)->getPosition(), includeZ);
65 }
66 
67 
68 TraCIPositionVector
getShape(const std::string & junctionID)69 Junction::getShape(const std::string& junctionID) {
70     return Helper::makeTraCIPositionVector(getJunction(junctionID)->getShape());
71 }
72 
73 
74 MSJunction*
getJunction(const std::string & id)75 Junction::getJunction(const std::string& id) {
76     MSJunction* j = MSNet::getInstance()->getJunctionControl().get(id);
77     if (j == nullptr) {
78         throw TraCIException("Junction '" + id + "' is not known");
79     }
80     return j;
81 }
82 
83 
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(Junction,JUNCTION)84 LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(Junction, JUNCTION)
85 
86 
87 NamedRTree*
88 Junction::getTree() {
89     NamedRTree* t = new NamedRTree();
90     for (const auto& i : MSNet::getInstance()->getJunctionControl()) {
91         Boundary b = i.second->getShape().getBoxBoundary();
92         const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
93         const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
94         t->Insert(cmin, cmax, i.second);
95     }
96     return t;
97 }
98 
99 
100 void
storeShape(const std::string & id,PositionVector & shape)101 Junction::storeShape(const std::string& id, PositionVector& shape) {
102     shape.push_back(getJunction(id)->getPosition());
103 }
104 
105 
106 std::shared_ptr<VariableWrapper>
makeWrapper()107 Junction::makeWrapper() {
108     return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
109 }
110 
111 
112 bool
handleVariable(const std::string & objID,const int variable,VariableWrapper * wrapper)113 Junction::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
114     switch (variable) {
115         case TRACI_ID_LIST:
116             return wrapper->wrapStringList(objID, variable, getIDList());
117         case ID_COUNT:
118             return wrapper->wrapInt(objID, variable, getIDCount());
119         case VAR_POSITION:
120             return wrapper->wrapPosition(objID, variable, getPosition(objID));
121         default:
122             return false;
123     }
124 }
125 
126 
127 }
128 
129 
130 /****************************************************************************/
131