Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 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 : /****************************************************************************/
14 : /// @file NBDistrict.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @date Sept 2002
19 : ///
20 : // A class representing a single district
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <cassert>
25 : #include <vector>
26 : #include <string>
27 : #include <utility>
28 : #include <iostream>
29 : #include <algorithm>
30 : #include <utils/common/Named.h>
31 : #include <utils/common/StringUtils.h>
32 : #include <utils/iodevices/OutputDevice.h>
33 : #include "NBEdge.h"
34 : #include "NBDistrict.h"
35 :
36 :
37 : // ===========================================================================
38 : // member method definitions
39 : // ===========================================================================
40 50 : NBDistrict::NBDistrict(const std::string& id, const Position& pos)
41 50 : : Named(StringUtils::convertUmlaute(id)),
42 50 : myPosition(pos) {}
43 :
44 :
45 0 : NBDistrict::NBDistrict(const std::string& id)
46 0 : : Named(id), myPosition(0, 0) {}
47 :
48 :
49 100 : NBDistrict::~NBDistrict() {}
50 :
51 :
52 : // ----------- Applying offset
53 : void
54 50 : NBDistrict::reshiftPosition(double xoff, double yoff) {
55 : myPosition.add(xoff, yoff, 0);
56 50 : myShape.add(xoff, yoff, 0);
57 50 : }
58 :
59 :
60 : void
61 0 : NBDistrict::mirrorX() {
62 : myPosition.mul(1, -1);
63 0 : myShape.mirrorX();
64 0 : }
65 :
66 :
67 : bool
68 0 : NBDistrict::addSource(NBEdge* const source, double weight) {
69 0 : EdgeVector::iterator i = std::find(mySources.begin(), mySources.end(), source);
70 0 : if (i != mySources.end()) {
71 : return false;
72 : }
73 0 : mySources.push_back(source);
74 0 : mySourceWeights.push_back(weight);
75 : assert(source->getID() != "");
76 : return true;
77 : }
78 :
79 :
80 : bool
81 0 : NBDistrict::addSink(NBEdge* const sink, double weight) {
82 0 : EdgeVector::iterator i = std::find(mySinks.begin(), mySinks.end(), sink);
83 0 : if (i != mySinks.end()) {
84 : return false;
85 : }
86 0 : mySinks.push_back(sink);
87 0 : mySinkWeights.push_back(weight);
88 : assert(sink->getID() != "");
89 : return true;
90 : }
91 :
92 :
93 : void
94 0 : NBDistrict::setCenter(const Position& pos) {
95 0 : myPosition = pos;
96 0 : }
97 :
98 :
99 : void
100 0 : NBDistrict::replaceIncoming(const EdgeVector& which, NBEdge* const by) {
101 : // temporary structures
102 : EdgeVector newList;
103 : WeightsCont newWeights;
104 0 : double joinedVal = 0;
105 : // go through the list of sinks
106 : EdgeVector::iterator i = mySinks.begin();
107 : WeightsCont::iterator j = mySinkWeights.begin();
108 0 : for (; i != mySinks.end(); i++, j++) {
109 0 : NBEdge* tmp = (*i);
110 0 : double val = (*j);
111 0 : if (find(which.begin(), which.end(), tmp) == which.end()) {
112 : // if the current edge shall not be replaced, add to the
113 : // temporary list
114 0 : newList.push_back(tmp);
115 0 : newWeights.push_back(val);
116 : } else {
117 : // otherwise, skip it and add its weight to the one to be inserted
118 : // instead
119 0 : joinedVal += val;
120 : }
121 : }
122 : // add the one to be inserted instead
123 0 : newList.push_back(by);
124 0 : newWeights.push_back(joinedVal);
125 : // assign to values
126 0 : mySinks = newList;
127 0 : mySinkWeights = newWeights;
128 0 : }
129 :
130 :
131 : void
132 0 : NBDistrict::replaceOutgoing(const EdgeVector& which, NBEdge* const by) {
133 : // temporary structures
134 : EdgeVector newList;
135 : WeightsCont newWeights;
136 0 : double joinedVal = 0;
137 : // go through the list of sinks
138 : EdgeVector::iterator i = mySources.begin();
139 : WeightsCont::iterator j = mySourceWeights.begin();
140 0 : for (; i != mySources.end(); i++, j++) {
141 0 : NBEdge* tmp = (*i);
142 0 : double val = (*j);
143 0 : if (find(which.begin(), which.end(), tmp) == which.end()) {
144 : // if the current edge shall not be replaced, add to the
145 : // temporary list
146 0 : newList.push_back(tmp);
147 0 : newWeights.push_back(val);
148 : } else {
149 : // otherwise, skip it and add its weight to the one to be inserted
150 : // instead
151 0 : joinedVal += val;
152 : }
153 : }
154 : // add the one to be inserted instead
155 0 : newList.push_back(by);
156 0 : newWeights.push_back(joinedVal);
157 : // assign to values
158 0 : mySources = newList;
159 0 : mySourceWeights = newWeights;
160 0 : }
161 :
162 :
163 : void
164 0 : NBDistrict::removeFromSinksAndSources(NBEdge* const e) {
165 : int i;
166 0 : for (i = 0; i < (int)mySinks.size(); ++i) {
167 0 : if (mySinks[i] == e) {
168 0 : mySinks.erase(mySinks.begin() + i);
169 0 : mySinkWeights.erase(mySinkWeights.begin() + i);
170 : }
171 : }
172 0 : for (i = 0; i < (int)mySources.size(); ++i) {
173 0 : if (mySources[i] == e) {
174 0 : mySources.erase(mySources.begin() + i);
175 0 : mySourceWeights.erase(mySourceWeights.begin() + i);
176 : }
177 : }
178 0 : }
179 :
180 :
181 : void
182 30 : NBDistrict::addShape(const PositionVector& p) {
183 : myShape = p;
184 30 : }
185 :
186 :
187 : /****************************************************************************/
|