Skip to content

Commit 829793d

Browse files
committed
Refactor SegmentedFunction
1 parent 2d95b9a commit 829793d

File tree

8 files changed

+58
-55
lines changed

8 files changed

+58
-55
lines changed

src/base/anim/easinggradient.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ namespace Anim
1212
class EasingGradient : protected Math::SegmentedFunction<double>
1313
{
1414
public:
15-
using Math::SegmentedFunction<double>::SegmentedFunction;
15+
friend struct SegmentedFunction;
16+
using SegmentedFunction::SegmentedFunction;
1617

17-
static EasingGradient Bezier(const Geom::Point &p1,
18+
[[nodiscard]] static EasingGradient Bezier(const Geom::Point &p1,
1819
const Geom::Point &p2,
1920
size_t stepCount = 10);
2021

21-
double operator()(double t) const { return at(t); }
22+
using SegmentedFunction::operator();
2223
};
2324

2425
}

src/base/gfx/colorgradient.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
#include "color.h"
1212

1313
template struct Math::SegmentedFunction<Gfx::Color>;
14+
template Gfx::ColorGradient
15+
Math::SegmentedFunction<Gfx::Color>::operator*<Gfx::ColorGradient>(
16+
this const Gfx::ColorGradient &,
17+
double value);
18+
template Gfx::ColorGradient
19+
Math::SegmentedFunction<Gfx::Color>::operator+<Gfx::ColorGradient>(
20+
this const Gfx::ColorGradient &,
21+
const Gfx::ColorGradient &);
1422

1523
namespace Gfx
1624
{

src/base/gfx/colorgradient.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@ struct ColorGradient : Math::SegmentedFunction<Color>
1717

1818
using SegmentedFunction::SegmentedFunction;
1919

20-
explicit ColorGradient(SegmentedFunction gradient) :
21-
SegmentedFunction(std::move(gradient))
22-
{}
23-
2420
explicit ColorGradient(
2521
const std::string &stoplist = std::string());
2622

2723
explicit operator std::string() const;
28-
29-
using SegmentedFunction::at;
3024
};
3125

3226
}

src/base/math/segmentedfunc.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ template <typename T> struct SegmentedFunction
2626
stops(std::move(stops))
2727
{}
2828

29-
SegmentedFunction operator*(double value) const;
30-
SegmentedFunction operator+(const SegmentedFunction &other) const;
31-
bool operator==(const SegmentedFunction &other) const;
29+
template <class Self>
30+
[[nodiscard]] Self operator*(this const Self &, double);
3231

33-
[[nodiscard]] T at(double pos) const;
32+
template <class Self>
33+
[[nodiscard]] Self operator+(this const Self &, const Self &);
34+
35+
[[nodiscard]] bool operator==(
36+
const SegmentedFunction &other) const = default;
37+
38+
[[nodiscard]] T operator()(double pos) const;
3439
};
3540

3641
}

src/base/math/segmentedfunc.tpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,61 @@
11
#ifndef MATH_SEGMENTEDFUNC_TPP
22
#define MATH_SEGMENTEDFUNC_TPP
33

4+
#include <ranges>
5+
46
#include "base/math/interpolation.h"
57
#include "base/math/range.h"
68

79
#include "segmentedfunc.h"
810

911
template <typename T>
10-
T Math::SegmentedFunction<T>::at(double pos) const
12+
T Math::SegmentedFunction<T>::operator()(double pos) const
1113
{
1214
if (stops.empty()) return T();
13-
if (stops.size() == 1) return stops.at(0).value;
14-
15-
if (pos < stops.front().pos) return stops.front().value;
16-
if (pos > stops.back().pos) return stops.back().value;
15+
if (stops.size() == 1 || pos < stops.front().pos)
16+
return stops.front().value;
1717

18-
for (auto i = 1U; i < stops.size(); ++i) {
19-
auto &[ppos, pval] = stops[i - 1];
20-
auto &[cpos, cval] = stops[i];
21-
if (ppos <= pos && pos <= cpos)
22-
return interpolate(pval,
23-
cval,
24-
Range{ppos, cpos}.rescale(pos));
25-
}
18+
if (auto it = std::ranges::adjacent_find(stops,
19+
[pos](auto &&cur, auto &&next)
20+
{
21+
return cur.pos <= pos && pos <= next.pos;
22+
});
23+
it != stops.end())
24+
return interpolate(it->value,
25+
std::next(it)->value,
26+
Range{it->pos, std::next(it)->pos}.rescale(pos));
2627

2728
return stops.back().value;
2829
}
2930

3031
template <typename T>
31-
bool Math::SegmentedFunction<T>::operator==(
32-
const SegmentedFunction &other) const
33-
{
34-
return stops == other.stops;
35-
}
36-
37-
template <typename T>
38-
Math::SegmentedFunction<T> Math::SegmentedFunction<T>::operator*(
39-
double value) const
32+
template <class Self>
33+
Self Math::SegmentedFunction<T>::operator*(this const Self &self,
34+
double value)
4035
{
41-
auto res = *this;
36+
auto res = self;
4237
for (auto &stop : res.stops) stop.value = stop.value * value;
4338
return res;
4439
}
4540

