SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // A storage for loaded polygons and pois 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 00023 00024 // =========================================================================== 00025 // included modules 00026 // =========================================================================== 00027 #ifdef _MSC_VER 00028 #include <windows_config.h> 00029 #else 00030 #include <config.h> 00031 #endif 00032 00033 #include <string> 00034 #include <map> 00035 #include <utils/common/MsgHandler.h> 00036 #include <utils/common/ToString.h> 00037 #include <utils/common/UtilExceptions.h> 00038 #include <utils/common/StringUtils.h> 00039 #include <utils/shapes/Polygon.h> 00040 #include <utils/iodevices/OutputDevice.h> 00041 #include <utils/xml/SUMOSAXAttributes.h> 00042 #include "PCPolyContainer.h" 00043 00044 #ifdef CHECK_MEMORY_LEAKS 00045 #include <foreign/nvwa/debug_new.h> 00046 #endif // CHECK_MEMORY_LEAKS 00047 00048 00049 // =========================================================================== 00050 // method definitions 00051 // =========================================================================== 00052 PCPolyContainer::PCPolyContainer(bool prune, 00053 const Boundary& prunningBoundary, 00054 const std::vector<std::string> &removeByNames) 00055 : myPrunningBoundary(prunningBoundary), myDoPrunne(prune), 00056 myRemoveByNames(removeByNames) {} 00057 00058 00059 PCPolyContainer::~PCPolyContainer() { 00060 clear(); 00061 } 00062 00063 00064 bool 00065 PCPolyContainer::insert(const std::string& id, Polygon* poly, 00066 int layer, bool ignorePrunning) { 00067 // check whether the polygon lies within the wished area 00068 // - if such an area was given 00069 if (myDoPrunne && !ignorePrunning) { 00070 Boundary b = poly->getShape().getBoxBoundary(); 00071 if (!b.partialWithin(myPrunningBoundary)) { 00072 delete poly; 00073 return true; 00074 } 00075 } 00076 // check whether the polygon was named to be a removed one 00077 if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) { 00078 delete poly; 00079 return true; 00080 } 00081 // 00082 PolyCont::iterator i = myPolyCont.find(id); 00083 if (i != myPolyCont.end()) { 00084 return false; 00085 } 00086 myPolyCont[id] = poly; 00087 myPolyLayerMap[poly] = layer; 00088 return true; 00089 } 00090 00091 00092 bool 00093 PCPolyContainer::insert(const std::string& id, PointOfInterest* poi, 00094 int layer, bool ignorePrunning) { 00095 // check whether the poi lies within the wished area 00096 // - if such an area was given 00097 if (myDoPrunne && !ignorePrunning) { 00098 if (!myPrunningBoundary.around(*poi)) { 00099 delete poi; 00100 return true; 00101 } 00102 } 00103 // check whether the polygon was named to be a removed one 00104 if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) { 00105 delete poi; 00106 return true; 00107 } 00108 // 00109 POICont::iterator i = myPOICont.find(id); 00110 if (i != myPOICont.end()) { 00111 return false; 00112 } 00113 myPOICont[id] = poi; 00114 myPOILayerMap[poi] = layer; 00115 return true; 00116 } 00117 00118 00119 bool 00120 PCPolyContainer::containsPolygon(const std::string& id) { 00121 return myPolyCont.find(id) != myPolyCont.end(); 00122 } 00123 00124 00125 void 00126 PCPolyContainer::clear() { 00127 // polys 00128 for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); i++) { 00129 delete(*i).second; 00130 } 00131 myPolyCont.clear(); 00132 myPolyLayerMap.clear(); 00133 // pois 00134 for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); i++) { 00135 delete(*i).second; 00136 } 00137 myPOICont.clear(); 00138 myPOILayerMap.clear(); 00139 } 00140 00141 00142 void 00143 PCPolyContainer::report() { 00144 WRITE_MESSAGE(" " + toString(getNoPolygons()) + " polygons loaded."); 00145 WRITE_MESSAGE(" " + toString(getNoPOIs()) + " pois loaded."); 00146 } 00147 00148 00149 void 00150 PCPolyContainer::save(const std::string& file) { 00151 OutputDevice& out = OutputDevice::getDevice(file); 00152 out.writeXMLHeader("shapes", SUMOSAXAttributes::ENCODING); 00153 // write polygons 00154 for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); ++i) { 00155 out.openTag("poly") << " id=\"" << StringUtils::escapeXML((*i).second->getID()) 00156 << "\" type=\"" << (*i).second->getType() 00157 << "\" color=\"" << (*i).second->getColor() 00158 << "\" fill=\"" << (*i).second->fill() 00159 << "\" layer=\"" << myPolyLayerMap[(*i).second] 00160 << "\" shape=\"" << (*i).second->getShape() << "\""; 00161 out.closeTag(true); 00162 } 00163 // write pois 00164 for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); ++i) { 00165 out.openTag("poi") << " id=\"" << StringUtils::escapeXML((*i).second->getID()) 00166 << "\" type=\"" << StringUtils::escapeXML((*i).second->getType()) 00167 << "\" color=\"" << *static_cast<RGBColor*>((*i).second) 00168 << "\" layer=\"" << myPOILayerMap[(*i).second] 00169 << "\" x=\"" << (*i).second->x() 00170 << "\" y=\"" << (*i).second->y() << "\""; 00171 out.closeTag(true); 00172 } 00173 out.close(); 00174 } 00175 00176 00177 int 00178 PCPolyContainer::getEnumIDFor(const std::string& key) { 00179 if (myIDEnums.find(key) == myIDEnums.end()) { 00180 myIDEnums[key] = 0; 00181 return 0; 00182 } else { 00183 myIDEnums[key] = myIDEnums[key] + 1; 00184 return myIDEnums[key]; 00185 } 00186 } 00187 00188 00189 00190 /****************************************************************************/ 00191