Skip to content

Commit af94f9f

Browse files
authored
Merge pull request #628 from vizzuhq/simplify_range
Simplify range
2 parents 2952c2c + d77cb62 commit af94f9f

File tree

17 files changed

+112
-163
lines changed

17 files changed

+112
-163
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Fix invalid read/write when animation is contiguous (onFinish callback calls setKeyframe).
8+
- Waterfall chart preset not aligned.
9+
- Split chart count negative values too.
10+
511
## [0.16.0] - 2024-11-28
612

713
### Fixed

src/apps/weblib/ts-api/plugins/presetconfigs.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ export const presetConfigs = {
4646
y: { set: [{ name: 'y' }, { name: 'x' }] },
4747
label: { set: [{ name: 'y' }] },
4848
color: { set: [{ name: 'y' }] }
49-
},
50-
align: 'stretch'
49+
}
5150
},
5251
mekko: {
5352
channels: {

src/base/geom/rect.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ struct Rect
9191

9292
void setHSize(const Math::Range<> &range)
9393
{
94-
setLeft(range.getMin());
95-
setRight(range.getMax());
94+
setLeft(range.min);
95+
setRight(range.max);
9696
}
9797

9898
void setVSize(const Math::Range<> &range)
9999
{
100-
setBottom(range.getMin());
101-
setTop(range.getMax());
100+
setBottom(range.min);
101+
setTop(range.max);
102102
}
103103

104104
[[nodiscard]] Rect bottomHalf() const

src/base/math/range.h

+21-48
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,6 @@ template <std::floating_point T = double> struct Range
2121
constexpr static auto less = Floating::less;
2222
constexpr static auto is_zero = Floating::is_zero;
2323

24-
constexpr static Range<T> Raw(const T &min, const T &max)
25-
{
26-
Range<T> range;
27-
range.min = min;
28-
range.max = max;
29-
return range;
30-
}
31-
32-
constexpr Range() :
33-
min(std::numeric_limits<T>::max()),
34-
max(std::numeric_limits<T>::lowest())
35-
{}
36-
37-
Range(const T &x, const T &y) :
38-
min(std::min(x, y, less)),
39-
max(std::max(x, y, less))
40-
{}
41-
4224
[[nodiscard]] bool isReal() const
4325
{
4426
return min != std::numeric_limits<T>::max()
@@ -52,7 +34,7 @@ template <std::floating_point T = double> struct Range
5234
min = std::min(min, value, less);
5335
}
5436

55-
void include(const Range<T> &range)
37+
void include(const Range &range)
5638
{
5739
include(range.min);
5840
include(range.max);
@@ -63,7 +45,7 @@ template <std::floating_point T = double> struct Range
6345
return !less(value, min) && !less(max, value);
6446
}
6547

66-
[[nodiscard]] bool includes(const Range<T> &range) const
48+
[[nodiscard]] bool includes(const Range &range) const
6749
{
6850
return !less(range.max, min) && !less(max, range.min);
6951
}
@@ -79,57 +61,57 @@ template <std::floating_point T = double> struct Range
7961
return value * size() + min;
8062
}
8163

82-
[[nodiscard]] Range<T> scale(const Range<T> &range) const
64+
[[nodiscard]] Range scale(const Range &range) const
8365
{
84-
return Range<T>(scale(range.min), scale(range.max));
66+
return {scale(range.min), scale(range.max)};
8567
}
8668

8769
[[nodiscard]] T normalize(const T &value) const
8870
{
8971
return is_zero(max) ? 0 : value / max;
9072
}
9173

92-
[[nodiscard]] Range<T> normalize(const Range<T> &range) const
74+
[[nodiscard]] Range normalize(const Range &range) const
9375
{
94-
return Range<T>(normalize(range.min), normalize(range.max));
76+
return {normalize(range.min), normalize(range.max)};
9577
}
9678

97-
bool operator==(const Range<T> &other) const
79+
bool operator==(const Range &other) const
9880
{
9981
return min == other.min && max == other.max;
10082
}
10183

102-
Range<T> operator+(double shift) const
84+
Range operator+(double shift) const
10385
{
104-
return Range<T>(min + shift, max + shift);
86+
return {min + shift, max + shift};
10587
}
10688

107-
Range<T> operator+(const Range<T> &other) const
89+
Range operator+(const Range &other) const
10890
{
109-
return Range<T>(min + other.min, max + other.max);
91+
return {min + other.min, max + other.max};
11092
}
11193

112-
Range<T> operator-(double shift) const
94+
Range operator-(double shift) const
11395
{
114-
return Range<T>(min - shift, max - shift);
96+
return {min - shift, max - shift};
11597
}
11698

117-
Range<T> operator*(double factor) const
99+
Range operator*(double factor) const
118100
{
119-
return Range<T>(min * factor, max * factor);
101+
return {min * factor, max * factor};
120102
}
121103

122-
Range<T> operator/(double factor) const
104+
Range operator/(double factor) const
123105
{
124-
return Range<T>(min / factor, max / factor);
106+
return {min / factor, max / factor};
125107
}
126108

127-
Range<T> operator*(const Transform &transf)
109+
Range operator*(const Transform &transf)
128110
{
129111
return *this * transf.factor + transf.shift;
130112
}
131113

132-
Transform operator/(const Range<T> range)
114+
Transform operator/(const Range range)
133115
{
134116
auto factor = range.size() != 0 ? size() / range.size() : 0;
135117
auto shift = min - range.min * factor;
@@ -140,17 +122,8 @@ template <std::floating_point T = double> struct Range
140122

141123
[[nodiscard]] T size() const { return max - min; }
142124

143-
[[nodiscard]] T getMin() const { return min; }
144-
[[nodiscard]] T getMax() const { return max; }
145-
146-
consteval static auto members()
147-
{
148-
return std::tuple{&Range::min, &Range::max};
149-
}
150-
151-
protected:
152-
T min;
153-
T max;
125+
T min{std::numeric_limits<T>::max()};
126+
T max{std::numeric_limits<T>::lowest()};
154127
};
155128

156129
}

