2424#include " slg/scene/scene.h"
2525#include " slg/lights/trianglelight.h"
2626#include " slg/lights/strategies/logpower.h"
27+ #include " slg/usings.h"
2728
2829using namespace std ;
2930using namespace luxrays ;
@@ -45,18 +46,27 @@ LightSourceDefinitions::LightSourceDefinitions() :
4546LightSourceDefinitions::~LightSourceDefinitions () {
4647}
4748
48- void LightSourceDefinitions::DefineLightSource (LightSourceUPtr&& newLight) {
49+ // / Insert a new light source in the container
50+ // /
51+ // / If a light source with the same name already exists in the container,
52+ // / it is extracted and returned.
53+ LightSourceUPtr LightSourceDefinitions::DefineLightSource (LightSourceUPtr&& newLight) {
4954 const string &name = newLight->GetName ();
5055
51- if (IsLightSourceDefined (name)) {
52- auto & oldLight = GetLightSource (name);
56+ auto e = lightsByName.find (name);
57+
58+ if (e != lightsByName.end ()) {
59+ // Light source already exists
60+ auto oldLight = std::move (e->second );
5361
5462 // Update name/LightSource definition
55- lightsByName.erase (name );
63+ lightsByName.erase (e );
5664 lightsByName[name] = std::move (newLight);
65+ return oldLight;
5766 } else {
5867 // Add the new LightSource
5968 lightsByName[name] = std::move (newLight);
69+ return LightSourceUPtr ();
6070 }
6171}
6272
@@ -120,31 +130,47 @@ vector<string> LightSourceDefinitions::GetLightSourceNames() const {
120130 return names;
121131}
122132
123- void LightSourceDefinitions::DeleteLightSource (const string &name) {
133+ LightSourceUPtr LightSourceDefinitions::DeleteLightSource (const string &name) {
124134 auto e = lightsByName.find (name);
125135
126136 if (e == lightsByName.end ())
127137 throw runtime_error (" Reference to an undefined LightSource in LightSourceDefinitions::DeleteLightSource(): " + name);
128- else {
129- lightsByName.erase (name);
130- }
138+
139+
140+ auto oldLight = std::move (e->second );
141+ lightsByName.erase (e);
142+ return oldLight;
131143}
132144
133- void LightSourceDefinitions::DeleteLightSourceStartWith (const string &namePrefix) {
145+
146+ std::vector<LightSourceUPtr> LightSourceDefinitions::DeleteLightSourceStartWith (
147+ const string &namePrefix
148+ ) {
149+ std::vector<LightSourceUPtr> oldLights;
150+
134151 // Build the list of lights to delete
135- vector<string> nameList;
152+ std:: vector<string> nameList;
136153 for (auto const &e : lightsByName) {
137154 const string &name = e.first ;
138155
139156 if (name.starts_with (namePrefix))
140157 nameList.push_back (name);
141158 }
142159
143- for (auto const &name : nameList)
144- DeleteLightSource (name);
160+ // Remove the lights from the container and return the removed lights
161+ for (auto const &name : nameList) {
162+ auto oldLight = DeleteLightSource (name);
163+ oldLights.push_back (std::move (oldLight));
164+ }
165+ return oldLights;
145166}
146167
147- void LightSourceDefinitions::DeleteLightSourceByMaterial (MaterialConstRef mat) {
168+
169+ std::vector<LightSourceUPtr> LightSourceDefinitions::DeleteLightSourceByMaterial (
170+ MaterialConstRef mat
171+ ) {
172+ std::vector<LightSourceUPtr> oldLights;
173+
148174 // Build the list of lights to delete
149175 std::vector<string> nameList;
150176 for (auto const &e : lightsByName) {
@@ -159,8 +185,12 @@ void LightSourceDefinitions::DeleteLightSourceByMaterial(MaterialConstRef mat) {
159185 }
160186 }
161187
162- for (auto const &name : nameList)
163- DeleteLightSource (name);
188+ // Remove the lights from the container and return the removed lights
189+ for (auto const &name : nameList) {
190+ auto oldLight = DeleteLightSource (name);
191+ oldLights.push_back (std::move (oldLight));
192+ }
193+ return oldLights;
164194}
165195
166196void LightSourceDefinitions::SetLightStrategy (const luxrays::Properties &props) {
0 commit comments