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  // Center view in the position of calibrator
84  myViewNet->centerTo(getGlID(), false);
85 }
86 
87 
89 }
90 
91 
92 void
94  // This additional cannot be moved
95 }
96 
97 
98 void
100  // Clear all containers
101  myShapeRotations.clear();
102  myShapeLengths.clear();
103 
104  // clear Shape
105  myShape.clear();
106 
107  // Iterate over lanes
108  for (int i = 0; i < (int)myEdge->getLanes().size(); i++) {
109 
110  // Get shape of lane parent
111  myShape.push_back(myEdge->getLanes().at(i)->getShape().positionAtOffset(myEdge->getLanes().at(i)->getPositionRelativeToParametricLenght(myPosition.x())));
112 
113  // Obtain first position
114  Position f = myShape[i] - Position(1, 0);
115 
116  // Obtain next position
117  Position s = myShape[i] + Position(1, 0);
118 
119  // Save rotation (angle) of the vector constructed by points f and s
120  myShapeRotations.push_back(myEdge->getLanes().at(i)->getShape().rotationDegreeAtOffset(myEdge->getLanes().at(i)->getPositionRelativeToParametricLenght(myPosition.x())) * -1);
121  }
122 }
123 
124 
125 Position
127  return myPosition;
128 }
129 
130 void
132  // Open calibrator dialog
133  GNECalibratorDialog calibratorDialog(this);
134 }
135 
136 
137 void
138 GNECalibrator::writeAdditional(OutputDevice& device, const std::string&) {
139  // Write parameters
140  device.openTag(getTag());
141  device.writeAttr(SUMO_ATTR_ID, getID());
142  device.writeAttr(SUMO_ATTR_LANE, myEdge->getLanes().at(0)->getID());
146  // Write all flows of this calibrator
147  for (std::map<std::string, CalibratorFlow>::iterator i = myFlowValues.begin(); i != myFlowValues.end(); ++i) {
148  // Open flow tag
149  device.openTag(SUMO_TAG_FLOW);
150  // Write ID
151  device.writeAttr(SUMO_ATTR_ID, i->first);
152  // Write begin
153  device.writeAttr(SUMO_ATTR_BEGIN, i->second.begin);
154  // Write nd
155  device.writeAttr(SUMO_ATTR_END, i->second.end);
156  // Write type
157  device.writeAttr(SUMO_ATTR_TYPE, i->second.type);
158  // Write route
159  device.writeAttr(SUMO_ATTR_ROUTE, i->second.route);
160  // Write color
161  device.writeAttr(SUMO_ATTR_COLOR, i->second.color);
162  // Write depart lane
163  device.writeAttr(SUMO_ATTR_DEPARTLANE, i->second.departLane);
164  // Write depart pos
165  device.writeAttr(SUMO_ATTR_DEPARTPOS, i->second.departPos);
166  // Write depart speed
167  device.writeAttr(SUMO_ATTR_DEPARTSPEED, i->second.departSpeed);
168  // Write arrival lane
169  device.writeAttr(SUMO_ATTR_ARRIVALLANE, i->second.arrivalLane);
170  // Write arrival pos
171  device.writeAttr(SUMO_ATTR_ARRIVALPOS, i->second.arrivalPos);
172  // Write arrival speed
173  device.writeAttr(SUMO_ATTR_ARRIVALSPEED, i->second.arrivalSpeed);
174  // Write line
175  device.writeAttr(SUMO_ATTR_LINE, i->second.line);
176  // Write person number
177  device.writeAttr(SUMO_ATTR_PERSON_NUMBER, i->second.personNumber);
178  // Write container number
179  device.writeAttr(SUMO_ATTR_CONTAINER_NUMBER, i->second.containerNumber);
180  // Write vehsPerHour
181  device.writeAttr(SUMO_ATTR_VEHSPERHOUR, i->second.vehsPerHour);
182  // Write period
183  device.writeAttr(SUMO_ATTR_PERIOD, i->second.period);
184  // Write probability
185  device.writeAttr(SUMO_ATTR_PROB, i->second.probability);
186  // Write number
187  device.writeAttr(SUMO_ATTR_NUMBER, i->second.number);
188  // Close flow tag
189  device.closeTag();
190  }
191  // Close tag
192  device.closeTag();
193 }
194 
195 
196 std::map<std::string, GNECalibrator::CalibratorFlow>
198  return myFlowValues;
199 }
200 
201 
202 void
203 GNECalibrator::setFlowValues(std::map<std::string, GNECalibrator::CalibratorFlow> calibratorFlowValues) {
204  myFlowValues = calibratorFlowValues;
205 }
206 
207 
208 void
209 GNECalibrator::insertFlow(const std::string& id, const CalibratorFlow& flow) {
210  if (myFlowValues.find(id) == myFlowValues.end()) {
211  myFlowValues[id] = flow;
212  } else {
213  throw InvalidArgument("Calibrators don't allow Flows with duplicate Id's (" + id + ")");
214  }
215 
216 }
217 
218 
219 void
220 GNECalibrator::removeFlow(const std::string& id) {
221  if (myFlowValues.find(id) != myFlowValues.end()) {
222  myFlowValues.erase(id);
223  } else {
224  throw InvalidArgument("Calibrator with Id''" + id + "' not exists");
225  }
226 
227 }
228 
229 
230 const std::string&
232  return myEdge->getMicrosimID();
233 }
234 
235 
236 void
238  // get values
239  glPushName(getGlID());
240  glLineWidth(1.0);
241  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
242 
243  glPushName(getGlID());
244  for (int i = 0; i < (int)myShape.size(); ++i) {
245  const Position& pos = myShape[i];
246  SUMOReal rot = myShapeRotations[i];
247  glPushMatrix();
248  glTranslated(pos.x(), pos.y(), getType());
249  glRotated(rot, 0, 0, 1);
250  glTranslated(0, 0, getType());
251  glScaled(exaggeration, exaggeration, 1);
252  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
253 
254  glBegin(GL_TRIANGLES);
255  glColor3d(1, .8f, 0);
256  // base
257  glVertex2d(0 - 1.4, 0);
258  glVertex2d(0 - 1.4, 6);
259  glVertex2d(0 + 1.4, 6);
260  glVertex2d(0 + 1.4, 0);
261  glVertex2d(0 - 1.4, 0);
262  glVertex2d(0 + 1.4, 6);
263  glEnd();
264 
265  // draw text
266  if (s.scale * exaggeration >= 1.) {
267  glTranslated(0, 0, .1);
268  glColor3d(0, 0, 0);
269  pfSetPosition(0, 0);
270  pfSetScale(3.f);
271  SUMOReal w = pfdkGetStringWidth("C");
272  glRotated(180, 0, 1, 0);
273  glTranslated(-w / 2., 2, 0);
274  pfDrawString("C");
275  glTranslated(w / 2., -2, 0);
276  }
277  glPopMatrix();
278  }
279  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
280  glPopName();
281 }
282 
283 
284 std::string
286  switch (key) {
287  case SUMO_ATTR_ID:
288  return getAdditionalID();
289  case SUMO_ATTR_LANE:
290  return toString(myEdge->getLanes().at(0)->getAttribute(SUMO_ATTR_ID));
291  case SUMO_ATTR_POSITION:
292  return toString(myPosition.x());
293  case SUMO_ATTR_FREQUENCY:
294  return time2string(myFrequency);
295  case SUMO_ATTR_OUTPUT:
296  return myOutput;
298  if (myRouteProbe) {
299  return myRouteProbe->getID();
300  } else {
301  return "";
302  }
303  default:
304  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
305  }
306 }
307 
308 
309 void
310 GNECalibrator::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
311  if (value == getAttribute(key)) {
312  return; //avoid needless changes, later logic relies on the fact that attributes have changed
313  }
314  switch (key) {
315  case SUMO_ATTR_ID:
316  case SUMO_ATTR_LANE:
317  case SUMO_ATTR_POSITION:
318  case SUMO_ATTR_FREQUENCY:
319  case SUMO_ATTR_OUTPUT:
321  undoList->p_add(new GNEChange_Attribute(this, key, value));
322  updateGeometry();
323  break;
324  default:
325  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
326  }
327 
328 }
329 
330 
331 bool
332 GNECalibrator::isValid(SumoXMLAttr key, const std::string& value) {
333  switch (key) {
334  case SUMO_ATTR_ID:
335  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
336  return true;
337  } else {
338  return false;
339  }
340  case SUMO_ATTR_LANE:
341  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
342  return true;
343  } else {
344  return false;
345  }
346  case SUMO_ATTR_POSITION:
347  case SUMO_ATTR_FREQUENCY:
348  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
349  case SUMO_ATTR_OUTPUT:
350  return isValidFileValue(value);
352  if (myViewNet->getNet()->getAdditional(SUMO_TAG_ROUTEPROBE, value) != NULL) {
353  return true;
354  } else {
355  return false;
356  }
357  default:
358  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
359  }
360 }
361 
362 // ===========================================================================
363 // private
364 // ===========================================================================
365 
366 void
367 GNECalibrator::setAttribute(SumoXMLAttr key, const std::string& value) {
368  switch (key) {
369  case SUMO_ATTR_ID:
370  setAdditionalID(value);
371  break;
372  case SUMO_ATTR_LANE:
373  myEdge->removeAdditional(this);
374  myEdge = &(myViewNet->getNet()->retrieveLane(value)->getParentEdge());
375  myEdge->addAdditional(this);
376  updateGeometry();
377  getViewNet()->update();
378  break;
379  case SUMO_ATTR_POSITION:
380  myPosition = Position(parse<SUMOReal>(value), 0);
381  updateGeometry();
382  getViewNet()->update();
383  break;
384  case SUMO_ATTR_FREQUENCY:
385  myFrequency = string2time(value);
386  break;
387  case SUMO_ATTR_OUTPUT:
388  myOutput = value;
389  break;
392  break;
393  default:
394  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
395  }
396 }
397 
398 /****************************************************************************/
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
virtual void centerTo(GUIGlID id, bool applyZoom, SUMOReal zoomDist=20)
centers to the chosen artifact
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:821
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:835
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:870
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:213
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.