Blue Brain BioExplorer
LightManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  *
5  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
6  *
7  * This library is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Lesser General Public License version 3.0 as published
9  * by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include "LightManager.h"
22 
25 
26 #include <algorithm>
27 
28 namespace core
29 {
31 {
32  // If light already added, return id
33  auto itInv = _lightsInverse.find(light);
34  if (itInv != _lightsInverse.end())
35  {
36  markModified();
37  return itInv->second;
38  }
39 
40  // If lights are empty we reset id counter to avoid huge numbers
41  if (_lights.empty())
42  _IDctr = 0;
43 
44  const size_t id = _IDctr++;
45  _lights.insert({id, light});
46  _lightsInverse.insert({light, id});
47  markModified();
48  return id;
49 }
50 
51 void LightManager::removeLight(const size_t id)
52 {
53  auto it = _lights.find(id);
54  if (it != _lights.end())
55  {
56  auto light = it->second;
57 
58  auto itInv = _lightsInverse.find(light);
59  assert(itInv != _lightsInverse.end());
60  if (itInv != _lightsInverse.end())
61  _lightsInverse.erase(itInv);
62 
63  _lights.erase(it);
64 
65  markModified();
66  }
67 }
68 
70 {
71  auto itInv = _lightsInverse.find(light);
72 
73  if (itInv != _lightsInverse.end())
74  {
75  const size_t id = itInv->second;
76  auto it = _lights.find(id);
77  assert(it != _lights.end());
78  if (it != _lights.end())
79  _lights.erase(it);
80  }
81 }
82 
84 {
85  auto it = _lights.find(id);
86  if (it != _lights.end())
87  return it->second;
88 
89  return nullptr;
90 }
91 
92 const std::map<size_t, LightPtr>& LightManager::getLights() const
93 {
94  return _lights;
95 }
96 
98 {
99  _lights.clear();
100  _lightsInverse.clear();
101  markModified();
102 }
103 
104 } // namespace core
void markModified(const bool triggerCallback=true)
Definition: BaseObject.h:65
PLATFORM_API void clearLights()
clearLights Removes all light sources managed by the LightManager object.
PLATFORM_API void removeLight(const size_t id)
removeLight Removes a light source from the scene for a given ID.
PLATFORM_API const std::map< size_t, LightPtr > & getLights() const
getLights Gets all light sources currently managed by the LightManager object.
PLATFORM_API size_t addLight(LightPtr light)
addLight Attaches a light source to the scene.
PLATFORM_API LightPtr getLight(const size_t id)
getLight Gets a light source from the scene for a given ID. Note: If changing the light then call mar...
std::shared_ptr< Light > LightPtr
Definition: Types.h:163