-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathje_nourish_fusion.cpp
130 lines (99 loc) · 3.94 KB
/
je_nourish_fusion.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
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
#include "stdafx.h"
#include <iostream>
// Anonymous namespace to avoid symbol collision
namespace je_nourish_fusion {
class FusionDevice {
public:
FusionDevice(OSVR_PluginRegContext ctx, Json::Value config) {
osvrPose3SetIdentity(&m_state);
m_useTimestamp = config.isMember("timestamp");
m_usePositionTimestamp = m_useTimestamp && config["timestamp"].asString().compare("position") == 0;
if ((m_useOffset = config.isMember("offsetFromRotationCenter"))) {
osvrVec3Zero(&m_offset);
if (config["offsetFromRotationCenter"].isMember("x")) {
osvrVec3SetX(&m_offset, config["offsetFromRotationCenter"]["x"].asDouble());
}
if (config["offsetFromRotationCenter"].isMember("y")) {
osvrVec3SetY(&m_offset, config["offsetFromRotationCenter"]["y"].asDouble());
}
if (config["offsetFromRotationCenter"].isMember("z")) {
osvrVec3SetZ(&m_offset, config["offsetFromRotationCenter"]["z"].asDouble());
}
}
OSVR_DeviceInitOptions opts = osvrDeviceCreateInitOptions(ctx);
osvrDeviceTrackerConfigure(opts, &m_tracker);
OSVR_DeviceToken token;
osvrAnalysisSyncInit(ctx, config["name"].asCString(), opts, &token, &m_ctx);
m_dev = new osvr::pluginkit::DeviceToken(token);
m_positionReader = PositionReaderFactory::getReader(m_ctx, config["position"]);
m_orientationReader = OrientationReaderFactory::getReader(m_ctx, config["orientation"]);
if (m_positionReader == NULL) {
std::cout << "Fusion Device: Position Reader not created" << std::endl;
}
if (m_orientationReader == NULL) {
std::cout << "Fusion Device: Orientation Reader not created" << std::endl;
}
m_dev->sendJsonDescriptor(je_nourish_fusion_json);
m_dev->registerUpdateCallback(this);
}
OSVR_ReturnCode update() {
osvrClientUpdate(m_ctx);
OSVR_TimeValue timeValuePosition;
OSVR_TimeValue timeValueOrientation;
m_positionReader->update(&m_state.translation, &timeValuePosition);
m_orientationReader->update(&m_state.rotation, &timeValueOrientation);
if (m_useOffset) {
Eigen::Quaterniond rotation = osvr::util::fromQuat(m_state.rotation);
Eigen::Map<Eigen::Vector3d> translation = osvr::util::vecMap(m_state.translation);
translation += rotation._transformVector(osvr::util::vecMap(m_offset));
}
if (m_useTimestamp) {
OSVR_TimeValue timeValue = m_usePositionTimestamp ? timeValuePosition : timeValueOrientation;
osvrDeviceTrackerSendPoseTimestamped(*m_dev, m_tracker, &m_state, 0, &timeValue);
}
else {
osvrDeviceTrackerSendPose(*m_dev, m_tracker, &m_state, 0);
}
return OSVR_RETURN_SUCCESS;
}
private:
IPositionReader* m_positionReader;
IOrientationReader* m_orientationReader;
OSVR_ClientContext m_ctx;
OSVR_ClientInterface m_position;
OSVR_ClientInterface m_orientation;
osvr::pluginkit::DeviceToken* m_dev;
OSVR_TrackerDeviceInterface m_tracker;
OSVR_PoseState m_state;
bool m_useOffset;
OSVR_Vec3 m_offset;
bool m_useTimestamp;
bool m_usePositionTimestamp;
};
class FusionDeviceConstructor {
public:
FusionDeviceConstructor() {}
OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx, const char *params) {
Json::Value root;
if (params) {
Json::Reader r;
if (!r.parse(params, root)) {
std::cerr << "Could not parse parameters!" << std::endl;
}
}
if (!root.isMember("name") || !root.isMember("position") || !root.isMember("orientation")) {
std::cerr << "Warning: got configuration, but no trackers specified"
<< std::endl;
return OSVR_RETURN_FAILURE;
}
osvr::pluginkit::registerObjectForDeletion(
ctx, new FusionDevice(ctx, root));
return OSVR_RETURN_SUCCESS;
}
};
} // namespace
OSVR_PLUGIN(je_nourish_fusion) {
osvr::pluginkit::registerDriverInstantiationCallback(
ctx, "FusionDevice", new je_nourish_fusion::FusionDeviceConstructor);
return OSVR_RETURN_SUCCESS;
}