NBDistrict.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include <cassert>
00031 #include <vector>
00032 #include <string>
00033 #include <utility>
00034 #include <iostream>
00035 #include <algorithm>
00036 #include <utils/common/Named.h>
00037 #include <utils/common/StringUtils.h>
00038 #include <utils/iodevices/OutputDevice.h>
00039 #include "NBEdge.h"
00040 #include "NBDistrict.h"
00041
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045
00046
00047
00048
00049
00050 NBDistrict::NBDistrict(const std::string &id, const Position2D &pos) throw()
00051 : Named(StringUtils::convertUmlaute(id)),
00052 myPosition(pos) {}
00053
00054
00055 NBDistrict::NBDistrict(const std::string &id) throw()
00056 : Named(id), myPosition(0, 0) {}
00057
00058
00059 NBDistrict::~NBDistrict() throw() {}
00060
00061
00062 bool
00063 NBDistrict::addSource(NBEdge * const source, SUMOReal weight) throw() {
00064 EdgeVector::iterator i = find(mySources.begin(), mySources.end(), source);
00065 if (i!=mySources.end()) {
00066 return false;
00067 }
00068 mySources.push_back(source);
00069 mySourceWeights.push_back(weight);
00070 assert(source->getID()!="");
00071 return true;
00072 }
00073
00074
00075 bool
00076 NBDistrict::addSink(NBEdge * const sink, SUMOReal weight) throw() {
00077 EdgeVector::iterator i = find(mySinks.begin(), mySinks.end(), sink);
00078 if (i!=mySinks.end()) {
00079 return false;
00080 }
00081 mySinks.push_back(sink);
00082 mySinkWeights.push_back(weight);
00083 assert(sink->getID()!="");
00084 return true;
00085 }
00086
00087
00088 void
00089 NBDistrict::writeXML(OutputDevice &into) throw() {
00090 VectorHelper<SUMOReal>::normalise(mySourceWeights, 1.0);
00091 VectorHelper<SUMOReal>::normalise(mySinkWeights, 1.0);
00092
00093 into << " <district id=\"" << myID << "\"";
00094 if (myShape.size()>0) {
00095 into << " shape=\"" << myShape << "\"";
00096 }
00097 into << ">\n";
00098 size_t i;
00099
00100 for (i=0; i<mySources.size(); i++) {
00101
00102 assert(i<mySources.size());
00103 into << " <dsource id=\"" << mySources[i]->getID() << "\" weight=\"" << mySourceWeights[i] << "\"/>\n";
00104 }
00105
00106 for (i=0; i<mySinks.size(); i++) {
00107
00108 assert(i<mySinks.size());
00109 into << " <dsink id=\"" << mySinks[i]->getID() << "\" weight=\"" << mySinkWeights[i] << "\"/>\n";
00110 }
00111
00112 into << " </district>\n\n";
00113 }
00114
00115
00116 void
00117 NBDistrict::setCenter(const Position2D &pos) throw() {
00118 myPosition = pos;
00119 }
00120
00121
00122 void
00123 NBDistrict::replaceIncoming(const EdgeVector &which, NBEdge * const by) throw() {
00124
00125 EdgeVector newList;
00126 WeightsCont newWeights;
00127 SUMOReal joinedVal = 0;
00128
00129 EdgeVector::iterator i=mySinks.begin();
00130 WeightsCont::iterator j=mySinkWeights.begin();
00131 for (; i!=mySinks.end(); i++, j++) {
00132 NBEdge *tmp = (*i);
00133 SUMOReal val = (*j);
00134 if (find(which.begin(), which.end(), tmp)==which.end()) {
00135
00136
00137 newList.push_back(tmp);
00138 newWeights.push_back(val);
00139 } else {
00140
00141
00142 joinedVal += val;
00143 }
00144 }
00145
00146 newList.push_back(by);
00147 newWeights.push_back(joinedVal);
00148
00149 mySinks = newList;
00150 mySinkWeights = newWeights;
00151 }
00152
00153
00154 void
00155 NBDistrict::replaceOutgoing(const EdgeVector &which, NBEdge * const by) throw() {
00156
00157 EdgeVector newList;
00158 WeightsCont newWeights;
00159 SUMOReal joinedVal = 0;
00160
00161 EdgeVector::iterator i=mySources.begin();
00162 WeightsCont::iterator j=mySourceWeights.begin();
00163 for (; i!=mySources.end(); i++, j++) {
00164 NBEdge *tmp = (*i);
00165 SUMOReal val = (*j);
00166 if (find(which.begin(), which.end(), tmp)==which.end()) {
00167
00168
00169 newList.push_back(tmp);
00170 newWeights.push_back(val);
00171 } else {
00172
00173
00174 joinedVal += val;
00175 }
00176 }
00177
00178 newList.push_back(by);
00179 newWeights.push_back(joinedVal);
00180
00181 mySources = newList;
00182 mySourceWeights = newWeights;
00183 }
00184
00185
00186 const Position2D &
00187 NBDistrict::getPosition() const throw() {
00188 return myPosition;
00189 }
00190
00191
00192 void
00193 NBDistrict::removeFromSinksAndSources(NBEdge * const e) throw() {
00194 size_t i;
00195 for (i=0; i<mySinks.size(); ++i) {
00196 if (mySinks[i]==e) {
00197 mySinks.erase(mySinks.begin()+i);
00198 mySinkWeights.erase(mySinkWeights.begin()+i);
00199 }
00200 }
00201 for (i=0; i<mySources.size(); ++i) {
00202 if (mySources[i]==e) {
00203 mySources.erase(mySources.begin()+i);
00204 mySourceWeights.erase(mySourceWeights.begin()+i);
00205 }
00206 }
00207 }
00208
00209
00210 void
00211 NBDistrict::addShape(const Position2DVector &p) throw() {
00212 myShape = p;
00213 }
00214
00215
00216 void
00217 NBDistrict::reshiftPosition(SUMOReal xoff, SUMOReal yoff) throw() {
00218 myPosition.reshiftRotate(xoff, yoff, 0);
00219 myShape.reshiftRotate(xoff, yoff, 0);
00220 }
00221
00222
00223
00224
00225