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 /****************************************************************************/
10 /// @file    RODFRouteCont.cpp
11 /// @author  Daniel Krajzewicz
12 /// @author  Laura Bieker
13 /// @author  Michael Behrisch
14 /// @date    Thu, 16.03.2006
15 /// @version $Id$
16 ///
17 // A container for routes
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <fstream>
27 #include <cassert>
28 #include "RODFRouteDesc.h"
29 #include "RODFRouteCont.h"
30 #include "RODFNet.h"
31 #include <router/ROEdge.h>
32 #include <utils/common/ToString.h>
33 #include <utils/iodevices/OutputDevice.h>
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
RODFRouteCont()39 RODFRouteCont::RODFRouteCont() {}
40 
41 
~RODFRouteCont()42 RODFRouteCont::~RODFRouteCont() {
43 }
44 
45 
46 void
addRouteDesc(RODFRouteDesc & desc)47 RODFRouteCont::addRouteDesc(RODFRouteDesc& desc) {
48     // routes may be duplicate as in-between routes may have different starting points
49     if (find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc)) == myRoutes.end()) {
50         // compute route id
51         setID(desc);
52         myRoutes.push_back(desc);
53     } else {
54         RODFRouteDesc& prev = *find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
55         prev.overallProb += desc.overallProb;
56     }
57 }
58 
59 
60 bool
removeRouteDesc(RODFRouteDesc & desc)61 RODFRouteCont::removeRouteDesc(RODFRouteDesc& desc) {
62     std::vector<RODFRouteDesc>::const_iterator j = find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
63     if (j == myRoutes.end()) {
64         return false;
65     }
66     return true;
67 }
68 
69 
70 bool
save(std::vector<std::string> & saved,const std::string & prependix,OutputDevice & out)71 RODFRouteCont::save(std::vector<std::string>& saved,
72                     const std::string& prependix, OutputDevice& out) {
73     bool haveSavedOneAtLeast = false;
74     for (std::vector<RODFRouteDesc>::const_iterator j = myRoutes.begin(); j != myRoutes.end(); ++j) {
75         const RODFRouteDesc& desc = (*j);
76         if (find(saved.begin(), saved.end(), desc.routename) != saved.end()) {
77             continue;
78         }
79         saved.push_back((*j).routename);
80         assert(desc.edges2Pass.size() >= 1);
81         out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, prependix + desc.routename);
82         out << " edges=\"";
83         for (ROEdgeVector::const_iterator k = desc.edges2Pass.begin(); k != desc.edges2Pass.end(); k++) {
84             if (k != desc.edges2Pass.begin()) {
85                 out << ' ';
86             }
87             out << (*k)->getID();
88         }
89         out << '"';
90         out.closeTag();
91         haveSavedOneAtLeast = true;
92     }
93     return haveSavedOneAtLeast;
94 }
95 
96 
97 void
sortByDistance()98 RODFRouteCont::sortByDistance() {
99     sort(myRoutes.begin(), myRoutes.end(), by_distance_sorter());
100 }
101 
102 
103 void
removeIllegal(const std::vector<ROEdgeVector> & illegals)104 RODFRouteCont::removeIllegal(const std::vector<ROEdgeVector >& illegals) {
105     for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end();) {
106         RODFRouteDesc& desc = *i;
107         bool remove = false;
108         for (std::vector<ROEdgeVector >::const_iterator j = illegals.begin(); !remove && j != illegals.end(); ++j) {
109             int noFound = 0;
110             for (ROEdgeVector::const_iterator k = (*j).begin(); !remove && k != (*j).end(); ++k) {
111                 if (find(desc.edges2Pass.begin(), desc.edges2Pass.end(), *k) != desc.edges2Pass.end()) {
112                     noFound++;
113                     if (noFound > 1) {
114                         remove = true;
115                     }
116                 }
117             }
118         }
119         if (remove) {
120             i = myRoutes.erase(i);
121         } else {
122             ++i;
123         }
124     }
125 }
126 
127 
128 void
setID(RODFRouteDesc & desc) const129 RODFRouteCont::setID(RODFRouteDesc& desc) const {
130     std::pair<ROEdge*, ROEdge*> c(desc.edges2Pass[0], desc.edges2Pass.back());
131     desc.routename = c.first->getID() + "_to_" + c.second->getID();
132     if (myConnectionOccurences.find(c) == myConnectionOccurences.end()) {
133         myConnectionOccurences[c] = 0;
134     } else {
135         myConnectionOccurences[c] = myConnectionOccurences[c] + 1;
136         desc.routename = desc.routename + "_" + toString(myConnectionOccurences[c]);
137     }
138 }
139 
140 
141 
142 /****************************************************************************/
143 
144