1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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    GNEPoly.h
11 /// @author  Pablo Alvarez Lopez
12 /// @date    Jun 2017
13 /// @version $Id$
14 ///
15 // A class for visualizing and editing POIS in netedit (adapted from
16 // GUIPolygon and NLHandler)
17 /****************************************************************************/
18 #ifndef GNEPoly_h
19 #define GNEPoly_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 
26 #include <utils/gui/globjects/GUIPolygon.h>
27 
28 #include "GNEShape.h"
29 
30 // ===========================================================================
31 // class declarations
32 // ===========================================================================
33 class GeoConvHelper;
34 class GNENetElement;
35 
36 // ===========================================================================
37 // class definitions
38 // ===========================================================================
39 /**
40  * @class GNEPoly
41  *
42  * In the case the represented junction's shape is empty, the boundary
43  *  is computed using the junction's position to which an offset of 1m to each
44  *  side is added.
45  */
46 class GNEPoly : public GUIPolygon, public GNEShape {
47 
48 public:
49     /// @brief needed to avoid diamond Problem between GUIPolygon and GNEShape
50     using GNEShape::getID;
51 
52     /** @brief Constructor
53      * @param[in] net net in which this polygon is placed
54      * @param[in] id The name of the polygon
55      * @param[in] type The (abstract) type of the polygon
56      * @param[in] shape The shape of the polygon
57      * @param[in] geo specifiy if shape was loaded as GEO
58      * @param[in] color The color of the polygon
59      * @param[in] layer The layer of the polygon
60      * @param[in] angle The rotation of the polygon
61      * @param[in] imgFile The raster image of the polygon
62      * @param[in] relativePath set image file as relative path
63      * @param[in] fill Whether the polygon shall be filled
64      * @param[in] lineWidth Line width when drawing unfilled polygon
65      * @param[in] movementBlocked if movement of POI is blocked
66      * @param[in] shapeBlocked if shape of POI is blocked
67      */
68     GNEPoly(GNENet* net, const std::string& id, const std::string& type, const PositionVector& shape, bool geo, bool fill, double lineWidth,
69             const RGBColor& color, double layer, double angle, const std::string& imgFile, bool relativePath, bool movementBlocked, bool shapeBlocked);
70 
71     /// @brief Destructor
72     ~GNEPoly();
73 
74     /// @brief gererate a new ID for an element child
75     std::string generateChildID(SumoXMLTag childTag);
76 
77     /// @name functions for edit geometry
78     /// @{
79     /// @brief begin movement (used when user click over edge to start a movement, to avoid problems with problems with GL Tree)
80     void startGeometryMoving();
81 
82     /// @brief begin movement (used when user click over edge to start a movement, to avoid problems with problems with GL Tree)
83     void endGeometryMoving();
84 
85     /**@brief change position of a vertex of shape without commiting change
86     * @param[in] index index of Vertex shape
87     * @param[in] newPos The new position of vertex
88     * @return index of vertex (in some cases index can change
89     */
90     int moveVertexShape(const int index, const Position& oldPos, const Position& offset);
91 
92     /**@brief move entire shape without commiting change
93     * @param[in] oldShape the old shape of polygon before moving
94     * @param[in] offset the offset of movement
95     */
96     void moveEntireShape(const PositionVector& oldShape, const Position& offset);
97 
98     /**@brief commit geometry changes in the attributes of an element after use of changeShapeGeometry(...)
99     * @param[in] oldShape the old shape of polygon
100     * @param[in] undoList The undoList on which to register changes
101     */
102     void commitShapeChange(const PositionVector& oldShape, GNEUndoList* undoList);
103     /// @}
104 
105     /// @name inherited from GNEShape
106     /// @{
107     /// @brief update pre-computed geometry information
108     void updateGeometry(bool updateGrid);
109 
110     /**@brief writte shape element into a xml file
111     * @param[in] device device in which write parameters of additional element
112     */
113     void writeShape(OutputDevice& device);
114 
115     /// @brief Returns position of additional in view
116     Position getPositionInView() const;
117 
118     /// @brief Returns the numerical id of the object
119     GUIGlID getGlID() const;
120     /// @}
121 
122     /// @name inherited from GUIGlObject
123     /// @{
124     /**@brief Returns the name of the parent object
125      * @return This object's parent id
126      */
127     std::string getParentName() const;
128 
129     /**@brief Returns an own popup-menu
130      *
131      * @param[in] app The application needed to build the popup-menu
132      * @param[in] parent The parent window needed to build the popup-menu
133      * @return The built popup-menu
134      * @see GUIGlObject::getPopUpMenu
135      */
136     GUIGLObjectPopupMenu* getPopUpMenu(GUIMainWindow& app, GUISUMOAbstractView& parent);
137 
138     /**@brief Returns an own parameter window
139      *
140      * @param[in] app The application needed to build the parameter window
141      * @param[in] parent The parent window needed to build the parameter window
142      * @return The built parameter window
143      * @see GUIGlObject::getParameterWindow
144      */
145     GUIParameterTableWindow* getParameterWindow(GUIMainWindow& app, GUISUMOAbstractView& parent);
146 
147     /// @brief Returns the boundary to which the view shall be centered in order to show the object
148     Boundary getCenteringBoundary() const;
149 
150     /**@brief Draws the object
151      * @param[in] s The settings for the current view (may influence drawing)
152      * @see GUIGlObject::drawGL
153      */
154     void drawGL(const GUIVisualizationSettings& s) const;
155     /// @}
156 
157     /// @name inherited from GNEAttributeCarrier
158     /// @{
159     /* @brief method for getting the Attribute of an XML key
160      * @param[in] key The attribute key
161      * @return string with the value associated to key
162      */
163     std::string getAttribute(SumoXMLAttr key) const;
164 
165     /**@brief method for setting the attribute and letting the object perform additional changes
166      * @param[in] key The attribute key
167      * @param[in] value The new value
168      * @param[in] undoList The undoList on which to register changes
169      */
170     void setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList);
171 
172     /**@brief method for checking if the key and their conrrespond attribute are valids
173      * @param[in] key The attribute key
174      * @param[in] value The value asociated to key key
175      * @return true if the value is valid, false in other case
176      */
177     bool isValid(SumoXMLAttr key, const std::string& value);
178     /// @}
179 
180     /// @name Functions related with generic parameters
181     /// @{
182 
183     /// @brief return generic parameters in string format
184     std::string getGenericParametersStr() const;
185 
186     /// @brief return generic parameters as vector of pairs format
187     std::vector<std::pair<std::string, std::string> > getGenericParameters() const;
188 
189     /// @brief set generic parameters in string format
190     void setGenericParametersStr(const std::string& value);
191 
192     /// @}
193 
194     /**@brief return index of a vertex of shape, or of a new vertex if position is over an shape's edge
195      * @param pos position of new/existent vertex
196      * @param createIfNoExist enable or disable creation of new verte if there isn't another vertex in position
197      * @param snapToGrid enable or disable snapToActiveGrid
198      * @return index of position vector
199      */
200     int getVertexIndex(Position pos, bool createIfNoExist, bool snapToGrid);
201 
202     /// @brief delete the geometry point closest to the given pos
203     void deleteGeometryPoint(const Position& pos, bool allowUndo = true);
204 
205     /// @brief return true if polygon is blocked
206     bool isPolygonBlocked() const;
207 
208     /// @brief check if polygon is closed
209     bool isPolygonClosed() const;
210 
211     /// @brief retrieve the netElement of which the shape is being edited
212     void setShapeEditedElement(GNENetElement* element);
213 
214     /// @brief retrieve the junction of which the shape is being edited
215     GNENetElement* getShapeEditedElement() const;
216 
217     /// @brief open polygon
218     void openPolygon(bool allowUndo = true);
219 
220     /// @brief close polygon
221     void closePolygon(bool allowUndo = true);
222 
223     /// @brief change first geometry point
224     void changeFirstGeometryPoint(int oldIndex, bool allowUndo = true);
225 
226     /// @brief replace the current shape with a rectangle
227     void simplifyShape(bool allowUndo = true);
228 
229 protected:
230     /// @brief junction of which the shape is being edited (optional)
231     GNENetElement* myNetElementShapeEdited;
232 
233     /// @brief Latitude of Polygon
234     PositionVector myGeoShape;
235 
236     /// @brief flag for block shape
237     bool myBlockShape;
238 
239     /// @brief flag to indicate if polygon is open or closed
240     bool myClosedShape;
241 
242     /// @brief flag to indicate if polygon is simplified
243     bool mySimplifiedShape;
244 
245     /// @brief index of vertex that is been moved (-1 means that none vertex is been moved)
246     int myCurrentMovingVertexIndex;
247 
248 private:
249     /// @brief hint size of vertex
250     static const double myHintSize;
251 
252     /// @brief set attribute after validation
253     void setAttribute(SumoXMLAttr key, const std::string& value);
254 
255     /// @brief Invalidated copy constructor.
256     GNEPoly(const GNEPoly&) = delete;
257 
258     /// @brief Invalidated assignment operator.
259     GNEPoly& operator=(const GNEPoly&) = delete;
260 };
261 
262 
263 #endif
264 
265 /****************************************************************************/
266 
267