-
Notifications
You must be signed in to change notification settings - Fork 0
/
vrrotationsensor.h
167 lines (147 loc) · 3.79 KB
/
vrrotationsensor.h
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
#ifndef ROTATIONSENSOR_H
#define ROTATIONSENSOR_H
#include <QObject>
#include <QQuaternion>
#include <QRotationSensor>
#include <QGyroscope>
#include <QElapsedTimer>
#include <cmath>
#ifdef Q_OS_ANDROID
#include <QAndroidJniObject>
#include <QAndroidJniEnvironment>
#include <jni.h>
#endif
/**
* @brief The RotationEvent class
*/
class RotationEvent
{
public:
/**
* @brief rotation Ratation from the sensor
*/
QQuaternion rotation;
/**
* @brief euler
* @return An euler representation of the rotation.
*/
QVector3D euler() const
{
float x, y, z;
float angle = 2 * acos(rotation.scalar());
double s = sqrt(1-rotation.scalar()*rotation.scalar()); // assuming quaternion normalised then w is less than 1, so term always positive.
if (s < 0.001) { // test to avoid divide by zero, s is always positive due to sqrt
// if s close to zero then direction of axis not important
x = rotation.x(); // if it is important that axis is normalised then replace with x=1; y=z=0;
y = rotation.y();
z = rotation.z();
} else {
x = rotation.x() / s; // normalise axis
y = rotation.y() / s;
z = rotation.z() / s;
}
return QVector3D(x*angle, y*angle, z*angle);
}
};
/**
* @brief The VrRotationSensor class which represents a rotation sensor
*/
class VrRotationSensor : public QObject
{
Q_OBJECT
public:
/**
* @brief VrRotationSensor
* @param enable_compass Enable correction of rotation to the magnetic field of earth
* @param parent
*/
explicit VrRotationSensor(bool enable_compass=false, QObject *parent = 0);
~VrRotationSensor();
/**
* @brief start Start the sensor
*/
void start();
/**
* @brief stop Stop the sensor
*/
void stop();
/**
* @brief reading
* @return Last reaging value.
*/
const RotationEvent * reading();
signals:
/**
* @brief rotationChanged Inform about new reading avilability
*/
void rotationChanged(const RotationEvent *);
public slots:
protected:
bool filter(QRotationReading * reading);
bool filter(QGyroscopeReading * reading);
private:
class Rotation: public QRotationFilter
{
public:
Rotation(VrRotationSensor * sensor): sensor(sensor)
{
rotation.addFilter(this);
}
void start()
{
rotation.start();
}
void stop()
{
rotation.stop();
}
protected:
bool filter(QRotationReading * reading) override
{
return sensor->filter(reading);
}
private:
VrRotationSensor * sensor;
QRotationSensor rotation;
};
class Gyroscope: public QGyroscopeFilter
{
public:
Gyroscope(VrRotationSensor * sensor): sensor(sensor)
{
gyroscope.addFilter(this);
}
void start()
{
gyroscope.start();
}
void stop()
{
gyroscope.stop();
}
protected:
bool filter(QGyroscopeReading * reading) override
{
return sensor->filter(reading);
}
private:
VrRotationSensor * sensor;
QGyroscope gyroscope;
};
bool enable_compass;
static VrRotationSensor * self_ptr;
RotationEvent event;
#ifdef Q_OS_ANDROID
void initJNI();
static void onRotationChanged(JNIEnv *, jobject, jfloat w, jfloat a, jfloat b, jfloat c);
QAndroidJniObject * java_backend;
#endif
Rotation rotation;
Gyroscope gyroscope;
QQuaternion quaternionGyroscope, deltaQuaternion;
QQuaternion quaternionRotationVector;
bool positionInitialised;
quint64 timestamp;
double gyroscopeRotationVelocity;
};
#endif // ROTATIONSENSOR_H