put all our unit conversions in one header file

This commit is contained in:
Sean Rhea
2009-10-10 12:27:42 -04:00
parent f449fbaaaf
commit 318c9debd4
11 changed files with 54 additions and 45 deletions

View File

@@ -20,6 +20,7 @@
#include "RideFile.h"
#include "RideItem.h"
#include "Settings.h"
#include "Units.h"
#include "Zones.h"
#include <assert.h>
@@ -167,9 +168,6 @@ class AllPlotZoneLabel: public QwtPlotItem
static inline double
max(double a, double b) { if (a > b) return a; else return b; }
#define MILES_PER_KM 0.62137119
#define FEET_PER_M 3.2808399
AllPlot::AllPlot(QWidget *parent):
QwtPlot(parent),
settings(NULL),
@@ -539,7 +537,7 @@ AllPlot::setData(RideItem *_rideItem)
if (!altArray.empty())
altArray[arrayLength] = (useMetricUnits
? point->alt
: point->alt * FEET_PER_M);
: point->alt * FEET_PER_METER);
interArray[arrayLength] = point->interval;
distanceArray[arrayLength] = max(0,
(useMetricUnits

View File

@@ -17,9 +17,7 @@
*/
#include "RideMetric.h"
#define MILES_PER_KM 0.62137119
#define FEET_PER_METER 3.2808399
#include "Units.h"
class WorkoutTime : public RideMetric {
double seconds;

View File

@@ -19,14 +19,13 @@
#include "CsvRideFile.h"
#include "Units.h"
#include <QRegExp>
#include <QTextStream>
#include <algorithm> // for std::sort
#include <assert.h>
#include "math.h"
#define MILES_TO_KM 1.609344
static int csvFileReaderRegistered =
RideFileFactory::instance().registerReader("csv", new CsvFileReader());
@@ -142,8 +141,8 @@ RideFile *CsvFileReader::openRideFile(QFile &file, QStringList &errors) const
interval = line.section(',', 7, 7).toInt();
alt = line.section(',', 8, 8).toDouble();
if (!metric) {
km *= MILES_TO_KM;
kph *= MILES_TO_KM;
km *= KM_PER_MILE;
kph *= KM_PER_MILE;
}
}
else if (iBike) {
@@ -160,8 +159,8 @@ RideFile *CsvFileReader::openRideFile(QFile &file, QStringList &errors) const
hr = line.section(',', 5, 5).toDouble();
interval = NULL; //not provided?
if (!metric) {
km *= MILES_TO_KM;
kph *= MILES_TO_KM;
km *= KM_PER_MILE;
kph *= KM_PER_MILE;
}
}
else {
@@ -190,8 +189,8 @@ RideFile *CsvFileReader::openRideFile(QFile &file, QStringList &errors) const
minutes = minutes/60.0;
if (!metric) {
km *= MILES_TO_KM;
kph *= MILES_TO_KM;
km *= KM_PER_MILE;
kph *= KM_PER_MILE;
}
}

View File

@@ -33,6 +33,7 @@
#include "RideMetric.h"
#include "Settings.h"
#include "TimeUtils.h"
#include "Units.h"
#include "WeeklySummaryWindow.h"
#include "Zones.h"
#include <assert.h>
@@ -611,7 +612,7 @@ void MainWindow::getBSFactors(float &timeBS, float &distanceBS)
{
int rides;
double seconds, distance, bs, convertUnit;
double seconds, distance, bs;
RideItem * lastRideItem;
QProgressDialog * progress;
bool aborted = false;
@@ -680,17 +681,8 @@ void MainWindow::getBSFactors(float &timeBS, float &distanceBS)
}
}
if (rides) {
// convert distance from metric:
if (!useMetricUnits)
{
const double MILES_PER_KM = 0.62137119;
convertUnit = MILES_PER_KM;
}
else {
convertUnit = 1.0;
}
distance *= convertUnit;
distance *= MILES_PER_KM;
timeBS = (bs * 3600) / seconds; // BS per hour
distanceBS = bs / distance; // BS per mile or km
}

View File

@@ -17,15 +17,13 @@
*/
#include "ManualRideFile.h"
#include "Units.h"
#include <QRegExp>
#include <QTextStream>
#include <algorithm> // for std::sort
#include <assert.h>
#include "math.h"
#define MILES_TO_KM 1.609344
#define FEET_TO_METERS 0.3048
static int manualFileReaderRegistered =
RideFileFactory::instance().registerReader("man", new ManualFileReader());
@@ -105,8 +103,8 @@ RideFile *ManualFileReader::openRideFile(QFile &file, QStringList &errors) const
hr = line.section(',', 4, 4).toDouble();
bs = line.section(',', 5, 5).toDouble();
if (!metric) {
km *= MILES_TO_KM;
kph *= MILES_TO_KM;
km *= KM_PER_MILE;
kph *= KM_PER_MILE;
}
cad = nm = 0.0;
interval = 0;

View File

@@ -17,6 +17,7 @@
*/
#include "PowerTapUtil.h"
#include "Units.h"
#include <QString>
#include <math.h>
@@ -114,7 +115,6 @@ my_round(double x)
#define PI M_PI
#define LBFIN_TO_NM 0.11298483
#define KM_TO_MI 0.62137119
#define BAD_LBFIN_TO_NM_1 0.112984
#define BAD_LBFIN_TO_NM_2 0.1129824
@@ -191,7 +191,7 @@ PowerTapUtil::unpack_data(unsigned char *buf, int compat, double rec_int_secs,
if (compat)
*mph = my_round(kph10) / 10.0 * BAD_KM_TO_MI;
else
*mph = kph10 / 10.0 * KM_TO_MI;
*mph = kph10 / 10.0 * MILES_PER_KM;
// from http://en.wikipedia.org/wiki/Torque#Conversion_to_other_units
double dMetersPerMinute = (kph10 / 10.0) * 1000.0 / 60.0;

View File

@@ -18,11 +18,10 @@
#include "RawRideFile.h"
#include "PowerTapUtil.h"
#include "Units.h"
#include <assert.h>
#include <math.h>
#define MILES_TO_KM 1.609344
#define KM_TO_MI 0.62137119
#define BAD_KM_TO_MI 0.62
static int rawFileReaderRegistered =
@@ -71,7 +70,7 @@ time_cb(struct tm *, time_t since_epoch, void *context)
state->start_since_epoch = since_epoch;
double secs = since_epoch - state->start_since_epoch;
state->rideFile->appendPoint(secs, 0.0, 0.0,
state->last_miles * MILES_TO_KM, 0.0,
state->last_miles * KM_PER_MILE, 0.0,
0.0, 0.0, 0.0, state->last_interval);
state->last_secs = secs;
}
@@ -85,8 +84,8 @@ data_cb(double secs, double nm, double mph, double watts, double miles, double a
if (watts < 0.0) watts = 0.0;
ReadState *state = (ReadState*) context;
state->rideFile->appendPoint(secs, cad, hr, miles * MILES_TO_KM,
mph * MILES_TO_KM, nm, watts, alt, interval);
state->rideFile->appendPoint(secs, cad, hr, miles * KM_PER_MILE,
mph * KM_PER_MILE, nm, watts, alt, interval);
state->last_secs = secs;
state->last_miles = miles;
state->last_interval = interval;
@@ -181,7 +180,7 @@ pt_read_raw(FILE *in, int compat, void *context,
if (compat)
miles = round(meters) / 1000.0 * BAD_KM_TO_MI;
else
miles = meters / 1000.0 * KM_TO_MI;
miles = meters / 1000.0 * MILES_PER_KM;
if (data_cb)
data_cb(secs, nm, mph, watts, miles, alt, cad,
hr, interval, context);

View File

@@ -17,9 +17,10 @@
*/
#include "RideFile.h"
#include "Settings.h"
#include "Units.h"
#include <QtXml/QtXml>
#include <assert.h>
#include "Settings.h"
static void
markInterval(QDomDocument &doc, QDomNode &xride, QDomNode &xintervals,
@@ -113,12 +114,10 @@ void RideFile::writeAsCsv(QFile &file, bool bIsMetric) const
if (!bIsMetric)
{
out << "Minutes,Torq (N-m),MPH,Watts,Miles,Cadence,Hrate,ID,Altitude (feet)\n";
const double MILES_PER_KM = 0.62137119;
convertUnit = MILES_PER_KM;
}
else {
out << "Minutes,Torq (N-m),Km/h,Watts,Km,Cadence,Hrate,ID,Altitude (m)\n";
// TODO: use KM_TO_MI from lib/pt.c instead?
convertUnit = 1.0;
}

View File

@@ -21,14 +21,12 @@
#include "RideFile.h"
#include "Settings.h"
#include "TimeUtils.h"
#include "Units.h"
#include "Zones.h"
#include <iostream> // delete me
#include <assert.h>
#include <math.h>
#include <QtXml/QtXml>
#define MILES_PER_KM 0.62137119
RideItem::RideItem(int type,
QString path, QString fileName, const QDateTime &dateTime,
Zones **zones, QString notesFileName) :

27
src/Units.h Normal file
View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2009 Sean C. Rhea (srhea@srhea.net)
*
* 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
*/
#ifndef _GC_Units_h
#define _GC_Units_h 1
#define KM_PER_MILE 1.609344
#define MILES_PER_KM 0.62137119
#define FEET_PER_METER 3.2808399
#endif // _GC_Units_h

View File

@@ -91,6 +91,7 @@ HEADERS += \
TcxRideFile.h \
TimeUtils.h \
ToolsDialog.h \
Units.h \
WeeklySummaryWindow.h \
WkoRideFile.h \
XmlRideFile.h \