SUMO - Simulation of Urban MObility
GNEDetectorE3.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>
48 
49 #include "GNEDetectorE3.h"
50 #include "GNEDetectorEntry.h"
51 #include "GNEDetectorExit.h"
52 #include "GNELane.h"
53 #include "GNEViewNet.h"
54 #include "GNEUndoList.h"
55 #include "GNENet.h"
56 #include "GNEChange_Attribute.h"
57 #include "GNEEdge.h"
58 
59 
60 // ===========================================================================
61 // member method definitions
62 // ===========================================================================
63 
64 GNEDetectorE3::GNEDetectorE3(const std::string& id, GNEViewNet* viewNet, Position pos, double freq, const std::string& filename, const double timeThreshold, double speedThreshold) :
65  GNEAdditional(id, viewNet, SUMO_TAG_E3DETECTOR, ICON_E3, true),
66  myPosition(pos),
67  myFreq(freq),
68  myFilename(filename),
69  myTimeThreshold(timeThreshold),
70  mySpeedThreshold(speedThreshold) {
71 }
72 
73 
75 
76 
77 void
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's geometry
96 
97  // Refresh element (neccesary to avoid grabbing problems)
99 }
100 
101 
102 Position
104  return myPosition;
105 }
106 
107 
108 void
109 GNEDetectorE3::moveGeometry(const Position& oldPos, const Position& offset) {
110  // restore old position, apply offset and update Geometry
111  myPosition = oldPos;
112  myPosition.add(offset);
113  updateGeometry();
114 }
115 
116 
117 void
119  undoList->p_begin("position of " + toString(getTag()));
120  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_X, toString(myPosition.x()), true, toString(oldPos.x())));
121  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_Y, toString(myPosition.y()), true, toString(oldPos.y())));
122  undoList->p_end();
123 }
124 
125 
126 void
128  // Only save E3 if have Entry/Exits
129  if (myAdditionalChilds.size() > 0) {
130  // Write parameters
131  device.openTag(getTag());
132  device.writeAttr(SUMO_ATTR_ID, getID());
134  if (!myFilename.empty()) {
136  }
139  device.writeAttr(SUMO_ATTR_X, myPosition.x());
140  device.writeAttr(SUMO_ATTR_Y, myPosition.y());
141 
142  // Write entrys and exits
143  for (auto i : myAdditionalChilds) {
144  i->writeAdditional(device);
145  }
146 
147  // Close E3 tag
148  device.closeTag();
149  } else {
150  WRITE_WARNING(toString(getTag()) + " with ID '" + getID() + "' cannot be writed in additional file because doesn't have childs.");
151  }
152 }
153 
154 
155 std::string
157  int counter = 0;
159  counter++;
160  }
161  return (getID() + toString(SUMO_TAG_DET_ENTRY) + toString(counter));
162 }
163 
164 
165 std::string
167  int counter = 0;
169  counter++;
170  }
171  return (getID() + toString(SUMO_TAG_DET_EXIT) + toString(counter));
172 }
173 
174 
175 const std::string&
177  return myViewNet->getNet()->getMicrosimID();
178 }
179 
180 
181 void
183  // Start drawing adding an gl identificator
184  glPushName(getGlID());
185 
186  // Add a draw matrix for drawing logo
187  glPushMatrix();
188  glTranslated(myShape[0].x(), myShape[0].y(), getType());
189  glColor3d(1, 1, 1);
190  glRotated(180, 0, 0, 1);
191  // Draw icon depending of detector is or isn't selected
192  if (isAdditionalSelected()) {
194  } else {
196  }
197 
198  // Pop logo matrix
199  glPopMatrix();
200 
201  // Show Lock icon depending of the Edit mode
202  drawLockIcon(0.4);
203 
204  // Draw connections
206 
207  // Pop name
208  glPopName();
209 
210  // Draw name
211  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
212 }
213 
214 
215 std::string
217  switch (key) {
218  case SUMO_ATTR_ID:
219  return getAdditionalID();
220  case SUMO_ATTR_X:
221  return toString(myPosition.x());
222  case SUMO_ATTR_Y:
223  return toString(myPosition.y());
224  case SUMO_ATTR_FREQUENCY:
225  return toString(myFreq);
226  case SUMO_ATTR_FILE:
227  return myFilename;
229  return toString(myTimeThreshold);
231  return toString(mySpeedThreshold);
233  return toString(myBlocked);
234  default:
235  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
236  }
237 }
238 
239 
240 void
241 GNEDetectorE3::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
242  if (value == getAttribute(key)) {
243  return; //avoid needless changes, later logic relies on the fact that attributes have changed
244  }
245  switch (key) {
246  case SUMO_ATTR_ID: {
247  // change ID of Entry
248  undoList->p_add(new GNEChange_Attribute(this, key, value));
249  // Change Ids of all Entry/Exits childs
250  for (auto i : myAdditionalChilds) {
251  if (i->getTag() == SUMO_TAG_ENTRY) {
252  i->setAttribute(SUMO_ATTR_ID, generateEntryID(), undoList);
253  } else {
254  i->setAttribute(SUMO_ATTR_ID, generateExitID(), undoList);
255  }
256  }
257  break;
258  }
259  case SUMO_ATTR_FREQUENCY:
260  case SUMO_ATTR_X:
261  case SUMO_ATTR_Y:
262  case SUMO_ATTR_FILE:
266  undoList->p_add(new GNEChange_Attribute(this, key, value));
267  break;
268  default:
269  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
270  }
271 }
272 
273 
274 bool
275 GNEDetectorE3::isValid(SumoXMLAttr key, const std::string& value) {
276  switch (key) {
277  case SUMO_ATTR_ID:
278  return isValidAdditionalID(value);
279  case SUMO_ATTR_X:
280  return canParse<double>(value);
281  case SUMO_ATTR_Y:
282  return canParse<double>(value);
283  case SUMO_ATTR_FREQUENCY:
284  return canParse<double>(value) && (parse<double>(value) >= 0);
285  case SUMO_ATTR_FILE:
286  return isValidFilename(value);
288  return canParse<double>(value) && (parse<double>(value) >= 0);
290  return canParse<double>(value) && (parse<double>(value) >= 0);
292  return canParse<bool>(value);
293  default:
294  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
295  }
296 }
297 
298 
299 void
300 GNEDetectorE3::setAttribute(SumoXMLAttr key, const std::string& value) {
301  switch (key) {
302  case SUMO_ATTR_ID:
303  changeAdditionalID(value);
304  break;
305  case SUMO_ATTR_X:
306  myPosition.setx(parse<double>(value));
307  break;
308  case SUMO_ATTR_Y:
309  myPosition.sety(parse<double>(value));
310  break;
311  case SUMO_ATTR_FREQUENCY:
312  myFreq = parse<double>(value);
313  break;
314  case SUMO_ATTR_FILE:
315  myFilename = value;
316  break;
318  myTimeThreshold = parse<double>(value);
319  break;
321  mySpeedThreshold = parse<double>(value);
322  break;
324  myBlocked = parse<bool>(value);
325  break;
326  default:
327  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
328  }
329  // After setting attribute always update Geometry
330  updateGeometry();
331 }
332 
333 /****************************************************************************/
static void drawTexturedBox(int which, double size)
Draws a named texture as a box with the given size.
double mySpeedThreshold
The speed-based threshold that describes how slow a vehicle has to be to be recognized as halting...
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
const std::string & getParentName() const
Returns the name of the parent object (if any)
double scale
information about a lane&#39;s width (temporary, used for a single view)
GNEDetectorE3(const std::string &id, GNEViewNet *viewNet, Position pos, double freq, const std::string &filename, const double timeThreshold, double speedThreshold)
GNEDetectorE3 Constructor.
void moveGeometry(const Position &oldPos, const Position &offset)
change the position of the element geometry without saving in undoList
GUIVisualizationTextSettings addName
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in all GNEAttributeCarriers.
Position myPosition
position of E3 in view
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
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1667
Position getPositionInView() const
Returns position of additional in view.
Stores the information about how to visualize structures.
double y() const
Returns the y-position.
Definition: Position.h:67
double myTimeThreshold
The time-based threshold that describes how much time has to pass until a vehicle is recognized as ha...
double x() const
Returns the x-position.
Definition: Position.h:62
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
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
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static bool isValidFilename(const std::string &value)
true if value is a valid file value
void changeAdditionalID(const std::string &newID)
change ID of additional
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
an e3 entry point
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
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 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
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
void updateGeometry()
update pre-computed geometry information
PositionVector myShape
The shape of the additional element.
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
void setx(double x)
set position x
Definition: Position.h:77
an e3 exit point
friend class GNEChange_Attribute
declare friend class
block movement of a graphic element
std::string generateEntryID()
gererate a new ID for an Entry detector child
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
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
Position myBlockIconOffset
The offSet of the block icon.
std::vector< GNEAdditional * > myAdditionalChilds
vector with the Additional childs
double myFreq
frequency of E3 detector
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
void commitGeometryMoving(const Position &oldPos, GNEUndoList *undoList)
commit geometry changes in the attributes of an element after use of moveGeometry(...)
An Element which don&#39;t belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:59
void setBlockIconRotation(GNELane *additionalLane=NULL)
void sety(double y)
set position y
Definition: Position.h:82
~GNEDetectorE3()
GNEDetectorE3 6Destructor.
GNENet * getNet() const
get the net object
GUIGlID getGlID() const
Returns the numerical id of the object.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
bool isValidAdditionalID(const std::string &newID) const
check if a new additional ID is valid
bool isAdditionalSelected() const
std::string generateExitID()
gererate a new ID for an Exit detector child
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
std::string myFilename
fielname of E3 detector
Position myBlockIconPosition
position of the block icon
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
SumoXMLTag getTag() const
get XML Tag assigned to this object