CARLA
TrafficLight.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
10 
11 #include <unordered_map>
12 #include <unordered_set>
13 
14 namespace carla {
15 namespace client {
16 
18  GetEpisode().Lock()->SetTrafficLightState(*this, state);
19  }
20 
22  return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.state;
23  }
24 
25  void TrafficLight::SetGreenTime(float green_time) {
26  GetEpisode().Lock()->SetTrafficLightGreenTime(*this, green_time);
27  }
28 
30  return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.green_time;
31  }
32 
33  void TrafficLight::SetYellowTime(float yellow_time) {
34  GetEpisode().Lock()->SetTrafficLightYellowTime(*this, yellow_time);
35  }
36 
38  return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.yellow_time;
39  }
40 
41  void TrafficLight::SetRedTime(float red_time) {
42  GetEpisode().Lock()->SetTrafficLightRedTime(*this, red_time);
43  }
44 
45  float TrafficLight::GetRedTime() const {
46  return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.red_time;
47  }
48 
50  return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.elapsed_time;
51  }
52 
53  void TrafficLight::Freeze(bool freeze) {
54  //GetEpisode().Lock()->FreezeTrafficLight(*this, freeze);
55  GetEpisode().Lock()->FreezeAllTrafficLights(freeze);
56  }
57 
58  bool TrafficLight::IsFrozen() const {
59  return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.time_is_frozen;
60  }
61 
63  {
64  return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.pole_index;
65  }
66 
67  std::vector<SharedPtr<TrafficLight>> TrafficLight::GetGroupTrafficLights() {
68  std::vector<SharedPtr<TrafficLight>> result;
69  auto ids = GetEpisode().Lock()->GetGroupTrafficLights(*this);
70  for (auto id : ids) {
71  SharedPtr<Actor> actor = GetWorld().GetActors()->Find(id);
72  result.push_back(boost::static_pointer_cast<TrafficLight>(actor));
73  }
74  return result;
75  }
76 
78  GetEpisode().Lock()->ResetTrafficLightGroup(*this);
79  }
80 
81  std::vector<SharedPtr<Waypoint>> TrafficLight::GetAffectedLaneWaypoints() const {
82  std::vector<SharedPtr<Waypoint>> result;
83  SharedPtr<Map> carla_map = GetEpisode().Lock()->GetCurrentMap();
84  std::vector<SharedPtr<Landmark>> landmarks = carla_map->GetLandmarksFromId(GetOpenDRIVEID());
85  for (auto& landmark : landmarks) {
86  for (const road::LaneValidity& validity : landmark->GetValidities()) {
87  if (validity._from_lane < validity._to_lane) {
88  for (int lane_id = validity._from_lane; lane_id <= validity._to_lane; ++lane_id) {
89  if(lane_id == 0) continue;
90  result.emplace_back(
91  carla_map->GetWaypointXODR(
92  landmark->GetRoadId(), lane_id, static_cast<float>(landmark->GetS())));
93  }
94  } else {
95  for (int lane_id = validity._from_lane; lane_id >= validity._to_lane; --lane_id) {
96  if(lane_id == 0) continue;
97  result.emplace_back(
98  carla_map->GetWaypointXODR(
99  landmark->GetRoadId(), lane_id, static_cast<float>(landmark->GetS())));
100  }
101  }
102  }
103  }
104  return result;
105  }
106 
107  std::vector<geom::BoundingBox> TrafficLight::GetLightBoxes() const {
108  return GetEpisode().Lock()->GetLightBoxes(*this);
109  }
110 
112  return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.sign_id;
113  }
114 
115  std::vector<SharedPtr<Waypoint>> TrafficLight::GetStopWaypoints() const {
116  std::vector<SharedPtr<Waypoint>> result;
117  SharedPtr<Map> carla_map = GetEpisode().Lock()->GetCurrentMap();
119  geom::Transform transform = GetTransform();
120  geom::Location box_position = box.location;
121  transform.TransformPoint(box_position);
122  geom::Vector3D right_direction = transform.GetForwardVector();
123  float min_x = -0.9f*box.extent.x;
124  float max_x = 0.9f*box.extent.x;
125  float current_x = min_x;
126  std::unordered_map<road::RoadId, std::unordered_set<road::LaneId>> road_lanes_map;
127  while (current_x < max_x) {
128  geom::Location query_point = box_position + geom::Location(right_direction*current_x);
129  SharedPtr<Waypoint> waypoint = carla_map->GetWaypoint(query_point);
130  if (road_lanes_map[waypoint->GetRoadId()].count(waypoint->GetLaneId()) == 0) {
131  road_lanes_map[waypoint->GetRoadId()].insert(waypoint->GetLaneId());
132  result.emplace_back(waypoint);
133  }
134  current_x += 1.f;
135  }
136  return result;
137  }
138 
139 } // namespace client
140 } // namespace carla
std::string SignId
Definition: RoadTypes.h:25
void SetGreenTime(float green_time)
SharedPtr< ActorList > GetActors() const
Return a list with all the actors currently present in the world.
Definition: World.cpp:106
std::vector< SharedPtr< TrafficLight > > GetGroupTrafficLights()
Return all traffic lights in the group this one belongs to.
Location location
Center of the BoundingBox in local space.
Vector3D extent
Half the size of the BoundingBox in local space.
std::vector< SharedPtr< Waypoint > > GetAffectedLaneWaypoints() const
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
const geom::BoundingBox & GetTriggerVolume() const
Definition: TrafficSign.h:20
std::vector< SharedPtr< Waypoint > > GetStopWaypoints() const
void SetRedTime(float red_time)
Vector3D GetForwardVector() const
geom::Location Location
Definition: rpc/Location.h:14
void SetState(rpc::TrafficLightState state)
geom::Transform GetTransform() const
Return the current transform of the actor.
Definition: Actor.cpp:19
void TransformPoint(Vector3D &in_point) const
Applies this transformation to in_point (first translation then rotation).
rpc::TrafficLightState GetState() const
Return the current state of the traffic light.
void Freeze(bool freeze)
void SetYellowTime(float yellow_time)
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
std::vector< geom::BoundingBox > GetLightBoxes() const
uint32_t GetPoleIndex()
Returns the index of the pole in the traffic light group.
road::SignId GetOpenDRIVEID() const