From 2695745d82e0002cf9c72bb8dcfe4e273b628d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 27 Jun 2016 20:30:45 +0200 Subject: [PATCH] Code cleanup --- src/track.cpp | 61 +++++++++++++++++---------------------------------- src/track.h | 2 +- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/track.cpp b/src/track.cpp index dfc6cb8c..b1902820 100644 --- a/src/track.cpp +++ b/src/track.cpp @@ -81,39 +81,34 @@ static QVector filter(const QVector &v, int window) Track::Track(const QVector &data) : _data(data) { - _distance = 0; + qreal dist = 0; - for (int i = 1; i < _data.count(); i++) - _dd.append(llDistance(_data.at(i).coordinates, - _data.at(i-1).coordinates)); - - for (int i = 0; i < _dd.size(); i++) - _distance += _dd.at(i); + _dd.append(dist); + for (int i = 1; i < _data.count(); i++) { + dist += llDistance(_data.at(i).coordinates, _data.at(i-1).coordinates); + _dd.append(dist); + } } void Track::elevationGraph(QVector &graph) const { - qreal dist = 0; QVector raw; if (!_data.size()) return; - if (_data.at(0).hasElevation()) - raw.append(QPointF(0, _data.at(0).elevation - _data.at(0).geoidheight)); - for (int i = 1; i < _data.size(); i++) { - dist += _dd.at(i-1); + for (int i = 0; i < _data.size(); i++) if (_data.at(i).hasElevation()) - raw.append(QPointF(dist, _data.at(i).elevation + raw.append(QPointF(_dd.at(i), _data.at(i).elevation - _data.at(i).geoidheight)); - } graph = filter(raw, WINDOW_EF); } void Track::speedGraph(QVector &graph) const { - qreal dist = 0, v, ds, dt; + qreal v, ds; + qint64 dt; QVector raw; if (!_data.size()) @@ -121,21 +116,17 @@ void Track::speedGraph(QVector &graph) const raw.append(QPointF(0, 0)); for (int i = 1; i < _data.size(); i++) { - ds = _dd.at(i-1); - dist += ds; - if (_data.at(i).hasSpeed()) v = _data.at(i).speed; else if (_data.at(i).hasTimestamp()) { - dt = _data.at(i-1).timestamp.msecsTo(_data.at(i).timestamp) - / 1000.0; - if (dt == 0) + if (!(dt = _data.at(i-1).timestamp.msecsTo(_data.at(i).timestamp))) continue; - v = ds / dt; + ds = _dd.at(i) - _dd.at(i-1); + v = ds / ((qreal)dt / 1000.0); } else continue; - raw.append(QPointF(dist, v)); + raw.append(QPointF(_dd.at(i), v)); } graph = filter(eliminate(raw, WINDOW_SE), WINDOW_SF); @@ -143,37 +134,26 @@ void Track::speedGraph(QVector &graph) const void Track::heartRateGraph(QVector &graph) const { - qreal dist = 0; QVector raw; if (!_data.size()) return; - if (_data.at(0).hasHeartRate()) - raw.append(QPointF(0, _data.at(0).heartRate)); - for (int i = 1; i < _data.count(); i++) { - dist += _dd.at(i-1); + for (int i = 0; i < _data.count(); i++) if (_data.at(i).hasHeartRate()) - raw.append(QPointF(dist, _data.at(i).heartRate)); - } + raw.append(QPointF(_dd.at(i), _data.at(i).heartRate)); graph = filter(eliminate(raw, WINDOW_HE), WINDOW_HF); } void Track::temperatureGraph(QVector &graph) const { - qreal dist = 0; - if (!_data.size()) return; - if (_data.at(0).hasTemperature()) - graph.append(QPointF(0, _data.at(0).temperature)); - for (int i = 1; i < _data.size(); i++) { - dist += _dd.at(i-1); + for (int i = 0; i < _data.size(); i++) if (_data.at(i).hasTemperature()) - graph.append(QPointF(dist, _data.at(i).temperature)); - } + graph.append(QPointF(_dd.at(i), _data.at(i).temperature)); } void Track::track(QVector &track) const @@ -187,14 +167,13 @@ qreal Track::time() const if (_data.size() < 2) return 0; - return (_data.at(0).timestamp.msecsTo(_data.at(_data.size() - 1).timestamp) - / 1000.0); + return (_data.first().timestamp.msecsTo(_data.last().timestamp) / 1000.0); } QDateTime Track::date() const { if (_data.size()) - return _data.at(0).timestamp; + return _data.first().timestamp; else return QDateTime(); } diff --git a/src/track.h b/src/track.h index 52283f49..400795f8 100644 --- a/src/track.h +++ b/src/track.h @@ -15,7 +15,7 @@ public: void heartRateGraph(QVector &graph) const; void temperatureGraph(QVector &graph) const; void track(QVector &track) const; - qreal distance() const {return _distance;} + qreal distance() const {return _dd.last();} qreal time() const; QDateTime date() const;