Skip to content

Commit fcb02d6

Browse files
committed
Fix ranges
1 parent d166817 commit fcb02d6

File tree

12 files changed

+84
-37
lines changed

12 files changed

+84
-37
lines changed

src/base/anim/interpolated.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ template <typename Type> class Interpolated
204204
return false;
205205
}
206206

207-
template <class T, class U>
207+
template <class T = double, class U>
208208
[[nodiscard]] T factor(const U &value) const
209209
{
210210
double res{};

src/base/math/range.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ template <std::floating_point T> struct Range
7474
return is_zero(s) ? 0.5 : (value - min) / s;
7575
}
7676

77-
[[nodiscard]] Range<T> rescale(const Range<T> &range) const
78-
{
79-
return Range<T>(rescale(range.min), rescale(range.max));
80-
}
81-
8277
[[nodiscard]] T scale(const T &value) const
8378
{
8479
return value * size() + min;

src/chart/generator/axis.cpp

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ MeasureAxis::MeasureAxis(Math::Range<double> interval,
4242
unit(std::string{unit}),
4343
origMeasureName(std::string{measName}),
4444
step(step ? *step : Math::Renard::R5().ceil(range.size() / 5.0))
45-
{}
45+
{
46+
if (std::signbit(this->step->value) != std::signbit(range.size()))
47+
this->step->value *= -1;
48+
}
4649

4750
bool MeasureAxis::operator==(const MeasureAxis &other) const
4851
{
@@ -68,8 +71,53 @@ MeasureAxis interpolate(const MeasureAxis &op0,
6871
interpolate(op0.origMeasureName, op1.origMeasureName, factor);
6972

7073
if (op0.enabled.get() && op1.enabled.get()) {
71-
res.range = Math::interpolate(op0.range, op1.range, factor);
72-
res.step = interpolate(op0.step, op1.step, factor);
74+
constexpr auto MAX = std::numeric_limits<double>::max() / 2;
75+
using Math::Floating::is_zero;
76+
77+
const auto s0 = op0.range.size();
78+
const auto s1 = op1.range.size();
79+
80+
const auto s0Inv = is_zero(s0) ? MAX : 1 / s0;
81+
const auto s1Inv = is_zero(s1) ? MAX : 1 / s1;
82+
83+
const auto interp = Math::interpolate(s0Inv, s1Inv, factor);
84+
85+
const auto s = is_zero(interp) ? MAX : 1 / interp;
86+
87+
res.range = Math::Range<double>::Raw(
88+
Math::interpolate(op0.range.getMin() * s0Inv,
89+
op1.range.getMin() * s1Inv,
90+
factor)
91+
* s,
92+
Math::interpolate(op0.range.getMax() * s0Inv,
93+
op1.range.getMax() * s1Inv,
94+
factor)
95+
* s);
96+
97+
auto step = Math::interpolate(op0.step.get() * s0Inv,
98+
op1.step.get() * s1Inv,
99+
factor)
100+
* s;
101+
102+
if (auto op0sign = std::signbit(op0.step.get());
103+
op0sign == std::signbit(op1.step.get()))
104+
res.step = interpolate(op0.step,
105+
op1.step,
106+
Math::Range<double>::Raw(op0.step.get(),
107+
op1.step.get())
108+
.rescale(step));
109+
else if (auto max = std::copysign(MAX, step);
110+
op0sign == std::signbit(step))
111+
res.step = interpolate(op0.step,
112+
Anim::Interpolated{max},
113+
Math::Range<double>::Raw(op0.step.get(), max)
114+
.rescale(step));
115+
else
116+
res.step = interpolate(op1.step,
117+
Anim::Interpolated{max},
118+
Math::Range<double>::Raw(op1.step.get(), max)
119+
.rescale(step));
120+
73121
res.unit = interpolate(op0.unit, op1.unit, factor);
74122
}
75123
else if (op0.enabled.get()) {

src/chart/generator/plotbuilder.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,23 +355,26 @@ void PlotBuilder::normalizeXY()
355355
}
356356

357357
plot->getOptions()->setAutoRange(
358-
!std::signbit(boundRect.positive().hSize().getMin()),
359-
!std::signbit(boundRect.positive().vSize().getMin()));
358+
!std::signbit(boundRect.hSize().getMin()),
359+
!std::signbit(boundRect.vSize().getMin()));
360360

361361
boundRect.setHSize(xrange.getRange(boundRect.hSize()));
362362
boundRect.setVSize(yrange.getRange(boundRect.vSize()));
363363

364364
for (auto &marker : plot->markers) {
365-
if (!boundRect.intersects(marker.toRectangle().positive()))
365+
if (!boundRect.positive().intersects(
366+
marker.toRectangle().positive()))
366367
marker.enabled = false;
367368

368369
auto rect = marker.toRectangle();
369370
auto newRect = boundRect.normalize(rect);
370371
marker.fromRectangle(newRect);
371372
}
372373

373-
getMeasTrackRange(ChannelId::x) = boundRect.hSize();
374-
getMeasTrackRange(ChannelId::y) = boundRect.vSize();
374+
getMeasTrackRange(ChannelId::x) =
375+
Math::Range<double>::Raw(boundRect.left(), boundRect.right());
376+
getMeasTrackRange(ChannelId::y) =
377+
Math::Range<double>::Raw(boundRect.bottom(), boundRect.top());
375378
}
376379

377380
void PlotBuilder::calcMeasureAxises(const Data::DataTable &dataTable)
@@ -478,7 +481,10 @@ void PlotBuilder::addAlignment(const Buckets &subBuckets) const
478481

479482
if (std::signbit(
480483
plot->axises.at(plot->getOptions()->subAxisType())
481-
.measure.range.getMin()))
484+
.measure.range.getMin())
485+
|| std::signbit(
486+
plot->axises.at(plot->getOptions()->subAxisType())
487+
.measure.range.getMax()))
482488
return;
483489

