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 
150 
151 void
152 GNEChargingStation::writeAdditional(OutputDevice& device, const std::string&) {
153  // Write additional
154  device.openTag(getTag());
155  device.writeAttr(SUMO_ATTR_ID, getID());
156  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
161  if (myChargeInTransit) {
162  device.writeAttr(SUMO_ATTR_CHARGEINTRANSIT, "true");
163  } else {
164  device.writeAttr(SUMO_ATTR_CHARGEINTRANSIT, "false");
165  }
167  // Close tag
168  device.closeTag();
169 }
170 
171 
172 SUMOReal
174  return myChargingPower;
175 }
176 
177 
178 SUMOReal
180  return myEfficiency;
181 }
182 
183 
184 bool
186  return myChargeInTransit;
187 }
188 
189 
190 SUMOReal
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 greather 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  myChargeDelay = chargeDelay;
226  } else {
227  throw InvalidArgument("Value of chargeDelay cannot be negative");
228  }
229 }
230 
231 
232 void
234  // Push name
235  glPushName(getGlID());
236 
237  // Push base matrix
238  glPushMatrix();
239 
240  // Traslate matrix
241  glTranslated(0, 0, getType());
242 
243  // Set Color
244  if (isAdditionalSelected()) {
246  } else {
248  }
249 
250  // Get exaggeration
251  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
252 
253  // Draw base
255 
256  // draw details unless zoomed out to far
257  if (s.scale * exaggeration >= 10) {
258  // Push sign matrix
259  glPushMatrix();
260 
261  // Set color of the charging power
262  if (isAdditionalSelected()) {
264  } else {
266  }
267 
268  // push charging power matrix
269  glPushMatrix();
270 
271  // Traslate End positionof signal
272  glTranslated(mySignPos.x(), mySignPos.y(), 0);
273 
274  // Rotate 180 (Eje X -> Mirror)
275  glRotated(180, 1, 0, 0);
276 
277  // Rotate again using myBlockIconRotation
278  glRotated(myBlockIconRotation, 0, 0, 1);
279 
280  // Set poligon mode
281  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
282 
283  // set polyfront position on 0,0
284  pfSetPosition(0, 0);
285 
286  // Set polyfront scale to 1
287  pfSetScale(1.f);
288 
289  // traslate matrix
290  glTranslated(1.2, 0, 0);
291 
292  // draw charging power
293  pfDrawString((toString(myChargingPower) + " W").c_str());
294 
295  // pop charging power matrix
296  glPopMatrix();
297 
298  // Set position over sign
299  glTranslated(mySignPos.x(), mySignPos.y(), 0);
300 
301  // Define nš points (for efficiency)
302  int noPoints = 9;
303 
304  // If the scale * exaggeration is more than 25, recalculate nš points
305  if (s.scale * exaggeration > 25) {
306  noPoints = MIN2((int)(9.0 + (s.scale * exaggeration) / 10.0), 36);
307  }
308 
309  // Scale matrix
310  glScaled(exaggeration, exaggeration, 1);
311 
312  // Set base color
313  if (isAdditionalSelected()) {
315  } else {
317  }
318 
319  // Draw extern
320  GLHelper::drawFilledCircle((SUMOReal) 1.1, noPoints);
321 
322  // Move to top
323  glTranslated(0, 0, .1);
324 
325  // Set sign color
326  if (isAdditionalSelected()) {
328  } else {
330  }
331  // Draw internt sign
332  GLHelper::drawFilledCircle((SUMOReal) 0.9, noPoints);
333 
334  // Draw sign 'C'
335  if (s.scale * exaggeration >= 4.5) {
336  if (isAdditionalSelected()) {
338  } else {
340  }
341  }
342  // Pop sign matrix
343  glPopMatrix();
344 
345  // Draw icon
347  }
348 
349  // Pop base matrix
350  glPopMatrix();
351 
352  // Pop name matrix
353  glPopName();
354 
355  // Draw name
356  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
357 }
358 
359 
360 std::string
362  switch (key) {
363  case SUMO_ATTR_ID:
364  return getAdditionalID();
365  case SUMO_ATTR_LANE:
367  case SUMO_ATTR_STARTPOS:
368  return toString(myStartPos);
369  case SUMO_ATTR_ENDPOS:
370  return toString(myEndPos);
372  return toString(myChargingPower);
374  return toString(myEfficiency);
376  return toString(myChargeInTransit);
378  return toString(myChargeDelay);
379  default:
380  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
381  }
382 }
383 
384 
385 void
386 GNEChargingStation::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
387  if (value == getAttribute(key)) {
388  return; //avoid needless changes, later logic relies on the fact that attributes have changed
389  }
390  switch (key) {
391  case SUMO_ATTR_ID:
392  case SUMO_ATTR_LANE:
393  case SUMO_ATTR_STARTPOS:
394  case SUMO_ATTR_ENDPOS:
399  undoList->p_add(new GNEChange_Attribute(this, key, value));
400  break;
401  default:
402  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
403  }
404 }
405 
406 
407 bool
408 GNEChargingStation::isValid(SumoXMLAttr key, const std::string& value) {
409  switch (key) {
410  case SUMO_ATTR_ID:
411  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
412  return true;
413  } else {
414  return false;
415  }
416  case SUMO_ATTR_LANE:
417  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
418  return true;
419  } else {
420  return false;
421  }
422  case SUMO_ATTR_STARTPOS:
423  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0 && parse<SUMOReal>(value) < (myEndPos - 1));
424  case SUMO_ATTR_ENDPOS:
425  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 1 && parse<SUMOReal>(value) > myStartPos);
427  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
429  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0 && parse<SUMOReal>(value) <= 1);
431  return canParse<bool>(value);
433  return (canParse<int>(value) && parse<int>(value) >= 0);
434  default:
435  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
436  }
437 }
438 
439 // ===========================================================================
440 // private
441 // ===========================================================================
442 
443 void
444 GNEChargingStation::setAttribute(SumoXMLAttr key, const std::string& value) {
445  switch (key) {
446  case SUMO_ATTR_ID:
447  setAdditionalID(value);
448  break;
449  case SUMO_ATTR_LANE:
451  break;
452  case SUMO_ATTR_STARTPOS:
453  myStartPos = parse<SUMOReal>(value);
454  updateGeometry();
455  getViewNet()->update();
456  break;
457  case SUMO_ATTR_ENDPOS:
458  myEndPos = parse<SUMOReal>(value);
459  updateGeometry();
460  getViewNet()->update();
461  break;
463  myChargingPower = parse<SUMOReal>(value);
464  break;
466  myEfficiency = parse<SUMOReal>(value);
467  break;
469  if (value == "true") {
470  myChargeInTransit = true;
471  } else {
472  myChargeInTransit = false;
473  }
474  break;
476  myChargeDelay = parse<int>(value);
477  break;
478  default:
479  throw InvalidArgument(toString(getType()) + "attribute '" + toString(key) + "' not allowed");
480  }
481 }
482 
483 /****************************************************************************/
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
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.
Stores the information about how to visualize structures.
bool myRotationLefthand
rotation depending of the option "Lefthand"
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:221
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::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 getPositionRelativeToParametricLenght(SUMOReal position) const
Definition: GNELane.cpp:530
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:658
void setBlockIconRotation(GNELane *lane=NULL)
GNELane * myLane
The lane this stopping place belongs.
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
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:69
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
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
bool myChargeInTransit
enable or disable charge in transit
std::string getAttribute(SumoXMLAttr key) const
Definition: GNELane.cpp:601
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1093
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:456
void changeLane(GNELane *newLane)
change lane of stopping place
std::string getAttribute(SumoXMLAttr key) const
SUMOReal myChargingPower
Charging power pro timestep.
void pfSetScale(SUMOReal s)
Definition: polyfonts.c:465
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)
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:845
Position getLineCenter() const
get line center
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.
SumoXMLTag getTag() const
get 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
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
void setChargingPower(SUMOReal chargingPower)
Set a new charging power in the charging station.
Position mySignPos
The position of the sign.
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)