CARLA
LightManager.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 
8 
10 
11 
12 namespace carla {
13 namespace client {
14 
16 
18  if(_episode.IsValid()) {
19  _episode.Lock()->RemoveOnTickEvent(_on_light_update_register_id);
20  _episode.Lock()->RemoveLightUpdateChangeEvent(_on_light_update_register_id);
21  }
23 }
24 
26 
27  _episode = episode;
28 
29  _on_tick_register_id = _episode.Lock()->RegisterOnTickEvent(
30  [&](const WorldSnapshot&) {
32  });
33 
34  _on_light_update_register_id = _episode.Lock()->RegisterLightUpdateChangeEvent(
35  [&](const WorldSnapshot& ) {
37  ApplyChanges();
38  });
39 
41 }
42 
43 std::vector<Light> LightManager::GetAllLights(LightGroup type) const {
44  std::vector<Light> result;
45 
46  for(auto lights_state : _lights_state) {
47  LightGroup group = lights_state.second._group;
48  if((type == LightGroup::None) || (group == type)) {
49  auto it_light = _lights.find(lights_state.first);
50  result.push_back(it_light->second);
51  }
52  }
53 
54  return result;
55 }
56 
57 void LightManager::TurnOn(std::vector<Light>& lights) {
58  for(Light& light : lights) {
59  SetActive(light._id, true);
60  }
61 }
62 
63 void LightManager::TurnOff(std::vector<Light>& lights) {
64  for(Light& light : lights) {
65  SetActive(light._id, false);
66  }
67 }
68 
69 void LightManager::SetActive(std::vector<Light>& lights, std::vector<bool>& active) {
70  size_t lights_to_update = (lights.size() < active.size()) ? lights.size() : active.size();
71  for(size_t i = 0; i < lights_to_update; i++) {
72  SetActive(lights[i]._id, active[i]);
73  }
74 }
75 
76 std::vector<bool> LightManager::IsActive(std::vector<Light>& lights) const {
77  std::vector<bool> result;
78  for(Light& light : lights) {
79  result.push_back( IsActive(light._id) );
80  }
81  return result;
82 }
83 
84 std::vector<Light> LightManager::GetTurnedOnLights(LightGroup type) const {
85  std::vector<Light> result;
86 
87  for(auto lights_state : _lights_state) {
88  LightState& state = lights_state.second;
89  LightGroup group = state._group;
90  if( (type == LightGroup::None || group == type) && state._active ) {
91  auto it_light = _lights.find(lights_state.first);
92  result.push_back(it_light->second);
93  }
94  }
95 
96  return result;
97 }
98 
99 std::vector<Light> LightManager::GetTurnedOffLights(LightGroup type) const {
100  std::vector<Light> result;
101 
102  for(auto lights_state : _lights_state) {
103  LightState& state = lights_state.second;
104  LightGroup group = state._group;
105  if( (type == LightGroup::None || group == type) && !state._active ) {
106  auto it_light = _lights.find(lights_state.first);
107  result.push_back(it_light->second);
108  }
109  }
110 
111  return result;
112 }
113 
114 void LightManager::SetColor(std::vector<Light>& lights, Color color) {
115  for(Light& light : lights) {
116  SetColor(light._id, color);
117  }
118 }
119 
120 void LightManager::SetColor(std::vector<Light>& lights, std::vector<Color>& colors) {
121  size_t lights_to_update = (lights.size() < colors.size()) ? lights.size() : colors.size();
122  for(size_t i = 0; i < lights_to_update; i++) {
123  SetColor(lights[i]._id, colors[i]);
124  }
125 }
126 
127 std::vector<Color> LightManager::GetColor(std::vector<Light>& lights) const {
128  std::vector<Color> result;
129  for(Light& light : lights) {
130  result.push_back( GetColor(light._id) );
131  }
132  return result;
133 }
134 
135 void LightManager::SetIntensity(std::vector<Light>& lights, float intensity) {
136  for(Light& light : lights) {
137  SetIntensity(light._id, intensity);
138  }
139 }
140 
141 void LightManager::SetIntensity(std::vector<Light>& lights, std::vector<float>& intensities) {
142  size_t lights_to_update = (lights.size() < intensities.size()) ? lights.size() : intensities.size();
143  for(size_t i = 0; i < lights_to_update; i++) {
144  SetIntensity(lights[i]._id, intensities[i]);
145  }
146 }
147 
148 std::vector<float> LightManager::GetIntensity(std::vector<Light>& lights) const {
149  std::vector<float> result;
150  for(Light& light : lights) {
151  result.push_back( GetIntensity(light._id) );
152  }
153  return result;
154 }
155 
156 void LightManager::SetLightGroup(std::vector<Light>& lights, LightGroup group) {
157  for(Light& light : lights) {
158  SetLightGroup(light._id, group);
159  }
160 }
161 
162 void LightManager::SetLightGroup(std::vector<Light>& lights, std::vector<LightGroup>& groups) {
163  size_t lights_to_update = (lights.size() < groups.size()) ? lights.size() : groups.size();
164  for(size_t i = 0; i < lights_to_update; i++) {
165  SetLightGroup(lights[i]._id, groups[i]);
166  }
167 }
168 
169 std::vector<LightGroup> LightManager::GetLightGroup(std::vector<Light>& lights) const {
170  std::vector<LightGroup> result;
171  for(Light& light : lights) {
172  result.push_back( GetLightGroup(light._id) );
173  }
174  return result;
175 }
176 
177 void LightManager::SetLightState(std::vector<Light>& lights, LightState state) {
178  for(Light& light : lights) {
179  SetLightState(light._id, state);
180  }
181 }
182 
183 void LightManager::SetLightState(std::vector<Light>& lights, std::vector<LightState>& states) {
184  size_t lights_to_update = (lights.size() < states.size()) ? lights.size() : states.size();
185  for(size_t i = 0; i < lights_to_update; i++) {
186  SetLightState(lights[i]._id, states[i]);
187  }
188 }
189 
190 std::vector<LightState> LightManager::GetLightState(std::vector<Light>& lights) const {
191  std::vector<LightState> result;
192  for(Light& light : lights) {
193  result.push_back( RetrieveLightState(light._id) );
194  }
195  return result;
196 }
197 
199  return RetrieveLightState(id)._color;
200 }
201 
203  return RetrieveLightState(id)._intensity;
204 }
205 
207  return RetrieveLightState(id);
208 }
209 
211  return RetrieveLightState(id)._group;
212 }
213 
215  return RetrieveLightState(id)._active;
216 }
217 
218 void LightManager::SetActive(LightId id, bool active) {
219  std::lock_guard<std::mutex> lock(_mutex);
220  LightState& state = const_cast<LightState&>(RetrieveLightState(id));
221  state._active = active;
222  _lights_changes[id] = state;
223  _dirty = true;
224 }
225 
227  std::lock_guard<std::mutex> lock(_mutex);
228  LightState& state = const_cast<LightState&>(RetrieveLightState(id));
229  state._color = color;
230  _lights_changes[id] = state;
231  _dirty = true;
232 }
233 
234 void LightManager::SetIntensity(LightId id, float intensity) {
235  std::lock_guard<std::mutex> lock(_mutex);
236  LightState& state = const_cast<LightState&>(RetrieveLightState(id));
237  state._intensity = intensity;
238  _lights_changes[id] = state;
239  _dirty = true;
240 }
241 
242 void LightManager::SetLightState(LightId id, const LightState& new_state) {
243  std::lock_guard<std::mutex> lock(_mutex);
244  LightState& state = const_cast<LightState&>(RetrieveLightState(id));
245  state = new_state;
246  _lights_changes[id] = state;
247  _dirty = true;
248 }
249 
251  std::lock_guard<std::mutex> lock(_mutex);
252  LightState& state = const_cast<LightState&>(RetrieveLightState(id));
253  state._group = group;
254  _lights_changes[id] = state;
255  _dirty = true;
256 }
257 
259  auto it = _lights_state.find(id);
260  if(it == _lights_state.end()) {
261  carla::log_warning("Invalid light", id);
262  return _state;
263  }
264  return it->second;
265 }
266 
268  std::lock_guard<std::mutex> lock(_mutex);
269  // Send blocking query
270  std::vector<rpc::LightState> lights_snapshot = _episode.Lock()->QueryLightsStateToServer();
271 
272  // Update lights
273  SharedPtr<LightManager> lm = _episode.Lock()->GetLightManager();
274 
275  for(const auto& it : lights_snapshot) {
276  _lights_state[it._id] = LightState(
277  it._intensity,
278  Color(it._color.r, it._color.g, it._color.b),
279  static_cast<LightState::LightGroup>(it._group),
280  it._active
281  );
282 
283  if(_lights.find(it._id) == _lights.end())
284  {
285  _lights[it._id] = Light(lm, it._location, it._id);
286  }
287  }
288 }
289 
290 void LightManager::UpdateServerLightsState(bool discard_client) {
291  std::lock_guard<std::mutex> lock(_mutex);
292 
293  if(_dirty) {
294  std::vector<rpc::LightState> message;
295  for(auto it : _lights_changes) {
296  auto it_light = _lights.find(it.first);
297  if(it_light != _lights.end()) {
298  rpc::LightState state(
299  it_light->second.GetLocation(),
300  it.second._intensity,
301  it.second._group,
302  rpc::Color(it.second._color.r, it.second._color.g, it.second._color.b),
303  it.second._active
304  );
305  state._id = it.first;
306  // Add to command
307  message.push_back(state);
308  }
309  }
310  _episode.Lock()->UpdateServerLightsState(message, discard_client);
311 
312  _lights_changes.clear();
313  _dirty = false;
314  }
315 }
316 
318  std::lock_guard<std::mutex> lock(_mutex);
319  for(const auto& it : _lights_changes) {
320  SetLightState(it.first, it.second);
321  }
322 }
323 
324 } // namespace client
325 } // namespace carla
std::unordered_map< LightId, LightState > _lights_changes
Definition: LightManager.h:96
sensor::data::Color Color
detail::EpisodeProxy _episode
Definition: LightManager.h:99
const LightState & RetrieveLightState(LightId id) const
void TurnOn(std::vector< Light > &lights)
A 32-bit BGRA color.
std::vector< Light > GetTurnedOnLights(LightGroup type=LightGroup::None) const
void SetColor(std::vector< Light > &lights, Color color)
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:99
void UpdateServerLightsState(bool discard_client=false)
void SetEpisode(detail::EpisodeProxy episode)
void SetActive(std::vector< Light > &lights, std::vector< bool > &active)
std::vector< float > GetIntensity(std::vector< Light > &lights) const
std::vector< LightState > GetLightState(std::vector< Light > &lights) const
std::unordered_map< LightId, Light > _lights
Definition: LightManager.h:97
void SetIntensity(std::vector< Light > &lights, float intensity)
std::unordered_map< LightId, LightState > _lights_state
Definition: LightManager.h:95
std::vector< Color > GetColor(std::vector< Light > &lights) const
std::vector< Light > GetTurnedOffLights(LightGroup type=LightGroup::None) const
std::vector< bool > IsActive(std::vector< Light > &lights) const
rpc::LightState::LightGroup LightGroup
Definition: Light.cpp:16
void SetLightState(std::vector< Light > &lights, LightState state)
static void log_warning(Args &&... args)
Definition: Logging.h:96
void TurnOff(std::vector< Light > &lights)
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
std::vector< LightGroup > GetLightGroup(std::vector< Light > &lights) const
void SetLightGroup(std::vector< Light > &lights, LightGroup group)
std::vector< Light > GetAllLights(LightGroup type=LightGroup::None) const