Skip to content

Commit

Permalink
unit tests for strength adaption
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Nov 20, 2024
1 parent ea94476 commit 21a7cf1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
6 changes: 3 additions & 3 deletions source/EngineInterface/SimulationParametersEditService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
12 changes: 6 additions & 6 deletions source/EngineInterface/SimulationParametersEditService.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
#include "Base/Singleton.h"
#include "SimulationParameters.h"

struct RadiationStrengths
{
std::vector<float> values;
std::set<int> pinned;
};

class SimulationParametersEditService
{
MAKE_SINGLETON(SimulationParametersEditService);

public:
struct RadiationStrengths
{
std::vector<float> values;
std::set<int> 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;
Expand Down
60 changes: 60 additions & 0 deletions source/EngineTests/SimulationParametersEditServiceTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "EngineInterface/SimulationParametersEditService.h"

#include <gtest/gtest.h>

#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]);
}
12 changes: 6 additions & 6 deletions source/Gui/RadiationSourcesWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void RadiationSourcesWindow::processBaseTab()
&parameters.baseStrengthRatioPinned)) {
newRatios.pinned.insert(0);
editService.adaptRadiationStrengths(newRatios, ratios, 0);
editService.applyRadiationStrengths(parameters, newRatios);
editService.applyRadiationStrengthValues(parameters, newRatios);
}

if (parameters != lastParameters) {
Expand Down Expand Up @@ -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(); };
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 21a7cf1

Please sign in to comment.