SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  myFrequency(frequency),
72  myOutput(output),
73  myRouteProbe(NULL),
74  myFlowValues(flowValues) {
75  // This additional belong to a edge
76  myEdge = edge;
77  // this additional ISN'T movable
78  myMovable = false;
79  // Update geometry;
81  // Set Colors
82  myBaseColor = RGBColor(255, 255, 50, 0);
83  myBaseColorSelected = RGBColor(255, 255, 125, 255);
84  // Center view in the position of calibrator
85  myViewNet->centerTo(getGlID(), false);
86 }
87 
88 
90 }
91 
92 
93 void
95  // This additional cannot be moved
96 }
97 
98 
99 void
101  // This additional cannot be moved
102 }
103 
104 
105 void
107  // Clear all containers
108  myShapeRotations.clear();
109  myShapeLengths.clear();
110 
111  // clear Shape
112  myShape.clear();
113 
114  // Iterate over lanes
115  for (int i = 0; i < (int)myEdge->getLanes().size(); i++) {
116 
117  // Get shape of lane parent
118  myShape.push_back(myEdge->getLanes().at(i)->getShape().positionAtOffset(myEdge->getLanes().at(i)->getPositionRelativeToParametricLenght(myPosition.x())));
119 
120  // Obtain first position
121  Position f = myShape[i] - Position(1, 0);
122 
123  // Obtain next position
124  Position s = myShape[i] + Position(1, 0);
125 
126  // Save rotation (angle) of the vector constructed by points f and s
127  myShapeRotations.push_back(myEdge->getLanes().at(i)->getShape().rotationDegreeAtOffset(myEdge->getLanes().at(i)->getPositionRelativeToParametricLenght(myPosition.x())) * -1);
128  }
129 
130  // Refresh element (neccesary to avoid grabbing problems)
132 }
133 
134 
135 Position
137  return myPosition;
138 }
139 
140 void
142  // Open calibrator dialog
143  GNECalibratorDialog calibratorDialog(this);
144 }
145 
146 
147 void
148 GNECalibrator::writeAdditional(OutputDevice& device, const std::string&) {
149  // Write parameters
150  device.openTag(getTag());
151  device.writeAttr(SUMO_ATTR_ID, getID());
152  device.writeAttr(SUMO_ATTR_LANE, myEdge->getLanes().at(0)->getID());
156  // Write all flows of this calibrator
157  for (std::map<std::string, CalibratorFlow>::iterator i = myFlowValues.begin(); i != myFlowValues.end(); ++i) {
158  // Open flow tag
159  device.openTag(SUMO_TAG_FLOW);
160  // Write ID
161  device.writeAttr(SUMO_ATTR_ID, i->first);
162  // Write begin
163  device.writeAttr(SUMO_ATTR_BEGIN, i->second.begin);
164  // Write nd
165  device.writeAttr(SUMO_ATTR_END, i->second.end);
166  // Write type
167  device.writeAttr(SUMO_ATTR_TYPE, i->second.type);
168  // Write route
169  device.writeAttr(SUMO_ATTR_ROUTE, i->second.route);
170  // Write color
171  device.writeAttr(SUMO_ATTR_COLOR, i->second.color);
172  // Write depart lane
173  device.writeAttr(SUMO_ATTR_DEPARTLANE, i->second.departLane);
174  // Write depart pos
175  device.writeAttr(SUMO_ATTR_DEPARTPOS, i->second.departPos);
176  // Write depart speed
177  device.writeAttr(SUMO_ATTR_DEPARTSPEED, i->second.departSpeed);
178  // Write arrival lane
179  device.writeAttr(SUMO_ATTR_ARRIVALLANE, i->second.arrivalLane);
180  // Write arrival pos
181  device.writeAttr(SUMO_ATTR_ARRIVALPOS, i->second.arrivalPos);
182  // Write arrival speed
183  device.writeAttr(SUMO_ATTR_ARRIVALSPEED, i->second.arrivalSpeed);
184  // Write line
185  device.writeAttr(SUMO_ATTR_LINE, i->second.line);
186  // Write person number
187  device.writeAttr(SUMO_ATTR_PERSON_NUMBER, i->second.personNumber);
188  // Write container number
189  device.writeAttr(SUMO_ATTR_CONTAINER_NUMBER, i->second.containerNumber);
190  // Write vehsPerHour
191  device.writeAttr(SUMO_ATTR_VEHSPERHOUR, i->second.vehsPerHour);
192  // Write period
193  device.writeAttr(SUMO_ATTR_PERIOD, i->second.period);
194  // Write probability
195  device.writeAttr(SUMO_ATTR_PROB, i->second.probability);
196  // Write number
197  device.writeAttr(SUMO_ATTR_NUMBER, i->second.number);
198  // Close flow tag
199  device.closeTag();
200  }
201  // Close tag
202  device.closeTag();
203 }
204 
205 
206 std::map<std::string, GNECalibrator::CalibratorFlow>
208  return myFlowValues;
209 }
210 
211 
212 void
213 GNECalibrator::setFlowValues(std::map<std::string, GNECalibrator::CalibratorFlow> calibratorFlowValues) {
214  myFlowValues = calibratorFlowValues;
215 }
216 
217 
218 void
219 GNECalibrator::insertFlow(const std::string& id, const CalibratorFlow& flow) {
220  if (myFlowValues.find(id) == myFlowValues.end()) {
221  myFlowValues[id] = flow;
222  } else {
223  throw InvalidArgument("Calibrators don't allow Flows with duplicate Id's (" + id + ")");
224  }
225 
226 }
227 
228 
229 void
230 GNECalibrator::removeFlow(const std::string& id) {
231  if (myFlowValues.find(id) != myFlowValues.end()) {
232  myFlowValues.erase(id);
233  } else {
234  throw InvalidArgument("Calibrator with Id''" + id + "' not exists");
235  }
236 
237 }
238 
239 
240 const std::string&
242  return myEdge->getMicrosimID();
243 }
244 
245 
246 void
248  // get values
249  glPushName(getGlID());
250  glLineWidth(1.0);
251  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
252 
253  glPushName(getGlID());
254  for (int i = 0; i < (int)myShape.size(); ++i) {
255  const Position& pos = myShape[i];
256  SUMOReal rot = myShapeRotations[i];
257  glPushMatrix();
258  glTranslated(pos.x(), pos.y(), getType());
259  glRotated(rot, 0, 0, 1);
260  glTranslated(0, 0, getType());
261  glScaled(exaggeration, exaggeration, 1);
262  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
263 
264  glBegin(GL_TRIANGLES);
265  glColor3d(1, .8f, 0);
266  // base
267  glVertex2d(0 - 1.4, 0);
268  glVertex2d(0 - 1.4, 6);
269  glVertex2d(0 + 1.4, 6);
270  glVertex2d(0 + 1.4, 0);
271  glVertex2d(0 - 1.4, 0);
272  glVertex2d(0 + 1.4, 6);
273  glEnd();
274 
275  // draw text
276  if (s.scale * exaggeration >= 1.) {
277  glTranslated(0, 0, .1);
278  glColor3d(0, 0, 0);
279  pfSetPosition(0, 0);
280  pfSetScale(3.f);
281  SUMOReal w = pfdkGetStringWidth("C");
282  glRotated(180, 0, 1, 0);
283  glTranslated(-w / 2., 2, 0);
284  pfDrawString("C");
285  glTranslated(w / 2., -2, 0);
286  }
287  glPopMatrix();
288  }
289  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
290  glPopName();
291 }
292 
293 
294 std::string
296  switch (key) {
297  case SUMO_ATTR_ID:
298  return getAdditionalID();
299  case SUMO_ATTR_LANE:
300  return toString(myEdge->getLanes().at(0)->getAttribute(SUMO_ATTR_ID));
301  case SUMO_ATTR_POSITION:
302  return toString(myPosition.x());
303  case SUMO_ATTR_FREQUENCY:
304  return time2string(myFrequency);
305  case SUMO_ATTR_OUTPUT:
306  return myOutput;
308  if (myRouteProbe) {
309  return myRouteProbe->getID();
310  } else {
311  return "";
312  }
313  default:
314  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
315  }
316 }
317 
318 
319 void
320 GNECalibrator::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
321  if (value == getAttribute(key)) {
322  return; //avoid needless changes, later logic relies on the fact that attributes have changed
323  }
324  switch (key) {
325  case SUMO_ATTR_ID:
326  case SUMO_ATTR_LANE:
327  case SUMO_ATTR_POSITION:
328  case SUMO_ATTR_FREQUENCY:
329  case SUMO_ATTR_OUTPUT:
331  undoList->p_add(new GNEChange_Attribute(this, key, value));
332  updateGeometry();
333  break;
334  default:
335  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
336  }
337 
338 }
339 
340 
341 bool
342 GNECalibrator::isValid(SumoXMLAttr key, const std::string& value) {
343  switch (key) {
344  case SUMO_ATTR_ID:
345  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
346  return true;
347  } else {
348  return false;
349  }
350  case SUMO_ATTR_LANE:
351  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
352  return true;
353  } else {
354  return false;
355  }
356  case SUMO_ATTR_POSITION:
357  case SUMO_ATTR_FREQUENCY:
358  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
359  case SUMO_ATTR_OUTPUT:
360  return isValidFileValue(value);
362  if (myViewNet->getNet()->getAdditional(SUMO_TAG_ROUTEPROBE, value) != NULL) {
363  return true;
364  } else {
365  return false;
366  }
367  default:
368  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
369  }
370 }
371 
372 // ===========================================================================
373 // private
374 // ===========================================================================
375 
376 void
377 GNECalibrator::setAttribute(SumoXMLAttr key, const std::string& value) {
378  switch (key) {
379  case SUMO_ATTR_ID:
380  setAdditionalID(value);
381  break;
382  case SUMO_ATTR_LANE:
383  changeLane(value);
384  break;
385  case SUMO_ATTR_POSITION:
386  myPosition = Position(parse<SUMOReal>(value), 0);
387  updateGeometry();
388  getViewNet()->update();
389  break;
390  case SUMO_ATTR_FREQUENCY:
391  myFrequency = string2time(value);
392  break;
393  case SUMO_ATTR_OUTPUT:
394  myOutput = value;
395  break;
398  break;
399  default:
400  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
401  }
402 }
403 
404 /****************************************************************************/
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.
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
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's width (temporary, used for a single view)
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 ...
const std::string & getAdditionalID() const
returns the ID of additional
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
void refreshAdditional(GNEAdditional *additional)
refreshes boundary information of an additional after a geometry update
Definition: GNENet.cpp:784
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:744
friend class GNEChange_Attribute
declare friend class
void commmitAdditionalGeometryMoved(SUMOReal, SUMOReal, GNEUndoList *)
updated geometry changes in the attributes of additional
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
void changeLane(const std::string &laneID)
change lane of additional
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:55
Dialog for edit calibrators.
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1250
const std::vector< GNELane * > & getLanes()
returns a reference to the lane vector
Definition: GNEEdge.cpp:485
void pfSetScale(SUMOReal s)
Definition: polyfonts.c:465
An Element which don'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
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:912
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
void moveAdditionalGeometry(SUMOReal, SUMOReal)
change the position of the calibrator geometry
GNEEdge * myEdge
The edge this additional belongs NULL if additional doesnt' belongs to a edge.
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.