SUMO - Simulation of Urban MObility
NIVissimDisturbance.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // -------------------
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
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 
33 #include <map>
34 #include <string>
35 #include <iostream>
36 #include <cassert>
37 #include <utils/common/ToString.h>
39 #include <utils/geom/GeomHelper.h>
40 #include <utils/geom/Boundary.h>
41 #include <netbuild/NBEdge.h>
42 #include <netbuild/NBNode.h>
43 #include <netbuild/NBEdgeCont.h>
44 #include <netbuild/NBNodeCont.h>
45 #include "NIVissimEdge.h"
46 #include "NIVissimConnection.h"
47 #include "NIVissimNodeDef.h"
48 #include "NIVissimDisturbance.h"
50 
51 
52 // ===========================================================================
53 // static member variables
54 // ===========================================================================
56 int NIVissimDisturbance::myRunningID = 100000000;
57 
59 
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
65  const std::string& name,
66  const NIVissimExtendedEdgePoint& edge,
67  const NIVissimExtendedEdgePoint& by)
68  : myID(id), myNode(-1), myName(name), myEdge(edge), myDisturbance(by) {}
69 
70 
72 
73 
74 
75 bool
76 NIVissimDisturbance::dictionary(const std::string& name,
77  const NIVissimExtendedEdgePoint& edge,
78  const NIVissimExtendedEdgePoint& by) {
79  int nid = myRunningID++;
81  new NIVissimDisturbance(nid, name, edge, by);
82  if (!dictionary(nid, o)) {
83  delete o;
84  }
85  return true;
86 }
87 
88 
89 bool
91  DictType::iterator i = myDict.find(id);
92  if (i == myDict.end()) {
93  myDict[id] = o;
94  return true;
95  }
96  return false;
97 }
98 
99 
102  DictType::iterator i = myDict.find(id);
103  if (i == myDict.end()) {
104  return 0;
105  }
106  return (*i).second;
107 }
108 
109 std::vector<int>
111  std::vector<int> ret;
112  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
113  if ((*i).second->crosses(poly)) {
114  ret.push_back((*i).second->myID);
115  }
116  }
117  return ret;
118 }
119 
120 
121 void
123  assert(myBoundary == 0);
124  Boundary* bound = new Boundary();
126  bound->add(myEdge.getGeomPosition());
127  }
130  }
131  myBoundary = bound;
132  assert(myBoundary != 0 && myBoundary->xmax() >= myBoundary->xmin());
133 }
134 
135 
136 
137 bool
139  NBNodeCont& nc, NBEdgeCont& ec) {
140  myNode = 0;
141  NIVissimConnection* pc =
143  NIVissimConnection* bc =
145  if (pc == 0 && bc == 0) {
146  // This has not been tested completely, yet
147  // Both competing abstract edges are normal edges
148  // We have to find a crossing point, build a node here,
149  // split both edges and add the connections
152  WRITE_WARNING("Ugly split to prohibit '" + toString<int>(e1->getID()) + "' by '" + toString<int>(e2->getID()) + "'.");
153  Position pos = e1->crossesEdgeAtPoint(e2);
154  std::string id1 = toString<int>(e1->getID()) + "x" + toString<int>(e2->getID());
155  std::string id2 = toString<int>(e2->getID()) + "x" + toString<int>(e1->getID());
156  NBNode* node1 = nc.retrieve(id1);
157  NBNode* node2 = nc.retrieve(id2);
158  NBNode* node = 0;
159  assert(node1 == 0 || node2 == 0);
160  if (node1 == 0 && node2 == 0) {
162  return false;
163  /* node = new NBNode(id1, pos.x(), pos.y(), "priority");
164  if(!myNodeCont.insert(node)) {
165  "nope, NIVissimDisturbance" << endl;
166  throw 1;
167  }*/
168  } else {
169  node = node1 == 0 ? node2 : node1;
170  }
171  ec.splitAt(dc, ec.retrievePossiblySplit(toString<int>(e1->getID()), myEdge.getPosition()), node);
172  ec.splitAt(dc, ec.retrievePossiblySplit(toString<int>(e2->getID()), myDisturbance.getPosition()), node);
173  // !!! in some cases, one of the edges is not being build because it's too short
174  // !!! what to do in these cases?
175  NBEdge* mayDriveFrom = ec.retrieve(toString<int>(e1->getID()) + "[0]");
176  NBEdge* mayDriveTo = ec.retrieve(toString<int>(e1->getID()) + "[1]");
177  NBEdge* mustStopFrom = ec.retrieve(toString<int>(e2->getID()) + "[0]");
178  NBEdge* mustStopTo = ec.retrieve(toString<int>(e2->getID()) + "[1]");
179  if (mayDriveFrom != 0 && mayDriveTo != 0 && mustStopFrom != 0 && mustStopTo != 0) {
180  node->addSortedLinkFoes(
181  NBConnection(mayDriveFrom, mayDriveTo),
182  NBConnection(mayDriveFrom, mayDriveTo));
183  } else {
185  return false;
186  // !!! warning
187  }
188 // }
189  } else if (pc != 0 && bc == 0) {
190  // The prohibited abstract edge is a connection, the other
191  // is not;
192  // The connection will be prohibitesd by all connections
193  // outgoing from the "real" edge
194 
196  if (e == 0) {
197  WRITE_WARNING("Could not prohibit '" + toString<int>(myEdge.getEdgeID()) + "' by '" + toString<int>(myDisturbance.getEdgeID()) + "'. Have not found disturbance.");
199  return false;
200  }
201  if (e->getFromNode() == e->getToNode()) {
202  WRITE_WARNING("Could not prohibit '" + toString<int>(myEdge.getEdgeID()) + "' by '" + toString<int>(myDisturbance.getEdgeID()) + "'. Disturbance connects same node.");
204  // What to do with self-looping edges?
205  return false;
206  }
207  // get the begin of the prohibited connection
208  std::string id_pcoe = toString<int>(pc->getFromEdgeID());
209  std::string id_pcie = toString<int>(pc->getToEdgeID());
210  NBEdge* pcoe = ec.retrievePossiblySplit(id_pcoe, id_pcie, true);
211  NBEdge* pcie = ec.retrievePossiblySplit(id_pcie, id_pcoe, false);
212  // check whether it's ending node is the node the prohibited
213  // edge end at
214  if (pcoe != 0 && pcie != 0 && pcoe->getToNode() == e->getToNode()) {
215  // if so, simply prohibit the connections
216  NBNode* node = e->getToNode();
217  const EdgeVector& connected = e->getConnectedEdges();
218  for (EdgeVector::const_iterator i = connected.begin(); i != connected.end(); i++) {
219  node->addSortedLinkFoes(
220  NBConnection(e, *i),
221  NBConnection(pcoe, pcie));
222  }
223  } else {
224  WRITE_WARNING("Would have to split edge '" + e->getID() + "' to build a prohibition");
226  // quite ugly - why was it not build?
227  return false;
228  /*
229  std::string nid1 = e->getID() + "[0]";
230  std::string nid2 = e->getID() + "[1]";
231 
232  if(ec.splitAt(e, node)) {
233  node->addSortedLinkFoes(
234  NBConnection(
235  ec.retrieve(nid1),
236  ec.retrieve(nid2)
237  ),
238  getConnection(node, myEdge.getEdgeID())
239  );
240  }
241  */
242  }
243  } else if (bc != 0 && pc == 0) {
244  // The prohibiting abstract edge is a connection, the other
245  // is not;
246  // We have to split the other one and add the prohibition
247  // description
248 
249  NBEdge* e = ec.retrievePossiblySplit(toString<int>(myEdge.getEdgeID()), myEdge.getPosition());
250  if (e == 0) {
251  WRITE_WARNING("Could not prohibit '" + toString<int>(myEdge.getEdgeID()) + "' - it was not built.");
252  return false;
253  }
254  std::string nid1 = e->getID() + "[0]";
255  std::string nid2 = e->getID() + "[1]";
256  if (e->getFromNode() == e->getToNode()) {
257  WRITE_WARNING("Could not prohibit '" + toString<int>(myEdge.getEdgeID()) + "' by '" + toString<int>(myDisturbance.getEdgeID()) + "'.");
259  // What to do with self-looping edges?
260  return false;
261  }
262  // get the begin of the prohibiting connection
263  std::string id_bcoe = toString<int>(bc->getFromEdgeID());
264  std::string id_bcie = toString<int>(bc->getToEdgeID());
265  NBEdge* bcoe = ec.retrievePossiblySplit(id_bcoe, id_bcie, true);
266  NBEdge* bcie = ec.retrievePossiblySplit(id_bcie, id_bcoe, false);
267  // check whether it's ending node is the node the prohibited
268  // edge end at
269  if (bcoe != 0 && bcie != 0 && bcoe->getToNode() == e->getToNode()) {
270  // if so, simply prohibit the connections
271  NBNode* node = e->getToNode();
272  const EdgeVector& connected = e->getConnectedEdges();
273  for (EdgeVector::const_iterator i = connected.begin(); i != connected.end(); i++) {
274  node->addSortedLinkFoes(
275  NBConnection(bcoe, bcie),
276  NBConnection(e, *i));
277  }
278  } else {
279  WRITE_WARNING("Would have to split edge '" + e->getID() + "' to build a prohibition");
281  return false;
282  /*
283  // quite ugly - why was it not build?
284  if(ec.splitAt(e, node)) {
285  node->addSortedLinkFoes(
286  getConnection(node, myDisturbance.getEdgeID()),
287  NBConnection(
288  ec.retrieve(nid1),
289  ec.retrieve(nid2)
290  )
291  );
292  }
293  */
294  }
295  } else {
296  // both the prohibiting and the prohibited abstract edges
297  // are connections
298  // We can retrieve the conected edges and add the desription
300  NBConnection conn2 = getConnection(node, myEdge.getEdgeID());
301  if (!conn1.check(ec) || !conn2.check(ec)) {
303  return false;
304  }
305  node->addSortedLinkFoes(conn1, conn2);
306  }
307  return true;
308 }
309 
310 
315  NBEdge* from =
316  node->getPossiblySplittedIncoming(toString<int>(c->getFromEdgeID()));
317  NBEdge* to =
318  node->getPossiblySplittedOutgoing(toString<int>(c->getToEdgeID()));
319 
320  // source is a connection
321  return NBConnection(toString<int>(c->getFromEdgeID()), from,
322  toString<int>(c->getToEdgeID()), to);
323  } else {
324  WRITE_WARNING("NIVissimDisturbance: no connection");
326 // throw 1; // !!! what to do?
327  }
328 
329 }
330 
331 void
333  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
334  delete(*i).second;
335  }
336  myDict.clear();
337 }
338 
339 
340 void
342  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
343  NIVissimDisturbance* d = (*i).second;
344  NIVissimAbstractEdge::dictionary(d->myEdge.getEdgeID())->addDisturbance((*i).first);
345  NIVissimAbstractEdge::dictionary(d->myDisturbance.getEdgeID())->addDisturbance((*i).first);
346  }
347  /* for(DictType::iterator i=myDict.begin(); i!=myDict.end(); i++) {
348  delete (*i).second;
349  }
350  */
351 }
352 
353 
354 void
356  if (refusedProhibits > 0) {
357  WRITE_WARNING("Could not build " + toString<int>(refusedProhibits) + " of " + toString<int>((int)myDict.size()) + " disturbances.");
358  }
359 }
360 
361 
362 
363 /****************************************************************************/
364 
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:106
bool check(const NBEdgeCont &ec)
checks whether the edges are still valid
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
static DictType myDict
static bool dictionary(int id, NIVissimAbstractEdge *e)
static std::vector< int > getWithin(const AbstractPoly &poly)
static const NBConnection InvalidConnection
Definition: NBConnection.h:127
The representation of a single edge during network building.
Definition: NBEdge.h:71
A container for districts.
static bool dictionary(const std::string &name, const NIVissimExtendedEdgePoint &edge, const NIVissimExtendedEdgePoint &by)
bool addToNode(NBNode *node, NBDistrictCont &dc, NBNodeCont &nc, NBEdgeCont &ec)
bool splitAt(NBDistrictCont &dc, NBEdge *edge, NBNode *node)
Splits the edge at the position nearest to the given node.
Definition: NBEdgeCont.cpp:416
static bool dictionary(int id, NIVissimConnection *o)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
A temporary storage for edges imported from Vissim.
Definition: NIVissimEdge.h:60
NIVissimExtendedEdgePoint myEdge
NBEdge * getPossiblySplittedIncoming(const std::string &edgeid)
get possibly splitted incoming edge
Definition: NBNode.cpp:1265
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
Definition: NBEdgeCont.cpp:287
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
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.
std::map< int, NIVissimDisturbance * > DictType
NBEdge * getPossiblySplittedOutgoing(const std::string &edgeid)
get possibly splitted outgoing edge
Definition: NBNode.cpp:1278
Position crossesEdgeAtPoint(NIVissimAbstractEdge *c) const
NBConnection getConnection(NBNode *node, int aedgeid)
NIVissimExtendedEdgePoint myDisturbance
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:41
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:251
Represents a single node (junction) during network building.
Definition: NBNode.h:75
NIVissimDisturbance(int id, const std::string &name, const NIVissimExtendedEdgePoint &edge, const NIVissimExtendedEdgePoint &by)
void addSortedLinkFoes(const NBConnection &mayDrive, const NBConnection &mustStop)
add shorted link FOES
Definition: NBNode.cpp:1248
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:1107
static void dict_SetDisturbances()
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:427
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:86
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:434