diff --git a/source/EngineInterface/SimulationParametersEditService.cpp b/source/EngineInterface/SimulationParametersEditService.cpp index 40111292a..446db6ad3 100644 --- a/source/EngineInterface/SimulationParametersEditService.cpp +++ b/source/EngineInterface/SimulationParametersEditService.cpp @@ -28,12 +28,12 @@ auto SimulationParametersEditService::getRadiationStrengths(SimulationParameters return result; } -void SimulationParametersEditService::applyRadiationStrengths(SimulationParameters& parameters, RadiationStrengths const& ratios) +void SimulationParametersEditService::applyRadiationStrengthValues(SimulationParameters& parameters, RadiationStrengths const& strengths) { - CHECK(parameters.numRadiationSources + 1 == ratios.values.size()); + CHECK(parameters.numRadiationSources + 1 == strengths.values.size()); for (int i = 0; i < parameters.numRadiationSources; ++i) { - parameters.radiationSources[i].strengthRatio = ratios.values.at(i + 1); + parameters.radiationSources[i].strengthRatio = strengths.values.at(i + 1); } } diff --git a/source/EngineInterface/SimulationParametersEditService.h b/source/EngineInterface/SimulationParametersEditService.h index 7ec7bfcb6..4ada3cd4d 100644 --- a/source/EngineInterface/SimulationParametersEditService.h +++ b/source/EngineInterface/SimulationParametersEditService.h @@ -7,19 +7,19 @@ #include "Base/Singleton.h" #include "SimulationParameters.h" +struct RadiationStrengths +{ + std::vector values; + std::set pinned; +}; class SimulationParametersEditService { MAKE_SINGLETON(SimulationParametersEditService); public: - struct RadiationStrengths - { - std::vector values; - std::set pinned; - }; RadiationStrengths getRadiationStrengths(SimulationParameters const& parameters) const; - void applyRadiationStrengths(SimulationParameters& parameters, RadiationStrengths const& ratios); + void applyRadiationStrengthValues(SimulationParameters& parameters, RadiationStrengths const& strengths); void adaptRadiationStrengths(RadiationStrengths& strengths, RadiationStrengths& origStrengths, int changeIndex) const; RadiationStrengths calcRadiationStrengthsForAddingSpot(RadiationStrengths const& strengths) const; diff --git a/source/EngineTests/SimulationParametersEditServiceTests.cpp b/source/EngineTests/SimulationParametersEditServiceTests.cpp new file mode 100644 index 000000000..0dd03c1d4 --- /dev/null +++ b/source/EngineTests/SimulationParametersEditServiceTests.cpp @@ -0,0 +1,60 @@ +#include "EngineInterface/SimulationParametersEditService.h" + +#include + +#include "Base/Definitions.h" + +class SimulationParametersEditServiceTests : public ::testing::Test +{ +public: + SimulationParametersEditServiceTests() = default; + + ~SimulationParametersEditServiceTests() = default; + +protected: + void checkApproxEqual(float expected, float actual) { EXPECT_TRUE(std::abs(actual - expected) < NEAR_ZERO); } +}; + +TEST_F(SimulationParametersEditServiceTests, getRadiationStrengths) +{ + SimulationParameters parameters; + parameters.numRadiationSources = 2; + parameters.radiationSources[0].strengthRatio = 0.3f; + parameters.radiationSources[1].strengthRatio = 0.6f; + + auto strengths = SimulationParametersEditService::get().getRadiationStrengths(parameters); + + checkApproxEqual(0.1f, strengths.values[0]); +} + +TEST_F(SimulationParametersEditServiceTests, applyRadiationStrengthValues) +{ + RadiationStrengths strengths; + strengths.values = {0.1f, 0.3f, 0.6f}; + strengths.pinned = {0, 2}; + + SimulationParameters parameters; + parameters.numRadiationSources = 2; + SimulationParametersEditService::get().applyRadiationStrengthValues(parameters, strengths); + + checkApproxEqual(0.3f, parameters.radiationSources[0].strengthRatio); + checkApproxEqual(0.6f, parameters.radiationSources[1].strengthRatio); +} + +TEST_F(SimulationParametersEditServiceTests, adaptRadiationStrengths_increase_allUnpinned) +{ + RadiationStrengths origStrengths; + origStrengths.values = {0.1f, 0.3f, 0.6f}; + origStrengths.pinned = {}; + + RadiationStrengths strengths; + strengths.values = {0.1f, 0.5f, 0.6f}; + strengths.pinned = {1}; + + SimulationParametersEditService::get().adaptRadiationStrengths(strengths, origStrengths, 1); + + auto factor = 0.5f / 0.7f; + checkApproxEqual(0.1f * factor, strengths.values[0]); + checkApproxEqual(0.5f, strengths.values[1]); + checkApproxEqual(0.6f * factor, strengths.values[2]); +} diff --git a/source/Gui/RadiationSourcesWindow.cpp b/source/Gui/RadiationSourcesWindow.cpp index 13bad531f..2c5fac8e2 100644 --- a/source/Gui/RadiationSourcesWindow.cpp +++ b/source/Gui/RadiationSourcesWindow.cpp @@ -87,7 +87,7 @@ void RadiationSourcesWindow::processBaseTab() ¶meters.baseStrengthRatioPinned)) { newRatios.pinned.insert(0); editService.adaptRadiationStrengths(newRatios, ratios, 0); - editService.applyRadiationStrengths(parameters, newRatios); + editService.applyRadiationStrengthValues(parameters, newRatios); } if (parameters != lastParameters) { @@ -146,7 +146,7 @@ bool RadiationSourcesWindow::processSourceTab(int index) newRatios.values.at(index + 1) = source.strengthRatio; newRatios.pinned.insert(index + 1); editService.adaptRadiationStrengths(newRatios, ratios, index + 1); - editService.applyRadiationStrengths(parameters, newRatios); + editService.applyRadiationStrengthValues(parameters, newRatios); } auto getMousePickerEnabledFunc = [&]() { return SimulationInteractionController::get().isPositionSelectionMode(); }; @@ -238,8 +238,8 @@ void RadiationSourcesWindow::onAppendTab() ++parameters.numRadiationSources; ++origParameters.numRadiationSources; - editService.applyRadiationStrengths(parameters, newStrengths); - editService.applyRadiationStrengths(origParameters, newStrengths); + editService.applyRadiationStrengthValues(parameters, newStrengths); + editService.applyRadiationStrengthValues(origParameters, newStrengths); _simulationFacade->setSimulationParameters(parameters); _simulationFacade->setOriginalSimulationParameters(origParameters); @@ -261,8 +261,8 @@ void RadiationSourcesWindow::onDeleteTab(int index) --parameters.numRadiationSources; --origParameters.numRadiationSources; - editService.applyRadiationStrengths(parameters, newStrengths); - editService.applyRadiationStrengths(origParameters, newStrengths); + editService.applyRadiationStrengthValues(parameters, newStrengths); + editService.applyRadiationStrengthValues(origParameters, newStrengths); _simulationFacade->setSimulationParameters(parameters); _simulationFacade->setOriginalSimulationParameters(origParameters);