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

439 lines
9.6 KiB
C++
Raw Normal View History

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
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;
2016-09-19 23:35:04 +02:00
static qreal median(QVector<qreal> &v)
2015-10-05 01:43:48 +02:00
{
2016-11-05 17:33:29 +01:00
qSort(v.begin(), v.end());
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);
qSort(v.begin(), v.end());
return v.at(v.size() / 2);
2015-12-06 00:51:02 +01:00
}
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)
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
}
2019-02-11 23:28:08 +01:00
Track::Track(const TrackData &data) : _data(data), _pause(0)
{
qreal ds, dt;
2019-02-11 23:28:08 +01:00
for (int i = 0; i < _data.size(); i++) {
const SegmentData &sd = _data.at(i);
if (sd.isEmpty())
continue;
// precompute distances, times, speeds and acceleration
QVector<qreal> acceleration;
_segments.append(Segment());
Segment &seg = _segments.last();
seg.distance.append(i ? _segments.at(i-1).distance.last() : 0);
seg.time.append(i ? _segments.at(i-1).time.last() :
sd.first().hasTimestamp() ? 0 : NAN);
seg.speed.append(sd.first().hasTimestamp() ? 0 : NAN);
acceleration.append(sd.first().hasTimestamp() ? 0 : NAN);
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 (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;
}
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-02-11 23:28:08 +01:00
// get stop-points + pause duration
for (int j = 1; j < seg.time.size(); j++) {
if (seg.time.at(j) > seg.time.at(j-1) + _pauseInterval
&& seg.speed.at(j) < _pauseSpeed) {
_pause += seg.time.at(j) - seg.time.at(j-1);
seg.stop.insert(j-1);
seg.stop.insert(j);
}
}
2019-02-11 23:28:08 +01:00
if (!_outlierEliminate)
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
}
}
2016-09-19 00:56:10 +02:00
Graph Track::elevation() 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
2019-02-11 23:28:08 +01:00
return ret;
2015-10-05 01:43:48 +02:00
}
2016-09-19 00:56:10 +02:00
Graph Track::speed() 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))
|| sd.at(j).hasSpeed())) {
v = 0;
stop.append(gs.size());
} else if (_useReportedSpeed && sd.at(j).hasSpeed()
2019-04-02 22:43:29 +02:00
&& !seg.outliers.contains(j))
2019-02-11 23:28:08 +01:00
v = sd.at(j).speed();
else if (!std::isnan(seg.speed.at(j)) && !seg.outliers.contains(j))
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);
}
2019-02-11 23:28:08 +01:00
return ret;
2015-10-05 01:43:48 +02:00
}
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));
}
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
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);
}
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);
}
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);
}
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
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;
}