Eclipse SUMO - Simulation of Urban MObility
GNEPathElements.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // A abstract class for representation of element paths
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <netedit/GNENet.h>
23 
24 #include "GNEPathElements.h"
25 
26 
27 // ===========================================================================
28 // member method definitions
29 // ===========================================================================
30 
31 // ---------------------------------------------------------------------------
32 // GNEPathElements::PathElement - methods
33 // ---------------------------------------------------------------------------
34 
36  myLane(lane),
37  myNextLane(nullptr) {
38 }
39 
40 
41 void
43  myNextLane = lane;
44 }
45 
46 
49  if (myNextLane) {
50  return myLane->getParentEdge()->getParentJunctions().back();
51  } else {
52  return nullptr;
53  }
54 }
55 
56 
57 GNELane*
59  return myLane;
60 }
61 
62 
63 GNELane*
65  return myNextLane;
66 }
67 
68 
70  myLane(nullptr),
71  myNextLane(nullptr) {
72 }
73 
74 // ---------------------------------------------------------------------------
75 // GNEPathElements - methods
76 // ---------------------------------------------------------------------------
77 
79  myAdditionalElement(additional),
80  myDemandElement(nullptr),
81  myGenericData(nullptr) {
82 }
83 
84 
86  myAdditionalElement(nullptr),
87  myDemandElement(demandElement),
88  myGenericData(nullptr) {
89 }
90 
91 
93  myAdditionalElement(nullptr),
94  myDemandElement(nullptr),
95  myGenericData(genericData) {
96 }
97 
98 
100 
101 
102 const std::vector<GNEPathElements::PathElement>&
104  return myPathElements;
105 }
106 
107 
108 void
109 GNEPathElements::drawLanePathChildren(const GUIVisualizationSettings& s, const GNELane* lane, const double offset) const {
110  // additionals
111  if (myAdditionalElement) {
112  for (const auto& pathElement : myPathElements) {
113  if (pathElement.getLane() == lane) {
114  myAdditionalElement->drawPartialGL(s, lane, offset);
115  }
116  }
117  }
118  // demand elements
119  if (myDemandElement) {
120  for (const auto& pathElement : myPathElements) {
121  if (pathElement.getLane() == lane) {
122  myDemandElement->drawPartialGL(s, lane, offset);
123  }
124  }
125  }
126  // generic datas (only in supermode Data)
128  for (const auto& pathElement : myPathElements) {
129  if (pathElement.getLane() == lane) {
130  myGenericData->drawPartialGL(s, lane, offset);
131  }
132  }
133  }
134 }
135 
136 
137 void
138 GNEPathElements::drawJunctionPathChildren(const GUIVisualizationSettings& s, const GNEJunction* junction, const double offset) const {
139  // additionals
140  if (myAdditionalElement) {
141  for (const auto& pathElement : myPathElements) {
142  if (pathElement.getJunction() == junction) {
143  myAdditionalElement->drawPartialGL(s, pathElement.getLane(), pathElement.getNextLane(), offset);
144  }
145  }
146  }
147  // demand elements
148  if (myDemandElement) {
149  for (const auto& pathElement : myPathElements) {
150  if (pathElement.getJunction() == junction) {
151  myDemandElement->drawPartialGL(s, pathElement.getLane(), pathElement.getNextLane(), offset);
152  }
153  }
154  }
155  // generic datas
157  for (const auto& pathElement : myPathElements) {
158  if (pathElement.getJunction() == junction) {
159  myGenericData->drawPartialGL(s, pathElement.getLane(), pathElement.getNextLane(), offset);
160  }
161  }
162  }
163 }
164 
165 // ---------------------------------------------------------------------------
166 // GNEPathElements - protected methods
167 // ---------------------------------------------------------------------------
168 
169 void
170 GNEPathElements::calculatePathLanes(SUMOVehicleClass vClass, const bool allowedVClass, GNELane* fromLane, GNELane* toLane, const std::vector<GNEEdge*>& /* viaEdges */) {
171  // check if from and to lane are valid
172  if (fromLane && toLane) {
173  // remove path elements from lanes and junctions
174  removeElements();
175  // get from-via-to edges
176  const std::vector<GNEEdge*> edges = calculateFromViaToEdges(fromLane, toLane, edges);
177  // calculate path
178  const std::vector<GNEEdge*> path = myDemandElement->getNet()->getPathCalculator()->calculatePath(vClass, edges);
179  // set new path lanes
180  myPathElements.clear();
181  // check if path was sucesfully calculated
182  if (path.size() > 0) {
183  for (int i = 0; i < (int)path.size(); i++) {
184  if (i == 0) {
185  myPathElements.push_back(fromLane);
186  } else if (i == (int)path.size()) {
187  myPathElements.push_back(toLane);
188  } else if (allowedVClass) {
189  myPathElements.push_back(path.at(i)->getLaneByAllowedVClass(vClass));
190  } else {
191  myPathElements.push_back(path.at(i)->getLaneByDisallowedVClass(vClass));
192  }
193  }
194  } else {
195  myPathElements = {fromLane, toLane};
196  }
197  // update path elements
199  // add path elements in lanes and junctions
200  addElements();
201  }
202 }
203 
204 
205 void
206 GNEPathElements::calculateConsecutivePathLanes(SUMOVehicleClass vClass, const bool allowedVClass, const std::vector<GNEEdge*>& edges) {
207  // remove path elements from lanes and junctions
208  removeElements();
209  // set new paht lanes
210  myPathElements.clear();
211  // use edges as path elements
212  for (const auto& edge : edges) {
213  if (allowedVClass) {
214  myPathElements.push_back(edge->getLaneByAllowedVClass(vClass));
215  } else {
216  myPathElements.push_back(edge->getLaneByDisallowedVClass(vClass));
217  }
218  }
219  // update path elements
221  // add path elements in lanes and junctions
222  addElements();
223 }
224 
225 
226 void
227 GNEPathElements::calculateConsecutivePathLanes(const std::vector<GNELane*>& lanes) {
228  // remove path elements from lanes and junctions
229  removeElements();
230  // set new route lanes
231  myPathElements.clear();
232  // use edges as path elements
233  for (const auto& lane : lanes) {
234  myPathElements.push_back(lane);
235  }
236  // update path elements
238  // add path elements in lanes and junctions
239  addElements();
240 }
241 
242 
243 void
244 GNEPathElements::resetPathLanes(SUMOVehicleClass vClass, const bool allowedVClass, GNELane* fromLane, GNELane* toLane, const std::vector<GNEEdge*>& /* viaEdges */) {
245  // check if from and to lane are valid
246  if (fromLane && toLane) {
247  // remove path elements from lanes and junctions
248  removeElements();
249  // get from-via-to edges
250  const std::vector<GNEEdge*> edges = calculateFromViaToEdges(fromLane, toLane, edges);
251  // set new route lanes
252  myPathElements.clear();
253  // use edges as path elements
254  if (edges.size() > 0) {
255  for (int i = 0; i < (int)edges.size(); i++) {
256  if (i == 0) {
257  myPathElements.push_back(fromLane);
258  } else if (i == (int)edges.size()) {
259  myPathElements.push_back(toLane);
260  } else if (allowedVClass) {
261  myPathElements.push_back(edges.at(i)->getLaneByAllowedVClass(vClass));
262  } else {
263  myPathElements.push_back(edges.at(i)->getLaneByDisallowedVClass(vClass));
264  }
265  }
266  } else {
267  myPathElements = {fromLane, toLane};
268  }
269  // update path elements
271  // add path elements in lanes and junctions
272  addElements();
273  }
274 }
275 
276 
277 void
278 GNEPathElements::calculateGenericDataLanePath(const std::vector<GNEEdge*>& edges) {
279  // only for demand elements
280  if (myGenericData) {
281  // remove path elements from lanes and junctions
282  removeElements();
283  // clear path
284  myPathElements.clear();
285  // iterate over edge lanes and add it
286  for (const auto& edge : edges) {
287  for (const auto& lane : edge->getLanes()) {
288  myPathElements.push_back(lane);
289  }
290  }
291  // update path elements
293  // add path elements in lanes and junctions
294  addElements();
295  }
296 }
297 
298 
299 void
301  // additionals
302  if (myAdditionalElement) {
303  // add demandElement into parent lanes
304  for (const auto& pathElement : myPathElements) {
305  pathElement.getLane()->addPathAdditionalElement(myAdditionalElement);
306  if (pathElement.getJunction()) {
307  pathElement.getJunction()->addPathAdditionalElement(myAdditionalElement);
308  }
309  }
310  }
311  // demand elements
312  if (myDemandElement) {
313  // add demandElement into parent lanes
314  for (const auto& pathElement : myPathElements) {
315  pathElement.getLane()->addPathDemandElement(myDemandElement);
316  if (pathElement.getJunction()) {
317  pathElement.getJunction()->addPathDemandElement(myDemandElement);
318  }
319  }
320  }
321  // demand elements
322  if (myGenericData) {
323  // add genericData into parent lanes
324  for (const auto& pathElement : myPathElements) {
325  pathElement.getLane()->addPathGenericData(myGenericData);
326  if (pathElement.getJunction()) {
327  pathElement.getJunction()->addPathGenericData(myGenericData);
328  }
329  }
330  }
331 }
332 
333 
334 void
336  // additionals
337  if (myAdditionalElement) {
338  // remove demandElement from parent lanes
339  for (const auto& pathElement : myPathElements) {
340  pathElement.getLane()->removePathAdditionalElement(myAdditionalElement);
341  if (pathElement.getJunction()) {
342  pathElement.getJunction()->removePathAdditionalElement(myAdditionalElement);
343  }
344  }
345  }
346  // demand elements
347  if (myDemandElement) {
348  // remove demandElement from parent lanes
349  for (const auto& pathElement : myPathElements) {
350  pathElement.getLane()->removePathDemandElement(myDemandElement);
351  if (pathElement.getJunction()) {
352  pathElement.getJunction()->removePathDemandElement(myDemandElement);
353  }
354  }
355  }
356  // generic datas
357  if (myGenericData) {
358  // remove genericData from parent lanes
359  for (const auto& pathElement : myPathElements) {
360  pathElement.getLane()->removePathGenericData(myGenericData);
361  if (pathElement.getJunction()) {
362  pathElement.getJunction()->removePathGenericData(myGenericData);
363  }
364  }
365  }
366 }
367 
368 
369 void
371  // update next lanes
372  for (auto i = myPathElements.begin(); i != (myPathElements.end() - 1); i++) {
373  i->updateNextLane((i + 1)->getLane());
374  }
375 }
376 
377 
378 const std::vector<GNEEdge*>
379 GNEPathElements::calculateFromViaToEdges(GNELane* fromLane, GNELane* toLane, const std::vector<GNEEdge*>& viaEdges) {
380  // declare a edge vector
381  std::vector<GNEEdge*> edges;
382  // add from-via-edge lanes
383  edges.push_back(fromLane->getParentEdge());
384  for (const auto& edge : viaEdges) {
385  edges.push_back(edge);
386  }
387  edges.push_back(toLane->getParentEdge());
388  // remove consecutive (adjacent) duplicates
389  edges.erase(std::unique(edges.begin(), edges.end()), edges.end());
390  // return edges
391  return edges;
392 }
393 
394 
396  myAdditionalElement(nullptr),
397  myDemandElement(nullptr),
398  myGenericData(nullptr) {
399 }
400 
401 /****************************************************************************/
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
An Element which don't belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:47
void drawPartialGL(const GUIVisualizationSettings &s, const GNELane *lane, const double offsetFront) const
Draws partial object (lane)
GNENet * getNet() const
get pointer to net
An Element which don't belongs to GNENet but has influency in the simulation.
virtual void drawPartialGL(const GUIVisualizationSettings &s, const GNELane *lane, const double offsetFront) const =0
Draws partial object (lane)
An Element which don't belongs to GNENet but has influency in the simulation.
virtual bool isGenericDataVisible() const =0
check if current generic data is visible
virtual void drawPartialGL(const GUIVisualizationSettings &s, const GNELane *lane, const double offsetFront) const =0
Draws partial object (lane)
const std::vector< GNEJunction * > & getParentJunctions() const
get parent junctions
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:45
GNEEdge * getParentEdge() const
get arent edge
Definition: GNELane.cpp:111
std::vector< GNEEdge * > calculatePath(const SUMOVehicleClass vClass, const std::vector< GNEEdge * > &partialEdges) const
calculate Dijkstra path between a list of partial edges
GNENetHelper::PathCalculator * getPathCalculator()
obtain instance of PathCalculator
Definition: GNENet.cpp:136
GNEJunction * getJunction() const
get junction
PathElement()
default constructor
GNELane * getNextLane() const
get next lane
void updateNextLane(GNELane *lane)
update nextLane
GNELane * getLane() const
get lane
void calculatePathLanes(SUMOVehicleClass vClass, const bool allowedVClass, GNELane *fromLane, GNELane *toLane, const std::vector< GNEEdge * > &viaEdges)
calculate path lanes (Dijkstra)
GNEGenericData * myGenericData
pointer to generic data
void updatePathElements()
update path element
void calculateGenericDataLanePath(const std::vector< GNEEdge * > &edges)
calculate consecutive path lanes (used by genericdatas)
GNEAdditional * myAdditionalElement
pointer to additional element
void removeElements()
remove elements
void drawJunctionPathChildren(const GUIVisualizationSettings &s, const GNEJunction *junction, const double offset) const
draw junction path child
void calculateConsecutivePathLanes(SUMOVehicleClass vClass, const bool allowedVClass, const std::vector< GNEEdge * > &edges)
calculate consecutive path lanes (used by routes)
GNEPathElements()
default constructor
const std::vector< GNEPathElements::PathElement > & getPath() const
get path edges
void drawLanePathChildren(const GUIVisualizationSettings &s, const GNELane *lane, const double offset) const
draw lane path child
~GNEPathElements()
Destructor.
void addElements()
add elements
GNEDemandElement * myDemandElement
pointer to demand element
void resetPathLanes(SUMOVehicleClass vClass, const bool allowedVClass, GNELane *fromLane, GNELane *toLane, const std::vector< GNEEdge * > &viaEdges)
reset path lanes
const std::vector< GNEEdge * > calculateFromViaToEdges(GNELane *fromLane, GNELane *toLane, const std::vector< GNEEdge * > &viaEdges)
calculate from-via-to edges
std::vector< PathElement > myPathElements
vector of edges used in paths
Stores the information about how to visualize structures.