-
Notifications
You must be signed in to change notification settings - Fork 7
/
FusionMath.cpp
29 lines (23 loc) · 1.12 KB
/
FusionMath.cpp
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
#include "stdafx.h"
namespace je_nourish_fusion {
void rpyFromQuaternion(OSVR_Quaternion* quaternion, OSVR_Vec3* rpy) {
double q[4] = {
osvrQuatGetW(quaternion),
osvrQuatGetZ(quaternion),
osvrQuatGetX(quaternion),
osvrQuatGetY(quaternion)
};
osvrVec3SetX(rpy, -atan2(2 * (q[0] * q[1] + q[2] * q[3]), 1 - 2 * (q[1] * q[1] + q[2] * q[2])));
osvrVec3SetY(rpy, -asin(2 * (q[0] * q[2] - q[3] * q[1])));
osvrVec3SetZ(rpy, -atan2(2 * (q[0] * q[3] + q[1] * q[2]), 1 - 2 * (q[2] * q[2] + q[3] * q[3])));
}
void quaternionFromRPY(OSVR_Vec3* rpy, OSVR_Quaternion* quaternion) {
double r = -osvrVec3GetX(rpy);
double p = -osvrVec3GetY(rpy);
double y = -osvrVec3GetZ(rpy);
osvrQuatSetZ(quaternion, sin(r / 2) * cos(p / 2) * cos(y / 2) - cos(r / 2) * sin(p / 2) * sin(y / 2));
osvrQuatSetX(quaternion, cos(r / 2) * sin(p / 2) * cos(y / 2) + sin(r / 2) * cos(p / 2) * sin(y / 2));
osvrQuatSetY(quaternion, cos(r / 2) * cos(p / 2) * sin(y / 2) - sin(r / 2) * sin(p / 2) * cos(y / 2));
osvrQuatSetW(quaternion, cos(r / 2) * cos(p / 2) * cos(y / 2) + sin(r / 2) * sin(p / 2) * sin(y / 2));
}
}