Eclipse SUMO - Simulation of Urban MObility
ROHelper.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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Some helping methods for router
17 /****************************************************************************/
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <functional>
25 #include <vector>
26 #include "ROEdge.h"
27 #include "ROVehicle.h"
28 #include "ROHelper.h"
29 
30 
31 // ===========================================================================
32 // class definitions
33 // ===========================================================================
34 
35 
36 namespace ROHelper {
37 void
39  // for simplicities sake, prevent removal of any mandatory edges
40  // in theory these edges could occur multiple times so it might be possible
41  // to delete some of them anyway.
42  // XXX check for departLane, departPos, departSpeed, ....
43 
44  // removal of edge loops within the route (edge occurs twice)
45  std::map<const ROEdge*, int> lastOccurence; // index of the last occurence of this edge
46  for (int ii = 0; ii < (int)edges.size(); ++ii) {
47  std::map<const ROEdge*, int>::iterator it_pre = lastOccurence.find(edges[ii]);
48  if (it_pre != lastOccurence.end() &&
49  noMandatory(mandatory, edges.begin() + it_pre->second, edges.begin() + ii)) {
50  edges.erase(edges.begin() + it_pre->second, edges.begin() + ii);
51  ii = it_pre->second;
52  } else {
53  lastOccurence[edges[ii]] = ii;
54  }
55  }
56 
57  // remove loops at the route's begin
58  // (vehicle makes a turnaround to get into the right direction at an already passed node)
59  const RONode* start = edges[0]->getFromJunction();
60  int lastStart = 0;
61  for (int i = 1; i < (int)edges.size(); i++) {
62  if (edges[i]->getFromJunction() == start) {
63  lastStart = i;
64  }
65  }
66  if (lastStart > 0 && noMandatory(mandatory, edges.begin(), edges.begin() + lastStart - 1)) {
67  edges.erase(edges.begin(), edges.begin() + lastStart - 1);
68  }
69  // remove loops at the route's end
70  // (vehicle makes a turnaround to get into the right direction at an already passed node)
71  const RONode* end = edges.back()->getToJunction();
72  for (int i = 0; i < (int)edges.size() - 1; i++) {
73  if (edges[i]->getToJunction() == end && noMandatory(mandatory, edges.begin() + i + 2, edges.end())) {
74  edges.erase(edges.begin() + i + 2, edges.end());
75  break;
76  }
77  }
78 
79  // removal of node loops (node occurs twice) is not done because these may occur legitimately
80  /*
81  std::vector<RONode*> nodes;
82  for (ConstROEdgeVector::iterator i = edges.begin(); i != edges.end(); ++i) {
83  nodes.push_back((*i)->getFromJunction());
84  }
85  nodes.push_back(edges.back()->getToJunction());
86  bool changed = false;
87  do {
88  changed = false;
89  for (int b = 0; b < nodes.size() && !changed; ++b) {
90  RONode* bn = nodes[b];
91  for (int e = b + 1; e < nodes.size() && !changed; ++e) {
92  if (bn == nodes[e]) {
93  changed = true;
94  nodes.erase(nodes.begin() + b, nodes.begin() + e);
95  edges.erase(edges.begin() + b, edges.begin() + e);
96  }
97  }
98  }
99  } while (changed);
100  */
101 }
102 
103 bool
105  ConstROEdgeVector::const_iterator start,
106  ConstROEdgeVector::const_iterator end) {
107  for (const ROEdge* m : mandatory) {
108  for (auto it = start; it != end; it++) {
109  if (*it == m) {
110  return false;
111  }
112  }
113  }
114  return true;
115 }
116 
117 
118 }
119 
120 
121 /****************************************************************************/
122 
void recheckForLoops(ConstROEdgeVector &edges, const ConstROEdgeVector &mandatory)
Checks whether the given edge list contains loops and removes them.
Definition: ROHelper.cpp:38
bool noMandatory(const ConstROEdgeVector &mandatory, ConstROEdgeVector::const_iterator start, ConstROEdgeVector::const_iterator end)
Definition: ROHelper.cpp:104
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:57
Some helping methods for router.
Definition: ROHelper.cpp:36
A basic edge for routing applications.
Definition: ROEdge.h:73
Base class for nodes used by the router.
Definition: RONode.h:46