SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PCPolyContainer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A storage for loaded polygons and pois
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <algorithm>
35 #include <map>
37 #include <utils/common/ToString.h>
40 #include <utils/shapes/Polygon.h>
43 #include "PCPolyContainer.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
54  const Boundary& prunningBoundary,
55  const std::vector<std::string>& removeByNames)
56  : myPrunningBoundary(prunningBoundary), myDoPrunne(prune),
57  myRemoveByNames(removeByNames) {}
58 
59 
61  clear();
62 }
63 
64 
65 bool
66 PCPolyContainer::insert(const std::string& id, Polygon* poly,
67  int layer, bool ignorePrunning) {
68  // check whether the polygon lies within the wished area
69  // - if such an area was given
70  if (myDoPrunne && !ignorePrunning) {
71  Boundary b = poly->getShape().getBoxBoundary();
73  delete poly;
74  return true;
75  }
76  }
77  // check whether the polygon was named to be a removed one
78  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
79  delete poly;
80  return true;
81  }
82  //
83  PolyCont::iterator i = myPolyCont.find(id);
84  if (i != myPolyCont.end()) {
85  return false;
86  }
87  myPolyCont[id] = poly;
88  myPolyLayerMap[poly] = layer;
89  return true;
90 }
91 
92 
93 bool
94 PCPolyContainer::insert(const std::string& id, PointOfInterest* poi,
95  int layer, bool ignorePrunning) {
96  // check whether the poi lies within the wished area
97  // - if such an area was given
98  if (myDoPrunne && !ignorePrunning) {
99  if (!myPrunningBoundary.around(*poi)) {
100  delete poi;
101  return true;
102  }
103  }
104  // check whether the polygon was named to be a removed one
105  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
106  delete poi;
107  return true;
108  }
109  //
110  POICont::iterator i = myPOICont.find(id);
111  if (i != myPOICont.end()) {
112  return false;
113  }
114  myPOICont[id] = poi;
115  myPOILayerMap[poi] = layer;
116  return true;
117 }
118 
119 
120 bool
121 PCPolyContainer::containsPolygon(const std::string& id) {
122  return myPolyCont.find(id) != myPolyCont.end();
123 }
124 
125 
126 void
128  // polys
129  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); i++) {
130  delete(*i).second;
131  }
132  myPolyCont.clear();
133  myPolyLayerMap.clear();
134  // pois
135  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); i++) {
136  delete(*i).second;
137  }
138  myPOICont.clear();
139  myPOILayerMap.clear();
140 }
141 
142 
143 void
145  WRITE_MESSAGE(" " + toString(getNoPolygons()) + " polygons loaded.");
146  WRITE_MESSAGE(" " + toString(getNoPOIs()) + " pois loaded.");
147 }
148 
149 
150 void
151 PCPolyContainer::save(const std::string& file) {
153  out.writeXMLHeader("shapes");
154  // write polygons
155  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); ++i) {
156  Polygon* p = i->second;
157  out.openTag(SUMO_TAG_POLY);
161  out.writeAttr(SUMO_ATTR_FILL, p->getFill());
164  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
166  }
167  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
169  }
170  const std::map<std::string, std::string>& attrs = p->getMap();
171  if (attrs.size() != 0) {
172  for (std::map<std::string, std::string>::const_iterator j = attrs.begin(); j != attrs.end(); ++j) {
173  out.openTag(SUMO_TAG_PARAM);
174  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
175  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
176  out.closeTag();
177  }
178  }
179  out.closeTag();
180  }
181  // write pois
182  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); ++i) {
183  PointOfInterest* p = i->second;
184  out.openTag(SUMO_TAG_POI);
189  out.writeAttr(SUMO_ATTR_X, p->x());
190  out.writeAttr(SUMO_ATTR_Y, p->y());
191  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
193  }
194  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
196  }
197  if (p->getWidth() != Shape::DEFAULT_IMG_WIDTH) {
199  }
200  if (p->getHeight() != Shape::DEFAULT_IMG_HEIGHT) {
202  }
203  const std::map<std::string, std::string>& attrs = p->getMap();
204  if (attrs.size() != 0) {
205  for (std::map<std::string, std::string>::const_iterator j = attrs.begin(); j != attrs.end(); ++j) {
206  out.openTag(SUMO_TAG_PARAM);
207  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
208  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
209  out.closeTag();
210  }
211  }
212  out.closeTag();
213  }
214  out.close();
215 }
216 
217 
218 int
219 PCPolyContainer::getEnumIDFor(const std::string& key) {
220  if (myIDEnums.find(key) == myIDEnums.end()) {
221  myIDEnums[key] = 0;
222  return 0;
223  } else {
224  myIDEnums[key] = myIDEnums[key] + 1;
225  return myIDEnums[key];
226  }
227 }
228 
229 
230 
231 /****************************************************************************/
232 
void clear()
Removes all stored objects (polygons and pois)
bool insert(const std::string &id, Polygon *poly, int layer, bool ignorePrunning=false)
Adds a polygon to the storage.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
bool containsPolygon(const std::string &kidey)
Returns the information whether a polygon with the given key is in the container. ...
void close()
Closes the device and removes it from the dictionary.
const std::string & getImgFile() const
Returns the imgFile of the Shape.
Definition: Shape.h:100
unsigned int getNoPOIs()
Returns the number of stored pois.
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:150
bool around(const Position &p, SUMOReal offset=0) const
Returns whether the boundary contains the given coordinate.
Definition: Boundary.cpp:148
POICont myPOICont
The poi container, accessed by the pois&#39; ids.
A layer number.
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: Polygon.h:75
bool partialWithin(const AbstractPoly &poly, SUMOReal offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:190
static std::string escapeXML(const std::string &orig)
Replaces the standard escapes by their XML entities.
Boundary myPrunningBoundary
The boundary that described the rectangle within which an object must be in order to be kept...
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
bool myDoPrunne
Information whether the prunning boundary shall be used.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
A 2D- or 3D-polygon.
Definition: Polygon.h:48
void save(const std::string &file)
Saves the stored polygons into the given file.
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:149
const std::string & getID() const
Returns the id.
Definition: Named.h:60
static const SUMOReal DEFAULT_IMG_HEIGHT
Definition: Shape.h:152
unsigned int getNoPolygons()
Returns the number of stored polygons.
~PCPolyContainer()
Destructor.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:51
std::map< std::string, int > myIDEnums
An id to int map for proper enumeration.
const RGBColor & getColor() const
Returns the color of the Shape.
Definition: Shape.h:78
PCPolyContainer(bool prune, const Boundary &prunningBoundary, const std::vector< std::string > &removeByNames)
Constructor.
void report()
Reports how many polygons and pois were added.
std::map< PointOfInterest *, int > myPOILayerMap
A map from pois to the layers they are located in.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
std::map< Polygon *, int > myPolyLayerMap
A map from polygons to the layers they are located in.
int getEnumIDFor(const std::string &key)
Retuns a unique id for a given name.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
std::vector< std::string > myRemoveByNames
List of names of polygons/pois that shall be removed.
SUMOReal getWidth() const
SUMOReal getHeight() const
Returns whether the image hidth of the POI.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
A point-of-interest.
SUMOReal getLayer() const
Returns the layer of the Shape.
Definition: Shape.h:86
PolyCont myPolyCont
The polygon container, accessed by the polygons&#39; ids.
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:70
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:197
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
A color information.
bool getFill() const
Returns whether the polygon is filled.
Definition: Polygon.h:82
Fill the polygon.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
SUMOReal getAngle() const
Returns the angle of the Shape.
Definition: Shape.h:93
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:151