cgv
Loading...
Searching...
No Matches
vr_kit.cxx
1#include "vr_kit.h"
2#include "vr_driver.h"
3#include <cgv/math/fmat.h>
4
5namespace vr {
13 vr_trackable_state& vr_kit::ref_tracking_reference_state(const std::string& serial_nummer)
14 {
15 return driver->ref_tracking_reference_state(serial_nummer);
16 }
34 {
35 delete camera;
36 camera = nullptr;
37 }
39 vr_kit::vr_kit(vr_driver* _driver, void* _handle, const std::string& _name, unsigned _width, unsigned _height, unsigned _nr_multi_samples) :
40 gl_vr_display(_width, _height, _nr_multi_samples), driver(_driver), handle(_handle), name(_name), camera(nullptr), skip_calibration(false) {}
43 {
44 if (camera)
45 camera->stop();
46 }
48 const vr_driver* vr_kit::get_driver() const { return driver; }
50 void* vr_kit::get_handle() const { return handle; }
52 vr_camera * vr_kit::get_camera() const { return camera; }
54 const std::string& vr_kit::get_name() const { return name; }
55
56 float dot(int n, const float* a, const float* b, int step_a = 1, int step_b=1)
57 {
58 float res = 0;
59 for (int i = 0; i < n; ++i)
60 res += a[i*step_a] * b[i*step_b];
61 return res;
62 }
63 void homogenize_matrix(float* A, int n, const float* B = 0)
64 {
65 if (B == 0)
66 B = A;
67 for (int j = n; j >= 0; --j) {
68 A[j*(n + 1) + n] = (j == n ? 1.0f : 0.0f);
69 for (int i = n - 1; i >= 0; --i)
70 A[j*(n + 1) + i] = B[j*n + i];
71 }
72 }
73 void invert_rigid_body_transformation(float* T)
74 {
75 int i;
76 float t[3];
77 for (i = 0; i < 3; ++i)
78 t[i] = -dot(3, T + 4 * i, T + 12);
79 for (i = 0; i < 3; ++i) {
80 T[12 + i] = t[i];
81 for (int j = i + 1; j < 3; ++j)
82 std::swap(T[4 * j + i], T[4 * i + j]);
83 }
84 }
85 void matrix_multiplication(const float* A, const float* B, float* C)
86 {
87 for (int i = 0; i < 4; ++i)
88 for (int j = 0; j < 4; ++j)
89 C[4 * j + i] = dot(4, A + i, B + 4 * j, 4);
90 }
92 void vr_kit::put_world_to_eye_transform(int eye, const float* hmd_pose, float* modelview_matrix) const
93 {
94 float eye_to_head[16];
95 put_eye_to_head_matrix(eye, eye_to_head);
96 homogenize_matrix(eye_to_head, 3);
97 invert_rigid_body_transformation(eye_to_head);
98 float head_to_world[16];
99 homogenize_matrix(head_to_world, 3, hmd_pose);
100 invert_rigid_body_transformation(head_to_world);
101 matrix_multiplication(eye_to_head, head_to_world, modelview_matrix);
102 }
103 bool vr_kit::query_state(vr_kit_state& state, int pose_query)
104 {
105 bool res = query_state_impl(state, pose_query);
106 const vr_driver* dp = get_driver();
107 if (pose_query == 0 || skip_calibration || !dp->is_calibration_transformation_enabled())
108 return res;
109 if (state.hmd.status == VRS_TRACKED)
110 dp->calibrate_pose(state.hmd.pose);
111 for (int i = 0; i < max_nr_controllers; ++i)
112 if (state.controller[i].status == VRS_TRACKED)
113 dp->calibrate_pose(state.controller[i].pose);
114 return res;
115 }
118 {
119 return info;
120 }
122 {
123 input_configs[ci][ii] = cic;
124 }
127 {
128 return input_configs[ci][ii];
129 }
130
131}
132
133
implements offscreen rendering
interface for mono or stereo cameras in VR headsets
Definition vr_camera.h:39
bool stop()
stop streaming of frames
Definition vr_camera.cxx:47
interface class for vr drivers.
Definition vr_driver.h:57
void clear_tracking_reference_states()
remove all tracking reference states
Definition vr_driver.cxx:28
void mark_tracking_references_as_untracked()
mark all tracking reference states as untracked
Definition vr_driver.cxx:34
void calibrate_pose(float(&pose)[12]) const
in case calibration matrix is enabled, transform given pose in place
Definition vr_driver.cxx:55
vr_tracking_system_info tracking_system_info
store tracking system info to be filled by driver implementations
Definition vr_driver.h:78
bool is_calibration_transformation_enabled() const
check whether calibration transformation is enabled; false after construction and typically set to tr...
Definition vr_driver.cxx:94
vr_trackable_state & ref_tracking_reference_state(const std::string &serial_nummer)
provide reference to tracking reference states
Definition vr_driver.cxx:22
virtual void put_world_to_eye_transform(int eye, const float *hmd_pose, float *modelview_matrix) const
access to 4x4 modelview transformation matrix of given eye in column major format,...
Definition vr_kit.cxx:92
vr_kit(vr_driver *_driver, void *_handle, const std::string &_name, unsigned _width, unsigned _height, unsigned _nr_multi_samples=4)
construct
Definition vr_kit.cxx:39
vr_trackable_state & ref_tracking_reference_state(const std::string &serial_nummer)
write access to the state of the tracking reference with given serial number
Definition vr_kit.cxx:13
vr_camera * get_camera() const
return camera or nullptr if not available
Definition vr_kit.cxx:52
vr_kit_info info
store vr kit info to be filled and updated by driver implementations
Definition vr_kit.h:82
controller_input_config input_configs[4][5]
store controller input configs per controller and input
Definition vr_kit.h:84
void destruct_camera()
destruct camera
Definition vr_kit.cxx:33
const controller_input_config & get_controller_input_config(int controller_index, int input_index) const
query the configuration of a controller input
Definition vr_kit.cxx:126
bool query_state(vr_kit_state &state, int pose_query=2)
query current state of vr kit and return whether this was successful
Definition vr_kit.cxx:103
const vr_kit_info & get_device_info() const
return information on the currently attached devices
Definition vr_kit.cxx:117
void * get_handle() const
return handle of vr kit
Definition vr_kit.cxx:50
const vr_driver * get_driver() const
return driver
Definition vr_kit.cxx:48
const std::string & get_name() const
return name of vr_kit
Definition vr_kit.cxx:54
virtual void set_controller_input_config(int controller_index, int input_index, const controller_input_config &cic)
set the configuration of a controller input
Definition vr_kit.cxx:121
void * handle
handle for internal use
Definition vr_kit.h:74
bool skip_calibration
whether to skip driver calibration - defaults to false
Definition vr_kit.h:78
vr_driver * driver
pointer to driver that created the vr kit
Definition vr_kit.h:72
void clear_tracking_reference_states()
remove all reference states
Definition vr_kit.cxx:23
virtual bool query_state_impl(vr_kit_state &state, int pose_query)=0
derived kits implement this without caring about calibration; vr_kit::query_state() will apply driver...
virtual ~vr_kit()
declare virtual destructor
Definition vr_kit.cxx:42
virtual void put_eye_to_head_matrix(int eye, float *pose_matrix) const =0
access to 3x4 matrix in column major format for transformation from eye (0..left, 1....
vr_camera * camera
pointer to camera
Definition vr_kit.h:76
void mark_tracking_references_as_untracked()
mark all reference states as untracked
Definition vr_kit.cxx:28
std::string name
name in case driver provides this information (not reliable)
Definition vr_kit.h:80
vr_tracking_system_info & ref_tracking_system_info()
write access to tracking system info
Definition vr_kit.cxx:18
the vr namespace for virtual reality support
@ VRS_TRACKED
trackable is connected and tracked
Definition vr_state.h:88
const unsigned max_nr_controllers
maximum number of attachable controller and tracker devices
Definition vr_state.h:19
configuration of a controller input axis
Definition vr_kit.h:20
float precision
if precision is larger than zero, values are rounded to multiples of precision.
Definition vr_kit.h:27
float dead_zone
all input values below dead_zone are clamped to 0.
Definition vr_kit.h:24
float threshold
if value gets larger than threshold a key press event is triggered if this is enabled.
Definition vr_kit.h:30
controller_input_config()
initialize to defaults (dead_zone=precision=0;threshold=0.5)
Definition vr_kit.cxx:7
information provided for a vr kit
Definition vr_info.h:146
structure that stores all information describing the state of a VR kit
Definition vr_state.h:139
vr_controller_state controller[max_nr_controllers]
status, pose, button, axes, and vibration information of up to vr::max_nr_controllers controller and ...
Definition vr_state.h:143
vr_trackable_state hmd
status and pose of hmd
Definition vr_state.h:141
a trackable knows whether it is tracked and its 6d pose stored as 3x4 matrix in column major format
Definition vr_state.h:94
VRStatus status
whether trackable is currently tracked, only in case of true, the pose member contains useful informa...
Definition vr_state.h:96
float pose[12]
pose as 3x4 matrix in column major format, where each column is a vector in world coordinates
Definition vr_state.h:104
information provided for tracking system
Definition vr_info.h:176
defines the class vr::vr_driver class and gives access to the driver registry with the functions vr::...