Eclipse SUMO - Simulation of Urban MObility
NBLoadedSUMOTLDef.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2011-2024 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 /****************************************************************************/
19 // A complete traffic light logic loaded from a sumo-net. (opted to reimplement
20 // since NBLoadedTLDef is quite vissim specific)
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <vector>
26 #include <string>
27 #include <set>
28 #include "NBNode.h"
29 #include "NBEdgeCont.h"
31 #include "NBTrafficLightLogic.h"
32 #include <utils/common/SUMOTime.h>
33 
34 
35 // ===========================================================================
36 // class definitions
37 // ===========================================================================
43 public:
44 
51  NBLoadedSUMOTLDef(const std::string& id, const std::string& programID, SUMOTime offset, TrafficLightType type);
52 
58 
59 
62 
63  void setID(const std::string& newID);
64 
68  void setProgramID(const std::string& programID);
69 
72  void setTLControllingInformation() const;
73 
79  void remapRemoved(NBEdge* removed,
80  const EdgeVector& incoming, const EdgeVector& outgoing);
81 
82 
89  void replaceRemoved(NBEdge* removed, int removedLane,
90  NBEdge* by, int byLane, bool incoming);
91 
95  void shiftTLConnectionLaneIndex(NBEdge* edge, int offset, int threshold = -1);
96 
109  void addPhase(const SUMOTime duration, const std::string& state, const SUMOTime minDur, const SUMOTime maxDur,
110  const SUMOTime earliestEnd, const SUMOTime latestEnd, const SUMOTime vehExt, const SUMOTime yellow,
111  const SUMOTime red, const std::vector<int>& next, const std::string& name);
112 
114  void phasesLoaded() {
115  myPhasesLoaded = true;
116  }
117 
120  void addConnection(NBEdge* from, NBEdge* to, int fromLane, int toLane, int linkIndex, int linkIndex2, bool reconstruct = true);
121 
122 
127  void removeConnection(const NBConnection& conn, bool reconstruct = true);
128 
130  void registerModifications(bool addedConnections, bool removedConnections);
131 
135  return myTLLogic;
136  }
137 
141  void setOffset(SUMOTime offset);
142 
146  void setType(TrafficLightType type);
147 
149  bool rightOnRedConflict(int index, int foeIndex) const;
150 
151  /* @brief shortens phase states to remove states that are not referenced by
152  * any controlled link and returns whether states were shortened
153  */
154  bool cleanupStates();
155 
157  bool usingSignalGroups() const;
158 
161 
163  void guessMinMaxDuration();
164 
166  void groupSignals();
167 
169  void ungroupSignals();
170 
172  // Note: Issues a warning when the grouping of def is incompatible with the current states
174 
176  void finalChecks() const;
177 
179  void replaceIndex(int oldIndex, int newIndex);
180 
181 protected:
183  void collectLinks();
184 
186  void collectEdges();
187 
192  NBTrafficLightLogic* myCompute(int brakingTimeSeconds);
193 
194  bool amInvalid() const;
195 
197  void initNeedsContRelation() const;
198 
200  int getMaxIndex();
201 
203  int getMaxValidIndex();
204 
206  std::string getStates(int index);
207 
209  bool isUsed(int index) const;
210 
212  std::set<const NBEdge*> getEdgesUsingIndex(int index) const;
213 
214 private:
215 
218 
220  void patchIfCrossingsAdded();
221 
223  std::set<NBEdge*> myShifted;
224 
229 
235  void collectEdgeVectors(EdgeVector& fromEdges, EdgeVector& toEdges, std::vector<int>& fromLanes) const;
236 
238  void reconstructLogic();
239 
241  bool hasValidIndices() const;
242 
243 private:
246  public:
249 
250  bool operator()(const NBConnection& c) const {
251  return c.getFrom() == myC.getFrom() && c.getTo() == myC.getTo() &&
252  c.getFromLane() == myC.getFromLane() && c.getToLane() == myC.getToLane();
253  }
254  private:
256  };
257 
258 };
long long int SUMOTime
Definition: GUI.h:35
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:35
TrafficLightType
NBEdge * getFrom() const
returns the from-edge (start of the connection)
int getFromLane() const
returns the from-lane
int getToLane() const
returns the to-lane
NBEdge * getTo() const
returns the to-edge (end of the connection)
The representation of a single edge during network building.
Definition: NBEdge.h:92
class for identifying connections
bool operator()(const NBConnection &c) const
connection_equal(const NBConnection &c)
constructor
A loaded (complete) traffic light logic.
void phasesLoaded()
mark phases as load
bool isUsed(int index) const
return whether the given link index is used by any connectons
NBTrafficLightLogic * myCompute(int brakingTimeSeconds)
Computes the traffic light logic finally in dependence to the type.
void setID(const std::string &newID)
resets the id
int getMaxIndex()
return the highest known tls link index used by any controlled connection or crossing
bool hasValidIndices() const
return whether all tls link indices are valid
bool usingSignalGroups() const
whether this definition uses signal group (multiple connections with the same link index)
std::string getStates(int index)
get all states for the given link index
void ungroupSignals()
let all connections use a distinct link index
NBLoadedSUMOTLDef(const std::string &id, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
void addPhase(const SUMOTime duration, const std::string &state, const SUMOTime minDur, const SUMOTime maxDur, const SUMOTime earliestEnd, const SUMOTime latestEnd, const SUMOTime vehExt, const SUMOTime yellow, const SUMOTime red, const std::vector< int > &next, const std::string &name)
Adds a phase to the logic the new phase is inserted at the end of the list of already added phases.
bool myReconstructAddedConnections
whether the logic must be reconstructed
bool rightOnRedConflict(int index, int foeIndex) const
whether the given index must yield to the foeIndex while turing right on a red light
void copyIndices(NBTrafficLightDefinition *def)
copy the assignment of link indices to connections from the given definition and rebuilt the states t...
void groupSignals()
let connections with the same state use the same link index
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurrences of the removed edge in incoming/outgoing edges of all definitions.
void collectEdges()
Build the list of participating edges.
void setProgramID(const std::string &programID)
Sets the programID.
void shiftTLConnectionLaneIndex(NBEdge *edge, int offset, int threshold=-1)
patches signal plans by modifying lane indices with the given offset, only indices with a value above...
NBTrafficLightLogic * myTLLogic
phases are added directly to myTLLogic which is then returned in myCompute()
std::set< NBEdge * > myShifted
set of edges with shifted lane indices (to avoid shifting twice)
void patchIfCrossingsAdded()
repair the plan if controlled nodes received pedestrian crossings
void removeConnection(const NBConnection &conn, bool reconstruct=true)
removes the given connection from the traffic light if recontruct=true, reconstructs the logic and in...
int getMaxValidIndex()
Returns the maximum index controlled by this traffic light.
void replaceIndex(int oldIndex, int newIndex)
replace the given link index in all connections
void collectLinks()
Collects the links participating in this traffic light (only if not previously loaded)
void registerModifications(bool addedConnections, bool removedConnections)
register changes that necessitate recomputation
void joinLogic(NBTrafficLightDefinition *def)
join nodes and states from the given logic (append red state)
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
void setType(TrafficLightType type)
Sets the algorithm type of this tls.
void collectEdgeVectors(EdgeVector &fromEdges, EdgeVector &toEdges, std::vector< int > &fromLanes) const
Collects the edges for each tlIndex.
void reconstructLogic()
adapt to removal or addition of connections
void addConnection(NBEdge *from, NBEdge *to, int fromLane, int toLane, int linkIndex, int linkIndex2, bool reconstruct=true)
Adds a connection and immediately informs the edges.
void guessMinMaxDuration()
heuristically add minDur and maxDur when switching from tlType fixed to actuated
std::set< const NBEdge * > getEdgesUsingIndex(int index) const
brief retrieve all edges with connections that use the given traffic light index
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane, bool incoming)
Replaces a removed edge/lane.
NBTrafficLightLogic * getLogic()
Returns the internal logic.
void setOffset(SUMOTime offset)
Sets the offset of this tls.
void finalChecks() const
perform optional final checks (on writing)
~NBLoadedSUMOTLDef()
Destructor.
void initNeedsContRelation() const
initialize myNeedsContRelation and set myNeedsContRelationReady to true
The base class for traffic light logic definitions.
A SUMO-compliant built logic for a traffic light.