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-2018 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 /****************************************************************************/
15 //
16 /****************************************************************************/
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
23 #include <string>
24 #include <iostream>
25 #include <utility>
30 #include <utils/common/ToString.h>
31 #include <utils/geom/GeomHelper.h>
36 #include <utils/gui/div/GLHelper.h>
44 #include <netedit/GNEViewNet.h>
45 #include <netedit/GNEUndoList.h>
46 #include <netedit/GNENet.h>
47 #include <netedit/GNEViewParent.h>
48 
49 #include "GNERerouter.h"
50 #include "GNERerouterInterval.h"
51 
52 
53 // ===========================================================================
54 // member method definitions
55 // ===========================================================================
56 
57 GNERerouter::GNERerouter(const std::string& id, GNEViewNet* viewNet, const Position& pos, const std::vector<GNEEdge*>& edges, const std::string& name, const std::string& filename, double probability, bool off, double timeThreshold, const std::string& vTypes, bool blockMovement) :
58  GNEAdditional(id, viewNet, GLO_REROUTER, SUMO_TAG_REROUTER, name, blockMovement, edges),
59  myPosition(pos),
60  myFilename(filename),
61  myProbability(probability),
62  myOff(off),
63  myTimeThreshold(timeThreshold),
64  myVTypes(vTypes) {
65 }
66 
67 
69 }
70 
71 
72 void
73 GNERerouter::updateGeometry(bool updateGrid) {
74  // first check if object has to be removed from grid (SUMOTree)
75  if (updateGrid) {
77  }
78 
79  // Clear shape
80  myShape.clear();
81 
82  // Set block icon position
84 
85  // Set block icon offset
86  myBlockIconOffset = Position(-0.5, -0.5);
87 
88  // Set block icon rotation, and using their rotation for draw logo
90 
91  // Set position
92  myShape.push_back(myPosition);
93 
94  // update connection positions
96 
97  // last step is to check if object has to be added into grid (SUMOTree) again
98  if (updateGrid) {
100  }
101 }
102 
103 
104 Position
106  return myPosition;
107 }
108 
109 
110 void
112  // Open rerouter dialog
113  GNERerouterDialog(this);
114 }
115 
116 
117 void
118 GNERerouter::moveGeometry(const Position& oldPos, const Position& offset) {
119  // restore old position, apply offset and update Geometry
120  myPosition = oldPos;
121  myPosition.add(offset);
122  updateGeometry(false);
123 }
124 
125 
126 void
128  // commit new position allowing undo/redo
129  undoList->p_begin("position of " + toString(getTag()));
130  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(myPosition), true, toString(oldPos)));
131  undoList->p_end();
132 }
133 
134 
135 std::string
137  return myViewNet->getNet()->getMicrosimID();
138 }
139 
140 
141 void
143  // Start drawing adding an gl identificator
144  glPushName(getGlID());
145 
146  // Add a draw matrix for drawing logo
147  glPushMatrix();
148  glTranslated(myShape[0].x(), myShape[0].y(), getType());
149 
150 
151  // Draw icon depending of detector is selected and if isn't being drawn for selecting
152  if (s.drawForSelecting) {
154  GLHelper::drawBoxLine(Position(0, 1), 0, 2, 1);
155  } else {
156  glColor3d(1, 1, 1);
157  glRotated(180, 0, 0, 1);
160  } else {
162  }
163  }
164 
165  // Pop draw matrix
166  glPopMatrix();
167 
168  // Only lock and childs if isn't being drawn for selecting
169  if (!s.drawForSelecting) {
170 
171  // Show Lock icon depending of the Edit mode
172  drawLockIcon(0.4);
173 
174  // Draw symbols in every lane
175  const double exaggeration = s.addSize.getExaggeration(s);
176 
177  if (s.scale * exaggeration >= 3) {
178  // draw rerouter symbol over all lanes
179  for (auto i : mySymbolsPositionAndRotation) {
180  glPushMatrix();
181  glTranslated(i.first.x(), i.first.y(), getType());
182  glRotated(-1 * i.second, 0, 0, 1);
183  glScaled(exaggeration, exaggeration, 1);
184  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
185 
186  glBegin(GL_TRIANGLES);
187  glColor3d(1, .8f, 0);
188  // base
189  glVertex2d(0 - 1.4, 0);
190  glVertex2d(0 - 1.4, 6);
191  glVertex2d(0 + 1.4, 6);
192  glVertex2d(0 + 1.4, 0);
193  glVertex2d(0 - 1.4, 0);
194  glVertex2d(0 + 1.4, 6);
195  glEnd();
196 
197  // draw "U"
198  GLHelper::drawText("U", Position(0, 2), .1, 3, RGBColor::BLACK, 180);
199 
200  // draw Probability
201  GLHelper::drawText((toString((int)(myProbability * 100)) + "%").c_str(), Position(0, 4), .1, 0.7, RGBColor::BLACK, 180);
202 
203  glPopMatrix();
204  }
205  }
206 
207  // Draw connections
209  }
210  // check if dotted contour has to be drawn
211  if (!s.drawForSelecting && (myViewNet->getACUnderCursor() == this)) {
213  // draw shape dotte contour aroud alld connections between child and parents
214  for (auto i : myChildConnectionPositions) {
216  }
217  // draw rerouter symbol over all lanes
218  for (auto i : mySymbolsPositionAndRotation) {
219  GLHelper::drawShapeDottedContour(getType(), i.first, 2.8, 6, -1 * i.second, 0, 3);
220  }
221  }
222 
223  // Draw name
224  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
225 
226  // Pop name
227  glPopName();
228 }
229 
230 
231 std::string
233  switch (key) {
234  case SUMO_ATTR_ID:
235  return getAdditionalID();
236  case SUMO_ATTR_EDGES:
237  return parseIDs(myEdgeChilds);
238  case SUMO_ATTR_POSITION:
239  return toString(myPosition);
240  case SUMO_ATTR_NAME:
241  return myAdditionalName;
242  case SUMO_ATTR_FILE:
243  return myFilename;
244  case SUMO_ATTR_PROB:
245  return toString(myProbability);
247  return toString(myTimeThreshold);
248  case SUMO_ATTR_VTYPES:
249  return myVTypes;
250  case SUMO_ATTR_OFF:
251  return toString(myOff);
253  return toString(myBlockMovement);
254  case GNE_ATTR_SELECTED:
256  case GNE_ATTR_GENERIC:
257  return getGenericParametersStr();
258  default:
259  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
260  }
261 }
262 
263 
264 void
265 GNERerouter::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
266  if (value == getAttribute(key)) {
267  return; //avoid needless changes, later logic relies on the fact that attributes have changed
268  }
269  switch (key) {
270  case SUMO_ATTR_ID: {
271  // change ID of Rerouter Interval
272  undoList->p_add(new GNEChange_Attribute(this, key, value));
273  // Change Ids of all Rerouter interval childs
274  for (auto i : myAdditionalChilds) {
275  i->setAttribute(SUMO_ATTR_ID, generateAdditionalChildID(SUMO_TAG_INTERVAL), undoList);
276  }
277  break;
278  }
279  case SUMO_ATTR_EDGES:
280  case SUMO_ATTR_POSITION:
281  case SUMO_ATTR_NAME:
282  case SUMO_ATTR_FILE:
283  case SUMO_ATTR_PROB:
285  case SUMO_ATTR_VTYPES:
286  case SUMO_ATTR_OFF:
288  case GNE_ATTR_SELECTED:
289  case GNE_ATTR_GENERIC:
290  undoList->p_add(new GNEChange_Attribute(this, key, value));
291  break;
292  default:
293  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
294  }
295 }
296 
297 
298 bool
299 GNERerouter::isValid(SumoXMLAttr key, const std::string& value) {
300  switch (key) {
301  case SUMO_ATTR_ID:
302  return isValidAdditionalID(value);
303  case SUMO_ATTR_EDGES:
304  if (value.empty()) {
305  return false;
306  } else {
307  return canParse<std::vector<GNEEdge*> >(myViewNet->getNet(), value, false);
308  }
309  case SUMO_ATTR_POSITION:
310  return canParse<Position>(value);
311  case SUMO_ATTR_NAME:
313  case SUMO_ATTR_FILE:
315  case SUMO_ATTR_PROB:
316  return canParse<double>(value) && (parse<double>(value) >= 0) && (parse<double>(value) <= 1);
318  return canParse<double>(value) && (parse<double>(value) >= 0);
319  case SUMO_ATTR_VTYPES:
320  if (value.empty()) {
321  return true;
322  } else {
324  }
325  case SUMO_ATTR_OFF:
326  return canParse<bool>(value);
328  return canParse<bool>(value);
329  case GNE_ATTR_SELECTED:
330  return canParse<bool>(value);
331  case GNE_ATTR_GENERIC:
332  return isGenericParametersValid(value);
333  default:
334  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
335  }
336 }
337 
338 
339 std::string
341  return toString(getTag()) + ": " + getID();
342 }
343 
344 
345 std::string
347  return toString(getTag());
348 }
349 
350 // ===========================================================================
351 // private
352 // ===========================================================================
353 
354 void
355 GNERerouter::setAttribute(SumoXMLAttr key, const std::string& value) {
356  switch (key) {
357  case SUMO_ATTR_ID:
358  changeAdditionalID(value);
359  break;
360  case SUMO_ATTR_EDGES: {
361  // remove references of this rerouter in all edge childs
362  for (auto i : myEdgeChilds) {
363  i->removeAdditionalParent(this);
364  }
365  // set new edges
366  myEdgeChilds = parse<std::vector<GNEEdge*> >(myViewNet->getNet(), value);
367  // add references to this rerouter in all newedge childs
368  for (auto i : myEdgeChilds) {
369  i->addAdditionalParent(this);
370  }
371  break;
372  }
373  case SUMO_ATTR_POSITION:
374  myPosition = parse<Position>(value);
375  break;
376  case SUMO_ATTR_NAME:
377  myAdditionalName = value;
378  break;
379  case SUMO_ATTR_FILE:
380  myFilename = value;
381  break;
382  case SUMO_ATTR_PROB:
383  myProbability = parse<double>(value);
384  break;
386  myTimeThreshold = parse<double>(value);
387  break;
388  case SUMO_ATTR_VTYPES:
389  myVTypes = value;
390  break;
391  case SUMO_ATTR_OFF:
392  myOff = parse<bool>(value);
393  break;
395  myBlockMovement = parse<bool>(value);
396  break;
397  case GNE_ATTR_SELECTED:
398  if (parse<bool>(value)) {
400  } else {
402  }
403  break;
404  case GNE_ATTR_GENERIC:
406  break;
407  default:
408  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
409  }
410  // After setting attribute always update Geometry
411  updateGeometry(true);
412 }
413 
414 /****************************************************************************/
static void drawTexturedBox(int which, double size)
Draws a named texture as a box with the given size.
const GNEAttributeCarrier * getACUnderCursor() const
get AttributeCarrier under cursor
double scale
information about a lane&#39;s width (temporary, used for a single view)
void setBlockIconRotation(GNELane *additionalLane=nullptr)
void moveGeometry(const Position &oldPos, const Position &offset)
change the position of the element geometry without saving in undoList
~GNERerouter()
Destructor.
Definition: GNERerouter.cpp:68
static bool isValidAttribute(const std::string &value)
whether the given string is a valid attribute for a certain key (for example, a name) ...
static bool isValidListOfTypeID(const std::string &value)
whether the given string is a valid list of ids for an edge or vehicle type (empty aren&#39;t allowed) ...
GUIVisualizationTextSettings addName
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:126
void setGenericParametersStr(const std::string &value)
set generic parameters in string format
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.
std::string getAttribute(SumoXMLAttr key) const
Position getPositionInView() const
Returns position of additional in view.
static std::string parseIDs(const std::vector< T > &ACs)
parses a list of specific Attribute Carriers into a string of IDs
Position myPosition
position of rerouter in view
Definition: GNERerouter.h:129
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:611
std::string getPopUpID() const
get PopPup ID (Used in AC Hierarchy)
void unselectAttributeCarrier(bool changeFlag=true)
unselect attribute carrier using GUIGlobalSelection
bool myOff
attribute to enable or disable inactive initially
Definition: GNERerouter.h:138
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:78
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
GNERerouter(const std::string &id, GNEViewNet *viewNet, const Position &pos, const std::vector< GNEEdge *> &edges, const std::string &name, const std::string &filename, double probability, bool off, double timeThreshold, const std::string &vTypes, bool blockMovement)
Constructor.
Definition: GNERerouter.cpp:57
static const RGBColor BLACK
Definition: RGBColor.h:180
double myProbability
probability of rerouter
Definition: GNERerouter.h:135
bool isAttributeCarrierSelected() const
check if attribute carrier is selected
void changeAdditionalID(const std::string &newID)
change ID of additional
generic attribute
std::vector< PositionVector > myChildConnectionPositions
Matrix with the Vertex&#39;s positions of connections between parents an their childs.
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.
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 ...
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:49
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:573
PositionVector myShape
The shape of the additional element.
std::string myFilename
filename of rerouter
Definition: GNERerouter.h:132
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:85
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
std::string myAdditionalName
name of additional
static bool isGenericParametersValid(const std::string &value)
check if given string can be parsed to a map/list of generic parameters
void removeGLObjectFromGrid(GUIGlObject *o)
add GL Object into net
Definition: GNENet.cpp:1167
friend class GNEChange_Attribute
declare friend class
void selectAttributeCarrier(bool changeFlag=true)
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
a Rerouter
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.
bool myBlockMovement
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
std::vector< GNEAdditional * > myAdditionalChilds
vector with the Additional childs
std::string myVTypes
optional vehicle types for restricting the rerouter
Definition: GNERerouter.h:144
static const RGBColor RED
named colors
Definition: RGBColor.h:172
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
static void drawShapeDottedContour(const int type, const PositionVector &shape, const double width)
draw a dotted contour around the given Non closed shape with certain width
Definition: GLHelper.cpp:471
An Element which don&#39;t belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:53
element is selected
std::vector< std::pair< Position, double > > mySymbolsPositionAndRotation
position and rotation of every simbol over lane
void updateGeometry(bool updateGrid)
update pre-computed geometry information
Definition: GNERerouter.cpp:73
std::string getGenericParametersStr() const
return generic parameters in string format
std::string getParentName() const
Returns the name of the parent object (if any)
GNENet * getNet() const
get the net object
std::string generateAdditionalChildID(SumoXMLTag childTag)
gererate a new ID for an additional child
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
an aggreagated-output interval
double myTimeThreshold
attribute to configure activation time threshold
Definition: GNERerouter.h:141
bool isValidAdditionalID(const std::string &newID) const
check if a new additional ID is valid
static void drawBoxLine(const Position &beg, double rot, double visLength, double width, double offset=0)
Draws a thick line.
Definition: GLHelper.cpp:132
bool drawForSelecting
whether drawing is performed for the purpose of selecting objects
void addGLObjectIntoGrid(GUIGlObject *o)
add GL Object into net
Definition: GNENet.cpp:1160
static bool isValidFilename(const std::string &value)
whether the given string is a valid attribute for a filename (for example, a name) ...
std::string getHierarchyName() const
get Hierarchy Name (Used in AC Hierarchy)
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