Eclipse SUMO - Simulation of Urban MObility
NBPTStop.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-2022 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 // The representation of a single pt stop
19 /****************************************************************************/
20 #include <config.h>
21 
24 #include "NBPTStop.h"
25 #include "NBEdge.h"
26 #include "NBEdgeCont.h"
27 
28 
29 // ===========================================================================
30 // method definitions
31 // ===========================================================================
32 NBPTStop::NBPTStop(std::string ptStopId, Position position, std::string edgeId, std::string origEdgeId, double length,
33  std::string name, SVCPermissions svcPermissions, double parkingLength, const RGBColor color) :
34  myPTStopId(ptStopId),
35  myPosition(position),
36  myEdgeId(edgeId),
37  myOrigEdgeId(origEdgeId),
38  myPTStopLength(length),
39  myName(name),
40  myParkingLength(parkingLength),
41  myColor(color),
42  myPermissions(svcPermissions),
43  myStartPos(0),
44  myEndPos(0),
45  myBidiStop(nullptr),
46  myIsLoose(origEdgeId == ""),
47  myIsPlatform(false),
48  myIsMultipleStopPositions(false),
49  myAreaID(-1) {
50 }
51 
52 
53 std::string
54 NBPTStop::getID() const {
55  return myPTStopId;
56 }
57 
58 
59 const std::string
61  return myOrigEdgeId;
62 }
63 
64 
65 const std::string
67  return myEdgeId;
68 }
69 
70 
71 const std::string
73  return myName;
74 }
75 
76 
77 const Position&
79  return myPosition;
80 }
81 
82 
83 void
85  myPosition.mul(1, -1);
86 }
87 
88 
89 void
90 NBPTStop::computeExtent(double center, double edgeLength) {
91  myStartPos = MAX2(0.0, center - myPTStopLength / 2.);
92  myEndPos = MIN2(center + myPTStopLength / 2., edgeLength);
93 }
94 
95 
96 void
97 NBPTStop::addLine(const std::string& line) {
98  const std::string l = StringUtils::escapeXML(line);
99  if (std::find(myLines.begin(), myLines.end(), l) == myLines.end()) {
100  myLines.push_back(l);
101  }
102 }
103 
104 
105 void
107  device.openTag(SUMO_TAG_BUS_STOP);
109  if (!myName.empty()) {
111  }
115  device.writeAttr(SUMO_ATTR_FRIENDLY_POS, "true");
116  if (myLines.size() > 0) {
118  }
119  if (myParkingLength > 0) {
121  }
122  if (myColor.isValid()) {
124  }
125  if (!myAccesses.empty()) {
126  std::sort(myAccesses.begin(), myAccesses.end());
127  for (auto tuple : myAccesses) {
128  device.openTag(SUMO_TAG_ACCESS);
129  device.writeAttr(SUMO_ATTR_LANE, std::get<0>(tuple));
130  device.writeAttr(SUMO_ATTR_POSITION, std::get<1>(tuple));
131  device.writeAttr(SUMO_ATTR_LENGTH, std::get<2>(tuple));
132  device.writeAttr(SUMO_ATTR_FRIENDLY_POS, true);
133  device.closeTag();
134  }
135  }
136  writeParams(device);
137  device.closeTag();
138 }
139 
140 
141 void
142 NBPTStop::reshiftPosition(const double offsetX, const double offsetY) {
143  myPosition.add(offsetX, offsetY, 0);
144  for (NBPTPlatform& platformCand : myPlatformCands) {
145  platformCand.reshiftPosition(offsetX, offsetY);
146  }
147 }
148 
149 
152  return myPermissions;
153 }
154 
155 
156 void
158  myPlatformCands.push_back(platform);
159 }
160 
161 
162 const std::vector<NBPTPlatform>&
164  return myPlatformCands;
165 }
166 
167 
168 bool
171 }
172 
173 
174 void
175 NBPTStop::setIsMultipleStopPositions(bool multipleStopPositions, long long int areaID) {
176  myIsMultipleStopPositions = multipleStopPositions;
177  myAreaID = areaID;
178 }
179 
180 
181 double
183  return myPTStopLength;
184 }
185 
186 
187 bool
188 NBPTStop::setEdgeId(std::string edgeId, const NBEdgeCont& ec) {
189  myEdgeId = edgeId;
191 }
192 
193 
194 void
195 NBPTStop::registerAdditionalEdge(std::string wayId, std::string edgeId) {
196  myAdditionalEdgeCandidates[wayId] = edgeId;
197 }
198 
199 
200 bool
202  NBEdge* edge = ec.getByID(myEdgeId);
203  return findLaneAndComputeBusStopExtent(edge);
204 }
205 
206 bool
208  if (edge != nullptr) {
209  myEdgeId = edge->getID();
210  int laneNr = -1;
211  for (const auto& it : edge->getLanes()) {
212  if ((it.permissions & getPermissions()) == getPermissions()) {
213  ++laneNr;
214  break;
215  }
216  laneNr++;
217  }
218  if (laneNr != -1) {
219  myLaneId = edge->getLaneID(laneNr);
220  const PositionVector& shape = edge->getLaneShape(laneNr);
221  double offset = shape.nearest_offset_to_point2D(getPosition(), false);
222  offset = offset * edge->getLoadedLength() / edge->getLength();
223  computeExtent(offset, edge->getLoadedLength());
224  return true;
225  }
226  }
227  return myEdgeId == ""; // loose stop. Try later when processing lines
228 }
229 
230 
231 void
233  myAccesses.clear();
234 }
235 
236 void
237 NBPTStop::addAccess(std::string laneID, double offset, double length) {
238  const std::string newEdgeID = SUMOXMLDefinitions::getEdgeIDFromLane(laneID);
239  // avoid duplicate access
240  for (auto it = myAccesses.begin(); it != myAccesses.end();) {
241  if (SUMOXMLDefinitions::getEdgeIDFromLane(std::get<0>(*it)) == newEdgeID) {
242  it = myAccesses.erase(it);
243  } else {
244  it++;
245  }
246  }
247  myAccesses.push_back(std::make_tuple(laneID, offset, length));
248 }
249 
250 
251 bool
252 NBPTStop::replaceEdge(const std::string& edgeID, const EdgeVector& replacement) {
253  if (myEdgeId == edgeID) {
254  // find best edge among replacement edges
255  double bestDist = std::numeric_limits<double>::max();
256  NBEdge* bestEdge = nullptr;
257  for (NBEdge* cand : replacement) {
258  double dist = cand->getGeometry().distance2D(myPosition);
259  if (dist < bestDist) {
260  bestDist = dist;
261  bestEdge = cand;
262  }
263  }
264  if (bestDist != std::numeric_limits<double>::max()) {
265  if ((bestEdge->getPermissions() & SVC_PEDESTRIAN) != 0) {
266  // no need for access
267  clearAccess();
268  }
269  return findLaneAndComputeBusStopExtent(bestEdge);
270  } else {
271  return false;
272  }
273  }
274  return true;
275 }
276 
277 /****************************************************************************/
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:35
@ SVC_PEDESTRIAN
pedestrian
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
@ SUMO_TAG_ACCESS
An access point for a train stop.
@ SUMO_TAG_BUS_STOP
A bus stop.
@ SUMO_ATTR_STARTPOS
@ SUMO_ATTR_LINES
@ SUMO_ATTR_LANE
@ SUMO_ATTR_PARKING_LENGTH
@ SUMO_ATTR_ENDPOS
@ SUMO_ATTR_NAME
@ SUMO_ATTR_FRIENDLY_POS
@ SUMO_ATTR_LENGTH
@ SUMO_ATTR_COLOR
A color information.
@ SUMO_ATTR_ID
@ SUMO_ATTR_POSITION
T MIN2(T a, T b)
Definition: StdDefs.h:74
T MAX2(T a, T b)
Definition: StdDefs.h:80
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
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.
The representation of a single edge during network building.
Definition: NBEdge.h:91
double getLength() const
Returns the computed length of the edge.
Definition: NBEdge.h:588
SVCPermissions getPermissions(int lane=-1) const
get the union of allowed classes over all lanes or for a specific lane
Definition: NBEdge.cpp:4022
double getLoadedLength() const
Returns the length was set explicitly or the computed length if it wasn't set.
Definition: NBEdge.h:597
const std::string & getID() const
Definition: NBEdge.h:1465
const std::vector< NBEdge::Lane > & getLanes() const
Returns the lane definitions.
Definition: NBEdge.h:701
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:752
std::string getLaneID(int lane) const
get lane ID
Definition: NBEdge.cpp:3693
const PositionVector & getLaneShape(int i) const
Returns the shape of the nth lane.
Definition: NBEdge.cpp:930
bool myIsMultipleStopPositions
Definition: NBPTStop.h:189
std::map< std::string, std::string > myAdditionalEdgeCandidates
Definition: NBPTStop.h:162
double myStartPos
Definition: NBPTStop.h:171
void registerAdditionalEdge(std::string wayId, std::string edgeId)
Definition: NBPTStop.cpp:195
const SVCPermissions myPermissions
Definition: NBPTStop.h:169
double myPTStopLength
Definition: NBPTStop.h:164
bool findLaneAndComputeBusStopExtent(const NBEdgeCont &ec)
Definition: NBPTStop.cpp:201
void addPlatformCand(NBPTPlatform platform)
Definition: NBPTStop.cpp:157
void clearAccess()
remove all access definitions
Definition: NBPTStop.cpp:232
std::string myLaneId
Definition: NBPTStop.h:168
std::string myPTStopId
Definition: NBPTStop.h:159
std::vector< NBPTPlatform > myPlatformCands
Definition: NBPTStop.h:188
std::vector< std::string > myLines
list of public transport lines (for displaying)
Definition: NBPTStop.h:178
bool setEdgeId(std::string edgeId, const NBEdgeCont &ec)
Definition: NBPTStop.cpp:188
const std::string getEdgeId() const
Definition: NBPTStop.cpp:66
bool replaceEdge(const std::string &edgeID, const EdgeVector &replacement)
replace the stop edge with the closest edge on the given edge list in all stops
Definition: NBPTStop.cpp:252
void computeExtent(double center, double d)
Definition: NBPTStop.cpp:90
std::string getID() const
Definition: NBPTStop.cpp:54
void mirrorX()
mirror coordinates along the x-axis
Definition: NBPTStop.cpp:84
bool getIsMultipleStopPositions() const
Definition: NBPTStop.cpp:169
NBPTStop(std::string ptStopId, Position position, std::string edgeId, std::string origEdgeId, double length, std::string name, SVCPermissions svcPermissions, double parkingLength=0, const RGBColor color=RGBColor(false))
Constructor.
Definition: NBPTStop.cpp:32
void addAccess(std::string laneID, double offset, double length)
Definition: NBPTStop.cpp:237
void write(OutputDevice &device)
Definition: NBPTStop.cpp:106
const std::vector< NBPTPlatform > & getPlatformCands()
Definition: NBPTStop.cpp:163
const std::string myName
Definition: NBPTStop.h:165
std::vector< std::tuple< std::string, double, double > > myAccesses
laneId, lanePos, accessLength
Definition: NBPTStop.h:175
Position myPosition
Definition: NBPTStop.h:160
void addLine(const std::string &line)
register line that services this stop (for displaying)
Definition: NBPTStop.cpp:97
double getLength() const
Definition: NBPTStop.cpp:182
void reshiftPosition(const double offsetX, const double offsetY)
Definition: NBPTStop.cpp:142
double myEndPos
Definition: NBPTStop.h:172
void setIsMultipleStopPositions(bool multipleStopPositions, long long int areaID)
Definition: NBPTStop.cpp:175
SVCPermissions getPermissions() const
Definition: NBPTStop.cpp:151
const double myParkingLength
Definition: NBPTStop.h:166
const Position & getPosition() const
Definition: NBPTStop.cpp:78
const RGBColor myColor
Definition: NBPTStop.h:167
std::string myOrigEdgeId
Definition: NBPTStop.h:163
long long int myAreaID
Definition: NBPTStop.h:190
const std::string getOrigEdgeId() const
Definition: NBPTStop.cpp:60
const std::string getName() const
Definition: NBPTStop.cpp:72
std::string myEdgeId
Definition: NBPTStop.h:161
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:248
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:125
void mul(double val)
Multiplies both positions with the given value.
Definition: Position.h:105
A list of positions.
double distance2D(const Position &p, bool perpendicular=false) const
closest 2D-distance to point p (or -1 if perpendicular is true and the point is beyond this vector)
double nearest_offset_to_point2D(const Position &p, bool perpendicular=true) const
return the nearest offest to point 2D
bool isValid() const
check if RGBColor is valid
Definition: RGBColor.cpp:120
static std::string getEdgeIDFromLane(const std::string laneID)
return edge id when given the lane ID
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.