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