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