SUMO - Simulation of Urban MObility
GNEChargingStation.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 "GNEChargingStation.h"
51 #include "GNELane.h"
52 #include "GNEEdge.h"
53 #include "GNEJunction.h"
54 #include "GNEUndoList.h"
55 #include "GNENet.h"
56 #include "GNEChange_Attribute.h"
57 #include "GNEViewNet.h"
58 
59 #ifdef CHECK_MEMORY_LEAKS
60 #include <foreign/nvwa/debug_new.h>
61 #endif
62 
63 // ===========================================================================
64 // member method definitions
65 // ===========================================================================
66 
67 GNEChargingStation::GNEChargingStation(const std::string& id, GNELane* lane, GNEViewNet* viewNet, SUMOReal startPos, SUMOReal endPos, SUMOReal chargingPower, SUMOReal efficiency, bool chargeInTransit, int chargeDelay, bool blocked) :
68  GNEStoppingPlace(id, viewNet, SUMO_TAG_CHARGING_STATION, lane, startPos, endPos, blocked),
69  myChargingPower(chargingPower),
70  myEfficiency(efficiency),
71  myChargeInTransit(chargeInTransit),
72  myChargeDelay(chargeDelay) {
73  // When a new additional element is created, updateGeometry() must be called
75  // Set Colors
76  myBaseColor = RGBColor(114, 210, 252, 255);
77  myBaseColorSelected = RGBColor(125, 255, 255, 255);
78  mySignColor = RGBColor(255, 235, 0, 255);
79  mySignColorSelected = RGBColor(255, 235, 0, 255);
80  myTextColor = RGBColor(114, 210, 252, 255);
81  myTextColorSelected = RGBColor(125, 255, 255, 255);
82 }
83 
84 
86 
87 
88 void
90  // Clear all containers
91  myShapeRotations.clear();
92  myShapeLengths.clear();
93 
94  // Clear shape
95  myShape.clear();
96 
97  // Get shape of lane parent
98  myShape = myLane->getShape();
99 
100  // Cut shape using as delimitators from start position and end position
102 
103  // Get number of parts of the shape
104  int numberOfSegments = (int) myShape.size() - 1;
105 
106  // If number of segments is more than 0
107  if (numberOfSegments >= 0) {
108 
109  // Reserve memory (To improve efficiency)
110  myShapeRotations.reserve(numberOfSegments);
111  myShapeLengths.reserve(numberOfSegments);
112 
113  // For every part of the shape
114  for (int i = 0; i < numberOfSegments; ++i) {
115 
116  // Obtain first position
117  const Position& f = myShape[i];
118 
119  // Obtain next position
120  const Position& s = myShape[i + 1];
121 
122  // Save distance between position into myShapeLengths
123  myShapeLengths.push_back(f.distanceTo(s));
124 
125  // Save rotation (angle) of the vector constructed by points f and s
126  myShapeRotations.push_back((SUMOReal) atan2((s.x() - f.x()), (f.y() - s.y())) * (SUMOReal) 180.0 / (SUMOReal) PI);
127  }
128  }
129 
130  // Obtain a copy of the shape
131  PositionVector tmpShape = myShape;
132 
133  // Move shape to side
134  if (myRotationLefthand) {
135  tmpShape.move2side(-1.5);
136  } else {
137  tmpShape.move2side(1.5);
138  }
139 
140  // Get position of the sign
141  mySignPos = tmpShape.getLineCenter();
142 
143  // Set block icon position
145 
146  // Set block icon rotation, and using their rotation for sign
148 
149  // Refresh element (neccesary to avoid grabbing problems)
151 }
152 
153 
154 void
155 GNEChargingStation::writeAdditional(OutputDevice& device, const std::string&) {
156  // Write additional
157  device.openTag(getTag());
158  device.writeAttr(SUMO_ATTR_ID, getID());
159  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
166  if (myBlocked) {
168  }
169  // Close tag
170  device.closeTag();
171 }
172 
173 
174 SUMOReal
176  return myChargingPower;
177 }
178 
179 
180 SUMOReal
182  return myEfficiency;
183 }
184 
185 
186 bool
188  return myChargeInTransit;
189 }
190 
191 
192 SUMOReal
194  return myChargeDelay;
195 }
196 
197 
198 void
200  if (chargingPower > 0) {
201  myChargingPower = chargingPower;
202  } else {
203  throw InvalidArgument("Value of charging Power must be greather than 0");
204  }
205 }
206 
207 
208 void
210  if (efficiency >= 0 && efficiency <= 1) {
211  myEfficiency = efficiency;
212  } else {
213  throw InvalidArgument("Value of efficiency must be between 0 and 1");
214  }
215 }
216 
217 
218 void
220  myChargeInTransit = chargeInTransit;
221 }
222 
223 
224 void
226  if (chargeDelay < 0) {
227  myChargeDelay = chargeDelay;
228  } else {
229  throw InvalidArgument("Value of chargeDelay cannot be negative");
230  }
231 }
232 
233 
234 void
236  // Push name
237  glPushName(getGlID());
238 
239  // Push base matrix
240  glPushMatrix();
241 
242  // Traslate matrix
243  glTranslated(0, 0, getType());
244 
245  // Set Color
246  if (isAdditionalSelected()) {
248  } else {
250  }
251 
252  // Get exaggeration
253  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
254 
255  // Draw base
257 
258  // draw details unless zoomed out to far
259  if (s.scale * exaggeration >= 10) {
260  // Push sign matrix
261  glPushMatrix();
262 
263  // Set color of the charging power
264  if (isAdditionalSelected()) {
266  } else {
268  }
269 
270  // push charging power matrix
271  glPushMatrix();
272 
273  // Traslate End positionof signal
274  glTranslated(mySignPos.x(), mySignPos.y(), 0);
275 
276  // Rotate 180 (Eje X -> Mirror)
277  glRotated(180, 1, 0, 0);
278 
279  // Rotate again using myBlockIconRotation
280  glRotated(myBlockIconRotation, 0, 0, 1);
281 
282  // Set poligon mode
283  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
284 
285  // set polyfront position on 0,0
286  pfSetPosition(0, 0);
287 
288  // Set polyfront scale to 1
289  pfSetScale(1.f);
290 
291  // traslate matrix
292  glTranslated(1.2, 0, 0);
293 
294  // draw charging power
295  pfDrawString((toString(myChargingPower) + " W").c_str());
296 
297  // pop charging power matrix
298  glPopMatrix();
299 
300  // Set position over sign
301  glTranslated(mySignPos.x(), mySignPos.y(), 0);
302 
303  // Define nš points (for efficiency)
304  int noPoints = 9;
305 
306  // If the scale * exaggeration is more than 25, recalculate nš points
307  if (s.scale * exaggeration > 25) {
308  noPoints = MIN2((int)(9.0 + (s.scale * exaggeration) / 10.0), 36);
309  }
310 
311  // Scale matrix
312  glScaled(exaggeration, exaggeration, 1);
313 
314  // Set base color
315  if (isAdditionalSelected()) {
317  } else {
319  }
320 
321  // Draw extern
322  GLHelper::drawFilledCircle((SUMOReal) 1.1, noPoints);
323 
324  // Move to top
325  glTranslated(0, 0, .1);
326 
327  // Set sign color
328  if (isAdditionalSelected()) {
330  } else {
332  }
333  // Draw internt sign
334  GLHelper::drawFilledCircle((SUMOReal) 0.9, noPoints);
335 
336  // Draw sign 'C'
337  if (s.scale * exaggeration >= 4.5) {
338  if (isAdditionalSelected()) {
340  } else {
342  }
343  }
344  // Pop sign matrix
345  glPopMatrix();
346 
347  // Draw icon
349  }
350 
351  // Pop base matrix
352  glPopMatrix();
353 
354  // Pop name matrix
355  glPopName();
356 
357  // Draw name
358  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
359 }
360 
361 
362 std::string
364  switch (key) {
365  case SUMO_ATTR_ID:
366  return getAdditionalID();
367  case SUMO_ATTR_LANE:
369  case SUMO_ATTR_STARTPOS:
370  return toString(myStartPos);
371  case SUMO_ATTR_ENDPOS:
372  return toString(myEndPos);
374  return toString(myChargingPower);
376  return toString(myEfficiency);
378  return toString(myChargeInTransit);
380  return toString(myChargeDelay);
382  return toString(myBlocked);
383  default:
384  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
385  }
386 }
387 
388 
389 void
390 GNEChargingStation::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
391  if (value == getAttribute(key)) {
392  return; //avoid needless changes, later logic relies on the fact that attributes have changed
393  }
394  switch (key) {
395  case SUMO_ATTR_ID:
396  case SUMO_ATTR_LANE:
397  case SUMO_ATTR_STARTPOS:
398  case SUMO_ATTR_ENDPOS:
404  undoList->p_add(new GNEChange_Attribute(this, key, value));
405  break;
406  default:
407  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
408  }
409 }
410 
411 
412 bool
413 GNEChargingStation::isValid(SumoXMLAttr key, const std::string& value) {
414  switch (key) {
415  case SUMO_ATTR_ID:
416  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
417  return true;
418  } else {
419  return false;
420  }
421  case SUMO_ATTR_LANE:
422  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
423  return true;
424  } else {
425  return false;
426  }
427  case SUMO_ATTR_STARTPOS:
428  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0 && parse<SUMOReal>(value) < (myEndPos - 1));
429  case SUMO_ATTR_ENDPOS: {
430  if (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 1 && parse<SUMOReal>(value) > myStartPos) {
431  // If extension is larger than Lane
432  if (parse<SUMOReal>(value) > myLane->getLaneParametricLenght()) {
433  // Ask user if want to assign the lenght of lane as endPosition
434  FXuint answer = FXMessageBox::question(getViewNet()->getApp(), MBOX_YES_NO,
435  "EndPosition exceeds the size of the lane", "%s",
436  "EndPosition exceeds the size of the lane. You want to assign the size of the lane as endPosition?");
437  if (answer == 1) { //1:yes, 2:no, 4:esc
438  return true;
439  } else {
440  return false;
441  }
442  } else {
443  return true;
444  }
445  } else {
446  return false;
447  }
448  }
450  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
452  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0 && parse<SUMOReal>(value) <= 1);
454  return canParse<bool>(value);
456  return (canParse<int>(value) && parse<int>(value) >= 0);
458  return canParse<bool>(value);
459  default:
460  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
461  }
462 }
463 
464 // ===========================================================================
465 // private
466 // ===========================================================================
467 
468 void
469 GNEChargingStation::setAttribute(SumoXMLAttr key, const std::string& value) {
470  switch (key) {
471  case SUMO_ATTR_ID:
472  setAdditionalID(value);
473  break;
474  case SUMO_ATTR_LANE:
475  changeLane(value);
476  break;
477  case SUMO_ATTR_STARTPOS:
478  myStartPos = parse<SUMOReal>(value);
479  updateGeometry();
480  getViewNet()->update();
481  break;
482  case SUMO_ATTR_ENDPOS:
483  if (parse<SUMOReal>(value) > myLane->getLaneParametricLenght()) {
485  } else {
486  myEndPos = parse<SUMOReal>(value);
487  }
488  updateGeometry();
489  getViewNet()->update();
490  break;
492  myChargingPower = parse<SUMOReal>(value);
493  break;
495  myEfficiency = parse<SUMOReal>(value);
496  break;
498  myChargeInTransit = parse<bool>(value);
499  break;
501  myChargeDelay = parse<int>(value);
502  break;
504  myBlocked = parse<bool>(value);
505  getViewNet()->update();
506  break;
507  default:
508  throw InvalidArgument(toString(getType()) + "attribute '" + toString(key) + "' not allowed");
509  }
510 }
511 
512 /****************************************************************************/
void drawName(const Position &pos, const SUMOReal scale, const GUIVisualizationTextSettings &settings, const SUMOReal angle=0) const
draw name of item
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
SUMOReal getEfficiency()
Returns the charging efficiency of the chargingStation.
int myChargeDelay
delay in the starting of charge
GUIVisualizationTextSettings addName
bool getChargeInTransit()
Returns the value of charge in transit of the chargingStation.
const std::string & getAdditionalID() const
returns the ID of additional
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:221
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1250
GNELane * myLane
The lane this additional belongs NULL if additional doesnt&#39; belongs to a lane.
Stores the information about how to visualize structures.
bool myRotationLefthand
rotation depending of the option "Lefthand"
RGBColor myTextColorSelected
Text color selected (Default blue)
void pfSetPosition(SUMOReal x, SUMOReal y)
Definition: polyfonts.c:480
static void drawBoxLines(const PositionVector &geom, const std::vector< SUMOReal > &rots, const std::vector< SUMOReal > &lengths, SUMOReal width, int cornerDetail=0, SUMOReal offset=0)
Draws thick lines.
Definition: GLHelper.cpp:176
SUMOReal getChargeDelay()
Returns the charge delay of the chargingStation.
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:55
SUMOReal getChargingPower()
Returns the charging power of the chargingStation.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static void drawText(const std::string &text, const Position &pos, const SUMOReal layer, const SUMOReal size, const RGBColor &col=RGBColor::BLACK, const SUMOReal angle=0)
draw Text with given parameters
Definition: GLHelper.cpp:460
SUMOReal myEndPos
The end position this stopping place is located at.
SUMOReal getLaneParametricLenght() const
returns the parameteric length of the lane
Definition: GNELane.cpp:681
Position getLineCenter() const
get line center
SUMOReal scale
information about a lane&#39;s width (temporary, used for a single view)
SUMOReal myEfficiency
efficiency of the charge
void setChargeInTransit(bool chargeInTransit)
Enable or disable charge in transit in the charging station.
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.
RGBColor myBaseColorSelected
base color selected (Default blue)
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
static void drawFilledCircle(SUMOReal width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:344
GUIVisualizationSizeSettings addSize
#define PI
Definition: polyfonts.c:61
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
std::vector< SUMOReal > myShapeLengths
The lengths of the shape parts.
SUMOReal myBlockIconRotation
The rotation of the block icon.
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:443
void refreshAdditional(GNEAdditional *additional)
refreshes boundary information of an additional after a geometry update
Definition: GNENet.cpp:784
PositionVector myShape
The shape of the additional element.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
RGBColor mySignColor
sign color (Default yellow)
A list of positions.
GNELane * retrieveLane(const std::string &id, bool failHard=true)
get lane by id
Definition: GNENet.cpp:744
void setBlockIconRotation(GNELane *lane=NULL)
friend class GNEChange_Attribute
declare friend class
void writeAdditional(OutputDevice &device, const std::string &)
writte additional element into a xml file
RGBColor myTextColor
Text color (Default cyan)
void updateGeometry()
update pre-computed geometry information
SUMOReal myStartPos
The start position this stopping place is located at.
PositionVector getSubpart(SUMOReal beginOffset, SUMOReal endOffset) const
get subpart of a position vector
T MIN2(T a, T b)
Definition: StdDefs.h:69
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
void setChargeDelay(SUMOReal chargeDelay)
Set a new charge delay in the charging station.
~GNEChargingStation()
Destructor.
void drawLockIcon(SUMOReal size=0.5) const
draw lock icon
void setAdditionalID(const std::string &id)
set the ID of additional
const std::string getID() const
function to support debugging
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
bool myChargeInTransit
enable or disable charge in transit
void changeLane(const std::string &laneID)
change lane of additional
std::string getAttribute(SumoXMLAttr key) const
Definition: GNELane.cpp:788
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:583
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
SUMOReal myChargingPower
Charging power pro timestep.
void pfSetScale(SUMOReal s)
Definition: polyfonts.c:465
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
SUMOReal getPositionRelativeToParametricLenght(SUMOReal position) const
Definition: GNELane.cpp:693
RGBColor myBaseColor
base color (Default green)
std::vector< SUMOReal > myShapeRotations
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:912
GNEChargingStation(const std::string &id, GNELane *lane, GNEViewNet *viewNet, SUMOReal startPos, SUMOReal endPos, SUMOReal chargingPower, SUMOReal efficiency, bool chargeInTransit, int chargeDelay, bool blocked)
Constructor of charging station.
GUIGlID getGlID() const
Returns the numerical id of the object.
std::string getAttribute(SumoXMLAttr key) const
void move2side(SUMOReal amount)
move position vector to side using certain ammount
void setEfficiency(SUMOReal efficiency)
Set a new efficiency in the charging station.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
SUMOReal getExaggeration(const GUIVisualizationSettings &s, SUMOReal factor=20) const
return the drawing size including exaggeration and constantSize values
bool isAdditionalSelected() const
void setChargingPower(SUMOReal chargingPower)
Set a new charging power in the charging station.
Position mySignPos
The position of the sign.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
RGBColor mySignColorSelected
sign selected color (Default blue)
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 Tag assigned to this object