NBDistrict.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A class representing a single district
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
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 // member method definitions
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     // write the head and the id of the district
00093     into << "   <district id=\"" << myID << "\"";
00094     if (myShape.size()>0) {
00095         into << " shape=\"" << myShape << "\"";
00096     }
00097     into << ">\n";
00098     size_t i;
00099     // write all sources
00100     for (i=0; i<mySources.size(); i++) {
00101         // write the head and the id of the source
00102         assert(i<mySources.size());
00103         into << "      <dsource id=\"" << mySources[i]->getID() << "\" weight=\"" << mySourceWeights[i] << "\"/>\n";
00104     }
00105     // write all sinks
00106     for (i=0; i<mySinks.size(); i++) {
00107         // write the head and the id of the sink
00108         assert(i<mySinks.size());
00109         into << "      <dsink id=\"" << mySinks[i]->getID() << "\" weight=\"" << mySinkWeights[i] << "\"/>\n";
00110     }
00111     // write the tail
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     // temporary structures
00125     EdgeVector newList;
00126     WeightsCont newWeights;
00127     SUMOReal joinedVal = 0;
00128     // go through the list of sinks
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             // if the current edge shall not be replaced, add to the
00136             //  temporary list
00137             newList.push_back(tmp);
00138             newWeights.push_back(val);
00139         } else {
00140             // otherwise, skip it and add its weight to the one to be inserted
00141             //  instead
00142             joinedVal += val;
00143         }
00144     }
00145     // add the one to be inserted instead
00146     newList.push_back(by);
00147     newWeights.push_back(joinedVal);
00148     // assign to values
00149     mySinks = newList;
00150     mySinkWeights = newWeights;
00151 }
00152 
00153 
00154 void
00155 NBDistrict::replaceOutgoing(const EdgeVector &which, NBEdge * const by) throw() {
00156     // temporary structures
00157     EdgeVector newList;
00158     WeightsCont newWeights;
00159     SUMOReal joinedVal = 0;
00160     // go through the list of sinks
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             // if the current edge shall not be replaced, add to the
00168             //  temporary list
00169             newList.push_back(tmp);
00170             newWeights.push_back(val);
00171         } else {
00172             // otherwise, skip it and add its weight to the one to be inserted
00173             //  instead
00174             joinedVal += val;
00175         }
00176     }
00177     // add the one to be inserted instead
00178     newList.push_back(by);
00179     newWeights.push_back(joinedVal);
00180     // assign to values
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 

Generated on Wed May 5 00:06:32 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6