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.dlr.de/
10 // Copyright (C) 2001-2017 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, const SUMOReal chargeDelay) :
68  GNEStoppingPlace(id, viewNet, SUMO_TAG_CHARGING_STATION, ICON_CHARGINGSTATION, lane, startPos, endPos),
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
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 greater 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  throw InvalidArgument("Value of chargeDelay cannot be negative");
228  }
229  myChargeDelay = chargeDelay;
230 }
231 
232 
233 void
235  // Push name
236  glPushName(getGlID());
237 
238  // Push base matrix
239  glPushMatrix();
240 
241  // Traslate matrix
242  glTranslated(0, 0, getType());
243 
244  // Set Color
245  if (isAdditionalSelected()) {
247  } else {
249  }
250 
251  // Get exaggeration
252  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
253 
254  // Draw base
256 
257  // draw details unless zoomed out to far
258  if (s.scale * exaggeration >= 10) {
259  // Push sign matrix
260  glPushMatrix();
261 
262  // Set color of the charging power
263  if (isAdditionalSelected()) {
265  } else {
267  }
268 
269  // push charging power matrix
270  glPushMatrix();
271 
272  // Traslate End positionof signal
273  glTranslated(mySignPos.x(), mySignPos.y(), 0);
274 
275  // Rotate 180 (Eje X -> Mirror)
276  glRotated(180, 1, 0, 0);
277 
278  // Rotate again using myBlockIconRotation
279  glRotated(myBlockIconRotation, 0, 0, 1);
280 
281  // Set poligon mode
282  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
283 
284  // set polyfront position on 0,0
285  pfSetPosition(0, 0);
286 
287  // Set polyfront scale to 1
288  pfSetScale(1.f);
289 
290  // traslate matrix
291  glTranslated(1.2, 0, 0);
292 
293  // draw charging power
294  pfDrawString((toString(myChargingPower) + " W").c_str());
295 
296  // pop charging power matrix
297  glPopMatrix();
298 
299  // Set position over sign
300  glTranslated(mySignPos.x(), mySignPos.y(), 0);
301 
302  // Define nš points (for efficiency)
303  int noPoints = 9;
304 
305  // If the scale * exaggeration is more than 25, recalculate nš points
306  if (s.scale * exaggeration > 25) {
307  noPoints = MIN2((int)(9.0 + (s.scale * exaggeration) / 10.0), 36);
308  }
309 
310  // Scale matrix
311  glScaled(exaggeration, exaggeration, 1);
312 
313  // Set base color
314  if (isAdditionalSelected()) {
316  } else {
318  }
319 
320  // Draw extern
321  GLHelper::drawFilledCircle((SUMOReal) 1.1, noPoints);
322 
323  // Move to top
324  glTranslated(0, 0, .1);
325 
326  // Set sign color
327  if (isAdditionalSelected()) {
329  } else {
331  }
332  // Draw internt sign
333  GLHelper::drawFilledCircle((SUMOReal) 0.9, noPoints);
334 
335  // Draw sign 'C'
336  if (s.scale * exaggeration >= 4.5) {
337  if (isAdditionalSelected()) {
339  } else {
341  }
342  }
343  // Pop sign matrix
344  glPopMatrix();
345 
346  // Draw icon
348  }
349 
350  // Pop base matrix
351  glPopMatrix();
352 
353  // Pop name matrix
354  glPopName();
355 
356  // Draw name
357  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
358 }
359 
360 
361 std::string
363  switch (key) {
364  case SUMO_ATTR_ID:
365  return getAdditionalID();
366  case SUMO_ATTR_LANE:
368  case SUMO_ATTR_STARTPOS:
369  return toString(myStartPos);
370  case SUMO_ATTR_ENDPOS:
371  return toString(myEndPos);
373  return toString(myChargingPower);
375  return toString(myEfficiency);
377  return toString(myChargeInTransit);
379  return toString(myChargeDelay);
381  return toString(myBlocked);
382  default:
383  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
384  }
385 }
386 
387 
388 void
389 GNEChargingStation::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
390  if (value == getAttribute(key)) {
391  return; //avoid needless changes, later logic relies on the fact that attributes have changed
392  }
393  switch (key) {
394  case SUMO_ATTR_ID:
395  case SUMO_ATTR_LANE:
396  case SUMO_ATTR_STARTPOS:
397  case SUMO_ATTR_ENDPOS:
403  undoList->p_add(new GNEChange_Attribute(this, key, value));
404  break;
405  default:
406  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
407  }
408 }
409 
410 
411 bool
412 GNEChargingStation::isValid(SumoXMLAttr key, const std::string& value) {
413  switch (key) {
414  case SUMO_ATTR_ID:
415  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
416  return true;
417  } else {
418  return false;
419  }
420  case SUMO_ATTR_LANE:
421  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
422  return true;
423  } else {
424  return false;
425  }
426  case SUMO_ATTR_STARTPOS:
427  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0 && parse<SUMOReal>(value) < (myEndPos - 1));
428  case SUMO_ATTR_ENDPOS: {
429  if (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 1 && parse<SUMOReal>(value) > myStartPos) {
430  // If extension is larger than Lane
431  if (parse<SUMOReal>(value) > myLane->getLaneParametricLenght()) {
432  // Ask user if want to assign the lenght of lane as endPosition
433  FXuint answer = FXMessageBox::question(getViewNet()->getApp(), MBOX_YES_NO,
434  (toString(SUMO_ATTR_ENDPOS) + " exceeds the size of the " + toString(SUMO_TAG_LANE)).c_str(), "%s",
435  (toString(SUMO_ATTR_ENDPOS) + " exceeds the size of the " + toString(SUMO_TAG_LANE) +
436  ". Do you want to assign the lenght of the " + toString(SUMO_TAG_LANE) + " as " + toString(SUMO_ATTR_ENDPOS) + "?").c_str());
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<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
458  return canParse<bool>(value);
459  default:
460  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
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<SUMOReal>(value);
502  break;
504  myBlocked = parse<bool>(value);
505  getViewNet()->update();
506  break;
507  default:
508  throw InvalidArgument(toString(getTag()) + "attribute '" + toString(key) + "' not allowed");
509  }
510 }
511 
512 
513 /****************************************************************************/
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:256
int pfDrawString(const char *c)
Definition: polyfonts.c:1074
SUMOReal getEfficiency()
Returns the charging efficiency of the chargingStation.
GUIVisualizationTextSettings addName
bool getChargeInTransit()
Returns the value of charge in transit of the chargingStation.
begin/end of the description of a single lane
GNELane * myLane
The lane this additional belongs.
Stores the information about how to visualize structures.
bool myRotationLefthand
rotation depending of the option "Lefthand"
Allow/disallow charge in transit in Charging Stations.
bool isAdditionalSelected() const
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:54
SUMOReal getChargingPower()
Returns the charging power of the chargingStation.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:240
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 x() const
Returns the x-position.
Definition: Position.h:63
GUIGlID getGlID() const
Returns the numerical id of the object.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
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.
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
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
#define PI
Definition: polyfonts.c:61
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
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
const std::string & getAdditionalID() const
returns the ID of additional
SUMOReal getLaneParametricLenght() const
returns the parameteric length of the lane
Definition: GNELane.cpp:677
void refreshAdditional(GNEAdditional *additional)
refreshes boundary information of an additional after a geometry update
Definition: GNENet.cpp:814
SUMOReal getPositionRelativeToParametricLenght(SUMOReal position) const
Definition: GNELane.cpp:689
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:774
void setBlockIconRotation(GNELane *lane=NULL)
set Rotation of block Icon
friend class GNEChange_Attribute
declare friend class
RGBColor myTextColor
Text color (Default cyan)
block movement of a graphic element
void updateGeometry()
update pre-computed geometry information
void drawLockIcon(SUMOReal size=0.5) const
draw lock icon
SUMOReal myStartPos
The start position this stopping place is located at.
T MIN2(T a, T b)
Definition: StdDefs.h:64
void setChargeDelay(SUMOReal chargeDelay)
Set a new charge delay in the charging station.
~GNEChargingStation()
Destructor.
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
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:742
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1265
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
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) ...
std::string getAttribute(SumoXMLAttr key) const
SUMOReal myChargingPower
Charging power pro timestep.
void pfSetScale(SUMOReal s)
Definition: polyfonts.c:465
GNEChargingStation(const std::string &id, GNELane *lane, GNEViewNet *viewNet, SUMOReal startPos, SUMOReal endPos, SUMOReal chargingPower, SUMOReal efficiency, bool chargeInTransit, const SUMOReal chargeDelay)
Constructor of charging station.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
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:989
Position getLineCenter() const
get line center
SumoXMLTag getTag() const
get XML Tag assigned to this object
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
Eficiency of the charge in Charging Stations.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:208
void setChargingPower(SUMOReal chargingPower)
Set a new charging power in the charging station.
Delay in the charge of charging stations.
Position mySignPos
The position of the sign.
SUMOReal myChargeDelay
delay in the starting of charge
PositionVector getSubpart(SUMOReal beginOffset, SUMOReal endOffset) const
get subpart of a position vector
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 ...