Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2005-2026 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 ShapeContainer.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @author Jakob Erdmann
18 : /// @date 2005-09-15
19 : ///
20 : // Storage for geometrical objects, sorted by the layers they are in
21 : /****************************************************************************/
22 : #pragma once
23 : #include <config.h>
24 :
25 : #include <string>
26 : #include <map>
27 : #include <memory>
28 : #include <utils/common/NamedObjectCont.h>
29 : #include "PointOfInterest.h"
30 : #include "SUMOPolygon.h"
31 :
32 :
33 : // ===========================================================================
34 : // class declarations
35 : // ===========================================================================
36 : class PolygonDynamics;
37 : class ShapeListener;
38 : class SUMOTrafficObject;
39 : template <class T, class S>
40 : class ParametrisedWrappingCommand;
41 :
42 :
43 : // ===========================================================================
44 : // class definitions
45 : // ===========================================================================
46 : /**
47 : * @class ShapeContainer
48 : * @brief Storage for geometrical objects
49 : */
50 : class ShapeContainer {
51 : public:
52 :
53 : /// @brief containers
54 : typedef NamedObjectCont<SUMOPolygon*> Polygons;
55 : typedef NamedObjectCont<PointOfInterest*> POIs;
56 :
57 : /// @brief Constructor
58 : ShapeContainer();
59 :
60 : /// @brief Destructor
61 : virtual ~ShapeContainer();
62 :
63 : /** @brief Builds a polygon using the given values and adds it to the container
64 : * @param[in] id The name of the polygon
65 : * @param[in] type The (abstract) type of the polygon
66 : * @param[in] color The color of the polygon
67 : * @param[in] layer The layer of the polygon
68 : * @param[in] angle The rotation of the polygon
69 : * @param[in] imgFile The raster image of the polygon
70 : * @param[in] shape The shape of the polygon
71 : * @param[in] geo specify if shape was loaded as GEO coordinate
72 : * @param[in] fill Whether the polygon shall be filled
73 : * @param[in] lineWidth Line width when drawing unfilled polygon
74 : * @return whether the polygon could be added
75 : */
76 : virtual bool addPolygon(const std::string& id, const std::string& type,
77 : const RGBColor& color, double layer,
78 : double angle, const std::string& imgFile,
79 : const PositionVector& shape, bool geo,
80 : bool fill, double lineWidth, bool ignorePruning = false,
81 : const std::string& name = Shape::DEFAULT_NAME);
82 :
83 : /**
84 : * @brief Adds dynamics (animation / tracking) to the given polygon
85 : * @param polyID ID of the polygon which should become dynamic
86 : * @return true if the operation was successful, false if not.
87 : * @see PolygonDynamics()
88 : */
89 : virtual PolygonDynamics* addPolygonDynamics(double simtime,
90 : std::string polyID,
91 : SUMOTrafficObject* trackedObject,
92 : const std::vector<double>& timeSpan,
93 : const std::vector<double>& alphaSpan,
94 : bool looped,
95 : bool rotate);
96 :
97 : /**
98 : * @brief Remove dynamics (animation / tracking) for the given polygon
99 : * @param polyID ID of the polygon for which dynamics shall be removed
100 : * @return true if the operation was successful (dynamics existed for the polygon), false if not.
101 : */
102 : virtual bool removePolygonDynamics(const std::string& polyID);
103 :
104 : /** @brief Builds a POI using the given values and adds it to the container
105 : * @param[in] id The name of the POI
106 : * @param[in] type The (abstract) type of the POI
107 : * @param[in] color The color of the POI
108 : * @param[in] pos The position of the POI
109 : * @param[in[ geo use GEO coordinates (lon/lat)
110 : * @param[in] lane The Lane in which this POI is placed
111 : * @param[in] posOverLane The position over Lane
112 : * @param[in] friendlyPos enable or disable friendly position over lane
113 : * @param[in] posLat The position lateral over Lane
114 : * @param[in] icon The icon of the POI
115 : * @param[in] layer The layer of the POI
116 : * @param[in] angle The rotation of the POI
117 : * @param[in] imgFile The raster image of the POI
118 : * @param[in] width The width of the POI image
119 : * @param[in] height The height of the POI image
120 : * @return whether the poi could be added
121 : */
122 : virtual bool addPOI(const std::string& id, const std::string& type, const RGBColor& color, const Position& pos, bool geo,
123 : const std::string& lane, double posOverLane, bool friendlyPos, double posLat, const std::string& icon, double layer,
124 : double angle, const std::string& imgFile, double width, double height, bool ignorePruning = false);
125 :
126 : /** @brief Removes a polygon from the container
127 : * @param[in] id The id of the polygon
128 : * @return Whether the polygon could be removed
129 : */
130 : virtual bool removePolygon(const std::string& id, bool useLock = true);
131 :
132 : /** @brief Removes a PoI from the container
133 : * @param[in] id The id of the PoI
134 : * @return Whether the poi could be removed
135 : */
136 : virtual bool removePOI(const std::string& id);
137 :
138 : /** @brief Assigns a new position to the named PoI
139 : * @param[in] id The id of the PoI to move
140 : * @param[in] pos The PoI's new position
141 : */
142 : virtual void movePOI(const std::string& id, const Position& pos);
143 :
144 : /** @brief Assigns a shape to the named polygon
145 : * @param[in] id The id of the polygon to reshape
146 : * @param[in] shape The polygon's new shape
147 : */
148 : virtual void reshapePolygon(const std::string& id, const PositionVector& shape);
149 :
150 : /// @brief Returns all polygons
151 : inline const Polygons& getPolygons() const {
152 571 : return myPolygons;
153 : }
154 :
155 : /// @brief Returns all pois
156 : inline const POIs& getPOIs() const {
157 356 : return myPOIs;
158 : }
159 :
160 : /** @brief Regular update event for updating polygon dynamics
161 : * @param[in] t The time at which the update is called
162 : * @param[in] pd The dynamics to be updated
163 : * @returns zero If dynamics has expired, next update time otherwise
164 : */
165 : virtual SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics* pd);
166 :
167 : /// @brief Register update command (for descheduling at removal)
168 : virtual void addPolygonUpdateCommand(std::string polyID, ParametrisedWrappingCommand<ShapeContainer, PolygonDynamics*>* cmd);
169 :
170 : /// @brief Remove all tracking polygons for the given object
171 : virtual void removeTrackers(std::string objectID);
172 :
173 : /// @brief register highlight of the specified type if the given id
174 : virtual void registerHighlight(const std::string& objectID, const int type, const std::string& polygonID);
175 :
176 : /** @brief Remove all dynamics before quick-loading state */
177 : void clearState();
178 :
179 : void addShapeListener(ShapeListener* listener) {
180 : myListeners.push_back(listener);
181 : }
182 :
183 : protected:
184 : /// @brief add polygon
185 : virtual bool add(SUMOPolygon* poly, bool ignorePruning = false);
186 :
187 : /// @brief add poi
188 : virtual bool add(PointOfInterest* poi, bool ignorePruning = false);
189 :
190 : /** @brief Unschedules the removal and update commands of the given polygon.
191 : * @param[in] id The id of the polygon
192 : */
193 : virtual void cleanupPolygonDynamics(const std::string& id);
194 :
195 : /// @name Management of highlights. For each type, only one highlight can be active,
196 : /// @see myHighlightPolygons, myHighlightedObjects
197 : /// @{
198 : /// @brief Remove any previously added highlight polygon of the specified type
199 : /// @param[out] toRemove will hold the id of any polygon that was highlighting the given object
200 : virtual void clearHighlight(const std::string& objectID, const int type, std::string& toRemove);
201 : /// @brief Clears all highlight information from the maps when the object leaves the net
202 : /// (Highlight polygons and dynamics are removed via removeTrackers())
203 : virtual void clearHighlights(const std::string& objectID, SUMOPolygon* p);
204 : /// @}
205 :
206 : protected:
207 : /// @brief stored Polygons
208 : Polygons myPolygons;
209 :
210 : /// @brief stored PolygonDynamics
211 : std::map<std::string, PolygonDynamics*> myPolygonDynamics;
212 :
213 : /// @brief maps objects to a map of highlight types to highlighting polygons
214 : std::map<std::string, std::map<int, std::string> > myHighlightPolygons;
215 : /// @brief inverse map to myHighlightPolygons saves the highlighted object for each polygon
216 : std::map<std::string, std::string> myHighlightedObjects;
217 :
218 : /// @brief Information about tracked objects
219 : /// @note Maps tracked object IDs to set of polygons, which are tracking the object.
220 : /// Needed at object removal to cancel tacking (i.e. remove tracking poly).
221 : std::map<const std::string, std::set<const SUMOPolygon*> > myTrackingPolygons;
222 :
223 : /// @brief stored POIs
224 : POIs myPOIs;
225 :
226 : private:
227 : /// @brief Command pointers for scheduled polygon update. Maps PolyID->Command
228 : std::map<const std::string, ParametrisedWrappingCommand<ShapeContainer, PolygonDynamics*>*> myPolygonUpdateCommands;
229 : std::vector<ShapeListener*> myListeners;
230 :
231 : };
232 :
233 :
234 : /**
235 : * @class ShapeListener
236 : * @brief Interface for objects which want to be notified about shape updates
237 : */
238 : class ShapeListener {
239 : public:
240 0 : virtual void polygonChanged(const SUMOPolygon* const poly, const bool added, const bool removed) {
241 : UNUSED_PARAMETER(poly);
242 : UNUSED_PARAMETER(added);
243 : UNUSED_PARAMETER(removed);
244 0 : }
245 0 : virtual void poiChanged(const PointOfInterest* const poi, const bool added, const bool removed) {
246 : UNUSED_PARAMETER(poi);
247 : UNUSED_PARAMETER(added);
248 : UNUSED_PARAMETER(removed);
249 0 : }
250 : };
|