SUMO - Simulation of Urban MObility
NBPTStopCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // Container for pt stops during the netbuilding process
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
23 #include <microsim/MSLane.h>
24 #include "NBPTStopCont.h"
25 #include "NBEdgeCont.h"
26 #include "NBEdge.h"
27 #include "NBNode.h"
28 #include <utils/geom/Position.h>
29 
30 
32  for (auto& myPTStop : myPTStops) {
33  delete myPTStop.second;
34  }
35  myPTStops.clear();
36 }
37 
39  std::string id = ptStop->getID();
40  auto i = myPTStops.find(id);
41  if (i != myPTStops.end()) {
42  return false;
43  }
44  myPTStops[id] = ptStop;
45  return true;
46 }
47 
48 NBPTStop* NBPTStopCont::get(std::string id) {
49  if (myPTStops.find(id) != myPTStops.end()) {
50  return myPTStops.find(id)->second;
51  }
52  return nullptr;
53 }
54 
55 
57 
58 // PTStopsCont::iterator end = myPTStops.end();
59 
60  std::vector<NBPTStop*> reverseStops;
61 
62  //frst pass localize pt stop at correct side of the street; create stop for opposite side if needed
63  for (auto& myPTStop : myPTStops) {
64 
65  NBPTStop* stop = myPTStop.second;
66 
67  bool multipleStopPositions = stop->getIsMultipleStopPositions();
68  bool platformsDefined = !stop->getPlatformCands().empty();
69  if (!platformsDefined) {
70  //create pt stop for reverse edge if edge exist
71  NBPTStop* reverseStop = getReverseStop(stop, cont);
72  if (reverseStop != 0) {
73  reverseStops.push_back(reverseStop);
74  }
75  } else if (multipleStopPositions) {
76  //create pt stop for closest platform at corresponding edge
78 
79  } else {
80  //create pt stop for each side of the street where a platform is defined (create additional pt stop as needed)
81  NBPTStop* additionalStop = assignAndCreatNewPTStopAsNeeded(stop, cont);
82  if (additionalStop != 0) {
83  reverseStops.push_back(additionalStop);
84  }
85  }
86  }
87 
88  //insrt new stops if any
89  for (auto& reverseStop : reverseStops) {
90  insert(reverseStop);
91  }
92 
93 
94  //scnd pass set correct lane
95  for (auto i = myPTStops.begin(); i != myPTStops.end();) {
96  NBPTStop* stop = i->second;
98  WRITE_WARNING("Could not find corresponding edge or compatible lane for pt stop: " + i->second->getName()
99  + ". Thus, it will be removed!");
100  EdgeVector edgeVector = cont.getGeneratedFrom((*i).second->getOrigEdgeId());
101  //std::cout << edgeVector.size() << std::endl;
102  myPTStops.erase(i++);
103  } else {
104  i++;
105  };
106  }
107 
108 }
109 
110 
112  std::string edgeId = pStop->getEdgeId();
113  NBEdge* edge = cont.getByID(edgeId);
114  NBEdge* reverse = NBPTStopCont::getReverseEdge(edge);
115  if (reverse != nullptr) {
116 
117  Position* pos = new Position(pStop->getPosition());
118  NBPTStop* ret = new NBPTStop("-1" + pStop->getID(), *pos, reverse->getID(), reverse->getID(),
119  pStop->getLength(), pStop->getName(), pStop->getPermissions());
120  return ret;
121 
122  }
123  return nullptr;
124 }
125 
127  std::string edgeId = pStop->getEdgeId();
128  NBEdge* edge = cont.getByID(edgeId);
129 
130 
131  bool rightOfEdge = false;
132  bool leftOfEdge = false;
133  NBPTPlatform* left = nullptr;
134  for (auto it = pStop->getPlatformCands().begin(); it != pStop->getPlatformCands().end(); it++) {
135  NBPTPlatform* platform = &(*it);
136  double crossProd = computeCrossProductEdgePosition(edge, platform->getMyPos());
137 
138  //TODO consider driving on the left!!! [GL May '17]
139  if (crossProd > 0) {
140  leftOfEdge = true;
141  left = platform;
142  } else {
143  rightOfEdge = true;
144  pStop->setMyPTStopLength(platform->getMyLength());
145  }
146 
147  }
148 
149  if (leftOfEdge && rightOfEdge) {
150  NBPTStop* leftStop = getReverseStop(pStop, cont);
151  leftStop->setMyPTStopLength(left->getMyLength());
152  return leftStop;
153  } else if (leftOfEdge) {
154  NBEdge* reverse = getReverseEdge(edge);
155  if (reverse != nullptr) {
156  pStop->setEdgeId(reverse->getID());
157  pStop->setMyPTStopLength(left->getMyLength());
158  }
159  }
160 
161  return nullptr;
162 }
163 
164 
165 
167  std::string edgeId = pStop->getEdgeId();
168  NBEdge* edge = cont.getByID(edgeId);
169  NBEdge* reverse = NBPTStopCont::getReverseEdge(edge);
170  NBPTPlatform* closestPlatform = getClosestPlatformToPTStopPosition(pStop);
171  pStop->setMyPTStopLength(closestPlatform->getMyLength());
172  if (reverse != 0) {
173 
174  //TODO make isLeft in PositionVector static [GL May '17]
175 // if (PositionVector::isLeft(edge->getFromNode()->getPosition(),edge->getToNode()->getPosition(),closestPlatform)){
176 //
177 // }
178  double crossProd = computeCrossProductEdgePosition(edge, closestPlatform->getMyPos());
179 
180  //TODO consider driving on the left!!! [GL May '17]
181  if (crossProd > 0) { //pt stop is on the left of the orig edge
182  pStop->setEdgeId(reverse->getID());
183  }
184  }
185 
186 
187 }
188 double NBPTStopCont::computeCrossProductEdgePosition(const NBEdge* edge, const Position* closestPlatform) const {
189  PositionVector geom = edge->getGeometry();
190  int idxTmp = geom.indexOfClosest(*closestPlatform);
191  double offset = geom.nearest_offset_to_point2D(*closestPlatform, true);
192  double offset2 = geom.offsetAtIndex2D(idxTmp);
193  int idx1, idx2;
194  if (offset2 < offset) {
195  idx1 = idxTmp;
196  idx2 = idx1 + 1;
197  } else {
198  idx2 = idxTmp;
199  idx1 = idxTmp - 1;
200  }
201  if (idx1 < 0 || idx1 >= (int)geom.size() || idx2 < 0 || idx2 >= (int)geom.size()) {
202  WRITE_WARNING("Could not determine cross product");
203  return 0;
204  }
205  Position p1 = geom[idx1];
206  Position p2 = geom[idx2];
207 
208  double x0 = p1.x();
209  double y0 = p1.y();
210  double x1 = p2.x();
211  double y1 = p2.y();
212  double x2 = closestPlatform->x();
213  double y2 = closestPlatform->y();
214  double crossProd = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
215  return crossProd;
216 }
218 
219  Position stopPosition = pStop->getPosition();
220 
221  NBPTPlatform* closest = nullptr;
222  double minSqrDist = std::numeric_limits<double>::max();
223 
224  for (auto it = pStop->getPlatformCands().begin();
225  it != pStop->getPlatformCands().end();
226  it++) {
227  NBPTPlatform platform = *it;
228  double sqrDist = stopPosition.distanceSquaredTo2D(*platform.getMyPos());
229  if (sqrDist < minSqrDist) {
230  minSqrDist = sqrDist;
231  closest = &(*it);
232  }
233  }
234  return closest;
235 }
236 
237 //static functions
238 
240  std::string edgeId = pStop->getEdgeId();
241  NBEdge* edge = cont.getByID(edgeId);
242  if (edge != nullptr) {
243 
244  int laneNr = -1;
245 
246  for (const auto& it : edge->getLanes()) {
247  if ((it.permissions & pStop->getPermissions()) > 0) {
248  ++laneNr;
249  break;
250  }
251  laneNr++;
252  }
253 
254  if (laneNr != -1) {
255  const std::string& lane = edge->getLaneID(laneNr);
256  pStop->setLaneID(lane);
257  const PositionVector& shape = edge->getLaneShape(laneNr);
258  double offset = shape.nearest_offset_to_point2D(pStop->getPosition(), true);
259  pStop->computExtent(offset, edge->getLength());
260 
261  } else {
262  return false;
263  }
264  } else {
265  return false;
266  }
267  return true;
268 }
269 
271  if (edge != nullptr) {
272  for (auto it = edge->getToNode()->getOutgoingEdges().begin();
273  it != edge->getToNode()->getOutgoingEdges().end();
274  it++) {
275  if ((*it)->getToNode() == edge->getFromNode()) {
276  return (*it);
277  }
278  }
279  }
280  return 0;
281 }
283  for (auto i = myPTStops.begin(); i != myPTStops.end();) {
284  if (cont.getByID((*i).second->getEdgeId()) == nullptr) {
285  WRITE_WARNING("Removing pt stop:" + (*i).first + " on non existing edge: " + (*i).second->getEdgeId());
286  myPTStops.erase(i++);
287  } else {
288  i++;
289  }
290  }
291 
292 }
const std::string getName()
Definition: NBPTStop.cpp:66
void reviseStops(NBEdgeCont &cont)
revise pt stops and remove stops on non existing (removed) edges
PTStopsCont myPTStops
The map of names to pt stops.
Definition: NBPTStopCont.h:78
void assignPTStopToEdgeOfClosestPlatform(NBPTStop *pStop, NBEdgeCont &cont)
double nearest_offset_to_point2D(const Position &p, bool perpendicular=true) const
return the nearest offest to point 2D
const std::string getEdgeId()
Definition: NBPTStop.cpp:61
const std::vector< NBEdge::Lane > & getLanes() const
Returns the lane definitions.
Definition: NBEdge.h:571
double x() const
Returns the x-position.
Definition: Position.h:63
NBPTPlatform * getClosestPlatformToPTStopPosition(NBPTStop *pStop)
The representation of a single edge during network building.
Definition: NBEdge.h:71
NBPTStop * assignAndCreatNewPTStopAsNeeded(NBPTStop *pStop, NBEdgeCont &cont)
The representation of a single pt stop.
Definition: NBPTStop.h:51
static bool findLaneAndComputeBusStopExtend(NBPTStop *pStop, NBEdgeCont &cont)
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
NBEdge * getByID(const std::string &edgeID) const
Returns the edge with id if it exists.
Definition: NBEdgeCont.cpp:842
const EdgeVector & getOutgoingEdges() const
Returns this node&#39;s outgoing edges (The edges which start at this node)
Definition: NBNode.h:248
const std::string & getID() const
Returns the id.
Definition: Named.h:66
#define max(a, b)
Definition: polyfonts.c:65
SVCPermissions getPermissions()
Definition: NBPTStop.cpp:103
std::string getID() const
Definition: NBPTStop.cpp:51
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
int indexOfClosest(const Position &p) const
index of the closest position to p
void computExtent(double center, double d)
Definition: NBPTStop.cpp:73
double y() const
Returns the y-position.
Definition: Position.h:68
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
double distanceSquaredTo2D(const Position &p2) const
returns the square of the distance to another position (Only using x and y positions) ...
Definition: Position.h:255
bool getIsMultipleStopPositions()
Definition: NBPTStop.cpp:112
void process(NBEdgeCont &cont)
void setLaneID(const std::string &laneId)
Definition: NBPTStop.cpp:78
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:435
double getLength() const
Returns the computed length of the edge.
Definition: NBEdge.h:482
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:41
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:596
std::vector< NBPTPlatform > & getPlatformCands()
Definition: NBPTStop.cpp:109
NBPTStop * get(std::string id)
Retrieve a previously inserted pt stop.
double getMyLength() const
NBPTStop * getReverseStop(NBPTStop *pStop, NBEdgeCont &cont)
const Position & getPosition()
Definition: NBPTStop.cpp:69
void setEdgeId(std::string edgeId)
Definition: NBPTStop.cpp:122
std::string getLaneID(int lane) const
get Lane ID (Secure)
Definition: NBEdge.cpp:2746
double offsetAtIndex2D(int index) const
return the offset at the given index
bool insert(NBPTStop *ptStop)
Inserts a node into the map.
EdgeVector getGeneratedFrom(const std::string &id) const
Returns the edges which have been built by splitting the edge of the given id.
Definition: NBEdgeCont.cpp:891
Position * getMyPos()
static NBEdge * getReverseEdge(NBEdge *edge)
double getLength()
Definition: NBPTStop.cpp:119
void setMyPTStopLength(double myPTStopLength)
Definition: NBPTStop.cpp:134
double computeCrossProductEdgePosition(const NBEdge *edge, const Position *closestPlatform) const
const PositionVector & getLaneShape(int i) const
Returns the shape of the nth lane.
Definition: NBEdge.cpp:770
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:428