1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00
GPXSee/src/elevationgraph.cpp

235 lines
4.0 KiB
C++
Raw Normal View History

#include <cmath>
#include "config.h"
2016-10-23 11:09:20 +02:00
#include "data.h"
2015-10-12 01:12:12 +02:00
#include "elevationgraph.h"
2015-11-25 23:58:46 +01:00
static qreal nMin(qreal a, qreal b)
{
if (!std::isnan(a) && !std::isnan(b))
return qMin(a, b);
else if (!std::isnan(a))
return a;
else if (!std::isnan(b))
return b;
else
return NAN;
}
static qreal nMax(qreal a, qreal b)
{
if (!std::isnan(a) && !std::isnan(b))
return qMax(a, b);
else if (!std::isnan(a))
return a;
else if (!std::isnan(b))
return b;
else
return NAN;
}
2016-06-24 00:55:44 +02:00
ElevationGraph::ElevationGraph(QWidget *parent) : GraphTab(parent)
2015-10-12 01:12:12 +02:00
{
_trackAscent = 0;
_routeAscent = 0;
_trackDescent = 0;
_routeDescent = 0;
_trackMin = NAN;
_trackMax = NAN;
_routeMin = NAN;
_routeMax = NAN;
_showRoutes = true;
_showTracks = true;
2015-10-12 01:12:12 +02:00
_units = Metric;
setYUnits();
setYLabel(tr("Elevation"));
2016-03-27 13:23:00 +02:00
setMinYRange(50.0);
2015-12-19 20:23:07 +01:00
}
2016-08-09 01:16:19 +02:00
void ElevationGraph::setInfo()
2015-12-19 20:23:07 +01:00
{
if (std::isnan(max()) || std::isnan(min()))
clearInfo();
else {
GraphView::addInfo(tr("Ascent"), QString::number(ascent() * yScale(),
'f', 0) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Descent"), QString::number(descent() * yScale(),
'f', 0) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f',
0) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Minimum"), QString::number(min() * yScale(), 'f',
0) + UNIT_SPACE + yUnits());
}
2016-08-09 01:16:19 +02:00
}
2016-03-27 13:23:00 +02:00
2016-09-19 00:56:10 +02:00
void ElevationGraph::loadGraph(const Graph &graph, Type type, PathItem *path)
2016-08-09 01:16:19 +02:00
{
qreal ascent = 0, descent = 0;
qreal min, max;
2016-08-09 01:16:19 +02:00
2016-09-19 23:35:04 +02:00
if (graph.size() < 2) {
2016-08-09 01:16:19 +02:00
skipColor();
return;
}
2016-09-19 23:35:04 +02:00
max = min = graph.at(0).y();
for (int j = 1; j < graph.size(); j++) {
qreal cur = graph.at(j).y();
qreal prev = graph.at(j-1).y();
2016-08-09 01:16:19 +02:00
if (cur > prev)
ascent += cur - prev;
if (cur < prev)
descent += prev - cur;
if (cur < min)
min = cur;
if (cur > max)
max = cur;
2016-08-09 01:16:19 +02:00
}
if (type == Track) {
_trackAscent += ascent;
_trackDescent += descent;
_trackMax = nMax(_trackMax, max);
_trackMin = nMin(_trackMin, min);
} else {
_routeAscent += ascent;
_routeDescent += descent;
_routeMax = nMax(_routeMax, max);
_routeMin = nMin(_routeMin, min);
}
2016-08-09 01:16:19 +02:00
2016-09-19 00:56:10 +02:00
GraphView::loadGraph(graph, path, type);
2015-10-12 01:12:12 +02:00
}
2016-10-23 11:09:20 +02:00
void ElevationGraph::loadData(const Data &data, const QList<PathItem *> &paths)
2015-10-12 01:12:12 +02:00
{
2016-09-19 00:56:10 +02:00
int p = 0;
2016-10-23 11:09:20 +02:00
for (int i = 0; i < data.tracks().count(); i++)
loadGraph(data.tracks().at(i)->elevation(), Track, paths.at(p++));
for (int i = 0; i < data.routes().count(); i++)
loadGraph(data.routes().at(i)->elevation(), Route, paths.at(p++));
2016-03-27 13:23:00 +02:00
2016-08-09 01:16:19 +02:00
setInfo();
redraw();
2015-10-12 01:12:12 +02:00
}
void ElevationGraph::clear()
{
_trackAscent = 0;
_routeAscent = 0;
_trackDescent = 0;
_routeDescent = 0;
_trackMin = NAN;
_trackMax = NAN;
_routeMin = NAN;
2016-08-19 21:10:19 +02:00
_routeMax = NAN;
2015-10-12 01:12:12 +02:00
GraphView::clear();
2015-10-12 01:12:12 +02:00
}
2015-12-19 20:23:07 +01:00
void ElevationGraph::setYUnits()
2015-12-19 20:23:07 +01:00
{
if (_units == Metric) {
GraphView::setYUnits(tr("m"));
setYScale(1);
2015-12-19 20:23:07 +01:00
} else {
GraphView::setYUnits(tr("ft"));
setYScale(M2FT);
2015-12-19 20:23:07 +01:00
}
}
void ElevationGraph::setUnits(enum Units units)
{
_units = units;
2016-08-09 01:16:19 +02:00
setYUnits();
2016-08-09 01:16:19 +02:00
setInfo();
2016-09-19 00:56:10 +02:00
GraphView::setUnits(units);
2015-12-19 20:23:07 +01:00
2016-08-09 01:16:19 +02:00
redraw();
2015-12-19 20:23:07 +01:00
}
void ElevationGraph::showTracks(bool show)
{
_showTracks = show;
setInfo();
showGraph(show, Track);
redraw();
}
void ElevationGraph::showRoutes(bool show)
{
_showRoutes = show;
showGraph(show, Route);
2016-08-20 11:28:08 +02:00
setInfo();
redraw();
}
qreal ElevationGraph::ascent() const
{
qreal val = 0;
if (_showRoutes)
val += _routeAscent;
if (_showTracks)
val += _trackAscent;
return val;
}
qreal ElevationGraph::descent() const
{
qreal val = 0;
if (_showRoutes)
val += _routeDescent;
if (_showTracks)
val += _trackDescent;
return val;
}
qreal ElevationGraph::max() const
{
qreal val;
if (_showRoutes && _showTracks)
val = nMax(_routeMax, _trackMax);
else if (_showTracks)
val = _trackMax;
else if (_showRoutes)
val = _routeMax;
else
val = NAN;
return val;
}
qreal ElevationGraph::min() const
{
qreal val;
if (_showRoutes && _showTracks)
val = nMin(_routeMin, _trackMin);
else if (_showTracks)
val = _trackMin;
else if (_showRoutes)
val = _routeMin;
else
val = NAN;
return val;
}