4641
template <typename T>
47-
Math::SegmentedFunction<T> Math::SegmentedFunction<T>::operator+(
48-
const SegmentedFunction &other) const
42+
template <class Self>
43+
Self Math::SegmentedFunction<T>::operator+(this const Self &self,
44+
const Self &other)
4945
{
50-
SegmentedFunction<T> res;
46+
Self res;
47+
auto &stops = self.stops;
5148

5249
for (auto it0 = stops.begin(), it1 = other.stops.begin();
5350
it0 != stops.end() || it1 != other.stops.end();) {
5451
if (it1 == other.stops.end() || it0->pos < it1->pos) {
5552
res.stops.emplace_back(it0->pos,
56-
it0->value + other.at(it0->pos));
53+
it0->value + other(it0->pos));
5754
++it0;
5855
}
5956
else if (it0 == stops.end() || it1->pos < it0->pos) {
6057
res.stops.emplace_back(it1->pos,
61-
it1->value + at(it1->pos));
58+
it1->value + self(it1->pos));
6259
++it1;
6360
}
6461
else {

src/chart/rendering/colorbuilder.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
namespace Vizzu::Draw
1414
{
1515

16-
ColorBuilder::ColorBuilder(const LighnessRange &lighnessRange,
16+
ColorBuilder::ColorBuilder(const LightnessRange &lightnessRange,
1717
const Anim::Interpolated<Gfx::ColorPalette> &palette,
1818
const Gfx::ColorGradient &gradient) :
19-
lighnessRange(lighnessRange),
19+
lightnessRange(lightnessRange),
2020
gradient(gradient),
2121
palette(palette)
2222
{}
@@ -37,7 +37,7 @@ Gfx::Color ColorBuilder::render(
3737
{
3838
return base.getLightness();
3939
});
40-
return lightnessAdjusted(gradient.get().at(pos), lightness);
40+
return lightnessAdjusted(gradient(pos), lightness);
4141
}
4242
return colorBase.combine(
4343
[this](const Gen::ColorBase &base)
@@ -56,16 +56,15 @@ Gfx::Color ColorBuilder::render(const Gen::ColorBase &colorBase) const
5656
Gfx::Color ColorBuilder::lightnessAdjusted(const Gfx::Color &color,
5757
double lightness) const
5858
{
59-
auto factor = lighnessRange.scale(lightness);
59+
auto factor = lightnessRange.scale(lightness);
6060
return color.lightnessScaled(factor);
6161
}
6262

6363
Gfx::Color ColorBuilder::baseColor(
6464
const Gen::ColorBase &colorBase) const
6565
{
66-
return colorBase.isDiscrete()
67-
? indexedColor(colorBase.getIndex())
68-
: gradient.get().at(colorBase.getPos());
66+
return colorBase.isDiscrete() ? indexedColor(colorBase.getIndex())
67+
: gradient(colorBase.getPos());
6968
}
7069

7170
[[nodiscard]] Gfx::Color ColorBuilder::indexedColor(

src/chart/rendering/colorbuilder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace Vizzu::Draw
1313

1414
struct ColorBuilder
1515
{
16-
using LighnessRange = Math::Range<double>;
16+
using LightnessRange = Math::Range<double>;
1717

18-
ColorBuilder(const LighnessRange &lighnessRange,
18+
ColorBuilder(const LightnessRange &lightnessRange,
1919
const ::Anim::Interpolated<Gfx::ColorPalette> &palette,
2020
const Gfx::ColorGradient &gradient);
2121

@@ -26,7 +26,7 @@ struct ColorBuilder
2626
const Gen::ColorBase &colorBase) const;
2727

2828
private:
29-
LighnessRange lighnessRange;
29+
LightnessRange lightnessRange;
3030

3131
std::reference_wrapper<const Gfx::ColorGradient> gradient;
3232
std::reference_wrapper<

src/chart/rendering/drawlegend.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ void DrawLegend::colorBar(const Info &info,
341341
info.canvas.save();
342342

343343
info.canvas.setBrushGradient({rect.leftSide(),
344-
Gfx::ColorGradient{*rootStyle.plot.marker.colorGradient
345-
* info.measureWeight}});
344+
*rootStyle.plot.marker.colorGradient * info.measureWeight});
346345
info.canvas.setLineColor(Gfx::Color::Transparent());
347346
info.canvas.setLineWidth(0);
348347

@@ -372,8 +371,8 @@ void DrawLegend::lightnessBar(const Info &info,
372371

373372
info.canvas.save();
374373

375-
info.canvas.setBrushGradient({rect.leftSide(),
376-
Gfx::ColorGradient{gradient * info.measureWeight}});
374+
info.canvas.setBrushGradient(
375+
{rect.leftSide(), gradient * info.measureWeight});
377376
info.canvas.setLineColor(Gfx::Color::Transparent());
378377
info.canvas.setLineWidth(0);
379378

0 commit comments

Comments
 (0)