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