SUMO - Simulation of Urban MObility
NIVissimDistrictConnection.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // -------------------
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <map>
33 #include <string>
34 #include <algorithm>
35 #include <cassert>
37 #include <utils/common/ToString.h>
38 #include <utils/geom/Position.h>
39 #include <utils/geom/GeomHelper.h>
42 #include "NIVissimAbstractEdge.h"
43 #include "NIVissimEdge.h"
44 #include <netbuild/NBEdge.h>
45 #include <netbuild/NBEdgeCont.h>
46 #include <netbuild/NBNode.h>
47 #include <netbuild/NBNodeCont.h>
48 #include <netbuild/NBDistrict.h>
53 
54 
55 // ===========================================================================
56 // static member definitions
57 // ===========================================================================
59 std::map<int, std::vector<int> > NIVissimDistrictConnection::myDistrictsConnections;
60 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
66  const std::string& name,
67  const std::vector<int>& districts, const std::vector<double>& percentages,
68  int edgeid, double position,
69  const std::vector<std::pair<int, int> >& assignedVehicles)
70  : myID(id), myName(name), myDistricts(districts),
71  myEdgeID(edgeid), myPosition(position),
72  myAssignedVehicles(assignedVehicles) {
73  std::vector<int>::iterator i = myDistricts.begin();
74  std::vector<double>::const_iterator j = percentages.begin();
75  while (i != myDistricts.end()) {
76  myPercentages[*i] = *j;
77  i++;
78  j++;
79  }
80 }
81 
82 
84 
85 
86 
87 bool
88 NIVissimDistrictConnection::dictionary(int id, const std::string& name,
89  const std::vector<int>& districts, const std::vector<double>& percentages,
90  int edgeid, double position,
91  const std::vector<std::pair<int, int> >& assignedVehicles) {
93  new NIVissimDistrictConnection(id, name, districts, percentages,
94  edgeid, position, assignedVehicles);
95  if (!dictionary(id, o)) {
96  delete o;
97  return false;
98  }
99  return true;
100 }
101 
102 
103 bool
105  DictType::iterator i = myDict.find(id);
106  if (i == myDict.end()) {
107  myDict[id] = o;
108  return true;
109  }
110  return false;
111 }
112 
113 
116  DictType::iterator i = myDict.find(id);
117  if (i == myDict.end()) {
118  return 0;
119  }
120  return (*i).second;
121 }
122 
123 void
125  // pre-assign connections to districts
126  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
127  NIVissimDistrictConnection* c = (*i).second;
128  const std::vector<int>& districts = c->myDistricts;
129  for (std::vector<int>::const_iterator j = districts.begin(); j != districts.end(); j++) {
130  // assign connection to district
131  myDistrictsConnections[*j].push_back((*i).first);
132  }
133  }
134 }
135 
136 
137 void
139  for (std::map<int, std::vector<int> >::iterator k = myDistrictsConnections.begin(); k != myDistrictsConnections.end(); k++) {
140  const std::vector<int>& connections = (*k).second;
141  for (std::vector<int>::const_iterator j = connections.begin(); j != connections.end(); j++) {
143  c->checkEdgeEnd();
144  }
145  }
146 }
147 
148 
149 void
152  assert(edge != 0);
154 }
155 
156 
157 void
159  NBNodeCont& nc) {
160  for (std::map<int, std::vector<int> >::iterator k = myDistrictsConnections.begin(); k != myDistrictsConnections.end(); k++) {
161  // get the connections
162  const std::vector<int>& connections = (*k).second;
163  // retrieve the current district
164  std::string dsid = toString<int>((*k).first);
165  NBDistrict* district = new NBDistrict(dsid);
166  dc.insert(district);
167  // compute the middle of the district
168  PositionVector pos;
169  for (std::vector<int>::const_iterator j = connections.begin(); j != connections.end(); j++) {
171  pos.push_back(c->geomPosition());
172  }
173  Position distCenter = pos.getPolygonCenter();
174  if (connections.size() == 1) { // !!! ok, ok, maybe not the best way just to add an offset
175  distCenter.add(10, 10);
176  }
177  district->setCenter(distCenter);
178  // build the node
179  std::string id = "District" + district->getID();
180  NBNode* districtNode =
181  new NBNode(id, district->getPosition(), district);
182  if (!nc.insert(districtNode)) {
183  throw 1;
184  }
185  }
186 }
187 
188 void
190  NBEdgeCont& ec,
191  NBNodeCont& nc) {
192  // add the sources and sinks
193  // their normalised probability is computed within NBDistrict
194  // to avoid double code writing and more securty within the converter
195  // go through the district table
196  for (std::map<int, std::vector<int> >::iterator k = myDistrictsConnections.begin(); k != myDistrictsConnections.end(); k++) {
197  // get the connections
198  const std::vector<int>& connections = (*k).second;
199  // retrieve the current district
200  NBDistrict* district =
201  dc.retrieve(toString<int>((*k).first));
202  NBNode* districtNode = nc.retrieve("District" + district->getID());
203  assert(district != 0 && districtNode != 0);
204 
205  for (std::vector<int>::const_iterator l = connections.begin(); l != connections.end(); l++) {
207  // get the edge to connect the parking place to
208  NBEdge* e = ec.retrieve(toString<int>(c->myEdgeID));
209  if (e == 0) {
210  e = ec.retrievePossiblySplit(toString<int>(c->myEdgeID), c->myPosition);
211  }
212  if (e == 0) {
213  WRITE_WARNING("Could not build district '" + toString<int>((*k).first) + "' - edge '" + toString<int>(c->myEdgeID) + "' is missing.");
214  continue;
215  }
216  std::string id = "ParkingPlace" + toString<int>(*l);
217  NBNode* parkingPlace = nc.retrieve(id);
218  if (parkingPlace == 0) {
219  double pos = c->getPosition();
220  if (pos < e->getLength() - pos) {
221  parkingPlace = e->getFromNode();
222  parkingPlace->invalidateIncomingConnections();
223  } else {
224  parkingPlace = e->getToNode();
225  parkingPlace->invalidateOutgoingConnections();
226  }
227  }
228  assert(
229  e->getToNode() == parkingPlace
230  ||
231  e->getFromNode() == parkingPlace);
232 
233  // build the connection to the source
234  if (e->getFromNode() == parkingPlace) {
235  id = "VissimFromParkingplace" + toString<int>((*k).first) + "-" + toString<int>(c->myID);
236  NBEdge* source =
237  new NBEdge(id, districtNode, parkingPlace,
238  "Connection", c->getMeanSpeed(/*distc*/) / (double) 3.6, 3, -1,
240  if (!ec.insert(source)) { // !!! in den Konstruktor
241  throw 1; // !!!
242  }
243  double percNormed =
244  c->myPercentages[(*k).first];
245  if (!district->addSource(source, percNormed)) {
246  throw 1;
247  }
248  }
249 
250  // build the connection to the destination
251  if (e->getToNode() == parkingPlace) {
252  id = "VissimToParkingplace" + toString<int>((*k).first) + "-" + toString<int>(c->myID);
253  NBEdge* destination =
254  new NBEdge(id, parkingPlace, districtNode,
255  "Connection", (double) 100 / (double) 3.6, 2, -1,
257  if (!ec.insert(destination)) { // !!! (in den Konstruktor)
258  throw 1; // !!!
259  }
260  double percNormed2 =
261  c->myPercentages[(*k).first];
262  if (!district->addSink(destination, percNormed2)) {
263  throw 1; // !!!
264  }
265  }
266 
267  /*
268  if(e->getToNode()==districtNode) {
269  double percNormed =
270  c->myPercentages[(*k).first];
271  district->addSink(e, percNormed);
272  }
273  if(e->getFromNode()==districtNode) {
274  double percNormed =
275  c->myPercentages[(*k).first];
276  district->addSource(e, percNormed);
277  }
278  */
279  }
280 
281  /*
282  // add them as sources and sinks to the current district
283  for(std::vector<int>::const_iterator l=connections.begin(); l!=connections.end(); l++) {
284  // get the current connections
285  NIVissimDistrictConnection *c = dictionary(*l);
286  // get the edge to connect the parking place to
287  NBEdge *e = NBEdgeCont::retrieve(toString<int>(c->myEdgeID));
288  Position edgepos = c->geomPosition();
289  NBNode *edgeend = e->tryGetNodeAtPosition(c->myPosition,
290  e->getLength()/4.0);
291  if(edgeend==0) {
292  // Edge splitting omitted on build district connections by now
293  assert(false);
294  }
295 
296  // build the district-node if not yet existing
297  std::string id = "VissimParkingplace" + district->getID();
298  NBNode *districtNode = nc.retrieve(id);
299  assert(districtNode!=0);
300 
301  if(e->getToNode()==edgeend) {
302  // build the connection to the source
303  id = std::string("VissimFromParkingplace")
304  + toString<int>((*k).first) + "-"
305  + toString<int>(c->myID);
306  NBEdge *source =
307  new NBEdge(id, id, districtNode, edgeend,
308  "Connection", 100/3.6, 2, 100, 0,
309  NBEdge::EDGEFUNCTION_SOURCE);
310  NBEdgeCont::insert(source); // !!! (in den Konstruktor)
311  double percNormed =
312  c->myPercentages[(*k).first];
313  district->addSource(source, percNormed);
314  } else {
315  // build the connection to the destination
316  id = std::string("VissimToParkingplace")
317  + toString<int>((*k).first) + "-"
318  + toString<int>(c->myID);
319  NBEdge *destination =
320  new NBEdge(id, id, edgeend, districtNode,
321  "Connection", 100/3.6, 2, 100, 0,
322  NBEdge::EDGEFUNCTION_SINK);
323  NBEdgeCont::insert(destination); // !!! (in den Konstruktor)
324 
325  // add both the source and the sink to the district
326  double percNormed =
327  c->myPercentages[(*k).first];
328  district->addSink(destination, percNormed);
329  }
330  }
331  */
332  }
333 }
334 
335 
336 
337 Position
340  return e->getGeomPosition(myPosition);
341 }
342 
343 
346  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
347  if ((*i).second->myEdgeID == edgeid) {
348  return (*i).second;
349  }
350  }
351  return 0;
352 }
353 
354 
355 void
357  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
358  delete(*i).second;
359  }
360  myDict.clear();
361 }
362 
363 
364 double
366  //assert(myAssignedVehicles.size()!=0);
367  if (myAssignedVehicles.size() == 0) {
368  WRITE_WARNING("No streams assigned at district'" + toString(myID) + "'.\n Using default speed 200km/h");
369  return (double) 200 / (double) 3.6;
370  }
371  double speed = 0;
372  std::vector<std::pair<int, int> >::const_iterator i;
373  for (i = myAssignedVehicles.begin(); i != myAssignedVehicles.end(); i++) {
374  speed += getRealSpeed((*i).second);
375  }
376  return speed / (double) myAssignedVehicles.size();
377 }
378 
379 
380 double
382  std::string id = toString<int>(distNo);
383  Distribution* dist = DistributionCont::dictionary("speed", id);
384  if (dist == 0) {
385  WRITE_WARNING("The referenced speed distribution '" + id + "' is not known.");
386  WRITE_WARNING(". Using default.");
387  return OptionsCont::getOptions().getFloat("vissim.default-speed");
388  }
389  assert(dist != 0);
390  double speed = dist->getMax();
391  if (speed < 0 || speed > 1000) {
392  WRITE_WARNING(" False speed at district '" + id);
393  WRITE_WARNING(". Using default.");
394  speed = OptionsCont::getOptions().getFloat("vissim.default-speed");
395  }
396  return speed;
397 }
398 
399 
400 
401 /****************************************************************************/
402 
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:108
Position getGeomPosition(double pos) const
NBDistrict * retrieve(const std::string &id) const
Returns the districts with the given id.
static void dict_BuildDistricts(NBDistrictCont &dc, NBEdgeCont &ec, NBNodeCont &nc)
Builds the districts.
static std::map< int, std::vector< int > > myDistrictsConnections
Map from ditricts to connections.
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:132
The representation of a single edge during network building.
Definition: NBEdge.h:70
A container for districts.
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:257
Position geomPosition() const
Returns the position The position yields from the edge geometry and the place the connection is plaed...
static bool dictionary(const std::string &type, const std::string &id, Distribution *d)
Adds a distribution of the given type and name to the container.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static NIVissimDistrictConnection * dict_findForEdge(int edgeid)
Returns the connection to a district placed at the given node Yep, there onyl should be one...
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:254
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
DistrictPercentages myPercentages
A map how many vehicles (key, amount) should leave to a district (key)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
A temporary storage for edges imported from Vissim.
Definition: NIVissimEdge.h:59
int myID
The id of the connections.
A class representing a single district.
Definition: NBDistrict.h:71
double getPosition() const
Returns the position of the connection at the edge.
std::vector< std::pair< int, int > > myAssignedVehicles
The vehicles using this connection.
void invalidateIncomingConnections()
invalidate incoming connections
Definition: NBNode.cpp:1339
int myEdgeID
The id of the connected edge.
bool insert(NBEdge *edge, bool ignorePrunning=false)
Adds an edge to the dictionary.
Definition: NBEdgeCont.cpp:157
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
Definition: NBEdgeCont.cpp:286
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
A list of positions.
Position getPolygonCenter() const
Returns the arithmetic of all corner points.
std::vector< int > myDistricts
The connected districts.
void invalidateOutgoingConnections()
invalidate outgoing connections
Definition: NBNode.cpp:1347
void checkDistrictConnectionExistanceAt(double pos)
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
static DictType myDict
District connection dictionary.
static bool dictionary(int id, const std::string &name, const std::string &type, int noLanes, double zuschlag1, double zuschlag2, double length, const PositionVector &geom, const NIVissimClosedLanesVector &clv)
Adds the described item to the dictionary Builds the edge first.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
NIVissimDistrictConnection(int id, const std::string &name, const std::vector< int > &districts, const std::vector< double > &percentages, int edgeid, double position, const std::vector< std::pair< int, int > > &assignedVehicles)
Contructor.
bool addSink(NBEdge *const sink, double weight)
Adds a sink.
Definition: NBDistrict.cpp:89
bool insert(NBDistrict *const district)
Adds a district to the dictionary.
static void clearDict()
Clears the dictionary.
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:250
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:79
Represents a single node (junction) during network building.
Definition: NBNode.h:74
double myPosition
The position on the edge.
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:426
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:66
std::map< int, NIVissimDistrictConnection * > DictType
Definition of a dictionary of district connections.
bool addSource(NBEdge *const source, double weight)
Adds a source.
Definition: NBDistrict.cpp:76
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:433
static void dict_BuildDistrictNodes(NBDistrictCont &dc, NBNodeCont &nc)
Builds the nodes that belong to a district.
static bool dictionary(int id, const std::string &name, const std::vector< int > &districts, const std::vector< double > &percentages, int edgeid, double position, const std::vector< std::pair< int, int > > &assignedVehicles)
Inserts the connection into the dictionary after building it.