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