Skip to content

Commit

Permalink
Added Helpers::Math with Lerp functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ffarrell17 committed Jun 19, 2023
1 parent 4d79f39 commit a1119bc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 35 deletions.
17 changes: 6 additions & 11 deletions src/Configuration/ConfigurationManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "ConfigurationManager.h"


void ConfigurationManager::Load(json& o_json)
{
logger::info("ConfigurationManager::Load");
Expand Down Expand Up @@ -179,21 +178,17 @@ void ConfigurationManager::LerpSettings(T& targetSettings, const T& outgoingSett

if constexpr ((typeid(target) == typeid(std::optional<bool>)) ||
(typeid(target) == typeid(std::optional<int>)) ||
//(typeid(target) == typeid(std::optional<uint32_t>)) ||
(typeid(target) == typeid(std::optional<uint32_t>)) ||
(typeid(target) == typeid(std::optional<float>))) {
if (current.value() == outgoing.value())
target = current.value();
else
target = (float)outgoing.value() + progress * ((float)current.value() - (float)outgoing.value());

target = Helpers::Math::Lerp(outgoing, current, progress);

} else if constexpr ((typeid(target) == typeid(std::optional<TODValue<bool>>)) ||
(typeid(target) == typeid(std::optional<TODValue<int>>)) ||
//(typeid(target) == typeid(std::optional<TODValue<uint32_t>>)) ||
(typeid(target) == typeid(std::optional<TODValue<uint32_t>>)) ||
(typeid(target) == typeid(std::optional<TODValue<float>>))) {
if (current->Get() == outgoing->Get())
target = current->Get();
else
target = (float)outgoing->Get() + progress * ((float)current->Get() - (float)outgoing->Get());

target = outgoing->LerpTo(current, progress);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Configuration/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "TODinfo.h"
#include "Helpers/Location.h"
#include "Helpers/Weather.h"
#include "Helpers/Math.h"
#include "boost/pfr.hpp"

namespace Configuration
Expand Down
42 changes: 18 additions & 24 deletions src/Configuration/TODValue.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "TODInfo.h"
#include "Helpers/UI.h"
#include "Helpers/Math.h"

namespace Configuration
{
Expand Down Expand Up @@ -86,40 +87,22 @@ namespace Configuration
if (todInfo.Exterior) {
switch (todInfo.TimePeriodType) {
case Configuration::TODInfo::TimePeriod::NightToDawn:

if (Night == Dawn)
return Night;
return static_cast<T>(Night + (Dawn - Night) * todInfo.TimePeriodPercentage);
return Helpers::Math::Lerp(Night, Dawn, todInfo.TimePeriodPercentage);

case Configuration::TODInfo::TimePeriod::DawnToSunrise:

if (Dawn == Sunrise)
return Dawn;
return static_cast<T>(Dawn + (Sunrise - Dawn) * todInfo.TimePeriodPercentage);
return Helpers::Math::Lerp(Dawn, Sunrise, todInfo.TimePeriodPercentage);

case Configuration::TODInfo::TimePeriod::SunriseToDay:

if (Sunrise == Day)
return Sunrise;
return static_cast<T>(Sunrise + (Day - Sunrise) * todInfo.TimePeriodPercentage);
return Helpers::Math::Lerp(Sunrise, Day, todInfo.TimePeriodPercentage);

case Configuration::TODInfo::TimePeriod::DayToSunset:

if (Day == Sunset)
return Day;
return static_cast<T>(Day + (Sunset - Day) * todInfo.TimePeriodPercentage);
return Helpers::Math::Lerp(Day, Sunset, todInfo.TimePeriodPercentage);

case Configuration::TODInfo::TimePeriod::SunsetToDusk:

if (Sunset == Dusk)
return Sunset;
return static_cast<T>(Sunset + (Dusk - Sunset) * todInfo.TimePeriodPercentage);
return Helpers::Math::Lerp(Sunset, Dusk, todInfo.TimePeriodPercentage);

case Configuration::TODInfo::TimePeriod::DuskToNight:

if (Dusk == Night)
return Dusk;
return static_cast<T>(Dusk + (Night - Dusk) * todInfo.TimePeriodPercentage);
return Helpers::Math::Lerp(Dusk, Night, todInfo.TimePeriodPercentage);

default:
return Day;
Expand Down Expand Up @@ -198,6 +181,17 @@ namespace Configuration
}
}

T LerpTo(std::optional<TODValue<T>>& end, float progress)
{
T startVal = Get();
T endVal = end->Get();

if (startVal == endVal)
return endVal;
else
return Helpers::Math::Lerp<T>(startVal, endVal, progress);
}

private:
bool _drawAllVals = true;
};
Expand Down
26 changes: 26 additions & 0 deletions src/Helpers/Math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <optional>
#include "..\Configuration\TODValue.h"

namespace Helpers
{
class Math
{
public:

template <typename T>
static T Lerp(T& start, T& end, float progress)
{
return static_cast<T>(start + (end - start) * progress);
}

template <typename T>
static T Lerp(std::optional<T>& start, std::optional<T>& end, float progress)
{
if (start.value() == end.value())
return end.value();
else
return Lerp<T>(start.value(), end.value(), progress);
}
};
}

0 comments on commit a1119bc

Please sign in to comment.