SUMO - Simulation of Urban MObility
GNERerouter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <string>
31 #include <iostream>
32 #include <utility>
38 #include <utils/common/ToString.h>
39 #include <utils/geom/GeomHelper.h>
46 #include <utils/gui/div/GLHelper.h>
50 
51 #include "GNEViewNet.h"
52 #include "GNERerouter.h"
53 #include "GNERerouterDialog.h"
54 #include "GNERerouterInterval.h"
55 #include "GNELane.h"
56 #include "GNEEdge.h"
57 #include "GNEViewNet.h"
58 #include "GNEUndoList.h"
59 #include "GNENet.h"
60 #include "GNEChange_Attribute.h"
61 
62 #ifdef CHECK_MEMORY_LEAKS
63 #include <foreign/nvwa/debug_new.h>
64 #endif
65 
66 // ===========================================================================
67 // member method definitions
68 // ===========================================================================
69 
70 GNERerouter::GNERerouter(const std::string& id, GNEViewNet* viewNet, Position pos, std::vector<GNEEdge*> edges, const std::string& filename, SUMOReal probability, bool off) :
71  GNEAdditional(id, viewNet, pos, SUMO_TAG_REROUTER, ICON_REROUTER),
72  myEdges(edges),
73  myFilename(filename),
74  myProbability(probability),
75  myOff(off) {
76  // Update geometry;
78  // Set colors
79  myBaseColor = RGBColor(76, 170, 50, 255);
80  myBaseColorSelected = RGBColor(161, 255, 135, 255);
81 }
82 
83 
85 }
86 
87 
88 void
90  // Clear shape
91  myShape.clear();
92 
93  // Set block icon position
95 
96  // Set block icon offset
97  myBlockIconOffset = Position(-0.5, -0.5);
98 
99  // Set block icon rotation, and using their rotation for draw logo
101 
102  // Set position
103  myShape.push_back(myPosition);
104 
105  /*
106  // Add shape of childs (To avoid graphics errors)
107  for (childEdges::iterator i = myChildEdges.begin(); i != myChildEdges.end(); i++) {
108  myShape.append(i->edge->getLanes().at(0)->getShape());
109  }
110 
111  // Update geometry of additional parent
112  updateConnections();
113  */
114  // Refresh element (neccesary to avoid grabbing problems)
116 }
117 
118 
119 Position
121  return myPosition;
122 }
123 
124 
125 void
127  // Open rerouter dialog
128  GNERerouterDialog(this);
129 }
130 
131 
132 void
134  // change Position
135  myPosition = Position(offsetx, offsety);
136  updateGeometry();
137 }
138 
139 
140 void
142  undoList->p_begin("position of " + toString(getTag()));
143  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(myPosition), true, toString(Position(oldPosx, oldPosy))));
144  undoList->p_end();
145  // Refresh element
147 }
148 
149 
150 void
152  // Write parameters
153  device.openTag(getTag());
154  device.writeAttr(SUMO_ATTR_ID, getID());
155  // obtain ID's of Edges
156  std::vector<std::string> edgeIDs;
157  for (std::vector<GNEEdge*>::const_iterator i = myEdges.begin(); i != myEdges.end(); i++) {
158  edgeIDs.push_back((*i)->getID());
159  }
160  device.writeAttr(SUMO_ATTR_EDGES, joinToString(edgeIDs, " ").c_str());
162  if (!myFilename.empty()) {
164  }
165  device.writeAttr(SUMO_ATTR_X, myPosition.x());
166  device.writeAttr(SUMO_ATTR_Y, myPosition.y());
167  if (myBlocked) {
169  }
170  // write intervals
171  for (std::vector<GNERerouterInterval>::const_iterator i = myRerouterIntervals.begin(); i != myRerouterIntervals.end(); i++) {
172  device.openTag(i->getTag());
173  device.writeAttr(SUMO_ATTR_BEGIN, i->getBegin());
174  device.writeAttr(SUMO_ATTR_END, i->getEnd());
175 
176  // write closing lane reroutes
177  for (std::vector<GNEClosingLaneReroute>::const_iterator j = i->getClosingLaneReroutes().begin(); j != i->getClosingLaneReroutes().end(); j++) {
178  device.openTag(j->getTag());
179  device.writeAttr(SUMO_ATTR_LANE, j->getClosedLane()->getID());
180  device.writeAttr(SUMO_ATTR_ALLOW, getVehicleClassNames(j->getAllowedVehicles()));
181  device.writeAttr(SUMO_ATTR_DISALLOW, getVehicleClassNames(j->getDisallowedVehicles()));
182  device.closeTag();
183  }
184 
185  // write closing reroutes
186  for (std::vector<GNEClosingReroute>::const_iterator j = i->getClosingReroutes().begin(); j != i->getClosingReroutes().end(); j++) {
187  device.openTag(j->getTag());
188  device.writeAttr(SUMO_ATTR_EDGE, j->getClosedEdge()->getID());
189  device.writeAttr(SUMO_ATTR_ALLOW, getVehicleClassNames(j->getAllowedVehicles()));
190  device.writeAttr(SUMO_ATTR_DISALLOW, getVehicleClassNames(j->getDisallowedVehicles()));
191  device.closeTag();
192  }
193 
194  // write dest prob reroutes
195  for (std::vector<GNEDestProbReroute>::const_iterator j = i->getDestProbReroutes().begin(); j != i->getDestProbReroutes().end(); j++) {
196  device.openTag(j->getTag());
197  device.writeAttr(SUMO_ATTR_EDGE, j->getNewDestination()->getID());
198  device.writeAttr(SUMO_ATTR_PROB, j->getProbability());
199  device.closeTag();
200  }
201 
202  // write route prob reroutes
203  for (std::vector<GNERouteProbReroute>::const_iterator j = i->getRouteProbReroutes().begin(); j != i->getRouteProbReroutes().end(); j++) {
204  device.openTag(j->getTag());
205  device.writeAttr(SUMO_ATTR_ROUTE, j->getNewRouteId());
206  device.writeAttr(SUMO_ATTR_PROB, j->getProbability());
207  device.closeTag();
208  }
209 
210  // Close tag
211  device.closeTag();
212  }
213 
214  // Close tag
215  device.closeTag();
216 }
217 
218 
219 void
221  // Check that edge is valid and doesn't exist previously
222  if (edge == NULL) {
223  throw InvalidArgument("Trying to add an empty " + toString(SUMO_TAG_EDGE) + " child in " + toString(getTag()) + " with ID='" + getID() + "'");
224  } else if (std::find(myEdges.begin(), myEdges.end(), edge) != myEdges.end()) {
225  throw InvalidArgument("Trying to add a duplicate " + toString(SUMO_TAG_EDGE) + " child in " + toString(getTag()) + " with ID='" + getID() + "'");
226  } else {
227  myEdges.push_back(edge);
228  }
229 }
230 
231 
232 void
234  // Check that edge is valid and exist previously
235  if (edge == NULL) {
236  throw InvalidArgument("Trying to remove an empty " + toString(SUMO_TAG_EDGE) + " child in " + toString(getTag()) + " with ID='" + getID() + "'");
237  } else if (std::find(myEdges.begin(), myEdges.end(), edge) == myEdges.end()) {
238  throw InvalidArgument("Trying to remove a non previously inserted " + toString(SUMO_TAG_EDGE) + " child in " + toString(getTag()) + " with ID='" + getID() + "'");
239  } else {
240  myEdges.erase(std::find(myEdges.begin(), myEdges.end(), edge));
241  }
242 }
243 
244 
245 const std::vector<GNERerouterInterval>&
247  return myRerouterIntervals;
248 }
249 
250 
251 void
252 GNERerouter::setRerouterIntervals(const std::vector<GNERerouterInterval>& rerouterIntervals) {
253  myRerouterIntervals = rerouterIntervals;
254 }
255 
256 
257 const std::string&
259  return myViewNet->getNet()->getMicrosimID();
260 }
261 
262 
263 void
265  // Start drawing adding an gl identificator
266  glPushName(getGlID());
267 
268  // Add a draw matrix for drawing logo
269  glPushMatrix();
270  glTranslated(myShape[0].x(), myShape[0].y(), getType());
271  glColor3d(1, 1, 1);
272  glRotated(180, 0, 0, 1);
273 
274  // Draw icon depending of rerouter is or isn't selected
275  if (isAdditionalSelected()) {
277  } else {
279  }
280 
281  // Pop draw matrix
282  glPopMatrix();
283 
284  // Show Lock icon depending of the Edit mode
285  drawLockIcon(0.4);
286 
287  /*
288  // Draw symbols in every lane
289  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
290 
291  if (s.scale * exaggeration >= 3) {
292  // draw rerouter symbol over all lanes
293 
294  for (childEdges::const_iterator i = myChildEdges.begin(); i != myChildEdges.end(); i++) {
295  for (int lanePosIt = 0; lanePosIt < (int)i->positionsOverLanes.size(); lanePosIt++) {
296  glPushMatrix();
297  glTranslated(i->positionsOverLanes.at(lanePosIt).x(), i->positionsOverLanes.at(lanePosIt).y(), 0);
298  glRotated(i->rotationsOverLanes.at(lanePosIt), 0, 0, 1);
299  glTranslated(0, 0, getType());
300  glScaled(exaggeration, exaggeration, 1);
301  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
302 
303  glBegin(GL_TRIANGLES);
304  glColor3d(1, .8f, 0);
305  // base
306  glVertex2d(0 - 1.4, 0);
307  glVertex2d(0 - 1.4, 6);
308  glVertex2d(0 + 1.4, 6);
309  glVertex2d(0 + 1.4, 0);
310  glVertex2d(0 - 1.4, 0);
311  glVertex2d(0 + 1.4, 6);
312  glEnd();
313 
314  glTranslated(0, 0, .1);
315  glColor3d(0, 0, 0);
316  pfSetPosition(0, 0);
317  pfSetScale(3.f);
318  SUMOReal w = pfdkGetStringWidth("U");
319  glRotated(180, 0, 1, 0);
320  glTranslated(-w / 2., 2, 0);
321  pfDrawString("U");
322 
323  glTranslated(w / 2., -2, 0);
324  std::string str = toString((int)(myProbability * 100)) + "%";
325  pfSetPosition(0, 0);
326  pfSetScale(.7f);
327  w = pfdkGetStringWidth(str.c_str());
328  glTranslated(-w / 2., 4, 0);
329  pfDrawString(str.c_str());
330  glPopMatrix();
331  }
332  }
333  glPopName();
334  }
335 
336  // Draw connections
337  drawConnections();
338  */
339  // Pop name
340  glPopName();
341 
342  // Draw name
343  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
344 }
345 
346 
347 std::string
349  switch (key) {
350  case SUMO_ATTR_ID:
351  return getAdditionalID();
352  case SUMO_ATTR_EDGES: {
353  // obtain ID's of Edges
354  std::vector<std::string> edgeIDs;
355  for (std::vector<GNEEdge*>::const_iterator i = myEdges.begin(); i != myEdges.end(); i++) {
356  edgeIDs.push_back((*i)->getID());
357  }
358  return joinToString(edgeIDs, " ");
359  }
360  case SUMO_ATTR_POSITION:
361  return toString(myPosition);
362  case SUMO_ATTR_FILE:
363  return myFilename;
364  case SUMO_ATTR_PROB:
365  return toString(myProbability);
366  case SUMO_ATTR_OFF:
367  return toString(myOff);
369  return toString(myBlocked);
370  default:
371  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
372  }
373 }
374 
375 
376 void
377 GNERerouter::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
378  if (value == getAttribute(key)) {
379  return; //avoid needless changes, later logic relies on the fact that attributes have changed
380  }
381  switch (key) {
382  case SUMO_ATTR_ID:
383  case SUMO_ATTR_EDGES:
384  case SUMO_ATTR_POSITION:
385  case SUMO_ATTR_FILE:
386  case SUMO_ATTR_PROB:
387  case SUMO_ATTR_OFF:
389  undoList->p_add(new GNEChange_Attribute(this, key, value));
390  updateGeometry();
391  break;
392  default:
393  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
394  }
395 }
396 
397 
398 bool
399 GNERerouter::isValid(SumoXMLAttr key, const std::string& value) {
400  switch (key) {
401  case SUMO_ATTR_ID:
402  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
403  return true;
404  } else {
405  return false;
406  }
407  case SUMO_ATTR_EDGES: {
408  std::vector<std::string> edgeIds;
409  SUMOSAXAttributes::parseStringVector(value, edgeIds);
410  // Empty Edges aren't valid
411  if (edgeIds.empty()) {
412  return false;
413  }
414  // Iterate over parsed edges and check that exists
415  for (int i = 0; i < (int)edgeIds.size(); i++) {
416  if (myViewNet->getNet()->retrieveEdge(edgeIds.at(i), false) == NULL) {
417  return false;
418  }
419  }
420  // all edges exist, then is valid
421  return true;
422  }
423  case SUMO_ATTR_POSITION:
424  bool ok;
425  return GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1;
426  case SUMO_ATTR_FILE:
427  return isValidFileValue(value);
428  case SUMO_ATTR_PROB:
429  return canParse<SUMOReal>(value);
430  case SUMO_ATTR_OFF:
431  return canParse<bool>(value);
433  return canParse<bool>(value);
434  default:
435  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
436  }
437 }
438 
439 
440 void
441 GNERerouter::setAttribute(SumoXMLAttr key, const std::string& value) {
442  switch (key) {
443  case SUMO_ATTR_ID:
444  setAdditionalID(value);
445  break;
446  case SUMO_ATTR_EDGES: {
447  // Declare variables
448  std::vector<std::string> edgeIds;
449  GNEEdge* edge;
450  // clear previous edges
451  myEdges.clear();
452  SUMOSAXAttributes::parseStringVector(value, edgeIds);
453  // Iterate over parsed edges and obtain pointer to edges
454  for (int i = 0; i < (int)edgeIds.size(); i++) {
455  edge = myViewNet->getNet()->retrieveEdge(edgeIds.at(i), false);
456  if (edge) {
457  myEdges.push_back(edge);
458  } else {
459  throw InvalidArgument("Trying to set an non-valid edge in " + getID());
460  }
461  }
462  break;
463  }
464  case SUMO_ATTR_POSITION:
465  bool ok;
466  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
467  updateGeometry();
468  getViewNet()->update();
469  break;
470  case SUMO_ATTR_FILE:
471  myFilename = value;
472  break;
473  case SUMO_ATTR_PROB:
474  myProbability = parse<SUMOReal>(value);
475  break;
476  case SUMO_ATTR_OFF:
477  myOff = parse<bool>(value);
478  break;
480  myBlocked = parse<bool>(value);
481  getViewNet()->update();
482  break;
483  default:
484  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
485  }
486 }
487 
488 /****************************************************************************/
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
GNEEdge * retrieveEdge(const std::string &id, bool failHard=true)
get edge by id
Definition: GNENet.cpp:732
~GNERerouter()
Destructor.
Definition: GNERerouter.cpp:84
GUIVisualizationTextSettings addName
static GUIGlID getTexture(GUITexture which)
returns a texture previously defined in the enum GUITexture
Stores the information about how to visualize structures.
bool isAdditionalSelected() const
weights: time range begin
const std::string & getParentName() const
Returns the name of the parent object (if any)
void removeEdgeChild(GNEEdge *edge)
remove edge child
bool myOff
attribute to enable or disable inactive initially
Definition: GNERerouter.h:147
void p_begin(const std::string &description)
Begin undo command sub-group. This begins a new group of commands that are treated as a single comman...
Definition: GNEUndoList.cpp:86
std::string getAttribute(SumoXMLAttr key) const
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
void moveAdditionalGeometry(SUMOReal offsetx, SUMOReal offsety)
change the position of the rerouter geometry
GNERerouter(const std::string &id, GNEViewNet *viewNet, Position pos, std::vector< GNEEdge * > edges, const std::string &filename, SUMOReal probability, bool off)
Constructor.
Definition: GNERerouter.cpp:70
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
GUIGlID getGlID() const
Returns the numerical id of the object.
SUMOReal scale
information about a lane&#39;s width (temporary, used for a single view)
GNEViewNet * myViewNet
The GNEViewNet this additional element belongs.
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
RGBColor myBaseColorSelected
base color selected (Default blue)
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
Dialog for edit rerouters.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
static bool isValidFileValue(const std::string &value)
true if value is a valid file value
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
the edges of a route
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
void setRerouterIntervals(const std::vector< GNERerouterInterval > &rerouterIntervals)
set rerouter intervals
const std::string & getAdditionalID() const
returns the ID of additional
void refreshAdditional(GNEAdditional *additional)
refreshes boundary information of an additional after a geometry update
Definition: GNENet.cpp:814
PositionVector myShape
The shape of the additional element.
std::string myFilename
filename of rerouter
Definition: GNERerouter.h:141
void p_end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise, the sub-group will be added as a new command into parent group. A matching begin() must have been called previously.
Definition: GNEUndoList.cpp:93
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
const std::vector< GNERerouterInterval > & getRerouterIntervals() const
get rerouter intervals
void setBlockIconRotation(GNELane *lane=NULL)
set Rotation of block Icon
friend class GNEChange_Attribute
declare friend class
block movement of a graphic element
void drawLockIcon(SUMOReal size=0.5) const
draw lock icon
void setAdditionalID(const std::string &id)
set the ID of additional
const std::string getID() const
function to support debugging
void drawName(const Position &pos, const SUMOReal scale, const GUIVisualizationTextSettings &settings, const SUMOReal angle=0) const
draw name of item
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
std::string getVehicleClassNames(SVCPermissions permissions)
Returns the ids of the given classes, divided using a &#39; &#39;.
Position myBlockIconOffset
The offSet of the block icon.
begin/end of the description of an edge
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:54
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1265
std::vector< GNEEdge * > myEdges
edges of Rerouter
Definition: GNERerouter.h:138
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
void commmitAdditionalGeometryMoved(SUMOReal oldPosx, SUMOReal oldPosy, GNEUndoList *undoList)
updated geometry changes in the attributes of additional
An Element which don&#39;t belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:62
weights: time range end
void addEdgeChild(GNEEdge *edge)
add edge child
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
RGBColor myBaseColor
base color (Default green)
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:989
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
void updateGeometry()
update pre-computed geometry information
Definition: GNERerouter.cpp:89
Position getPositionInView() const
Returns position of Rerouter in view.
SumoXMLTag getTag() const
get XML Tag assigned to this object
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
Position myPosition
The position in which this additional element is located.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:208
SUMOReal myProbability
probability of rerouter
Definition: GNERerouter.h:144
static void drawTexturedBox(int which, SUMOReal size)
Draws a named texture as a box with the given size.
static PositionVector parseShapeReporting(const std::string &shpdef, const std::string &objecttype, const char *objectid, bool &ok, bool allowEmpty, bool report=true)
Builds a PositionVector from a string representation, reporting occured errors.
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:188
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
std::vector< GNERerouterInterval > myRerouterIntervals
set with the GNERerouterInterval
Definition: GNERerouter.h:150
void openAdditionalDialog()
open GNERerouterDialog
Position myBlockIconPosition
position of the block icon