484490
if (plot->getOptions()->align == Base::Align::Type::none) return;

src/chart/options/channelrange.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ ChannelExtrema::operator std::string() const
2525
Math::Range<double> ChannelRange::getRange(
2626
const Math::Range<double> &original) const
2727
{
28-
return {getExtrema(min, original.getMin(), original),
29-
getExtrema(max, original.getMax(), original)};
28+
return Math::Range<double>::Raw(
29+
getExtrema(min, original.getMin(), original),
30+
getExtrema(max, original.getMax(), original));
3031
}
3132

3233
double ChannelRange::getExtrema(const OptionalChannelExtrema &extrema,

src/chart/rendering/drawaxes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ void DrawAxes::drawTitle(Gen::ChannelId axisIndex) const
227227
? titleStyle.orientation->get_or_first(index)
228228
.value
229229
== Styles::AxisTitle::Orientation::vertical
230-
: titleStyle.orientation->factor<double>(
230+
: titleStyle.orientation->factor(
231231
Styles::AxisTitle::Orientation::vertical));
232232

233233
auto orientedSize =
@@ -336,7 +336,7 @@ void DrawAxes::drawDimensionLabel(bool horizontal,
336336
labelStyle.position->interpolates()
337337
? labelStyle.side->get_or_first(index).value
338338
== Styles::AxisLabel::Side::negative
339-
: labelStyle.side->factor<double>(
339+
: labelStyle.side->factor(
340340
Styles::AxisLabel::Side::negative);
341341

342342
auto sign = 1 - 2 * under;

src/chart/rendering/drawguides.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ void DrawGuides::draw(bool horizontal)
3434
const auto &axis = axises.at(axisId).dimension;
3535

3636
if (axis.enabled && *guideStyle.lineWidth > 0
37-
&& (static_cast<double>(plot->guides.at(axisId).axisGuides)
38-
> 0)) {
37+
&& plot->guides.at(axisId).axisGuides != false) {
3938
canvas.setLineWidth(*guideStyle.lineWidth);
4039

4140
for (auto it = axis.begin(); it != axis.end(); ++it) {

src/chart/rendering/drawinterlacing.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void DrawInterlacing::draw(bool horizontal, bool text) const
7979
}
8080
else {
8181
auto highWeight =
82-
Math::Range(stepLow, stepHigh).rescale(step);
82+
Math::Range<double>::Raw(stepLow, stepHigh).rescale(step);
8383

8484
auto lowWeight = (1.0 - highWeight) * enabled;
8585
highWeight *= enabled;
@@ -119,9 +119,8 @@ void DrawInterlacing::draw(
119119

120120
const auto origo = plot->axises.origo();
121121

122-
if (static_cast<double>(enabled.interlacings || enabled.axisSticks
123-
|| enabled.labels)
124-
> 0) {
122+
if ((enabled.interlacings || enabled.axisSticks || enabled.labels)
123+
!= false) {
125124
auto interlaceIntensity = Math::FuzzyBool::And<double>(weight,
126125
enabled.interlacings);
127126
auto interlaceColor =
@@ -133,7 +132,9 @@ void DrawInterlacing::draw(
133132
auto textAlpha =
134133
Math::FuzzyBool::And<double>(weight, enabled.labels);
135134

136-
if (rangeSize <= 0) return;
135+
if (std::signbit(rangeSize) != std::signbit(stepSize)
136+
|| Math::Floating::is_zero(rangeSize))
137+
return;
137138

138139
auto stripWidth = stepSize / rangeSize;
139140

@@ -270,7 +271,7 @@ void DrawInterlacing::drawDataLabel(
270271
auto under = labelStyle.position->interpolates()
271272
? labelStyle.side->get_or_first(index).value
272273
== Styles::AxisLabel::Side::negative
273-
: labelStyle.side->factor<double>(
274+
: labelStyle.side->factor(
274275
Styles::AxisLabel::Side::negative);
275276

276277
auto &&posDir =

src/chart/rendering/drawlegend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void DrawLegend::drawMarker(Info &info,
244244
info.canvas.setLineColor(color);
245245
info.canvas.setLineWidth(0);
246246

247-
auto radius = rootStyle.legend.marker.type->factor<double>(
247+
auto radius = rootStyle.legend.marker.type->factor(
248248
Styles::Legend::Marker::Type::circle)
249249
* rect.size.minSize() / 2.0;
250250

src/chart/rendering/markerrenderer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void MarkerRenderer::drawLines(Gfx::ICanvas &canvas,
7575
auto center = Geom::Point{blended.center};
7676
center.x = Math::interpolate(center.x,
7777
1.0,
78-
getOptions().coordSystem.factor<double>(
78+
getOptions().coordSystem.factor(
7979
Gen::CoordSystem::polar));
8080
canvas.setLineColor(yLineColor);
8181
auto axisPoint = center.yComp() + origo.xComp();
@@ -142,9 +142,8 @@ void MarkerRenderer::drawMarkers(Gfx::ICanvas &canvas,
142142
const auto &blended0 =
143143
index == ::Anim::second ? *other : blended;
144144

145-
auto lineFactor =
146-
getOptions().geometry.factor<double>(
147-
Gen::ShapeType::line);
145+
auto lineFactor = getOptions().geometry.factor(
146+
Gen::ShapeType::line);
148147

149148
draw(canvas,
150149
painter,
@@ -335,7 +334,7 @@ void MarkerRenderer::drawLabel(Gfx::ICanvas &canvas,
335334
return abstractMarker.getLabelPos(position, coordSys);
336335
});
337336

338-
auto centered = labelStyle.position->factor<double>(
337+
auto centered = labelStyle.position->factor(
339338
Styles::MarkerLabel::Position::center);
340339

341340
OrientedLabel{{ctx()}}.draw(canvas,

0 commit comments

Comments
 (0)