CARLA
 
载入中...
搜索中...
未找到
LightManager.cpp
浏览该文件的文档.
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
12namespace carla {
13namespace client {
14
15 using LightGroup = rpc::LightState::LightGroup;
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& ) {
38 });
39
41}
42
43std::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
57void LightManager::TurnOn(std::vector<Light>& lights) {
58 for(Light& light : lights) {
59 SetActive(light._id, true);
60 }
61}
62
63void LightManager::TurnOff(std::vector<Light>& lights) {
64 for(Light& light : lights) {
65 SetActive(light._id, false);
66 }
67}
68
69void 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
76std::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
84std::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
99std::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
114void LightManager::SetColor(std::vector<Light>& lights, Color color) {
115 for(Light& light : lights) {
116 SetColor(light._id, color);
117 }
118}
119
120void 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
127std::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
135void LightManager::SetIntensity(std::vector<Light>& lights, float intensity) {
136 for(Light& light : lights) {
137 SetIntensity(light._id, intensity);
138 }
139}
140
141void 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
148std::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
156void LightManager::SetLightGroup(std::vector<Light>& lights, LightGroup group) {
157 for(Light& light : lights) {
158 SetLightGroup(light._id, group);
159 }
160}
161
162void 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
169std::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
177void LightManager::SetLightState(std::vector<Light>& lights, LightState state) {
178 for(Light& light : lights) {
179 SetLightState(light._id, state);
180 }
181}
182
183void 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
190std::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
201
205
209
213
215 return RetrieveLightState(id)._active;
216}
217
218void 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
234void 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
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
296void 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
330void LightManager::SetDayNightCycle(const bool active) {
331 _episode.Lock()->UpdateDayNightCycle(active);
332}
333
334} // namespace client
335} // namespace carla
std::vector< Light > GetTurnedOffLights(LightGroup type=LightGroup::None) const
std::vector< Light > GetTurnedOnLights(LightGroup type=LightGroup::None) const
std::vector< LightGroup > GetLightGroup(std::vector< Light > &lights) const
void SetIntensity(std::vector< Light > &lights, float intensity)
std::vector< bool > IsActive(std::vector< Light > &lights) const
std::vector< Color > GetColor(std::vector< Light > &lights) const
const LightState & RetrieveLightState(LightId id) const
void SetLightState(std::vector< Light > &lights, LightState state)
std::unordered_map< LightId, LightState > _lights_changes
void TurnOff(std::vector< Light > &lights)
void SetLightStateNoLock(LightId id, const LightState &new_state)
detail::WeakEpisodeProxy _episode
void SetColor(std::vector< Light > &lights, Color color)
void TurnOn(std::vector< Light > &lights)
std::vector< float > GetIntensity(std::vector< Light > &lights) const
void SetDayNightCycle(const bool active)
std::unordered_map< LightId, LightState > _lights_state
void SetLightGroup(std::vector< Light > &lights, LightGroup group)
void SetEpisode(detail::WeakEpisodeProxy episode)
void SetActive(std::vector< Light > &lights, std::vector< bool > &active)
std::unordered_map< LightId, Light > _lights
void UpdateServerLightsState(bool discard_client=false)
std::vector< LightState > GetLightState(std::vector< Light > &lights) const
std::vector< Light > GetAllLights(LightGroup type=LightGroup::None) const
EpisodeProxyImpl< EpisodeProxyPointerType::Weak > WeakEpisodeProxy
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
static void log_warning(Args &&... args)
Definition Logging.h:96
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