Skip to content

Commit

Permalink
Remove marker toRectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
schaumb committed Dec 6, 2024
1 parent 145694b commit cc3bd4e
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 65 deletions.
20 changes: 8 additions & 12 deletions src/base/math/range.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,10 @@ template <std::floating_point T = double> struct Range
return !less(value, min) && !less(max, value);
}

[[nodiscard]] bool includes(const Range &range) const
{
return !less(range.max, min) && !less(max, range.min);
}

[[nodiscard]] T rescale(const T &value) const
[[nodiscard]] T rescale(const T &value, T def = 0.5) const
{
auto s = size();
return is_zero(s) ? 0.5 : (value - min) / s;
return is_zero(s) ? def : (value - min) / s;
}

[[nodiscard]] T scale(const T &value) const
Expand All @@ -71,11 +66,6 @@ template <std::floating_point T = double> struct Range
return is_zero(max) ? 0 : value / max;
}

[[nodiscard]] Range normalize(const Range &range) const
{
return {normalize(range.min), normalize(range.max)};
}

bool operator==(const Range &other) const
{
return min == other.min && max == other.max;
Expand Down Expand Up @@ -136,6 +126,12 @@ template <std::floating_point T = double> struct Range
return !isOutside;
}

[[nodiscard]] Range positive() const
{
auto &&[min, max] = std::minmax(this->min, this->max, less);
return {min, max};
}

T min{std::numeric_limits<T>::max()};
T max{std::numeric_limits<T>::lowest()};
};
Expand Down
9 changes: 3 additions & 6 deletions src/chart/generator/marker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,11 @@ double Marker::getValueForChannel(const Channels &channels,
return value;
}

Geom::Rect Marker::toRectangle() const
{
return {position - size, {size}};
}

Math::Range<> Marker::getSizeBy(AxisId axisId) const
{
return toRectangle().oSize(orientation(axisId));
auto o = orientation(axisId);
return {position.getCoord(o) - size.getCoord(o),
position.getCoord(o)};
}

void Marker::setSizeBy(AxisId axisId, const Math::Range<> range)
Expand Down
2 changes: 0 additions & 2 deletions src/chart/generator/marker.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ class Marker
bool main,
bool polarConnection);

[[nodiscard]] Geom::Rect toRectangle() const;

[[nodiscard]] Math::Range<> getSizeBy(AxisId axisId) const;
void setSizeBy(AxisId axisId, Math::Range<> range);

Expand Down
8 changes: 4 additions & 4 deletions src/chart/generator/plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ bool Plot::isEmpty() const
return options->getChannels().isEmpty();
}

Geom::Rect Plot::getMarkersBounds() const
Math::Range<> Plot::getMarkersBounds(AxisId axisId) const
{
auto markerIt = markers.begin();
while (markerIt != markers.end()
&& !static_cast<bool>(markerIt->enabled))
++markerIt;

if (markerIt == markers.end()) return {};
if (markerIt == markers.end()) return {{}, {}};

auto boundRect = markerIt->toRectangle().positive();
auto boundRect = markerIt->getSizeBy(axisId).positive();
while (++markerIt != markers.end())
if (markerIt->enabled)
boundRect = boundRect.boundary(markerIt->toRectangle());
boundRect.include(markerIt->getSizeBy(axisId));
return boundRect;
}

Expand Down
2 changes: 1 addition & 1 deletion src/chart/generator/plot.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Plot
void detachOptions();
[[nodiscard]] bool isEmpty() const;

[[nodiscard]] Geom::Rect getMarkersBounds() const;
[[nodiscard]] Math::Range<> getMarkersBounds(AxisId axisId) const;

