Line data Source code
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 : /****************************************************************************/ 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 170 : Junction::getIDList() { 50 : std::vector<std::string> ids; 51 170 : MSNet::getInstance()->getJunctionControl().insertIDs(ids); 52 168 : return ids; 53 2 : } 54 : 55 : 56 : int 57 9 : Junction::getIDCount() { 58 9 : return (int)getIDList().size(); 59 : } 60 : 61 : 62 : TraCIPosition 63 10026 : Junction::getPosition(const std::string& junctionID, const bool includeZ) { 64 10026 : return Helper::makeTraCIPosition(getJunction(junctionID)->getPosition(), includeZ); 65 : } 66 : 67 : 68 : TraCIPositionVector 69 3 : Junction::getShape(const std::string& junctionID) { 70 3 : return Helper::makeTraCIPositionVector(getJunction(junctionID)->getShape()); 71 : } 72 : 73 : 74 : const std::vector<std::string> 75 6 : Junction::getIncomingEdges(const std::string& junctionID) { 76 : std::vector<std::string> result; 77 30 : for (const MSEdge* edge : getJunction(junctionID)->getIncoming()) { 78 24 : result.push_back(edge->getID()); 79 : } 80 6 : return result; 81 0 : } 82 : 83 : 84 : const std::vector<std::string> 85 6 : Junction::getOutgoingEdges(const std::string& junctionID) { 86 : std::vector<std::string> result; 87 30 : for (const MSEdge* edge : getJunction(junctionID)->getOutgoing()) { 88 24 : result.push_back(edge->getID()); 89 : } 90 6 : return result; 91 0 : } 92 : 93 : 94 : MSJunction* 95 30268 : Junction::getJunction(const std::string& id) { 96 30268 : MSJunction* j = MSNet::getInstance()->getJunctionControl().get(id); 97 30266 : if (j == nullptr) { 98 4 : throw TraCIException("Junction '" + id + "' is not known"); 99 : } 100 30266 : return j; 101 : } 102 : 103 : 104 : std::string 105 6 : Junction::getParameter(const std::string& junctionID, const std::string& param) { 106 12 : return getJunction(junctionID)->getParameter(param, ""); 107 : } 108 : 109 : 110 0 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(Junction) 111 : 112 : 113 : void 114 6 : Junction::setParameter(const std::string& junctionID, const std::string& name, const std::string& value) { 115 6 : getJunction(junctionID)->setParameter(name, value); 116 6 : } 117 : 118 : 119 8198 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(Junction, JUNCTION) 120 : 121 : 122 : NamedRTree* 123 212 : Junction::getTree() { 124 212 : if (myTree == nullptr) { 125 10 : myTree = new NamedRTree(); 126 100 : for (const auto& i : MSNet::getInstance()->getJunctionControl()) { 127 90 : Boundary b = i.second->getShape().getBoxBoundary(); 128 90 : const float cmin[2] = {(float) b.xmin(), (float) b.ymin()}; 129 90 : const float cmax[2] = {(float) b.xmax(), (float) b.ymax()}; 130 90 : myTree->Insert(cmin, cmax, i.second); 131 90 : } 132 : } 133 212 : return myTree; 134 : } 135 : 136 : void 137 35156 : Junction::cleanup() { 138 35156 : delete myTree; 139 35156 : myTree = nullptr; 140 35156 : } 141 : 142 : void 143 20215 : Junction::storeShape(const std::string& id, PositionVector& shape) { 144 20215 : shape.push_back(getJunction(id)->getPosition()); 145 20215 : } 146 : 147 : 148 : std::shared_ptr<VariableWrapper> 149 267 : Junction::makeWrapper() { 150 267 : return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults); 151 : } 152 : 153 : 154 : bool 155 6157 : Junction::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) { 156 6157 : switch (variable) { 157 107 : case TRACI_ID_LIST: 158 107 : return wrapper->wrapStringList(objID, variable, getIDList()); 159 7 : case ID_COUNT: 160 7 : return wrapper->wrapInt(objID, variable, getIDCount()); 161 6018 : case VAR_POSITION: 162 : case VAR_POSITION3D: 163 6018 : return wrapper->wrapPosition(objID, variable, getPosition(objID, variable == VAR_POSITION3D)); 164 3 : case VAR_SHAPE: 165 6 : return wrapper->wrapPositionVector(objID, variable, getShape(objID)); 166 4 : case libsumo::VAR_PARAMETER: 167 4 : paramData->readUnsignedByte(); 168 8 : return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString())); 169 4 : case INCOMING_EDGES: 170 4 : return wrapper->wrapStringList(objID, variable, getIncomingEdges(objID)); 171 4 : case OUTGOING_EDGES: 172 4 : return wrapper->wrapStringList(objID, variable, getOutgoingEdges(objID)); 173 0 : case libsumo::VAR_PARAMETER_WITH_KEY: 174 0 : paramData->readUnsignedByte(); 175 0 : return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString())); 176 : default: 177 : return false; 178 : } 179 : } 180 : 181 : 182 : } 183 : 184 : 185 : /****************************************************************************/