Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axis refactor v9 - Interpolated refactor #610

Merged
merged 9 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 47 additions & 90 deletions src/base/anim/interpolated.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ inline InterpolateIndex operator||(const InterpolateIndex &lhs,
template <typename Type> class Interpolated
{
public:
std::array<Weighted<Type>, 2> values;
Refl::EnumArray<InterpolateIndex, Weighted<Type>> values;
bool has_second{};

constexpr Interpolated() noexcept(
Expand All @@ -73,32 +73,30 @@ template <typename Type> class Interpolated
Interpolated &operator=(Interpolated &&) noexcept(
std::is_nothrow_move_assignable_v<Type>) = default;

constexpr explicit Interpolated(Type value)
{
values[0] = Weighted<Type>(std::move(value));
}
constexpr explicit Interpolated(Type value) :
values{{Weighted{std::move(value)}}}
{}

explicit Interpolated(const std::string &str)
requires(!std::is_same_v<Type, std::string>)
{
values[0] = Weighted<Type>(Conv::parse<Type>(str));
}
: values{{Weighted{Conv::parse<Type>(str)}}}
{}

Interpolated &operator=(Type value)
{
values[0] = Weighted<Type>(std::move(value));
values[first] = Weighted{std::move(value)};
return *this;
}

Interpolated &operator=(Weighted<Type> weightedValue)
{
values[0] = std::move(weightedValue);
values[first] = std::move(weightedValue);
return *this;
}

[[nodiscard]] bool hasOneValue() const
{
return !has_second && values[0].hasValue();
return !has_second && values[first].hasValue();
}

[[nodiscard]] bool interpolates() const { return has_second; }
Expand All @@ -110,33 +108,30 @@ template <typename Type> class Interpolated

[[nodiscard]] const Type &get() const
{
if (!has_second) return values[0].value;
if (!has_second) return values[first].value;

throw std::logic_error("Invalid Weigthed Pair");
}

[[nodiscard]] const Weighted<Type> &get_or_first(
InterpolateIndex index) const
{
return values[has_second && static_cast<bool>(index)];
return values[static_cast<InterpolateIndex>(
has_second && static_cast<bool>(index))];
}

template <class T>
[[nodiscard]] std::optional<InterpolateIndex> get_index(
const T &type) const
{
if (values[0].value == type) return first;
if (has_second && values[1].value == type) return second;
if (values[first].value == type) return first;
if (has_second && values[second].value == type) return second;
return {};
}

template <class T>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
[[nodiscard]] auto get(T &&) const = delete;

[[nodiscard]] auto toString() const
{
if (!has_second) return Conv::toString(values[0].value);
if (!has_second) return Conv::toString(values[first].value);

throw std::logic_error("Invalid Weigthed Pair");
}
Expand All @@ -153,97 +148,57 @@ template <typename Type> class Interpolated
bool operator==(const Interpolated<Type> &other) const
{
return has_second == other.has_second
&& values[0] == other.values[0]
&& (!has_second || values[1] == other.values[1]);
&& values[first] == other.values[first]
&& (!has_second
|| values[second] == other.values[second]);
}

bool operator==(const Type &other) const
{
return !has_second && values[0].weight == 1.0
&& values[0].value == other;
}

bool operator<(const Interpolated<Type> &other) const
{
if (has_second && other.has_second)
throw std::logic_error("cannot compare weigthed pairs");
if (!other.has_second) return false;
return values[0] < other.values[0];
return hasOneValue() && values[first].value == other;
schaumb marked this conversation as resolved.
Show resolved Hide resolved
}

template <class T> void visit(T &&branch) const
{
if (values[0].hasValue()) branch(first, values[0]);
if (has_second && values[1].hasValue())
std::forward<T>(branch)(second, values[1]);
if (values[first].hasValue()) branch(first, values[first]);
if (has_second && values[second].hasValue())
std::forward<T>(branch)(second, values[second]);
}

template <class U = void,
class Fun,
class Fun = std::identity,
class T = std::conditional_t<std::is_void_v<U>,
std::invoke_result_t<Fun, Type>,
std::remove_cvref_t<std::invoke_result_t<Fun, Type>>,
U>>
T combine(Fun &&branch) const
T combine(Fun &&branch = {}) const
{
auto res = static_cast<T>(branch(values[0].value))
* values[0].weight;
auto res = static_cast<T>(branch(values[first].value))
* values[first].weight;
if (has_second)
res = res
+ static_cast<T>(
std::forward<Fun>(branch)(values[1].value))
* values[1].weight;
std::forward<Fun>(branch)(values[second].value))
* values[second].weight;
return static_cast<T>(res);
}

[[nodiscard]] bool contains(const Type &value) const
template <class T = Type>
[[nodiscard]] bool contains(const T &value) const
{
if (value == values[0].value) return true;
if (has_second && value == values[1].value) return true;
if (value == values[first].value) return true;
if (has_second && value == values[second].value) return true;
return false;
}

template <class T = double, class U>
[[nodiscard]] T factor(const U &value) const
{
double res{};
if (values[0].value == value) res += values[0].weight;
if (has_second && values[1].value == value)
res += values[1].weight;
if (values[first].value == value) res += values[first].weight;
if (has_second && values[second].value == value)
res += values[second].weight;
return T{res};
}

template <typename T = Type> [[nodiscard]] T calculate() const
{
auto res = static_cast<T>(this->values[0].value)
* this->values[0].weight;
if (has_second)
res = res
+ static_cast<T>(this->values[1].value)
* this->values[1].weight;
return res;
}

template <typename T = Type> [[nodiscard]] T min() const
{
using Less = std::conditional_t<std::floating_point<T>,
decltype(Math::Floating::less),
std::less<T>>;
return !has_second ? this->values[0].value
: std::min(this->values[0].value,
this->values[1].value,
Less{});
}

template <typename T = Type> [[nodiscard]] T max() const
{
using Less = std::conditional_t<std::floating_point<T>,
decltype(Math::Floating::less),
std::less<T>>;
return !has_second ? this->values[0].value
: std::max(this->values[0].value,
this->values[1].value,
Less{});
}
};

template <typename Type>
Expand All @@ -258,17 +213,19 @@ Interpolated<Type> interpolate(const Interpolated<Type> &op0,
throw std::logic_error("Cannot interpolate Weigthed Pairs");

Interpolated<Type> res;
if (op0.values[0].value == op1.values[0].value) {
res.values[0].value = op0.values[0].value;
res.values[0].weight = Math::interpolate(op0.values[0].weight,
op1.values[0].weight,
factor);
if (op0.values[first].value == op1.values[first].value) {
res.values[first].value = op0.values[first].value;
res.values[first].weight =
Math::interpolate(op0.values[first].weight,
op1.values[first].weight,
factor);
}
else {
res.values[0].value = op0.values[0].value;
res.values[0].weight = op0.values[0].weight * (1.0 - factor);
res.values[1].value = op1.values[0].value;
res.values[1].weight = op1.values[0].weight * factor;
res.values[first].value = op0.values[first].value;
res.values[first].weight =
op0.values[first].weight * (1.0 - factor);
res.values[second].value = op1.values[first].value;
res.values[second].weight = op1.values[first].weight * factor;
res.has_second = true;
}
return res;
Expand Down
12 changes: 6 additions & 6 deletions src/base/geom/rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ struct Rect
[[nodiscard]] double width() const { return size.x; }
[[nodiscard]] double height() const { return size.y; }

[[nodiscard]] Math::Range<double> hSize() const
[[nodiscard]] Math::Range<> hSize() const
{
return {left(), right()};
}
[[nodiscard]] Math::Range<double> vSize() const
[[nodiscard]] Math::Range<> vSize() const
{
return {bottom(), top()};
}

[[nodiscard]] Math::Range<double> x() const
[[nodiscard]] Math::Range<> x() const
{
return {pos.x, pos.x + size.x};
}
[[nodiscard]] Math::Range<double> y() const
[[nodiscard]] Math::Range<> y() const
{
return {pos.y, pos.y + size.y};
}
Expand Down Expand Up @@ -89,13 +89,13 @@ struct Rect
void setWidth(double val) { size.x = val; }
void setHeight(double val) { size.y = val; }

void setHSize(const Math::Range<double> &range)
void setHSize(const Math::Range<> &range)
{
setLeft(range.getMin());
setRight(range.getMax());
}

void setVSize(const Math::Range<double> &range)
void setVSize(const Math::Range<> &range)
{
setBottom(range.getMin());
setTop(range.getMax());
Expand Down
14 changes: 0 additions & 14 deletions src/base/math/floating.cpp

This file was deleted.

8 changes: 2 additions & 6 deletions src/base/math/floating.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
namespace Math::Floating
{

[[nodiscard]] int orderOfMagnitude(double value, double base = 10);

constexpr auto inline less = [](auto a, auto b)
{
static_assert(std::floating_point<decltype(a)>);
Expand All @@ -20,12 +18,10 @@ constexpr auto inline is_zero = [](auto value)
{
using F = decltype(value);
static_assert(std::floating_point<F>);
if constexpr (std::numeric_limits<F>::is_iec559) {
if constexpr (std::numeric_limits<F>::is_iec559)
return value == F{};
}
else {
else
return std::is_eq(std::weak_order(F{}, value));
}
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/base/math/normalizednumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ NormalizedNumber::NormalizedNumber(double value, double base) :
if (value != 0) {
positive = !std::signbit(value);
if (!positive) value = -value;
exponent = Floating::orderOfMagnitude(value, base);
simzer marked this conversation as resolved.
Show resolved Hide resolved
exponent = static_cast<int>(floor(log(value) / log(base)));
coefficient = value / pow(base, exponent);
coefficient = std::clamp(coefficient, 1.0, base);
}
Expand Down
2 changes: 0 additions & 2 deletions src/base/math/normalizednumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class NormalizedNumber
return positive ? coefficient : -coefficient;
}
void setExponent(int exp);

private:
};

class ScientificNumber : public NormalizedNumber
Expand Down
2 changes: 1 addition & 1 deletion src/base/math/range.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Math
{

template <std::floating_point T> struct Range
template <std::floating_point T = double> struct Range
{
struct Transform
{
Expand Down
3 changes: 2 additions & 1 deletion src/base/math/segmentedfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ template <typename T, class CRTP> struct SegmentedFunction

for (auto it0 = stops.begin(), it1 = other.stops.begin();
it0 != stops.end() || it1 != other.stops.end();) {
if (it1 == other.stops.end() || it0->pos < it1->pos) {
if (it1 == other.stops.end()
|| (it0 != stops.end() && it0->pos < it1->pos)) {
res.stops.emplace_back(it0->pos,
it0->value + other(it0->pos));
++it0;
Expand Down
Loading