~ubuntu-branches/ubuntu/saucy/goldencheetah/saucy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright (c) 2010 Damien Grauser (Damien.Grauser@pev-geneve.ch)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "RideMetric.h"
#include "Settings.h"
#include "Zones.h"
#include "HrZones.h"
#include <math.h>
#include "MainWindow.h"
#include <QApplication>

// This is Morton/Banister with Green et al coefficient.
//
// HR_TRIMP = time(min) * (AvgHR-RHR)/(MaxHR-RHR)*0.64*EXP(Ksex*(AvgHr-RHR)/(MaxHR-RHR))
//
// Ksex = 1.92 for man and 1.67 for woman
// RHR = resting heart rate
//
class TRIMPPoints : public RideMetric {
    Q_DECLARE_TR_FUNCTIONS(TRIMPPoints)

    double score;

    public:

    static const double K;

    TRIMPPoints() : score(0.0)
    {
        setSymbol("trimp_points");
        setInternalName("TRIMP Points");
    }
    void initialize() {
        setName(tr("TRIMP Points"));
        setMetricUnits("");
        setImperialUnits("");
        setType(RideMetric::Total);
    }

    void compute(const RideFile *rideFile,
                 const Zones *, int,
                 const HrZones *hrZones, int hrZoneRange,
                 const QHash<QString,RideMetric*> &deps,
                 const MainWindow *main)
    {
        if (!hrZones || hrZoneRange < 0) {
            setValue(0);
            return;
        }

        // use resting HR from zones, but allow it to be
        // overriden in ride metadata
        double maxHr = hrZones->getMaxHr(hrZoneRange);
        double restHr = hrZones->getRestHr(hrZoneRange);
        restHr = rideFile->getTag("Rest HR", QString("%1").arg(restHr)).toDouble();

        assert(deps.contains("time_riding"));
        assert(deps.contains("workout_time"));
        assert(deps.contains("average_hr"));
        //const RideMetric *workoutTimeMetric = deps.value("workout_time");
        const RideMetric *timeRidingMetric = deps.value("time_riding");
        const RideMetric *averageHrMetric = deps.value("average_hr");
        const RideMetric *durationMetric = deps.value("workout_time");
        assert(timeRidingMetric);
        assert(durationMetric);
        assert(averageHrMetric);

        double secs = timeRidingMetric->value(true) ? timeRidingMetric->value(true) :
                                                      durationMetric->value(true);;
        double hr = averageHrMetric->value(true);

        //TRIMP: = t x %HRR x 0.64e1,92(%HRR)

        // Can we lookup the athletes gender?
        // Default to male if we fail
        QString athlete;
        double ksex = 1.92;
        if ((athlete = rideFile->getTag("Athlete", "unknown")) != "unknown") {
            if (appsettings->cvalue(main->cyclist, GC_SEX).toInt() == 1) ksex = 1.67; // Female
            else ksex = 1.92; // Male
        }

        // ok lets work the score out
        score = (secs == 0.0 || hr<restHr) ? 0.0 :  secs/60 *
                (hr-restHr)/(maxHr-restHr)*0.64*exp(ksex*(hr-restHr)/(maxHr-restHr));
        setValue(score);
    }

    RideMetric *clone() const { return new TRIMPPoints(*this); }
};



class TRIMP100Points : public RideMetric {
    Q_DECLARE_TR_FUNCTIONS(TRIMP100Points)

    double score;

public:

    static const double K;

    TRIMP100Points() : score(0.0)
    {
        setSymbol("trimp_100_points");
        setInternalName("TRIMP(100) Points");
    }
    void initialize() {
        setName(tr("TRIMP(100) Points"));
        setMetricUnits("");
        setImperialUnits("");
        setType(RideMetric::Total);
    }

