Skip to content

Commit 97707cb

Browse files
committed
Remove segmentedfunc.tpp
1 parent 8e6fdab commit 97707cb

File tree

6 files changed

+36
-64
lines changed

6 files changed

+36
-64
lines changed

src/base/anim/easing.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ Easing::Easing(const std::string &name)
1616
{
1717
if (name.empty()) return;
1818

19-
auto nameCopy = name;
20-
Text::SmartString::trim(nameCopy);
21-
22-
if (nameCopy == "none") { func = &Easing::none; }
19+
if (auto nameCopy = Text::SmartString::trim_view(name);
20+
nameCopy == "none") {
21+
func = &Easing::none;
22+
}
2323
else if (nameCopy == "linear") {
2424
func = &Easing::linear;
2525
}
@@ -45,7 +45,7 @@ Easing::Easing(const std::string &name)
4545
func = EasingGradient::Bezier(Geom::Point{0.42, 0},
4646
Geom::Point{0.58, 1});
4747
}
48-
else if (const Text::FuncString f(nameCopy);
48+
else if (const Text::FuncString f(name);
4949
f.getName() == "cubic-bezier") {
5050
if (f.getParams().size() != 4)
5151
throw std::logic_error("parameter count missmatch");

src/base/anim/easinggradient.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
#include "base/geom/bezier.h"
88
#include "base/geom/point.h"
9-
#include "base/math/segmentedfunc.h"
10-
#include "base/math/segmentedfunc.tpp"
11-
12-
template struct Math::SegmentedFunction<double, Anim::EasingGradient>;
139

1410
namespace Anim
1511
{

src/base/gfx/color.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,18 @@ Color Color::fromString(const std::string &string)
4747
if (const Text::FuncString f(string); !f.isEmpty()) {
4848
using Conv::parse;
4949

50-
if (f.getName() == "rgb") {
51-
auto ps = f.getParams();
52-
if (ps.size() != 3)
53-
throw std::logic_error("invalid color string");
50+
auto &&ps = f.getParams();
51+
if (f.getName() == "rgb" && ps.size() == 3) {
5452
return RGBA(parse<uint32_t>(ps.at(0)),
5553
parse<uint32_t>(ps.at(1)),
5654
parse<uint32_t>(ps.at(2)));
5755
}
58-
if (f.getName() == "rgba") {
59-
auto ps = f.getParams();
60-
if (ps.size() != 4)
61-
throw std::logic_error("invalid color string");
56+
if (f.getName() == "rgba" && ps.size() == 4) {
6257
return RGBA(parse<uint32_t>(ps.at(0)),
6358
parse<uint32_t>(ps.at(1)),
6459
parse<uint32_t>(ps.at(2)),
6560
static_cast<uint8_t>(parse<double>(ps.at(3)) * 255));
6661
}
67-
throw std::logic_error("invalid color string");
6862
}
6963
throw std::logic_error("invalid color string");
7064
}

src/base/gfx/colorgradient.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,25 @@
44
#include <stdexcept>
55
#include <string>
66

7-
#include "base/math/segmentedfunc.h"
8-
#include "base/math/segmentedfunc.tpp"
97
#include "base/text/smartstring.h"
108

119
#include "color.h"
1210

13-
template struct Math::SegmentedFunction<Gfx::Color,
14-
Gfx::ColorGradient>;
15-
1611
namespace Gfx
1712
{
1813

1914
ColorGradient::ColorGradient(const std::string &stoplist)
2015
{
21-
auto stopStrings = Text::SmartString::split(stoplist, ',', true);
22-
auto pos = 0.0;
23-
for (auto &stopString : stopStrings) {
24-
auto parts = Text::SmartString::split(stopString, ' ', true);
25-
if (parts.size() == 2) {
26-
pos = std::stod(parts[1]);
27-
stops.emplace_back(pos, Color::fromString(parts[0]));
28-
}
16+
for (const auto &stopString :
17+
Text::SmartString::split(stoplist, ',', true))
18+
if (auto &&parts =
19+
Text::SmartString::split(stopString, ' ', true);
20+
parts.size() == 2)
21+
stops.emplace_back(std::stod(parts[1]),
22+
Color::fromString(parts[0]));
2923
else
3024
throw std::logic_error(
3125
"invalid gradient stop: " + stopString);
32-
}
3326
}
3427

3528
ColorGradient::operator std::string() const

src/base/math/segmentedfunc.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#include <vector>
55

6+
#include "interpolation.h"
7+
#include "range.h"
8+
69
namespace Math
710
{
811

@@ -65,7 +68,24 @@ template <typename T, class CRTP> struct SegmentedFunction
6568
[[nodiscard]] bool operator==(
6669
const SegmentedFunction &other) const = default;
6770

68-
[[nodiscard]] T operator()(double pos) const;
71+
[[nodiscard]] T operator()(double pos) const
72+
{
73+
if (stops.empty()) return T();
74+
if (stops.size() == 1 || pos < stops.front().pos)
75+
return stops.front().value;
76+
77+
if (auto it = std::ranges::adjacent_find(stops,
78+
[pos](auto &&cur, auto &&next)
79+
{
80+
return cur.pos <= pos && pos <= next.pos;
81+
});
82+
it != stops.end())
83+
return interpolate(it->value,
84+
std::next(it)->value,
85+
Range{it->pos, std::next(it)->pos}.rescale(pos));
86+
87+
return stops.back().value;
88+
}
6989
};
7090

7191
}

src/base/math/segmentedfunc.tpp

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)