PCPolyContainer.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A storage for loaded polygons and pois
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 <string>
00031 #include <map>
00032 #include <utils/common/MsgHandler.h>
00033 #include <utils/common/ToString.h>
00034 #include <utils/shapes/Polygon2D.h>
00035 #include "PCPolyContainer.h"
00036 #include <utils/common/UtilExceptions.h>
00037 #include <utils/iodevices/OutputDevice.h>
00038 #include <utils/common/StringUtils.h>
00039 
00040 #ifdef CHECK_MEMORY_LEAKS
00041 #include <foreign/nvwa/debug_new.h>
00042 #endif // CHECK_MEMORY_LEAKS
00043 
00044 
00045 // ===========================================================================
00046 // method definitions
00047 // ===========================================================================
00048 PCPolyContainer::PCPolyContainer(bool prune,
00049                                  const Boundary &prunningBoundary,
00050                                  const std::vector<std::string> &removeByNames) throw()
00051         : myPrunningBoundary(prunningBoundary), myDoPrunne(prune),
00052         myRemoveByNames(removeByNames) {}
00053 
00054 
00055 PCPolyContainer::~PCPolyContainer() throw() {
00056     clear();
00057 }
00058 
00059 
00060 bool
00061 PCPolyContainer::insert(const std::string &id, Polygon2D *poly,
00062                         int layer, bool ignorePrunning) throw() {
00063     // check whether the polygon lies within the wished area
00064     //  - if such an area was given
00065     if (myDoPrunne&&!ignorePrunning) {
00066         Boundary b = poly->getShape().getBoxBoundary();
00067         if (!b.partialWithin(myPrunningBoundary)) {
00068             delete poly;
00069             return true;
00070         }
00071     }
00072     // check whether the polygon was named to be a removed one
00073     if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id)!=myRemoveByNames.end()) {
00074         delete poly;
00075         return true;
00076     }
00077     //
00078     PolyCont::iterator i=myPolyCont.find(id);
00079     if (i!=myPolyCont.end()) {
00080         return false;
00081     }
00082     myPolyCont[id] = poly;
00083     myPolyLayerMap[poly] = layer;
00084     return true;
00085 }
00086 
00087 
00088 bool
00089 PCPolyContainer::insert(const std::string &id, PointOfInterest *poi,
00090                         int layer, bool ignorePrunning) throw() {
00091     // check whether the poi lies within the wished area
00092     //  - if such an area was given
00093     if (myDoPrunne&&!ignorePrunning) {
00094         if (!myPrunningBoundary.around(*poi)) {
00095             delete poi;
00096             return true;
00097         }
00098     }
00099     // check whether the polygon was named to be a removed one
00100     if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id)!=myRemoveByNames.end()) {
00101         delete poi;
00102         return true;
00103     }
00104     //
00105     POICont::iterator i=myPOICont.find(id);
00106     if (i!=myPOICont.end()) {
00107         return false;
00108     }
00109     myPOICont[id] = poi;
00110     myPOILayerMap[poi] = layer;
00111     return true;
00112 }
00113 
00114 
00115 bool
00116 PCPolyContainer::containsPolygon(const std::string &id) throw() {
00117     return myPolyCont.find(id)!=myPolyCont.end();
00118 }
00119 
00120 
00121 void
00122 PCPolyContainer::clear() throw() {
00123     // polys
00124     for (PolyCont::iterator i=myPolyCont.begin(); i!=myPolyCont.end(); i++) {
00125         delete(*i).second;
00126     }
00127     myPolyCont.clear();
00128     myPolyLayerMap.clear();
00129     // pois
00130     for (POICont::iterator i=myPOICont.begin(); i!=myPOICont.end(); i++) {
00131         delete(*i).second;
00132     }
00133     myPOICont.clear();
00134     myPOILayerMap.clear();
00135 }
00136 
00137 
00138 void
00139 PCPolyContainer::report() throw() {
00140     WRITE_MESSAGE("   " + toString(getNoPolygons()) + " polygons loaded.");
00141     WRITE_MESSAGE("   " + toString(getNoPOIs()) + " pois loaded.");
00142 }
00143 
00144 
00145 void
00146 PCPolyContainer::save(const std::string &file) throw(IOError) {
00147     OutputDevice& out = OutputDevice::getDevice(file);
00148     out.writeXMLHeader("shapes");
00149     // write polygons
00150     for (PolyCont::iterator i=myPolyCont.begin(); i!=myPolyCont.end(); ++i) {
00151         out << "    <poly id=\"" << StringUtils::escapeXML((*i).second->getID()) << "\" type=\""
00152         << (*i).second->getType() << "\" color=\""
00153         << (*i).second->getColor() << "\" fill=\""
00154         << (*i).second->fill() << "\"";
00155         out << " layer=\"" << myPolyLayerMap[(*i).second] << "\"";
00156         out << " shape=\"" << (*i).second->getShape() << "\"/>\n";
00157     }
00158     // write pois
00159     for (POICont::iterator i=myPOICont.begin(); i!=myPOICont.end(); ++i) {
00160         out << "    <poi id=\"" << StringUtils::escapeXML((*i).second->getID()) << "\" type=\""
00161         << (*i).second->getType() << "\" color=\""
00162         << *static_cast<RGBColor*>((*i).second) << '"';
00163         out << " layer=\"" << myPOILayerMap[(*i).second] << "\"";
00164         out << " x=\"" << (*i).second->x() << "\""
00165         << " y=\"" << (*i).second->y() << "\""
00166         << "/>\n";
00167     }
00168     out.close();
00169 }
00170 
00171 
00172 int
00173 PCPolyContainer::getEnumIDFor(const std::string &key) throw() {
00174     if (myIDEnums.find(key)==myIDEnums.end()) {
00175         myIDEnums[key] = 0;
00176         return 0;
00177     } else {
00178         myIDEnums[key] = myIDEnums[key] + 1;
00179         return myIDEnums[key];
00180     }
00181 }
00182 
00183 
00184 
00185 /****************************************************************************/
00186 

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