SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NBAlgorithms_Ramps.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // Algorithms for highway on-/off-ramps computation
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
10 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <cassert>
34 #include "NBNetBuilder.h"
35 #include "NBNodeCont.h"
36 #include "NBNode.h"
37 #include "NBEdge.h"
38 #include "NBAlgorithms_Ramps.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // static members
47 // ===========================================================================
48 const std::string NBRampsComputer::ADDED_ON_RAMP_EDGE("-AddedOnRampEdge");
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 // ---------------------------------------------------------------------------
54 // NBRampsComputer
55 // ---------------------------------------------------------------------------
56 void
58  SUMOReal minHighwaySpeed = oc.getFloat("ramps.min-highway-speed");
59  SUMOReal maxRampSpeed = oc.getFloat("ramps.max-ramp-speed");
60  SUMOReal rampLength = oc.getFloat("ramps.ramp-length");
61  bool dontSplit = oc.getBool("ramps.no-split");
62  std::set<NBEdge*> incremented;
63  // check whether on-off ramps shall be guessed
64  if (oc.getBool("ramps.guess")) {
65  NBNodeCont& nc = nb.getNodeCont();
66  NBEdgeCont& ec = nb.getEdgeCont();
68  std::set<NBNode*> potOnRamps;
69  std::set<NBNode*> potOffRamps;
70  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
71  NBNode* cur = (*i).second;
72  if (mayNeedOnRamp(cur, minHighwaySpeed, maxRampSpeed)) {
73  potOnRamps.insert(cur);
74  }
75  if (mayNeedOffRamp(cur, minHighwaySpeed, maxRampSpeed)) {
76  potOffRamps.insert(cur);
77  }
78  }
79  for (std::set<NBNode*>::const_iterator i = potOnRamps.begin(); i != potOnRamps.end(); ++i) {
80  buildOnRamp(*i, nc, ec, dc, rampLength, dontSplit, incremented);
81  }
82  for (std::set<NBNode*>::const_iterator i = potOffRamps.begin(); i != potOffRamps.end(); ++i) {
83  buildOffRamp(*i, nc, ec, dc, rampLength, dontSplit, incremented);
84  }
85  }
86  // check whether on-off ramps shall be guessed
87  if (oc.isSet("ramps.set")) {
88  std::vector<std::string> edges = oc.getStringVector("ramps.set");
89  NBNodeCont& nc = nb.getNodeCont();
90  NBEdgeCont& ec = nb.getEdgeCont();
92  for (std::vector<std::string>::iterator i = edges.begin(); i != edges.end(); ++i) {
93  NBEdge* e = ec.retrieve(*i);
94  if (e == 0) {
95  WRITE_WARNING("Can not build on ramp on edge '" + *i + "' - the edge is not known.");
96  continue;
97  }
98  NBNode* from = e->getFromNode();
99  if (from->getIncomingEdges().size() == 2 && from->getOutgoingEdges().size() == 1) {
100  buildOnRamp(from, nc, ec, dc, rampLength, dontSplit, incremented);
101  }
102  // load edge again to check offramps
103  e = ec.retrieve(*i);
104  if (e == 0) {
105  WRITE_WARNING("Can not build off ramp on edge '" + *i + "' - the edge is not known.");
106  continue;
107  }
108  NBNode* to = e->getToNode();
109  if (to->getIncomingEdges().size() == 1 && to->getOutgoingEdges().size() == 2) {
110  buildOffRamp(to, nc, ec, dc, rampLength, dontSplit, incremented);
111  }
112  }
113  }
114 }
115 
116 
117 bool
118 NBRampsComputer::mayNeedOnRamp(NBNode* cur, SUMOReal minHighwaySpeed, SUMOReal maxRampSpeed) {
119  if (cur->getOutgoingEdges().size() != 1 || cur->getIncomingEdges().size() != 2) {
120  return false;
121  }
122  NBEdge* potHighway, *potRamp, *cont;
123  getOnRampEdges(cur, &potHighway, &potRamp, &cont);
124  // may be an on-ramp
125  return fulfillsRampConstraints(potHighway, potRamp, cont, minHighwaySpeed, maxRampSpeed);
126 }
127 
128 
129 bool
130 NBRampsComputer::mayNeedOffRamp(NBNode* cur, SUMOReal minHighwaySpeed, SUMOReal maxRampSpeed) {
131  if (cur->getIncomingEdges().size() != 1 || cur->getOutgoingEdges().size() != 2) {
132  return false;
133  }
134  // may be an off-ramp
135  NBEdge* potHighway, *potRamp, *prev;
136  getOffRampEdges(cur, &potHighway, &potRamp, &prev);
137  return fulfillsRampConstraints(potHighway, potRamp, prev, minHighwaySpeed, maxRampSpeed);
138 }
139 
140 
141 void
142 NBRampsComputer::buildOnRamp(NBNode* cur, NBNodeCont& nc, NBEdgeCont& ec, NBDistrictCont& dc, SUMOReal rampLength, bool dontSplit, std::set<NBEdge*>& incremented) {
143  NBEdge* potHighway, *potRamp, *cont;
144  getOnRampEdges(cur, &potHighway, &potRamp, &cont);
145  // compute the number of lanes to append
146  const unsigned int firstLaneNumber = potHighway->getNumLanes();
147  int toAdd = (potRamp->getNumLanes() + firstLaneNumber) - cont->getNumLanes();
148  NBEdge* first = cont;
149  NBEdge* last = cont;
150  NBEdge* curr = cont;
151  if (toAdd > 0 && find(incremented.begin(), incremented.end(), cont) == incremented.end()) {
152  SUMOReal currLength = 0;
153  while (curr != 0 && currLength + curr->getGeometry().length() - POSITION_EPS < rampLength) {
154  if (find(incremented.begin(), incremented.end(), curr) == incremented.end()) {
155  curr->incLaneNo(toAdd);
156  curr->invalidateConnections(true);
157  incremented.insert(curr);
158  moveRampRight(curr, toAdd);
159  currLength += curr->getLength(); // !!! loaded length?
160  last = curr;
161  }
162  NBNode* nextN = curr->getToNode();
163  if (nextN->getOutgoingEdges().size() == 1) {
164  curr = nextN->getOutgoingEdges()[0];
165  if (curr->getNumLanes() != firstLaneNumber) {
166  // the number of lanes changes along the computation; we'll stop...
167  curr = 0;
168  }
169  } else {
170  // ambigous; and, in fact, what should it be? ...stop
171  curr = 0;
172  }
173  }
174  // check whether a further split is necessary
175  if (curr != 0 && !dontSplit && currLength - POSITION_EPS < rampLength && curr->getNumLanes() == firstLaneNumber && find(incremented.begin(), incremented.end(), curr) == incremented.end()) {
176  // there is enough place to build a ramp; do it
177  bool wasFirst = first == curr;
178  NBNode* rn = new NBNode(curr->getID() + "-AddedOnRampNode", curr->getGeometry().positionAtLengthPosition(rampLength - currLength));
179  if (!nc.insert(rn)) {
180  throw ProcessError("Ups - could not build on-ramp for edge '" + curr->getID() + "' (node could not be build)!");
181  }
182  std::string name = curr->getID();
183  bool ok = ec.splitAt(dc, curr, rn, curr->getID() + ADDED_ON_RAMP_EDGE, curr->getID(), curr->getNumLanes() + toAdd, curr->getNumLanes());
184  if (!ok) {
185  WRITE_ERROR("Ups - could not build on-ramp for edge '" + curr->getID() + "'!");
186  return;
187  }
188  //ec.retrieve(name)->invalidateConnections();
189  curr = ec.retrieve(name + ADDED_ON_RAMP_EDGE);
190  curr->invalidateConnections(true);
191  incremented.insert(curr);
192  last = curr;
193  moveRampRight(curr, toAdd);
194  if (wasFirst) {
195  first = curr;
196  }
197  }
198  }
199  // set connections from ramp/highway to added ramp
200  if (!potHighway->addLane2LaneConnections(0, first, potRamp->getNumLanes(), MIN2(first->getNumLanes() - potRamp->getNumLanes(), potHighway->getNumLanes()), NBEdge::L2L_VALIDATED, true, true)) {
201  throw ProcessError("Could not set connection!");
202  }
203  if (!potRamp->addLane2LaneConnections(0, first, 0, potRamp->getNumLanes(), NBEdge::L2L_VALIDATED, true, true)) {
204  throw ProcessError("Could not set connection!");
205  }
206  // patch ramp geometry
207  PositionVector p = potRamp->getGeometry();
208  p.pop_back();
209  p.push_back(first->getLaneShape(0)[0]);
210  potRamp->setGeometry(p);
211  // set connections from added ramp to following highway
212  NBNode* nextN = last->getToNode();
213  if (nextN->getOutgoingEdges().size() == 1) {
214  NBEdge* next = nextN->getOutgoingEdges()[0];//const EdgeVector& o1 = cont->getToNode()->getOutgoingEdges();
215  if (next->getNumLanes() < last->getNumLanes()) {
216  last->addLane2LaneConnections(last->getNumLanes() - next->getNumLanes(), next, 0, next->getNumLanes(), NBEdge::L2L_VALIDATED);
217  }
218  }
219 }
220 
221 
222 void
223 NBRampsComputer::buildOffRamp(NBNode* cur, NBNodeCont& nc, NBEdgeCont& ec, NBDistrictCont& dc, SUMOReal rampLength, bool dontSplit, std::set<NBEdge*>& incremented) {
224  NBEdge* potHighway, *potRamp, *prev;
225  getOffRampEdges(cur, &potHighway, &potRamp, &prev);
226  // compute the number of lanes to append
227  const unsigned int firstLaneNumber = potHighway->getNumLanes();
228  int toAdd = (potRamp->getNumLanes() + firstLaneNumber) - prev->getNumLanes();
229  NBEdge* first = prev;
230  NBEdge* last = prev;
231  NBEdge* curr = prev;
232  if (toAdd > 0 && find(incremented.begin(), incremented.end(), prev) == incremented.end()) {
233  SUMOReal currLength = 0;
234  while (curr != 0 && currLength + curr->getGeometry().length() - POSITION_EPS < rampLength) {
235  if (find(incremented.begin(), incremented.end(), curr) == incremented.end()) {
236  curr->incLaneNo(toAdd);
237  curr->invalidateConnections(true);
238  incremented.insert(curr);
239  moveRampRight(curr, toAdd);
240  currLength += curr->getLength(); // !!! loaded length?
241  last = curr;
242  }
243  NBNode* prevN = curr->getFromNode();
244  if (prevN->getIncomingEdges().size() == 1) {
245  curr = prevN->getIncomingEdges()[0];
246  if (curr->getNumLanes() != firstLaneNumber) {
247  // the number of lanes changes along the computation; we'll stop...
248  curr = 0;
249  }
250  } else {
251  // ambigous; and, in fact, what should it be? ...stop
252  curr = 0;
253  }
254  }
255  // check whether a further split is necessary
256  if (curr != 0 && !dontSplit && currLength - POSITION_EPS < rampLength && curr->getNumLanes() == firstLaneNumber && find(incremented.begin(), incremented.end(), curr) == incremented.end()) {
257  // there is enough place to build a ramp; do it
258  bool wasFirst = first == curr;
259  Position pos = curr->getGeometry().positionAtLengthPosition(curr->getGeometry().length() - (rampLength - currLength));
260  NBNode* rn = new NBNode(curr->getID() + "-AddedOffRampNode", pos);
261  if (!nc.insert(rn)) {
262  throw ProcessError("Ups - could not build on-ramp for edge '" + curr->getID() + "' (node could not be build)!");
263  }
264  std::string name = curr->getID();
265  bool ok = ec.splitAt(dc, curr, rn, curr->getID(), curr->getID() + "-AddedOffRampEdge", curr->getNumLanes(), curr->getNumLanes() + toAdd);
266  if (!ok) {
267  WRITE_ERROR("Ups - could not build on-ramp for edge '" + curr->getID() + "'!");
268  return;
269  }
270  curr = ec.retrieve(name + "-AddedOffRampEdge");
271  curr->invalidateConnections(true);
272  incremented.insert(curr);
273  last = curr;
274  moveRampRight(curr, toAdd);
275  if (wasFirst) {
276  first = curr;
277  }
278  }
279  }
280  // set connections from added ramp to ramp/highway
281  if (!first->addLane2LaneConnections(potRamp->getNumLanes(), potHighway, 0, MIN2(first->getNumLanes() - 1, potHighway->getNumLanes()), NBEdge::L2L_VALIDATED, true)) {
282  throw ProcessError("Could not set connection!");
283  }
284  if (!first->addLane2LaneConnections(0, potRamp, 0, potRamp->getNumLanes(), NBEdge::L2L_VALIDATED, false)) {
285  throw ProcessError("Could not set connection!");
286  }
287  // patch ramp geometry
288  PositionVector p = potRamp->getGeometry();
289  p.pop_front();
290  p.push_front(first->getLaneShape(0)[-1]);
291  potRamp->setGeometry(p);
292  // set connections from previous highway to added ramp
293  NBNode* prevN = last->getFromNode();
294  if (prevN->getIncomingEdges().size() == 1) {
295  NBEdge* prev = prevN->getIncomingEdges()[0];//const EdgeVector& o1 = cont->getToNode()->getOutgoingEdges();
296  if (prev->getNumLanes() < last->getNumLanes()) {
297  last->addLane2LaneConnections(last->getNumLanes() - prev->getNumLanes(), last, 0, prev->getNumLanes(), NBEdge::L2L_VALIDATED);
298  }
299  }
300 }
301 
302 
303 void
304 NBRampsComputer::moveRampRight(NBEdge* ramp, int addedLanes) {
305  if (ramp->getLaneSpreadFunction() != LANESPREAD_CENTER) {
306  return;
307  }
308  try {
309  PositionVector g = ramp->getGeometry();
310  SUMOReal factor = SUMO_const_laneWidthAndOffset * (SUMOReal)(addedLanes - 1) + SUMO_const_halfLaneAndOffset * (SUMOReal)(addedLanes % 2);
311  g.move2side(factor);
312  ramp->setGeometry(g);
313  } catch (InvalidArgument&) {
314  WRITE_WARNING("For edge '" + ramp->getID() + "': could not compute shape.");
315  }
316 }
317 
318 
319 bool
321  if (fabs((*potHighway)->getSpeed() - (*potRamp)->getSpeed()) < .1) {
322  return false;
323  }
324  if ((*potHighway)->getSpeed() < (*potRamp)->getSpeed()) {
325  std::swap(*potHighway, *potRamp);
326  }
327  return true;
328 }
329 
330 
331 bool
333  if ((*potHighway)->getNumLanes() == (*potRamp)->getNumLanes()) {
334  return false;
335  }
336  if ((*potHighway)->getNumLanes() < (*potRamp)->getNumLanes()) {
337  std::swap(*potHighway, *potRamp);
338  }
339  return true;
340 }
341 
342 
343 void
344 NBRampsComputer::getOnRampEdges(NBNode* n, NBEdge** potHighway, NBEdge** potRamp, NBEdge** other) {
345  *other = n->getOutgoingEdges()[0];
346  const std::vector<NBEdge*>& edges = n->getIncomingEdges();
347  assert(edges.size() == 2);
348  *potHighway = edges[0];
349  *potRamp = edges[1];
350  /*
351  // heuristic: highway is faster than ramp
352  if(determinedBySpeed(potHighway, potRamp)) {
353  return;
354  }
355  // heuristic: highway has more lanes than ramp
356  if(determinedByLaneNumber(potHighway, potRamp)) {
357  return;
358  }
359  */
360  // heuristic: ramp comes from right
361  const std::vector<NBEdge*>& edges2 = n->getEdges();
362  std::vector<NBEdge*>::const_iterator i = std::find(edges2.begin(), edges2.end(), *other);
363  NBContHelper::nextCW(edges2, i);
364  if ((*i) == *potHighway) {
365  std::swap(*potHighway, *potRamp);
366  }
367 }
368 
369 
370 void
371 NBRampsComputer::getOffRampEdges(NBNode* n, NBEdge** potHighway, NBEdge** potRamp, NBEdge** other) {
372  *other = n->getIncomingEdges()[0];
373  const std::vector<NBEdge*>& edges = n->getOutgoingEdges();
374  *potHighway = edges[0];
375  *potRamp = edges[1];
376  assert(edges.size() == 2);
377  /*
378  // heuristic: highway is faster than ramp
379  if(determinedBySpeed(potHighway, potRamp)) {
380  return;
381  }
382  // heuristic: highway has more lanes than ramp
383  if(determinedByLaneNumber(potHighway, potRamp)) {
384  return;
385  }
386  */
387  // heuristic: ramp goes to right
388  const std::vector<NBEdge*>& edges2 = n->getEdges();
389  std::vector<NBEdge*>::const_iterator i = std::find(edges2.begin(), edges2.end(), *other);
390  NBContHelper::nextCW(edges2, i);
391  if ((*i) == *potRamp) {
392  std::swap(*potHighway, *potRamp);
393  }
394 }
395 
396 
397 bool
399  NBEdge* potHighway, NBEdge* potRamp, NBEdge* other, SUMOReal minHighwaySpeed, SUMOReal maxRampSpeed) {
400  // do not build ramps on rail edges
401  if (isRailway(potHighway->getPermissions()) || isRailway(potRamp->getPermissions())) {
402  return false;
403  }
404  // do not build ramps on connectors
405  if (potHighway->isMacroscopicConnector() || potRamp->isMacroscopicConnector() || other->isMacroscopicConnector()) {
406  return false;
407  }
408  // check whether a lane is missing
409  if (potHighway->getNumLanes() + potRamp->getNumLanes() <= other->getNumLanes()) {
410  return false;
411  }
412  // check conditions
413  // is it really a highway?
414  SUMOReal maxSpeed = MAX3(potHighway->getSpeed(), other->getSpeed(), potRamp->getSpeed());
415  if (maxSpeed < minHighwaySpeed) {
416  return false;
417  }
418  /*
419  if (potHighway->getSpeed() < minHighwaySpeed || other->getSpeed() < minHighwaySpeed) {
420  return false;
421  }
422  */
423  // is it really a ramp?
424  if (maxRampSpeed > 0 && maxRampSpeed < potRamp->getSpeed()) {
425  return false;
426  }
427  return true;
428 }
429 
430 
431 /****************************************************************************/
432