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-2014 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& pruningBoundary,
55  const std::vector<std::string>& removeByNames)
56  : myPruningBoundary(pruningBoundary), myDoPrune(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 ignorePruning) {
68  // check whether the polygon lies within the wished area
69  // - if such an area was given
70  if (myDoPrune && !ignorePruning) {
71  Boundary b = poly->getShape().getBoxBoundary();
73  delete poly;
74  return false;
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 false;
81  }
82  //
83  PolyCont::iterator i = myPolyCont.find(id);
84  if (i != myPolyCont.end()) {
85  WRITE_ERROR("Polygon '" + id + "' could not be added.");
86  delete poly;
87  return false;
88  }
89  myPolyCont[id] = poly;
90  myPolyLayerMap[poly] = layer;
91  return true;
92 }
93 
94 
95 bool
96 PCPolyContainer::insert(const std::string& id, PointOfInterest* poi,
97  int layer, bool ignorePruning) {
98  // check whether the poi lies within the wished area
99  // - if such an area was given
100  if (myDoPrune && !ignorePruning) {
101  if (!myPruningBoundary.around(*poi)) {
102  delete poi;
103  return false;
104  }
105  }
106  // check whether the polygon was named to be a removed one
107  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
108  delete poi;
109  return false;
110  }
111  //
112  POICont::iterator i = myPOICont.find(id);
113  if (i != myPOICont.end()) {
114  WRITE_ERROR("POI '" + id + "' could not be added.");
115  delete poi;
116  return false;
117  }
118  myPOICont[id] = poi;
119  myPOILayerMap[poi] = layer;
120  return true;
121 }
122 
123 
124 bool
125 PCPolyContainer::containsPolygon(const std::string& id) {
126  return myPolyCont.find(id) != myPolyCont.end();
127 }
128 
129 
130 void
132  // polys
133  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); i++) {
134  delete(*i).second;
135  }
136  myPolyCont.clear();
137  myPolyLayerMap.clear();
138  // pois
139  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); i++) {
140  delete(*i).second;
141  }
142  myPOICont.clear();
143  myPOILayerMap.clear();
144 }
145 
146 
147 void
149  WRITE_MESSAGE(" " + toString(getNoPolygons()) + " polygons loaded.");
150  WRITE_MESSAGE(" " + toString(getNoPOIs()) + " pois loaded.");
151 }
152 
153 
154 void
155 PCPolyContainer::save(const std::string& file) {
157  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/additional_file.xsd\"");
158  // write polygons
159  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); ++i) {
160  Polygon* p = i->second;
161  out.openTag(SUMO_TAG_POLY);
165  out.writeAttr(SUMO_ATTR_FILL, p->getFill());
168  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
170  }
171  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
173  }
174  const std::map<std::string, std::string>& attrs = p->getMap();
175  if (attrs.size() != 0) {
176  for (std::map<std::string, std::string>::const_iterator j = attrs.begin(); j != attrs.end(); ++j) {
177  out.openTag(SUMO_TAG_PARAM);
178  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
179  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
180  out.closeTag();
181  }
182  }
183  out.closeTag();
184  }
185  // write pois
186  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); ++i) {
187  PointOfInterest* p = i->second;
188  out.openTag(SUMO_TAG_POI);
193  out.writeAttr(SUMO_ATTR_X, p->x());
194  out.writeAttr(SUMO_ATTR_Y, p->y());
195  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
197  }
198  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
200  }
201  if (p->getWidth() != Shape::DEFAULT_IMG_WIDTH) {
203  }
204  if (p->getHeight() != Shape::DEFAULT_IMG_HEIGHT) {
206  }
207  const std::map<std::string, std::string>& attrs = p->getMap();
208  if (attrs.size() != 0) {
209  for (std::map<std::string, std::string>::const_iterator j = attrs.begin(); j != attrs.end(); ++j) {
210  out.openTag(SUMO_TAG_PARAM);
211  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
212  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
213  out.closeTag();
214  }
215  }
216  out.closeTag();
217  }
218  out.close();
219 }
220 
221 
222 int
223 PCPolyContainer::getEnumIDFor(const std::string& key) {
224  if (myIDEnums.find(key) == myIDEnums.end()) {
225  myIDEnums[key] = 0;
226  return 0;
227  } else {
228  myIDEnums[key] = myIDEnums[key] + 1;
229  return myIDEnums[key];
230  }
231 }
232 
233 
234 
235 /****************************************************************************/
236 
void clear()
Removes all stored objects (polygons and pois)
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
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:151
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:78
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:48
void save(const std::string &file)
Saves the stored polygons into the given file.
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:150
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:153
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:52
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
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
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: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' ids.
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:70
bool myDoPrune
Information whether the pruning boundary shall be used.
#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:86
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:152