简体   繁体   English

将QGyroscopeReading转换为QVector3D的正确方法是什么?

[英]What is correct way to convert QGyroscopeReading to QVector3D?

Is there a correct/good way to convert QGyroscopeReading to QVector3D in Qt5 ? 是否有正确/好的方法将QGyroscopeReading中的QVector3D转换为QVector3D

QGyroscopeReading has its x , y and z values stored as qreal , while QVector3D uses float . QGyroscopeReadingxyz值存储为qreal ,而QVector3D使用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: 由于不能保证qreal是浮点型的(它的类型是在Qt构建时指定的),因此无警告的天真转换看起来非常丑陋:

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

Surely there is something better? 当然有更好的东西了吗?

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. 为了防止此类代码在项目中传播,请从QVector3D继承您的类,并使用qreal参数定义构造函数。

class QRealVector3D: public QVector3D
{
QRealVector3D (qreal x, qreal y, qreal z):
QVector3D (static_cast<float>(x)
  , static_cast<float>(y)
  , static_cast<float>(z)
{}
}

From Qt doc. 来自Qt doc。 QGyroscopeReading Class : QGyroscopeReading类别

QGyroscopeReading Units Q陀螺仪读取单元

The reading contains 3 values, measured in degrees per second that define the movement of the device around the x, y and z axes. 读数包含3个值(以每秒度数为单位),这些值定义了设备围绕x,y和z轴的移动。 Unlike QRotationReading , the values represent the current angular velocity rather than a fixed rotation. QRotationReading不同,这些值表示当前角速度,而不是固定旋转。 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). 因此,将qreal转换为float是您的最小问题,除了您只想将值存储在QVector3D (请记住,这并不代表3D空间中的点或向量)。 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 .) (尽管,我不明白为什么不像QGyroscopeReading那样存储陀螺仪读数。)

If you want to apply QGyroscodeReading to a QVector3D (eg to display the effect), then you could apply the rotations to a predefined vector (eg QVector3D(0, 0, 1) ). 如果要将QGyroscodeReading应用于QVector3D (例如,显示效果),则可以将旋转应用于预定义的矢量(例如, 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 (ie determine duration from current timestamp and the previous one). 对于时间而言, QGyroscopeReading::timestamp()可能很有趣(即从当前时间戳和上一个时间戳确定持续时间)。 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. 应为可能导致时间戳倒退的偶发性问题准备应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM