Eclipse SUMO - Simulation of Urban MObility
NBPTStopCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 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 /****************************************************************************/
18 // Container for pt stops during the netbuilding process
19 /****************************************************************************/
20 
21 
23 #include <utils/geom/Boundary.h>
25 #include <microsim/MSLane.h>
26 #include "NBPTStopCont.h"
27 #include "NBEdgeCont.h"
28 #include "NBEdge.h"
29 #include "NBNode.h"
30 #include <utils/geom/Position.h>
31 
32 
34  for (auto& myPTStop : myPTStops) {
35  delete myPTStop.second;
36  }
37  myPTStops.clear();
38 }
39 
40 
41 bool
43  std::string id = ptStop->getID();
44  auto i = myPTStops.find(id);
45  if (i != myPTStops.end()) {
46  return false;
47  }
48  myPTStops[id] = ptStop;
49  return true;
50 }
51 
52 
53 NBPTStop*
54 NBPTStopCont::get(std::string id) {
55  if (myPTStops.find(id) != myPTStops.end()) {
56  return myPTStops.find(id)->second;
57  }
58  return nullptr;
59 }
60 
61 
62 void
64  std::vector<NBPTStop*> reverseStops;
65  //first pass localize pt stop at correct side of the street; create stop for opposite side if needed
66  for (auto& myPTStop : myPTStops) {
67 
68  NBPTStop* stop = myPTStop.second;
69 
70  bool multipleStopPositions = stop->getIsMultipleStopPositions();
71  bool platformsDefined = !stop->getPlatformCands().empty();
72  if (!platformsDefined) {
73  //create pt stop for reverse edge if edge exists
74  NBPTStop* reverseStop = getReverseStop(stop, cont);
75  if (reverseStop != nullptr) {
76  reverseStops.push_back(reverseStop);
77  }
78  } else if (multipleStopPositions) {
79  //create pt stop for closest platform at corresponding edge
81 
82  } else {
83  //create pt stop for each side of the street where a platform is defined (create additional pt stop as needed)
84  NBPTStop* additionalStop = assignAndCreatNewPTStopAsNeeded(stop, cont);
85  if (additionalStop != nullptr) {
86  reverseStops.push_back(additionalStop);
87  }
88  }
89  }
90 
91  //insrt new stops if any
92  for (auto& reverseStop : reverseStops) {
93  insert(reverseStop);
94  }
95 }
96 
97 
99  //scnd pass set correct lane
100  for (auto i = myPTStops.begin(); i != myPTStops.end();) {
101  NBPTStop* stop = i->second;
102 
103  if (!stop->findLaneAndComputeBusStopExtent(cont)) {
104  WRITE_WARNING("Could not find corresponding edge or compatible lane for pt stop '" + i->first
105  + "' (" + i->second->getName() + "). Thus, it will be removed!");
106  EdgeVector edgeVector = cont.getGeneratedFrom((*i).second->getOrigEdgeId());
107  //std::cout << edgeVector.size() << std::endl;
108  myPTStops.erase(i++);
109  } else {
110  i++;
111  }
112  }
113 }
114 
115 
116 int
118  //scnd pass set correct lane
119  std::vector<NBPTStop*> toAdd;
120  for (auto i = myPTStops.begin(); i != myPTStops.end(); i++) {
121  NBPTStop* stop = i->second;
122  NBEdge* edge = ec.getByID(stop->getEdgeId());
123  if (edge != nullptr && edge->isBidiRail()) {
124  NBEdge* bidiEdge = edge->getTurnDestination(true);
125  assert(bidiEdge != 0);
126  const std::string id = getReverseID(stop->getID());
127  if (myPTStops.count(id) > 0) {
128  if (myPTStops[id]->getEdgeId() != bidiEdge->getID()) {
129  WRITE_WARNING("Could not create reverse-direction stop for superposed edge '" + bidiEdge->getID()
130  + "' (origStop '" + i->first + "'). Stop id '" + id
131  + "' already in use by stop on edge '" + myPTStops[id]->getEdgeId() + "'.");
132  }
133  continue;
134  }
135  NBPTStop* bidiStop = new NBPTStop(id,
136  stop->getPosition(),
137  bidiEdge->getID(),
138  stop->getOrigEdgeId(),
139  stop->getLength(),
140  stop->getName(),
141  stop->getPermissions());
142  if (bidiStop->findLaneAndComputeBusStopExtent(ec)) {
143  toAdd.push_back(bidiStop);
144  stop->setBidiStop(bidiStop);
145  bidiStop->setBidiStop(stop);
146  } else {
147  // should not happen
148  assert(false);
149  }
150  }
151  }
152  for (NBPTStop* newStop : toAdd) {
153  myPTStops[newStop->getID()] = newStop;
154  }
155  if (toAdd.size() > 0) {
156  WRITE_MESSAGE("Added " + toString(toAdd.size()) + " stops for superposed rail edges.");
157  }
158  return (int)toAdd.size();
159 }
160 
161 
162 NBPTStop*
164  std::string edgeId = pStop->getEdgeId();
165  NBEdge* edge = cont.getByID(edgeId);
166  NBEdge* reverse = NBPTStopCont::getReverseEdge(edge);
167  if (reverse != nullptr) {
168  const std::string reverseID = getReverseID(pStop->getID());
169  if (myPTStops.count(reverseID) == 0) {
170  return new NBPTStop(reverseID, pStop->getPosition(), reverse->getID(), reverse->getID(),
171  pStop->getLength(), pStop->getName(), pStop->getPermissions());
172  } else {
173  return myPTStops[reverseID];
174  }
175  }
176  return nullptr;
177 }
178 
179 
180 NBPTStop*
182  std::string edgeId = pStop->getEdgeId();
183  NBEdge* edge = cont.getByID(edgeId);
184  bool rightOfEdge = false;
185  bool leftOfEdge = false;
186  const NBPTPlatform* left = nullptr;
187  for (const NBPTPlatform& platform : pStop->getPlatformCands()) {
188  double crossProd = computeCrossProductEdgePosition(edge, platform.getPos());
189  //TODO consider driving on the left!!! [GL May '17]
190  if (crossProd > 0) {
191  leftOfEdge = true;
192  left = &platform;
193  } else {
194  rightOfEdge = true;
195  pStop->setMyPTStopLength(platform.getLength());
196  }
197  }
198 
199  if (leftOfEdge && rightOfEdge) {
200  NBPTStop* leftStop = getReverseStop(pStop, cont);
201  leftStop->setMyPTStopLength(left->getLength());
202  return leftStop;
203  } else if (leftOfEdge) {
204  NBEdge* reverse = getReverseEdge(edge);
205  if (reverse != nullptr) {
206  pStop->setEdgeId(reverse->getID(), cont);
207  pStop->setMyPTStopLength(left->getLength());
208  }
209  }
210 
211  return nullptr;
212 }
213 
214 
215 void
217  std::string edgeId = pStop->getEdgeId();
218  NBEdge* edge = cont.getByID(edgeId);
219  NBEdge* reverse = NBPTStopCont::getReverseEdge(edge);
220  const NBPTPlatform* closestPlatform = getClosestPlatformToPTStopPosition(pStop);
221  pStop->setMyPTStopLength(closestPlatform->getLength());
222  if (reverse != nullptr) {
223 
224  //TODO make isLeft in PositionVector static [GL May '17]
225 // if (PositionVector::isLeft(edge->getFromNode()->getPosition(),edge->getToNode()->getPosition(),closestPlatform)){
226 //
227 // }
228  double crossProd = computeCrossProductEdgePosition(edge, closestPlatform->getPos());
229 
230  //TODO consider driving on the left!!! [GL May '17]
231  if (crossProd > 0) { //pt stop is on the left of the orig edge
232  pStop->setEdgeId(reverse->getID(), cont);
233  }
234  }
235 }
236 
237 
238 double
239 NBPTStopCont::computeCrossProductEdgePosition(const NBEdge* edge, const Position& closestPlatform) const {
240  PositionVector geom = edge->getGeometry();
241  int idxTmp = geom.indexOfClosest(closestPlatform);
242  double offset = geom.nearest_offset_to_point2D(closestPlatform, true);
243  double offset2 = geom.offsetAtIndex2D(idxTmp);
244  int idx1, idx2;
245  if (offset2 < offset) {
246  idx1 = idxTmp;
247  idx2 = idx1 + 1;
248  } else {
249  idx2 = idxTmp;
250  idx1 = idxTmp - 1;
251  }
252  if (idx1 < 0 || idx1 >= (int) geom.size() || idx2 < 0 || idx2 >= (int) geom.size()) {
253  WRITE_WARNING("Could not determine cross product");
254  return 0;
255  }
256  Position p1 = geom[idx1];
257  Position p2 = geom[idx2];
258 
259  double x0 = p1.x();
260  double y0 = p1.y();
261  double x1 = p2.x();
262  double y1 = p2.y();
263  double x2 = closestPlatform.x();
264  double y2 = closestPlatform.y();
265  double crossProd = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
266  return crossProd;
267 }
268 
269 
270 const NBPTPlatform*
272  Position stopPosition = pStop->getPosition();
273  const NBPTPlatform* closest = nullptr;
274  double minSqrDist = std::numeric_limits<double>::max();
275  for (const NBPTPlatform& platform : pStop->getPlatformCands()) {
276  double sqrDist = stopPosition.distanceSquaredTo2D(platform.getPos());
277  if (sqrDist < minSqrDist) {
278  minSqrDist = sqrDist;
279  closest = &platform;
280  }
281  }
282  return closest;
283 }
284 
285 //static functions
286 
287 NBEdge*
289  if (edge != nullptr) {
290  for (auto it = edge->getToNode()->getOutgoingEdges().begin();
291  it != edge->getToNode()->getOutgoingEdges().end();
292  it++) {
293  if ((*it)->getToNode() == edge->getFromNode()) {
294  return (*it);
295  }
296  }
297  }
298  return nullptr;
299 }
300 
301 
302 int
304  int numDeleted = 0;
305  for (auto i = myPTStops.begin(); i != myPTStops.end();) {
306  if (cont.getByID((*i).second->getEdgeId()) == nullptr) {
307  WRITE_WARNING("Removing pt stop:" + (*i).first + " on non existing edge: " + (*i).second->getEdgeId());
308  myPTStops.erase(i++);
309  numDeleted++;
310  } else {
311  i++;
312  }
313  }
314  return numDeleted;
315 }
316 
317 
318 void
319 NBPTStopCont::addEdges2Keep(const OptionsCont& oc, std::set<std::string>& into) {
320  if (oc.isSet("ptstop-output")) {
321  for (auto stop : myPTStops) {
322  into.insert(stop.second->getEdgeId());
323  }
324  }
325 }
326 
327 
328 void
329 NBPTStopCont::postprocess(std::set<std::string>& usedStops) {
330  for (auto i = myPTStops.begin(); i != myPTStops.end();) {
331  if (usedStops.find(i->second->getID()) == usedStops.end()) {
332  myPTStops.erase(i++);
333  } else {
334  i++;
335  }
336  }
337 }
338 
339 std::string
340 NBPTStopCont::getReverseID(const std::string& id) {
341  return id.size() > 0 && id[0] == '-' ? id.substr(1) : "-" + id;
342 }
343 
344 void
346  PTStopsCont stops = myPTStops;
347  for (auto& i : stops) {
348  const std::string& stopId = i.second->getID();
349  if (i.second->getEdgeId() == "") {
350  continue;
351  }
352  const char edgeSign = i.second->getEdgeId().at(0);
353  const char stopSign = stopId.at(0);
354  if (edgeSign != stopSign && (edgeSign == '-' || stopSign == '-')) {
355  i.second->setMyPTStopId(getReverseID(stopId));
356  myPTStops.erase(stopId);
357  myPTStops[i.second->getID()] = i.second;
358  }
359  }
360 }
361 
362 
363 void
364 NBPTStopCont::findAccessEdgesForRailStops(NBEdgeCont& cont, double maxRadius, int maxCount, double accessFactor) {
365  NamedRTree r;
366  for (auto edge : cont) {
367  const Boundary& bound = edge.second->getGeometry().getBoxBoundary();
368  float min[2] = { static_cast<float>(bound.xmin()), static_cast<float>(bound.ymin()) };
369  float max[2] = { static_cast<float>(bound.xmax()), static_cast<float>(bound.ymax()) };
370  r.Insert(min, max, edge.second);
371  }
372  for (auto& ptStop : myPTStops) {
373  const std::string& stopEdgeID = ptStop.second->getEdgeId();
374  NBEdge* stopEdge = cont.getByID(stopEdgeID);
375  //std::cout << "findAccessEdgesForRailStops edge=" << stopEdgeID << " exists=" << (stopEdge != 0) << "\n";
376  if (stopEdge != nullptr && (stopEdge->getPermissions() & SVC_PEDESTRIAN) == 0) {
377  //if (stopEdge != 0 && isRailway(stopEdge->getPermissions())) {
378  std::set<const Named*> edges;
379  Named::StoringVisitor visitor(edges);
380  const Position& pos = ptStop.second->getPosition();
381  float min[2] = {static_cast<float>(pos.x() - maxRadius), static_cast<float>(pos.y() - maxRadius)};
382  float max[2] = {static_cast<float>(pos.x() + maxRadius), static_cast<float>(pos.y() + maxRadius)};
383  r.Search(min, max, visitor);
384  std::vector<NBEdge*> edgCants;
385  for (const Named* namedEdge : edges) {
386  NBEdge* e = const_cast<NBEdge*>(dynamic_cast<const NBEdge*>(namedEdge));
387  edgCants.push_back(e);
388  }
389  std::sort(edgCants.begin(), edgCants.end(), [pos](NBEdge * a, NBEdge * b) {
390  return a->getLaneShape(0).distance2D(pos, false) < b->getLaneShape(0).distance2D(pos, false);
391  });
392  int cnt = 0;
393  for (auto edge : edgCants) {
394  int laneIdx = 0;
395  for (auto lane : edge->getLanes()) {
396  if ((lane.permissions & SVC_PEDESTRIAN) != 0) {
397  double offset = lane.shape.nearest_offset_to_point2D(pos, false);
398  double finalLength = edge->getFinalLength();
399  double laneLength = lane.shape.length();
400  double accessLength = pos.distanceTo2D(lane.shape.positionAtOffset2D(offset)) * accessFactor;
401  ptStop.second->addAccess(edge->getLaneID(laneIdx), offset * finalLength / laneLength, accessLength);
402  cnt++;
403  break;
404  }
405  laneIdx++;
406  }
407  if (cnt == maxCount) {
408  break;
409  }
410  }
411  }
412  }
413 }
414 
415 
416 NBPTStop*
417 NBPTStopCont::findStop(const std::string& origEdgeID, Position pos, double threshold) const {
418  for (auto& item : myPTStops) {
419  if (item.second->getOrigEdgeId() == origEdgeID &&
420  item.second->getPosition().distanceTo2D(pos) < threshold) {
421  return item.second;
422  }
423  }
424  return nullptr;
425 }
426 
427 
428 /****************************************************************************/
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:278
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
@ SVC_PEDESTRIAN
pedestrian
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:129
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:117
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:135
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:123
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:59
NBEdge * getByID(const std::string &edgeID) const
Returns the edge with id if it exists.
EdgeVector getGeneratedFrom(const std::string &id) const
Returns the edges which have been built by splitting the edge of the given id.
The representation of a single edge during network building.
Definition: NBEdge.h:91
SVCPermissions getPermissions(int lane=-1) const
get the union of allowed classes over all lanes or for a specific lane
Definition: NBEdge.cpp:3655
bool isBidiRail(bool ignoreSpread=false) const
whether this edge is part of a bidirectional railway
Definition: NBEdge.cpp:713
const std::string & getID() const
Definition: NBEdge.h:1423
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:516
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:716
NBEdge * getTurnDestination(bool possibleDestination=false) const
Definition: NBEdge.cpp:3336
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:509
const EdgeVector & getOutgoingEdges() const
Returns this node's outgoing edges (The edges which start at this node)
Definition: NBNode.h:259
double getLength() const
const Position & getPos() const
bool insert(NBPTStop *ptStop)
Inserts a node into the map.
static std::string getReverseID(const std::string &id)
int cleanupDeleted(NBEdgeCont &cont)
remove stops on non existing (removed) edges
static NBEdge * getReverseEdge(NBEdge *edge)
double computeCrossProductEdgePosition(const NBEdge *edge, const Position &closestPlatform) const
void postprocess(std::set< std::string > &usedStops)
NBPTStop * findStop(const std::string &origEdgeID, Position pos, double threshold=1) const
void addEdges2Keep(const OptionsCont &oc, std::set< std::string > &into)
add edges that must be kept
NBPTStop * get(std::string id)
Retrieve a previously inserted pt stop.
std::map< std::string, NBPTStop * > PTStopsCont
Definition of the map of names to pt stops.
Definition: NBPTStopCont.h:93
PTStopsCont myPTStops
The map of names to pt stops.
Definition: NBPTStopCont.h:96
const NBPTPlatform * getClosestPlatformToPTStopPosition(NBPTStop *pStop)
void localizePTStops(NBEdgeCont &cont)
void alignIdSigns()
NBPTStop * getReverseStop(NBPTStop *pStop, NBEdgeCont &cont)
void findAccessEdgesForRailStops(NBEdgeCont &cont, double maxRadius, int maxCount, double accessFactor)
int generateBidiStops(NBEdgeCont &cont)
duplicate stops for superposed rail edges and return the number of generated stops
void assignLanes(NBEdgeCont &cont)
void assignPTStopToEdgeOfClosestPlatform(NBPTStop *pStop, NBEdgeCont &cont)
NBPTStop * assignAndCreatNewPTStopAsNeeded(NBPTStop *pStop, NBEdgeCont &cont)
The representation of a single pt stop.
Definition: NBPTStop.h:44
bool findLaneAndComputeBusStopExtent(const NBEdgeCont &ec)
Definition: NBPTStop.cpp:206
bool setEdgeId(std::string edgeId, const NBEdgeCont &ec)
Definition: NBPTStop.cpp:176
const std::string getEdgeId() const
Definition: NBPTStop.cpp:61
std::string getID() const
Definition: NBPTStop.cpp:50
bool getIsMultipleStopPositions() const
Definition: NBPTStop.cpp:158
const std::vector< NBPTPlatform > & getPlatformCands()
Definition: NBPTStop.cpp:152
double getLength() const
Definition: NBPTStop.cpp:170
void setBidiStop(NBPTStop *bidiStop)
Definition: NBPTStop.h:85
void setMyPTStopLength(double myPTStopLength)
Definition: NBPTStop.cpp:201
SVCPermissions getPermissions() const
Definition: NBPTStop.cpp:140
const Position & getPosition() const
Definition: NBPTStop.cpp:73
const std::string getOrigEdgeId() const
Definition: NBPTStop.cpp:55
const std::string getName() const
Definition: NBPTStop.cpp:67
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:89
Base class for objects which have an id.
Definition: Named.h:53
A RT-tree for efficient storing of SUMO's Named objects.
Definition: NamedRTree.h:60
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:78
int Search(const float a_min[2], const float a_max[2], const Named::StoringVisitor &c) const
Find all within search rectangle.
Definition: NamedRTree.h:111
A storage for options typed value containers)
Definition: OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
double distanceSquaredTo2D(const Position &p2) const
returns the square of the distance to another position (Only using x and y positions)
Definition: Position.h:246
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:241
double x() const
Returns the x-position.
Definition: Position.h:54
double y() const
Returns the y-position.
Definition: Position.h:59
A list of positions.
double nearest_offset_to_point2D(const Position &p, bool perpendicular=true) const
return the nearest offest to point 2D
double offsetAtIndex2D(int index) const
return the offset at the given index
int indexOfClosest(const Position &p) const
index of the closest position to p