SUMO - Simulation of Urban MObility
GNEAdditionalSet.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 "GNEAdditionalSet.h"
51 #include "GNEAdditional.h"
52 #include "GNEUndoList.h"
53 #include "GNELane.h"
54 #include "GNEEdge.h"
55 #include "GNENet.h"
56 #include "GNEViewNet.h"
57 
58 #ifdef CHECK_MEMORY_LEAKS
59 #include <foreign/nvwa/debug_new.h>
60 #endif
61 
62 
63 // ===========================================================================
64 // member method definitions
65 // ===========================================================================
66 
67 GNEAdditionalSet::GNEAdditionalSet(const std::string& id, GNEViewNet* viewNet, Position pos, SumoXMLTag tag, bool blocked, std::vector<GNEAdditional*> additionalChilds, std::vector<GNEEdge*> edgeChilds, std::vector<GNELane*> laneChilds) :
68  GNEAdditional(id, viewNet, pos, tag, NULL, blocked) {
69  // Insert additionals
70  for (int i = 0; i < (int)additionalChilds.size(); i++) {
71  addAdditionalChild(additionalChilds.at(i));
72  }
73  // Insert edges
74  for (int i = 0; i < (int)edgeChilds.size(); i++) {
75  edgeChilds.at(i)->addAdditionalSet(this);
76  edgeChild myEdgeChild;
77  myEdgeChild.edge = edgeChilds.at(i);
78  myChildEdges.push_back(myEdgeChild);
79  }
80  // Insert lanes
81  for (int i = 0; i < (int)laneChilds.size(); i++) {
82  laneChilds.at(i)->addAdditionalSet(this);
83  laneChild myLaneChild;
84  myLaneChild.lane = laneChilds.at(i);
85  myChildLanes.push_back(myLaneChild);
86  }
87  // Update connections
89 }
90 
91 
93  // Remove references to this additional Set in edges
94  childEdges childEdgesToRemove = myChildEdges;
95  for (childEdges::iterator i = childEdgesToRemove.begin(); i != childEdgesToRemove.end(); i++) {
96  (i->edge->removeAdditionalGeometrySet(this));
97  }
98  // Remove references to this additional Set in lanes
99  childLanes childLanesToRemove = myChildLanes;
100  for (childLanes::iterator i = childLanesToRemove.begin(); i != childLanesToRemove.end(); i++) {
101  (i->lane->removeAdditionalGeometrySet(this));
102  }
103 }
104 
105 
106 bool
108  for (childAdditionals::iterator i = myChildAdditionals.begin(); i != myChildAdditionals.end(); i++) {
109  if ((*i) == additional) {
110  return false;
111  }
112  }
113  // If wasn't found, insert it
114  myChildAdditionals.push_back(additional);
115  return true;
116 }
117 
118 
119 bool
121  for (childAdditionals::iterator i = myChildAdditionals.begin(); i != myChildAdditionals.end(); i++) {
122  if ((*i) == additional) {
123  myChildAdditionals.erase(i);
125  return true;
126  }
127  }
128  // If wasn't found, return false
129  return false;
130 }
131 
132 
133 bool
135  for (childEdges::iterator i = myChildEdges.begin(); i != myChildEdges.end(); i++) {
136  if (i->edge == edge) {
137  return false;
138  }
139  }
140  // If wasn't found, insert it
141  edgeChild myEdgeChild;
142  myEdgeChild.edge = edge;
143  myChildEdges.push_back(myEdgeChild);
145  return true;
146 }
147 
148 
149 bool
151  for (childEdges::iterator i = myChildEdges.begin(); i != myChildEdges.end(); i++) {
152  if (i->edge == edge) {
153  myChildEdges.erase(i);
155  return true;
156  }
157  }
158  // If wasn't found, return false
159  return false;
160 }
161 
162 
163 bool
165  for (childLanes::iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
166  if (i->lane == lane) {
167  return false;
168  }
169  }
170  // If wasn't found, insert it
171  laneChild myLaneChild;
172  myLaneChild.lane = lane;
173  myChildLanes.push_back(myLaneChild);
175  return true;
176 }
177 
178 
179 bool
181  for (childLanes::iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
182  if (i->lane == lane) {
183  myChildLanes.erase(i);
185  return true;
186  }
187  }
188  // If wasn't found, return false
189  return false;
190 }
191 
192 
193 void
195  // Clear map with the middle positions
197 
198  // Iterate over additonals
199  for (childAdditionals::iterator i = myChildAdditionals.begin(); i != myChildAdditionals.end(); i++) {
200  Position PositionOfChild = (*i)->getPositionInView();
201  SUMOReal angleBetweenParentAndChild = myPosition.angleTo2D(PositionOfChild);
202  SUMOReal distancieBetweenParentAndChild = myPosition.distanceTo2D(PositionOfChild);
203  // Calculate middle point
204  myConnectionMiddlePosition[*i] = Position(myPosition.x() + cos(angleBetweenParentAndChild) * distancieBetweenParentAndChild, myPosition.y());
205  }
206 
207  // Iterate over eges
208  for (childEdges::iterator i = myChildEdges.begin(); i != myChildEdges.end(); i++) {
209  // clear position of lanes and rotations
210  i->positionsOverLanes.clear();
211  i->rotationsOverLanes.clear();
212  // Calculate position and rotation of every lane
213  for (int j = 0; j < (int)i->edge->getLanes().size(); j++) {
214  i->positionsOverLanes.push_back(i->edge->getLanes().at(j)->getShape().positionAtOffset(i->edge->getLanes().at(j)->getShape().length() - 10));
215  i->rotationsOverLanes.push_back(i->edge->getLanes().at(j)->getShape().rotationDegreeAtOffset(i->edge->getLanes().at(j)->getShape().length() - 10) * -1);
216  }
217  // Calculate middle position of lanes
218  Position middlePoint((i->positionsOverLanes.front().x() + i->positionsOverLanes.back().x()) / 2, (i->positionsOverLanes.front().y() + i->positionsOverLanes.back().y()) / 2);
219  // Set position of connection
220  SUMOReal angleBetweenParentAndChild = myPosition.angleTo2D(middlePoint);
221  SUMOReal distancieBetweenParentAndChild = myPosition.distanceTo2D(middlePoint);
222  myConnectionMiddlePosition[i->edge] = Position(myPosition.x() + cos(angleBetweenParentAndChild) * distancieBetweenParentAndChild, myPosition.y());
223  }
224 
225  // Iterate over lanes
226  for (childLanes::iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
227  // Calculate position and rotation of every lane
228  i->positionOverLane = i->lane->getShape().positionAtOffset(i->lane->getShape().length() - 10);
229  i->rotationOverLane = i->lane->getShape().rotationDegreeAtOffset(i->lane->getShape().length() - 10) * -1;
230  // Set position of connection
231  SUMOReal angleBetweenParentAndChild = myPosition.angleTo2D(i->positionOverLane);
232  SUMOReal distancieBetweenParentAndChild = myPosition.distanceTo2D(i->positionOverLane);
233  myConnectionMiddlePosition[i->lane] = Position(myPosition.x() + cos(angleBetweenParentAndChild) * distancieBetweenParentAndChild, myPosition.y());
234  }
235 }
236 
237 
238 void
240  // Iterate over list of additionals
241  for (childAdditionals::const_iterator i = myChildAdditionals.begin(); i != myChildAdditionals.end(); i++) {
242  // Add a draw matrix
243  glPushMatrix();
244  // traslate in the Z axis
245  glTranslated(0, 0, getType() - 0.01);
246  // Set color of the base
247  GLHelper::setColor(RGBColor(255, 235, 0, 255));
248  // Draw Line
250  GLHelper::drawLine(myConnectionMiddlePosition.at(*i), (*i)->getPositionInView());
251  // Pop draw matrix
252  glPopMatrix();
253  }
254  // Iterate over edges
255  for (childEdges::const_iterator i = myChildEdges.begin(); i != myChildEdges.end(); i++) {
256  // Add a draw matrix
257  glPushMatrix();
258  // traslate in the Z axis
259  glTranslated(0, 0, getType() - 0.01);
260  // Set color of the base
261  GLHelper::setColor(RGBColor(255, 235, 0, 255));
262  // Calculate middle point between lanes
263  /*** @todo ADD DISTANCE AS PARAMETER **/
264  Position middlePoint((i->positionsOverLanes.front().x() + i->positionsOverLanes.back().x()) / 2, (i->positionsOverLanes.front().y() + i->positionsOverLanes.back().y()) / 2);
265  // Draw Line
267  GLHelper::drawLine(myConnectionMiddlePosition.at(i->edge), middlePoint);
268  // Pop draw matrix
269  glPopMatrix();
270  }
271  // Iterate over lanes
272  for (childLanes::const_iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
273  // Add a draw matrix
274  glPushMatrix();
275  // traslate in the Z axis
276  glTranslated(0, 0, getType() - 0.01);
277  // Set color of the base
278  GLHelper::setColor(RGBColor(255, 235, 0, 255));
279  // Draw Line
281  GLHelper::drawLine(myConnectionMiddlePosition.at(i->lane), i->positionOverLane);
282  // Pop draw matrix
283  glPopMatrix();
284  }
285 }
286 
287 
288 void
289 GNEAdditionalSet::writeAdditionalChildrens(OutputDevice& device, const std::string& currentDirectory) {
290  for (childAdditionals::iterator i = myChildAdditionals.begin(); i != myChildAdditionals.end(); i++) {
291  (*i)->writeAdditional(device, currentDirectory);
292  }
293 }
294 
295 
296 int
298  return int(myChildAdditionals.size());
299 }
300 
301 
302 int
304  return int(myChildEdges.size());
305 }
306 
307 
308 int
310  return int(myChildLanes.size());
311 }
312 
313 
314 std::vector<std::string>
316  // Declare and resize vector
317  std::vector<std::string> vectorOfAdditionalsIds;
318  vectorOfAdditionalsIds.resize(myChildAdditionals.size());
319  // Save Ids
320  for (int i = 0; i < (int)myChildAdditionals.size(); i++) {
321  vectorOfAdditionalsIds[i] = myChildAdditionals.at(i)->getID();
322  }
323  return vectorOfAdditionalsIds;
324 }
325 
326 
327 std::vector<std::string>
329  // Declare and resize vector
330  std::vector<std::string> vectorOfEdgesIds;
331  vectorOfEdgesIds.resize(myChildEdges.size());
332  // Save Ids
333  for (int i = 0; i < (int)myChildEdges.size(); i++) {
334  vectorOfEdgesIds[i] = myChildEdges.at(i).edge->getID();
335  }
336  return vectorOfEdgesIds;
337 }
338 
339 
340 std::vector<std::string>
342  // Declare and resize vector
343  std::vector<std::string> vectorOfLanesIds;
344  vectorOfLanesIds.resize(myChildLanes.size());
345  // Save Ids
346  for (int i = 0; i < (int)myChildLanes.size(); i++) {
347  vectorOfLanesIds[i] = myChildLanes.at(i).lane->getID();
348  }
349  return vectorOfLanesIds;
350 }
351 
352 
353 void
354 GNEAdditionalSet::setEdgeChilds(std::vector<GNEEdge*> edges) {
355  // First remove all existent edges
356  for (childEdges::iterator i = myChildEdges.begin(); i != myChildEdges.end(); i++) {
357  i->edge->removeAdditionalGeometrySet(this);
358  }
359  // clear edge childs vector
360  myChildEdges.clear();
361  // Iterate over vector of new edges
362  for (std::vector<GNEEdge*>::iterator i = edges.begin(); i != edges.end(); i++) {
363  (*i)->addAdditionalSet(this);
364  addEdgeChild(*i);
365  }
366  // Update geometry
367  updateGeometry();
368 }
369 
370 
371 void
372 GNEAdditionalSet::setLaneChilds(std::vector<GNELane*> lanes) {
373  // First remove all existent lanes
374  for (childLanes::iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
375  i->lane->removeAdditionalGeometrySet(this);
376  }
377  // clear lane childs vector
378  myChildLanes.clear();
379  // Iterate over vector of new lanes
380  for (std::vector<GNELane*>::iterator i = lanes.begin(); i != lanes.end(); i++) {
381  (*i)->addAdditionalSet(this);
382  addLaneChild(*i);
383  }
384  // Update geometry
385  updateGeometry();
386 }
387 
388 /****************************************************************************/
bool removeLaneChild(GNELane *lane)
remove lane element to this set
bool addEdgeChild(GNEEdge *edge)
add edge element to this set
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::vector< std::string > getEdgeChildIds() const
get ids of edge childs
bool addLaneChild(GNELane *lane)
add lane element to this set
~GNEAdditionalSet()
Destructor.
SUMOReal angleTo2D(const Position &other) const
returns the angle in the plane of the vector pointing from here to the other position ...
Definition: Position.h:243
int getNumberOfLaneChilds() const
get number of lane childs of this additionalSet
int getNumberOfEdgeChilds() const
get number of edge childs of this additionalSet
GNEAdditionalSet(const std::string &id, GNEViewNet *viewNet, Position pos, SumoXMLTag tag, bool blocked=false, std::vector< GNEAdditional *> additionalChilds=std::vector< GNEAdditional *>(), std::vector< GNEEdge *> edgeChilds=std::vector< GNEEdge *>(), std::vector< GNELane *> laneChilds=std::vector< GNELane *>())
Constructor.
childEdges myChildEdges
map of child edges and their positions and rotation
childLanes myChildLanes
list of child lanes and their positions and rotation
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:55
struct for edge childs
bool removeEdgeChild(GNEEdge *edge)
remove edge element to this set
SUMOReal distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:232
std::map< GUIGlObject *, Position > myConnectionMiddlePosition
map to keep the middle position of connection
void updateConnections()
update connections.
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:443
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
bool removeAdditionalGeometryChild(GNEAdditional *additional)
remove additional element to this set
bool addAdditionalChild(GNEAdditional *additional)
add additional element to this set
void drawConnections() const
draw connections.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
struct for lane childs
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:55
std::vector< GNEAdditional * > myChildAdditionals
list of additional childs (Position and rotations is derived from additional)
void setLaneChilds(std::vector< GNELane *> lanes)
set lane childs
void setEdgeChilds(std::vector< GNEEdge *> edges)
set edge childs
std::vector< std::string > getAdditionalChildIds() const
get ids of additional childs
An Element which don&#39;t belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:63
virtual void updateGeometry()=0
update pre-computed geometry information
std::vector< std::string > getLaneChildIds() const
get ids of lane childs
std::vector< laneChild > childLanes
void writeAdditionalChildrens(OutputDevice &device, const std::string &currentDirectory)
writte children of this additionalSet
static void drawLine(const Position &beg, SUMOReal rot, SUMOReal visLength)
Draws a thin line.
Definition: GLHelper.cpp:269
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
Position myPosition
The position in which this additional element is located.
#define SUMOReal
Definition: config.h:213
int getNumberOfAdditionalChilds() const
get number of additional childs of this additionalSet
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
std::vector< edgeChild > childEdges