1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 13:41:16 +01:00
GPXSee/src/data/track.cpp

628 lines
14 KiB
C++
Raw Normal View History

#include "dem.h"
2015-10-05 01:43:48 +02:00
#include "track.h"
2017-05-22 14:54:22 +02:00
int Track::_elevationWindow = 3;
2017-05-23 16:06:05 +02:00
int Track::_speedWindow = 5;
2017-05-22 14:54:22 +02:00
int Track::_heartRateWindow = 3;
int Track::_cadenceWindow = 3;
int Track::_powerWindow = 3;
2016-11-05 17:33:29 +01:00
2019-11-13 08:27:54 +01:00
bool Track::_automaticPause = true;
2017-05-22 14:54:22 +02:00
qreal Track::_pauseSpeed = 0.5;
int Track::_pauseInterval = 10;
2016-01-14 00:37:51 +01:00
2017-05-22 14:54:22 +02:00
bool Track::_outlierEliminate = true;
2018-06-21 20:54:24 +02:00
bool Track::_useReportedSpeed = false;
bool Track::_useDEM = false;
bool Track::_show2ndElevation = false;
bool Track::_show2ndSpeed = false;
2020-09-20 22:18:35 +02:00
bool Track::_useSegments = true;
2016-09-19 23:35:04 +02:00
2019-11-13 08:27:54 +01:00
static qreal avg(const QVector<qreal> &v)
{
qreal sum = 0;
for (int i = 0; i < v.size(); i++)
sum += v.at(i);
return sum/v.size();
}
static qreal median(QVector<qreal> &v)
2015-10-05 01:43:48 +02:00
{
std::sort(v.begin(), v.end());
2016-11-05 17:33:29 +01:00
return v.at(v.size() / 2);
2015-10-05 01:43:48 +02:00
}
static qreal MAD(QVector<qreal> &v, qreal m)
2015-10-05 01:43:48 +02:00
{
for (int i = 0; i < v.size(); i++)
2016-11-05 17:33:29 +01:00
v[i] = qAbs(v.at(i) - m);
2019-11-13 08:27:54 +01:00
return median(v);
2015-12-06 00:51:02 +01:00
}
/*
Modified Z-score (Iglewicz and Hoaglin)
The acceleration data distribution has usualy a (much) higher kurtosis than
the normal distribution thus a higher comparsion value than the usual 3.5 is
required.
*/
static QSet<int> eliminate(const QVector<qreal> &v)
2015-12-06 00:51:02 +01:00
{
2016-11-05 17:33:29 +01:00
QSet<int> rm;
2015-11-23 02:37:08 +01:00
QVector<qreal> w(v);
qreal m = median(w);
qreal M = MAD(w, m);
2015-10-05 01:43:48 +02:00
for (int i = 0; i < v.size(); i++)
if (qAbs((0.6745 * (v.at(i) - m)) / M) > 5.0)
2016-11-05 17:33:29 +01:00
rm.insert(i);
2015-11-25 23:17:39 +01:00
2016-11-05 17:33:29 +01:00
return rm;
2015-10-05 01:43:48 +02:00
}
2019-02-11 23:28:08 +01:00
static GraphSegment filter(const GraphSegment &g, int window)
2015-10-05 01:43:48 +02:00
{
if (g.size() < window || window < 2)
2019-02-11 23:28:08 +01:00
return GraphSegment(g);
2017-05-22 22:37:35 +02:00
qreal acc = 0;
2019-02-11 23:28:08 +01:00
GraphSegment ret(g.size());
2015-10-05 01:43:48 +02:00
for (int i = 0; i < window; i++)
acc += g.at(i).y();
for (int i = 0; i <= window/2; i++)
2017-05-22 22:37:35 +02:00
ret[i] = GraphPoint(g.at(i).s(), g.at(i).t(), acc/window);
2015-10-05 01:43:48 +02:00
for (int i = window/2 + 1; i < g.size() - window/2; i++) {
acc += g.at(i + window/2).y() - g.at(i - (window/2 + 1)).y();
2017-05-22 22:37:35 +02:00
ret[i] = GraphPoint(g.at(i).s(), g.at(i).t(), acc/window);
2015-10-05 01:43:48 +02:00
}
for (int i = g.size() - window/2; i < g.size(); i++)
2017-05-22 22:37:35 +02:00
ret[i] = GraphPoint(g.at(i).s(), g.at(i).t(), acc/window);
2015-11-23 02:37:08 +01:00
return ret;
2016-01-14 00:37:51 +01:00
}
2020-09-20 22:18:35 +02:00
Track::Track(const TrackData &data) : _pause(0)
{
qreal ds, dt;
2020-09-20 22:18:35 +02:00
if (_useSegments)
_data = data;
else {
2020-09-26 12:23:41 +02:00
if (!data.isEmpty()) {
_data.append(data.first());
for (int i = 1; i < data.size(); i++)
_data.first() << data.at(i);
}
2020-09-20 22:18:35 +02:00
}
2019-02-11 23:28:08 +01:00
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
_segments.append(Segment());
2019-02-11 23:28:08 +01:00
if (sd.isEmpty())
continue;
// precompute distances, times, speeds and acceleration
QVector<qreal> acceleration;
Segment &seg = _segments.last();
seg.distance.append(i && !_segments.at(i-1).distance.isEmpty()
? _segments.at(i-1).distance.last() : 0);
seg.time.append(i && !_segments.at(i-1).time.isEmpty()
? _segments.at(i-1).time.last() :
2019-02-11 23:28:08 +01:00
sd.first().hasTimestamp() ? 0 : NAN);
seg.speed.append(sd.first().hasTimestamp() ? 0 : NAN);
acceleration.append(sd.first().hasTimestamp() ? 0 : NAN);
bool hasTime = !std::isnan(seg.time.first());
2019-02-11 23:28:08 +01:00
for (int j = 1; j < sd.size(); j++) {
ds = sd.at(j).coordinates().distanceTo(
sd.at(j-1).coordinates());
seg.distance.append(seg.distance.last() + ds);
if (hasTime && sd.at(j).timestamp().isValid()) {
if (sd.at(j).timestamp() > sd.at(j-1).timestamp())
dt = sd.at(j-1).timestamp().msecsTo(
sd.at(j).timestamp()) / 1000.0;
else {
qWarning("%s: %s: time skew detected", qPrintable(
_data.name()), qPrintable(sd.at(j).timestamp().toString(
Qt::ISODate)));
dt = 0;
}
} else {
dt = NAN;
if (hasTime) {
qWarning("%s: missing timestamp(s), time graphs disabled",
qPrintable(_data.name()));
hasTime = false;
for (int i = 0; i < seg.time.size(); i++)
seg.time[i] = NAN;
for (int i = 0; i < seg.speed.size(); i++)
seg.speed[i] = NAN;
}
}
2019-02-11 23:28:08 +01:00
seg.time.append(seg.time.last() + dt);
if (dt < 1e-3) {
seg.speed.append(seg.speed.last());
acceleration.append(acceleration.last());
} else {
qreal v = ds / dt;
qreal dv = v - seg.speed.last();
seg.speed.append(v);
acceleration.append(dv / dt);
}
2016-11-05 17:33:29 +01:00
}
2019-11-13 08:27:54 +01:00
if (!hasTime)
continue;
2019-02-11 23:28:08 +01:00
// get stop-points + pause duration
2019-11-13 08:27:54 +01:00
int pauseInterval;
qreal pauseSpeed;
if (_automaticPause) {
pauseSpeed = (avg(seg.speed) > 2.8) ? 0.40 : 0.15;
2019-11-13 08:27:54 +01:00
pauseInterval = 10;
} else {
pauseSpeed = _pauseSpeed;
pauseInterval = _pauseInterval;
}
2019-11-03 17:27:15 +01:00
int ss = 0, la = 0;
2019-02-11 23:28:08 +01:00
for (int j = 1; j < seg.time.size(); j++) {
2019-11-13 08:27:54 +01:00
if (seg.speed.at(j) > pauseSpeed)
2019-11-03 17:27:15 +01:00
ss = -1;
else if (ss < 0)
ss = j-1;
2019-11-03 17:27:15 +01:00
2019-11-13 08:27:54 +01:00
if (ss >= 0 && seg.time.at(j) > seg.time.at(ss) + pauseInterval) {
2019-11-03 17:27:15 +01:00
int l = qMax(ss, la);
_pause += seg.time.at(j) - seg.time.at(l);
for (int k = l; k <= j; k++)
seg.stop.insert(k);
la = j;
2019-02-11 23:28:08 +01:00
}
}
2019-11-13 08:27:54 +01:00
if (!_outlierEliminate)
2019-02-11 23:28:08 +01:00
continue;
2016-11-05 17:33:29 +01:00
2019-02-11 23:28:08 +01:00
// eliminate outliers
seg.outliers = eliminate(acceleration);
2019-02-11 23:28:08 +01:00
// stop-points can not be outliers
QSet<int>::const_iterator it;
for (it = seg.stop.constBegin(); it != seg.stop.constEnd(); ++it)
seg.outliers.remove(*it);
// recompute distances (and dependand data) without outliers
int last = 0;
for (int j = 0; j < sd.size(); j++) {
if (seg.outliers.contains(j))
last++;
else
break;
}
for (int j = last + 1; j < sd.size(); j++) {
2019-02-12 00:20:39 +01:00
if (seg.outliers.contains(j))
2019-02-11 23:28:08 +01:00
continue;
if (discardStopPoint(seg, j)) {
seg.distance[j] = seg.distance.at(last);
seg.speed[j] = 0;
} else {
ds = sd.at(j).coordinates().distanceTo(
sd.at(last).coordinates());
seg.distance[j] = seg.distance.at(last) + ds;
2019-02-12 00:20:39 +01:00
dt = seg.time.at(j) - seg.time.at(last);
seg.speed[j] = (dt < 1e-3) ? seg.speed.at(last) : ds / dt;
2019-02-11 23:28:08 +01:00
}
last = j;
2018-06-20 20:15:22 +02:00
}
2016-09-19 23:35:04 +02:00
}
}
Graph Track::gpsElevation() const
2015-11-23 02:37:08 +01:00
{
2019-02-11 23:28:08 +01:00
Graph ret;
2015-10-05 01:43:48 +02:00
for (int i = 0; i < _data.size(); i++) {
2019-02-11 23:28:08 +01:00
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
2019-02-11 23:28:08 +01:00
const Segment &seg = _segments.at(i);
GraphSegment gs;
for (int j = 0; j < sd.size(); j++) {
if (!sd.at(j).hasElevation() || seg.outliers.contains(j))
2019-02-11 23:28:08 +01:00
continue;
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j),
sd.at(j).elevation()));
}
2019-02-11 23:28:08 +01:00
ret.append(filter(gs, _elevationWindow));
}
2015-10-05 01:43:48 +02:00
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
2019-02-11 23:28:08 +01:00
return ret;
2015-10-05 01:43:48 +02:00
}
Graph Track::demElevation() const
{
Graph ret;
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
const Segment &seg = _segments.at(i);
GraphSegment gs;
for (int j = 0; j < sd.size(); j++) {
qreal dem = DEM::elevation(sd.at(j).coordinates());
if (std::isnan(dem) || seg.outliers.contains(j))
continue;
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j), dem));
}
ret.append(filter(gs, _elevationWindow));
}
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
return ret;
}
GraphPair Track::elevation() const
{
if (_useDEM) {
Graph dem(demElevation());
if (dem.isValid())
return GraphPair(dem, _show2ndElevation ? gpsElevation() : Graph());
else
return GraphPair(gpsElevation(), Graph());
} else {
Graph gps(gpsElevation());
if (gps.isValid())
return GraphPair(gps, _show2ndElevation ? demElevation() : Graph());
else
return GraphPair(demElevation(), Graph());
}
}
Graph Track::computedSpeed() const
2015-10-05 01:43:48 +02:00
{
2019-02-11 23:28:08 +01:00
Graph ret;
2015-10-05 01:43:48 +02:00
2016-11-05 17:33:29 +01:00
for (int i = 0; i < _data.size(); i++) {
2019-02-11 23:28:08 +01:00
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
2019-02-11 23:28:08 +01:00
const Segment &seg = _segments.at(i);
GraphSegment gs;
QList<int> stop;
qreal v;
for (int j = 0; j < sd.size(); j++) {
if (seg.stop.contains(j) && !std::isnan(seg.speed.at(j))) {
2019-02-11 23:28:08 +01:00
v = 0;
stop.append(gs.size());
} else if (!std::isnan(seg.speed.at(j)) && !seg.outliers.contains(j))
2019-02-11 23:28:08 +01:00
v = seg.speed.at(j);
else
continue;
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j), v));
}
2015-10-05 01:43:48 +02:00
2019-02-11 23:28:08 +01:00
ret.append(filter(gs, _speedWindow));
GraphSegment &filtered = ret.last();
2019-02-11 23:28:08 +01:00
for (int j = 0; j < stop.size(); j++)
filtered[stop.at(j)].setY(0);
}
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
2019-02-11 23:28:08 +01:00
return ret;
2015-10-05 01:43:48 +02:00
}
Graph Track::reportedSpeed() const
{
Graph ret;
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
const Segment &seg = _segments.at(i);
GraphSegment gs;
QList<int> stop;
qreal v;
for (int j = 0; j < sd.size(); j++) {
if (seg.stop.contains(j) && sd.at(j).hasSpeed()) {
v = 0;
stop.append(gs.size());
} else if (sd.at(j).hasSpeed() && !seg.outliers.contains(j))
v = sd.at(j).speed();
else
continue;
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j), v));
}
ret.append(filter(gs, _speedWindow));
GraphSegment &filtered = ret.last();
for (int j = 0; j < stop.size(); j++)
filtered[stop.at(j)].setY(0);
}
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
return ret;
}
GraphPair Track::speed() const
{
if (_useReportedSpeed) {
Graph reported(reportedSpeed());
if (reported.isValid())
return GraphPair(reported, _show2ndSpeed ? computedSpeed()
: Graph());
else
return GraphPair(computedSpeed(), Graph());
} else {
Graph computed(computedSpeed());
if (computed.isValid())
return GraphPair(computed, _show2ndSpeed ? reportedSpeed()
: Graph());
else
return GraphPair(reportedSpeed(), Graph());
}
}
2016-09-19 00:56:10 +02:00
Graph Track::heartRate() const
2015-11-23 02:37:08 +01:00
{
2019-02-11 23:28:08 +01:00
Graph ret;
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
2019-02-11 23:28:08 +01:00
const Segment &seg = _segments.at(i);
GraphSegment gs;
2019-02-11 23:28:08 +01:00
for (int j = 0; j < sd.size(); j++)
if (sd.at(j).hasHeartRate() && !seg.outliers.contains(j))
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j),
sd.at(j).heartRate()));
2015-11-23 02:37:08 +01:00
2019-02-11 23:28:08 +01:00
ret.append(filter(gs, _heartRateWindow));
}
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
2019-02-11 23:28:08 +01:00
return ret;
}
2015-11-23 02:37:08 +01:00
2016-09-19 00:56:10 +02:00
Graph Track::temperature() const
2016-06-16 20:47:32 +02:00
{
2019-02-11 23:28:08 +01:00
Graph ret;
2016-11-05 17:33:29 +01:00
2019-02-11 23:28:08 +01:00
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
2019-02-11 23:28:08 +01:00
const Segment &seg = _segments.at(i);
GraphSegment gs;
for (int j = 0; j < sd.count(); j++) {
if (sd.at(j).hasTemperature() && !seg.outliers.contains(j))
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j),
sd.at(j).temperature()));
}
ret.append(gs);
}
2016-09-19 00:56:10 +02:00
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
2019-02-11 23:28:08 +01:00
return ret;
2016-11-06 03:28:08 +01:00
}
Graph Track::ratio() const
{
2019-02-11 23:28:08 +01:00
Graph ret;
2019-02-11 23:28:08 +01:00
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
2019-02-11 23:28:08 +01:00
const Segment &seg = _segments.at(i);
GraphSegment gs;
for (int j = 0; j < sd.size(); j++)
if (sd.at(j).hasRatio() && !seg.outliers.contains(j))
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j),
sd.at(j).ratio()));
2019-02-11 23:28:08 +01:00
ret.append(gs);
}
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
2019-02-11 23:28:08 +01:00
return ret;
}
2016-11-06 03:28:08 +01:00
Graph Track::cadence() const
{
2019-02-11 23:28:08 +01:00
Graph ret;
2016-11-06 03:28:08 +01:00
for (int i = 0; i < _data.size(); i++) {
2019-02-11 23:28:08 +01:00
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
2019-02-11 23:28:08 +01:00
const Segment &seg = _segments.at(i);
GraphSegment gs;
QList<int> stop;
qreal c;
for (int j = 0; j < sd.size(); j++) {
if (sd.at(j).hasCadence() && seg.stop.contains(j)) {
c = 0;
stop.append(gs.size());
} else if (sd.at(j).hasCadence() && !seg.outliers.contains(j))
c = sd.at(j).cadence();
else
continue;
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j), c));
}
2019-02-11 23:28:08 +01:00
ret.append(filter(gs, _cadenceWindow));
GraphSegment &filtered = ret.last();
2019-02-11 23:28:08 +01:00
for (int j = 0; j < stop.size(); j++)
filtered[stop.at(j)].setY(0);
}
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
2019-02-11 23:28:08 +01:00
return ret;
2016-11-06 03:28:08 +01:00
}
Graph Track::power() const
{
2019-02-11 23:28:08 +01:00
Graph ret;
QList<int> stop;
qreal p;
2016-11-06 03:28:08 +01:00
2019-02-11 23:28:08 +01:00
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
2019-02-11 23:28:08 +01:00
const Segment &seg = _segments.at(i);
GraphSegment gs;
for (int j = 0; j < sd.size(); j++) {
if (sd.at(j).hasPower() && seg.stop.contains(j)) {
2019-02-11 23:28:08 +01:00
p = 0;
stop.append(gs.size());
} else if (sd.at(j).hasPower() && !seg.outliers.contains(j))
p = sd.at(j).power();
2019-02-11 23:28:08 +01:00
else
continue;
gs.append(GraphPoint(seg.distance.at(j), seg.time.at(j), p));
}
2019-02-11 23:28:08 +01:00
ret.append(filter(gs, _powerWindow));
GraphSegment &filtered = ret.last();
2019-02-11 23:28:08 +01:00
for (int j = 0; j < stop.size(); j++)
filtered[stop.at(j)].setY(0);
}
if (_data.style().color().isValid())
ret.setColor(_data.style().color());
2019-02-11 23:28:08 +01:00
return ret;
2016-06-16 20:47:32 +02:00
}
qreal Track::distance() const
2015-12-20 09:33:40 +01:00
{
2019-02-11 23:28:08 +01:00
for (int i = _segments.size() - 1; i >= 0; i--) {
const Segment &seg = _segments.at(i);
for (int j = seg.distance.size() - 1; j >= 0; j--)
if (!seg.outliers.contains(j))
return seg.distance.at(j);
}
return 0;
}
2015-12-20 09:33:40 +01:00
qreal Track::time() const
{
2019-02-11 23:28:08 +01:00
for (int i = _segments.size() - 1; i >= 0; i--) {
const Segment &seg = _segments.at(i);
for (int j = seg.time.size() - 1; j >= 0; j--)
if (!seg.outliers.contains(j))
return seg.time.at(j);
}
return 0;
2015-12-20 09:33:40 +01:00
}
2016-01-14 00:37:51 +01:00
qreal Track::movingTime() const
{
return (time() - _pause);
}
QDateTime Track::date() const
2016-01-14 00:37:51 +01:00
{
2019-02-11 23:28:08 +01:00
return (_data.size() && _data.first().size())
? _data.first().first().timestamp() : QDateTime();
2016-01-14 00:37:51 +01:00
}
2016-11-05 17:33:29 +01:00
2016-11-13 09:49:31 +01:00
Path Track::path() const
2016-11-05 17:33:29 +01:00
{
Path ret;
2019-02-11 23:28:08 +01:00
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
if (sd.size() < 2)
continue;
2019-02-11 23:28:08 +01:00
const Segment &seg = _segments.at(i);
ret.append(PathSegment());
PathSegment &ps = ret.last();
for (int j = 0; j < sd.size(); j++)
if (!seg.outliers.contains(j) && !discardStopPoint(seg, j))
ps.append(PathPoint(sd.at(j).coordinates(),
seg.distance.at(j)));
}
2016-11-05 17:33:29 +01:00
2022-09-25 02:15:24 +02:00
ret.setStyle(_data.style());
2016-11-05 17:33:29 +01:00
return ret;
}
2019-02-11 23:28:08 +01:00
bool Track::discardStopPoint(const Segment &seg, int i) const
{
2019-02-11 23:28:08 +01:00
return (seg.stop.contains(i) && seg.stop.contains(i-1)
&& seg.stop.contains(i+1) && i > 0 && i < seg.distance.size() - 1);
}
bool Track::isValid() const
{
for (int i = 0; i < _data.size(); i++)
if (_data.at(i).size() >= 2)
return true;
return false;
}