Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-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 Simulation.h
15 : /// @author Robert Hilbrich
16 : /// @date 15.09.2017
17 : ///
18 : // C++ TraCI client API implementation
19 : /****************************************************************************/
20 : #pragma once
21 : #include <vector>
22 : #include <libsumo/TraCIDefs.h>
23 :
24 :
25 : // ===========================================================================
26 : // class declarations
27 : // ===========================================================================
28 : #ifndef LIBTRACI
29 : class PositionVector;
30 : #endif
31 :
32 :
33 : // ===========================================================================
34 : // class definitions
35 : // ===========================================================================
36 : namespace LIBSUMO_NAMESPACE {
37 : /**
38 : * @class Simulation
39 : * @brief C++ TraCI client API implementation
40 : */
41 : class Simulation {
42 : public:
43 : // not implemented in libsumo
44 : static std::pair<int, std::string> init(int port = 8813, int numRetries = libsumo::DEFAULT_NUM_RETRIES,
45 : const std::string& host = "localhost", const std::string& label = "default", FILE* const pipe = nullptr);
46 :
47 : static std::pair<int, std::string> start(const std::vector<std::string>& cmd, int port = -1, int numRetries = libsumo::DEFAULT_NUM_RETRIES,
48 : const std::string& label = "default", const bool verbose = false,
49 : const std::string& traceFile = "", bool traceGetters = true, void* _stdout = nullptr);
50 :
51 : static bool isLibsumo();
52 :
53 : // we cannot call this switch because it is a reserved word in C++
54 : // not implemented in libsumo
55 : static void switchConnection(const std::string& label);
56 :
57 : // not implemented in libsumo
58 : static const std::string& getLabel();
59 :
60 : // not implemented in libsumo
61 : static void setOrder(int order);
62 :
63 : /// @brief load a simulation with the given arguments
64 : static void load(const std::vector<std::string>& args);
65 :
66 : /// @brief whether we run with graphical user interface (sumo-gui)
67 : static bool hasGUI();
68 :
69 : /// @brief return whether a simulation (network) is present
70 : static bool isLoaded();
71 :
72 : /// @brief Advances by one step (or up to the given time)
73 : static void step(const double time = 0.);
74 :
75 : /// @brief Advances a "half" step
76 : static void executeMove();
77 :
78 : /// @brief close simulation
79 : static void close(const std::string& reason = "Libsumo requested termination.");
80 :
81 : /// @brief return TraCI API and SUMO version
82 : static std::pair<int, std::string> getVersion();
83 :
84 : /// @brief return the SUMO option value
85 : static std::string getOption(const std::string& option);
86 :
87 : static int getCurrentTime();
88 : static double getTime();
89 :
90 : /// @brief return configured end time
91 : static double getEndTime();
92 :
93 : static int getLoadedNumber();
94 : static std::vector<std::string> getLoadedIDList();
95 : static int getDepartedNumber();
96 : static std::vector<std::string> getDepartedIDList();
97 : static int getArrivedNumber();
98 : static std::vector<std::string> getArrivedIDList();
99 : static int getParkingStartingVehiclesNumber();
100 : static std::vector<std::string> getParkingStartingVehiclesIDList();
101 : static int getParkingEndingVehiclesNumber();
102 : static std::vector<std::string> getParkingEndingVehiclesIDList();
103 : static int getStopStartingVehiclesNumber();
104 : static std::vector<std::string> getStopStartingVehiclesIDList();
105 : static int getStopEndingVehiclesNumber();
106 : static std::vector<std::string> getStopEndingVehiclesIDList();
107 : static int getCollidingVehiclesNumber();
108 : static std::vector<std::string> getCollidingVehiclesIDList();
109 : static int getEmergencyStoppingVehiclesNumber();
110 : static std::vector<std::string> getEmergencyStoppingVehiclesIDList();
111 : static int getStartingTeleportNumber();
112 : static std::vector<std::string> getStartingTeleportIDList();
113 : static int getEndingTeleportNumber();
114 : static std::vector<std::string> getEndingTeleportIDList();
115 :
116 : static int getDepartedPersonNumber();
117 : static std::vector<std::string> getDepartedPersonIDList();
118 : static int getArrivedPersonNumber();
119 : static std::vector<std::string> getArrivedPersonIDList();
120 :
121 : static std::vector<std::string> getBusStopIDList();
122 : static int getBusStopWaiting(const std::string& stopID);
123 :
124 : /** @brief Returns the IDs of the transportables on a given bus stop.
125 : */
126 : static std::vector<std::string> getBusStopWaitingIDList(const std::string& stopID);
127 : static std::vector<std::string> getPendingVehicles();
128 :
129 : static std::vector<libsumo::TraCICollision> getCollisions();
130 :
131 : static double getScale();
132 : static double getDeltaT();
133 :
134 : static libsumo::TraCIPositionVector getNetBoundary();
135 :
136 : static int getMinExpectedNumber();
137 :
138 : static libsumo::TraCIPosition convert2D(const std::string& edgeID, double pos, int laneIndex = 0, bool toGeo = false);
139 :
140 : static libsumo::TraCIPosition convert3D(const std::string& edgeID, double pos, int laneIndex = 0, bool toGeo = false);
141 :
142 : static libsumo::TraCIRoadPosition convertRoad(double x, double y, bool isGeo = false, const std::string& vClass = "ignoring");
143 :
144 : static libsumo::TraCIPosition convertGeo(double x, double y, bool fromGeo = false);
145 :
146 : static double getDistance2D(double x1, double y1, double x2, double y2, bool isGeo = false, bool isDriving = false);
147 : static double getDistanceRoad(const std::string& edgeID1, double pos1, const std::string& edgeID2, double pos2, bool isDriving = false);
148 :
149 : static libsumo::TraCIStage findRoute(const std::string& fromEdge, const std::string& toEdge, const std::string& vType = "", const double depart = -1., const int routingMode = 0);
150 :
151 : /* @note: default arrivalPos is not -1 because this would lead to very short walks when moving against the edge direction,
152 : * instead the middle of the edge is used. DepartPos is treated differently so that 1-edge walks do not have length 0.
153 : */
154 : static std::vector<libsumo::TraCIStage> findIntermodalRoute(const std::string& fromEdge, const std::string& toEdge, const std::string& modes = "",
155 : double depart = -1., const int routingMode = 0, double speed = -1., double walkFactor = -1.,
156 : double departPos = 0, double arrivalPos = libsumo::INVALID_DOUBLE_VALUE, const double departPosLat = 0,
157 : const std::string& pType = "", const std::string& vType = "", const std::string& destStop = "");
158 :
159 : static std::string getParameter(const std::string& objectID, const std::string& key);
160 : static const std::pair<std::string, std::string> getParameterWithKey(const std::string& objectID, const std::string& key);
161 : static void setParameter(const std::string& objectID, const std::string& key, const std::string& value);
162 :
163 : static void setScale(double value);
164 : static void clearPending(const std::string& routeID = "");
165 : static void saveState(const std::string& fileName);
166 : /// @brief quick-load simulation state from file and return the state time
167 : static double loadState(const std::string& fileName);
168 : static void writeMessage(const std::string& msg);
169 :
170 0 : LIBSUMO_SUBSCRIPTION_API
171 :
172 : static void subscribe(const std::vector<int>& varIDs = std::vector<int>({-1}), double begin = libsumo::INVALID_DOUBLE_VALUE, double end = libsumo::INVALID_DOUBLE_VALUE, const libsumo::TraCIResults& parameters = libsumo::TraCIResults());
173 : static const libsumo::TraCIResults getSubscriptionResults();
174 :
175 : #ifndef LIBTRACI
176 : #ifndef SWIG
177 : static void storeShape(PositionVector& shape);
178 :
179 : static std::shared_ptr<VariableWrapper> makeWrapper();
180 :
181 : static bool handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData);
182 : #endif
183 : #endif
184 :
185 : private:
186 : #ifndef LIBTRACI
187 : static SubscriptionResults mySubscriptionResults;
188 : static ContextSubscriptionResults myContextSubscriptionResults;
189 : #ifdef HAVE_FOX
190 : /// @brief to avoid concurrent write access to the subscription results
191 : static FXMutex myStepMutex;
192 : #endif
193 : #endif
194 :
195 : /// @brief invalidated standard constructor
196 : Simulation() = delete;
197 : };
198 :
199 :
200 : }
|