SUMO - Simulation of Urban MObility
NBDistrict.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-2018 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 /****************************************************************************/
17 // A class representing a single district
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <cassert>
27 #include <vector>
28 #include <string>
29 #include <utility>
30 #include <iostream>
31 #include <algorithm>
32 #include <utils/common/Named.h>
35 #include "NBEdge.h"
36 #include "NBDistrict.h"
37 
38 
39 // ===========================================================================
40 // member method definitions
41 // ===========================================================================
42 NBDistrict::NBDistrict(const std::string& id, const Position& pos)
43  : Named(StringUtils::convertUmlaute(id)),
44  myPosition(pos) {}
45 
46 
47 NBDistrict::NBDistrict(const std::string& id)
48  : Named(id), myPosition(0, 0) {}
49 
50 
52 
53 
54 // ----------- Applying offset
55 void
56 NBDistrict::reshiftPosition(double xoff, double yoff) {
57  myPosition.add(xoff, yoff, 0);
58  myShape.add(xoff, yoff, 0);
59 }
60 
61 
62 void
64  myPosition.mul(1, -1);
65  myShape.mirrorX();
66 }
67 
68 
69 bool
70 NBDistrict::addSource(NBEdge* const source, double weight) {
71  EdgeVector::iterator i = find(mySources.begin(), mySources.end(), source);
72  if (i != mySources.end()) {
73  return false;
74  }
75  mySources.push_back(source);
76  mySourceWeights.push_back(weight);
77  assert(source->getID() != "");
78  return true;
79 }
80 
81 
82 bool
83 NBDistrict::addSink(NBEdge* const sink, double weight) {
84  EdgeVector::iterator i = find(mySinks.begin(), mySinks.end(), sink);
85  if (i != mySinks.end()) {
86  return false;
87  }
88  mySinks.push_back(sink);
89  mySinkWeights.push_back(weight);
90  assert(sink->getID() != "");
91  return true;
92 }
93 
94 
95 void
97  myPosition = pos;
98 }
99 
100 
101 void
103  // temporary structures
104  EdgeVector newList;
105  WeightsCont newWeights;
106  double joinedVal = 0;
107  // go through the list of sinks
108  EdgeVector::iterator i = mySinks.begin();
109  WeightsCont::iterator j = mySinkWeights.begin();
110  for (; i != mySinks.end(); i++, j++) {
111  NBEdge* tmp = (*i);
112  double val = (*j);
113  if (find(which.begin(), which.end(), tmp) == which.end()) {
114  // if the current edge shall not be replaced, add to the
115  // temporary list
116  newList.push_back(tmp);
117  newWeights.push_back(val);
118  } else {
119  // otherwise, skip it and add its weight to the one to be inserted
120  // instead
121  joinedVal += val;
122  }
123  }
124  // add the one to be inserted instead
125  newList.push_back(by);
126  newWeights.push_back(joinedVal);
127  // assign to values
128  mySinks = newList;
129  mySinkWeights = newWeights;
130 }
131 
132 
133 void
135  // temporary structures
136  EdgeVector newList;
137  WeightsCont newWeights;
138  double joinedVal = 0;
139  // go through the list of sinks
140  EdgeVector::iterator i = mySources.begin();
141  WeightsCont::iterator j = mySourceWeights.begin();
142  for (; i != mySources.end(); i++, j++) {
143  NBEdge* tmp = (*i);
144  double val = (*j);
145  if (find(which.begin(), which.end(), tmp) == which.end()) {
146  // if the current edge shall not be replaced, add to the
147  // temporary list
148  newList.push_back(tmp);
149  newWeights.push_back(val);
150  } else {
151  // otherwise, skip it and add its weight to the one to be inserted
152  // instead
153  joinedVal += val;
154  }
155  }
156  // add the one to be inserted instead
157  newList.push_back(by);
158  newWeights.push_back(joinedVal);
159  // assign to values
160  mySources = newList;
161  mySourceWeights = newWeights;
162 }
163 
164 
165 void
167  int i;
168  for (i = 0; i < (int)mySinks.size(); ++i) {
169  if (mySinks[i] == e) {
170  mySinks.erase(mySinks.begin() + i);
171  mySinkWeights.erase(mySinkWeights.begin() + i);
172  }
173  }
174  for (i = 0; i < (int)mySources.size(); ++i) {
175  if (mySources[i] == e) {
176  mySources.erase(mySources.begin() + i);
177  mySourceWeights.erase(mySourceWeights.begin() + i);
178  }
179  }
180 }
181 
182 
183 void
185  myShape = p;
186 }
187 
188 
189 
190 /****************************************************************************/
191 
void replaceOutgoing(const EdgeVector &which, NBEdge *const by)
Replaces outgoing edges from the vector (source) by the given edge.
Definition: NBDistrict.cpp:134
WeightsCont mySinkWeights
The weights of the sinks.
Definition: NBDistrict.h:252
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:127
Some static methods for string processing.
Definition: StringUtils.h:40
The representation of a single edge during network building.
Definition: NBEdge.h:65
std::vector< double > WeightsCont
Definition of a vector of connection weights.
Definition: NBDistrict.h:240
void mirrorX()
mirror coordinates along the x-axis
Definition: NBDistrict.cpp:63
const std::string & getID() const
Returns the id.
Definition: Named.h:78
void reshiftPosition(double xoff, double yoff)
Applies an offset to the district.
Definition: NBDistrict.cpp:56
void addShape(const PositionVector &p)
Sets the shape of this district.
Definition: NBDistrict.cpp:184
NBDistrict(const std::string &id, const Position &pos)
Constructor with id, and position.
Definition: NBDistrict.cpp:42
EdgeVector mySources
The sources (connection from district to network)
Definition: NBDistrict.h:243
void setCenter(const Position &pos)
Sets the center coordinates.
Definition: NBDistrict.cpp:96
Position myPosition
The position of the district.
Definition: NBDistrict.h:255
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
void removeFromSinksAndSources(NBEdge *const e)
Removes the given edge from the lists of sources and sinks.
Definition: NBDistrict.cpp:166
~NBDistrict()
Destructor.
Definition: NBDistrict.cpp:51
Base class for objects which have an id.
Definition: Named.h:58
bool addSink(NBEdge *const sink, double weight)
Adds a sink.
Definition: NBDistrict.cpp:83
EdgeVector mySinks
The sinks (connection from network to district)
Definition: NBDistrict.h:249
WeightsCont mySourceWeights
The weights of the sources.
Definition: NBDistrict.h:246
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
PositionVector myShape
The shape of the dsitrict.
Definition: NBDistrict.h:258
void mul(double val)
Multiplies both positions with the given value.
Definition: Position.h:107
void add(double xoff, double yoff, double zoff)
bool addSource(NBEdge *const source, double weight)
Adds a source.
Definition: NBDistrict.cpp:70
void replaceIncoming(const EdgeVector &which, NBEdge *const by)
Replaces incoming edges from the vector (sinks) by the given edge.
Definition: NBDistrict.cpp:102