SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NBNodeCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
13 // Container for nodes during the netbuilding process
14 /****************************************************************************/
15 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
16 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
17 /****************************************************************************/
18 //
19 // This file is part of SUMO.
20 // SUMO is free software: you can redistribute it and/or modify
21 // it under the terms of the GNU General Public License as published by
22 // the Free Software Foundation, either version 3 of the License, or
23 // (at your option) any later version.
24 //
25 /****************************************************************************/
26 
27 
28 // ===========================================================================
29 // included modules
30 // ===========================================================================
31 #ifdef _MSC_VER
32 #include <windows_config.h>
33 #else
34 #include <config.h>
35 #endif
36 
37 #include <string>
38 #include <map>
39 #include <algorithm>
40 #include <cmath>
42 #include <utils/geom/Boundary.h>
43 #include <utils/geom/GeomHelper.h>
47 #include <utils/common/StdDefs.h>
48 #include <utils/common/ToString.h>
52 #include "NBDistrict.h"
53 #include "NBEdgeCont.h"
55 #include "NBJoinedEdgesMap.h"
56 #include "NBOwnTLDef.h"
57 #include "NBNodeCont.h"
58 
59 #ifdef CHECK_MEMORY_LEAKS
60 #include <foreign/nvwa/debug_new.h>
61 #endif // CHECK_MEMORY_LEAKS
62 
63 
64 // ===========================================================================
65 // method definitions
66 // ===========================================================================
68  : myInternalID(1) {}
69 
70 
72  clear();
73 }
74 
75 
76 // ----------- Insertion/removal/retrieval of nodes
77 bool
78 NBNodeCont::insert(const std::string& id, const Position& position,
79  NBDistrict* district) {
80  NodeCont::iterator i = myNodes.find(id);
81  if (i != myNodes.end()) {
82  return false;
83  }
84  NBNode* node = new NBNode(id, position, district);
85  myNodes[id] = node;
86  return true;
87 }
88 
89 
90 bool
91 NBNodeCont::insert(const std::string& id, const Position& position) {
92  NodeCont::iterator i = myNodes.find(id);
93  if (i != myNodes.end()) {
94  return false;
95  }
96  NBNode* node = new NBNode(id, position);
97  myNodes[id] = node;
98  return true;
99 }
100 
101 
102 Position
103 NBNodeCont::insert(const std::string& id) {
104  std::pair<SUMOReal, SUMOReal> ret(-1.0, -1.0);
105  NodeCont::iterator i = myNodes.find(id);
106  if (i != myNodes.end()) {
107  return (*i).second->getPosition();
108  } else {
109  NBNode* node = new NBNode(id, Position(-1.0, -1.0));
110  myNodes[id] = node;
111  }
112  return Position(-1, -1);
113 }
114 
115 
116 bool
118  std::string id = node->getID();
119  NodeCont::iterator i = myNodes.find(id);
120  if (i != myNodes.end()) {
121  return false;
122  }
123  myNodes[id] = node;
124  return true;
125 }
126 
127 
128 NBNode*
129 NBNodeCont::retrieve(const std::string& id) const {
130  NodeCont::const_iterator i = myNodes.find(id);
131  if (i == myNodes.end()) {
132  return 0;
133  }
134  return (*i).second;
135 }
136 
137 
138 NBNode*
139 NBNodeCont::retrieve(const Position& position, SUMOReal offset) const {
140  for (NodeCont::const_iterator i = myNodes.begin(); i != myNodes.end(); i++) {
141  NBNode* node = (*i).second;
142  if (fabs(node->getPosition().x() - position.x()) < offset
143  &&
144  fabs(node->getPosition().y() - position.y()) < offset) {
145  return node;
146  }
147  }
148  return 0;
149 }
150 
151 
152 bool
154  if (extract(node)) {
155  delete node;
156  return true;
157  } else {
158  return false;
159  }
160 }
161 
162 
163 bool
164 NBNodeCont::extract(NBNode* node, bool remember) {
165  NodeCont::iterator i = myNodes.find(node->getID());
166  if (i == myNodes.end()) {
167  return false;
168  }
169  myNodes.erase(i);
170  node->removeTrafficLights();
171  if (remember) {
172  myExtractedNodes.insert(node);
173  }
174  return true;
175 }
176 
177 
178 // ----------- Adapting the input
179 void
181  unsigned int no = 0;
182  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
183  no += (*i).second->removeSelfLoops(dc, ec, tc);
184  }
185  if (no != 0) {
186  WRITE_WARNING(toString(no) + " self-looping edge(s) removed.");
187  }
188 }
189 
190 
191 void
193  // magic values
194  SUMOReal distanceThreshold = 7; // don't merge edges further apart
195  SUMOReal lengthThreshold = 0.05; // don't merge edges with higher relative length-difference
196 
197  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
198  // count the edges to other nodes outgoing from the current node
199  std::map<NBNode*, EdgeVector> connectionCount;
200  const EdgeVector& outgoing = (*i).second->getOutgoingEdges();
201  for (EdgeVector::const_iterator j = outgoing.begin(); j != outgoing.end(); j++) {
202  NBEdge* e = (*j);
203  NBNode* connected = e->getToNode();
204  if (connectionCount.find(connected) == connectionCount.end()) {
205  connectionCount[connected] = EdgeVector();
206  }
207  connectionCount[connected].push_back(e);
208  }
209  // check whether more than a single edge connect another node and join them
210  std::map<NBNode*, EdgeVector>::iterator k;
211  for (k = connectionCount.begin(); k != connectionCount.end(); k++) {
212  // possibly we do not have anything to join...
213  if ((*k).second.size() < 2) {
214  continue;
215  }
216  // for the edges that seem to be a single street,
217  // check whether the geometry is similar
218  const EdgeVector& ev = (*k).second;
219  const NBEdge* const first = ev.front();
220  EdgeVector::const_iterator jci; // join candidate iterator
221  for (jci = ev.begin() + 1; jci != ev.end(); ++jci) {
222  const SUMOReal relativeLengthDifference = fabs(first->getLoadedLength() - (*jci)->getLoadedLength()) / first->getLoadedLength();
223  if ((!first->isNearEnough2BeJoined2(*jci, distanceThreshold)) ||
224  (relativeLengthDifference > lengthThreshold) ||
225  (first->getSpeed() != (*jci)->getSpeed())
226  // @todo check vclass
227  ) {
228  break;
229  }
230  }
231  // @bug If there are 3 edges of which 2 can be joined, no joining will
232  // take place with the current implementation
233  if (jci == ev.end()) {
234  ec.joinSameNodeConnectingEdges(dc, tlc, ev);
235  }
236  }
237  }
238 }
239 
240 
241 void
243  UNUSED_PARAMETER(tc);
244  // Warn of isolated edges, i.e. a single edge with no connection to another edge
245  int edgeCounter = 0;
246  const std::vector<std::string>& edgeNames = ec.getAllNames();
247  for (std::vector<std::string>::const_iterator it = edgeNames.begin(); it != edgeNames.end(); ++it) {
248  // Test whether this node starts at a dead end, i.e. it has only one adjacent node
249  // to which an edge exists and from which an edge may come.
250  NBEdge* e = ec.retrieve(*it);
251  if (e == 0) {
252  continue;
253  }
254  NBNode* from = e->getFromNode();
255  const EdgeVector& outgoingEdges = from->getOutgoingEdges();
256  if (outgoingEdges.size() != 1) {
257  // At this node, several edges or no edge start; so, this node is no dead end.
258  continue;
259  }
260  const EdgeVector& incomingEdges = from->getIncomingEdges();
261  if (incomingEdges.size() > 1) {
262  // At this node, several edges end; so, this node is no dead end.
263  continue;
264  } else if (incomingEdges.size() == 1) {
265  NBNode* fromNodeOfIncomingEdge = incomingEdges[0]->getFromNode();
266  NBNode* toNodeOfOutgoingEdge = outgoingEdges[0]->getToNode();
267  if (fromNodeOfIncomingEdge != toNodeOfOutgoingEdge) {
268  // At this node, an edge ends which is not the inverse direction of
269  // the starting node.
270  continue;
271  }
272  }
273  // Now we know that the edge e starts a dead end.
274  // Next we test if the dead end is isolated, i.e. does not lead to a junction
275  bool hasJunction = false;
276  EdgeVector road;
277  NBEdge* eOld = 0;
278  NBNode* to;
279  std::set<NBNode*> adjacentNodes;
280  do {
281  road.push_back(e);
282  eOld = e;
283  from = e->getFromNode();
284  to = e->getToNode();
285  const EdgeVector& outgoingEdgesOfToNode = to->getOutgoingEdges();
286  const EdgeVector& incomingEdgesOfToNode = to->getIncomingEdges();
287  adjacentNodes.clear();
288  for (EdgeVector::const_iterator itOfOutgoings = outgoingEdgesOfToNode.begin(); itOfOutgoings != outgoingEdgesOfToNode.end(); ++itOfOutgoings) {
289  if ((*itOfOutgoings)->getToNode() != from // The back path
290  && (*itOfOutgoings)->getToNode() != to // A loop / dummy edge
291  ) {
292  e = *itOfOutgoings; // Probably the next edge
293  }
294  adjacentNodes.insert((*itOfOutgoings)->getToNode());
295  }
296  for (EdgeVector::const_iterator itOfIncomings = incomingEdgesOfToNode.begin(); itOfIncomings != incomingEdgesOfToNode.end(); ++itOfIncomings) {
297  adjacentNodes.insert((*itOfIncomings)->getFromNode());
298  }
299  adjacentNodes.erase(to); // Omit loops
300  if (adjacentNodes.size() > 2) {
301  hasJunction = true;
302  }
303  } while (!hasJunction && eOld != e);
304  if (!hasJunction) {
305  edgeCounter += int(road.size());
306  std::string warningString = "Removed a road without junctions: ";
307  for (EdgeVector::iterator roadIt = road.begin(); roadIt != road.end(); ++roadIt) {
308  if (roadIt == road.begin()) {
309  warningString += (*roadIt)->getID();
310  } else {
311  warningString += ", " + (*roadIt)->getID();
312  }
313 
314  NBNode* fromNode = (*roadIt)->getFromNode();
315  NBNode* toNode = (*roadIt)->getToNode();
316  ec.erase(dc, *roadIt);
317  if (fromNode->getIncomingEdges().size() == 0 && fromNode->getOutgoingEdges().size() == 0) {
318  // Node is empty; can be removed
319  erase(fromNode);
320  }
321  if (toNode->getIncomingEdges().size() == 0 && toNode->getOutgoingEdges().size() == 0) {
322  // Node is empty; can be removed
323  erase(toNode);
324  }
325  }
326  WRITE_WARNING(warningString);
327  }
328  }
329  if (edgeCounter > 0 && !OptionsCont::getOptions().getBool("remove-edges.isolated")) {
330  WRITE_WARNING("Detected isolated roads. Use the option --remove-edges.isolated to get a list of all affected edges.");
331  }
332 }
333 
334 
335 unsigned int
338  bool removeGeometryNodes) {
339  unsigned int no = 0;
340  std::vector<NBNode*> toRemove;
341  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
342  NBNode* current = (*i).second;
343  bool remove = false;
344  std::vector<std::pair<NBEdge*, NBEdge*> > toJoin;
345  // check for completely empty nodes
346  if (current->getOutgoingEdges().size() == 0 && current->getIncomingEdges().size() == 0) {
347  // remove if empty
348  remove = true;
349  }
350  // check for nodes which are only geometry nodes
351  if (removeGeometryNodes) {
352  if ((current->getOutgoingEdges().size() == 1 && current->getIncomingEdges().size() == 1)
353  ||
354  (current->getOutgoingEdges().size() == 2 && current->getIncomingEdges().size() == 2)) {
355  // ok, one in, one out or two in, two out
356  // -> ask the node whether to join
357  remove = current->checkIsRemovable();
358  if (remove) {
359  toJoin = current->getEdgesToJoin();
360  }
361  }
362  }
363  // remove the node and join the geometries when wished
364  if (!remove) {
365  continue;
366  }
367  for (std::vector<std::pair<NBEdge*, NBEdge*> >::iterator j = toJoin.begin(); j != toJoin.end(); j++) {
368  NBEdge* begin = (*j).first;
369  NBEdge* continuation = (*j).second;
370  begin->append(continuation);
371  continuation->getToNode()->replaceIncoming(continuation, begin, 0);
372  tlc.replaceRemoved(continuation, -1, begin, -1);
373  je.appended(begin->getID(), continuation->getID());
374  ec.erase(dc, continuation);
375  }
376  toRemove.push_back(current);
377  no++;
378  }
379  // erase all
380  for (std::vector<NBNode*>::iterator j = toRemove.begin(); j != toRemove.end(); ++j) {
381  erase(*j);
382  }
383  return no;
384 }
385 
386 
387 // ----------- (Helper) methods for joining nodes
388 void
390  std::set<NBNode*> visited;
391  for (NodeCont::const_iterator i = myNodes.begin(); i != myNodes.end(); i++) {
392  std::vector<NBNode*> toProc;
393  if (visited.find((*i).second) != visited.end()) {
394  continue;
395  }
396  toProc.push_back((*i).second);
397  std::set<NBNode*> c;
398  while (!toProc.empty()) {
399  NBNode* n = toProc.back();
400  toProc.pop_back();
401  if (visited.find(n) != visited.end()) {
402  continue;
403  }
404  c.insert(n);
405  visited.insert(n);
406  const EdgeVector& edges = n->getEdges();
407  for (EdgeVector::const_iterator j = edges.begin(); j != edges.end(); ++j) {
408  NBEdge* e = *j;
409  NBNode* s = 0;
410  if (n->hasIncoming(e)) {
411  s = e->getFromNode();
412  } else {
413  s = e->getToNode();
414  }
415  if (visited.find(s) != visited.end()) {
416  continue;
417  }
418  if (e->getLoadedLength() < maxDist) {
419  toProc.push_back(s);
420  }
421  }
422  }
423  if (c.size() < 2) {
424  continue;
425  }
426  into.push_back(c);
427  }
428 }
429 
430 
431 void
432 NBNodeCont::addJoinExclusion(const std::vector<std::string>& ids, bool check) {
433  for (std::vector<std::string>::const_iterator it = ids.begin(); it != ids.end(); it++) {
434  // error handling has to take place here since joinExclusions could be
435  // loaded from multiple files / command line
436  if (myJoined.count(*it) > 0) {
437  WRITE_WARNING("Ignoring join exclusion for node '" + *it + "' since it already occured in a list of nodes to be joined");
438  } else if (check && retrieve(*it) == 0) {
439  WRITE_WARNING("Ignoring join exclusion for unknown node '" + *it + "'");
440  } else {
441  myJoinExclusions.insert(*it);
442  }
443  }
444 }
445 
446 
447 void
448 NBNodeCont::addCluster2Join(std::set<std::string> cluster) {
449  // error handling has to take place here since joins could be loaded from multiple files
450  for (std::set<std::string>::const_iterator it = cluster.begin(); it != cluster.end(); it++) {
451  if (myJoinExclusions.count(*it) > 0) {
452  WRITE_WARNING("Ignoring join-cluster because node '" + *it + "' was already excluded from joining");
453  return;
454  } else if (myJoined.count(*it) > 0) {
455  WRITE_WARNING("Ignoring join-cluster because node '" + *it + "' already occured in another join-cluster");
456  return;
457  } else {
458  myJoined.insert(*it);
459  }
460  }
461  myClusters2Join.push_back(cluster);
462 }
463 
464 
465 unsigned int
467  NodeClusters clusters;
468  for (std::vector<std::set<std::string> >::iterator it = myClusters2Join.begin(); it != myClusters2Join.end(); it++) {
469  // verify loaded cluster
470  std::set<NBNode*> cluster;
471  for (std::set<std::string>::iterator it_id = it->begin(); it_id != it->end(); it_id++) {
472  NBNode* node = retrieve(*it_id);
473  if (node == 0) {
474  WRITE_WARNING("Ignoring unknown node '" + *it_id + "' while joining");
475  } else {
476  cluster.insert(node);
477  }
478  }
479  if (cluster.size() > 1) {
480  clusters.push_back(cluster);
481  }
482  }
483  joinNodeClusters(clusters, dc, ec, tlc);
484  myClusters2Join.clear(); // make save for recompute
485  return (int)clusters.size();
486 }
487 
488 
489 unsigned int
491  NodeClusters cands;
492  NodeClusters clusters;
493  generateNodeClusters(maxdist, cands);
494  for (NodeClusters::iterator i = cands.begin(); i != cands.end(); ++i) {
495  std::set<NBNode*> cluster = (*i);
496  // remove join exclusions
497  for (std::set<NBNode*>::iterator j = cluster.begin(); j != cluster.end();) {
498  std::set<NBNode*>::iterator check = j;
499  ++j;
500  if (myJoinExclusions.count((*check)->getID()) > 0) {
501  cluster.erase(check);
502  }
503  }
504  // iteratively remove the fringe
505  bool pruneFringe = true;
506  while (pruneFringe) {
507  pruneFringe = false;
508  for (std::set<NBNode*>::iterator j = cluster.begin(); j != cluster.end();) {
509  std::set<NBNode*>::iterator check = j;
510  NBNode* n = *check;
511  ++j;
512  // remove nodes with degree <= 2 at fringe of the cluster (at least one edge leads to a non-cluster node)
513  if (
514  (n->getIncomingEdges().size() <= 1 && n->getOutgoingEdges().size() <= 1) &&
515  ((n->getIncomingEdges().size() == 0 ||
516  (n->getIncomingEdges().size() == 1 && cluster.count(n->getIncomingEdges()[0]->getFromNode()) == 0)) ||
517  (n->getOutgoingEdges().size() == 0 ||
518  (n->getOutgoingEdges().size() == 1 && cluster.count(n->getOutgoingEdges()[0]->getToNode()) == 0)))
519  ) {
520  cluster.erase(check);
521  pruneFringe = true; // other nodes could belong to the fringe now
522  }
523  }
524  }
525  if (cluster.size() > 1) {
526  // check for clusters which are to complex and probably won't work very well
527  // we count the incoming edges of the final junction
528  std::set<NBEdge*> finalIncoming;
529  std::vector<std::string> nodeIDs;
530  for (std::set<NBNode*>::const_iterator j = cluster.begin(); j != cluster.end(); ++j) {
531  nodeIDs.push_back((*j)->getID());
532  const EdgeVector& edges = (*j)->getIncomingEdges();
533  for (EdgeVector::const_iterator it_edge = edges.begin(); it_edge != edges.end(); ++it_edge) {
534  NBEdge* edge = *it_edge;
535  if (cluster.count(edge->getFromNode()) == 0) {
536  // incoming edge, does not originate in the cluster
537  finalIncoming.insert(edge);
538  }
539  }
540 
541  }
542  if (finalIncoming.size() > 4) {
543  WRITE_WARNING("Not joining junctions " + joinToString(nodeIDs, ',') + " because the cluster is too complex");
544  } else {
545  clusters.push_back(cluster);
546  }
547  }
548  }
549  joinNodeClusters(clusters, dc, ec, tlc);
550  return (int)clusters.size();
551 }
552 
553 
554 void
557  for (NodeClusters::iterator i = clusters.begin(); i != clusters.end(); ++i) {
558  std::set<NBNode*> cluster = *i;
559  assert(cluster.size() > 1);
560  Position pos;
561  bool setTL;
562  std::string id;
563  TrafficLightType type;
564  analyzeCluster(cluster, id, pos, setTL, type);
565  if (!insert(id, pos)) {
566  // should not fail
567  WRITE_WARNING("Could not join junctions " + id);
568  continue;
569  }
570  NBNode* newNode = retrieve(id);
571  if (setTL) {
572  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(id, newNode, 0, type);
573  if (!tlc.insert(tlDef)) {
574  // actually, nothing should fail here
575  delete tlDef;
576  throw ProcessError("Could not allocate tls '" + id + "'.");
577  }
578  }
579  // collect edges
580  std::set<NBEdge*> allEdges;
581  for (std::set<NBNode*>::const_iterator j = cluster.begin(); j != cluster.end(); ++j) {
582  const EdgeVector& edges = (*j)->getEdges();
583  allEdges.insert(edges.begin(), edges.end());
584  }
585 
586  // remap and remove edges which are completely within the new intersection
587  for (std::set<NBEdge*>::iterator j = allEdges.begin(); j != allEdges.end();) {
588  NBEdge* e = (*j);
589  NBNode* from = e->getFromNode();
590  NBNode* to = e->getToNode();
591  if (cluster.count(from) > 0 && cluster.count(to) > 0) {
592  for (std::set<NBEdge*>::iterator l = allEdges.begin(); l != allEdges.end(); ++l) {
593  if (e != *l) {
594  (*l)->replaceInConnections(e, e->getConnections());
595  }
596  }
597  ec.erase(dc, e);
598  allEdges.erase(j++); // erase does not invalidate the other iterators
599  } else {
600  ++j;
601  }
602  }
603 
604  // remap edges which are incoming / outgoing
605  for (std::set<NBEdge*>::iterator j = allEdges.begin(); j != allEdges.end(); ++j) {
606  NBEdge* e = (*j);
607  std::vector<NBEdge::Connection> conns = e->getConnections();
608  const bool outgoing = cluster.count(e->getFromNode()) > 0;
609  NBNode* from = outgoing ? newNode : e->getFromNode();
610  NBNode* to = outgoing ? e->getToNode() : newNode;
611  e->reinitNodes(from, to);
612  // re-add connections which previously existed and may still valid.
613  // connections to removed edges will be ignored
614  for (std::vector<NBEdge::Connection>::iterator k = conns.begin(); k != conns.end(); ++k) {
615  e->addLane2LaneConnection((*k).fromLane, (*k).toEdge, (*k).toLane, NBEdge::L2L_USER, false, (*k).mayDefinitelyPass);
616  }
617  }
618  // remove original nodes
619  registerJoinedCluster(cluster);
620  for (std::set<NBNode*>::const_iterator j = cluster.begin(); j != cluster.end(); ++j) {
621  erase(*j);
622  }
623  }
624 }
625 
626 
627 void
628 NBNodeCont::registerJoinedCluster(const std::set<NBNode*>& cluster) {
629  std::set<std::string> ids;
630  for (std::set<NBNode*>::const_iterator j = cluster.begin(); j != cluster.end(); j++) {
631  ids.insert((*j)->getID());
632  }
633  myJoinedClusters.push_back(ids);
634 }
635 
636 
637 void
638 NBNodeCont::analyzeCluster(std::set<NBNode*> cluster, std::string& id, Position& pos,
639  bool& hasTLS, TrafficLightType& type) {
640  id = "cluster";
641  hasTLS = false;
642  std::vector<std::string> member_ids;
643  bool ambiguousType = false;
644  for (std::set<NBNode*>::const_iterator j = cluster.begin(); j != cluster.end(); j++) {
645  member_ids.push_back((*j)->getID());
646  pos.add((*j)->getPosition());
647  // add a traffic light if any of the cluster members was controlled
648  if ((*j)->isTLControlled()) {
649  if (!hasTLS) {
650  // init type
651  type = (*(*j)->getControllingTLS().begin())->getType();
652  } else if (type != (*(*j)->getControllingTLS().begin())->getType()) {
653  ambiguousType = true;
654  }
655  hasTLS = true;
656  }
657  }
658  pos.mul(1.0 / cluster.size());
659  // need to sort the member names to make the output deterministic
660  sort(member_ids.begin(), member_ids.end());
661  for (std::vector<std::string>::iterator j = member_ids.begin(); j != member_ids.end(); j++) {
662  id = id + "_" + (*j);
663  }
664  if (ambiguousType) {
665  type = SUMOXMLDefinitions::TrafficLightTypes.get(OptionsCont::getOptions().getString("tls.default-type"));
666  WRITE_WARNING("Ambiguous traffic light type for node cluster '" + id + "' set to '" + toString(type) + "'");
667  }
668 }
669 
670 
671 // ----------- (Helper) methods for guessing/computing traffic lights
672 bool
673 NBNodeCont::shouldBeTLSControlled(const std::set<NBNode*>& c) const {
674  unsigned int noIncoming = 0;
675  unsigned int noOutgoing = 0;
676  bool tooFast = false;
677  SUMOReal f = 0;
678  std::set<NBEdge*> seen;
679  for (std::set<NBNode*>::const_iterator j = c.begin(); j != c.end(); ++j) {
680  const EdgeVector& edges = (*j)->getEdges();
681  for (EdgeVector::const_iterator k = edges.begin(); k != edges.end(); ++k) {
682  if (c.find((*k)->getFromNode()) != c.end() && c.find((*k)->getToNode()) != c.end()) {
683  continue;
684  }
685  if ((*j)->hasIncoming(*k)) {
686  ++noIncoming;
687  f += (SUMOReal)(*k)->getNumLanes() * (*k)->getLaneSpeed(0);
688  } else {
689  ++noOutgoing;
690  }
691  if ((*k)->getLaneSpeed(0) * 3.6 > 79) {
692  tooFast = true;
693  }
694  }
695  }
696  return !tooFast && f >= 150. / 3.6 && c.size() != 0;
697 }
698 
699 
700 void
702  // build list of definitely not tls-controlled junctions
703  std::vector<NBNode*> ncontrolled;
704  if (oc.isSet("tls.unset")) {
705  std::vector<std::string> notTLControlledNodes = oc.getStringVector("tls.unset");
706  for (std::vector<std::string>::const_iterator i = notTLControlledNodes.begin(); i != notTLControlledNodes.end(); ++i) {
707  NBNode* n = NBNodeCont::retrieve(*i);
708  if (n == 0) {
709  throw ProcessError(" The node '" + *i + "' to set as not-controlled is not known.");
710  }
711  std::set<NBTrafficLightDefinition*> tls = n->getControllingTLS();
712  for (std::set<NBTrafficLightDefinition*>::const_iterator j = tls.begin(); j != tls.end(); ++j) {
713  (*j)->removeNode(n);
714  }
715  n->removeTrafficLights();
716  ncontrolled.push_back(n);
717  }
718  }
719 
721  // loop#1 checking whether the node shall be tls controlled,
722  // because it is assigned to a district
723  if (oc.exists("tls.taz-nodes") && oc.getBool("tls.taz-nodes")) {
724  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
725  NBNode* cur = (*i).second;
726  if (cur->isNearDistrict() && find(ncontrolled.begin(), ncontrolled.end(), cur) == ncontrolled.end()) {
727  setAsTLControlled(cur, tlc, type);
728  }
729  }
730  }
731 
732  // maybe no tls shall be guessed
733  if (!oc.getBool("tls.guess")) {
734  return;
735  }
736 
737  // guess joined tls first, if wished
738  if (oc.getBool("tls.join")) {
739  // get node clusters
740  std::vector<std::set<NBNode*> > cands;
741  generateNodeClusters(oc.getFloat("tls.join-dist"), cands);
742  // check these candidates (clusters) whether they should be controlled by a tls
743  for (std::vector<std::set<NBNode*> >::iterator i = cands.begin(); i != cands.end();) {
744  std::set<NBNode*>& c = (*i);
745  // regard only junctions which are not yet controlled and are not
746  // forbidden to be controlled
747  for (std::set<NBNode*>::iterator j = c.begin(); j != c.end();) {
748  if ((*j)->isTLControlled() || find(ncontrolled.begin(), ncontrolled.end(), *j) != ncontrolled.end()) {
749  c.erase(j++);
750  } else {
751  ++j;
752  }
753  }
754  // check whether the cluster should be controlled
755  if (!shouldBeTLSControlled(c)) {
756  i = cands.erase(i);
757  } else {
758  ++i;
759  }
760  }
761  // cands now only contain sets of junctions that shall be joined into being tls-controlled
762  unsigned int index = 0;
763  for (std::vector<std::set<NBNode*> >::iterator i = cands.begin(); i != cands.end(); ++i) {
764  std::vector<NBNode*> nodes;
765  for (std::set<NBNode*>::iterator j = (*i).begin(); j != (*i).end(); j++) {
766  nodes.push_back(*j);
767  }
768  std::string id = "joinedG_" + toString(index++);
769  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(id, nodes, 0, type);
770  if (!tlc.insert(tlDef)) {
771  // actually, nothing should fail here
772  WRITE_WARNING("Could not build guessed, joined tls");
773  delete tlDef;
774  return;
775  }
776  }
777  }
778 
779  // guess tls
780  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
781  NBNode* cur = (*i).second;
782  // do nothing if already is tl-controlled
783  if (cur->isTLControlled()) {
784  continue;
785  }
786  // do nothing if in the list of explicit non-controlled junctions
787  if (find(ncontrolled.begin(), ncontrolled.end(), cur) != ncontrolled.end()) {
788  continue;
789  }
790  std::set<NBNode*> c;
791  c.insert(cur);
792  if (!shouldBeTLSControlled(c) || cur->getIncomingEdges().size() < 3) {
793  continue;
794  }
795  setAsTLControlled((*i).second, tlc, type);
796  }
797 }
798 
799 
800 void
802  std::vector<std::set<NBNode*> > cands;
803  generateNodeClusters(maxdist, cands);
804  unsigned int index = 0;
805  for (std::vector<std::set<NBNode*> >::iterator i = cands.begin(); i != cands.end(); ++i) {
806  std::set<NBNode*>& c = (*i);
807  for (std::set<NBNode*>::iterator j = c.begin(); j != c.end();) {
808  if (!(*j)->isTLControlled()) {
809  c.erase(j++);
810  } else {
811  ++j;
812  }
813  }
814  if (c.size() < 2) {
815  continue;
816  }
817  // figure out type of the joined TLS
818  Position dummyPos;
819  bool dummySetTL;
820  std::string dummyId;
821  TrafficLightType type;
822  analyzeCluster(c, dummyId, dummyPos, dummySetTL, type);
823  for (std::set<NBNode*>::iterator j = c.begin(); j != c.end(); ++j) {
824  std::set<NBTrafficLightDefinition*> tls = (*j)->getControllingTLS();
825  (*j)->removeTrafficLights();
826  for (std::set<NBTrafficLightDefinition*>::iterator k = tls.begin(); k != tls.end(); ++k) {
827  tlc.removeFully((*j)->getID());
828  }
829  }
830  std::string id = "joinedS_" + toString(index++);
831  std::vector<NBNode*> nodes;
832  for (std::set<NBNode*>::iterator j = c.begin(); j != c.end(); j++) {
833  nodes.push_back(*j);
834  }
835  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(id, nodes, 0, type);
836  if (!tlc.insert(tlDef)) {
837  // actually, nothing should fail here
838  WRITE_WARNING("Could not build a joined tls.");
839  delete tlDef;
840  return;
841  }
842  }
843 }
844 
845 
846 void
848  TrafficLightType type, std::string id) {
849  if (id == "") {
850  id = node->getID();
851  }
852  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(id, node, 0, type);
853  if (!tlc.insert(tlDef)) {
854  // actually, nothing should fail here
855  WRITE_WARNING("Building a tl-logic for node '" + id + "' twice is not possible.");
856  delete tlDef;
857  return;
858  }
859 }
860 
861 
862 // -----------
863 void
865  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
866  (*i).second->computeLanes2Lanes();
867  }
868 }
869 
870 
871 // computes the "wheel" of incoming and outgoing edges for every node
872 void
874  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
875  (*i).second->computeLogic(ec, oc);
876  }
877 }
878 
879 
880 void
882  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
883  delete((*i).second);
884  }
885  myNodes.clear();
886  for (std::set<NBNode*>::iterator i = myExtractedNodes.begin(); i != myExtractedNodes.end(); i++) {
887  delete(*i);
888  }
889  myExtractedNodes.clear();
890 }
891 
892 
893 std::string
895  // !!! not guaranteed to be free
896  std::string ret = "SUMOGenerated" + toString<int>(size());
897  assert(retrieve(ret) == 0);
898  return ret;
899 }
900 
901 
902 void
904  for (NodeCont::iterator i = myNodes.begin(); i != myNodes.end(); i++) {
905  (*i).second->computeNodeShape(leftHand);
906  }
907 }
908 
909 
910 void
912  int numUnregulatedJunctions = 0;
913  int numDeadEndJunctions = 0;
914  int numPriorityJunctions = 0;
915  int numRightBeforeLeftJunctions = 0;
916  for (NodeCont::const_iterator i = myNodes.begin(); i != myNodes.end(); i++) {
917  switch ((*i).second->getType()) {
918  case NODETYPE_NOJUNCTION:
919  ++numUnregulatedJunctions;
920  break;
921  case NODETYPE_DEAD_END:
922  ++numDeadEndJunctions;
923  break;
926  ++numPriorityJunctions;
927  break;
929  ++numRightBeforeLeftJunctions;
930  break;
931  case NODETYPE_DISTRICT:
932  ++numRightBeforeLeftJunctions;
933  break;
934  case NODETYPE_UNKNOWN:
935  break;
936  default:
937  break;
938  }
939  }
940  WRITE_MESSAGE(" Node type statistics:");
941  WRITE_MESSAGE(" Unregulated junctions : " + toString(numUnregulatedJunctions));
942  if (numDeadEndJunctions > 0) {
943  WRITE_MESSAGE(" Dead-end junctions : " + toString(numDeadEndJunctions));
944  }
945  WRITE_MESSAGE(" Priority junctions : " + toString(numPriorityJunctions));
946  WRITE_MESSAGE(" Right-before-left junctions : " + toString(numRightBeforeLeftJunctions));
947 }
948 
949 
950 std::vector<std::string>
952  std::vector<std::string> ret;
953  for (NodeCont::const_iterator i = myNodes.begin(); i != myNodes.end(); ++i) {
954  ret.push_back((*i).first);
955  }
956  return ret;
957 }
958 
959 
960 void
961 NBNodeCont::rename(NBNode* node, const std::string& newID) {
962  if (myNodes.count(newID) != 0) {
963  throw ProcessError("Attempt to rename node using existing id '" + newID + "'");
964  }
965  myNodes.erase(node->getID());
966  node->setID(newID);
967  myNodes[newID] = node;
968 }
969 
970 
971 void
973  for (NodeCont::const_iterator i = myNodes.begin(); i != myNodes.end(); ++i) {
974  NBNode* node = i->second;
975  if (!geometryLike || node->geometryLike()) {
976  // make a copy of tldefs
977  const std::set<NBTrafficLightDefinition*> tldefs = node->getControllingTLS();
978  for (std::set<NBTrafficLightDefinition*>::const_iterator it = tldefs.begin(); it != tldefs.end(); ++it) {
979  NBTrafficLightDefinition* tlDef = *it;
980  node->removeTrafficLight(tlDef);
981  tlc.extract(tlDef);
982  }
983  node->reinit(node->getPosition(), NODETYPE_UNKNOWN);
984  }
985  }
986 }
987 
988 /****************************************************************************/
989