static bool dimensionMatch(const Plot &a, const Plot &b);
static bool hasMarkerChange(const Plot &source,
Expand Down
56 changes: 30 additions & 26 deletions src/chart/generator/plotbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,41 +326,45 @@ void PlotBuilder::calcAxises(const Data::DataTable &dataTable,
const std::size_t &subBucketSize)
{
auto mainAxis = plot->getOptions()->mainAxisType();
auto &&[subRanges, subMax] = addSeparation(buckets,
plot->getOptions()->subAxisType(),
mainBucketSize);
auto &&subRanges =
addSeparation(buckets, !mainAxis, mainBucketSize);

auto &&[mainRanges, mainMax] =
addSeparation(buckets.sort(&Marker::mainId),
mainAxis,
subBucketSize);
auto &&mainRanges = addSeparation(buckets.sort(&Marker::mainId),
mainAxis,
subBucketSize);

const auto &xrange =
plot->getOptions()->getHorizontalAxis().range;
const auto &yrange = plot->getOptions()->getVerticalAxis().range;
auto mainBoundRect = plot->getMarkersBounds(mainAxis);
auto subBoundRect = plot->getMarkersBounds(!mainAxis);
if (mainAxis != AxisId::x) std::swap(mainBoundRect, subBoundRect);

auto boundRect = plot->getMarkersBounds();
plot->getOptions()->setAutoRange(!std::signbit(mainBoundRect.min),
!std::signbit(subBoundRect.min));

plot->getOptions()->setAutoRange(
!std::signbit(boundRect.hSize().min),
!std::signbit(boundRect.vSize().min));
if (mainAxis != AxisId::x) std::swap(mainBoundRect, subBoundRect);

boundRect.setHSize(xrange.getRange(boundRect.hSize()));
boundRect.setVSize(yrange.getRange(boundRect.vSize()));
mainBoundRect =
plot->getOptions()->getChannels().at(mainAxis).range.getRange(
mainBoundRect);
subBoundRect = plot->getOptions()
->getChannels()
.at(!mainAxis)
.range.getRange(subBoundRect);

for (auto &&[axis, ranges] :
{std::pair{mainAxis, &mainRanges}, {!mainAxis, &subRanges}}) {
auto o = orientation(axis);
for (auto &&[axis, ranges, boundSize] :
{std::tuple{mainAxis, &mainRanges, std::move(mainBoundRect)},
{!mainAxis, &subRanges, std::move(subBoundRect)}}) {
for (auto &marker : plot->markers) {
if (!boundRect.positive().oSize(o).intersects(
marker.toRectangle().positive().oSize(o)))
auto &&markerSize = marker.getSizeBy(axis);
if (!boundSize.positive().intersects(
markerSize.positive()))
marker.enabled = false;

marker.setSizeBy(axis,
boundRect.normalize(marker.toRectangle()).oSize(o));
{boundSize.rescale(markerSize.min, 0.0),
boundSize.rescale(markerSize.max, 0.0)});
}

stats.setIfRange(axis, boundRect.oSize(o));
stats.setIfRange(axis, boundSize);
}

for (const AxisId &ch : {AxisId::x, AxisId::y})
Expand Down Expand Up @@ -530,8 +534,8 @@ void PlotBuilder::addAlignment(const Buckets &buckets,
}
}

std::pair<std::vector<Math::Range<>>, Math::Range<>>
PlotBuilder::addSeparation(const Buckets &buckets,
std::vector<Math::Range<>> PlotBuilder::addSeparation(
const Buckets &buckets,
AxisId axisIndex,
const std::size_t &otherBucketSize) const
{
Expand Down Expand Up @@ -575,7 +579,7 @@ PlotBuilder::addSeparation(const Buckets &buckets,
Base::Align{align, ranges[idx.itemId]}.getAligned(
marker.getSizeBy(axisIndex)));

return {ranges, max};
return ranges;
}

void PlotBuilder::normalizeSizes()
Expand Down
4 changes: 2 additions & 2 deletions src/chart/generator/plotbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class PlotBuilder
void calcLegendAndLabel(const Data::DataTable &dataTable);
void calcAxis(const Data::DataTable &dataTable, AxisId type);
void addAlignment(const Buckets &buckets, AxisId axisIndex) const;
[[nodiscard]] std::pair<std::vector<Math::Range<>>, Math::Range<>>
addSeparation(const Buckets &buckets,
[[nodiscard]] std::vector<Math::Range<>> addSeparation(
const Buckets &buckets,
AxisId axisIndex,
const std::size_t &otherBucketSize) const;
void normalizeSizes();
Expand Down
2 changes: 1 addition & 1 deletion src/chart/main/stylesheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void Sheet::setAfterStyles(Gen::Plot &plot, const Geom::Size &size)
ranges.end(),
[&next_range](const Math::Range<> &other)
{
return other.includes(next_range);
return other.intersects(next_range);
})) {
has_collision = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const testSteps = [
config: {
channels: {
y: { detach: ['Value 2 (+)'], range: { min: 'auto', max: 'auto' } },
x: { attach: ['Value 2 (+)'] },
x: { attach: ['Value 2 (+)'], range: { max: '100%' } },
label: { attach: ['Country'] }
},
title: 'Polar Coordinate',
Expand Down Expand Up @@ -340,7 +340,7 @@ const testSteps = [
chart.animate({
config: {
channels: {
x: { detach: ['Value 2 (+)', 'Country'] },
x: { detach: ['Value 2 (+)', 'Country'], range: { max: 'auto' } },
size: { attach: ['Value 2 (+)'] }
},
title: 'Without Coordinate',
Expand Down
14 changes: 7 additions & 7 deletions test/e2e/test_cases/test_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@
"refs": ["e187f48"]
},
"basic_animations/labels/marker/area_2dis_3con": {
"refs": ["3711b35"]
"refs": ["b543e3d"]
},
"basic_animations/labels/marker/circle_negative_2dis_3con": {
"refs": ["6df75bb"]
"refs": ["7c4e5cc"]
},
"basic_animations/labels/marker/line_2dis_3con": {
"refs": ["9427821"]
"refs": ["fc92094"]
},
"basic_animations/labels/marker/padding_test_rectangle_negative_2dis_3con": {
"refs": ["005e51c"]
"refs": ["68d1990"]
},
"basic_animations/labels/marker/rectangle_negative_2dis_3con": {
"refs": ["da55724"]
"refs": ["cf62886"]
},
"basic_animations/labels/rectangle_labels_rotated_charts": {
"refs": ["ab6aa1d"]
"refs": ["3e62c90"]
},
"basic_animations/legend_transitions/color_2discrete_anim": {
"refs": ["fed1ebe"]
Expand Down Expand Up @@ -395,7 +395,7 @@
"refs": ["d8ffda3"]
},
"static_chart_types/polar_coo_sys/coxcomb_stacked_rectangle_2dis_2con": {
"refs": ["80fac6a"]
"refs": ["19ccbba"]
},
"static_chart_types/polar_coo_sys/radial_rectangle_1dis_1con": {
"refs": ["ffbd29a"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const testSteps = [
data,
config: {
channels: {
y: { set: ['Value 5 (+/-)'], range: { min: '0%', max: '110%' } },
y: { set: ['Value 5 (+/-)'] },
x: 'Country',
color: 'Country',
label: 'Value 5 (+/-)'
Expand All @@ -40,7 +40,6 @@ const testSteps = [
chart.animate({
config: {
channels: {
y: { range: { min: 'auto', max: 'auto' } },
x: ['Country', 'Value 2 (+)']
},
title: 'Polar Coordinate',
Expand Down

0 comments on commit cc3bd4e

Please sign in to comment.