SUMO - Simulation of Urban MObility
GNECalibrator.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2013 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>
37 #include <utils/common/ToString.h>
38 #include <utils/geom/GeomHelper.h>
45 #include <utils/gui/div/GLHelper.h>
49 
50 #include "GNECalibrator.h"
51 #include "GNEEdge.h"
52 #include "GNELane.h"
53 #include "GNEViewNet.h"
54 #include "GNEUndoList.h"
55 #include "GNENet.h"
56 #include "GNEChange_Attribute.h"
57 #include "GNERouteProbe.h"
58 #include "GNECalibratorDialog.h"
59 
60 #ifdef CHECK_MEMORY_LEAKS
61 #include <foreign/nvwa/debug_new.h>
62 #endif
63 
64 
65 // ===========================================================================
66 // member method definitions
67 // ===========================================================================
68 
69 GNECalibrator::GNECalibrator(const std::string& id, GNEEdge* edge, GNEViewNet* viewNet, SUMOReal pos, SUMOTime frequency, const std::string& output, const std::map<std::string, CalibratorFlow>& flowValues, bool blocked) :
70  GNEAdditional(id, viewNet, Position(pos, 0), SUMO_TAG_CALIBRATOR, NULL, blocked),
71  myEdge(edge),
72  myFrequency(frequency),
73  myOutput(output),
74  myRouteProbe(NULL),
75  myFlowValues(flowValues) {
76  // this additional ISN'T movable
77  myMovable = false;
78  // Update geometry;
80  // Set Colors
81  myBaseColor = RGBColor(255, 255, 50, 0);
82  myBaseColorSelected = RGBColor(255, 255, 125, 255);
83 }
84 
85 
87 }
88 
89 
90 void
92  // This additional cannot be moved
93 }
94 
95 
96 void
98  // Clear all containers
99  myShapeRotations.clear();
100  myShapeLengths.clear();
101 
102  // clear Shape
103  myShape.clear();
104 
105  // Iterate over lanes
106  for (int i = 0; i < (int)myEdge->getLanes().size(); i++) {
107 
108  // Get shape of lane parent
109  myShape.push_back(myEdge->getLanes().at(i)->getShape().positionAtOffset(myEdge->getLanes().at(i)->getPositionRelativeToParametricLenght(myPosition.x())));
110 
111  // Obtain first position
112  Position f = myShape[i] - Position(1, 0);
113 
114  // Obtain next position
115  Position s = myShape[i] + Position(1, 0);
116 
117  // Save rotation (angle) of the vector constructed by points f and s
118  myShapeRotations.push_back(myEdge->getLanes().at(i)->getShape().rotationDegreeAtOffset(myEdge->getLanes().at(i)->getPositionRelativeToParametricLenght(myPosition.x())) * -1);
119  }
120 }
121 
122 
123 Position
125  return myPosition;
126 }
127 
128 void
130  // Open calibrator dialog
131  GNECalibratorDialog calibratorDialog(this);
132 }
133 
134 
135 void
136 GNECalibrator::writeAdditional(OutputDevice& device, const std::string&) {
137  // Write parameters
138  device.openTag(getTag());
139  device.writeAttr(SUMO_ATTR_ID, getID());
140  device.writeAttr(SUMO_ATTR_LANE, myEdge->getLanes().at(0)->getID());
144  // Write all flows of this calibrator
145  for (std::map<std::string, CalibratorFlow>::iterator i = myFlowValues.begin(); i != myFlowValues.end(); ++i) {
146  // Open flow tag
147  device.openTag(SUMO_TAG_FLOW);
148  // Write ID
149  device.writeAttr(SUMO_ATTR_ID, i->first);
150  // Write begin
151  device.writeAttr(SUMO_ATTR_BEGIN, i->second.begin);
152  // Write nd
153  device.writeAttr(SUMO_ATTR_END, i->second.end);
154  // Write type
155  device.writeAttr(SUMO_ATTR_TYPE, i->second.type);
156  // Write route
157  device.writeAttr(SUMO_ATTR_ROUTE, i->second.route);
158  // Write color
159  device.writeAttr(SUMO_ATTR_COLOR, i->second.color);
160  // Write depart lane
161  device.writeAttr(SUMO_ATTR_DEPARTLANE, i->second.departLane);
162  // Write depart pos
163  device.writeAttr(SUMO_ATTR_DEPARTPOS, i->second.departPos);
164  // Write depart speed
165  device.writeAttr(SUMO_ATTR_DEPARTSPEED, i->second.departSpeed);
166  // Write arrival lane
167  device.writeAttr(SUMO_ATTR_ARRIVALLANE, i->second.arrivalLane);
168  // Write arrival pos
169  device.writeAttr(SUMO_ATTR_ARRIVALPOS, i->second.arrivalPos);
170  // Write arrival speed
171  device.writeAttr(SUMO_ATTR_ARRIVALSPEED, i->second.arrivalSpeed);
172  // Write line
173  device.writeAttr(SUMO_ATTR_LINE, i->second.line);
174  // Write person number
175  device.writeAttr(SUMO_ATTR_PERSON_NUMBER, i->second.personNumber);
176  // Write container number
177  device.writeAttr(SUMO_ATTR_CONTAINER_NUMBER, i->second.containerNumber);
178  // Write vehsPerHour
179  device.writeAttr(SUMO_ATTR_VEHSPERHOUR, i->second.vehsPerHour);
180  // Write period
181  device.writeAttr(SUMO_ATTR_PERIOD, i->second.period);
182  // Write probability
183  device.writeAttr(SUMO_ATTR_PROB, i->second.probability);
184  // Write number
185  device.writeAttr(SUMO_ATTR_NUMBER, i->second.number);
186  // Close flow tag
187  device.closeTag();
188  }
189  // Close tag
190  device.closeTag();
191 }
192 
193 
194 std::map<std::string, GNECalibrator::CalibratorFlow>
196  return myFlowValues;
197 }
198 
199 
200 void
201 GNECalibrator::setFlowValues(std::map<std::string, GNECalibrator::CalibratorFlow> calibratorFlowValues) {
202  myFlowValues = calibratorFlowValues;
203 }
204 
205 
206 void
207 GNECalibrator::insertFlow(const std::string& id, const CalibratorFlow& flow) {
208  if (myFlowValues.find(id) == myFlowValues.end()) {
209  myFlowValues[id] = flow;
210  } else {
211  throw InvalidArgument("Calibrators don't allow Flows with duplicate Id's (" + id + ")");
212  }
213 
214 }
215 
216 
217 void
218 GNECalibrator::removeFlow(const std::string& id) {
219  if (myFlowValues.find(id) != myFlowValues.end()) {
220  myFlowValues.erase(id);
221  } else {
222  throw InvalidArgument("Calibrator with Id''" + id + "' not exists");
223  }
224 
225 }
226 
227 
228 const std::string&
230  return myEdge->getMicrosimID();
231 }
232 
233 
234 void
236  // get values
237  glPushName(getGlID());
238  glLineWidth(1.0);
239  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
240 
241  glPushName(getGlID());
242  for (int i = 0; i < (int)myShape.size(); ++i) {
243  const Position& pos = myShape[i];
244  SUMOReal rot = myShapeRotations[i];
245  glPushMatrix();
246  glTranslated(pos.x(), pos.y(), getType());
247  glRotated(rot, 0, 0, 1);
248  glTranslated(0, 0, getType());
249  glScaled(exaggeration, exaggeration, 1);
250  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
251 
252  glBegin(GL_TRIANGLES);
253  glColor3d(1, .8f, 0);
254  // base
255  glVertex2d(0 - 1.4, 0);
256  glVertex2d(0 - 1.4, 6);
257  glVertex2d(0 + 1.4, 6);
258  glVertex2d(0 + 1.4, 0);
259  glVertex2d(0 - 1.4, 0);
260  glVertex2d(0 + 1.4, 6);
261  glEnd();
262 
263  // draw text
264  if (s.scale * exaggeration >= 1.) {
265  glTranslated(0, 0, .1);
266  glColor3d(0, 0, 0);
267  pfSetPosition(0, 0);
268  pfSetScale(3.f);
269  SUMOReal w = pfdkGetStringWidth("C");
270  glRotated(180, 0, 1, 0);
271  glTranslated(-w / 2., 2, 0);
272  pfDrawString("C");
273  glTranslated(w / 2., -2, 0);
274  }
275  glPopMatrix();
276  }
277  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
278  glPopName();
279 }
280 
281 
282 std::string
284  switch (key) {
285  case SUMO_ATTR_ID:
286  return getAdditionalID();
287  case SUMO_ATTR_LANE:
288  return toString(myEdge->getLanes().at(0)->getAttribute(SUMO_ATTR_ID));
289  case SUMO_ATTR_POSITION:
290  return toString(myPosition.x());
291  case SUMO_ATTR_FREQUENCY:
292  return time2string(myFrequency);
293  case SUMO_ATTR_OUTPUT:
294  return myOutput;
296  if (myRouteProbe) {
297  return myRouteProbe->getID();
298  } else {
299  return "";
300  }
301  default:
302  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
303  }
304 }
305 
306 
307 void
308 GNECalibrator::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
309  if (value == getAttribute(key)) {
310  return; //avoid needless changes, later logic relies on the fact that attributes have changed
311  }
312  switch (key) {
313  case SUMO_ATTR_ID:
314  case SUMO_ATTR_LANE:
315  case SUMO_ATTR_POSITION:
316  case SUMO_ATTR_FREQUENCY:
317  case SUMO_ATTR_OUTPUT:
319  undoList->p_add(new GNEChange_Attribute(this, key, value));
320  updateGeometry();
321  break;
322  default:
323  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
324  }
325 
326 }
327 
328 
329 bool
330 GNECalibrator::isValid(SumoXMLAttr key, const std::string& value) {
331  switch (key) {
332  case SUMO_ATTR_ID:
333  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
334  return true;
335  } else {
336  return false;
337  }
338  case SUMO_ATTR_LANE:
339  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
340  return true;
341  } else {
342  return false;
343  }
344  case SUMO_ATTR_POSITION:
345  case SUMO_ATTR_FREQUENCY:
346  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
347  case SUMO_ATTR_OUTPUT:
348  return isValidFileValue(value);
350  if (myViewNet->getNet()->getAdditional(SUMO_TAG_ROUTEPROBE, value) != NULL) {
351  return true;
352  } else {
353  return false;
354  }
355  default:
356  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
357  }
358 }
359 
360 // ===========================================================================
361 // private
362 // ===========================================================================
363 
364 void
365 GNECalibrator::setAttribute(SumoXMLAttr key, const std::string& value) {
366  switch (key) {
367  case SUMO_ATTR_ID:
368  setAdditionalID(value);
369  break;
370  case SUMO_ATTR_LANE:
371  myEdge->removeAdditional(this);
372  myEdge = &(myViewNet->getNet()->retrieveLane(value)->getParentEdge());
373  myEdge->addAdditional(this);
374  updateGeometry();
375  getViewNet()->update();
376  break;
377  case SUMO_ATTR_POSITION:
378  myPosition = Position(parse<SUMOReal>(value), 0);
379  updateGeometry();
380  getViewNet()->update();
381  break;
382  case SUMO_ATTR_FREQUENCY:
383  myFrequency = string2time(value);
384  break;
385  case SUMO_ATTR_OUTPUT:
386  myOutput = value;
387  break;
390  break;
391  default:
392  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
393  }
394 }
395 
396 /****************************************************************************/
SUMOReal getExaggeration(const GUIVisualizationSettings &s, SUMOReal factor=20) const
return the drawing size including exaggeration and constantSize values
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:257
int pfDrawString(const char *c)
Definition: polyfonts.c:1074
long long int SUMOTime
Definition: SUMOTime.h:43
void writeAdditional(OutputDevice &device, const std::string &)
writte additional element into a xml file
GUIVisualizationTextSettings addName
Stores the information about how to visualize structures.
void insertFlow(const std::string &id, const CalibratorFlow &flow)
insert a new flow
~GNECalibrator()
Destructor.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
SUMOTime myFrequency
Frequency of calibrator.
void pfSetPosition(SUMOReal x, SUMOReal y)
Definition: polyfonts.c:480
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
GNERouteProbe * myRouteProbe
pointer to RouteProbe
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
bool addAdditional(GNEAdditional *additional)
add additional to this edge
Definition: GNEEdge.cpp:822
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
void removeFlow(const std::string &id)
remove a existent flow
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
GUIGlID getGlID() const
Returns the numerical id of the object.
void openAdditionalDialog()
open Calibrator Dialog
SUMOReal scale
information about a lane&#39;s width (temporary, used for a single view)
bool removeAdditional(GNEAdditional *additional)
remove additional from this edge
Definition: GNEEdge.cpp:836
void setFlowValues(std::map< std::string, CalibratorFlow > calibratorFlowValues)
set Calbratorflow values
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
GUIVisualizationSizeSettings addSize
std::map< std::string, CalibratorFlow > getFlowValues() const
get Calbratorflow values
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
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.
std::string myOutput
output of calibrator
std::vector< SUMOReal > myShapeLengths
The lengths of the shape parts.
bool myMovable
boolean to check if additional element is movable (with the mouse). By default true ...
GNEEdge & getParentEdge()
Returns underlying parent edge.
Definition: GNELane.cpp:828
const std::string & getAdditionalID() const
returns the ID of additional
bool isValid(SumoXMLAttr key, const std::string &value)
std::string getAttribute(SumoXMLAttr key) const
PositionVector myShape
The shape of the additional element.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
GNELane * retrieveLane(const std::string &id, bool failHard=true)
get lane by id
Definition: GNENet.cpp:658
friend class GNEChange_Attribute
declare friend class
void moveAdditional(SUMOReal, SUMOReal, GNEUndoList *)
change the position of the calibrator geometry
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
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
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:54
Dialog for edit calibrators.
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1093
const std::vector< GNELane * > & getLanes()
returns a reference to the lane vector
Definition: GNEEdge.cpp:424
void pfSetScale(SUMOReal s)
Definition: polyfonts.c:465
An Element which don&#39;t belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:63
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
RGBColor myBaseColor
base color (Default green)
std::vector< SUMOReal > myShapeRotations
GNEEdge * myEdge
edge in which this calibrator is placed
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:845
SUMOReal pfdkGetStringWidth(const char *c)
Definition: polyfonts.c:1113
SumoXMLTag getTag() const
get 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:214
void updateGeometry()
update pre-computed geometry information
const std::string & getParentName() const
Returns the name of the parent object (if any)
A color information.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Position getPositionInView() const
Returns position of Calibrator in view.
std::map< std::string, CalibratorFlow > myFlowValues
Calbratorflow values.
GNECalibrator(const std::string &id, GNEEdge *edge, GNEViewNet *viewNet, SUMOReal pos, SUMOTime frequency, const std::string &output, const std::map< std::string, CalibratorFlow > &flowValues, bool blocked)
Constructor.