diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2017-02-22 20:35:39 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2017-03-06 14:43:34 +0000 |
commit | 9a1b163e1fdd99a838c082805c687205b766da25 (patch) | |
tree | c9ce4b7fc05d93280628043c98c4e24b5c34e901 /src/plugins | |
parent | bd1e063980355c82bb20af81a826a2860248ce6a (diff) | |
download | qtsensors-9a1b163e1fdd99a838c082805c687205b766da25.tar.gz |
Use qRadiansToDegrees() and qDegreesToRadians() more widely
They document intent more clearly than arithmetic with pi does.
Also eliminate some hand-rolled RADIANS_TO_DEGREES constants in
favor of calling qRadiansToDegrees().
Change-Id: I7ca5e876b3591433bf681b56ad51c4cb409ac59f
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
Diffstat (limited to 'src/plugins')
8 files changed, 27 insertions, 37 deletions
diff --git a/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp b/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp index f2f0a78..3368870 100644 --- a/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp +++ b/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp @@ -41,7 +41,6 @@ #include <QtCore/qmath.h> -#define RADIANS_TO_DEGREES 57.2957795 #define TIMER_TIMEOUT 250 QT_BEGIN_NAMESPACE @@ -117,8 +116,8 @@ void QPickupSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading const qreal ydiff = pYaxis - y; const qreal zdiff = pZaxis - z; - qreal pitch = qAtan(y / qSqrt(x*x + z*z)) * RADIANS_TO_DEGREES; - qreal roll = qAtan(x / qSqrt(y*y + z*z)) * RADIANS_TO_DEGREES; + qreal pitch = qRadiansToDegrees(qAtan(y / qSqrt(x * x + z * z))); + qreal roll = qRadiansToDegrees(qAtan(x / qSqrt(y * y + z * z))); if ((qAbs(xdiff) < 0.7 && qAbs(ydiff) < .7 && qAbs(zdiff) < .7) || z < 0) { diff --git a/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp b/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp index 1411f44..6a1f170 100644 --- a/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp +++ b/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp @@ -44,7 +44,6 @@ QT_BEGIN_NAMESPACE -#define RADIANS_TO_DEGREES 57.2957795 #define TIMER_TIMEOUT 750 QTwistSensorGestureRecognizer::QTwistSensorGestureRecognizer(QObject *parent) : QSensorGestureRecognizer(parent) @@ -155,7 +154,7 @@ void QTwistSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) if (!detecting && !checking&& dataList.count() > 21) dataList.removeFirst(); - qreal angle = qAtan(x / qSqrt(y*y + z*z)) * RADIANS_TO_DEGREES; + qreal angle = qRadiansToDegrees(qAtan(x / qSqrt(y * y + z * z))); if (qAbs(angle) > 2) { if (detecting) { @@ -177,7 +176,7 @@ void QTwistSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) if (!detecting && increaseCount > 3 && qAbs(angle) > 30) { decreaseCount = 0; detecting = true; - detectedAngle = qAtan(y / qSqrt(x*x + z*z)) * RADIANS_TO_DEGREES; + detectedAngle = qRadiansToDegrees(qAtan(y / qSqrt(x * x + z * z))); } } else { increaseCount = 0; diff --git a/src/plugins/sensors/android/src/androidcompass.cpp b/src/plugins/sensors/android/src/androidcompass.cpp index e98f048..610dcde 100644 --- a/src/plugins/sensors/android/src/androidcompass.cpp +++ b/src/plugins/sensors/android/src/androidcompass.cpp @@ -168,7 +168,6 @@ void AndroidCompass::testStuff() return; qreal azimuth = AndroidSensors::getCompassAzimuth(m_accelerometerListener->reading, m_magnetometerListener->reading); - azimuth = azimuth * 180.0 / M_PI; - m_reading.setAzimuth(azimuth); + m_reading.setAzimuth(qRadiansToDegrees(azimuth)); newReadingAvailable(); } diff --git a/src/plugins/sensors/android/src/androidgyroscope.cpp b/src/plugins/sensors/android/src/androidgyroscope.cpp index 623838d..c963761 100644 --- a/src/plugins/sensors/android/src/androidgyroscope.cpp +++ b/src/plugins/sensors/android/src/androidgyroscope.cpp @@ -38,7 +38,7 @@ ****************************************************************************/ #include "androidgyroscope.h" -#include <math.h> +#include <QtCore/qmath.h> AndroidGyroscope::AndroidGyroscope(AndroidSensors::AndroidSensorType type, QSensor *sensor) : AndroidCommonSensor<QGyroscopeReading>(type, sensor) @@ -50,9 +50,9 @@ void AndroidGyroscope::onSensorChanged(jlong timestamp, const jfloat *values, ui return; m_reader.setTimestamp(timestamp/1000); // check https://developer.android.com/reference/android/hardware/SensorEvent.html#values - m_reader.setX(values[0]*180/M_PI); - m_reader.setY(values[1]*180/M_PI); - m_reader.setZ(values[2]*180/M_PI); + m_reader.setX(qRadiansToDegrees(values[0])); + m_reader.setY(qRadiansToDegrees(values[1])); + m_reader.setZ(qRadiansToDegrees(values[2])); newReadingAvailable(); } diff --git a/src/plugins/sensors/android/src/androidrotation.cpp b/src/plugins/sensors/android/src/androidrotation.cpp index dfe4f2a..f7d0225 100644 --- a/src/plugins/sensors/android/src/androidrotation.cpp +++ b/src/plugins/sensors/android/src/androidrotation.cpp @@ -38,7 +38,7 @@ ****************************************************************************/ #include "androidrotation.h" -#include <math.h> +#include <QtCore/qmath.h> AndroidRotation::AndroidRotation(AndroidSensors::AndroidSensorType type, QSensor *sensor) : AndroidCommonSensor<QRotationReading>(type, sensor) @@ -55,9 +55,9 @@ void AndroidRotation::onSensorChanged(jlong timestamp, const jfloat *values, uin return; m_reader.setTimestamp(timestamp/1000); - float rz = -values[0]*180/M_PI; - float rx = -values[1]*180/M_PI; - float ry = values[2]*180/M_PI; + float rz = -qRadiansToDegrees(values[0]); + float rx = -qRadiansToDegrees(values[1]); + float ry = qRadiansToDegrees(values[2]); m_reader.setFromEuler(rx, ry, rz); newReadingAvailable(); } diff --git a/src/plugins/sensors/generic/genericrotationsensor.cpp b/src/plugins/sensors/generic/genericrotationsensor.cpp index adf28e4..4076829 100644 --- a/src/plugins/sensors/generic/genericrotationsensor.cpp +++ b/src/plugins/sensors/generic/genericrotationsensor.cpp @@ -41,8 +41,6 @@ #include <QDebug> #include <qmath.h> -#define RADIANS_TO_DEGREES 57.2957795 - char const * const genericrotationsensor::id("generic.rotation"); genericrotationsensor::genericrotationsensor(QSensor *sensor) @@ -88,8 +86,8 @@ bool genericrotationsensor::filter(QSensorReading *reading) // Note that the formula used come from this document: // http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf - pitch = qAtan(y / qSqrt(x*x + z*z)) * RADIANS_TO_DEGREES; - roll = qAtan(x / qSqrt(y*y + z*z)) * RADIANS_TO_DEGREES; + pitch = qRadiansToDegrees(qAtan(y / qSqrt(x * x + z * z))); + roll = qRadiansToDegrees(qAtan(x / qSqrt(y * y + z * z))); // Roll is a left-handed rotation but we need right-handed rotation roll = -roll; @@ -98,7 +96,7 @@ bool genericrotationsensor::filter(QSensorReading *reading) // Note that theta is defined as the angle of the Z axis relative // to gravity (see referenced document). It's negative when the // face of the device points downward. - qreal theta = qAtan(qSqrt(x*x + y*y) / z) * RADIANS_TO_DEGREES; + qreal theta = qRadiansToDegrees(qAtan(qSqrt(x * x + y * y) / z)); if (theta < 0) { if (roll > 0) roll = 180 - roll; diff --git a/src/plugins/sensors/generic/generictiltsensor.cpp b/src/plugins/sensors/generic/generictiltsensor.cpp index 831b03e..a4ada7d 100644 --- a/src/plugins/sensors/generic/generictiltsensor.cpp +++ b/src/plugins/sensors/generic/generictiltsensor.cpp @@ -39,14 +39,13 @@ #include "generictiltsensor.h" #include <QDebug> -#define _USE_MATH_DEFINES #include <qmath.h> char const * const GenericTiltSensor::id("generic.tilt"); GenericTiltSensor::GenericTiltSensor(QSensor *sensor) : QSensorBackend(sensor) - , radAccuracy(M_PI / 180) + , radAccuracy(qDegreesToRadians(qreal(1))) , pitch(0) , roll(0) , calibratedPitch(0) @@ -106,11 +105,6 @@ void GenericTiltSensor::calibrate() calibratedRoll = roll; } -static qreal rad2deg(qreal rad) -{ - return rad / (2 * M_PI) * 360; -} - bool GenericTiltSensor::filter(QAccelerometerReading *reading) { /* @@ -159,18 +153,18 @@ bool GenericTiltSensor::filter(QAccelerometerReading *reading) qDebug() << "new yrot: " << yrot; qDebug() << "----------------------------------"; #endif - qreal dxrot = rad2deg(xrot) - xRotation; - qreal dyrot = rad2deg(yrot) - yRotation; + qreal dxrot = qRadiansToDegrees(xrot) - xRotation; + qreal dyrot = qRadiansToDegrees(yrot) - yRotation; if (dxrot < 0) dxrot = -dxrot; if (dyrot < 0) dyrot = -dyrot; bool setNewReading = false; - if (dxrot >= rad2deg(radAccuracy) || !sensor()->skipDuplicates()) { - xRotation = rad2deg(xrot); + if (dxrot >= qRadiansToDegrees(radAccuracy) || !sensor()->skipDuplicates()) { + xRotation = qRadiansToDegrees(xrot); setNewReading = true; } - if (dyrot >= rad2deg(radAccuracy) || !sensor()->skipDuplicates()) { - yRotation = rad2deg(yrot); + if (dyrot >= qRadiansToDegrees(radAccuracy) || !sensor()->skipDuplicates()) { + yRotation = qRadiansToDegrees(yrot); setNewReading = true; } diff --git a/src/plugins/sensors/ios/iosgyroscope.mm b/src/plugins/sensors/ios/iosgyroscope.mm index 0cd37ae..8155aba 100644 --- a/src/plugins/sensors/ios/iosgyroscope.mm +++ b/src/plugins/sensors/ios/iosgyroscope.mm @@ -41,6 +41,7 @@ #include "iosgyroscope.h" #import <CoreMotion/CoreMotion.h> +#import <QtCore/qmath.h> char const * const IOSGyroscope::id("ios.gyroscope"); @@ -89,9 +90,9 @@ void IOSGyroscope::timerEvent(QTimerEvent *) if (rate.x != rate.x || rate.y != rate.y || rate.z != rate.z) return; m_reading.setTimestamp(quint64(data.timestamp * 1e6)); - m_reading.setX((qreal(rate.x) / M_PI) * 180); - m_reading.setY((qreal(rate.y) / M_PI) * 180); - m_reading.setZ((qreal(rate.z) / M_PI) * 180); + m_reading.setX(qRadiansToDegrees(qreal(rate.x))); + m_reading.setY(qRadiansToDegrees(qreal(rate.y))); + m_reading.setZ(qRadiansToDegrees(qreal(rate.z))); newReadingAvailable(); } |