    void compute(const RideFile *rideFile,
                 const Zones *, int,
                 const HrZones *hrZones, int hrZoneRange,
                 const QHash<QString,RideMetric*> &deps,
                 const MainWindow *main)
    {
        if (!hrZones || hrZoneRange < 0) {
            setValue(0);
            return;
        }

        // use resting HR from zones, but allow it to be
        // overriden in ride metadata
        double maxHr = hrZones->getMaxHr(hrZoneRange);
        double restHr = hrZones->getRestHr(hrZoneRange);
        double ltHr = hrZones->getLT(hrZoneRange);
        restHr = rideFile->getTag("Rest HR", QString("%1").arg(restHr)).toDouble();


        assert(deps.contains("trimp_points"));
        const RideMetric *trimpPointsMetric = deps.value("trimp_points");
        assert(trimpPointsMetric);
        double trimp = trimpPointsMetric->value(true);

        //TRIMP: = t x %HRR x 0.64e1,92(%HRR)

        // Can we lookup the athletes gender?
        // Default to male if we fail
        QString athlete;
        double ksex = 1.92;
        if ((athlete = rideFile->getTag("Athlete", "unknown")) != "unknown") {
            if (appsettings->cvalue(main->cyclist, GC_SEX).toInt() == 1) ksex = 1.67; // Female
            else ksex = 1.92; // Male
        }

        score = trimp == 0.0 ? 0.0 :  100 * trimp /
                (60 * (ltHr-restHr)/(maxHr-restHr)*0.64*exp(ksex*(ltHr-restHr)/(maxHr-restHr)));

        setValue(score);
    }
    RideMetric *clone() const { return new TRIMP100Points(*this); }
};

//0.84 (zone 1 64-76%), 1.65 (zone 2 77-83%), 2.57 (zone 3 84-89%), 4.01 (zone 4 90-94%), and 5.91 (zone 5 95-100%)
//1 (zone 1 50-60%), 1.1 (zone 2 60-70%), 1.2 (zone 3 70-80%), 2.2 (zone 4 80-90%), and 4.5 (zone 5 90-100%)

// 0, 68, 83, 94, 105 of LT for LT 80% Max-> 0, 55, 66, 75, 84
// 0.9 (zone 1 0-55%), 1.1 (zone 2 55-66%), 1.2 (zone 3 66-75%), 2 (zone 4 75-84%), and 5 (zone 5 84-100%)

class TRIMPZonalPoints : public RideMetric {
    Q_DECLARE_TR_FUNCTIONS(TRIMPZonalPoints)

    double score;

public:

    static const double K;

    TRIMPZonalPoints() : score(0.0)
    {
        setSymbol("trimp_zonal_points");
        setInternalName("TRIMP Zonal Points");
    }
    void initialize() {
        setName(tr("TRIMP Zonal Points"));
        setMetricUnits("");
        setImperialUnits("");
        setType(RideMetric::Total);
    }

    void compute(const RideFile *,
                 const Zones *, int,
                 const HrZones *hrZones, int hrZoneRange,
                 const QHash<QString,RideMetric*> &deps,
                 const MainWindow *)
    {
        assert(deps.contains("average_hr"));
        const RideMetric *averageHrMetric = deps.value("average_hr");
        assert(averageHrMetric);
        double hr = averageHrMetric->value(true);

        if (hrZoneRange == -1 || hr == 0) {
            setValue(0);
            return;
        }

        QList <double> trimpk = hrZones->getZoneTrimps(hrZoneRange);
        double value = 0;

        if (trimpk.size()>0) {
            assert(deps.contains("time_in_zone_H1"));
            const RideMetric *time1Metric = deps.value("time_in_zone_H1");
            assert(time1Metric);
            double time1 = time1Metric->value(true);
            double trimpk1 = trimpk[0];
            value += trimpk1 * time1;
        }

        if (trimpk.size()>1) {
            assert(deps.contains("time_in_zone_H2"));
            const RideMetric *time2Metric = deps.value("time_in_zone_H2");
            assert(time2Metric);
            double time2 = time2Metric->value(true);
            double trimpk2 = trimpk[1];
            value += trimpk2 * time2;
        }

        if (trimpk.size()>2) {
            assert(deps.contains("time_in_zone_H3"));
            const RideMetric *time3Metric = deps.value("time_in_zone_H3");
            assert(time3Metric);
            double time3 = time3Metric->value(true);
            double trimpk3 = trimpk[2];
            value += trimpk3 * time3;
        }

        if (trimpk.size()>3) {
            assert(deps.contains("time_in_zone_H4"));
            const RideMetric *time4Metric = deps.value("time_in_zone_H4");
            assert(time4Metric);
            double time4 = time4Metric->value(true);
            double trimpk4 = trimpk[3];
            value += trimpk4 * time4;
        }

        if (trimpk.size()>4) {
            assert(deps.contains("time_in_zone_H5"));
            const RideMetric *time5Metric = deps.value("time_in_zone_H5");
            assert(time5Metric);
            double time5 = time5Metric->value(true);
            double trimpk5 = trimpk[4];
            value += trimpk5 * time5;
        }

        if (trimpk.size()>5) {
            assert(deps.contains("time_in_zone_H6"));
            const RideMetric *time6Metric = deps.value("time_in_zone_H6");
            assert(time6Metric);
            double time6 = time6Metric->value(true);
            double trimpk6 = trimpk[5];
            value += trimpk6 * time6;
        }

        if (trimpk.size()>6) {
            assert(deps.contains("time_in_zone_H7"));
            const RideMetric *time7Metric = deps.value("time_in_zone_H7");
            assert(time7Metric);
            double time7 = time7Metric->value(true);
            double trimpk7 = trimpk[6];
            value += trimpk7 * time7;
        }

        if (trimpk.size()>7) {
            assert(deps.contains("time_in_zone_H8"));
            const RideMetric *time8Metric = deps.value("time_in_zone_H8");
            assert(time8Metric);
            double time8 = time8Metric->value(true);
            double trimpk8 = trimpk[7];
            value += trimpk8 * time8;
        }

        setValue(value/60);
        return;
    }
    RideMetric *clone() const { return new TRIMPZonalPoints(*this); }
};


