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 /****************************************************************************/
10 // A storage for loaded polygons and pois
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2005-2014 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <algorithm>
36 #include <map>
38 #include <utils/common/ToString.h>
41 #include <utils/shapes/Polygon.h>
44 #include "PCPolyContainer.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  const Boundary& pruningBoundary,
56  const std::vector<std::string>& removeByNames)
57  : myPruningBoundary(pruningBoundary), myDoPrune(prune),
58  myRemoveByNames(removeByNames) {}
59 
60 
62  clear();
63 }
64 
65 
66 bool
67 PCPolyContainer::insert(const std::string& id, Polygon* poly,
68  int layer, bool ignorePruning) {
69  // check whether the polygon lies within the wished area
70  // - if such an area was given
71  if (myDoPrune && !ignorePruning) {
72  Boundary b = poly->getShape().getBoxBoundary();
74  delete poly;
75  return false;
76  }
77  }
78  // check whether the polygon was named to be a removed one
79  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
80  delete poly;
81  return false;
82  }
83  //
84  PolyCont::iterator i = myPolyCont.find(id);
85  if (i != myPolyCont.end()) {
86  WRITE_ERROR("Polygon '" + id + "' could not be added.");
87  delete poly;
88  return false;
89  }
90  myPolyCont[id] = poly;
91  myPolyLayerMap[poly] = layer;
92  return true;
93 }
94 
95 
96 bool
97 PCPolyContainer::insert(const std::string& id, PointOfInterest* poi,
98  int layer, bool ignorePruning) {
99  // check whether the poi lies within the wished area
100  // - if such an area was given
101  if (myDoPrune && !ignorePruning) {
102  if (!myPruningBoundary.around(*poi)) {
103  delete poi;
104  return false;
105  }
106  }
107  // check whether the polygon was named to be a removed one
108  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
109  delete poi;
110  return false;
111  }
112  //
113  POICont::iterator i = myPOICont.find(id);
114  if (i != myPOICont.end()) {
115  WRITE_ERROR("POI '" + id + "' could not be added.");
116  delete poi;
117  return false;
118  }
119  myPOICont[id] = poi;
120  myPOILayerMap[poi] = layer;
121  return true;
122 }
123 
124 
125 bool
126 PCPolyContainer::containsPolygon(const std::string& id) {
127  return myPolyCont.find(id) != myPolyCont.end();
128 }
129 
130 
131 void
133  // polys
134  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); i++) {
135  delete(*i).second;
136  }
137  myPolyCont.clear();
138  myPolyLayerMap.clear();
139  // pois
140  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); i++) {
141  delete(*i).second;
142  }
143  myPOICont.clear();
144  myPOILayerMap.clear();
145 }
146 
147 
148 void
150  WRITE_MESSAGE(" " + toString(getNoPolygons()) + " polygons loaded.");
151  WRITE_MESSAGE(" " + toString(getNoPOIs()) + " pois loaded.");
152 }
153 
154 
155 void
156 PCPolyContainer::save(const std::string& file) {
158  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/additional_file.xsd\"");
159  // write polygons
160  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); ++i) {
161  Polygon* p = i->second;
162  out.openTag(SUMO_TAG_POLY);
166  out.writeAttr(SUMO_ATTR_FILL, p->getFill());
169  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
171  }
172  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
174  }
175  const std::map<std::string, std::string>& attrs = p->getMap();
176  if (attrs.size() != 0) {
177  for (std::map<std::string, std::string>::const_iterator j = attrs.begin(); j != attrs.end(); ++j) {
178  out.openTag(SUMO_TAG_PARAM);
179  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
180  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
181  out.closeTag();
182  }
183  }
184  out.closeTag();
185  }
186  // write pois
187  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); ++i) {
188  PointOfInterest* p = i->second;
189  out.openTag(SUMO_TAG_POI);
194  out.writeAttr(SUMO_ATTR_X, p->x());
195  out.writeAttr(SUMO_ATTR_Y, p->y());
196  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
198  }
199  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
201  }
202  if (p->getWidth() != Shape::DEFAULT_IMG_WIDTH) {
204  }
205  if (p->getHeight() != Shape::DEFAULT_IMG_HEIGHT) {
207  }
208  const std::map<std::string, std::string>& attrs = p->getMap();
209  if (attrs.size() != 0) {
210  for (std::map<std::string, std::string>::const_iterator j = attrs.begin(); j != attrs.end(); ++j) {
211  out.openTag(SUMO_TAG_PARAM);
212  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
213  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
214  out.closeTag();
215  }
216  }
217  out.closeTag();
218  }
219  out.close();
220 }
221 
222 
223 int
224 PCPolyContainer::getEnumIDFor(const std::string& key) {
225  if (myIDEnums.find(key) == myIDEnums.end()) {
226  myIDEnums[key] = 0;
227  return 0;
228  } else {
229  myIDEnums[key] = myIDEnums[key] + 1;
230  return myIDEnums[key];
231  }
232 }
233 
234 
235 
236 /****************************************************************************/
237 
void clear()
Removes all stored objects (polygons and pois)
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
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:101
unsigned int getNoPOIs()
Returns the number of stored pois.
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:152
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' ids.
PCPolyContainer(bool prune, const Boundary &pruningBoundary, const std::vector< std::string > &removeByNames)
Constructor.
A layer number.
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: Polygon.h:80
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.
bool insert(const std::string &id, Polygon *poly, int layer, bool ignorePruning=false)
Adds a polygon to the storage.
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
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:50
void save(const std::string &file)
Saves the stored polygons into the given file.
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:151
Boundary myPruningBoundary
The boundary that described the rectangle within which an object must be in order to be kept...
const std::string & getID() const
Returns the id.
Definition: Named.h:60
static const SUMOReal DEFAULT_IMG_HEIGHT
Definition: Shape.h:154
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:53
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:79
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
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
Returns whether the image width of the POI.
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:71
bool closeTag()
Closes the most recently opened tag.
A point-of-interest.
SUMOReal getLayer() const
Returns the layer of the Shape.
Definition: Shape.h:87
PolyCont myPolyCont
The polygon container, accessed by the polygons' ids.
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:71
bool myDoPrune
Information whether the pruning boundary shall be used.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
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:88
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:94
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:153