1

Is there a correct/good way to convert QGyroscopeReading to QVector3D in Qt5?

QGyroscopeReading has its x,y and z values stored as qreal, while QVector3D uses float.

Since qreal is not guaranteed to be float (it's type is specified at Qt build time), the warning-free naive conversion looks really ugly:

QGyroscopeReading gr;
QVector3D myVec(static_cast<float>(gr.x())
  , static_cast<float>(gr.y())
  , static_cast<float>(gr.z()));

Surely there is something better?

Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
  • Please, see my [preliminary answer](https://stackoverflow.com/a/52034850/7478597). Can you elaborate _**convert** `QGyroscopeReading` to `QVector3D` in Qt5_? Do want to convert the angular velocities to a 3d vector (i.e. a point or vector in 3d space)? – Scheff's Cat Aug 27 '18 at 07:58

2 Answers2

1

It's designed to look ugly. It has to remind you that there is some dangerous code here.

To prevent spreading such code in the project inherit your class from QVector3D and define constructor with qreal parameters.

class QRealVector3D: public QVector3D
{
QRealVector3D (qreal x, qreal y, qreal z):
QVector3D (static_cast<float>(x)
  , static_cast<float>(y)
  , static_cast<float>(z)
{}
}
Konstantin T.
  • 994
  • 8
  • 22
1

From Qt doc. QGyroscopeReading Class:

QGyroscopeReading Units

The reading contains 3 values, measured in degrees per second that define the movement of the device around the x, y and z axes. Unlike QRotationReading, the values represent the current angular velocity rather than a fixed rotation. The measurements are in degrees per second.

So, conversion of qreal to float is your least problem except you just want to store the values in a QVector3D (remembering that this doesn't represent a point or vector in 3D space). But if this is the case, then your conversion is fine. (Although, I don't understand why not to store gyroscope reading just as QGyroscopeReading.)

If you want to apply QGyroscodeReading to a QVector3D (e.g. to display the effect), then you could apply the rotations to a predefined vector (e.g. QVector3D(0, 0, 1)). For a cumulate update, the time would be necessary as well (to convert angular velocities to angles).

For the time, the QGyroscopeReading::timestamp() could be interesting (i.e. determine duration from current timestamp and the previous one). Though, the doc. is not very encouraging:

Note that some platforms do not deliver timestamps correctly. Applications should be prepared for occasional issues that cause timestamps to jump backwards.

Community
  • 1
  • 1
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • QVector3D does not necessarily represent a point in space either, but it gives access to many usefil ways of manipulating a vector (and a vector is after all just an 1D array of numbers). I understand the argument that it could be confusing but at the same, how should we apply manipulations on the reading if not with QVector3D? – Mr. Developerdude Aug 29 '18 at 00:43
  • 1
    @LennartRolland I agree. It seems, you just want to use `QVector3D` as container. If this is the case and you are concerned about precision of components, why not a self-made `struct AngleVelocities { qReal rX, rY, rZ; };`? I assume `QVector3D` uses explicitly `float` instead of `qReal` as its intended usage is for OpenGL vertices where number of bytes is a critical factor to be used carefully. – Scheff's Cat Aug 29 '18 at 05:34