Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2013-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 MSDevice_FCDReplay.h
15 : /// @author Michael Behrisch
16 : /// @date 01.03.2024
17 : ///
18 : // A device which replays recorded floating car data
19 : /****************************************************************************/
20 : #pragma once
21 : #include <config.h>
22 :
23 : #include <utils/common/Command.h>
24 : #include <utils/common/MapMatcher.h>
25 : #include <utils/xml/SUMOSAXHandler.h>
26 : #include "MSVehicleDevice.h"
27 :
28 :
29 : // ===========================================================================
30 : // class declarations
31 : // ===========================================================================
32 : class SUMOSAXReader;
33 :
34 :
35 : // ===========================================================================
36 : // class definitions
37 : // ===========================================================================
38 : /**
39 : * @class MSDevice_FCDReplay
40 : * @brief A device which replays a vehicle trajectory from an fcd file
41 : *
42 : * @see MSDevice
43 : */
44 : class MSDevice_FCDReplay : public MSVehicleDevice {
45 : public:
46 : /** @brief Inserts MSDevice_FCDReplay-options
47 : * @param[filled] oc The options container to add the options to
48 : */
49 : static void insertOptions(OptionsCont& oc);
50 :
51 :
52 : /** @brief Build devices for the given vehicle, if needed
53 : *
54 : * The options are read and evaluated whether a FCDReplay-device shall be built
55 : * for the given vehicle.
56 : *
57 : * The built device is stored in the given vector.
58 : *
59 : * @param[in] v The vehicle for which a device may be built
60 : * @param[filled] into The vector to store the built device in
61 : */
62 : static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
63 :
64 : /** @brief Static intialization
65 : */
66 : static void init();
67 : static SUMOTime parseNext(SUMOTime t);
68 :
69 : public:
70 : /// @brief Destructor.
71 : ~MSDevice_FCDReplay();
72 :
73 : void move(SUMOTime currentTime);
74 :
75 : /// @brief return the name for this type of device
76 0 : const std::string deviceName() const override {
77 0 : return "fcd-replay";
78 : }
79 :
80 70300 : struct TrajectoryEntry {
81 : SUMOTime time;
82 : Position pos;
83 : std::string edgeOrLane;
84 : double lanePos;
85 : double speed;
86 : double angle;
87 : };
88 :
89 : typedef std::vector<TrajectoryEntry> Trajectory;
90 :
91 : void setTrajectory(const Trajectory* const t) {
92 36 : myTrajectory = t;
93 36 : myTrajectoryIndex = 1;
94 : }
95 :
96 : private:
97 : /** @brief Constructor
98 : *
99 : * @param[in] holder The vehicle that holds this device
100 : * @param[in] id The ID of the device
101 : */
102 : MSDevice_FCDReplay(SUMOVehicle& holder, const std::string& id);
103 :
104 24 : class MoveVehicles : public Command {
105 : public:
106 : SUMOTime execute(SUMOTime currentTime) override;
107 : private:
108 : /// @brief Invalidated assignment operator.
109 : MoveVehicles& operator=(const MoveVehicles&) = delete;
110 : };
111 :
112 : class FCDHandler : public SUMOSAXHandler, public MapMatcher<MSEdge, MSLane, MSJunction> {
113 : public:
114 : FCDHandler(const std::string& file);
115 : SUMOTime getTime() const {
116 56340 : return myTime;
117 : }
118 : void updateTrafficObjects(const SUMOTime intervalStart);
119 :
120 : const std::map<std::string, Trajectory>& getTrajectories() {
121 : return myTrajectories;
122 : }
123 :
124 : protected:
125 : /// @name inherited from GenericSAXHandler
126 : //@{
127 :
128 : /** @brief Called on the opening of a tag
129 : *
130 : * @param[in] element ID of the currently opened element
131 : * @param[in] attrs Attributes within the currently opened element
132 : * @exception ProcessError If something fails
133 : * @see GenericSAXHandler::myStartElement
134 : */
135 : void myStartElement(int element, const SUMOSAXAttributes& attrs) override;
136 : //@}
137 :
138 : void initLaneTree(NamedRTree* tree) override;
139 :
140 : MSEdge* retrieveEdge(const std::string& id) override;
141 :
142 : private:
143 200 : struct StageStart {
144 : std::string vehicle;
145 : int trajectoryOffset;
146 : int routeOffset;
147 : };
148 :
149 : MSTransportable::MSTransportablePlan* makePlan(const SUMOVehicleParameter& params, const ConstMSEdgeVector& route,
150 : const std::vector<StageStart>& stages, const Trajectory& t);
151 : ConstMSEdgeVector checkRoute(const ConstMSEdgeVector& edges, const SUMOVehicle* const vehicle);
152 :
153 : SUMOTime myTime = 0;
154 : std::map<std::string, Trajectory> myTrajectories;
155 : std::map<std::string, std::tuple<SUMOTime, std::string, bool, ConstMSEdgeVector, std::vector<StageStart> > > myRoutes;
156 : std::map<const Position, std::string> myPositions;
157 : };
158 :
159 : private:
160 : static FCDHandler* myHandler;
161 : static SUMOSAXReader* myParser;
162 : const Trajectory* myTrajectory = nullptr;
163 : int myTrajectoryIndex = 0;
164 :
165 : private:
166 : /// @brief Invalidated copy constructor.
167 : MSDevice_FCDReplay(const MSDevice_FCDReplay&) = delete;
168 :
169 : /// @brief Invalidated assignment operator.
170 : MSDevice_FCDReplay& operator=(const MSDevice_FCDReplay&) = delete;
171 :
172 : };
|