// RPE is the rate of percieved exercion (borg scale).
// Is a numerical value the riders give in "average" fatigue of the training session he percieved.
//
// Calculate the session RPE that is the product of RPE * time (minutes) of training/race ride. I
// We have 3 different "training load" parameters:
//    - internal load (TRIMPS)
//    - external load (bikescore/TSS)
//    - perceived load (session RPE)
//
class SessionRPE : public RideMetric {
    Q_DECLARE_TR_FUNCTIONS(SessionRPE)

    double score;

    public:

    SessionRPE() : score(0.0)
    {
        setSymbol("session_rpe");
        setInternalName("Session RPE");
    }
    void initialize() {
        setName(tr("Session RPE"));
        setMetricUnits("");
        setImperialUnits("");
        setType(RideMetric::Total);
    }
    void compute(const RideFile *rideFile,
                 const Zones *, int,
                 const HrZones *, int,
                 const QHash<QString,RideMetric*> &deps,
                 const MainWindow *)
    {
        // use RPE value in ride metadata
        double rpe = rideFile->getTag("RPE", "0.0").toDouble();

        assert(deps.contains("time_riding"));
        const RideMetric *timeRidingMetric = deps.value("time_riding");
        assert(timeRidingMetric);

        double secs = timeRidingMetric->value(true);

        // ok lets work the score out
        score = ((secs == 0.0 || rpe == 0) ? 0.0 :  secs/60 *rpe);
        setValue(score);
    }

    RideMetric *clone() const { return new SessionRPE(*this); }
};

static bool added() {
    QVector<QString> deps;
    deps.append("time_riding");
    deps.append("average_hr");
    deps.append("workout_time");
    RideMetricFactory::instance().addMetric(TRIMPPoints(), &deps);

    deps.clear();
    deps.append("trimp_points");
    RideMetricFactory::instance().addMetric(TRIMP100Points(), &deps);

    deps.clear();
    deps.append("average_hr");
    deps.append("time_in_zone_H1");
    deps.append("time_in_zone_H2");
    deps.append("time_in_zone_H3");
    deps.append("time_in_zone_H4");
    deps.append("time_in_zone_H5");
    deps.append("time_in_zone_H6");
    deps.append("time_in_zone_H7");
    deps.append("time_in_zone_H8");
    RideMetricFactory::instance().addMetric(TRIMPZonalPoints(), &deps);

    deps.clear();
    deps.append("time_riding");
    RideMetricFactory::instance().addMetric(SessionRPE(), &deps);
    return true;
}

static bool added_ = added();