Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file NBEdgeCont.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Sascha Krieg
18 : /// @author Michael Behrisch
19 : /// @date Tue, 20 Nov 2001
20 : ///
21 : // Storage for edges, including some functionality operating on multiple edges
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <vector>
26 : #include <string>
27 : #include <cassert>
28 : #include <algorithm>
29 : #include <cmath>
30 : #include <utils/geom/Boundary.h>
31 : #include <utils/geom/GeomHelper.h>
32 : #include <utils/geom/GeoConvHelper.h>
33 : #include <utils/geom/GeomConvHelper.h>
34 : #include <utils/common/MsgHandler.h>
35 : #include <utils/common/ToString.h>
36 : #include <utils/common/StringUtils.h>
37 : #include <utils/common/IDSupplier.h>
38 : #include <utils/common/StringUtils.h>
39 : #include <utils/common/StringTokenizer.h>
40 : #include <utils/common/UtilExceptions.h>
41 : #include <utils/iodevices/OutputDevice.h>
42 : #include <utils/options/OptionsCont.h>
43 : #include "NBNetBuilder.h"
44 : #include "NBEdgeCont.h"
45 : #include "NBNodeCont.h"
46 : #include "NBPTLineCont.h"
47 : #include "NBPTStop.h"
48 : #include "NBHelpers.h"
49 : #include "NBCont.h"
50 : #include "NBTrafficLightLogicCont.h"
51 : #include "NBDistrictCont.h"
52 : #include "NBTypeCont.h"
53 :
54 : #define JOIN_TRAM_MAX_ANGLE 10
55 : #define JOIN_TRAM_MIN_LENGTH 3
56 :
57 : //#define DEBUG_GUESS_ROUNDABOUT
58 : //#define DEBUG_JOIN_TRAM
59 : #define DEBUG_EDGE_ID ""
60 :
61 : // ===========================================================================
62 : // method definitions
63 : // ===========================================================================
64 2113 : NBEdgeCont::NBEdgeCont(NBTypeCont& tc) :
65 2113 : myTypeCont(tc),
66 2113 : myVehicleClasses2Keep(0),
67 2113 : myVehicleClasses2Remove(0),
68 2113 : myNeedGeoTransformedPruningBoundary(false) {
69 2113 : }
70 :
71 :
72 2113 : NBEdgeCont::~NBEdgeCont() {
73 2113 : clear();
74 2113 : }
75 :
76 :
77 : void
78 2113 : NBEdgeCont::applyOptions(OptionsCont& oc) {
79 : // set edges dismiss/accept options
80 2113 : myEdgesMinSpeed = oc.getFloat("keep-edges.min-speed");
81 4126 : myRemoveEdgesAfterLoading = oc.exists("keep-edges.postload") && oc.getBool("keep-edges.postload");
82 : // we possibly have to load the edges to keep/remove
83 4226 : if (oc.isSet("keep-edges.input-file")) {
84 4 : NBHelpers::loadEdgesFromFile(oc.getString("keep-edges.input-file"), myEdges2Keep);
85 : }
86 4226 : if (oc.isSet("remove-edges.input-file")) {
87 2 : NBHelpers::loadEdgesFromFile(oc.getString("remove-edges.input-file"), myEdges2Remove);
88 : }
89 4226 : if (oc.isSet("keep-edges.explicit")) {
90 22 : const std::vector<std::string> edges = oc.getStringVector("keep-edges.explicit");
91 : myEdges2Keep.insert(edges.begin(), edges.end());
92 11 : }
93 4226 : if (oc.isSet("remove-edges.explicit")) {
94 28 : const std::vector<std::string> edges = oc.getStringVector("remove-edges.explicit");
95 : myEdges2Remove.insert(edges.begin(), edges.end());
96 14 : }
97 4126 : if (oc.exists("keep-edges.by-vclass") && oc.isSet("keep-edges.by-vclass")) {
98 69 : myVehicleClasses2Keep = parseVehicleClasses(oc.getStringVector("keep-edges.by-vclass"));
99 : }
100 4126 : if (oc.exists("remove-edges.by-vclass") && oc.isSet("remove-edges.by-vclass")) {
101 39 : myVehicleClasses2Remove = parseVehicleClasses(oc.getStringVector("remove-edges.by-vclass"));
102 : }
103 4112 : if (oc.exists("keep-edges.by-type") && oc.isSet("keep-edges.by-type")) {
104 14 : const std::vector<std::string> types = oc.getStringVector("keep-edges.by-type");
105 : myTypes2Keep.insert(types.begin(), types.end());
106 7 : }
107 4112 : if (oc.exists("remove-edges.by-type") && oc.isSet("remove-edges.by-type")) {
108 18 : const std::vector<std::string> types = oc.getStringVector("remove-edges.by-type");
109 : myTypes2Remove.insert(types.begin(), types.end());
110 9 : }
111 :
112 4202 : if (oc.isSet("keep-edges.in-boundary") || oc.isSet("keep-edges.in-geo-boundary")) {
113 :
114 14 : std::string polyPlainString = oc.getValueString(oc.isSet("keep-edges.in-boundary") ?
115 18 : "keep-edges.in-boundary" : "keep-edges.in-geo-boundary");
116 : // try interpreting the boundary like shape attribute with spaces
117 14 : bool ok = true;
118 17 : PositionVector boundaryShape = GeomConvHelper::parseShapeReporting(polyPlainString, "pruning-boundary", 0, ok, false, false);
119 14 : if (ok) {
120 2 : if (boundaryShape.size() < 2) {
121 2 : throw ProcessError(TL("Invalid boundary: need at least 2 coordinates"));
122 1 : } else if (boundaryShape.size() == 2) {
123 : // prunning boundary (box)
124 0 : myPruningBoundary.push_back(boundaryShape[0]);
125 0 : myPruningBoundary.push_back(Position(boundaryShape[1].x(), boundaryShape[0].y()));
126 0 : myPruningBoundary.push_back(boundaryShape[1]);
127 0 : myPruningBoundary.push_back(Position(boundaryShape[0].x(), boundaryShape[1].y()));
128 : } else {
129 : myPruningBoundary = boundaryShape;
130 : }
131 : } else {
132 : // maybe positions are separated by ',' instead of ' '
133 28 : std::vector<std::string> polyS = oc.getStringVector(oc.isSet("keep-edges.in-boundary") ?
134 12 : "keep-edges.in-boundary" : "keep-edges.in-geo-boundary");
135 : std::vector<double> poly;
136 83 : for (std::vector<std::string>::iterator i = polyS.begin(); i != polyS.end(); ++i) {
137 72 : poly.push_back(StringUtils::toDouble((*i))); // !!! may throw something anyhow...
138 : }
139 11 : if (poly.size() < 4) {
140 0 : throw ProcessError(TL("Invalid boundary: need at least 2 coordinates"));
141 11 : } else if (poly.size() % 2 != 0) {
142 2 : throw ProcessError(TL("Invalid boundary: malformed coordinate"));
143 10 : } else if (poly.size() == 4) {
144 : // prunning boundary (box)
145 8 : myPruningBoundary.push_back(Position(poly[0], poly[1]));
146 8 : myPruningBoundary.push_back(Position(poly[2], poly[1]));
147 8 : myPruningBoundary.push_back(Position(poly[2], poly[3]));
148 8 : myPruningBoundary.push_back(Position(poly[0], poly[3]));
149 : } else {
150 19 : for (std::vector<double>::iterator j = poly.begin(); j != poly.end();) {
151 17 : double x = *j++;
152 17 : double y = *j++;
153 17 : myPruningBoundary.push_back(Position(x, y));
154 : }
155 : }
156 14 : }
157 11 : myNeedGeoTransformedPruningBoundary = oc.isSet("keep-edges.in-geo-boundary");
158 14 : }
159 2103 : }
160 :
161 :
162 : void
163 2113 : NBEdgeCont::clear() {
164 105050 : for (const auto& i : myEdges) {
165 102937 : delete i.second;
166 : }
167 : myEdges.clear();
168 11353 : for (const auto& i : myExtractedEdges) {
169 9240 : delete i.second;
170 : }
171 : myExtractedEdges.clear();
172 2133 : for (NBEdge* const e : myEdgeCemetery) {
173 20 : delete e;
174 : }
175 : myEdgeCemetery.clear();
176 2113 : }
177 :
178 :
179 :
180 : // ----- edge access methods
181 : bool
182 120319 : NBEdgeCont::insert(NBEdge* edge, bool ignorePrunning) {
183 120319 : if (myEdges.count(edge->getID()) != 0) {
184 0 : return false;
185 : }
186 120319 : if (!ignorePrunning && ignoreFilterMatch(edge)) {
187 7734 : edge->getFromNode()->removeEdge(edge);
188 7734 : edge->getToNode()->removeEdge(edge);
189 7734 : myIgnoredEdges.insert(edge->getID());
190 7734 : delete edge;
191 : } else {
192 112585 : OptionsCont& oc = OptionsCont::getOptions();
193 209751 : if (oc.exists("dismiss-vclasses") && oc.getBool("dismiss-vclasses")) {
194 200 : edge->dismissVehicleClassInformation();
195 : }
196 112585 : myEdges[edge->getID()] = edge;
197 : }
198 : return true;
199 : }
200 :
201 :
202 : bool
203 152704 : NBEdgeCont::ignoreFilterMatch(NBEdge* edge) {
204 152704 : if (!myRemoveEdgesAfterLoading) {
205 : // check whether the edge is a named edge to keep
206 152591 : if (myEdges2Keep.size() != 0) {
207 142 : if (myEdges2Keep.count(edge->getID()) == 0) {
208 : // explicit whitelisting may be combined additively with other filters
209 63 : if (myVehicleClasses2Keep == 0 && myVehicleClasses2Remove == 0
210 63 : && myTypes2Keep.size() == 0 && myTypes2Remove.size() == 0
211 104 : && myPruningBoundary.size() == 0) {
212 : return true;
213 : }
214 : } else {
215 : // explicit whitelisting overrides other filters
216 79 : return false;
217 : }
218 : }
219 : // remove edges which allow a speed below a set one (set using "keep-edges.min-speed")
220 152471 : if (edge->getSpeed() < myEdgesMinSpeed) {
221 : return true;
222 : }
223 : // check whether the edge shall be removed because it does not allow any of the wished classes
224 152465 : if (myVehicleClasses2Keep != 0 && (myVehicleClasses2Keep & edge->getPermissions()) == 0) {
225 : return true;
226 : }
227 : // check whether the edge shall be removed due to allowing unwished classes only
228 147376 : if (myVehicleClasses2Remove != 0 && (myVehicleClasses2Remove | edge->getPermissions()) == myVehicleClasses2Remove) {
229 : return true;
230 : }
231 : }
232 : // check whether the edge is a named edge to remove
233 147170 : if (myEdges2Remove.size() != 0) {
234 292 : if (myEdges2Remove.count(edge->getID()) != 0) {
235 33 : return true;
236 : }
237 : }
238 : // check whether the edge shall be removed because it does not have one of the requested types
239 147137 : if (myTypes2Keep.size() != 0) {
240 : if (myTypes2Keep.count(edge->getTypeID()) == 0) {
241 1443 : return true;
242 : }
243 : }
244 : // check whether the edge shall be removed because it has one of the forbidden types
245 145694 : if (myTypes2Remove.size() != 0) {
246 : if (myTypes2Remove.count(edge->getTypeID()) > 0) {
247 52 : return true;
248 : }
249 : }
250 : // check whether the edge is within the pruning boundary
251 145642 : if (myPruningBoundary.size() != 0) {
252 987 : if (myNeedGeoTransformedPruningBoundary) {
253 4 : if (GeoConvHelper::getProcessing().usingGeoProjection()) {
254 2 : NBNetBuilder::transformCoordinates(myPruningBoundary, false);
255 2 : } else if (GeoConvHelper::getLoaded().usingGeoProjection()) {
256 : // XXX what if input file with different projections are loaded?
257 10 : for (int i = 0; i < (int) myPruningBoundary.size(); i++) {
258 8 : GeoConvHelper::getLoaded().x2cartesian_const(myPruningBoundary[i]);
259 : }
260 : } else {
261 0 : WRITE_ERROR(TL("Cannot prune edges using a geo-boundary because no projection has been loaded"));
262 : }
263 4 : myNeedGeoTransformedPruningBoundary = false;
264 : }
265 987 : if (!(edge->getGeometry().getBoxBoundary().grow(POSITION_EPS).overlapsWith(myPruningBoundary))) {
266 : return true;
267 691 : } else if (!(edge->getGeometry().partialWithin(myPruningBoundary, 2 * POSITION_EPS) || edge->getGeometry().intersects(myPruningBoundary))) {
268 : // a more detailed check is necessary because the bounding box may be much bigger than the edge
269 : // @note: overlapsWith implicitly closes the edge shape but this is not wanted here
270 : return true;
271 : }
272 : }
273 145344 : if (myTypeCont.knows(edge->getTypeID()) && myTypeCont.getEdgeTypeShallBeDiscarded(edge->getTypeID())) {
274 : return true;
275 : }
276 : return false;
277 : }
278 :
279 :
280 : NBEdge*
281 333181 : NBEdgeCont::retrieve(const std::string& id, bool retrieveExtracted) const {
282 : EdgeCont::const_iterator i = myEdges.find(id);
283 333181 : if (i == myEdges.end()) {
284 90563 : if (retrieveExtracted) {
285 : i = myExtractedEdges.find(id);
286 18621 : if (i == myExtractedEdges.end()) {
287 : return nullptr;
288 : }
289 : } else {
290 : return nullptr;
291 : }
292 : }
293 242656 : return (*i).second;
294 : }
295 :
296 : // FIXME: This can't work
297 : /*
298 : NBEdge*
299 : NBEdgeCont::retrievePossiblySplit(const std::string& id, bool downstream) const {
300 : NBEdge* edge = retrieve(id);
301 : if (edge == 0) {
302 : return 0;
303 : }
304 : const EdgeVector* candidates = downstream ? &edge->getToNode()->getOutgoingEdges() : &edge->getFromNode()->getIncomingEdges();
305 : while (candidates->size() == 1) {
306 : const std::string& nextID = candidates->front()->getID();
307 : if (nextID.find(id) != 0 || nextID.size() <= id.size() + 1 || (nextID[id.size()] != '.' && nextID[id.size()] != '-')) {
308 : break;
309 : }
310 : edge = candidates->front();
311 : candidates = downstream ? &edge->getToNode()->getOutgoingEdges() : &edge->getFromNode()->getIncomingEdges();
312 : }
313 : return edge;
314 : }*/
315 :
316 : NBEdge*
317 104 : NBEdgeCont::retrievePossiblySplit(const std::string& id, bool downstream) const {
318 104 : NBEdge* edge = retrieve(id);
319 104 : if (edge != nullptr) {
320 : return edge;
321 : }
322 : // NOTE: (TODO) for multiply split edges (e.g. 15[0][0]) one could try recursion
323 0 : if ((retrieve(id + "[0]") != nullptr) && (retrieve(id + "[1]") != nullptr)) {
324 : // Edge was split during the netbuilding process
325 0 : if (downstream) {
326 0 : return retrieve(id + "[1]");
327 : } else {
328 0 : return retrieve(id + "[0]");
329 : }
330 : }
331 : return edge;
332 : }
333 :
334 :
335 : NBEdge*
336 2402 : NBEdgeCont::retrievePossiblySplit(const std::string& id, const std::string& hint, bool incoming) const {
337 : // try to retrieve using the given name (iterative)
338 2402 : NBEdge* edge = retrieve(id);
339 2402 : if (edge != nullptr) {
340 : return edge;
341 : }
342 : // now, we did not find it; we have to look over all possibilities
343 : EdgeVector hints;
344 : // check whether at least the hint was not splitted
345 710 : NBEdge* hintedge = retrieve(hint);
346 710 : if (hintedge == nullptr) {
347 132 : hints = getGeneratedFrom(hint);
348 : } else {
349 578 : hints.push_back(hintedge);
350 : }
351 710 : EdgeVector candidates = getGeneratedFrom(id);
352 965 : for (const NBEdge* const currHint : hints) {
353 3555 : for (NBEdge* const poss_searched : candidates) {
354 3300 : const NBNode* const node = incoming ? poss_searched->myTo : poss_searched->myFrom;
355 3300 : const EdgeVector& cont = incoming ? node->getOutgoingEdges() : node->getIncomingEdges();
356 3300 : if (find(cont.begin(), cont.end(), currHint) != cont.end()) {
357 : return poss_searched;
358 : }
359 : }
360 : }
361 33 : return nullptr;
362 710 : }
363 :
364 :
365 : NBEdge*
366 261 : NBEdgeCont::retrievePossiblySplit(const std::string& id, double pos) const {
367 : // check whether the edge was not split, yet
368 261 : NBEdge* edge = retrieve(id);
369 261 : if (edge != nullptr) {
370 : return edge;
371 : }
372 : int maxLength = 0;
373 111 : std::string tid = id + "[";
374 21423 : for (EdgeCont::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
375 21312 : if ((*i).first.find(tid) == 0) {
376 660 : maxLength = MAX2(maxLength, (int)(*i).first.length());
377 : }
378 : }
379 : // find the part of the edge which matches the position
380 : double seen = 0;
381 : std::vector<std::string> names;
382 111 : names.push_back(id + "[1]");
383 111 : names.push_back(id + "[0]");
384 576 : while (names.size() > 0) {
385 : // retrieve the first subelement (to follow)
386 : std::string cid = names.back();
387 : names.pop_back();
388 558 : edge = retrieve(cid);
389 : // The edge was splitted; check its subparts within the
390 : // next step
391 558 : if (edge == nullptr) {
392 231 : if ((int)cid.length() + 3 < maxLength) {
393 213 : names.push_back(cid + "[1]");
394 426 : names.push_back(cid + "[0]");
395 : }
396 : }
397 : // an edge with the name was found,
398 : // check whether the position lies within it
399 : else {
400 327 : seen += edge->getLength();
401 327 : if (seen >= pos) {
402 : return edge;
403 : }
404 : }
405 : }
406 : return nullptr;
407 111 : }
408 :
409 :
410 : void
411 363 : NBEdgeCont::erase(NBDistrictCont& dc, NBEdge* edge) {
412 363 : extract(dc, edge);
413 363 : delete edge;
414 363 : }
415 :
416 :
417 : void
418 9650 : NBEdgeCont::extract(NBDistrictCont& dc, NBEdge* edge, bool remember) {
419 9650 : if (remember) {
420 9262 : const auto& prevExtracted = myExtractedEdges.find(edge->getID());
421 9262 : if (prevExtracted != myExtractedEdges.end()) {
422 22 : if (edge != prevExtracted->second) {
423 20 : myEdgeCemetery.insert(prevExtracted->second);
424 20 : prevExtracted->second = edge;
425 : }
426 : } else {
427 9240 : myExtractedEdges[edge->getID()] = edge;
428 : }
429 : }
430 9650 : myEdges.erase(edge->getID());
431 9650 : edge->myFrom->removeEdge(edge);
432 9650 : edge->myTo->removeEdge(edge);
433 9650 : dc.removeFromSinksAndSources(edge);
434 9650 : }
435 :
436 :
437 : void
438 193 : NBEdgeCont::rename(NBEdge* edge, const std::string& newID) {
439 : if (myEdges.count(newID) != 0) {
440 0 : throw ProcessError(TLF("Attempt to rename edge using existing id '%'", newID));
441 : }
442 193 : myEdges.erase(edge->getID());
443 193 : edge->setID(newID);
444 193 : myEdges[newID] = edge;
445 : // update oppositeID
446 193 : if (edge->getLanes().back().oppositeID != "") {
447 0 : NBEdge* oppo = retrieve(SUMOXMLDefinitions::getEdgeIDFromLane(edge->getLanes().back().oppositeID));
448 0 : if (oppo != nullptr) {
449 0 : oppo->getLaneStruct(oppo->getNumLanes() - 1).oppositeID = edge->getLaneID(edge->getNumLanes() - 1);
450 : }
451 : }
452 193 : }
453 :
454 :
455 : // ----- explicit edge manipulation methods
456 :
457 : void
458 19029 : NBEdgeCont::processSplits(NBEdge* e, std::vector<Split> splits,
459 : NBNodeCont& nc, NBDistrictCont& dc, NBTrafficLightLogicCont& tlc) {
460 19029 : if (splits.empty()) {
461 18796 : return;
462 : }
463 235 : const std::string origID = e->getID();
464 235 : sort(splits.begin(), splits.end(), split_sorter());
465 : int maxNumLanes = e->getNumLanes();
466 : // compute the node positions and sort the lanes
467 554 : for (Split& split : splits) {
468 319 : sort(split.lanes.begin(), split.lanes.end());
469 319 : maxNumLanes = MAX2(maxNumLanes, (int)split.lanes.size());
470 : }
471 : // split the edge
472 : std::vector<int> currLanes;
473 670 : for (int l = 0; l < e->getNumLanes(); ++l) {
474 435 : currLanes.push_back(l);
475 : }
476 235 : if (e->getNumLanes() != (int)splits.back().lanes.size()) {
477 : // invalidate traffic light definitions loaded from a SUMO network
478 178 : e->getToNode()->invalidateTLS(tlc, true, true);
479 : // if the number of lanes changes the connections should be
480 : // recomputed
481 178 : e->invalidateConnections(true);
482 : }
483 :
484 235 : std::string firstID = "";
485 : double seen = 0;
486 552 : for (const Split& exp : splits) {
487 : assert(exp.lanes.size() != 0);
488 596 : if (exp.pos > 0 && e->getLoadedLength() + seen > exp.pos && exp.pos > seen) {
489 276 : nc.insert(exp.node);
490 276 : nc.markAsSplit(exp.node);
491 : // split the edge
492 276 : const std::string idBefore = exp.idBefore == "" ? e->getID() : exp.idBefore;
493 276 : const std::string idAfter = exp.idAfter == "" ? exp.nameID : exp.idAfter;
494 276 : if (firstID == "") {
495 : firstID = idBefore;
496 : }
497 276 : const bool ok = splitAt(dc, e, exp.pos - seen, exp.node,
498 276 : idBefore, idAfter, e->getNumLanes(), (int) exp.lanes.size(), exp.speed);
499 276 : if (!ok) {
500 6 : WRITE_WARNINGF(TL("Error on parsing a split (edge '%')."), origID);
501 : return;
502 : }
503 274 : seen = exp.pos;
504 274 : std::vector<int> newLanes = exp.lanes;
505 274 : NBEdge* pe = retrieve(idBefore);
506 274 : NBEdge* ne = retrieve(idAfter);
507 : // reconnect lanes
508 274 : pe->invalidateConnections(true);
509 : // new on right
510 274 : int rightMostP = currLanes[0];
511 274 : int rightMostN = newLanes[0];
512 313 : for (int l = 0; l < (int) rightMostP - (int) rightMostN; ++l) {
513 78 : pe->addLane2LaneConnection(0, ne, l, NBEdge::Lane2LaneInfoType::VALIDATED, true);
514 : }
515 : // new on left
516 274 : int leftMostP = currLanes.back();
517 274 : int leftMostN = newLanes.back();
518 447 : for (int l = 0; l < (int) leftMostN - (int) leftMostP; ++l) {
519 346 : pe->addLane2LaneConnection(pe->getNumLanes() - 1, ne, leftMostN - l - rightMostN, NBEdge::Lane2LaneInfoType::VALIDATED, true);
520 : }
521 : // all other connected
522 977 : for (int l = 0; l < maxNumLanes; ++l) {
523 703 : if (find(currLanes.begin(), currLanes.end(), l) == currLanes.end()) {
524 226 : continue;
525 : }
526 477 : if (find(newLanes.begin(), newLanes.end(), l) == newLanes.end()) {
527 56 : continue;
528 : }
529 842 : pe->addLane2LaneConnection(l - rightMostP, ne, l - rightMostN, NBEdge::Lane2LaneInfoType::VALIDATED, true);
530 : }
531 : // if there are edges at this node which are not connected
532 : // we can assume that this split was attached to an
533 : // existing node. Reset all connections to let the default
534 : // algorithm recompute them
535 274 : if (exp.node->getIncomingEdges().size() > 1 || exp.node->getOutgoingEdges().size() > 1 || exp.node->getType() == SumoXMLNodeType::ZIPPER) {
536 30 : for (NBEdge* in : exp.node->getIncomingEdges()) {
537 20 : in->invalidateConnections(true);
538 : }
539 : }
540 : // move to next
541 : e = ne;
542 274 : currLanes = newLanes;
543 317 : } else if (exp.pos == 0) {
544 42 : const int laneCountDiff = e->getNumLanes() - (int)exp.lanes.size();
545 42 : if (laneCountDiff < 0) {
546 1 : e->incLaneNo(-laneCountDiff);
547 : } else {
548 41 : e->decLaneNo(laneCountDiff);
549 : }
550 42 : currLanes = exp.lanes;
551 : // invalidate traffic light definition loaded from a SUMO network
552 : // XXX it would be preferable to reconstruct the phase definitions heuristically
553 42 : e->getFromNode()->invalidateTLS(tlc, true, true);
554 42 : if (exp.speed != -1.) {
555 42 : e->setSpeed(-1, exp.speed);
556 : }
557 : } else {
558 3 : WRITE_WARNINGF(TL("Split at '%' lies beyond the edge's length (edge '%')."), toString(exp.pos), origID);
559 : }
560 : }
561 : // patch lane offsets
562 233 : e = retrieve(firstID);
563 233 : if (e != nullptr) {
564 231 : if (splits.front().pos != 0) {
565 : // add a dummy split at the beginning to ensure correct offset
566 : Split start;
567 190 : start.pos = 0;
568 532 : for (int lane = 0; lane < (int)e->getNumLanes(); ++lane) {
569 342 : start.lanes.push_back(lane);
570 : }
571 190 : start.offset = splits.front().offset;
572 190 : start.offsetFactor = splits.front().offsetFactor;
573 190 : splits.insert(splits.begin(), start);
574 190 : }
575 735 : for (const Split& split : splits) {
576 504 : int maxLeft = split.lanes.back();
577 504 : double offset = split.offset;
578 504 : if (maxLeft < maxNumLanes) {
579 504 : if (e->getLaneSpreadFunction() == LaneSpreadFunction::RIGHT) {
580 419 : offset += split.offsetFactor * SUMO_const_laneWidth * (maxNumLanes - 1 - maxLeft);
581 : } else {
582 85 : offset += split.offsetFactor * SUMO_const_halfLaneWidth * (maxNumLanes - 1 - maxLeft);
583 : }
584 : }
585 504 : int maxRight = split.lanes.front();
586 504 : if (maxRight > 0 && e->getLaneSpreadFunction() == LaneSpreadFunction::CENTER) {
587 19 : offset -= split.offsetFactor * SUMO_const_halfLaneWidth * maxRight;
588 : }
589 : //std::cout << " processSplits " << origID << " splitOffset=" << (*i).offset << " offset=" << offset << "\n";
590 504 : if (offset != 0) {
591 : PositionVector g = e->getGeometry();
592 269 : g.move2side(offset);
593 269 : e->setGeometry(g);
594 269 : }
595 504 : if (e->getToNode()->getOutgoingEdges().size() != 0) {
596 426 : e = e->getToNode()->getOutgoingEdges()[0];
597 : }
598 : }
599 : }
600 235 : }
601 :
602 :
603 : bool
604 188 : NBEdgeCont::splitAt(NBDistrictCont& dc, NBEdge* edge, NBNode* node) {
605 376 : return splitAt(dc, edge, node, edge->getID() + "[0]", edge->getID() + "[1]",
606 188 : (int) edge->myLanes.size(), (int) edge->myLanes.size());
607 : }
608 :
609 :
610 : bool
611 468 : NBEdgeCont::splitAt(NBDistrictCont& dc, NBEdge* edge, NBNode* node,
612 : const std::string& firstEdgeName,
613 : const std::string& secondEdgeName,
614 : int noLanesFirstEdge, int noLanesSecondEdge,
615 : const double speed, const double friction,
616 : const int changedLeft) {
617 : double pos;
618 468 : pos = edge->getGeometry().nearest_offset_to_point2D(node->getPosition());
619 468 : if (pos <= 0) {
620 3 : pos = GeomHelper::nearest_offset_on_line_to_point2D(
621 3 : edge->myFrom->getPosition(), edge->myTo->getPosition(),
622 : node->getPosition());
623 : }
624 468 : if (pos <= 0 || pos + POSITION_EPS > edge->getGeometry().length()) {
625 3 : return false;
626 : }
627 465 : return splitAt(dc, edge, pos, node, firstEdgeName, secondEdgeName,
628 465 : noLanesFirstEdge, noLanesSecondEdge, speed, friction, changedLeft);
629 : }
630 :
631 :
632 : bool
633 778 : NBEdgeCont::splitAt(NBDistrictCont& dc,
634 : NBEdge* edge, double pos, NBNode* node,
635 : const std::string& firstEdgeName,
636 : const std::string& secondEdgeName,
637 : int noLanesFirstEdge, int noLanesSecondEdge,
638 : const double speed, const double friction,
639 : const int changedLeft) {
640 778 : if (firstEdgeName != edge->getID() && myEdges.count(firstEdgeName) != 0) {
641 0 : WRITE_ERRORF(TL("Could not insert edge '%' before split of edge '%'."), firstEdgeName, edge->getID());
642 0 : return false;
643 : }
644 778 : if (secondEdgeName == firstEdgeName || (secondEdgeName != edge->getID() && myEdges.count(secondEdgeName) != 0)) {
645 6 : WRITE_ERRORF(TL("Could not insert edge '%' after split of edge '%'."), secondEdgeName, edge->getID());
646 2 : return false;
647 : }
648 : // there must be at least some overlap between first and second edge
649 : assert(changedLeft > -((int)noLanesFirstEdge));
650 : assert(changedLeft < (int)noLanesSecondEdge);
651 :
652 : // build the new edges' geometries
653 : double geomPos = pos;
654 776 : if (edge->hasLoadedLength()) {
655 4 : geomPos *= edge->getGeometry().length() / edge->getLoadedLength();
656 : }
657 776 : std::pair<PositionVector, PositionVector> geoms = edge->getGeometry().splitAt(geomPos);
658 : // reduce inaccuracies and preserve bidi
659 1552 : if (geoms.first[-1].almostSame(node->getPosition()) || edge->isBidi()) {
660 622 : geoms.first[-1] = node->getPosition();
661 622 : geoms.second[0] = node->getPosition();
662 : }
663 : // build and insert the edges
664 776 : NBEdge* one = new NBEdge(firstEdgeName, edge->myFrom, node, edge, geoms.first, noLanesFirstEdge);
665 776 : NBEdge* two = new NBEdge(secondEdgeName, node, edge->myTo, edge, geoms.second, noLanesSecondEdge);
666 1552 : if (OptionsCont::getOptions().getBool("output.original-names")) {
667 54 : const std::string origID = edge->getLaneStruct(0).getParameter(SUMO_PARAM_ORIGID, edge->getID());
668 18 : if (firstEdgeName != origID) {
669 34 : one->setOrigID(origID, false);
670 : }
671 18 : if (secondEdgeName != origID) {
672 20 : two->setOrigID(origID, false);
673 : }
674 : }
675 776 : two->copyConnectionsFrom(edge);
676 776 : if (speed != -1.) {
677 274 : two->setSpeed(-1, speed);
678 : }
679 776 : if (friction != -1.) {
680 776 : two->setFriction(-1, friction);
681 : }
682 776 : if (edge->getDistance() != 0) {
683 : one->setDistance(edge->getDistance());
684 3 : two->setDistance(one->getDistance() + pos);
685 : }
686 776 : if (edge->hasLoadedLength()) {
687 4 : one->setLoadedLength(pos);
688 4 : two->setLoadedLength(edge->getLoadedLength() - pos);
689 : }
690 : // replace information about this edge within the nodes
691 776 : edge->myFrom->replaceOutgoing(edge, one, 0);
692 776 : edge->myTo->replaceIncoming(edge, two, 0);
693 : // patch tls
694 800 : for (NBTrafficLightDefinition* const tld : edge->myFrom->getControllingTLS()) {
695 24 : tld->replaceRemoved(edge, -1, one, -1, false);
696 : }
697 826 : for (NBTrafficLightDefinition* const tld : edge->myTo->getControllingTLS()) {
698 50 : tld->replaceRemoved(edge, -1, two, -1, true);
699 : }
700 : // the edge is now occuring twice in both nodes...
701 : // clean up
702 776 : edge->myFrom->removeDoubleEdges();
703 776 : edge->myTo->removeDoubleEdges();
704 : // add connections from the first to the second edge
705 : // there will be as many connections as there are lanes on the second edge
706 : // by default lanes will be added / discontinued on the right side
707 : // (appropriate for highway on-/off-ramps)
708 776 : const int offset = (int)one->getNumLanes() - (int)two->getNumLanes() + changedLeft;
709 2231 : for (int i2 = 0; i2 < (int)two->getNumLanes(); i2++) {
710 1455 : const int i1 = MIN2(MAX2((int)0, i2 + offset), (int)one->getNumLanes());
711 2910 : if (!one->addLane2LaneConnection(i1, two, i2, NBEdge::Lane2LaneInfoType::COMPUTED)) {
712 0 : throw ProcessError(TL("Could not set connection!"));
713 : }
714 : }
715 776 : if (myRemoveEdgesAfterLoading) {
716 0 : if (myEdges2Keep.count(edge->getID()) != 0) {
717 0 : myEdges2Keep.insert(one->getID());
718 0 : myEdges2Keep.insert(two->getID());
719 : }
720 0 : if (myEdges2Remove.count(edge->getID()) != 0) {
721 0 : myEdges2Remove.insert(one->getID());
722 0 : myEdges2Remove.insert(two->getID());
723 : }
724 : }
725 : // erase the splitted edge
726 776 : patchRoundabouts(edge, one, two, myRoundabouts);
727 776 : patchRoundabouts(edge, one, two, myGuessedRoundabouts);
728 776 : const std::string oldID = edge->getID();
729 776 : extract(dc, edge, true);
730 776 : insert(one, true); // duplicate id check happened earlier
731 776 : insert(two, true); // duplicate id check happened earlier
732 776 : myEdgesSplit[edge] = {one, two};
733 : myWasSplit.insert(one);
734 : myWasSplit.insert(two);
735 : return true;
736 : }
737 :
738 :
739 : void
740 1552 : NBEdgeCont::patchRoundabouts(NBEdge* orig, NBEdge* part1, NBEdge* part2, std::set<EdgeSet>& roundabouts) {
741 : std::set<EdgeSet> addLater;
742 1555 : for (std::set<EdgeSet>::iterator it = roundabouts.begin(); it != roundabouts.end(); ++it) {
743 : EdgeSet roundaboutSet = *it;
744 : if (roundaboutSet.count(orig) > 0) {
745 : roundaboutSet.erase(orig);
746 : roundaboutSet.insert(part1);
747 : roundaboutSet.insert(part2);
748 : }
749 : addLater.insert(roundaboutSet);
750 : }
751 : roundabouts.clear();
752 1552 : roundabouts.insert(addLater.begin(), addLater.end());
753 1552 : }
754 :
755 :
756 : // ----- container access methods
757 : std::vector<std::string>
758 89 : NBEdgeCont::getAllNames() const {
759 : std::vector<std::string> ret;
760 27702 : for (EdgeCont::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
761 27613 : ret.push_back((*i).first);
762 : }
763 89 : return ret;
764 0 : }
765 :
766 :
767 : NBEdge*
768 3 : NBEdgeCont::getSplitBase(const std::string& edgeID) const {
769 : NBEdge* longest = nullptr;
770 7 : for (auto item : myEdgesSplit) {
771 4 : if (item.first->getID() == edgeID) {
772 3 : if (longest == nullptr || longest->getLoadedLength() < item.first->getLoadedLength()) {
773 : longest = const_cast<NBEdge*>(item.first);
774 : }
775 : }
776 : }
777 3 : return longest;
778 : }
779 :
780 : // ----- Adapting the input
781 : int
782 2 : NBEdgeCont::removeUnwishedEdges(NBDistrictCont& dc) {
783 : EdgeVector toRemove;
784 32 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
785 30 : NBEdge* edge = (*i).second;
786 30 : if (!myEdges2Keep.count(edge->getID())) {
787 13 : edge->getFromNode()->removeEdge(edge);
788 13 : edge->getToNode()->removeEdge(edge);
789 13 : toRemove.push_back(edge);
790 : }
791 : }
792 15 : for (EdgeVector::iterator j = toRemove.begin(); j != toRemove.end(); ++j) {
793 13 : erase(dc, *j);
794 : }
795 2 : return (int)toRemove.size();
796 2 : }
797 :
798 :
799 : void
800 4 : NBEdgeCont::splitGeometry(NBDistrictCont& dc, NBNodeCont& nc) {
801 : // make a copy of myEdges because splitting will modify it
802 : EdgeCont edges = myEdges;
803 208 : for (auto& item : edges) {
804 204 : NBEdge* edge = item.second;
805 204 : if (edge->getGeometry().size() < 3) {
806 108 : continue;
807 : }
808 : PositionVector geom = edge->getGeometry();
809 96 : const std::string id = edge->getID();
810 : double offset = 0;
811 361 : for (int i = 1; i < (int)geom.size() - 1; i++) {
812 265 : offset += geom[i - 1].distanceTo(geom[i]);
813 530 : std::string nodeID = id + "." + toString((int)offset);
814 265 : if (!nc.insert(nodeID, geom[i])) {
815 126 : WRITE_WARNING("Could not split geometry of edge '" + id + "' at index " + toString(i));
816 : continue;
817 : }
818 223 : NBNode* node = nc.retrieve(nodeID);
819 223 : splitAt(dc, edge, node, edge->getID(), nodeID, edge->getNumLanes(), edge->getNumLanes());
820 223 : edge = retrieve(nodeID);
821 : }
822 96 : }
823 4 : }
824 :
825 :
826 : void
827 11 : NBEdgeCont::reduceGeometries(const double minDist) {
828 473 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
829 462 : (*i).second->reduceGeometry(minDist);
830 : }
831 11 : }
832 :
833 :
834 : void
835 1760 : NBEdgeCont::checkGeometries(const double maxAngle, bool fixAngle, const double minRadius, bool fix, bool fixRailways, bool silent) {
836 1760 : if (maxAngle > 0 || minRadius > 0) {
837 97434 : for (auto& item : myEdges) {
838 95674 : if ((item.second->getPermissions() & (SVC_PUBLIC_CLASSES | SVC_PASSENGER)) == 0) {
839 25545 : continue;
840 : }
841 140258 : item.second->checkGeometry(maxAngle, fixAngle, minRadius, fix || (fixRailways && isRailway(item.second->getPermissions())), silent);
842 : }
843 : }
844 1760 : }
845 :
846 :
847 : // ----- processing methods
848 : void
849 1813 : NBEdgeCont::clearControllingTLInformation() const {
850 104317 : for (EdgeCont::const_iterator i = myEdges.begin(); i != myEdges.end(); i++) {
851 102504 : (*i).second->clearControllingTLInformation();
852 : }
853 1813 : }
854 :
855 :
856 : void
857 1813 : NBEdgeCont::sortOutgoingLanesConnections() {
858 104317 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); i++) {
859 102504 : (*i).second->sortOutgoingConnectionsByAngle();
860 : }
861 1813 : }
862 :
863 :
864 : void
865 1813 : NBEdgeCont::computeEdge2Edges(bool noLeftMovers) {
866 104317 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); i++) {
867 102504 : (*i).second->computeEdge2Edges(noLeftMovers);
868 : }
869 1813 : }
870 :
871 :
872 : void
873 1813 : NBEdgeCont::computeLanes2Edges() {
874 104317 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); i++) {
875 102504 : (*i).second->computeLanes2Edges();
876 : }
877 1813 : }
878 :
879 :
880 : void
881 1813 : NBEdgeCont::recheckLanes() {
882 3626 : const bool fixOppositeLengths = OptionsCont::getOptions().getBool("opposites.guess.fix-lengths");
883 104317 : for (const auto& edgeIt : myEdges) {
884 102504 : NBEdge* const edge = edgeIt.second;
885 102504 : edge->recheckLanes();
886 102504 : edge->recheckOpposite(*this, fixOppositeLengths);
887 : }
888 1813 : }
889 :
890 :
891 : void
892 1070 : NBEdgeCont::appendTurnarounds(bool noTLSControlled, bool noFringe, bool onlyDeadends, bool onlyTurnlane, bool noGeometryLike) {
893 67008 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); i++) {
894 65938 : (*i).second->appendTurnaround(noTLSControlled, noFringe, onlyDeadends, onlyTurnlane, noGeometryLike, true);
895 : }
896 1070 : }
897 :
898 :
899 : void
900 743 : NBEdgeCont::appendTurnarounds(const std::set<std::string>& ids, bool noTLSControlled) {
901 743 : for (std::set<std::string>::const_iterator it = ids.begin(); it != ids.end(); it++) {
902 0 : myEdges[*it]->appendTurnaround(noTLSControlled, false, false, false, false, false);
903 : }
904 743 : }
905 :
906 :
907 : void
908 25 : NBEdgeCont::appendRailwayTurnarounds(const NBPTStopCont& sc) {
909 : std::set<std::string> stopEdgeIDs;
910 374 : for (auto& stopItem : sc.getStops()) {
911 349 : stopEdgeIDs.insert(stopItem.second->getEdgeId());
912 : }
913 7164 : for (auto& item : myEdges) {
914 7139 : NBEdge* edge = item.second;
915 7139 : if (edge->isBidiRail()
916 7139 : && (stopEdgeIDs.count(item.first) > 0 ||
917 2173 : stopEdgeIDs.count(edge->getTurnDestination(true)->getID()) > 0)) {
918 294 : NBEdge* to = edge->getTurnDestination(true);
919 : assert(to != 0);
920 588 : edge->setConnection(edge->getNumLanes() - 1,
921 : to, to->getNumLanes() - 1, NBEdge::Lane2LaneInfoType::VALIDATED, false, false,
922 : KEEPCLEAR_UNSPECIFIED,
923 : NBEdge::UNSPECIFIED_CONTPOS, NBEdge::UNSPECIFIED_VISIBILITY_DISTANCE,
924 : SUMO_const_haltingSpeed);
925 : }
926 : }
927 25 : }
928 :
929 : void
930 1971 : NBEdgeCont::computeEdgeShapes(double smoothElevationThreshold) {
931 137677 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); i++) {
932 135706 : (*i).second->computeEdgeShape(smoothElevationThreshold);
933 : }
934 : // equalize length of opposite edges
935 137677 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); i++) {
936 135706 : NBEdge* edge = i->second;
937 135706 : const std::string& oppositeID = edge->getLanes().back().oppositeID;
938 135706 : if (oppositeID != "" && oppositeID != "-") {
939 94 : NBEdge* oppEdge = retrieve(oppositeID.substr(0, oppositeID.rfind("_")));
940 187 : if (oppEdge == nullptr || oppEdge->getLaneID(oppEdge->getNumLanes() - 1) != oppositeID) {
941 1 : continue;
942 : }
943 93 : if (fabs(oppEdge->getLength() - edge->getLength()) > NUMERICAL_EPS) {
944 5 : double avgLength = (oppEdge->getLength() + edge->getLength()) / 2;
945 5 : edge->setAverageLengthWithOpposite(avgLength);
946 5 : oppEdge->setAverageLengthWithOpposite(avgLength);
947 : }
948 : }
949 : }
950 1971 : }
951 :
952 :
953 : void
954 1971 : NBEdgeCont::computeLaneShapes() {
955 137677 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
956 135706 : (*i).second->computeLaneShapes();
957 : }
958 1971 : }
959 :
960 :
961 : void
962 12 : NBEdgeCont::joinSameNodeConnectingEdges(NBDistrictCont& dc,
963 : NBTrafficLightLogicCont& tlc,
964 : EdgeVector edges) {
965 : // !!! Attention!
966 : // No merging of the geometry to come is being done
967 : // The connections are moved from one edge to another within
968 : // the replacement where the edge is a node's incoming edge.
969 :
970 : // count the number of lanes, the speed and the id
971 : int nolanes = 0;
972 : double speed = 0;
973 : int priority = -1;
974 : bool joinEdges = true;
975 : std::string id;
976 12 : sort(edges.begin(), edges.end(), NBContHelper::same_connection_edge_sorter());
977 : // retrieve the connected nodes
978 12 : NBEdge* tpledge = *(edges.begin());
979 : NBNode* from = tpledge->getFromNode();
980 : NBNode* to = tpledge->getToNode();
981 : EdgeVector::const_iterator i;
982 : int myPriority = (*edges.begin())->getPriority();
983 36 : for (i = edges.begin(); i != edges.end(); i++) {
984 : // some assertions
985 : assert((*i)->getFromNode() == from);
986 : assert((*i)->getToNode() == to);
987 : // ad the number of lanes the current edge has
988 24 : nolanes += (*i)->getNumLanes();
989 : // build the id
990 24 : if (i != edges.begin()) {
991 : id += "+";
992 : }
993 24 : id += (*i)->getID();
994 : // compute the speed
995 24 : speed += (*i)->getSpeed();
996 : // build the priority
997 : // merged edges should have the same inherited priority
998 24 : if (myPriority == (*i)->getPriority()) {
999 : priority = myPriority;
1000 : } else {
1001 : priority = -1;
1002 : joinEdges = false;
1003 : }
1004 : }
1005 12 : if (joinEdges) {
1006 11 : speed /= (double)edges.size();
1007 : // build the new edge
1008 : NBEdge* newEdge = new NBEdge(id, from, to, "", speed, NBEdge::UNSPECIFIED_FRICTION, nolanes, priority,
1009 : NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET,
1010 22 : tpledge->myLaneSpreadFunction, tpledge->getStreetName());
1011 : // copy lane attributes
1012 : int laneIndex = 0;
1013 33 : for (i = edges.begin(); i != edges.end(); ++i) {
1014 22 : const std::vector<NBEdge::Lane>& lanes = (*i)->getLanes();
1015 49 : for (int j = 0; j < (int)lanes.size(); ++j) {
1016 27 : newEdge->setPermissions(lanes[j].permissions, laneIndex);
1017 27 : newEdge->setLaneWidth(laneIndex, lanes[j].width);
1018 27 : newEdge->setEndOffset(laneIndex, lanes[j].endOffset);
1019 27 : laneIndex++;
1020 : }
1021 : }
1022 11 : insert(newEdge, true);
1023 : // replace old edge by current within the nodes
1024 : // and delete the old
1025 11 : from->replaceOutgoing(edges, newEdge);
1026 11 : to->replaceIncoming(edges, newEdge);
1027 : // patch connections
1028 : // add edge2edge-information
1029 33 : for (i = edges.begin(); i != edges.end(); i++) {
1030 22 : EdgeVector ev = (*i)->getConnectedEdges();
1031 59 : for (EdgeVector::iterator j = ev.begin(); j != ev.end(); j++) {
1032 37 : newEdge->addEdge2EdgeConnection(*j);
1033 : }
1034 22 : }
1035 : // copy outgoing connections to the new edge
1036 : int currLane = 0;
1037 33 : for (i = edges.begin(); i != edges.end(); i++) {
1038 22 : newEdge->moveOutgoingConnectionsFrom(*i, currLane);
1039 22 : currLane += (*i)->getNumLanes();
1040 : }
1041 : // patch tl-information
1042 : currLane = 0;
1043 33 : for (i = edges.begin(); i != edges.end(); i++) {
1044 22 : int noLanes = (*i)->getNumLanes();
1045 49 : for (int j = 0; j < noLanes; j++, currLane++) {
1046 : // replace in traffic lights
1047 27 : tlc.replaceRemoved(*i, j, newEdge, currLane, true);
1048 27 : tlc.replaceRemoved(*i, j, newEdge, currLane, false);
1049 : }
1050 : }
1051 : // delete joined edges
1052 33 : for (i = edges.begin(); i != edges.end(); i++) {
1053 22 : extract(dc, *i, true);
1054 : }
1055 : }
1056 12 : }
1057 :
1058 :
1059 : void
1060 16 : NBEdgeCont::guessOpposites() {
1061 : //@todo magic values
1062 32 : const bool fixOppositeLengths = OptionsCont::getOptions().getBool("opposites.guess.fix-lengths");
1063 : // ensure consistency of loaded values before starting to guess
1064 63 : for (const auto& edgeIt : myEdges) {
1065 47 : NBEdge* const edge = edgeIt.second;
1066 47 : edge->recheckOpposite(*this, fixOppositeLengths);
1067 : }
1068 63 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
1069 47 : NBEdge* edge = i->second;
1070 47 : edge->guessOpposite();
1071 : }
1072 16 : }
1073 :
1074 :
1075 : void
1076 49 : NBEdgeCont::recheckLaneSpread() {
1077 704 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
1078 655 : NBEdge* opposite = getOppositeByID(i->first);
1079 655 : if (opposite != nullptr) {
1080 324 : i->second->setLaneSpreadFunction(LaneSpreadFunction::RIGHT);
1081 324 : opposite->setLaneSpreadFunction(LaneSpreadFunction::RIGHT);
1082 : } else {
1083 331 : i->second->setLaneSpreadFunction(LaneSpreadFunction::CENTER);
1084 : }
1085 : }
1086 49 : }
1087 :
1088 :
1089 : NBEdge*
1090 1143 : NBEdgeCont::getOppositeByID(const std::string& edgeID) const {
1091 1143 : const std::string oppositeID = edgeID[0] == '-' ? edgeID.substr(1) : "-" + edgeID;
1092 : EdgeCont::const_iterator it = myEdges.find(oppositeID);
1093 1143 : return it != myEdges.end() ? it->second : (NBEdge*)nullptr;
1094 : }
1095 :
1096 : NBEdge*
1097 19713 : NBEdgeCont::getByID(const std::string& edgeID) const {
1098 : EdgeCont::const_iterator it = myEdges.find(edgeID);
1099 19713 : return it != myEdges.end() ? it->second : (NBEdge*)nullptr;
1100 : }
1101 :
1102 : // ----- other
1103 : void
1104 18 : NBEdgeCont::addPostProcessConnection(const std::string& from, int fromLane, const std::string& to, int toLane, bool mayDefinitelyPass,
1105 : KeepClear keepClear, double contPos, double visibility, double speed, double friction, double length,
1106 : const PositionVector& customShape, bool uncontrolled, bool warnOnly,
1107 : SVCPermissions permissions, bool indirectLeft, const std::string& edgeType, SVCPermissions changeLeft, SVCPermissions changeRight) {
1108 36 : myConnections[from].push_back(PostProcessConnection(from, fromLane, to, toLane, mayDefinitelyPass, keepClear, contPos, visibility,
1109 : speed, friction, length, customShape, uncontrolled, warnOnly, permissions, indirectLeft, edgeType, changeLeft, changeRight));
1110 18 : }
1111 :
1112 : bool
1113 8080 : NBEdgeCont::hasPostProcessConnection(const std::string& from, const std::string& to) {
1114 : if (myConnections.count(from) == 0) {
1115 8076 : return false;
1116 : } else {
1117 4 : if (to == "") {
1118 : // wildcard
1119 : return true;
1120 : }
1121 0 : for (const auto& ppc : myConnections[from]) {
1122 0 : if (ppc.to == to) {
1123 : return true;
1124 : }
1125 : }
1126 : return false;
1127 : }
1128 : }
1129 :
1130 : void
1131 1813 : NBEdgeCont::recheckPostProcessConnections() {
1132 3538 : const bool warnOnly = OptionsCont::getOptions().exists("ignore-errors.connections") && OptionsCont::getOptions().getBool("ignore-errors.connections");
1133 1820 : for (const auto& item : myConnections) {
1134 25 : for (std::vector<PostProcessConnection>::const_iterator i = item.second.begin(); i != item.second.end(); ++i) {
1135 18 : NBEdge* from = retrievePossiblySplit((*i).from, true);
1136 18 : NBEdge* to = retrievePossiblySplit((*i).to, false);
1137 36 : if (from == nullptr || to == nullptr ||
1138 18 : !from->addLane2LaneConnection((*i).fromLane, to, (*i).toLane, NBEdge::Lane2LaneInfoType::USER, true, (*i).mayDefinitelyPass,
1139 18 : (*i).keepClear, (*i).contPos, (*i).visibility, (*i).speed, (*i).friction, (*i).customLength, (*i).customShape,
1140 18 : (*i).uncontrolled, (*i).permissions, (*i).indirectLeft, (*i).edgeType, (*i).changeLeft, (*i).changeRight,
1141 : true)) {
1142 1 : const std::string msg = "Could not insert connection between '" + (*i).from + "' and '" + (*i).to + "' after build.";
1143 1 : if (warnOnly || (*i).warnOnly) {
1144 0 : WRITE_WARNING(msg);
1145 : } else {
1146 3 : WRITE_ERROR(msg);
1147 : }
1148 : }
1149 : }
1150 : }
1151 : // during loading we also kept some ambiguous connections in hope they might be valid after processing
1152 : // we need to make sure that all invalid connections are removed now
1153 104317 : for (EdgeCont::iterator it = myEdges.begin(); it != myEdges.end(); ++it) {
1154 102504 : NBEdge* edge = it->second;
1155 : NBNode* to = edge->getToNode();
1156 : // make a copy because we may delete connections
1157 102504 : std::vector<NBEdge::Connection> connections = edge->getConnections();
1158 174883 : for (std::vector<NBEdge::Connection>::iterator it_con = connections.begin(); it_con != connections.end(); ++it_con) {
1159 : NBEdge::Connection& c = *it_con;
1160 72379 : if (c.toEdge != nullptr && c.toEdge->getFromNode() != to) {
1161 12 : WRITE_WARNING("Found and removed invalid connection from edge '" + edge->getID() +
1162 : "' to edge '" + c.toEdge->getID() + "' via junction '" + to->getID() + "'.");
1163 6 : edge->removeFromConnections(c.toEdge);
1164 : }
1165 : }
1166 102504 : }
1167 1813 : }
1168 :
1169 :
1170 : EdgeVector
1171 842 : NBEdgeCont::getGeneratedFrom(const std::string& id) const {
1172 842 : int len = (int)id.length();
1173 : EdgeVector ret;
1174 149420 : for (EdgeCont::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
1175 : std::string curr = (*i).first;
1176 : // the next check makes it possibly faster - we don not have
1177 : // to compare the names
1178 148578 : if ((int)curr.length() <= len) {
1179 55988 : continue;
1180 : }
1181 : // the name must be the same as the given id but something
1182 : // beginning with a '[' must be appended to it
1183 185180 : if (curr.substr(0, len) == id && curr[len] == '[') {
1184 4828 : ret.push_back((*i).second);
1185 4828 : continue;
1186 : }
1187 : // ok, maybe the edge is a compound made during joining of edges
1188 : std::string::size_type pos = curr.find(id);
1189 : // surely not
1190 87762 : if (pos == std::string::npos) {
1191 75438 : continue;
1192 : }
1193 : // check leading char
1194 12324 : if (pos > 0) {
1195 3975 : if (curr[pos - 1] != ']' && curr[pos - 1] != '+') {
1196 : // actually, this is another id
1197 3975 : continue;
1198 : }
1199 : }
1200 8349 : if (pos + id.length() < curr.length()) {
1201 8349 : if (curr[pos + id.length()] != '[' && curr[pos + id.length()] != '+') {
1202 : // actually, this is another id
1203 8349 : continue;
1204 : }
1205 : }
1206 0 : ret.push_back((*i).second);
1207 : }
1208 842 : return ret;
1209 0 : }
1210 :
1211 :
1212 : int
1213 1988 : NBEdgeCont::guessRoundabouts() {
1214 : myGuessedRoundabouts.clear();
1215 : std::set<NBEdge*> loadedRoundaboutEdges;
1216 2075 : for (std::set<EdgeSet>::const_iterator it = myRoundabouts.begin(); it != myRoundabouts.end(); ++it) {
1217 87 : loadedRoundaboutEdges.insert(it->begin(), it->end());
1218 : }
1219 : // step 1: keep only those edges which have no turnarounds and which are not
1220 : // part of a loaded roundabout
1221 : std::set<NBEdge*> candidates;
1222 1988 : SVCPermissions valid = SVCAll & ~SVC_PEDESTRIAN;
1223 138710 : for (EdgeCont::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
1224 136722 : NBEdge* e = (*i).second;
1225 : NBNode* const to = e->getToNode();
1226 136722 : if (e->getTurnDestination() == nullptr
1227 66395 : && to->getConnectionTo(e->getFromNode()) == nullptr
1228 187508 : && (e->getPermissions() & valid) != 0) {
1229 : candidates.insert(e);
1230 : }
1231 : }
1232 :
1233 : // step 2:
1234 : std::set<NBEdge*> visited;
1235 33047 : for (std::set<NBEdge*>::const_iterator i = candidates.begin(); i != candidates.end(); ++i) {
1236 : EdgeVector loopEdges;
1237 : // start with a random edge (this doesn't have to be a roundabout edge)
1238 : // loop over connected edges (using always the leftmost one)
1239 : // and keep the list in loopEdges
1240 : // continue until we loop back onto a loopEdges and extract the loop
1241 31059 : NBEdge* e = (*i);
1242 14076 : if (visited.count(e) > 0) {
1243 : // already seen
1244 : continue;
1245 : }
1246 16983 : loopEdges.push_back(e);
1247 : bool doLoop = true;
1248 : #ifdef DEBUG_GUESS_ROUNDABOUT
1249 : gDebugFlag1 = e->getID() == DEBUG_EDGE_ID;
1250 : #endif
1251 21506 : do {
1252 : #ifdef DEBUG_GUESS_ROUNDABOUT
1253 : if (gDebugFlag1) {
1254 : std::cout << " e=" << e->getID() << " loopEdges=" << toString(loopEdges) << "\n";
1255 : gDebugFlag1 = true;
1256 : }
1257 : #endif
1258 : visited.insert(e);
1259 33954 : const EdgeVector& edges = e->getToNode()->getEdges();
1260 32544 : if ((e->getToNode()->getType() == SumoXMLNodeType::RIGHT_BEFORE_LEFT || e->getToNode()->getType() == SumoXMLNodeType::LEFT_BEFORE_RIGHT)
1261 33963 : && !e->getToNode()->typeWasGuessed()) {
1262 : doLoop = false;
1263 : #ifdef DEBUG_GUESS_ROUNDABOUT
1264 : if (gDebugFlag1) {
1265 : std::cout << " rbl\n";
1266 : }
1267 : gDebugFlag1 = false;
1268 : #endif
1269 12448 : break;
1270 : }
1271 32914 : if (e->getToNode()->getRoundaboutType() == RoundaboutType::NO) {
1272 : doLoop = false;
1273 : #ifdef DEBUG_GUESS_ROUNDABOUT
1274 : if (gDebugFlag1) {
1275 : std::cout << " disabled\n";
1276 : }
1277 : gDebugFlag1 = false;
1278 : #endif
1279 : break;
1280 : }
1281 32913 : if (edges.size() < 2) {
1282 : doLoop = false;
1283 : #ifdef DEBUG_GUESS_ROUNDABOUT
1284 : if (gDebugFlag1) {
1285 : std::cout << " deadend\n";
1286 : }
1287 : gDebugFlag1 = false;
1288 : #endif
1289 : break;
1290 : }
1291 29624 : if (e->getTurnDestination(true) != nullptr || e->getToNode()->getConnectionTo(e->getFromNode()) != nullptr) {
1292 : // do not follow turn-arounds while in a (tentative) loop
1293 : doLoop = false;
1294 : #ifdef DEBUG_GUESS_ROUNDABOUT
1295 : if (gDebugFlag1) {
1296 : std::cout << " invalid turnAround e=" << e->getID() << " dest=" << Named::getIDSecure(e->getTurnDestination()) << "\n";
1297 : }
1298 : gDebugFlag1 = false;
1299 : #endif
1300 : break;
1301 : }
1302 26054 : EdgeVector::const_iterator me = std::find(edges.begin(), edges.end(), e);
1303 26054 : NBContHelper::nextCW(edges, me);
1304 26054 : NBEdge* left = *me;
1305 27706 : while ((left->getPermissions() & valid) == 0 && left != e) {
1306 1652 : NBContHelper::nextCW(edges, me);
1307 1652 : left = *me;
1308 : }
1309 26054 : if (left == e) {
1310 : // no usable continuation edge found
1311 : doLoop = false;
1312 : #ifdef DEBUG_GUESS_ROUNDABOUT
1313 : if (gDebugFlag1) {
1314 : std::cout << " noContinuation\n";
1315 : }
1316 : gDebugFlag1 = false;
1317 : #endif
1318 : break;
1319 : }
1320 26025 : NBContHelper::nextCW(edges, me);
1321 26025 : NBEdge* nextLeft = *me;
1322 26025 : double angle = fabs(NBHelpers::relAngle(e->getAngleAtNode(e->getToNode()), left->getAngleAtNode(e->getToNode())));
1323 26025 : double nextAngle = nextLeft == e ? 180 : fabs(NBHelpers::relAngle(e->getAngleAtNode(e->getToNode()), nextLeft->getAngleAtNode(e->getToNode())));
1324 : #ifdef DEBUG_GUESS_ROUNDABOUT
1325 : if (gDebugFlag1) {
1326 : std::cout << " e=" << e->getID() << " left=" << left->getID() << " nextLeft=" << nextLeft->getID() << " angle=" << angle << " nextAngle=" << nextAngle << " eLength=" << e->getLength() << " lLength=" << left->getLength() << " dist=" << e->getLaneShape(0).back().distanceTo2D(left->getLaneShape(0).front()) << "\n";
1327 : }
1328 : #endif
1329 : // there should be no straigher edge further left
1330 26025 : if (angle >= 90 && nextAngle < 45) {
1331 : doLoop = false;
1332 : #ifdef DEBUG_GUESS_ROUNDABOUT
1333 : if (gDebugFlag1) {
1334 : std::cout << " failed nextAngle=" << nextAngle << "\n";
1335 : }
1336 : gDebugFlag1 = false;
1337 : #endif
1338 : break;
1339 : }
1340 : // roundabouts do not have sharp turns (or they wouldn't be called 'round')
1341 : // however, if the roundabout is very small then most of the roundness may be in the junction so the angle may be as high as 180 (for smooth attachments at a joined junction)
1342 : if (angle >= 90) {
1343 2487 : double edgeAngle = fabs(NBHelpers::relAngle(e->getStartAngle(), e->getEndAngle()));
1344 2487 : double edgeAngle2 = fabs(NBHelpers::relAngle(left->getStartAngle(), left->getEndAngle()));
1345 2487 : double edgeRadius = e->getGeometry().length2D() / DEG2RAD(edgeAngle);
1346 2487 : double edgeRadius2 = left->getGeometry().length2D() / DEG2RAD(edgeAngle2);
1347 2487 : const double avgRadius = 0.5 * (edgeRadius + edgeRadius2);
1348 2487 : double junctionRadius = e->getLaneShape(0).back().distanceTo2D(left->getLaneShape(0).front()) / DEG2RAD(angle);
1349 : //std::cout << " junction=" << e->getToNode()->getID() << " e=" << e->getID() << " left=" << left->getID() << " angle=" << angle << " eRadius=" << edgeRadius << " eRadius2=" << edgeRadius2 << " jRadius3=" << junctionRadius << "\n";
1350 2487 : if (junctionRadius < 0.8 * avgRadius) {
1351 : doLoop = false;
1352 : #ifdef DEBUG_GUESS_ROUNDABOUT
1353 : if (gDebugFlag1) {
1354 : std::cout << " failed angle=" << angle << " eRadius=" << edgeRadius << " eRadius2=" << edgeRadius2 << " jRadius3=" << junctionRadius << "\n";
1355 : }
1356 : gDebugFlag1 = false;
1357 : #endif
1358 : break;
1359 : }
1360 : }
1361 :
1362 21684 : EdgeVector::const_iterator loopClosed = std::find(loopEdges.begin(), loopEdges.end(), left);
1363 21684 : const int loopSize = (int)(loopEdges.end() - loopClosed);
1364 21684 : if (loopSize > 0) {
1365 : // loop found
1366 178 : if (loopSize < 3) {
1367 : doLoop = false; // need at least 3 edges for a roundabout
1368 110 : } else if (loopSize < (int)loopEdges.size()) {
1369 : // remove initial edges not belonging to the loop
1370 48 : EdgeVector(loopEdges.begin() + (loopEdges.size() - loopSize), loopEdges.end()).swap(loopEdges);
1371 : }
1372 : // count attachments to the outside. need at least 3 or a roundabout doesn't make much sense
1373 : int attachments = 0;
1374 997 : for (EdgeVector::const_iterator j = loopEdges.begin(); j != loopEdges.end(); ++j) {
1375 819 : if ((*j)->getToNode()->getEdges().size() > 2) {
1376 542 : attachments++;
1377 : }
1378 : }
1379 178 : if (attachments < 3) {
1380 : doLoop = false;
1381 : #ifdef DEBUG_GUESS_ROUNDABOUT
1382 : if (gDebugFlag1) {
1383 : std::cout << " attachments=" << attachments << "\n";
1384 : }
1385 : gDebugFlag1 = false;
1386 : #endif
1387 : }
1388 : break;
1389 : }
1390 : if (visited.count(left) > 0) {
1391 : doLoop = false;
1392 : } else {
1393 : // keep going
1394 16971 : loopEdges.push_back(left);
1395 16971 : e = left;
1396 : }
1397 : } while (doLoop);
1398 16911 : if (doLoop) {
1399 : // check form factor to avoid elongated shapes (circle: 1, square: ~0.79)
1400 : #ifdef DEBUG_GUESS_ROUNDABOUT
1401 : if (gDebugFlag1) {
1402 : std::cout << " formFactor=" << formFactor(loopEdges) << "\n";
1403 : }
1404 : #endif
1405 : double loopLength = 0;
1406 637 : for (const NBEdge* const le : loopEdges) {
1407 536 : loopLength += le->getLoadedLength();
1408 : }
1409 101 : if (formFactor(loopEdges) > 0.6
1410 199 : && loopLength < OptionsCont::getOptions().getFloat("roundabouts.guess.max-length")) {
1411 : // collected edges are marked in markRoundabouts
1412 96 : EdgeSet guessed(loopEdges.begin(), loopEdges.end());
1413 : if (loadedRoundaboutEdges.count(loopEdges.front()) != 0) {
1414 79 : if (find(myRoundabouts.begin(), myRoundabouts.end(), guessed) == myRoundabouts.end()) {
1415 2 : for (auto it = myRoundabouts.begin(); it != myRoundabouts.end(); it++) {
1416 : if ((*it).count(loopEdges.front()) != 0) {
1417 4 : WRITE_WARNINGF(TL("Replacing loaded roundabout '%' with '%'."), toString(*it), toString(guessed));
1418 : myRoundabouts.erase(it);
1419 : break;
1420 : }
1421 : }
1422 : myGuessedRoundabouts.insert(guessed);
1423 : }
1424 : } else {
1425 : myGuessedRoundabouts.insert(guessed);
1426 : #ifdef DEBUG_GUESS_ROUNDABOUT
1427 : if (gDebugFlag1) {
1428 : std::cout << " foundRoundabout=" << toString(loopEdges) << "\n";
1429 : }
1430 : #endif
1431 : }
1432 : }
1433 : }
1434 : #ifdef DEBUG_GUESS_ROUNDABOUT
1435 : gDebugFlag1 = false;
1436 : #endif
1437 31059 : }
1438 3976 : return (int)myGuessedRoundabouts.size();
1439 : }
1440 :
1441 :
1442 : int
1443 183 : NBEdgeCont::extractRoundabouts() {
1444 : std::set<NBEdge*> candidateEdges;
1445 41733 : for (const auto& edge : myEdges) {
1446 41550 : NBEdge* const e = edge.second;
1447 41550 : if (e->getJunctionPriority(e->getToNode()) == NBEdge::JunctionPriority::ROUNDABOUT || e->getJunctionPriority(e->getFromNode()) == NBEdge::JunctionPriority::ROUNDABOUT) {
1448 : candidateEdges.insert(e);
1449 : }
1450 : }
1451 : std::set<NBEdge*> visited;
1452 : int extracted = 0;
1453 242 : for (const auto& edgeIt : candidateEdges) {
1454 : EdgeVector loopEdges;
1455 59 : NBEdge* e = edgeIt;
1456 48 : if (visited.count(e) > 0) {
1457 : // already seen
1458 : continue;
1459 : }
1460 11 : loopEdges.push_back(e);
1461 : bool doLoop = true;
1462 : //
1463 59 : do {
1464 70 : if (std::find(visited.begin(), visited.end(), e) != visited.end()) {
1465 11 : if (loopEdges.size() > 1) {
1466 11 : addRoundabout(EdgeSet(loopEdges.begin(), loopEdges.end()));
1467 11 : ++extracted;
1468 : }
1469 : doLoop = false;
1470 : break;
1471 : }
1472 : visited.insert(e);
1473 59 : loopEdges.push_back(e);
1474 59 : const EdgeVector& outgoingEdges = e->getToNode()->getOutgoingEdges();
1475 : EdgeVector::const_iterator me = std::find_if(outgoingEdges.begin(), outgoingEdges.end(), [](const NBEdge * outgoingEdge) {
1476 83 : return outgoingEdge->getJunctionPriority(outgoingEdge->getToNode()) == NBEdge::JunctionPriority::ROUNDABOUT;
1477 : });
1478 59 : if (me == outgoingEdges.end()) { // no closed loop
1479 : doLoop = false;
1480 : } else {
1481 59 : e = *me;
1482 : }
1483 : } while (doLoop);
1484 59 : }
1485 183 : return extracted;
1486 : }
1487 :
1488 :
1489 : void
1490 18 : NBEdgeCont::cleanupRoundabouts() {
1491 : // only loaded roundabouts are of concern here since guessing comes later
1492 : std::set<EdgeSet> validRoundabouts;
1493 : std::set<NBEdge*> validEdges;
1494 1320 : for (auto item : myEdges) {
1495 : validEdges.insert(item.second);
1496 : }
1497 21 : for (EdgeSet roundabout : myRoundabouts) {
1498 : EdgeSet validRoundabout;
1499 5 : for (NBEdge* cand : roundabout) {
1500 : if (validEdges.count(cand) != 0) {
1501 : validRoundabout.insert(cand);
1502 : }
1503 : }
1504 3 : if (validRoundabout.size() > 0) {
1505 : validRoundabouts.insert(validRoundabout);
1506 : }
1507 : }
1508 : myRoundabouts = validRoundabouts;
1509 18 : }
1510 :
1511 :
1512 : double
1513 101 : NBEdgeCont::formFactor(const EdgeVector& loopEdges) {
1514 : // A circle (which maximizes area per circumference) has a formfactor of 1, non-circular shapes have a smaller value
1515 101 : PositionVector points;
1516 637 : for (EdgeVector::const_iterator it = loopEdges.begin(); it != loopEdges.end(); ++it) {
1517 536 : points.append((*it)->getGeometry());
1518 : }
1519 101 : double circumference = points.length2D();
1520 202 : return 4 * M_PI * points.area() / (circumference * circumference);
1521 101 : }
1522 :
1523 :
1524 : const std::set<EdgeSet>
1525 7455 : NBEdgeCont::getRoundabouts() const {
1526 : std::set<EdgeSet> result = myRoundabouts;
1527 7455 : result.insert(myGuessedRoundabouts.begin(), myGuessedRoundabouts.end());
1528 7455 : return result;
1529 : }
1530 :
1531 :
1532 : void
1533 82 : NBEdgeCont::addRoundabout(const EdgeSet& roundabout) {
1534 82 : if (roundabout.size() > 0) {
1535 80 : if (find(myRoundabouts.begin(), myRoundabouts.end(), roundabout) != myRoundabouts.end()) {
1536 0 : WRITE_WARNING("Ignoring duplicate roundabout: " + toString(roundabout));
1537 : } else {
1538 : myRoundabouts.insert(roundabout);
1539 : }
1540 : }
1541 82 : }
1542 :
1543 : void
1544 4 : NBEdgeCont::removeRoundabout(const NBNode* node) {
1545 4 : for (auto it = myRoundabouts.begin(); it != myRoundabouts.end(); ++it) {
1546 2 : for (NBEdge* e : *it) {
1547 2 : if (e->getToNode() == node) {
1548 : myRoundabouts.erase(it);
1549 : return;
1550 : }
1551 : }
1552 : }
1553 : }
1554 :
1555 : void
1556 23 : NBEdgeCont::removeRoundaboutEdges(const EdgeSet& toRemove) {
1557 23 : removeRoundaboutEdges(toRemove, myRoundabouts);
1558 23 : removeRoundaboutEdges(toRemove, myGuessedRoundabouts);
1559 23 : }
1560 :
1561 : void
1562 46 : NBEdgeCont::removeRoundaboutEdges(const EdgeSet& toRemove, std::set<EdgeSet>& roundabouts) {
1563 : // members of a set are constant so we have to do some tricks
1564 : std::vector<EdgeSet> rList;
1565 48 : for (const EdgeSet& r : roundabouts) {
1566 : EdgeSet r2;
1567 2 : std::set_difference(r.begin(), r.end(), toRemove.begin(), toRemove.end(), std::inserter(r2, r2.end()));
1568 2 : rList.push_back(r2);
1569 : }
1570 : roundabouts.clear();
1571 : roundabouts.insert(rList.begin(), rList.end());
1572 46 : }
1573 :
1574 :
1575 : void
1576 1861 : NBEdgeCont::markRoundabouts() {
1577 1953 : for (const EdgeSet& roundaboutSet : getRoundabouts()) {
1578 536 : for (NBEdge* const edge : roundaboutSet) {
1579 : // disable turnarounds on incoming edges
1580 : NBNode* const node = edge->getToNode();
1581 1201 : for (NBEdge* const inEdge : node->getIncomingEdges()) {
1582 444 : if (roundaboutSet.count(inEdge) > 0) {
1583 444 : continue;
1584 : }
1585 313 : if (inEdge->getStep() >= NBEdge::EdgeBuildingStep::LANES2LANES_USER) {
1586 199 : continue;
1587 : }
1588 114 : if (inEdge->getTurnDestination() != nullptr) {
1589 52 : inEdge->removeFromConnections(inEdge->getTurnDestination(), -1);
1590 : } else {
1591 : // also remove connections that are effecively a turnaround but
1592 : // where not correctly detector due to geometrical quirks
1593 62 : const std::vector<NBEdge::Connection> cons = inEdge->getConnections();
1594 126 : for (const NBEdge::Connection& con : cons) {
1595 64 : if (con.toEdge && roundaboutSet.count(con.toEdge) == 0) {
1596 10 : const double angle = fabs(NBHelpers::normRelAngle(inEdge->getAngleAtNode(node), con.toEdge->getAngleAtNode(node)));
1597 10 : if (angle > 160) {
1598 1 : inEdge->removeFromConnections(con.toEdge, -1);
1599 : }
1600 : }
1601 : }
1602 62 : }
1603 :
1604 : }
1605 : // let the connections to succeeding roundabout edge have a higher priority
1606 444 : edge->setJunctionPriority(node, NBEdge::JunctionPriority::ROUNDABOUT);
1607 444 : edge->setJunctionPriority(edge->getFromNode(), NBEdge::JunctionPriority::ROUNDABOUT);
1608 444 : node->setRoundabout();
1609 : }
1610 : }
1611 1861 : }
1612 :
1613 :
1614 : void
1615 1 : NBEdgeCont::generateStreetSigns() {
1616 23 : for (EdgeCont::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
1617 22 : NBEdge* e = i->second;
1618 22 : const double offset = MAX2(0., e->getLength() - 3);
1619 22 : if (e->getToNode()->isSimpleContinuation(false)) {
1620 : // not a "real" junction?
1621 10 : continue;
1622 : }
1623 : const SumoXMLNodeType nodeType = e->getToNode()->getType();
1624 12 : switch (nodeType) {
1625 4 : case SumoXMLNodeType::PRIORITY:
1626 : // yield or major?
1627 4 : if (e->getJunctionPriority(e->getToNode()) > 0) {
1628 6 : e->addSign(NBSign(NBSign::SIGN_TYPE_PRIORITY, offset));
1629 : } else {
1630 6 : e->addSign(NBSign(NBSign::SIGN_TYPE_YIELD, offset));
1631 : }
1632 : break;
1633 0 : case SumoXMLNodeType::PRIORITY_STOP:
1634 : // yield or major?
1635 0 : if (e->getJunctionPriority(e->getToNode()) > 0) {
1636 0 : e->addSign(NBSign(NBSign::SIGN_TYPE_PRIORITY, offset));
1637 : } else {
1638 0 : e->addSign(NBSign(NBSign::SIGN_TYPE_STOP, offset));
1639 : }
1640 : break;
1641 0 : case SumoXMLNodeType::ALLWAY_STOP:
1642 0 : e->addSign(NBSign(NBSign::SIGN_TYPE_ALLWAY_STOP, offset));
1643 0 : break;
1644 4 : case SumoXMLNodeType::RIGHT_BEFORE_LEFT:
1645 8 : e->addSign(NBSign(NBSign::SIGN_TYPE_RIGHT_BEFORE_LEFT, offset));
1646 4 : break;
1647 0 : case SumoXMLNodeType::LEFT_BEFORE_RIGHT:
1648 0 : e->addSign(NBSign(NBSign::SIGN_TYPE_LEFT_BEFORE_RIGHT, offset));
1649 0 : break;
1650 : default:
1651 : break;
1652 : }
1653 : }
1654 1 : }
1655 :
1656 :
1657 : int
1658 21 : NBEdgeCont::guessSpecialLanes(SUMOVehicleClass svc, double width, double minSpeed, double maxSpeed, bool fromPermissions, const std::string& excludeOpt,
1659 : NBTrafficLightLogicCont& tlc) {
1660 : int lanesCreated = 0;
1661 : std::vector<std::string> edges;
1662 21 : if (excludeOpt != "") {
1663 21 : edges = OptionsCont::getOptions().getStringVector(excludeOpt);
1664 : }
1665 21 : std::set<std::string> exclude(edges.begin(), edges.end());
1666 443 : for (EdgeCont::iterator it = myEdges.begin(); it != myEdges.end(); it++) {
1667 422 : NBEdge* edge = it->second;
1668 : if (// not excluded
1669 422 : exclude.count(edge->getID()) == 0
1670 : // does not yet have a sidewalk/bikelane
1671 419 : && !edge->hasRestrictedLane(svc)
1672 : // needs a sidewalk/bikelane
1673 301 : && ((edge->getPermissions() & ~SVC_VULNERABLE) != 0 || (edge->getPermissions() & svc) == 0)
1674 284 : && (
1675 : // guess.from-permissions
1676 138 : (fromPermissions && (edge->getPermissions() & svc) != 0)
1677 : // guess from speed
1678 146 : || (!fromPermissions && edge->getSpeed() > minSpeed && edge->getSpeed() <= maxSpeed)
1679 : )) {
1680 236 : edge->addRestrictedLane(width, svc);
1681 236 : lanesCreated += 1;
1682 236 : if (svc != SVC_PEDESTRIAN) {
1683 36 : if (edge->getStep() == NBEdge::EdgeBuildingStep::LANES2LANES_USER) {
1684 : // preserve existing connections and only add new ones
1685 : edge->declareConnectionsAsLoaded(NBEdge::EdgeBuildingStep::LANES2LANES_DONE);
1686 9 : edge->getFromNode()->recheckVClassConnections(edge);
1687 30 : for (NBEdge* to : edge->getToNode()->getOutgoingEdges()) {
1688 21 : edge->getToNode()->recheckVClassConnections(to);
1689 : }
1690 : // patching TLS is not feasible because existing states may
1691 : // change from 'G' to 'g' when bike lanes are added (i.e. right-turns)
1692 : } else {
1693 27 : edge->invalidateConnections(true);
1694 27 : edge->getFromNode()->invalidateOutgoingConnections(true);
1695 : }
1696 36 : edge->getFromNode()->invalidateTLS(tlc, true, false);
1697 36 : edge->getToNode()->invalidateTLS(tlc, true, false);
1698 : }
1699 : }
1700 : }
1701 21 : return lanesCreated;
1702 21 : }
1703 :
1704 :
1705 : void
1706 3 : NBEdgeCont::updateAllChangeRestrictions(SVCPermissions ignoring) {
1707 9 : for (auto item : myEdges) {
1708 6 : item.second->updateChangeRestrictions(ignoring);
1709 : }
1710 3 : }
1711 :
1712 :
1713 : void
1714 0 : NBEdgeCont::addPrefix(const std::string& prefix) {
1715 : // make a copy of node containers
1716 : const auto nodeContainerCopy = myEdges;
1717 : myEdges.clear();
1718 0 : for (const auto& node : nodeContainerCopy) {
1719 0 : node.second->setID(prefix + node.second->getID());
1720 0 : myEdges[node.second->getID()] = node.second;
1721 : }
1722 0 : }
1723 :
1724 :
1725 : int
1726 1813 : NBEdgeCont::remapIDs(bool numericaIDs, bool reservedIDs, bool keptIDs, const std::string& prefix, NBPTStopCont& sc) {
1727 1813 : bool startGiven = !OptionsCont::getOptions().isDefault("numerical-ids.edge-start");
1728 1813 : if (!numericaIDs && !reservedIDs && prefix == "" && !startGiven) {
1729 : return 0;
1730 : }
1731 : std::vector<std::string> avoid;
1732 65 : if (startGiven) {
1733 6 : avoid.push_back(toString(OptionsCont::getOptions().getInt("numerical-ids.edge-start") - 1));
1734 : } else {
1735 62 : avoid = getAllNames();
1736 : }
1737 : std::set<std::string> reserve;
1738 65 : if (reservedIDs) {
1739 4 : NBHelpers::loadPrefixedIDsFomFile(OptionsCont::getOptions().getString("reserved-ids"), "edge:", reserve);
1740 2 : avoid.insert(avoid.end(), reserve.begin(), reserve.end());
1741 : }
1742 130 : IDSupplier idSupplier("", avoid);
1743 : std::set<NBEdge*, ComparatorIdLess> toChange;
1744 25305 : for (EdgeCont::iterator it = myEdges.begin(); it != myEdges.end(); it++) {
1745 25240 : if (startGiven) {
1746 78 : toChange.insert(it->second);
1747 78 : continue;
1748 : }
1749 25162 : if (numericaIDs) {
1750 : try {
1751 24972 : StringUtils::toLong(it->first);
1752 9543 : } catch (NumberFormatException&) {
1753 9543 : toChange.insert(it->second);
1754 9543 : }
1755 : }
1756 25162 : if (reservedIDs && reserve.count(it->first) > 0) {
1757 2 : toChange.insert(it->second);
1758 : }
1759 : }
1760 : std::set<std::string> keep;
1761 65 : if (keptIDs) {
1762 4 : NBHelpers::loadPrefixedIDsFomFile(OptionsCont::getOptions().getString("kept-ids"), "edge:", keep);
1763 10 : for (auto it = toChange.begin(); it != toChange.end();) {
1764 8 : if (keep.count((*it)->getID()) != 0) {
1765 : toChange.erase(it++);
1766 : } else {
1767 : it++;
1768 : }
1769 : }
1770 : }
1771 :
1772 : std::map<std::string, std::vector<std::shared_ptr<NBPTStop> > > stopsOnEdge;
1773 91 : for (const auto& item : sc.getStops()) {
1774 26 : stopsOnEdge[item.second->getEdgeId()].push_back(item.second);
1775 : }
1776 :
1777 130 : const bool origNames = OptionsCont::getOptions().getBool("output.original-names");
1778 9684 : for (NBEdge* edge : toChange) {
1779 9619 : myEdges.erase(edge->getID());
1780 : }
1781 9684 : for (NBEdge* edge : toChange) {
1782 9619 : const std::string origID = edge->getID();
1783 9619 : if (origNames) {
1784 18346 : edge->setOrigID(origID, false);
1785 : }
1786 9619 : edge->setID(idSupplier.getNext());
1787 9619 : myEdges[edge->getID()] = edge;
1788 9638 : for (std::shared_ptr<NBPTStop> stop : stopsOnEdge[origID]) {
1789 57 : stop->setEdgeId(prefix + edge->getID(), *this);
1790 : }
1791 : }
1792 65 : if (prefix.empty()) {
1793 55 : return (int)toChange.size();
1794 : } else {
1795 : int renamed = 0;
1796 : // make a copy because we will modify the map
1797 : auto oldEdges = myEdges;
1798 208 : for (auto item : oldEdges) {
1799 396 : if (!StringUtils::startsWith(item.first, prefix) && keep.count(item.first) == 0) {
1800 193 : rename(item.second, prefix + item.first);
1801 193 : renamed++;
1802 : }
1803 : }
1804 : return renamed;
1805 : }
1806 130 : }
1807 :
1808 :
1809 : void
1810 0 : NBEdgeCont::checkOverlap(double threshold, double zThreshold) const {
1811 0 : for (EdgeCont::const_iterator it = myEdges.begin(); it != myEdges.end(); it++) {
1812 0 : const NBEdge* e1 = it->second;
1813 0 : Boundary b1 = e1->getGeometry().getBoxBoundary();
1814 0 : b1.grow(e1->getTotalWidth());
1815 0 : PositionVector outline1 = e1->getCCWBoundaryLine(*e1->getFromNode());
1816 0 : outline1.append(e1->getCCWBoundaryLine(*e1->getToNode()));
1817 : // check is symmetric. only check once per pair
1818 0 : for (EdgeCont::const_iterator it2 = it; it2 != myEdges.end(); it2++) {
1819 0 : const NBEdge* e2 = it2->second;
1820 0 : if (e1 == e2) {
1821 0 : continue;
1822 : }
1823 0 : Boundary b2 = e2->getGeometry().getBoxBoundary();
1824 0 : b2.grow(e2->getTotalWidth());
1825 0 : if (b1.overlapsWith(b2)) {
1826 0 : PositionVector outline2 = e2->getCCWBoundaryLine(*e2->getFromNode());
1827 0 : outline2.append(e2->getCCWBoundaryLine(*e2->getToNode()));
1828 0 : const double overlap = outline1.getOverlapWith(outline2, zThreshold);
1829 0 : if (overlap > threshold) {
1830 0 : WRITE_WARNINGF(TL("Edge '%' overlaps with edge '%' by %."), e1->getID(), e2->getID(), overlap);
1831 : }
1832 0 : }
1833 : }
1834 0 : }
1835 0 : }
1836 :
1837 :
1838 : void
1839 25 : NBEdgeCont::checkGrade(double threshold) const {
1840 3251 : for (EdgeCont::const_iterator it = myEdges.begin(); it != myEdges.end(); it++) {
1841 3226 : const NBEdge* edge = it->second;
1842 6176 : for (int i = 0; i < (int)edge->getNumLanes(); i++) {
1843 3308 : double maxJump = 0;
1844 3308 : const double grade = edge->getLaneShape(i).getMaxGrade(maxJump);
1845 3308 : if (maxJump > 0.01) {
1846 0 : WRITE_WARNINGF(TL("Edge '%' has a vertical jump of %m."), edge->getID(), maxJump);
1847 3308 : } else if (grade > threshold) {
1848 1074 : WRITE_WARNINGF(TL("Edge '%' has a grade of %%."), edge->getID(), grade * 100, "%");
1849 358 : break;
1850 : }
1851 : }
1852 : const std::vector<NBEdge::Connection>& connections = edge->getConnections();
1853 4306 : for (std::vector<NBEdge::Connection>::const_iterator it_con = connections.begin(); it_con != connections.end(); ++it_con) {
1854 : const NBEdge::Connection& c = *it_con;
1855 1084 : double maxJump = 0;
1856 1084 : const double grade = MAX2(c.shape.getMaxGrade(maxJump), c.viaShape.getMaxGrade(maxJump));
1857 1084 : if (maxJump > 0.01) {
1858 0 : WRITE_WARNINGF(TL("Connection '%' has a vertical jump of %m."), c.getDescription(edge), maxJump);
1859 1084 : } else if (grade > threshold) {
1860 8 : WRITE_WARNINGF(TL("Connection '%' has a grade of %%."), c.getDescription(edge), grade * 100, "%");
1861 4 : break;
1862 : }
1863 : }
1864 : }
1865 25 : }
1866 :
1867 :
1868 : int
1869 2 : NBEdgeCont::joinLanes(SVCPermissions perms) {
1870 : int affectedEdges = 0;
1871 38 : for (auto item : myEdges) {
1872 36 : if (item.second->joinLanes(perms)) {
1873 18 : affectedEdges++;
1874 : }
1875 : }
1876 2 : return affectedEdges;
1877 : }
1878 :
1879 :
1880 : bool
1881 2940 : NBEdgeCont::MinLaneComparatorIdLess::operator()(const std::pair<NBEdge*, int>& a, const std::pair<NBEdge*, int>& b) const {
1882 2940 : if (a.first->getID() == b.first->getID()) {
1883 44 : return a.second < b.second;
1884 : }
1885 2896 : return a.first->getID() < b.first->getID();
1886 : }
1887 :
1888 : int
1889 7 : NBEdgeCont::joinTramEdges(NBDistrictCont& dc, NBPTStopCont& sc, NBPTLineCont& lc, double maxDist) {
1890 : // this is different from joinSimilarEdges because there don't need to be
1891 : // shared nodes and tram edges may be split
1892 : std::vector<NBEdge*> tramEdges;
1893 : std::vector<NBEdge*> targetEdges;
1894 2250 : for (auto item : myEdges) {
1895 2243 : SVCPermissions permissions = item.second->getPermissions();
1896 2243 : if (isTram(permissions)) {
1897 494 : if (item.second->getNumLanes() == 1) {
1898 466 : tramEdges.push_back(item.second);
1899 : } else {
1900 84 : WRITE_WARNINGF(TL("Not joining tram edge '%' with % lanes."), item.second->getID(), item.second->getNumLanes());
1901 : }
1902 1749 : } else if ((permissions & (SVC_PASSENGER | SVC_BUS)) != 0) {
1903 1181 : targetEdges.push_back(item.second);
1904 : }
1905 : }
1906 7 : if (tramEdges.empty() || targetEdges.empty()) {
1907 : return 0;
1908 : }
1909 : int numJoined = 0;
1910 : NamedRTree tramTree;
1911 473 : for (NBEdge* const edge : tramEdges) {
1912 466 : const Boundary& bound = edge->getGeometry().getBoxBoundary();
1913 466 : float min[2] = { static_cast<float>(bound.xmin()), static_cast<float>(bound.ymin()) };
1914 466 : float max[2] = { static_cast<float>(bound.xmax()), static_cast<float>(bound.ymax()) };
1915 932 : tramTree.Insert(min, max, edge);
1916 : }
1917 : // {targetEdge, laneIndex : tramEdge}
1918 : std::map<std::pair<NBEdge*, int>, NBEdge*, MinLaneComparatorIdLess> matches;
1919 :
1920 1188 : for (NBEdge* const edge : targetEdges) {
1921 1181 : Boundary bound = edge->getGeometry().getBoxBoundary();
1922 1181 : bound.grow(maxDist + edge->getTotalWidth());
1923 1181 : float min[2] = { static_cast<float>(bound.xmin()), static_cast<float>(bound.ymin()) };
1924 1181 : float max[2] = { static_cast<float>(bound.xmax()), static_cast<float>(bound.ymax()) };
1925 : std::set<const Named*> near;
1926 : Named::StoringVisitor visitor(near);
1927 : tramTree.Search(min, max, visitor);
1928 : // the nearby set is actually just re-sorting according to the id to make the tests comparable
1929 : std::set<NBEdge*, ComparatorIdLess> nearby;
1930 4795 : for (const Named* namedEdge : near) {
1931 3614 : nearby.insert(const_cast<NBEdge*>(static_cast<const NBEdge*>(namedEdge)));
1932 : }
1933 4795 : for (NBEdge* const tramEdge : nearby) {
1934 : // find a continous stretch of tramEdge that runs along one of the lanes of the road edge
1935 : PositionVector tramShape = tramEdge->getGeometry();
1936 3614 : if (tramEdge->getToNode() == edge->getToNode()) {
1937 87 : tramShape.extrapolate(tramShape.back().distanceTo2D(edge->getGeometry().back()), false, true);
1938 : }
1939 3614 : double minEdgeDist = maxDist + 1;
1940 : int minLane = -1;
1941 : // find the lane where the maximum distance from the tram geometry
1942 : // is minimal and within maxDist
1943 10529 : for (int i = 0; i < edge->getNumLanes(); i++) {
1944 : double maxLaneDist = -1;
1945 6915 : if ((edge->getPermissions(i) & (SVC_PASSENGER | SVC_BUS)) != 0) {
1946 6393 : const PositionVector& laneShape = edge->getLaneShape(i);
1947 7475 : for (Position pos : laneShape) {
1948 7216 : const double dist = tramShape.distance2D(pos, false);
1949 : #ifdef DEBUG_JOIN_TRAM
1950 : //if (edge->getID() == "106838214#1") {
1951 : // std::cout << " edge=" << edge->getID() << " tramEdge=" << tramEdge->getID() << " lane=" << i << " pos=" << pos << " dist=" << dist << "\n";
1952 : //}
1953 : #endif
1954 7216 : if (dist == GeomHelper::INVALID_OFFSET || dist > maxDist) {
1955 : maxLaneDist = -1;
1956 : break;
1957 : }
1958 : maxLaneDist = MAX2(maxLaneDist, dist);
1959 : }
1960 6393 : if (maxLaneDist >= 0 && maxLaneDist < minEdgeDist) {
1961 : minEdgeDist = maxLaneDist;
1962 : minLane = i;
1963 : }
1964 : }
1965 : }
1966 3614 : if (minLane >= 0) {
1967 : // edge could run in the wrong direction and still fit the threshold we check the angle as well
1968 253 : const PositionVector& laneShape = edge->getLaneShape(minLane);
1969 253 : const double offset1 = tramShape.nearest_offset_to_point2D(laneShape.front(), false);
1970 253 : const double offset2 = tramShape.nearest_offset_to_point2D(laneShape.back(), false);
1971 253 : Position p1 = tramShape.positionAtOffset2D(offset1);
1972 253 : Position p2 = tramShape.positionAtOffset2D(offset2);
1973 253 : double tramAngle = GeomHelper::legacyDegree(p1.angleTo2D(p2), true);
1974 253 : bool angleOK = GeomHelper::getMinAngleDiff(tramAngle, edge->getTotalAngle()) < JOIN_TRAM_MAX_ANGLE;
1975 253 : if (angleOK && offset2 > offset1) {
1976 205 : std::pair<NBEdge*, int> key = std::make_pair(edge, minLane);
1977 : if (matches.count(key) == 0) {
1978 195 : matches[key] = tramEdge;
1979 : } else {
1980 40 : WRITE_WARNINGF(TL("Ambiguous tram edges '%' and '%' for lane '%'."), matches[key]->getID(), tramEdge->getID(), edge->getLaneID(minLane));
1981 : }
1982 : #ifdef DEBUG_JOIN_TRAM
1983 : std::cout << edge->getLaneID(minLane) << " is close to tramEdge " << tramEdge->getID() << " maxLaneDist=" << minEdgeDist << " tramLength=" << tramEdge->getLength() << " edgeLength=" << edge->getLength() << " tramAngle=" << tramAngle << " edgeAngle=" << edge->getTotalAngle() << "\n";
1984 : #endif
1985 : }
1986 : }
1987 3614 : }
1988 : }
1989 7 : if (matches.size() == 0) {
1990 : return 0;
1991 : }
1992 14 : const bool origNames = OptionsCont::getOptions().getBool("output.original-names");
1993 : // find continous runs of matched edges for each tramEdge
1994 473 : for (NBEdge* tramEdge : tramEdges) {
1995 : std::vector<std::pair<double, std::pair<NBEdge*, int> > > roads;
1996 14774 : for (auto item : matches) {
1997 14308 : if (item.second == tramEdge) {
1998 : NBEdge* road = item.first.first;
1999 : int laneIndex = item.first.second;
2000 195 : const PositionVector& laneShape = road->getLaneShape(laneIndex);
2001 195 : double tramPos = tramEdge->getGeometry().nearest_offset_to_point2D(laneShape.front(), false);
2002 : //std::cout << " road=" << road->getID() << " tramEdge=" << tramEdge->getID() << " tramShape=" << tramEdge->getGeometry() << " laneFront=" << laneShape.front() << " tramPos=" << tramPos << "\n";
2003 195 : roads.push_back(std::make_pair(tramPos, item.first));
2004 : }
2005 : }
2006 466 : if (roads.size() != 0) {
2007 :
2008 62 : sort(roads.begin(), roads.end());
2009 : #ifdef DEBUG_JOIN_TRAM
2010 : std::cout << " tramEdge=" << tramEdge->getID() << " roads=";
2011 : for (auto item : roads) {
2012 : std::cout << item.second.first->getLaneID(item.second.second) << ",";
2013 : }
2014 : std::cout << " offsets=";
2015 : for (auto item : roads) {
2016 : std::cout << item.first << ",";
2017 : }
2018 : std::cout << "\n";
2019 : #endif
2020 : // merge tramEdge into road lanes
2021 : EdgeVector replacement;
2022 : double pos = 0;
2023 : int tramPart = 0;
2024 62 : std::string tramEdgeID = tramEdge->getID();
2025 : NBNode* tramFrom = tramEdge->getFromNode();
2026 : PositionVector tramShape = tramEdge->getGeometry();
2027 62 : const double tramLength = tramShape.length();
2028 62 : EdgeVector incoming = tramFrom->getIncomingEdges();
2029 : bool erasedLast = false;
2030 257 : for (const auto& item : roads) {
2031 195 : const double gap = item.first - pos;
2032 195 : NBEdge* road = item.second.first;
2033 195 : int laneIndex = item.second.second;
2034 195 : if (gap >= JOIN_TRAM_MIN_LENGTH && road->getFromNode() != tramEdge->getFromNode()) {
2035 : #ifdef DEBUG_JOIN_TRAM
2036 : std::cout << " splitting tramEdge=" << tramEdge->getID() << " at " << item.first << " (gap=" << gap << ")\n";
2037 : #endif
2038 74 : const std::string firstPartID = tramEdgeID + "#" + toString(tramPart++);
2039 37 : splitAt(dc, tramEdge, gap, road->getFromNode(), firstPartID, tramEdgeID, 1, 1);
2040 37 : tramEdge = retrieve(tramEdgeID); // second part;
2041 37 : NBEdge* firstPart = retrieve(firstPartID);
2042 37 : firstPart->invalidateConnections(true);
2043 : incoming.clear();
2044 37 : incoming.push_back(firstPart);
2045 37 : replacement.push_back(firstPart);
2046 : }
2047 195 : pos = item.first + road->getGeometry().length();
2048 195 : numJoined++;
2049 195 : replacement.push_back(road);
2050 : // merge section of tramEdge into road lane
2051 195 : if (road->getToNode() != tramEdge->getToNode() && (tramLength - pos) >= JOIN_TRAM_MIN_LENGTH) {
2052 164 : tramEdge->reinitNodes(road->getToNode(), tramEdge->getToNode());
2053 164 : tramEdge->setGeometry(tramShape.getSubpart(pos, tramShape.length()));
2054 : erasedLast = false;
2055 : #ifdef DEBUG_JOIN_TRAM
2056 : std::cout << " shorted tramEdge=" << tramEdge->getID() << " (joined with roadEdge=" << road->getID() << "\n";
2057 : #endif
2058 : } else {
2059 : #ifdef DEBUG_JOIN_TRAM
2060 : std::cout << " erased tramEdge=" << tramEdge->getID() << "\n";
2061 : #endif
2062 31 : extract(dc, tramEdge, true);
2063 : erasedLast = true;
2064 : }
2065 195 : road->setPermissions(road->getPermissions(laneIndex) | SVC_TRAM, laneIndex);
2066 195 : if (origNames) {
2067 114 : road->setOrigID(tramEdgeID, true, laneIndex);
2068 : }
2069 296 : for (NBEdge* in : incoming) {
2070 101 : if (isTram(in->getPermissions()) && !in->isConnectedTo(road)) {
2071 53 : if (in->getFromNode() != road->getFromNode()) {
2072 50 : in->reinitNodes(in->getFromNode(), road->getFromNode());
2073 : } else {
2074 3 : extract(dc, in, true);
2075 : #ifdef DEBUG_JOIN_TRAM
2076 : std::cout << " erased incoming tramEdge=" << in->getID() << "\n";
2077 : #endif
2078 : }
2079 : }
2080 : }
2081 : incoming.clear();
2082 : }
2083 62 : NBEdge* lastRoad = roads.back().second.first;
2084 62 : if (erasedLast) {
2085 : // copy to avoid concurrent modification
2086 29 : auto outEdges = tramEdge->getToNode()->getOutgoingEdges();
2087 94 : for (NBEdge* out : outEdges) {
2088 65 : if (isTram(out->getPermissions()) && !lastRoad->isConnectedTo(out)) {
2089 31 : if (lastRoad->getToNode() != out->getToNode()) {
2090 30 : out->reinitNodes(lastRoad->getToNode(), out->getToNode());
2091 : } else {
2092 1 : extract(dc, out, true);
2093 : #ifdef DEBUG_JOIN_TRAM
2094 : std::cout << " erased outgoing tramEdge=" << out->getID() << "\n";
2095 : #endif
2096 :
2097 : }
2098 : }
2099 : }
2100 29 : } else {
2101 33 : replacement.push_back(tramEdge);
2102 : }
2103 : // update ptstops and ptlines
2104 62 : sc.replaceEdge(tramEdgeID, replacement);
2105 62 : lc.replaceEdge(tramEdgeID, replacement);
2106 124 : }
2107 466 : }
2108 :
2109 7 : return numJoined;
2110 7 : }
2111 :
2112 :
2113 : EdgeVector
2114 155 : NBEdgeCont::getAllEdges() const {
2115 : EdgeVector result;
2116 52744 : for (auto item : myEdges) {
2117 52589 : item.second->setNumericalID((int)result.size());
2118 52589 : result.push_back(item.second);
2119 : }
2120 155 : return result;
2121 0 : }
2122 :
2123 : RouterEdgeVector
2124 65 : NBEdgeCont::getAllRouterEdges() const {
2125 65 : EdgeVector all = getAllEdges();
2126 130 : return RouterEdgeVector(all.begin(), all.end());
2127 65 : }
2128 :
2129 : bool
2130 1904 : NBEdgeCont::checkConsistency(const NBNodeCont& nc) {
2131 : bool ok = true;
2132 97019 : for (const auto& item : myEdges) {
2133 95115 : NBEdge* e = item.second;
2134 95115 : if (nc.retrieve(e->getFromNode()->getID()) == nullptr) {
2135 4 : WRITE_ERRORF(TL("Edge's '%' from-node '%' is not known."), e->getID(), e->getFromNode()->getID());
2136 : ok = false;
2137 : }
2138 95115 : if (nc.retrieve(e->getToNode()->getID()) == nullptr) {
2139 4 : WRITE_ERRORF(TL("Edge's '%' to-node '%' is not known."), e->getID(), e->getToNode()->getID());
2140 : ok = false;
2141 : }
2142 :
2143 : }
2144 1904 : return ok;
2145 : }
2146 :
2147 :
2148 : void
2149 83 : NBEdgeCont::fixSplitCustomLength() {
2150 2235 : for (auto item : myEdges) {
2151 : NBEdge* e = item.second;
2152 2152 : if (e->hasLoadedLength() && myWasSplit.count(e) != 0) {
2153 : // subtract half the length of the longest incoming / outgoing connection
2154 : double maxLengthOut = 0;
2155 5 : for (const NBEdge::Connection& c : e->getConnections()) {
2156 3 : maxLengthOut = MAX2(maxLengthOut, c.length + c.viaLength);
2157 : }
2158 : double maxLengthIn = 0;
2159 4 : for (const NBEdge* in : e->getIncomingEdges()) {
2160 7 : for (const NBEdge::Connection& c : in->getConnectionsFromLane(-1, e, -1)) {
2161 5 : maxLengthIn = MAX2(maxLengthIn, c.length + c.viaLength);
2162 2 : }
2163 2 : }
2164 4 : e->setLoadedLength(MAX2(POSITION_EPS, e->getLoadedLength() - (maxLengthIn + maxLengthOut) / 2));
2165 : }
2166 : }
2167 83 : }
2168 :
2169 : void
2170 156 : NBEdgeCont::computeAngles() {
2171 25308 : for (auto item : myEdges) {
2172 25152 : item.second->computeAngle();
2173 : }
2174 156 : }
2175 :
2176 :
2177 : std::set<std::string>
2178 1742 : NBEdgeCont::getUsedTypes() const {
2179 : std::set<std::string> result;
2180 102439 : for (auto item : myEdges) {
2181 100697 : if (item.second->getTypeID() != "") {
2182 : result.insert(item.second->getTypeID());
2183 : }
2184 : }
2185 1742 : return result;
2186 : }
2187 :
2188 :
2189 : int
2190 4 : NBEdgeCont::removeEdgesBySpeed(NBDistrictCont& dc) {
2191 : EdgeSet toRemove;
2192 84 : for (auto item : myEdges) {
2193 80 : NBEdge* edge = item.second;
2194 : // remove edges which allow a speed below a set one (set using "keep-edges.min-speed")
2195 80 : if (edge->getSpeed() < myEdgesMinSpeed) {
2196 : toRemove.insert(edge);
2197 : }
2198 : }
2199 : int numRemoved = 0;
2200 4 : for (NBEdge* edge : toRemove) {
2201 : // explicit whitelist overrides removal
2202 0 : if (myEdges2Keep.size() == 0 || myEdges2Keep.count(edge->getID()) == 0) {
2203 0 : extract(dc, edge);
2204 0 : numRemoved++;
2205 : }
2206 : }
2207 4 : return numRemoved;
2208 : }
2209 :
2210 :
2211 : int
2212 4 : NBEdgeCont::removeEdgesByPermissions(NBDistrictCont& dc) {
2213 : EdgeSet toRemove;
2214 84 : for (auto item : myEdges) {
2215 80 : NBEdge* edge = item.second;
2216 : // check whether the edge shall be removed because it does not allow any of the wished classes
2217 80 : if (myVehicleClasses2Keep != 0 && (myVehicleClasses2Keep & edge->getPermissions()) == 0) {
2218 : toRemove.insert(edge);
2219 : }
2220 : // check whether the edge shall be removed due to allowing unwished classes only
2221 80 : if (myVehicleClasses2Remove != 0 && (myVehicleClasses2Remove | edge->getPermissions()) == myVehicleClasses2Remove) {
2222 : toRemove.insert(edge);
2223 : }
2224 : }
2225 : int numRemoved = 0;
2226 29 : for (NBEdge* edge : toRemove) {
2227 : // explicit whitelist overrides removal
2228 25 : if (myEdges2Keep.size() == 0 || myEdges2Keep.count(edge->getID()) == 0) {
2229 25 : extract(dc, edge);
2230 25 : numRemoved++;
2231 : }
2232 : }
2233 4 : return numRemoved;
2234 : }
2235 :
2236 :
2237 : int
2238 1813 : NBEdgeCont::removeLanesByWidth(NBDistrictCont& dc, const double minWidth) {
2239 : EdgeSet toRemove;
2240 111384 : for (auto item : myEdges) {
2241 109571 : NBEdge* const edge = item.second;
2242 : std::vector<int> indices;
2243 109571 : int idx = 0;
2244 253234 : for (const auto& lane : edge->getLanes()) {
2245 143663 : if (lane.width != NBEdge::UNSPECIFIED_WIDTH && lane.width < minWidth) {
2246 4 : indices.push_back(idx);
2247 : }
2248 143663 : idx++;
2249 : }
2250 109571 : if ((int)indices.size() == edge->getNumLanes()) {
2251 : toRemove.insert(edge);
2252 : } else {
2253 : std::reverse(indices.begin(), indices.end());
2254 109575 : for (const int i : indices) {
2255 4 : edge->deleteLane(i, false, true);
2256 : }
2257 : }
2258 109571 : }
2259 : int numRemoved = 0;
2260 1813 : for (NBEdge* edge : toRemove) {
2261 : // explicit whitelist overrides removal
2262 0 : if (myEdges2Keep.size() == 0 || myEdges2Keep.count(edge->getID()) == 0) {
2263 0 : extract(dc, edge);
2264 0 : numRemoved++;
2265 : }
2266 : }
2267 1813 : return numRemoved;
2268 : }
2269 :
2270 :
2271 : int
2272 1 : NBEdgeCont::attachRemoved(NBNodeCont& nc, NBDistrictCont& dc, const double maxDist) {
2273 : int numSplit = 0;
2274 : std::map<std::string, std::vector<std::string> > node2edge;
2275 172 : for (auto item : myEdges) {
2276 171 : if (item.second->hasParameter(SUMO_PARAM_REMOVED_NODES)) {
2277 45 : for (std::string& nodeID : StringTokenizer(item.second->getParameter(SUMO_PARAM_REMOVED_NODES)).getVector()) {
2278 19 : node2edge[nodeID].push_back(item.first);
2279 13 : }
2280 : }
2281 : }
2282 84 : for (auto item : nc) {
2283 : NBNode* n = item.second;
2284 : auto itRN = node2edge.find(n->getID());
2285 83 : if (itRN != node2edge.end()) {
2286 : bool rebuildConnections = false;
2287 : // make a copy because we modify the original
2288 6 : std::vector<std::string> edgeIDs = itRN->second;
2289 20 : for (const std::string& eID : edgeIDs) {
2290 14 : NBEdge* edge = retrieve(eID);
2291 : assert(edge != nullptr);
2292 14 : const double dist = edge->getGeometry().distance2D(n->getPosition(), true);
2293 14 : if (dist != GeomHelper::INVALID_OFFSET && dist <= maxDist) {
2294 12 : std::string idAfter = edge->getID();
2295 12 : int index = 1;
2296 : size_t spos = idAfter.find("#");
2297 12 : if (spos != std::string::npos && spos > 1) {
2298 24 : idAfter = idAfter.substr(0, spos);
2299 : }
2300 62 : while (retrieve(idAfter + "#" + toString(index), true) != nullptr) {
2301 19 : index++;
2302 : }
2303 24 : idAfter += "#" + toString(index);
2304 12 : const bool ok = splitAt(dc, edge, n, edge->getID(), idAfter, edge->getNumLanes(), edge->getNumLanes());
2305 12 : if (ok) {
2306 : rebuildConnections = true;
2307 12 : numSplit++;
2308 12 : NBEdge* secondEdge = retrieve(eID); // original was extracted on splitting
2309 44 : for (std::string& nodeID : StringTokenizer(secondEdge->getParameter(SUMO_PARAM_REMOVED_NODES)).getVector()) {
2310 20 : node2edge[nodeID].push_back(idAfter);
2311 12 : }
2312 : }
2313 : }
2314 : }
2315 6 : if (rebuildConnections) {
2316 24 : for (NBEdge* e : n->getIncomingEdges()) {
2317 18 : e->invalidateConnections(true);
2318 : }
2319 : }
2320 6 : }
2321 : }
2322 1 : return numSplit;
2323 : }
2324 :
2325 : /****************************************************************************/
|