src/base/math/segmentedfunc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ template <typename T, class CRTP> struct SegmentedFunction
8282
});
8383
return interpolate(it->value,
8484
std::next(it)->value,
85-
Range{it->pos, std::next(it)->pos}.rescale(pos));
85+
Range<>{it->pos, std::next(it)->pos}.rescale(pos));
8686
}
8787

8888
protected:

src/chart/generator/axis.cpp

+24-32
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ MeasureAxis::MeasureAxis(const Math::Range<> &interval,
105105
const std::string_view &unit,
106106
const std::optional<double> &step) :
107107
enabled(true),
108-
range(interval.isReal() ? interval : Math::Range<>::Raw({}, {})),
108+
range(interval.isReal() ? interval : Math::Range<>{{}, {}}),
109109
series(std::move(series)),
110110
unit(std::string{unit}),
111111
step(step ? *step : Math::Renard::R5().ceil(range.size() / 5.0))
@@ -120,7 +120,7 @@ MeasureAxis::MeasureAxis(const Math::Range<> &interval,
120120
double MeasureAxis::origo() const
121121
{
122122
if (range.size() == 0) return 0;
123-
return -range.getMin() / range.size();
123+
return -range.min / range.size();
124124
}
125125

126126
MeasureAxis interpolate(const MeasureAxis &op0,
@@ -140,13 +140,12 @@ MeasureAxis interpolate(const MeasureAxis &op0,
140140

141141
if (auto s0Zero = is_zero(s0), s1Zero = is_zero(s1);
142142
s0Zero && s1Zero) {
143-
res.range = Math::Range<>::Raw(
144-
Math::interpolate(op0.range.getMin(),
145-
op1.range.getMin(),
146-
factor),
147-
Math::interpolate(op0.range.getMax(),
148-
op1.range.getMax(),
149-
factor));
143+
res.range = {Math::interpolate(op0.range.min,
144+
op1.range.min,
145+
factor),
146+
Math::interpolate(op0.range.max,
147+
op1.range.max,
148+
factor)};
150149
res.step = interpolate(op0.step, op1.step, factor);
151150
}
152151
else if (s1Zero) {
@@ -157,18 +156,16 @@ MeasureAxis interpolate(const MeasureAxis &op0,
157156
0.0,
158157
factor);
159158

160-
res.range = Math::Range<>::Raw(op1.range.middle()
161-
- middleAt * size,
159+
res.range = {op1.range.middle() - middleAt * size,
162160
op1.range.middle()
163-
+ (factor == 1.0 ? 0.0 : (1 - middleAt) * size));
161+
+ (factor == 1.0 ? 0.0 : (1 - middleAt) * size)};
164162

165163
auto step = op0.step.get() / s0 * size;
166164
auto max = std::copysign(MAX, step);
167165

168166
res.step = interpolate(op0.step,
169167
Anim::Interpolated{max},
170-
Math::Range<>::Raw(op0.step.get(), max)
171-
.rescale(step));
168+
Math::Range<>{op0.step.get(), max}.rescale(step));
172169
}
173170
else if (s0Zero) {
174171
auto size = factor == 0.0 ? MAX : s1 / factor;
@@ -177,18 +174,16 @@ MeasureAxis interpolate(const MeasureAxis &op0,
177174
op1.range.rescale(op0.range.middle()),
178175
factor);
179176

180-
res.range = Math::Range<>::Raw(op0.range.middle()
181-
- middleAt * size,
177+
res.range = {op0.range.middle() - middleAt * size,
182178
op0.range.middle()
183-
+ (factor == 0.0 ? 0.0 : (1 - middleAt) * size));
179+
+ (factor == 0.0 ? 0.0 : (1 - middleAt) * size)};
184180

185181
auto step = op1.step.get() / s1 * size;
186182
auto max = std::copysign(MAX, step);
187183

188184
res.step = interpolate(op1.step,
189185
Anim::Interpolated{max},
190-
Math::Range<>::Raw(op1.step.get(), max)
191-
.rescale(step));
186+
Math::Range<>{op1.step.get(), max}.rescale(step));
192187
}
193188
else {
194189
auto s0Inv = 1 / s0;
@@ -199,15 +194,14 @@ MeasureAxis interpolate(const MeasureAxis &op0,
199194

200195
const auto size = is_zero(interp) ? MAX : 1 / interp;
201196

202-
res.range = Math::Range<>::Raw(
203-
Math::interpolate(op0.range.getMin() * s0Inv,
204-
op1.range.getMin() * s1Inv,
197+
res.range = {Math::interpolate(op0.range.min * s0Inv,
198+
op1.range.min * s1Inv,
199+
factor)
200+
* size,
201+
Math::interpolate(op0.range.max * s0Inv,
202+
op1.range.max * s1Inv,
205203
factor)
206-
* size,
207-
Math::interpolate(op0.range.getMax() * s0Inv,
208-
op1.range.getMax() * s1Inv,
209-
factor)
210-
* size);
204+
* size};
211205

212206
auto step = Math::interpolate(op0.step.get() * s0Inv,
213207
op1.step.get() * s1Inv,
@@ -218,19 +212,17 @@ MeasureAxis interpolate(const MeasureAxis &op0,
218212
op0sign == std::signbit(op1.step.get()))
219213
res.step = interpolate(op0.step,
220214
op1.step,
221-
Math::Range<>::Raw(op0.step.get(), op1.step.get())
215+
Math::Range<>{op0.step.get(), op1.step.get()}
222216
.rescale(step));
223217
else if (auto max = std::copysign(MAX, step);
224218
op0sign == std::signbit(step))
225219
res.step = interpolate(op0.step,
226220
Anim::Interpolated{max},
227-
Math::Range<>::Raw(op0.step.get(), max)
228-
.rescale(step));
221+
Math::Range<>{op0.step.get(), max}.rescale(step));
229222
else
230223
res.step = interpolate(op1.step,
231224
Anim::Interpolated{max},
232-
Math::Range<>::Raw(op1.step.get(), max)
233-
.rescale(step));
225+
Math::Range<>{op1.step.get(), max}.rescale(step));
234226
}
235227

236228
res.unit = interpolate(op0.unit, op1.unit, factor);

src/chart/generator/axis.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct ChannelStats
5454
struct MeasureAxis
5555
{
5656
::Anim::Interpolated<bool> enabled{false};
57-
Math::Range<> range = Math::Range<>::Raw(0, 1);
57+
Math::Range<> range{0, 1};
5858
std::string series;
5959
::Anim::String unit;
6060
::Anim::Interpolated<double> step{1.0};
@@ -168,8 +168,8 @@ struct DimensionAxis
168168
[[nodiscard]] bool operator()(const Item &lhs,
169169
const Item &rhs) const
170170
{
171-
return Math::Floating::less(lhs.range.getMin(),
172-
rhs.range.getMin());
171+
return Math::Floating::less(lhs.range.min,
172+
rhs.range.min);
173173
}
174174
};
175175
return std::multiset<std::reference_wrapper<Item>,

0 commit comments

Comments
 (0)