SUMO - Simulation of Urban MObility
GNERerouter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 //
18 /****************************************************************************/
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #ifdef _MSC_VER
24 #include <windows_config.h>
25 #else
26 #include <config.h>
27 #endif
28 
29 #include <string>
30 #include <iostream>
31 #include <utility>
36 #include <utils/common/ToString.h>
37 #include <utils/geom/GeomHelper.h>
43 #include <utils/gui/div/GLHelper.h>
47 
48 #include "GNEViewNet.h"
49 #include "GNERerouter.h"
50 #include "GNERerouterDialog.h"
51 #include "GNERerouterInterval.h"
52 #include "GNELane.h"
53 #include "GNEEdge.h"
54 #include "GNEViewNet.h"
55 #include "GNEUndoList.h"
56 #include "GNENet.h"
57 #include "GNEChange_Attribute.h"
58 
59 
60 // ===========================================================================
61 // member method definitions
62 // ===========================================================================
63 
64 GNERerouter::GNERerouter(const std::string& id, GNEViewNet* viewNet, Position pos, std::vector<GNEEdge*> edges, const std::string& filename, double probability, bool off, double timeThreshold) :
65  GNEAdditional(id, viewNet, SUMO_TAG_REROUTER, ICON_REROUTER, true, edges),
66  myPosition(pos),
67  myFilename(filename),
68  myProbability(probability),
69  myOff(off),
70  myTimeThreshold(timeThreshold) {
71 }
72 
73 
75 }
76 
77 
78 void
80  // Clear shape
81  myShape.clear();
82 
83  // Set block icon position
85 
86  // Set block icon offset
87  myBlockIconOffset = Position(-0.5, -0.5);
88 
89  // Set block icon rotation, and using their rotation for draw logo
91 
92  // Set position
93  myShape.push_back(myPosition);
94 
95  // update connection positions
97 
98  // Refresh element (neccesary to avoid grabbing problems)
100 }
101 
102 
103 Position
105  return myPosition;
106 }
107 
108 
109 void
111  // Open rerouter dialog
112  GNERerouterDialog(this);
113 }
114 
115 
116 void
117 GNERerouter::moveGeometry(const Position& oldPos, const Position& offset) {
118  // restore old position, apply offset and update Geometry
119  myPosition = oldPos;
120  myPosition.add(offset);
121  updateGeometry();
122 }
123 
124 
125 void
127  undoList->p_begin("position of " + toString(getTag()));
128  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(myPosition), true, toString(oldPos)));
129  undoList->p_end();
130 }
131 
132 
133 void
135  // Write parameters
136  device.openTag(getTag());
137  device.writeAttr(SUMO_ATTR_ID, getID());
140  if (!myFilename.empty()) {
142  }
143  if (myTimeThreshold > 0) {
145  }
146  device.writeAttr(SUMO_ATTR_OFF, myOff);
147  device.writeAttr(SUMO_ATTR_X, myPosition.x());
148  device.writeAttr(SUMO_ATTR_Y, myPosition.y());
149 
150  // write intervals and their values
151  for (auto i : myRerouterIntervals) {
152  i->writeRerouterInterval(device);
153  }
154 
155  // Close tag
156  device.closeTag();
157 }
158 
159 
160 void
162  auto it = std::find(myRerouterIntervals.begin(), myRerouterIntervals.end(), rerouterInterval);
163  if (it == myRerouterIntervals.end()) {
164  myRerouterIntervals.push_back(rerouterInterval);
165  // sort intervals always after a adding/restoring
166  sortIntervals();
167  } else {
168  throw ProcessError("Rerouter Interval already exist");
169  }
170 }
171 
172 
173 void
175  auto it = std::find(myRerouterIntervals.begin(), myRerouterIntervals.end(), rerouterInterval);
176  if (it != myRerouterIntervals.end()) {
177  myRerouterIntervals.erase(it);
178  // sort intervals always after a adding/restoring
179  sortIntervals();
180  } else {
181  throw ProcessError("Rerouter Interval doesn't exist");
182  }
183 }
184 
185 
186 const std::vector<GNERerouterInterval*>&
188  return myRerouterIntervals;
189 }
190 
191 
192 int
194  int numOverlappings = 0;
195  // iterate over intervals to save the number of overlappings
196  for (int i = 0; i < (int)(myRerouterIntervals.size() - 1); i++) {
197  if (myRerouterIntervals.at(i)->getEnd() > myRerouterIntervals.at(i + 1)->getBegin()) {
198  numOverlappings++;
199  } else if (myRerouterIntervals.at(i)->getEnd() > myRerouterIntervals.at(i + 1)->getEnd()) {
200  numOverlappings++;
201  }
202  }
203  // return number of overlappings found
204  return numOverlappings;
205 }
206 
207 
208 void
210  // declare a vector to keep sorted intervals
211  std::vector<GNERerouterInterval*> sortedIntervals;
212  // sort intervals usin begin as criterium
213  while (myRerouterIntervals.size() > 0) {
214  int begin_small = 0;
215  // find the interval with the small begin
216  for (int i = 0; i < (int)myRerouterIntervals.size(); i++) {
217  if (myRerouterIntervals.at(i)->getBegin() < myRerouterIntervals.at(begin_small)->getBegin()) {
218  begin_small = i;
219  }
220  }
221  // add it to sortd intervals and remove it from myRerouterIntervals
222  sortedIntervals.push_back(myRerouterIntervals.at(begin_small));
223  myRerouterIntervals.erase(myRerouterIntervals.begin() + begin_small);
224  }
225  // restore myRerouterIntervals using sorted intervals
226  myRerouterIntervals = sortedIntervals;
227 }
228 
229 
230 const std::string&
232  return myViewNet->getNet()->getMicrosimID();
233 }
234 
235 
236 void
238  // Start drawing adding an gl identificator
239  glPushName(getGlID());
240 
241  // Add a draw matrix for drawing logo
242  glPushMatrix();
243  glTranslated(myShape[0].x(), myShape[0].y(), getType());
244  glColor3d(1, 1, 1);
245  glRotated(180, 0, 0, 1);
246 
247  // Draw icon depending of rerouter is or isn't selected
248  if (isAdditionalSelected()) {
250  } else {
252  }
253 
254  // Pop draw matrix
255  glPopMatrix();
256 
257  // Show Lock icon depending of the Edit mode
258  drawLockIcon(0.4);
259 
260  // Draw symbols in every lane
261  const double exaggeration = s.addSize.getExaggeration(s);
262 
263  if (s.scale * exaggeration >= 3) {
264  // draw rerouter symbol over all lanes
265  for (auto i : mySymbolsPositionAndRotation) {
266  glPushMatrix();
267  glTranslated(i.first.x(), i.first.y(), getType());
268  glRotated(-1 * i.second, 0, 0, 1);
269  glScaled(exaggeration, exaggeration, 1);
270  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
271 
272  glBegin(GL_TRIANGLES);
273  glColor3d(1, .8f, 0);
274  // base
275  glVertex2d(0 - 1.4, 0);
276  glVertex2d(0 - 1.4, 6);
277  glVertex2d(0 + 1.4, 6);
278  glVertex2d(0 + 1.4, 0);
279  glVertex2d(0 - 1.4, 0);
280  glVertex2d(0 + 1.4, 6);
281  glEnd();
282 
283  // draw "U"
284  GLHelper::drawText("U", Position(0, 2), .1, 3, RGBColor::BLACK, 180);
285 
286  // draw Probability
287  GLHelper::drawText((toString((int)(myProbability * 100)) + "%").c_str(), Position(0, 4), .1, 0.7, RGBColor::BLACK, 180);
288 
289  glPopMatrix();
290  }
291  glPopName();
292  }
293 
294  // Draw connections
296 
297  // Pop name
298  glPopName();
299 
300  // Draw name
301  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
302 }
303 
304 
305 std::string
307  switch (key) {
308  case SUMO_ATTR_ID:
309  return getAdditionalID();
310  case SUMO_ATTR_EDGES:
311  return parseGNEEdges(myEdgeChilds);
312  case SUMO_ATTR_POSITION:
313  return toString(myPosition);
314  case SUMO_ATTR_FILE:
315  return myFilename;
316  case SUMO_ATTR_PROB:
317  return toString(myProbability);
319  return toString(myTimeThreshold);
320  case SUMO_ATTR_OFF:
321  return toString(myOff);
323  return toString(myBlocked);
324  default:
325  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
326  }
327 }
328 
329 
330 void
331 GNERerouter::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
332  if (value == getAttribute(key)) {
333  return; //avoid needless changes, later logic relies on the fact that attributes have changed
334  }
335  switch (key) {
336  case SUMO_ATTR_ID:
337  case SUMO_ATTR_EDGES:
338  case SUMO_ATTR_POSITION:
339  case SUMO_ATTR_FILE:
340  case SUMO_ATTR_PROB:
342  case SUMO_ATTR_OFF:
344  undoList->p_add(new GNEChange_Attribute(this, key, value));
345  break;
346  default:
347  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
348  }
349 }
350 
351 
352 bool
353 GNERerouter::isValid(SumoXMLAttr key, const std::string& value) {
354  switch (key) {
355  case SUMO_ATTR_ID:
356  return isValidAdditionalID(value);
357  case SUMO_ATTR_EDGES:
358  if (value.empty()) {
359  return false;
360  } else {
361  return checkGNEEdgesValid(myViewNet->getNet(), value, false);
362  }
363  case SUMO_ATTR_POSITION: {
364  bool ok;
365  return GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1;
366  }
367  case SUMO_ATTR_FILE:
368  return isValidFilename(value);
369  case SUMO_ATTR_PROB:
370  return canParse<double>(value) && (parse<double>(value) >= 0) && (parse<double>(value) <= 1);
372  return canParse<double>(value) && (parse<double>(value) >= 0);
373  case SUMO_ATTR_OFF:
374  return canParse<bool>(value);
376  return canParse<bool>(value);
377  default:
378  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
379  }
380 }
381 
382 
383 void
384 GNERerouter::setAttribute(SumoXMLAttr key, const std::string& value) {
385  switch (key) {
386  case SUMO_ATTR_ID:
387  changeAdditionalID(value);
388  break;
389  case SUMO_ATTR_EDGES: {
390  // remove references of this rerouter in all edge childs
391  for (auto i : myEdgeChilds) {
392  i->removeAdditionalParent(this);
393  }
394  // set new edges
395  myEdgeChilds = parseGNEEdges(myViewNet->getNet(), value);
396  // add references to this rerouter in all newedge childs
397  for (auto i : myEdgeChilds) {
398  i->addAdditionalParent(this);
399  }
400  break;
401  }
402  case SUMO_ATTR_POSITION:
403  bool ok;
404  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
405  break;
406  case SUMO_ATTR_FILE:
407  myFilename = value;
408  break;
409  case SUMO_ATTR_PROB:
410  myProbability = parse<double>(value);
411  break;
413  myTimeThreshold = parse<double>(value);
414  break;
415  case SUMO_ATTR_OFF:
416  myOff = parse<bool>(value);
417  break;
419  myBlocked = parse<bool>(value);
420  break;
421  default:
422  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
423  }
424  // After setting attribute always update Geometry
425  updateGeometry();
426 }
427 
428 /****************************************************************************/
static void drawTexturedBox(int which, double size)
Draws a named texture as a box with the given size.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
double scale
information about a lane&#39;s width (temporary, used for a single view)
void moveGeometry(const Position &oldPos, const Position &offset)
change the position of the element geometry without saving in undoList
void removeRerouterInterval(GNERerouterInterval *rerouterInterval)
add rerouter interval
~GNERerouter()
Destructor.
Definition: GNERerouter.cpp:74
GUIVisualizationTextSettings addName
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:132
void refreshElement(GUIGlObject *o)
refreshes boundary information for o and update
Definition: GNENet.cpp:1082
const std::string & getAdditionalID() const
returns Additional ID
static GUIGlID getTexture(GUITexture which)
returns a texture previously defined in the enum GUITexture
Stores the information about how to visualize structures.
double y() const
Returns the y-position.
Definition: Position.h:67
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in all GNEAttributeCarriers.
Position getPositionInView() const
Returns position of additional in view.
int getNumberOfOverlappedIntervals() const
get number of overlapped intervals
double x() const
Returns the x-position.
Definition: Position.h:62
Position myPosition
position of rerouter in view
Definition: GNERerouter.h:153
static void drawText(const std::string &text, const Position &pos, const double layer, const double size, const RGBColor &col=RGBColor::BLACK, const double angle=0, int align=0, double width=-1)
Definition: GLHelper.cpp:487
bool myOff
attribute to enable or disable inactive initially
Definition: GNERerouter.h:162
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:84
const std::string & getParentName() const
Returns the name of the parent object (if any)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static const RGBColor BLACK
Definition: RGBColor.h:186
double myProbability
probability of rerouter
Definition: GNERerouter.h:159
static bool isValidFilename(const std::string &value)
true if value is a valid file value
static std::vector< GNEEdge * > parseGNEEdges(GNENet *net, const std::string &value)
parse string into vector of GNEEdges
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
void changeAdditionalID(const std::string &newID)
change ID of additional
void commitGeometryMoving(const Position &oldPos, GNEUndoList *undoList)
commit geometry changes in the attributes of an element after use of moveGeometry(...)
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
void addRerouterInterval(GNERerouterInterval *rerouterInterval)
add rerouter interval
GNEViewNet * myViewNet
The GNEViewNet this additional element belongs.
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 ...
std::vector< GNERerouterInterval * > myRerouterIntervals
set with the GNERerouterInterval
Definition: GNERerouter.h:168
GUIVisualizationSizeSettings addSize
Dialog for edit rerouters.
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
void drawChildConnections() const
draw connections between Parent and childrens
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
void drawLockIcon(double size=0.5) const
draw lock icon
the edges of a route
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
PositionVector myShape
The shape of the additional element.
std::string myFilename
filename of rerouter
Definition: GNERerouter.h:156
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:91
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
friend class GNEChange_Attribute
declare friend class
GNERerouter(const std::string &id, GNEViewNet *viewNet, Position pos, std::vector< GNEEdge *> edges, const std::string &filename, double probability, bool off, double timeThreshold)
Constructor.
Definition: GNERerouter.cpp:64
block movement of a graphic element
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
void updateChildConnections()
update Connection&#39;s geometry
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
const std::string getID() const
function to support debugging
Position myBlockIconOffset
The offSet of the block icon.
void sortIntervals()
sort intervals
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
An Element which don&#39;t belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:59
const std::vector< GNERerouterInterval * > & getRerouterIntervals() const
get rerouter intervals
void setBlockIconRotation(GNELane *additionalLane=NULL)
std::vector< std::pair< Position, double > > mySymbolsPositionAndRotation
position and rotation of every simbol over lane
void updateGeometry()
update pre-computed geometry information
Definition: GNERerouter.cpp:79
GNENet * getNet() const
get the net object
GUIGlID getGlID() const
Returns the numerical id of the object.
double getExaggeration(const GUIVisualizationSettings &s, double factor=20) const
return the drawing size including exaggeration and constantSize values
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
double myTimeThreshold
attribute to configure activation time threshold
Definition: GNERerouter.h:165
bool isValidAdditionalID(const std::string &newID) const
check if a new additional ID is valid
bool isAdditionalSelected() const
static bool checkGNEEdgesValid(GNENet *net, const std::string &value, bool report)
check if a list of edge IDs is valid
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.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void openAdditionalDialog()
open GNERerouterDialog
Position myBlockIconPosition
position of the block icon
std::vector< GNEEdge * > myEdgeChilds
vector with the edge childs of this additional
SumoXMLTag getTag() const
get XML Tag assigned to this object