SUMO - Simulation of Urban MObility
NIVissimDisturbance.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 /****************************************************************************/
18 // -------------------
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 
32 #include <map>
33 #include <string>
34 #include <iostream>
35 #include <cassert>
36 #include <utils/common/ToString.h>
38 #include <utils/geom/GeomHelper.h>
39 #include <utils/geom/Boundary.h>
40 #include <netbuild/NBEdge.h>
41 #include <netbuild/NBNode.h>
42 #include <netbuild/NBEdgeCont.h>
43 #include <netbuild/NBNodeCont.h>
44 #include "NIVissimEdge.h"
45 #include "NIVissimConnection.h"
46 #include "NIVissimNodeDef.h"
47 #include "NIVissimDisturbance.h"
49 
50 
51 // ===========================================================================
52 // static member variables
53 // ===========================================================================
55 int NIVissimDisturbance::myRunningID = 100000000;
56 
58 
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
64  const std::string& name,
65  const NIVissimExtendedEdgePoint& edge,
66  const NIVissimExtendedEdgePoint& by)
67  : myID(id), myNode(-1), myName(name), myEdge(edge), myDisturbance(by) {}
68 
69 
71 
72 
73 
74 bool
75 NIVissimDisturbance::dictionary(const std::string& name,
76  const NIVissimExtendedEdgePoint& edge,
77  const NIVissimExtendedEdgePoint& by) {
78  int nid = myRunningID++;
80  new NIVissimDisturbance(nid, name, edge, by);
81  if (!dictionary(nid, o)) {
82  delete o;
83  }
84  return true;
85 }
86 
87 
88 bool
90  DictType::iterator i = myDict.find(id);
91  if (i == myDict.end()) {
92  myDict[id] = o;
93  return true;
94  }
95  return false;
96 }
97 
98 
101  DictType::iterator i = myDict.find(id);
102  if (i == myDict.end()) {
103  return 0;
104  }
105  return (*i).second;
106 }
107 
108 std::vector<int>
110  std::vector<int> ret;
111  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
112  if ((*i).second->crosses(poly)) {
113  ret.push_back((*i).second->myID);
114  }
115  }
116  return ret;
117 }
118 
119 
120 void
122  assert(myBoundary == 0);
123  Boundary* bound = new Boundary();
125  bound->add(myEdge.getGeomPosition());
126  }
129  }
130  myBoundary = bound;
131  assert(myBoundary != 0 && myBoundary->xmax() >= myBoundary->xmin());
132 }
133 
134 
135 
136 bool
138  NBNodeCont& nc, NBEdgeCont& ec) {
139  myNode = 0;
140  NIVissimConnection* pc =
142  NIVissimConnection* bc =
144  if (pc == 0 && bc == 0) {
145  // This has not been tested completely, yet
146  // Both competing abstract edges are normal edges
147  // We have to find a crossing point, build a node here,
148  // split both edges and add the connections
151  WRITE_WARNING("Ugly split to prohibit '" + toString<int>(e1->getID()) + "' by '" + toString<int>(e2->getID()) + "'.");
152  Position pos = e1->crossesEdgeAtPoint(e2);
153  std::string id1 = toString<int>(e1->getID()) + "x" + toString<int>(e2->getID());
154  std::string id2 = toString<int>(e2->getID()) + "x" + toString<int>(e1->getID());
155  NBNode* node1 = nc.retrieve(id1);
156  NBNode* node2 = nc.retrieve(id2);
157  NBNode* node = 0;
158  assert(node1 == 0 || node2 == 0);
159  if (node1 == 0 && node2 == 0) {
161  return false;
162  /* node = new NBNode(id1, pos.x(), pos.y(), "priority");
163  if(!myNodeCont.insert(node)) {
164  "nope, NIVissimDisturbance" << endl;
165  throw 1;
166  }*/
167  } else {
168  node = node1 == 0 ? node2 : node1;
169  }
170  ec.splitAt(dc, ec.retrievePossiblySplit(toString<int>(e1->getID()), myEdge.getPosition()), node);
171  ec.splitAt(dc, ec.retrievePossiblySplit(toString<int>(e2->getID()), myDisturbance.getPosition()), node);
172  // !!! in some cases, one of the edges is not being build because it's too short
173  // !!! what to do in these cases?
174  NBEdge* mayDriveFrom = ec.retrieve(toString<int>(e1->getID()) + "[0]");
175  NBEdge* mayDriveTo = ec.retrieve(toString<int>(e1->getID()) + "[1]");
176  NBEdge* mustStopFrom = ec.retrieve(toString<int>(e2->getID()) + "[0]");
177  NBEdge* mustStopTo = ec.retrieve(toString<int>(e2->getID()) + "[1]");
178  if (mayDriveFrom != 0 && mayDriveTo != 0 && mustStopFrom != 0 && mustStopTo != 0) {
179  node->addSortedLinkFoes(
180  NBConnection(mayDriveFrom, mayDriveTo),
181  NBConnection(mayDriveFrom, mayDriveTo));
182  } else {
184  return false;
185  // !!! warning
186  }
187 // }
188  } else if (pc != 0 && bc == 0) {
189  // The prohibited abstract edge is a connection, the other
190  // is not;
191  // The connection will be prohibitesd by all connections
192  // outgoing from the "real" edge
193 
195  if (e == 0) {
196  WRITE_WARNING("Could not prohibit '" + toString<int>(myEdge.getEdgeID()) + "' by '" + toString<int>(myDisturbance.getEdgeID()) + "'. Have not found disturbance.");
198  return false;
199  }
200  if (e->getFromNode() == e->getToNode()) {
201  WRITE_WARNING("Could not prohibit '" + toString<int>(myEdge.getEdgeID()) + "' by '" + toString<int>(myDisturbance.getEdgeID()) + "'. Disturbance connects same node.");
203  // What to do with self-looping edges?
204  return false;
205  }
206  // get the begin of the prohibited connection
207  std::string id_pcoe = toString<int>(pc->getFromEdgeID());
208  std::string id_pcie = toString<int>(pc->getToEdgeID());
209  NBEdge* pcoe = ec.retrievePossiblySplit(id_pcoe, id_pcie, true);
210  NBEdge* pcie = ec.retrievePossiblySplit(id_pcie, id_pcoe, false);
211  // check whether it's ending node is the node the prohibited
212  // edge end at
213  if (pcoe != 0 && pcie != 0 && pcoe->getToNode() == e->getToNode()) {
214  // if so, simply prohibit the connections
215  NBNode* node = e->getToNode();
216  const EdgeVector& connected = e->getConnectedEdges();
217  for (EdgeVector::const_iterator i = connected.begin(); i != connected.end(); i++) {
218  node->addSortedLinkFoes(
219  NBConnection(e, *i),
220  NBConnection(pcoe, pcie));
221  }
222  } else {
223  WRITE_WARNING("Would have to split edge '" + e->getID() + "' to build a prohibition");
225  // quite ugly - why was it not build?
226  return false;
227  /*
228  std::string nid1 = e->getID() + "[0]";
229  std::string nid2 = e->getID() + "[1]";
230 
231  if(ec.splitAt(e, node)) {
232  node->addSortedLinkFoes(
233  NBConnection(
234  ec.retrieve(nid1),
235  ec.retrieve(nid2)
236  ),
237  getConnection(node, myEdge.getEdgeID())
238  );
239  }
240  */
241  }
242  } else if (bc != 0 && pc == 0) {
243  // The prohibiting abstract edge is a connection, the other
244  // is not;
245  // We have to split the other one and add the prohibition
246  // description
247 
248  NBEdge* e = ec.retrievePossiblySplit(toString<int>(myEdge.getEdgeID()), myEdge.getPosition());
249  if (e == 0) {
250  WRITE_WARNING("Could not prohibit '" + toString<int>(myEdge.getEdgeID()) + "' - it was not built.");
251  return false;
252  }
253  std::string nid1 = e->getID() + "[0]";
254  std::string nid2 = e->getID() + "[1]";
255  if (e->getFromNode() == e->getToNode()) {
256  WRITE_WARNING("Could not prohibit '" + toString<int>(myEdge.getEdgeID()) + "' by '" + toString<int>(myDisturbance.getEdgeID()) + "'.");
258  // What to do with self-looping edges?
259  return false;
260  }
261  // get the begin of the prohibiting connection
262  std::string id_bcoe = toString<int>(bc->getFromEdgeID());
263  std::string id_bcie = toString<int>(bc->getToEdgeID());
264  NBEdge* bcoe = ec.retrievePossiblySplit(id_bcoe, id_bcie, true);
265  NBEdge* bcie = ec.retrievePossiblySplit(id_bcie, id_bcoe, false);
266  // check whether it's ending node is the node the prohibited
267  // edge end at
268  if (bcoe != 0 && bcie != 0 && bcoe->getToNode() == e->getToNode()) {
269  // if so, simply prohibit the connections
270  NBNode* node = e->getToNode();
271  const EdgeVector& connected = e->getConnectedEdges();
272  for (EdgeVector::const_iterator i = connected.begin(); i != connected.end(); i++) {
273  node->addSortedLinkFoes(
274  NBConnection(bcoe, bcie),
275  NBConnection(e, *i));
276  }
277  } else {
278  WRITE_WARNING("Would have to split edge '" + e->getID() + "' to build a prohibition");
280  return false;
281  /*
282  // quite ugly - why was it not build?
283  if(ec.splitAt(e, node)) {
284  node->addSortedLinkFoes(
285  getConnection(node, myDisturbance.getEdgeID()),
286  NBConnection(
287  ec.retrieve(nid1),
288  ec.retrieve(nid2)
289  )
290  );
291  }
292  */
293  }
294  } else {
295  // both the prohibiting and the prohibited abstract edges
296  // are connections
297  // We can retrieve the conected edges and add the desription
299  NBConnection conn2 = getConnection(node, myEdge.getEdgeID());
300  if (!conn1.check(ec) || !conn2.check(ec)) {
302  return false;
303  }
304  node->addSortedLinkFoes(conn1, conn2);
305  }
306  return true;
307 }
308 
309 
314  NBEdge* from =
315  node->getPossiblySplittedIncoming(toString<int>(c->getFromEdgeID()));
316  NBEdge* to =
317  node->getPossiblySplittedOutgoing(toString<int>(c->getToEdgeID()));
318 
319  // source is a connection
320  return NBConnection(toString<int>(c->getFromEdgeID()), from,
321  toString<int>(c->getToEdgeID()), to);
322  } else {
323  WRITE_WARNING("NIVissimDisturbance: no connection");
325 // throw 1; // !!! what to do?
326  }
327 
328 }
329 
330 void
332  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
333  delete(*i).second;
334  }
335  myDict.clear();
336 }
337 
338 
339 void
341  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
342  NIVissimDisturbance* d = (*i).second;
343  NIVissimAbstractEdge::dictionary(d->myEdge.getEdgeID())->addDisturbance((*i).first);
344  NIVissimAbstractEdge::dictionary(d->myDisturbance.getEdgeID())->addDisturbance((*i).first);
345  }
346  /* for(DictType::iterator i=myDict.begin(); i!=myDict.end(); i++) {
347  delete (*i).second;
348  }
349  */
350 }
351 
352 
353 void
355  if (refusedProhibits > 0) {
356  WRITE_WARNING("Could not build " + toString<int>(refusedProhibits) + " of " + toString<int>((int)myDict.size()) + " disturbances.");
357  }
358 }
359 
360 
361 
362 /****************************************************************************/
363 
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:108
bool check(const NBEdgeCont &ec)
checks whether the edges are still valid
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:131
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:126
The representation of a single edge during network building.
Definition: NBEdge.h:70
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:415
static bool dictionary(int id, NIVissimConnection *o)
const std::string & getID() const
Returns the id.
Definition: Named.h:65
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
A temporary storage for edges imported from Vissim.
Definition: NIVissimEdge.h:59
NIVissimExtendedEdgePoint myEdge
NBEdge * getPossiblySplittedIncoming(const std::string &edgeid)
get possibly splitted incoming edge
Definition: NBNode.cpp:1255
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
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:125
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:1268
Position crossesEdgeAtPoint(NIVissimAbstractEdge *c) const
NBConnection getConnection(NBNode *node, int aedgeid)
NIVissimExtendedEdgePoint myDisturbance
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:40
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:250
Represents a single node (junction) during network building.
Definition: NBNode.h:74
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:1238
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:1134
static void dict_SetDisturbances()
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
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:85
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:433