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  LightState& state = const_cast<LightState&>(RetrieveLightState(id));
252  state = new_state;
253  _lights_changes[id] = state;
254 }
255 
257  std::lock_guard<std::mutex> lock(_mutex);
258  LightState& state = const_cast<LightState&>(RetrieveLightState(id));
259  state._group = group;
260  _lights_changes[id] = state;
261  _dirty = true;
262 }
263 
265  auto it = _lights_state.find(id);
266  if(it == _lights_state.end()) {
267  carla::log_warning("Invalid light", id);
268  return _state;
269  }
270  return it->second;
271 }
272 
274  std::lock_guard<std::mutex> lock(_mutex);
275  // Send blocking query
276  std::vector<rpc::LightState> lights_snapshot = _episode.Lock()->QueryLightsStateToServer();
277 
278  // Update lights
279  SharedPtr<LightManager> lm = _episode.Lock()->GetLightManager();
280 
281  for(const auto& it : lights_snapshot) {
282  _lights_state[it._id] = LightState(
283  it._intensity,
284  Color(it._color.r, it._color.g, it._color.b),
285  static_cast<LightState::LightGroup>(it._group),
286  it._active
287  );
288 
289  if(_lights.find(it._id) == _lights.end())
290  {
291  _lights[it._id] = Light(lm, it._location, it._id);
292  }
293  }
294 }
295 
296 void LightManager::UpdateServerLightsState(bool discard_client) {
297  std::lock_guard<std::mutex> lock(_mutex);
298 
299  if(_dirty) {
300  std::vector<rpc::LightState> message;
301  for(auto it : _lights_changes) {
302  auto it_light = _lights.find(it.first);
303  if(it_light != _lights.end()) {
304  rpc::LightState state(
305  it_light->second.GetLocation(),
306  it.second._intensity,
307  it.second._group,
308  rpc::Color(it.second._color.r, it.second._color.g, it.second._color.b),
309  it.second._active
310  );
311  state._id = it.first;
312  // Add to command
313  message.push_back(state);
314  }
315  }
316  _episode.Lock()->UpdateServerLightsState(message, discard_client);
317 
318  _lights_changes.clear();
319  _dirty = false;
320  }
321 }
322 
324  std::lock_guard<std::mutex> lock(_mutex);
325  for(const auto& it : _lights_changes) {
326  SetLightStateNoLock(it.first, it.second);
327  }
328 }
329 
330 void LightManager::SetDayNightCycle(const bool active) {
331  _episode.Lock()->UpdateDayNightCycle(active);
332 }
333 
334 } // namespace client
335 } // namespace carla
std::unordered_map< LightId, LightState > _lights_changes
Definition: LightManager.h:99
sensor::data::Color Color
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:133
void SetLightStateNoLock(LightId id, const LightState &new_state)
void UpdateServerLightsState(bool discard_client=false)
void SetActive(std::vector< Light > &lights, std::vector< bool > &active)
detail::WeakEpisodeProxy _episode
Definition: LightManager.h:102
std::vector< float > GetIntensity(std::vector< Light > &lights) const
void SetEpisode(detail::WeakEpisodeProxy episode)
std::vector< LightState > GetLightState(std::vector< Light > &lights) const
std::unordered_map< LightId, Light > _lights
Definition: LightManager.h:100
void SetIntensity(std::vector< Light > &lights, float intensity)
std::unordered_map< LightId, LightState > _lights_state
Definition: LightManager.h:98
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)
std::vector< LightGroup > GetLightGroup(std::vector< Light > &lights) const
void SetLightGroup(std::vector< Light > &lights, LightGroup group)
void SetDayNightCycle(const bool active)
EpisodeProxyImpl< EpisodeProxyPointerType::Weak > WeakEpisodeProxy
Definition: EpisodeProxy.h:71
std::vector< Light > GetAllLights(LightGroup type=LightGroup::None) const