20#define likely(x) __builtin_expect((x), 1)
21#define unlikely(x) __builtin_expect((x), 0)
27namespace xrt::tracking::hand::mercury::lm {
32#define assert_quat_length_1(q) \
34 const T scale = q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]; \
35 if (abs(scale - T(1.0)) > 0.001) { \
36 std::cout << "Length bad! " << scale << std::endl; \
41#define assert_quat_length_1(q)
49CurlToQuaternion(
const T &theta, Quat<T> &result)
51 const T theta_squared = theta * theta;
54 if (likely(theta_squared > T(0.0))) {
55 const T half_theta = theta * T(0.5);
56 const T k = sin(half_theta) / theta;
57 result.w = cos(half_theta);
75SwingToQuaternion(
const Vec2<T> swing, Quat<T> &result)
78 const T &a0 = swing.x;
79 const T &a1 = swing.y;
80 const T theta_squared = a0 * a0 + a1 * a1;
83 if (likely(theta_squared > T(0.0))) {
84 const T theta = sqrt(theta_squared);
85 const T half_theta = theta * T(0.5);
86 const T k = sin(half_theta) / theta;
87 result.w = cos(half_theta);
110SwingTwistToQuaternion(
const Vec2<T> swing,
const T twist, Quat<T> &result)
116 T theta_squared_swing = swing_x * swing_x + swing_y * swing_y;
121 if (theta_squared_swing > T(0.0)) {
124 T theta = sqrt(theta_squared_swing);
126 T half_theta = theta * T(0.5);
129 T half_twist = twist * T(0.5);
131 T cos_half_theta = cos(half_theta);
132 T cos_half_twist = cos(half_twist);
134 T sin_half_twist = sin(half_twist);
136 T sin_half_theta_over_theta = sin(half_theta) / theta;
138 result.w = cos_half_theta * cos_half_twist;
140 T x_part_1 = (swing_x * cos_half_twist * sin_half_theta_over_theta);
141 T x_part_2 = (swing_y * sin_half_twist * sin_half_theta_over_theta);
143 result.x = x_part_1 + x_part_2;
145 T y_part_1 = (swing_y * cos_half_twist * sin_half_theta_over_theta);
146 T y_part_2 = (swing_x * sin_half_twist * sin_half_theta_over_theta);
148 result.y = y_part_1 - y_part_2;
150 result.z = cos_half_theta * sin_half_twist;
159 T half_twist = twist * T(0.5);
161 T cos_half_twist = cos(half_twist);
163 T sin_half_twist = sin(half_twist);
165 T sin_half_theta_over_theta = T(0.5);
168 result.w = cos_half_twist;
170 T x_part_1 = (swing_x * cos_half_twist * sin_half_theta_over_theta);
171 T x_part_2 = (swing_y * sin_half_twist * sin_half_theta_over_theta);
173 result.x = x_part_1 + x_part_2;
175 T y_part_1 = (swing_y * cos_half_twist * sin_half_theta_over_theta);
176 T y_part_2 = (swing_x * sin_half_twist * sin_half_theta_over_theta);
178 result.y = y_part_1 - y_part_2;
180 result.z = sin_half_twist;
Defines for Levenberg-Marquardt kinematic optimizer.
Autodiff-safe rotations for Levenberg-Marquardt kinematic optimizer.