SUMO - Simulation of Urban MObility
GNEPoly.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // A class for visualizing and editing POIS in netedit (adapted from
8 // GUIPolygon and NLHandler)
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <utility>
36 #include <utils/geom/Position.h>
40 #include <utils/xml/XMLSubSys.h>
47 #include <utils/gui/div/GLHelper.h>
53 #include <netwrite/NWWriter_SUMO.h>
54 #include "GNENet.h"
55 #include "GNEEdge.h"
56 #include "GNEUndoList.h"
57 #include "GNEViewNet.h"
58 #include "GNEChange_Attribute.h"
59 #include "GNEPoly.h"
60 
61 
62 // ===========================================================================
63 // static members
64 // ===========================================================================
65 
66 // ===========================================================================
67 // method definitions
68 // ===========================================================================
69 GNEPoly::GNEPoly(GNENet* net, GNEJunction* junction, const std::string& id, const std::string& type, const PositionVector& shape, bool fill,
70  const RGBColor& color, double layer,
71  double angle, const std::string& imgFile) :
72  GUIPolygon(id, type, color, shape, fill, layer, angle, imgFile),
74  myNet(net),
75  myJunction(junction) {
76 }
77 
78 
80 
81 
82 void
84  const double hintSize = 0.8;
86  // draw geometry hints
87  if (s.scale * hintSize > 1.) { // check whether it is not too small
88  RGBColor current = GLHelper::getColor();
89  RGBColor darker = current.changedBrightness(-32);
90  GLHelper::setColor(darker);
91  glPushName(getGlID());
92  for (int i = 0; i < (int)myShape.size() - 1; i++) {
93  Position pos = myShape[i];
94  glPushMatrix();
95  glTranslated(pos.x(), pos.y(), GLO_POLYGON + 0.01);
96  GLHelper:: drawFilledCircle(hintSize, 32);
97  glPopMatrix();
98  }
99  glPopName();
100  }
101 
102 }
103 
104 
107  GUISUMOAbstractView& parent) {
109  new FXMenuSeparator(ret);
110  new FXMenuCommand(ret, "Set custom shape (ENTER)", 0, &app, MID_GNE_HOTKEY_ENTER);
111  new FXMenuCommand(ret, "Discard custom shape (ESC)", 0, &app, MID_GNE_ABORT);
112  new FXMenuCommand(ret, "Simplify Shape\t\tReplace shape with a rectangle", 0, &parent, MID_GNE_SIMPLIFY_SHAPE);
113  new FXMenuCommand(ret, "Remove geometry point\t\tRemove the closest geometry point", 0, &parent, MID_GNE_DELETE_GEOMETRY);
114  // let the GNEViewNet store the popup position
115  (dynamic_cast<GNEViewNet&>(parent)).markPopupPosition();
116  return ret;
117 }
118 
119 
120 Position
121 GNEPoly::moveGeometry(const Position& oldPos, const Position& newPos, bool relative) {
122  PositionVector geom = myShape;
123  bool changed = GNEEdge::changeGeometry(geom, getMicrosimID(), oldPos, newPos, relative, true);
124  if (changed) {
125  myShape = geom;
126  myNet->refreshElement(this);
127  return newPos;
128  } else {
129  return oldPos;
130  }
131 }
132 
133 
134 void
136  const Boundary b = myShape.getBoxBoundary();
137  myShape.clear();
138  myShape.push_back(Position(b.xmin(), b.ymin()));
139  myShape.push_back(Position(b.xmin(), b.ymax()));
140  myShape.push_back(Position(b.xmax(), b.ymax()));
141  myShape.push_back(Position(b.xmax(), b.ymin()));
142  myShape.push_back(myShape[0]);
143 }
144 
145 
146 void
148  if (myShape.size() <= 3) {
149  return;
150  }
151  int index = myShape.indexOfClosest(pos);
152  if ((index == 0 || index == (int)myShape.size() - 1) && myShape.front() == myShape.back()) {
153  myShape.erase(myShape.begin());
154  myShape.erase(myShape.end() - 1);
155  myShape.push_back(myShape.front());
156  } else {
157  myShape.erase(myShape.begin() + index);
158  }
159 }
160 
161 
162 std::string
164  switch (key) {
165  case SUMO_ATTR_ID:
166  return getMicrosimID();
167  break;
168  case SUMO_ATTR_TYPE:
169  return toString(Polygon::getType());
170  break;
171  default:
172  throw InvalidArgument("POI attribute '" + toString(key) + "' not allowed");
173  }
174 }
175 
176 
177 void
178 GNEPoly::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* /* undoList */) {
179  if (value == getAttribute(key)) {
180  return; //avoid needless changes, later logic relies on the fact that attributes have changed
181  }
182  //switch (key) {
183  //case SUMO_ATTR_ID:
184  //case SUMO_ATTR_POSITION:
185  //case GNE_ATTR_MODIFICATION_STATUS:
186  // undoList->add(new GNEChange_Attribute(this, key, value), true);
187  // break;
188  //case SUMO_ATTR_TYPE: {
189  // undoList->p_begin("change junction type");
190  // bool resetConnections = false;
191  // if (SUMOXMLDefinitions::NodeTypes.get(value) == NODETYPE_TRAFFIC_LIGHT) {
192  // // create new traffic light
193  // undoList->add(new GNEChange_TLS(this, 0, true), true);
194  // } else if (myNBNode.getType() == NODETYPE_TRAFFIC_LIGHT) {
195  // // delete old traffic light
196  // // make a copy because we will modify the original
197  // const std::set<NBTrafficLightDefinition*> tls = myNBNode.getControllingTLS();
198  // for (std::set<NBTrafficLightDefinition*>::iterator it=tls.begin(); it!=tls.end(); it++) {
199  // undoList->add(new GNEChange_TLS(this, *it, false), true);
200  // }
201  // }
202  // // must be the final step, otherwise we do not know which traffic lights to remove via GNEChange_TLS
203  // undoList->add(new GNEChange_Attribute(this, key, value), true);
204  // undoList->p_end();
205  // break;
206  //}
207  //default:
208  throw InvalidArgument("POI attribute '" + toString(key) + "' not allowed");
209  //}
210 }
211 
212 
213 bool
214 GNEPoly::isValid(SumoXMLAttr key, const std::string& /* value */) {
215  //switch (key) {
216  //case SUMO_ATTR_ID:
217  // return isValidID(value) && myNet->retrieveJunction(value, false) == 0;
218  // break;
219  //case SUMO_ATTR_TYPE:
220  // return SUMOXMLDefinitions::NodeTypes.hasString(value);
221  // break;
222  //case SUMO_ATTR_POSITION:
223  // bool ok;
224  // return GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1;
225  // break;
226  //default:
227  throw InvalidArgument("POI attribute '" + toString(key) + "' not allowed");
228  //}
229 }
230 
231 
232 // ===========================================================================
233 // private
234 // ===========================================================================
235 
236 void
237 GNEPoly::setAttribute(SumoXMLAttr key, const std::string& /* value */) {
238  //switch (key) {
239  //case SUMO_ATTR_ID:
240  // myNet->renameJunction(this, value);
241  // break;
242  //case SUMO_ATTR_TYPE: {
243  // myNBNode.reinit(myNBNode.getPosition(), SUMOXMLDefinitions::NodeTypes.get(value));
244  // break;
245  //}
246  //case SUMO_ATTR_POSITION:
247  // bool ok;
248  // myOrigPos = GeomConvHelper::parseShapeReporting(value, "netedit-given", 0, ok, false)[0];
249  // move(myOrigPos);
250  // break;
251  //default:
252  throw InvalidArgument("POI attribute '" + toString(key) + "' not allowed");
253  //}
254 }
255 
256 
257 // ===========================================================================
258 // GNEPolyHandler methods
259 // ===========================================================================
260 
261 
262 /****************************************************************************/
Position moveGeometry(const Position &oldPos, const Position &newPos, bool relative=false)
draw the polygon and also little movement handles
Definition: GNEPoly.cpp:121
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
double scale
information about a lane&#39;s width (temporary, used for a single view)
RGBColor changedBrightness(int change, int toChange=3) const
Returns a new color with altered brightness.
Definition: RGBColor.cpp:146
a polygon
void refreshElement(GUIGlObject *o)
refreshes boundary information for o and update
Definition: GNENet.cpp:817
int indexOfClosest(const Position &p) const
index of the closest position to p
abort current edit operation
Definition: GUIAppEnum.h:419
GNEPoly(GNENet *net, GNEJunction *junction, const std::string &id, const std::string &type, const PositionVector &shape, bool fill, const RGBColor &color, double layer, double angle=0, const std::string &imgFile="")
Constructor.
Definition: GNEPoly.cpp:69
Stores the information about how to visualize structures.
double y() const
Returns the y-position.
Definition: Position.h:68
double x() const
Returns the x-position.
Definition: Position.h:63
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:88
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static void drawFilledCircle(double width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:340
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
simplify polygon geometry
Definition: GUIAppEnum.h:634
GNENet * myNet
the net for querying updates
Definition: GNEPoly.h:184
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:439
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
Definition: GNEPoly.cpp:83
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
hot key <ENTER>
Definition: GUIAppEnum.h:425
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
bool isValid(SumoXMLAttr key, const std::string &value)
Definition: GNEPoly.cpp:214
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
Definition: GNEPoly.cpp:178
std::string getAttribute(SumoXMLAttr key) const
Definition: GNEPoly.cpp:163
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
Definition: GNEPoly.cpp:106
virtual void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
Definition: GUIPolygon.cpp:136
The popup menu of a globject.
void simplifyShape()
replace the current shape with a rectangle
Definition: GNEPoly.cpp:135
GUIGlID getGlID() const
Returns the numerical id of the object.
virtual ~GNEPoly()
Destructor.
Definition: GNEPoly.cpp:79
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
delete geometry point
Definition: GUIAppEnum.h:636
PositionVector myShape
The positions of the polygon.
Definition: Polygon.h:128
static bool changeGeometry(PositionVector &geom, const std::string &id, const Position &oldPos, const Position &newPos, bool relative=false, bool moveEndPoints=false)
Definition: GNEEdge.cpp:294
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
Definition: GUIPolygon.cpp:64
begin/end of the description of a polygon
void deleteGeometryNear(const Position &pos)
delete the geometry point closest to the given pos
Definition: GNEPoly.cpp:147
static RGBColor getColor()
gets the gl-color
Definition: GLHelper.cpp:445