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