SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NBNode.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // The representation of a single node
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <map>
36 #include <cassert>
37 #include <algorithm>
38 #include <vector>
39 #include <deque>
40 #include <set>
41 #include <cmath>
42 #include <iterator>
46 #include <utils/geom/Line.h>
47 #include <utils/geom/GeomHelper.h>
48 #include <utils/geom/bezier.h>
50 #include <utils/common/StdDefs.h>
51 #include <utils/common/ToString.h>
54 #include <iomanip>
55 #include "NBNode.h"
56 #include "NBNodeCont.h"
57 #include "NBNodeShapeComputer.h"
58 #include "NBEdgeCont.h"
59 #include "NBTypeCont.h"
60 #include "NBHelpers.h"
61 #include "NBDistrict.h"
62 #include "NBContHelper.h"
63 #include "NBRequest.h"
64 #include "NBOwnTLDef.h"
67 
68 #ifdef CHECK_MEMORY_LEAKS
69 #include <foreign/nvwa/debug_new.h>
70 #endif // CHECK_MEMORY_LEAKS
71 
72 
73 // ===========================================================================
74 // static members
75 // ===========================================================================
76 
77 // ===========================================================================
78 // method definitions
79 // ===========================================================================
80 /* -------------------------------------------------------------------------
81  * NBNode::ApproachingDivider-methods
82  * ----------------------------------------------------------------------- */
84  EdgeVector* approaching, NBEdge* currentOutgoing) :
85  myApproaching(approaching), myCurrentOutgoing(currentOutgoing) {
86  // check whether origin lanes have been given
87  assert(myApproaching != 0);
88 }
89 
90 
92 
93 
94 void
95 NBNode::ApproachingDivider::execute(const unsigned int src, const unsigned int dest) {
96  assert(myApproaching->size() > src);
97  // get the origin edge
98  NBEdge* incomingEdge = (*myApproaching)[src];
99  if (incomingEdge->getStep() == NBEdge::LANES2LANES_DONE || incomingEdge->getStep() == NBEdge::LANES2LANES_USER) {
100  return;
101  }
102  std::vector<int> approachingLanes =
103  incomingEdge->getConnectionLanes(myCurrentOutgoing);
104  assert(approachingLanes.size() != 0);
105  std::deque<int>* approachedLanes = spread(approachingLanes, dest);
106  assert(approachedLanes->size() <= myCurrentOutgoing->getNumLanes());
107  // set lanes
108  for (unsigned int i = 0; i < approachedLanes->size(); i++) {
109  unsigned int approached = (*approachedLanes)[i];
110  assert(approachedLanes->size() > i);
111  assert(approachingLanes.size() > i);
112  incomingEdge->setConnection((unsigned int) approachingLanes[i], myCurrentOutgoing,
113  approached, NBEdge::L2L_COMPUTED);
114  }
115  delete approachedLanes;
116 }
117 
118 
119 std::deque<int>*
120 NBNode::ApproachingDivider::spread(const std::vector<int>& approachingLanes,
121  int dest) const {
122  std::deque<int>* ret = new std::deque<int>();
123  unsigned int noLanes = (unsigned int) approachingLanes.size();
124  // when only one lane is approached, we check, whether the SUMOReal-value
125  // is assigned more to the left or right lane
126  if (noLanes == 1) {
127  ret->push_back(dest);
128  return ret;
129  }
130 
131  unsigned int noOutgoingLanes = myCurrentOutgoing->getNumLanes();
132  //
133  ret->push_back(dest);
134  unsigned int noSet = 1;
135  int roffset = 1;
136  int loffset = 1;
137  while (noSet < noLanes) {
138  // It may be possible, that there are not enough lanes the source
139  // lanes may be divided on
140  // In this case, they remain unset
141  // !!! this is only a hack. It is possible, that this yields in
142  // uncommon divisions
143  if (noOutgoingLanes == noSet) {
144  return ret;
145  }
146 
147  // as due to the conversion of SUMOReal->uint the numbers will be lower
148  // than they should be, we try to append to the left side first
149  //
150  // check whether the left boundary of the approached street has
151  // been overridden; if so, move all lanes to the right
152  if (dest + loffset >= static_cast<int>(noOutgoingLanes)) {
153  loffset -= 1;
154  roffset += 1;
155  for (unsigned int i = 0; i < ret->size(); i++) {
156  (*ret)[i] = (*ret)[i] - 1;
157  }
158  }
159  // append the next lane to the left of all edges
160  // increase the position (destination edge)
161  ret->push_back(dest + loffset);
162  noSet++;
163  loffset += 1;
164 
165  // as above
166  if (noOutgoingLanes == noSet) {
167  return ret;
168  }
169 
170  // now we try to append the next lane to the right side, when needed
171  if (noSet < noLanes) {
172  // check whether the right boundary of the approached street has
173  // been overridden; if so, move all lanes to the right
174  if (dest < roffset) {
175  loffset += 1;
176  roffset -= 1;
177  for (unsigned int i = 0; i < ret->size(); i++) {
178  (*ret)[i] = (*ret)[i] + 1;
179  }
180  }
181  ret->push_front(dest - roffset);
182  noSet++;
183  roffset += 1;
184  }
185  }
186  return ret;
187 }
188 
189 
190 
191 
192 /* -------------------------------------------------------------------------
193  * NBNode-methods
194  * ----------------------------------------------------------------------- */
195 NBNode::NBNode(const std::string& id, const Position& position) :
196  Named(StringUtils::convertUmlaute(id)),
197  myPosition(position),
199 { }
200 
201 
202 NBNode::NBNode(const std::string& id, const Position& position,
203  SumoXMLNodeType type) :
204  Named(StringUtils::convertUmlaute(id)),
205  myPosition(position),
206  myType(type), myDistrict(0), myRequest(0)
207 { }
208 
209 
210 NBNode::NBNode(const std::string& id, const Position& position, NBDistrict* district) :
211  Named(StringUtils::convertUmlaute(id)),
212  myPosition(position),
213  myType(NODETYPE_DISTRICT), myDistrict(district), myRequest(0)
214 { }
215 
216 
218  delete myRequest;
219 }
220 
221 
222 void
224  bool updateEdgeGeometries) {
225  myPosition = position;
226  // patch type
227  myType = type;
230  }
231  if (updateEdgeGeometries) {
232  for (EdgeVector::iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
233  PositionVector geom = (*i)->getGeometry();
234  geom[-1] = myPosition;
235  (*i)->setGeometry(geom);
236  }
237  for (EdgeVector::iterator i = myOutgoingEdges.begin(); i != myOutgoingEdges.end(); i++) {
238  PositionVector geom = (*i)->getGeometry();
239  geom[0] = myPosition;
240  (*i)->setGeometry(geom);
241  }
242  }
243 }
244 
245 
246 
247 // ----------- Applying offset
248 void
250  myPosition.add(xoff, yoff, 0);
251  myPoly.add(xoff, yoff, 0);
252 }
253 
254 
255 // ----------- Methods for dealing with assigned traffic lights
256 void
258  myTrafficLights.insert(tlDef);
261  }
262 }
263 
264 
265 void
267  tlDef->removeNode(this);
268  myTrafficLights.erase(tlDef);
269 }
270 
271 
272 void
274  std::set<NBTrafficLightDefinition*> trafficLights = myTrafficLights; // make a copy because we will modify the original
275  for (std::set<NBTrafficLightDefinition*>::const_iterator i = trafficLights.begin(); i != trafficLights.end(); ++i) {
276  removeTrafficLight(*i);
277  }
278 }
279 
280 
281 bool
283  if (!isTLControlled()) {
284  return false;
285  }
286  for (std::set<NBTrafficLightDefinition*>::const_iterator i = myTrafficLights.begin(); i != myTrafficLights.end(); ++i) {
287  if ((*i)->getID().find("joined") == 0) {
288  return true;
289  }
290  }
291  return false;
292 }
293 
294 
295 void
297  if (isTLControlled()) {
298  std::set<NBTrafficLightDefinition*> newDefs;
299  for (std::set<NBTrafficLightDefinition*>::iterator it = myTrafficLights.begin(); it != myTrafficLights.end(); ++it) {
300  NBTrafficLightDefinition* orig = *it;
301  if (dynamic_cast<NBOwnTLDef*>(orig) != 0) {
302  // this definition will be guessed anyway. no need to invalidate
303  newDefs.insert(orig);
304  } else {
305  const std::string new_id = orig->getID() + "_reguessed";
306  NBTrafficLightDefinition* newDef = new NBOwnTLDef(new_id, orig->getOffset(), orig->getType());
307  if (!tlCont.insert(newDef)) {
308  // the original definition was shared by other nodes and was already invalidated
309  delete newDef;
310  newDef = tlCont.getDefinition(new_id, orig->getProgramID());
311  assert(newDef != 0);
312  }
313  newDefs.insert(newDef);
314  }
315  }
317  for (std::set<NBTrafficLightDefinition*>::iterator it = newDefs.begin(); it != newDefs.end(); ++it) {
318  (*it)->addNode(this);
319  }
320  }
321 }
322 
323 
324 // ----------- Prunning the input
325 unsigned int
327  unsigned int ret = 0;
328  unsigned int pos = 0;
329  EdgeVector::const_iterator j = myIncomingEdges.begin();
330  while (j != myIncomingEdges.end()) {
331  // skip edges which are only incoming and not outgoing
332  if (find(myOutgoingEdges.begin(), myOutgoingEdges.end(), *j) == myOutgoingEdges.end()) {
333  ++j;
334  ++pos;
335  continue;
336  }
337  // an edge with both its origin and destination being the current
338  // node should be removed
339  NBEdge* dummy = *j;
340  WRITE_WARNING(" Removing self-looping edge '" + dummy->getID() + "'");
341  // get the list of incoming edges connected to the self-loop
342  EdgeVector incomingConnected;
343  for (EdgeVector::const_iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
344  if ((*i)->isConnectedTo(dummy) && *i != dummy) {
345  incomingConnected.push_back(*i);
346  }
347  }
348  // get the list of outgoing edges connected to the self-loop
349  EdgeVector outgoingConnected;
350  for (EdgeVector::const_iterator i = myOutgoingEdges.begin(); i != myOutgoingEdges.end(); i++) {
351  if (dummy->isConnectedTo(*i) && *i != dummy) {
352  outgoingConnected.push_back(*i);
353  }
354  }
355  // let the self-loop remap its connections
356  dummy->remapConnections(incomingConnected);
357  remapRemoved(tc, dummy, incomingConnected, outgoingConnected);
358  // delete the self-loop
359  ec.erase(dc, dummy);
360  j = myIncomingEdges.begin() + pos;
361  ++ret;
362  }
363  return ret;
364 }
365 
366 
367 // -----------
368 void
370  assert(edge != 0);
371  if (find(myIncomingEdges.begin(), myIncomingEdges.end(), edge) == myIncomingEdges.end()) {
372  myIncomingEdges.push_back(edge);
373  myAllEdges.push_back(edge);
374  }
375 }
376 
377 
378 void
380  assert(edge != 0);
381  if (find(myOutgoingEdges.begin(), myOutgoingEdges.end(), edge) == myOutgoingEdges.end()) {
382  myOutgoingEdges.push_back(edge);
383  myAllEdges.push_back(edge);
384  }
385 }
386 
387 
388 bool
390  // one in, one out->continuation
391  if (myIncomingEdges.size() == 1 && myOutgoingEdges.size() == 1) {
392  // both must have the same number of lanes
393  return (*(myIncomingEdges.begin()))->getNumLanes() == (*(myOutgoingEdges.begin()))->getNumLanes();
394  }
395  // two in and two out and both in reverse direction
396  if (myIncomingEdges.size() == 2 && myOutgoingEdges.size() == 2) {
397  for (EdgeVector::const_iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
398  NBEdge* in = *i;
399  EdgeVector::const_iterator opposite = find_if(myOutgoingEdges.begin(), myOutgoingEdges.end(), NBContHelper::opposite_finder(in, this));
400  // must have an opposite edge
401  if (opposite == myOutgoingEdges.end()) {
402  return false;
403  }
404  // both must have the same number of lanes
406  if (in->getNumLanes() != (*opposite)->getNumLanes()) {
407  return false;
408  }
409  }
410  return true;
411  }
412  // nope
413  return false;
414 }
415 
416 
419  NBEdge* toE, int toL, int numPoints) const {
420  if (fromL >= (int) fromE->getNumLanes()) {
421  throw ProcessError("Connection '" + fromE->getID() + "_" + toString(fromL) + "->" + toE->getID() + "_" + toString(toL) + "' starts at a not existing lane.");
422  }
423  if (toL >= (int) toE->getNumLanes()) {
424  throw ProcessError("Connection '" + fromE->getID() + "_" + toString(fromL) + "->" + toE->getID() + "_" + toString(toL) + "' yields in a not existing lane.");
425  }
426  bool noSpline = false;
427  PositionVector ret;
428  PositionVector init;
429  Position beg = fromE->getLaneShape(fromL).back();
430  Position end = toE->getLaneShape(toL).front();
431  Position intersection;
432  unsigned int noInitialPoints = 0;
433  if (beg.distanceTo(end) <= POSITION_EPS) {
434  noSpline = true;
435  } else {
436  if (fromE->getTurnDestination() == toE) {
437  // turnarounds:
438  // - end of incoming lane
439  // - position between incoming/outgoing end/begin shifted by the distance orthogonally
440  // - begin of outgoing lane
441  noInitialPoints = 3;
442  init.push_back(beg);
443  Line straightConn(fromE->getLaneShape(fromL)[-1], toE->getLaneShape(toL)[0]);
444  Position straightCenter = straightConn.getPositionAtDistance((SUMOReal) straightConn.length() / (SUMOReal) 2.);
445  Position center = straightCenter;//.add(straightCenter);
446  Line cross(straightConn);
447  cross.sub(cross.p1().x(), cross.p1().y());
448  cross.rotateAtP1(M_PI / 2);
449  center.sub(cross.p2());
450  init.push_back(center);
451  init.push_back(end);
452  } else {
453  const SUMOReal angle = fabs(fromE->getLaneShape(fromL).getEndLine().atan2Angle() - toE->getLaneShape(toL).getBegLine().atan2Angle());
454  if (angle < M_PI / 4. || angle > 7. / 4.*M_PI) {
455  // very low angle: almost straight
456  noInitialPoints = 4;
457  init.push_back(beg);
458  Line begL = fromE->getLaneShape(fromL).getEndLine();
459  begL.extrapolateSecondBy(100);
460  Line endL = toE->getLaneShape(toL).getBegLine();
461  endL.extrapolateFirstBy(100);
462  SUMOReal distance = beg.distanceTo(end);
463  if (distance > 10) {
464  {
465  SUMOReal off1 = fromE->getLaneShape(fromL).getEndLine().length() + (SUMOReal) 5. * (SUMOReal) fromE->getNumLanes();
466  off1 = MIN2(off1, (SUMOReal)(fromE->getLaneShape(fromL).getEndLine().length() + distance / 2.));
467  Position tmp = begL.getPositionAtDistance(off1);
468  init.push_back(tmp);
469  }
470  {
471  SUMOReal off1 = (SUMOReal) 100. - (SUMOReal) 5. * (SUMOReal) toE->getNumLanes();
472  off1 = MAX2(off1, (SUMOReal)(100. - distance / 2.));
473  Position tmp = endL.getPositionAtDistance(off1);
474  init.push_back(tmp);
475  }
476  } else {
477  noSpline = true;
478  }
479  init.push_back(end);
480  } else {
481  // turning
482  // - end of incoming lane
483  // - intersection of the extrapolated lanes
484  // - begin of outgoing lane
485  // attention: if there is no intersection, use a straight line
486  noInitialPoints = 3;
487  init.push_back(beg);
488  Line begL = fromE->getLaneShape(fromL).getEndLine();
489  Line endL = toE->getLaneShape(toL).getBegLine();
490  bool check = !begL.p1().almostSame(begL.p2()) && !endL.p1().almostSame(endL.p2());
491  if (check) {
492  begL.extrapolateSecondBy(100);
493  endL.extrapolateFirstBy(100);
494  } else {
495  WRITE_WARNING("Could not use edge geometry for internal lane, node '" + getID() + "'.");
496  }
497  if (!check || !begL.intersects(endL)) {
498  noSpline = true;
499  } else {
500  init.push_back(begL.intersectsAt(endL));
501  }
502  init.push_back(end);
503  }
504  }
505  }
506  //
507  if (noSpline) {
508  ret.push_back(fromE->getLaneShape(fromL).back());
509  ret.push_back(toE->getLaneShape(toL).front());
510  } else {
511  SUMOReal* def = new SUMOReal[1 + noInitialPoints * 3];
512  for (int i = 0; i < (int) init.size(); ++i) {
513  // starts at index 1
514  def[i * 3 + 1] = init[i].x();
515  def[i * 3 + 2] = 0;
516  def[i * 3 + 3] = init[i].y();
517  }
518  SUMOReal* ret_buf = new SUMOReal[numPoints * 3 + 1];
519  bezier(noInitialPoints, def, numPoints, ret_buf);
520  delete[] def;
521  Position prev;
522  for (int i = 0; i < (int) numPoints; i++) {
523  Position current(ret_buf[i * 3 + 1], ret_buf[i * 3 + 3]);
524  if (prev != current) {
525  ret.push_back(current);
526  }
527  prev = current;
528  }
529  delete[] ret_buf;
530  }
531  const NBEdge::Lane& lane = fromE->getLaneStruct(fromL);
532  if (lane.offset > 0) {
533  PositionVector beg = lane.shape.getSubpart(lane.shape.length() - lane.offset, lane.shape.length());;
534  beg.append(ret);
535  ret = beg;
536  }
537  return ret;
538 }
539 
540 
541 bool
542 NBNode::needsCont(NBEdge* fromE, NBEdge* toE, NBEdge* otherFromE, NBEdge* otherToE, const NBEdge::Connection& c) const {
544  return false;
545  }
546  if (fromE == otherFromE) {
547  // ignore same edge links
548  return false;
549  }
550  if (!foes(otherFromE, otherToE, fromE, toE)) {
551  // if they do not cross, no waiting place is needed
552  return false;
553  }
554  LinkDirection d1 = getDirection(fromE, toE);
555  LinkDirection d2 = getDirection(otherFromE, otherToE);
556  bool thisLeft = (d1 == LINKDIR_LEFT || d1 == LINKDIR_TURN);
557  bool otherLeft = (d2 == LINKDIR_LEFT || d2 == LINKDIR_TURN);
558  bool bothLeft = thisLeft && otherLeft;
559  if (c.tlID != "" && !bothLeft) {
560  // tls-controlled links will have space
561  return true;
562  }
563  if (fromE->getJunctionPriority(this) > 0 && otherFromE->getJunctionPriority(this) > 0) {
564  return mustBrake(fromE, toE, c.toLane);
565  }
566  return false;
567 }
568 
569 
570 void
572  delete myRequest; // possibly recomputation step
573  myRequest = 0;
574  if (myIncomingEdges.size() == 0 || myOutgoingEdges.size() == 0) {
575  // no logic if nothing happens here
577  return;
578  }
579  // check whether the node was set to be unregulated by the user
580  if (oc.getBool("keep-nodes-unregulated") || oc.isInStringVector("keep-nodes-unregulated.explicit", getID())
581  || (oc.getBool("keep-nodes-unregulated.district-nodes") && (isNearDistrict() || isDistrict()))) {
583  return;
584  }
585  // compute the logic if necessary or split the junction
587  // build the request
588  myRequest = new NBRequest(ec, this,
590  // check whether it is not too large
591  unsigned int numConnections = myRequest->getSizes().second;
592  if (numConnections >= 64) {
593  // yep -> make it untcontrolled, warn
594  WRITE_WARNING("Junction '" + getID() + "' is too complicated (#links>64); will be set to unregulated.");
595  delete myRequest;
596  myRequest = 0;
598  } else if (numConnections == 0) {
599  delete myRequest;
600  myRequest = 0;
602  } else {
604  }
605  }
606 }
607 
608 
609 bool
610 NBNode::writeLogic(OutputDevice& into, const bool checkLaneFoes) const {
611  if (myRequest) {
612  myRequest->writeLogic(myID, into, checkLaneFoes);
613  return true;
614  }
615  return false;
616 }
617 
618 
619 void
620 NBNode::computeNodeShape(bool leftHand) {
621  if (myIncomingEdges.size() == 0 && myOutgoingEdges.size() == 0) {
622  return;
623  }
624  try {
625  NBNodeShapeComputer computer(*this);
626  myPoly = computer.compute(leftHand);
627  } catch (InvalidArgument&) {
628  WRITE_WARNING("For node '" + getID() + "': could not compute shape.");
629  // make sure our shape is not empty because our XML schema forbids empty attributes
630  myPoly.clear();
632  }
633 }
634 
635 
636 void
638  // special case a):
639  // one in, one out, the outgoing has one lane more
640  if (myIncomingEdges.size() == 1 && myOutgoingEdges.size() == 1
641  && myIncomingEdges[0]->getNumLanes() == myOutgoingEdges[0]->getNumLanes() - 1
642  && myIncomingEdges[0] != myOutgoingEdges[0]
643  && myIncomingEdges[0]->isConnectedTo(myOutgoingEdges[0])) {
644 
645  NBEdge* incoming = myIncomingEdges[0];
646  NBEdge* outgoing = myOutgoingEdges[0];
647  // check if it's not the turnaround
648  if (incoming->getTurnDestination() == outgoing) {
649  // will be added later or not...
650  return;
651  }
652  for (int i = 0; i < (int) incoming->getNumLanes(); ++i) {
653  incoming->setConnection(i, outgoing, i + 1, NBEdge::L2L_COMPUTED);
654  }
655  incoming->setConnection(0, outgoing, 0, NBEdge::L2L_COMPUTED);
656  return;
657  }
658  // special case b):
659  // two in, one out, the outgoing has the same number of lanes as the sum of the incoming
660  // --> highway on-ramp
661  bool check = false;
662  if (myIncomingEdges.size() == 2 && myOutgoingEdges.size() == 1) {
663  check = myIncomingEdges[0]->getNumLanes() + myIncomingEdges[1]->getNumLanes() == myOutgoingEdges[0]->getNumLanes();
664  check &= (myIncomingEdges[0]->getStep() <= NBEdge::LANES2EDGES);
665  check &= (myIncomingEdges[1]->getStep() <= NBEdge::LANES2EDGES);
666  check &= myIncomingEdges[0] != myOutgoingEdges[0];
667  check &= myIncomingEdges[1] != myOutgoingEdges[0];
668  check &= myIncomingEdges[0]->isConnectedTo(myOutgoingEdges[0]);
669  check &= myIncomingEdges[1]->isConnectedTo(myOutgoingEdges[0]);
670  }
671  if (check) {
672  NBEdge* inc1 = myIncomingEdges[0];
673  NBEdge* inc2 = myIncomingEdges[1];
674  // for internal: check which one is the rightmost
675  SUMOReal a1 = inc1->getAngleAtNode(this);
676  SUMOReal a2 = inc2->getAngleAtNode(this);
679  if (ccw > cw) {
680  std::swap(inc1, inc2);
681  }
682  inc1->addLane2LaneConnections(0, myOutgoingEdges[0], 0, inc1->getNumLanes(), NBEdge::L2L_VALIDATED, true, true);
683  inc2->addLane2LaneConnections(0, myOutgoingEdges[0], inc1->getNumLanes(), inc2->getNumLanes(), NBEdge::L2L_VALIDATED, true, true);
684  return;
685  }
686  // special case c):
687  // one in, two out, the incoming has the same number of lanes as the sum of the outgoing
688  // --> highway off-ramp
689  check = false;
690  if (myIncomingEdges.size() == 1 && myOutgoingEdges.size() == 2) {
691  check = myIncomingEdges[0]->getNumLanes() == myOutgoingEdges[1]->getNumLanes() + myOutgoingEdges[0]->getNumLanes();
692  check &= (myIncomingEdges[0]->getStep() <= NBEdge::LANES2EDGES);
693  check &= myIncomingEdges[0] != myOutgoingEdges[0];
694  check &= myIncomingEdges[0] != myOutgoingEdges[1];
695  check &= myIncomingEdges[0]->isConnectedTo(myOutgoingEdges[0]);
696  check &= myIncomingEdges[0]->isConnectedTo(myOutgoingEdges[1]);
697  }
698  if (check) {
699  NBEdge* out1 = myOutgoingEdges[0];
700  NBEdge* out2 = myOutgoingEdges[1];
701  // for internal: check which one is the rightmost
703  std::swap(out1, out2);
704  }
705  myIncomingEdges[0]->addLane2LaneConnections(0, out1, 0, out1->getNumLanes(), NBEdge::L2L_VALIDATED, true, true);
706  myIncomingEdges[0]->addLane2LaneConnections(out1->getNumLanes(), out2, 0, out2->getNumLanes(), NBEdge::L2L_VALIDATED, false, true);
707  return;
708  }
709 
710  // go through this node's outgoing edges
711  // for every outgoing edge, compute the distribution of the node's
712  // incoming edges on this edge when approaching this edge
713  // the incoming edges' steps will then also be marked as LANE2LANE_RECHECK...
714  EdgeVector::reverse_iterator i;
715  for (i = myOutgoingEdges.rbegin(); i != myOutgoingEdges.rend(); i++) {
716  NBEdge* currentOutgoing = *i;
717  // get the information about edges that do approach this edge
718  EdgeVector* approaching = getEdgesThatApproach(currentOutgoing);
719  if (approaching->size() != 0) {
720  ApproachingDivider divider(approaching, currentOutgoing);
721  Bresenham::compute(&divider, static_cast<unsigned int>(approaching->size()),
722  currentOutgoing->getNumLanes());
723  }
724  delete approaching;
725  }
726  // ... but we may have the case that there are no outgoing edges
727  // In this case, we have to mark the incoming edges as being in state
728  // LANE2LANE( not RECHECK) by hand
729  if (myOutgoingEdges.size() == 0) {
730  for (i = myIncomingEdges.rbegin(); i != myIncomingEdges.rend(); i++) {
731  (*i)->markAsInLane2LaneState();
732  }
733  }
734 }
735 
736 
737 EdgeVector*
739  // get the position of the node to get the approaching nodes of
740  EdgeVector::const_iterator i = find(myAllEdges.begin(),
741  myAllEdges.end(), currentOutgoing);
742  // get the first possible approaching edge
744  // go through the list of edges clockwise and add the edges
745  EdgeVector* approaching = new EdgeVector();
746  for (; *i != currentOutgoing;) {
747  // check only incoming edges
748  if ((*i)->getToNode() == this && (*i)->getTurnDestination() != currentOutgoing) {
749  std::vector<int> connLanes = (*i)->getConnectionLanes(currentOutgoing);
750  if (connLanes.size() != 0) {
751  approaching->push_back(*i);
752  }
753  }
755  }
756  return approaching;
757 }
758 
759 
760 void
761 NBNode::replaceOutgoing(NBEdge* which, NBEdge* by, unsigned int laneOff) {
762  // replace the edge in the list of outgoing nodes
763  EdgeVector::iterator i = find(myOutgoingEdges.begin(), myOutgoingEdges.end(), which);
764  if (i != myOutgoingEdges.end()) {
765  (*i) = by;
766  i = find(myAllEdges.begin(), myAllEdges.end(), which);
767  (*i) = by;
768  }
769  // replace the edge in connections of incoming edges
770  for (i = myIncomingEdges.begin(); i != myIncomingEdges.end(); ++i) {
771  (*i)->replaceInConnections(which, by, laneOff);
772  }
773  // replace within the connetion prohibition dependencies
774  replaceInConnectionProhibitions(which, by, 0, laneOff);
775 }
776 
777 
778 void
780  // replace edges
781  unsigned int laneOff = 0;
782  for (EdgeVector::const_iterator i = which.begin(); i != which.end(); i++) {
783  replaceOutgoing(*i, by, laneOff);
784  laneOff += (*i)->getNumLanes();
785  }
786  // removed SUMOReal occurences
788  // check whether this node belongs to a district and the edges
789  // must here be also remapped
790  if (myDistrict != 0) {
791  myDistrict->replaceOutgoing(which, by);
792  }
793 }
794 
795 
796 void
797 NBNode::replaceIncoming(NBEdge* which, NBEdge* by, unsigned int laneOff) {
798  // replace the edge in the list of incoming nodes
799  EdgeVector::iterator i = find(myIncomingEdges.begin(), myIncomingEdges.end(), which);
800  if (i != myIncomingEdges.end()) {
801  (*i) = by;
802  i = find(myAllEdges.begin(), myAllEdges.end(), which);
803  (*i) = by;
804  }
805  // replace within the connetion prohibition dependencies
806  replaceInConnectionProhibitions(which, by, laneOff, 0);
807 }
808 
809 
810 void
812  // replace edges
813  unsigned int laneOff = 0;
814  for (EdgeVector::const_iterator i = which.begin(); i != which.end(); i++) {
815  replaceIncoming(*i, by, laneOff);
816  laneOff += (*i)->getNumLanes();
817  }
818  // removed SUMOReal occurences
820  // check whether this node belongs to a district and the edges
821  // must here be also remapped
822  if (myDistrict != 0) {
823  myDistrict->replaceIncoming(which, by);
824  }
825 }
826 
827 
828 
829 void
831  unsigned int whichLaneOff, unsigned int byLaneOff) {
832  // replace in keys
833  NBConnectionProhibits::iterator j = myBlockedConnections.begin();
834  while (j != myBlockedConnections.end()) {
835  bool changed = false;
836  NBConnection c = (*j).first;
837  if (c.replaceFrom(which, whichLaneOff, by, byLaneOff)) {
838  changed = true;
839  }
840  if (c.replaceTo(which, whichLaneOff, by, byLaneOff)) {
841  changed = true;
842  }
843  if (changed) {
844  myBlockedConnections[c] = (*j).second;
845  myBlockedConnections.erase(j);
846  j = myBlockedConnections.begin();
847  } else {
848  j++;
849  }
850  }
851  // replace in values
852  for (j = myBlockedConnections.begin(); j != myBlockedConnections.end(); j++) {
853  NBConnectionVector& prohibiting = (*j).second;
854  for (NBConnectionVector::iterator k = prohibiting.begin(); k != prohibiting.end(); k++) {
855  NBConnection& sprohibiting = *k;
856  sprohibiting.replaceFrom(which, whichLaneOff, by, byLaneOff);
857  sprohibiting.replaceTo(which, whichLaneOff, by, byLaneOff);
858  }
859  }
860 }
861 
862 
863 
864 void
866  unsigned int i, j;
867  // check incoming
868  for (i = 0; myIncomingEdges.size() > 0 && i < myIncomingEdges.size() - 1; i++) {
869  j = i + 1;
870  while (j < myIncomingEdges.size()) {
871  if (myIncomingEdges[i] == myIncomingEdges[j]) {
872  myIncomingEdges.erase(myIncomingEdges.begin() + j);
873  } else {
874  j++;
875  }
876  }
877  }
878  // check outgoing
879  for (i = 0; myOutgoingEdges.size() > 0 && i < myOutgoingEdges.size() - 1; i++) {
880  j = i + 1;
881  while (j < myOutgoingEdges.size()) {
882  if (myOutgoingEdges[i] == myOutgoingEdges[j]) {
883  myOutgoingEdges.erase(myOutgoingEdges.begin() + j);
884  } else {
885  j++;
886  }
887  }
888  }
889  // check all
890  for (i = 0; myAllEdges.size() > 0 && i < myAllEdges.size() - 1; i++) {
891  j = i + 1;
892  while (j < myAllEdges.size()) {
893  if (myAllEdges[i] == myAllEdges[j]) {
894  myAllEdges.erase(myAllEdges.begin() + j);
895  } else {
896  j++;
897  }
898  }
899  }
900 }
901 
902 
903 bool
904 NBNode::hasIncoming(const NBEdge* const e) const {
905  return find(myIncomingEdges.begin(), myIncomingEdges.end(), e) != myIncomingEdges.end();
906 }
907 
908 
909 bool
910 NBNode::hasOutgoing(const NBEdge* const e) const {
911  return find(myOutgoingEdges.begin(), myOutgoingEdges.end(), e) != myOutgoingEdges.end();
912 }
913 
914 
915 NBEdge*
917  EdgeVector edges = myIncomingEdges;
918  if (find(edges.begin(), edges.end(), e) != edges.end()) {
919  edges.erase(find(edges.begin(), edges.end(), e));
920  }
921  if (edges.size() == 0) {
922  return 0;
923  }
924  if (e->getToNode() == this) {
925  sort(edges.begin(), edges.end(), NBContHelper::edge_opposite_direction_sorter(e, this));
926  } else {
927  sort(edges.begin(), edges.end(), NBContHelper::edge_similar_direction_sorter(e));
928  }
929  return edges[0];
930 }
931 
932 
933 void
935  const NBConnection& mustStop) {
936  if (mayDrive.getFrom() == 0 ||
937  mayDrive.getTo() == 0 ||
938  mustStop.getFrom() == 0 ||
939  mustStop.getTo() == 0) {
940 
941  WRITE_WARNING("Something went wrong during the building of a connection...");
942  return; // !!! mark to recompute connections
943  }
944  NBConnectionVector conn = myBlockedConnections[mustStop];
945  conn.push_back(mayDrive);
946  myBlockedConnections[mustStop] = conn;
947 }
948 
949 
950 NBEdge*
951 NBNode::getPossiblySplittedIncoming(const std::string& edgeid) {
952  unsigned int size = (unsigned int) edgeid.length();
953  for (EdgeVector::iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
954  std::string id = (*i)->getID();
955  if (id.substr(0, size) == edgeid) {
956  return *i;
957  }
958  }
959  return 0;
960 }
961 
962 
963 NBEdge*
964 NBNode::getPossiblySplittedOutgoing(const std::string& edgeid) {
965  unsigned int size = (unsigned int) edgeid.length();
966  for (EdgeVector::iterator i = myOutgoingEdges.begin(); i != myOutgoingEdges.end(); i++) {
967  std::string id = (*i)->getID();
968  if (id.substr(0, size) == edgeid) {
969  return *i;
970  }
971  }
972  return 0;
973 }
974 
975 
976 void
977 NBNode::removeEdge(NBEdge* edge, bool removeFromConnections) {
978  EdgeVector::iterator i = find(myAllEdges.begin(), myAllEdges.end(), edge);
979  if (i != myAllEdges.end()) {
980  myAllEdges.erase(i);
981  i = find(myOutgoingEdges.begin(), myOutgoingEdges.end(), edge);
982  if (i != myOutgoingEdges.end()) {
983  myOutgoingEdges.erase(i);
984  } else {
985  i = find(myIncomingEdges.begin(), myIncomingEdges.end(), edge);
986  if (i != myIncomingEdges.end()) {
987  myIncomingEdges.erase(i);
988  } else {
989  // edge must have been either incoming or outgoing
990  assert(false);
991  }
992  }
993  if (removeFromConnections) {
994  for (i = myAllEdges.begin(); i != myAllEdges.end(); ++i) {
995  (*i)->removeFromConnections(edge);
996  }
997  }
998  }
999 }
1000 
1001 
1002 Position
1004  Position pos(0, 0);
1005  EdgeVector::const_iterator i;
1006  for (i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
1007  NBNode* conn = (*i)->getFromNode();
1008  Position toAdd = conn->getPosition();
1009  toAdd.sub(myPosition);
1010  toAdd.mul((SUMOReal) 1.0 / sqrt(toAdd.x()*toAdd.x() + toAdd.y()*toAdd.y()));
1011  pos.add(toAdd);
1012  }
1013  for (i = myOutgoingEdges.begin(); i != myOutgoingEdges.end(); i++) {
1014  NBNode* conn = (*i)->getToNode();
1015  Position toAdd = conn->getPosition();
1016  toAdd.sub(myPosition);
1017  toAdd.mul((SUMOReal) 1.0 / sqrt(toAdd.x()*toAdd.x() + toAdd.y()*toAdd.y()));
1018  pos.add(toAdd);
1019  }
1020  pos.mul((SUMOReal) - 1.0 / (myIncomingEdges.size() + myOutgoingEdges.size()));
1021  if (pos.x() == 0 && pos.y() == 0) {
1022  pos = Position(1, 0);
1023  }
1024  pos.norm2d();
1025  return pos;
1026 }
1027 
1028 
1029 
1030 void
1032  for (EdgeVector::const_iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
1033  (*i)->invalidateConnections();
1034  }
1035 }
1036 
1037 
1038 void
1040  for (EdgeVector::const_iterator i = myOutgoingEdges.begin(); i != myOutgoingEdges.end(); i++) {
1041  (*i)->invalidateConnections();
1042  }
1043 }
1044 
1045 
1046 bool
1047 NBNode::mustBrake(const NBEdge* const from, const NBEdge* const to, int toLane) const {
1048  // check whether it is participant to a traffic light
1049  // - controlled links are set by the traffic lights, not the normal
1050  // right-of-way rules
1051  // - uncontrolled participants (spip lanes etc.) should always break
1052  if (myTrafficLights.size() != 0) {
1053  // ok, we have a traffic light, return true by now, it will be later
1054  // controlled by the tls
1055  return true;
1056  }
1057  // unregulated->does not need to brake
1058  if (myRequest == 0) {
1059  return false;
1060  }
1061  // vehicles which do not have a following lane must always decelerate to the end
1062  if (to == 0) {
1063  return true;
1064  }
1065  // check whether any other connection on this node prohibits this connection
1066  bool try1 = myRequest->mustBrake(from, to);
1067  if (!try1 || toLane == -1) {
1068  return try1;
1069  }
1070  if (from->getSpeed() < 70. / 3.6) {
1071  return try1;
1072  }
1073  // on highways (on-ramps, in fact):
1074  // check whether any other connection uses the same destination edge
1075  for (EdgeVector::const_iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
1076  if ((*i) == from) {
1077  continue;
1078  }
1079  const std::vector<NBEdge::Connection>& connections = (*i)->getConnections();
1080  for (std::vector<NBEdge::Connection>::const_iterator j = connections.begin(); j != connections.end(); ++j) {
1081  if ((*j).toEdge == to && ((*j).toLane < 0 || (*j).toLane == toLane)) {
1082  return true;
1083  }
1084  }
1085  }
1086  return false;
1087 }
1088 
1089 
1090 bool
1091 NBNode::isLeftMover(const NBEdge* const from, const NBEdge* const to) const {
1092  // when the junction has only one incoming edge, there are no
1093  // problems caused by left blockings
1094  if (myIncomingEdges.size() == 1 || myOutgoingEdges.size() == 1) {
1095  return false;
1096  }
1097  SUMOReal fromAngle = from->getAngleAtNode(this);
1098  SUMOReal toAngle = to->getAngleAtNode(this);
1099  SUMOReal cw = GeomHelper::getCWAngleDiff(fromAngle, toAngle);
1100  SUMOReal ccw = GeomHelper::getCCWAngleDiff(fromAngle, toAngle);
1101  std::vector<NBEdge*>::const_iterator i = std::find(myAllEdges.begin(), myAllEdges.end(), from);
1102  do {
1104  } while ((!hasOutgoing(*i) || from->isTurningDirectionAt(this, *i)) && *i != from);
1105  return cw < ccw && (*i) == to && myOutgoingEdges.size() > 2;
1106 }
1107 
1108 
1109 bool
1110 NBNode::forbids(const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
1111  const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
1112  bool regardNonSignalisedLowerPriority) const {
1113  return myRequest != 0 && myRequest->forbids(possProhibitorFrom, possProhibitorTo,
1114  possProhibitedFrom, possProhibitedTo,
1115  regardNonSignalisedLowerPriority);
1116 }
1117 
1118 
1119 bool
1120 NBNode::foes(const NBEdge* const from1, const NBEdge* const to1,
1121  const NBEdge* const from2, const NBEdge* const to2) const {
1122  return myRequest != 0 && myRequest->foes(from1, to1, from2, to2);
1123 }
1124 
1125 
1126 void
1128  NBEdge* removed, const EdgeVector& incoming,
1129  const EdgeVector& outgoing) {
1130  assert(find(incoming.begin(), incoming.end(), removed) == incoming.end());
1131  bool changed = true;
1132  while (changed) {
1133  changed = false;
1134  NBConnectionProhibits blockedConnectionsTmp = myBlockedConnections;
1135  NBConnectionProhibits blockedConnectionsNew;
1136  // remap in connections
1137  for (NBConnectionProhibits::iterator i = blockedConnectionsTmp.begin(); i != blockedConnectionsTmp.end(); i++) {
1138  const NBConnection& blocker = (*i).first;
1139  const NBConnectionVector& blocked = (*i).second;
1140  // check the blocked connections first
1141  // check whether any of the blocked must be changed
1142  bool blockedChanged = false;
1143  NBConnectionVector newBlocked;
1144  NBConnectionVector::const_iterator j;
1145  for (j = blocked.begin(); j != blocked.end(); j++) {
1146  const NBConnection& sblocked = *j;
1147  if (sblocked.getFrom() == removed || sblocked.getTo() == removed) {
1148  blockedChanged = true;
1149  }
1150  }
1151  // adapt changes if so
1152  for (j = blocked.begin(); blockedChanged && j != blocked.end(); j++) {
1153  const NBConnection& sblocked = *j;
1154  if (sblocked.getFrom() == removed && sblocked.getTo() == removed) {
1155  /* for(EdgeVector::const_iterator k=incoming.begin(); k!=incoming.end(); k++) {
1156  !!! newBlocked.push_back(NBConnection(*k, *k));
1157  }*/
1158  } else if (sblocked.getFrom() == removed) {
1159  assert(sblocked.getTo() != removed);
1160  for (EdgeVector::const_iterator k = incoming.begin(); k != incoming.end(); k++) {
1161  newBlocked.push_back(NBConnection(*k, sblocked.getTo()));
1162  }
1163  } else if (sblocked.getTo() == removed) {
1164  assert(sblocked.getFrom() != removed);
1165  for (EdgeVector::const_iterator k = outgoing.begin(); k != outgoing.end(); k++) {
1166  newBlocked.push_back(NBConnection(sblocked.getFrom(), *k));
1167  }
1168  } else {
1169  newBlocked.push_back(NBConnection(sblocked.getFrom(), sblocked.getTo()));
1170  }
1171  }
1172  if (blockedChanged) {
1173  blockedConnectionsNew[blocker] = newBlocked;
1174  changed = true;
1175  }
1176  // if the blocked were kept
1177  else {
1178  if (blocker.getFrom() == removed && blocker.getTo() == removed) {
1179  changed = true;
1180  /* for(EdgeVector::const_iterator k=incoming.begin(); k!=incoming.end(); k++) {
1181  !!! blockedConnectionsNew[NBConnection(*k, *k)] = blocked;
1182  }*/
1183  } else if (blocker.getFrom() == removed) {
1184  assert(blocker.getTo() != removed);
1185  changed = true;
1186  for (EdgeVector::const_iterator k = incoming.begin(); k != incoming.end(); k++) {
1187  blockedConnectionsNew[NBConnection(*k, blocker.getTo())] = blocked;
1188  }
1189  } else if (blocker.getTo() == removed) {
1190  assert(blocker.getFrom() != removed);
1191  changed = true;
1192  for (EdgeVector::const_iterator k = outgoing.begin(); k != outgoing.end(); k++) {
1193  blockedConnectionsNew[NBConnection(blocker.getFrom(), *k)] = blocked;
1194  }
1195  } else {
1196  blockedConnectionsNew[blocker] = blocked;
1197  }
1198  }
1199  }
1200  myBlockedConnections = blockedConnectionsNew;
1201  }
1202  // remap in traffic lights
1203  tc.remapRemoved(removed, incoming, outgoing);
1204 }
1205 
1206 
1208 NBNode::getDirection(const NBEdge* const incoming, const NBEdge* const outgoing) const {
1209  // ok, no connection at all -> dead end
1210  if (outgoing == 0) {
1211  return LINKDIR_NODIR;
1212  }
1213  // turning direction
1214  if (incoming->isTurningDirectionAt(this, outgoing)) {
1215  return LINKDIR_TURN;
1216  }
1217  // get the angle between incoming/outgoing at the junction
1218  SUMOReal angle =
1219  NBHelpers::normRelAngle(incoming->getAngleAtNode(this), outgoing->getAngleAtNode(this));
1220  // ok, should be a straight connection
1221  if (abs((int) angle) + 1 < 45) {
1222  return LINKDIR_STRAIGHT;
1223  }
1224 
1225  // check for left and right, first
1226  if (angle > 0) {
1227  // check whether any other edge goes further to the right
1228  EdgeVector::const_iterator i =
1229  find(myAllEdges.begin(), myAllEdges.end(), outgoing);
1231  while ((*i) != incoming) {
1232  if ((*i)->getFromNode() == this) {
1233  return LINKDIR_PARTRIGHT;
1234  }
1236  }
1237  return LINKDIR_RIGHT;
1238  }
1239  // check whether any other edge goes further to the left
1240  EdgeVector::const_iterator i =
1241  find(myAllEdges.begin(), myAllEdges.end(), outgoing);
1243  while ((*i) != incoming) {
1244  if ((*i)->getFromNode() == this && !incoming->isTurningDirectionAt(this, *i)) {
1245  return LINKDIR_PARTLEFT;
1246  }
1248  }
1249  return LINKDIR_LEFT;
1250 }
1251 
1252 
1253 LinkState
1254 NBNode::getLinkState(const NBEdge* incoming, NBEdge* outgoing, int fromlane,
1255  bool mayDefinitelyPass, const std::string& tlID) const {
1256  if (tlID != "") {
1258  }
1259  if (outgoing == 0) { // always off
1261  }
1263  return LINKSTATE_EQUAL; // all the same
1264  }
1265  if (myType == NODETYPE_ALLWAY_STOP) {
1266  return LINKSTATE_ALLWAY_STOP; // all drive, first one to arrive may drive first
1267  }
1268  if ((!incoming->isInnerEdge() && mustBrake(incoming, outgoing, fromlane)) && !mayDefinitelyPass) {
1269  return myType == NODETYPE_PRIORITY_STOP ? LINKSTATE_STOP : LINKSTATE_MINOR; // minor road
1270  }
1271  // traffic lights are not regarded here
1272  return LINKSTATE_MAJOR;
1273 }
1274 
1275 
1276 bool
1278  // check whether this node is included in a traffic light
1279  if (myTrafficLights.size() != 0) {
1280  return false;
1281  }
1282  EdgeVector::const_iterator i;
1283  // one in, one out -> just a geometry ...
1284  if (myOutgoingEdges.size() == 1 && myIncomingEdges.size() == 1) {
1285  // ... if types match ...
1286  if (!myIncomingEdges[0]->expandableBy(myOutgoingEdges[0])) {
1287  return false;
1288  }
1289  //
1290  return myIncomingEdges[0]->getFromNode() != myOutgoingEdges[0]->getToNode();
1291  }
1292  // two in, two out -> may be something else
1293  if (myOutgoingEdges.size() == 2 && myIncomingEdges.size() == 2) {
1294  // check whether the origin nodes of the incoming edges differ
1295  std::set<NBNode*> origSet;
1296  for (i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
1297  origSet.insert((*i)->getFromNode());
1298  }
1299  if (origSet.size() < 2) {
1300  return false;
1301  }
1302  // check whether this node is an intermediate node of
1303  // a two-directional street
1304  for (i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
1305  // try to find the opposite direction
1306  NBNode* origin = (*i)->getFromNode();
1307  // find the back direction of the current edge
1308  EdgeVector::const_iterator j =
1309  find_if(myOutgoingEdges.begin(), myOutgoingEdges.end(),
1311  // check whether the back direction exists
1312  if (j != myOutgoingEdges.end()) {
1313  // check whether the edge from the backdirection (must be
1314  // the counter-clockwise one) may be joined with the current
1316  // check whether the types allow joining
1317  if (!(*i)->expandableBy(*j)) {
1318  return false;
1319  }
1320  } else {
1321  // ok, at least one outgoing edge is not an opposite
1322  // of an incoming one
1323  return false;
1324  }
1325  }
1326  return true;
1327  }
1328  // ok, a real node
1329  return false;
1330 }
1331 
1332 
1333 std::vector<std::pair<NBEdge*, NBEdge*> >
1335  assert(checkIsRemovable());
1336  std::vector<std::pair<NBEdge*, NBEdge*> > ret;
1337  // one in, one out-case
1338  if (myOutgoingEdges.size() == 1 && myIncomingEdges.size() == 1) {
1339  ret.push_back(
1340  std::pair<NBEdge*, NBEdge*>(
1342  return ret;
1343  }
1344  // two in, two out-case
1345  for (EdgeVector::const_iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
1346  NBNode* origin = (*i)->getFromNode();
1347  EdgeVector::const_iterator j =
1348  find_if(myOutgoingEdges.begin(), myOutgoingEdges.end(),
1351  ret.push_back(std::pair<NBEdge*, NBEdge*>(*i, *j));
1352  }
1353  return ret;
1354 }
1355 
1356 
1357 const PositionVector&
1359  return myPoly;
1360 }
1361 
1362 
1363 NBEdge*
1365  for (EdgeVector::const_iterator i = myOutgoingEdges.begin(); i != myOutgoingEdges.end(); i++) {
1366  if ((*i)->getToNode() == n) {
1367  return (*i);
1368  }
1369  }
1370  return 0;
1371 }
1372 
1373 
1374 bool
1376  if (isDistrict()) {
1377  return false;
1378  }
1379  EdgeVector edges;
1380  copy(getIncomingEdges().begin(), getIncomingEdges().end(),
1381  back_inserter(edges));
1382  copy(getOutgoingEdges().begin(), getOutgoingEdges().end(),
1383  back_inserter(edges));
1384  for (EdgeVector::const_iterator j = edges.begin(); j != edges.end(); ++j) {
1385  NBEdge* t = *j;
1386  NBNode* other = 0;
1387  if (t->getToNode() == this) {
1388  other = t->getFromNode();
1389  } else {
1390  other = t->getToNode();
1391  }
1392  EdgeVector edges2;
1393  copy(other->getIncomingEdges().begin(), other->getIncomingEdges().end(), back_inserter(edges2));
1394  copy(other->getOutgoingEdges().begin(), other->getOutgoingEdges().end(), back_inserter(edges2));
1395  for (EdgeVector::const_iterator k = edges2.begin(); k != edges2.end(); ++k) {
1396  if ((*k)->getFromNode()->isDistrict() || (*k)->getToNode()->isDistrict()) {
1397  return true;
1398  }
1399  }
1400  }
1401  return false;
1402 }
1403 
1404 
1405 bool
1407  return myType == NODETYPE_DISTRICT;
1408 }
1409 
1410 
1411 void
1413  unsigned int noInternalNoSplits = 0;
1414  for (EdgeVector::const_iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
1415  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
1416  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
1417  if ((*k).toEdge == 0) {
1418  continue;
1419  }
1420  noInternalNoSplits++;
1421  }
1422  }
1423  unsigned int lno = 0;
1424  unsigned int splitNo = 0;
1425  for (EdgeVector::const_iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
1426  (*i)->buildInnerEdges(*this, noInternalNoSplits, lno, splitNo);
1427  }
1428 }
1429 
1430 
1431 bool
1433  if (myIncomingEdges.size() == 1 && myOutgoingEdges.size() == 1) {
1434  return true;
1435  }
1436  if (myIncomingEdges.size() == 2 && myOutgoingEdges.size() == 2) {
1437  // check whether the incoming and outgoing edges are pairwise (near) parallel and
1438  // thus the only cross-connections could be turn-arounds
1439  NBEdge* out0 = myOutgoingEdges[0];
1440  NBEdge* out1 = myOutgoingEdges[1];
1441  for (EdgeVector::const_iterator it = myIncomingEdges.begin(); it != myIncomingEdges.end(); ++it) {
1442  NBEdge* inEdge = *it;
1443  SUMOReal angle0 = fabs(NBHelpers::relAngle(inEdge->getAngleAtNode(this), out0->getAngleAtNode(this)));
1444  SUMOReal angle1 = fabs(NBHelpers::relAngle(inEdge->getAngleAtNode(this), out1->getAngleAtNode(this)));
1445  if (MAX2(angle0, angle1) <= 160) {
1446  // neither of the outgoing edges is parallel to inEdge
1447  return false;
1448  }
1449  }
1450  return true;
1451  }
1452  return false;
1453 }
1454 
1455 Position
1457  /* Conceptually, the center point would be identical with myPosition.
1458  * However, if the shape is influenced by custom geometry endpoints of the adjoining edges,
1459  * myPosition may fall outside the shape. In this case it is better to use
1460  * the center of the shape
1461  **/
1462  PositionVector tmp = myPoly;
1463  tmp.closePolygon();
1464  //std::cout << getID() << " around=" << tmp.around(myPosition) << " dist=" << tmp.distance(myPosition) << "\n";
1465  if (tmp.size() < 3 || tmp.around(myPosition) || tmp.distance(myPosition) < POSITION_EPS) {
1466  return myPosition;
1467  } else {
1468  return myPoly.getPolygonCenter();
1469  }
1470 }
1471 
1472 /****************************************************************************/
1473