SUMO - Simulation of Urban MObility
GNEContainerStop.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 "GNEContainerStop.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 // CHECK_MEMORY_LEAKS
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
66 
67 GNEContainerStop::GNEContainerStop(const std::string& id, GNELane* lane, GNEViewNet* viewNet, SUMOReal startPos, SUMOReal endPos, const std::vector<std::string>& lines, bool blocked) :
68  GNEStoppingPlace(id, viewNet, SUMO_TAG_CONTAINER_STOP, lane, startPos, endPos, blocked),
69  myLines(lines) {
70  // When a new additional element is created, updateGeometry() must be called
72  // Set colors
73  myBaseColor = RGBColor(83, 89, 172, 255);
74  myBaseColorSelected = RGBColor(103, 109, 192, 255);
75  mySignColor = RGBColor(177, 184, 186, 171);
76  mySignColorSelected = RGBColor(197, 204, 206, 171);
77  myTextColor = RGBColor(83, 89, 172, 255);
78  myTextColorSelected = RGBColor(103, 109, 192, 255);
79 }
80 
81 
83 
84 
85 void
87  // Clear all containers
88  myShapeRotations.clear();
89  myShapeLengths.clear();
90 
91  // Get value of option "lefthand"
92  SUMOReal offsetSign = OptionsCont::getOptions().getBool("lefthand") ? -1 : 1;
93 
94  // Get shape of lane parent
95  myShape = myLane->getShape();
96 
97  // Move shape to side
98  myShape.move2side(1.65 * offsetSign);
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  tmpShape.move2side(1.5 * offsetSign);
135 
136  // Get position of the sign
137  mySignPos = tmpShape.getLineCenter();
138 
139  // Set block icon position
141 
142  // Set block icon rotation, and using their rotation for sign
144 
145  // Refresh element (neccesary to avoid grabbing problems)
147 }
148 
149 
150 void
151 GNEContainerStop::writeAdditional(OutputDevice& device, const std::string&) {
152  // Write parameters
153  device.openTag(getTag());
154  device.writeAttr(SUMO_ATTR_ID, getID());
155  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
158  if (myLines.size() > 0) {
160  }
161  if (myBlocked) {
163  }
164  // Close tag
165  device.closeTag();
166 }
167 
168 
169 std::vector<std::string>
171  return myLines;
172 }
173 
174 
175 void
177  // Start drawing adding an gl identificator
178  glPushName(getGlID());
179 
180  // Add a draw matrix
181  glPushMatrix();
182 
183  // Start with the drawing of the area traslating matrix to origin
184  glTranslated(0, 0, getType());
185 
186  // Set color of the base
187  if (isAdditionalSelected()) {
189  } else {
191  }
192 
193  // Obtain exaggeration of the draw
194  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
195 
196  // Draw the area using shape, shapeRotations, shapeLenghts and value of exaggeration
198 
199  // Check if the distance is enought to draw details
200  if (s.scale * exaggeration >= 10) {
201 
202  // Add a draw matrix
203  glPushMatrix();
204 
205  // Obtain rotation of the sing depeding of the option "lefthand"
206  SUMOReal rotSign = OptionsCont::getOptions().getBool("lefthand");
207 
208  // Set color of the lines
209  if (isAdditionalSelected()) {
211  } else {
213  }
214 
215  // Iterate over every line
216  for (int i = 0; i < (int)myLines.size(); ++i) {
217  // Add a new push matrix
218  glPushMatrix();
219 
220  // Traslate End positionof signal
221  glTranslated(mySignPos.x(), mySignPos.y(), 0);
222 
223  // Rotate 180 (Eje X -> Mirror)
224  glRotated(180, 1, 0, 0);
225 
226  // Rotate again depending of the option rotSign
227  glRotated(rotSign * myBlockIconRotation, 0, 0, 1);
228 
229  // Set poligon mode
230  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
231 
232  // set polyfront position ot 0
233  pfSetPosition(0, 0);
234 
235  // Set polyfront scale to 1
236  pfSetScale(1.f);
237 
238  // traslate matrix for every line
239  glTranslated(1.2, -(double)i, 0);
240 
241  // draw line
242  pfDrawString(myLines[i].c_str());
243 
244  // pop matrix
245  glPopMatrix();
246  }
247 
248  // Start drawing sign traslating matrix to signal position
249  glTranslated(mySignPos.x(), mySignPos.y(), 0);
250 
251  // Define nš points (for efficiency)
252  int noPoints = 9;
253 
254  // If the scale * exaggeration is more than 25, recalculate nš points
255  if (s.scale * exaggeration > 25) {
256  noPoints = MIN2((int)(9.0 + (s.scale * exaggeration) / 10.0), 36);
257  }
258 
259  // scale matrix depending of the exaggeration
260  glScaled(exaggeration, exaggeration, 1);
261 
262  // Draw green circle
263  GLHelper::drawFilledCircle((SUMOReal) 1.1, noPoints);
264 
265  // Traslate to front
266  glTranslated(0, 0, .1);
267 
268  // Set color of the lines
269  if (isAdditionalSelected()) {
271  } else {
273  }
274 
275  // draw another circle in the same position, but a little bit more small
276  GLHelper::drawFilledCircle((SUMOReal) 0.9, noPoints);
277 
278  // If the scale * exageration is equal or more than 4.5, draw H
279  if (s.scale * exaggeration >= 4.5) {
280  if (isAdditionalSelected()) {
282  } else {
284  }
285  }
286 
287  // pop draw matrix
288  glPopMatrix();
289 
290  // Show Lock icon depending of the Edit mode
291  drawLockIcon();
292  }
293 
294  // pop draw matrix
295  glPopMatrix();
296 
297  // Draw name
298  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
299 
300  // Pop name
301  glPopName();
302 }
303 
304 
305 std::string
307  switch (key) {
308  case SUMO_ATTR_ID:
309  return getAdditionalID();
310  case SUMO_ATTR_LANE:
312  case SUMO_ATTR_STARTPOS:
313  return toString(myStartPos);
314  case SUMO_ATTR_ENDPOS:
315  return toString(myEndPos);
316  case SUMO_ATTR_LINES:
317  return joinToString(myLines, " ");
319  return toString(myBlocked);
320  default:
321  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
322  }
323 }
324 
325 
326 void
327 GNEContainerStop::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
328  if (value == getAttribute(key)) {
329  return; //avoid needless changes, later logic relies on the fact that attributes have changed
330  }
331  switch (key) {
332  case SUMO_ATTR_ID:
333  case SUMO_ATTR_LANE:
334  case SUMO_ATTR_STARTPOS:
335  case SUMO_ATTR_ENDPOS:
336  case SUMO_ATTR_LINES:
338  undoList->p_add(new GNEChange_Attribute(this, key, value));
339  updateGeometry();
340  break;
341  default:
342  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
343  }
344 }
345 
346 
347 bool
348 GNEContainerStop::isValid(SumoXMLAttr key, const std::string& value) {
349  switch (key) {
350  case SUMO_ATTR_ID:
351  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
352  return true;
353  } else {
354  return false;
355  }
356  case SUMO_ATTR_LANE:
357  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
358  return true;
359  } else {
360  return false;
361  }
362  case SUMO_ATTR_STARTPOS:
363  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0 && parse<SUMOReal>(value) < (myEndPos - 1));
364  case SUMO_ATTR_ENDPOS: {
365  if (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 1 && parse<SUMOReal>(value) > myStartPos) {
366  // If extension is larger than Lane
367  if (parse<SUMOReal>(value) > myLane->getLaneParametricLenght()) {
368  // Ask user if want to assign the lenght of lane as endPosition
369  FXuint answer = FXMessageBox::question(getViewNet()->getApp(), MBOX_YES_NO,
370  "EndPosition exceeds the size of the lane", "%s",
371  "EndPosition exceeds the size of the lane. You want to assign the size of the lane as endPosition?");
372  if (answer == 1) { //1:yes, 2:no, 4:esc
373  return true;
374  } else {
375  return false;
376  }
377  } else {
378  return true;
379  }
380  } else {
381  return false;
382  }
383  }
384  case SUMO_ATTR_LINES:
385  return isValidStringVector(value);
387  return canParse<bool>(value);
388  default:
389  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
390  }
391 }
392 
393 // ===========================================================================
394 // private
395 // ===========================================================================
396 
397 void
398 GNEContainerStop::setAttribute(SumoXMLAttr key, const std::string& value) {
399  switch (key) {
400  case SUMO_ATTR_ID:
401  setAdditionalID(value);
402  break;
403  case SUMO_ATTR_LANE:
404  changeLane(value);
405  break;
406  case SUMO_ATTR_STARTPOS:
407  myStartPos = parse<SUMOReal>(value);
408  updateGeometry();
409  getViewNet()->update();
410  break;
411  case SUMO_ATTR_ENDPOS:
412  if (parse<SUMOReal>(value) > myLane->getLaneParametricLenght()) {
414  } else {
415  myEndPos = parse<SUMOReal>(value);
416  }
417  updateGeometry();
418  getViewNet()->update();
419  break;
420  case SUMO_ATTR_LINES:
421  myLines.clear();
423  getViewNet()->update();
424  break;
426  myBlocked = parse<bool>(value);
427  getViewNet()->update();
428  break;
429  default:
430  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
431  }
432 }
433 
434 /****************************************************************************/
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
~GNEContainerStop()
Destructor.
GUIVisualizationTextSettings addName
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
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.
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
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:55
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
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.
std::vector< std::string > getLines() const
get string vector with the lines of the busStop
std::string getAttribute(SumoXMLAttr key) const
SUMOReal getLaneParametricLenght() const
returns the parameteric length of the lane
Definition: GNELane.cpp:681
Position getLineCenter() const
get line center
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
SUMOReal scale
information about a lane&#39;s width (temporary, used for a single view)
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
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
#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
void updateGeometry()
update pre-computed geometry information
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)
std::vector< std::string > myLines
The list of lines that are assigned to this stop.
friend class GNEChange_Attribute
declare friend class
RGBColor myTextColor
Text color (Default cyan)
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 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
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) ...
static bool isValidStringVector(const std::string &value)
true if value is a valid string vector
void pfSetScale(SUMOReal s)
Definition: polyfonts.c:465
SUMOReal getPositionRelativeToParametricLenght(SUMOReal position) const
Definition: GNELane.cpp:693
RGBColor myBaseColor
base color (Default green)
std::vector< SUMOReal > myShapeRotations
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:912
GUIGlID getGlID() const
Returns the numerical id of the object.
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:188
void move2side(SUMOReal amount)
move position vector to side using certain ammount
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
GNEContainerStop(const std::string &id, GNELane *lane, GNEViewNet *viewNet, SUMOReal startPos, SUMOReal endPos, const std::vector< std::string > &lines, bool blocked)
Constructor.
bool isAdditionalSelected() const
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)
void writeAdditional(OutputDevice &device, const std::string &)
writte additional element into a xml file
Position myBlockIconPosition
position of the block icon
SumoXMLTag getTag() const
get Tag assigned to this object