Monado OpenXR Runtime
m_api.h
Go to the documentation of this file.
1// Copyright 2019-2021, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief C interface to math library.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 * @author Moshi Turner <moshiturner@protonmail.com>
8 * @author Nis Madsen <nima_zero_one@protonmail.com>
9 *
10 * @see xrt_vec3
11 * @see xrt_quat
12 * @see xrt_pose
13 * @see xrt_space_relation
14 * @ingroup aux_math
15 */
16
17#pragma once
18
19#include "xrt/xrt_defines.h"
20
21#include "math/m_mathinclude.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27
28/*!
29 * @defgroup aux_math Math
30 * @ingroup aux
31 *
32 * @brief C interface to some transform-related math functions.
33 */
34
35/*!
36 * @dir auxiliary/math
37 * @ingroup aux
38 *
39 * @brief C interface to some transform-related math functions.
40 */
41
42/*
43 *
44 * Defines.
45 *
46 */
47
48/*!
49 * Standard gravity acceleration constant.
50 *
51 * @ingroup aux_math
52 */
53#define MATH_GRAVITY_M_S2 (9.8066)
54
55/*!
56 * Minimum of A and B.
57 *
58 * @ingroup aux_math
59 */
60#ifndef MIN // Avoid clash with OpenCV def
61#define MIN(A, B) ((A) < (B) ? (A) : (B))
62#endif
63
64/*!
65 * Maximum of A and B.
66 *
67 * @ingroup aux_math
68 */
69#ifndef MAX // Avoid clash with OpenCV def
70#define MAX(A, B) ((A) > (B) ? (A) : (B))
71#endif
72
73/*!
74 * X clamped to the range [A, B].
75 *
76 * @ingroup aux_math
77 */
78#define CLAMP(X, A, B) (MIN(MAX((X), (A)), (B)))
79
80/*!
81 * Degrees to radians conversion.
82 *
83 * @ingroup aux_math
84 */
85// clang-format off
86// @todo: Remove the clang-format off/on when we move to a newer clang-format in CI.
87#define DEG_TO_RAD(DEG) ((DEG) * M_PI / 180.)
88// clang-format on
89
90/*!
91 * Radians to degrees conversion.
92 *
93 * @ingroup aux_math
94 */
95// clang-format off
96#define RAD_TO_DEG(RAD) ((RAD) * 180.0 / M_PI)
97// clang-format on
98
99
100/*
101 *
102 * Hash functions.
103 *
104 */
105
106/*!
107 * Generate a hash value from the given string, trailing zero not included.
108 *
109 * Hashing function used is not specified so no guarantee of staying the same
110 * between different versions of the software, or even when the same version
111 * is compiled on different platforms/libc++ as it might use std::hash.
112 *
113 * @ingroup aux_math
114 */
115size_t
116math_hash_string(const char *str_c, size_t length);
117
118
119/*
120 *
121 * Vector functions
122 *
123 */
124
125/*!
126 * Check if this vec3 is valid for math operations.
127 *
128 * @relates xrt_vec3
129 * @ingroup aux_math
130 */
131bool
132math_vec3_validate(const struct xrt_vec3 *vec3);
133
134/*!
135 * Accumulate a vector by adding in-place.
136 *
137 * Logically, *inAndOut += *additional
138 * OK if the two arguments are the same addresses.
139 *
140 * @relates xrt_vec3
141 * @ingroup aux_math
142 */
143void
144math_vec3_accum(const struct xrt_vec3 *additional, struct xrt_vec3 *inAndOut);
145
146/*!
147 * Subtract from a vector in-place.
148 *
149 * Logically, *inAndOut -= *subtrahend
150 * OK if the two arguments are the same addresses.
151 *
152 * @relates xrt_vec3
153 * @ingroup aux_math
154 */
155void
156math_vec3_subtract(const struct xrt_vec3 *subtrahend, struct xrt_vec3 *inAndOut);
157
158/*!
159 * Multiply a vector in-place.
160 *
161 * Logically, *inAndOut *= scalar
162 *
163 * @relates xrt_vec3
164 * @ingroup aux_math
165 */
166void
167math_vec3_scalar_mul(float scalar, struct xrt_vec3 *inAndOut);
168
169/*!
170 * Cross product of a vector.
171 *
172 * @relates xrt_vec3
173 * @ingroup aux_math
174 */
175void
176math_vec3_cross(const struct xrt_vec3 *l, const struct xrt_vec3 *r, struct xrt_vec3 *result);
177
178/*!
179 * Get translation vector from isometry matrix (col-major).
180 *
181 * @relates xrt_vec3
182 * @ingroup aux_math
183 */
184void
185math_vec3_translation_from_isometry(const struct xrt_matrix_4x4 *isometry, struct xrt_vec3 *result);
186
187/*!
188 * Normalize a vec3 in place.
189 *
190 * @relates xrt_vec3
191 * @ingroup aux_math
192 */
193void
194math_vec3_normalize(struct xrt_vec3 *in);
195
196
197/*
198 *
199 * 64 bit vector functions.
200 *
201 */
202
203/*!
204 * Cross product of a vec3_f64.
205 *
206 * @relates xrt_vec3_f64
207 * @ingroup aux_math
208 */
209void
210math_vec3_f64_cross(const struct xrt_vec3_f64 *l, const struct xrt_vec3_f64 *r, struct xrt_vec3_f64 *result);
211
212/*!
213 * Normalize a vec3_f64 in place.
214 *
215 * @relates xrt_vec3_f64
216 * @ingroup aux_math
217 */
218void
220
221
222/*
223 *
224 * Quat functions.
225 *
226 */
227
228/*!
229 * Create a rotation from an angle in radians and a unit vector.
230 *
231 * @relates xrt_quat
232 * @see xrt_vec3
233 * @ingroup aux_math
234 */
235void
236math_quat_from_angle_vector(float angle_rads, const struct xrt_vec3 *vector, struct xrt_quat *result);
237
238/*!
239 * Create a rotation from euler angles to a quaternion
240 * @relates xrt_quat
241 * @ingroup aux_math
242 */
243void
244math_quat_from_euler_angles(const struct xrt_vec3 *angles, struct xrt_quat *result);
245
246/*!
247 * Create a rotation from a quaternion to euler angles
248 * @relates xrt_quat
249 * @ingroup aux_math
250 */
251void
252math_quat_to_euler_angles(const struct xrt_quat *quat, struct xrt_vec3 *euler_angles);
253
254/*!
255 * Create a rotation from a 3x3 rotation (row major) matrix.
256 *
257 * @relates xrt_quat
258 * @see xrt_matrix_3x3
259 * @ingroup aux_math
260 */
261void
262math_quat_from_matrix_3x3(const struct xrt_matrix_3x3 *mat, struct xrt_quat *result);
263
264/*!
265 * Create a rotation from two vectors plus x and z, by creating a rotation
266 * matrix by crossing z and x to get the y axis.
267 *
268 * Input vectors should be normalized.
269 *
270 * @relates xrt_quat
271 * @see xrt_vec3
272 * @ingroup aux_math
273 */
274void
275math_quat_from_plus_x_z(const struct xrt_vec3 *plus_x, const struct xrt_vec3 *plus_z, struct xrt_quat *result);
276
277/*!
278 * Create a rotation from two vectors vec_a and vec_b that would
279 * rotate vec_a into vec_b
280 *
281 * @relates xrt_quat
282 * @see xrt_vec3
283 * @ingroup aux_math
284 */
285void
286math_quat_from_vec_a_to_vec_b(const struct xrt_vec3 *vec_a, const struct xrt_vec3 *vec_b, struct xrt_quat *result);
287
288/*!
289 * Check if this quat can be used in transformation operations.
290 *
291 * @relates xrt_quat
292 * @ingroup aux_math
293 */
294bool
295math_quat_validate(const struct xrt_quat *quat);
296
297/*!
298 * Check if this quat is (approximately) identity.
299 *
300 * @relates xrt_quat
301 * @ingroup aux_math
302 */
303bool
304math_quat_is_identity(const struct xrt_quat *quat, float epsilon);
305
306/*!
307 * Check if this quat is within 1% of unit length.
308 *
309 * @relates xrt_quat
310 * @ingroup aux_math
311 */
312bool
314
315/*!
316 * Invert a quaternion.
317 *
318 * @relates xrt_quat
319 * @ingroup aux_math
320 */
321void
322math_quat_invert(const struct xrt_quat *quat, struct xrt_quat *out_quat);
323
324/*!
325 * The euclidean norm or length of a quaternion. Same as if it were a vec4.
326 *
327 * @relates xrt_quat
328 * @ingroup aux_math
329 */
330float
331math_quat_len(const struct xrt_quat *quat);
332
333/*!
334 * The dot product of 2 quaternions. It has a analogous interpretation
335 * as for vec3. For unit quaternions, it provides cos(theta) of the
336 * angle between the 2 quaternion rotations.
337 *
338 * @relates xrt_quat
339 * @ingroup aux_math
340 */
341static inline float
342math_quat_dot(const struct xrt_quat *l, const struct xrt_quat *r)
343{
344 return l->x * r->x + l->y * r->y + l->z * r->z + l->w * r->w;
345}
346
347/*!
348 * Normalize a quaternion.
349 *
350 * @relates xrt_quat
351 * @ingroup aux_math
352 */
353void
354math_quat_normalize(struct xrt_quat *inout);
355
356/*!
357 * Normalizes a quaternion if it has accumulated float precision errors.
358 * Returns true if the quaternion was already normalized or was normalized after
359 * being found within a small float precision tolerance.
360 * Returns false if the quaternion was not at all normalized.
361 *
362 * @relates xrt_quat
363 * @ingroup aux_math
364 */
365bool
367
368/*!
369 * Rotate a vector.
370 *
371 * @relates xrt_quat
372 * @see xrt_vec3
373 * @ingroup aux_math
374 */
375void
376math_quat_rotate_vec3(const struct xrt_quat *left, const struct xrt_vec3 *right, struct xrt_vec3 *result);
377
378/*!
379 * Rotate a quaternion (compose rotations).
380 *
381 * @relates xrt_quat
382 * @ingroup aux_math
383 */
384void
385math_quat_rotate(const struct xrt_quat *left, const struct xrt_quat *right, struct xrt_quat *result);
386
387/*!
388 * Inverse of @ref math_quat_rotate. Removes @p left rotation from @p right.
389 *
390 * @relates xrt_quat
391 * @ingroup aux_math
392 */
393void
394math_quat_unrotate(const struct xrt_quat *left, const struct xrt_quat *right, struct xrt_quat *result);
395
396/*!
397 * Integrate a local angular velocity vector (exponential map) and apply to a
398 * quaternion.
399 *
400 * ang_vel and dt should share the same units of time, and the ang_vel
401 * vector should be in radians per unit of time.
402 *
403 * @relates xrt_quat
404 * @see xrt_vec3
405 * @ingroup aux_math
406 */
407void
408math_quat_integrate_velocity(const struct xrt_quat *quat,
409 const struct xrt_vec3 *ang_vel,
410 float dt,
411 struct xrt_quat *result);
412
413/*!
414 * Compute a global angular velocity vector (exponential map format) by taking
415 * the finite difference of two quaternions.
416 *
417 * quat1 is the orientation dt time after the orientation was quat0
418 *
419 * out_ang_vel and dt share the same units of time, and out_ang_vel is be in
420 * radians per unit of time.
421 *
422 * @relates xrt_quat
423 * @see xrt_vec3
424 * @ingroup aux_math
425 */
426void
427math_quat_finite_difference(const struct xrt_quat *quat0,
428 const struct xrt_quat *quat1,
429 float dt,
430 struct xrt_vec3 *out_ang_vel);
431
432/*!
433 * Takes a rotation vector equal to half of a Rodrigues rotation vector and returns its corresponding unit quaternion.
434 * Useful for head tracking and pose-prediction.
435 *
436 * @relates xrt_quat
437 * @see xrt_vec3
438 * @ingroup aux_math
439 */
440void
441math_quat_exp(const struct xrt_vec3 *axis_angle, struct xrt_quat *out_quat);
442
443
444/*!
445 * Takes a unit quaternion and returns a rotation vector equal to half of its corresponding Rodrigues rotation vector.
446 * Useful for head tracking and pose-prediction.
447 *
448 * @relates xrt_quat
449 * @see xrt_vec3
450 * @ingroup aux_math
451 */
452void
453math_quat_ln(const struct xrt_quat *quat, struct xrt_vec3 *out_axis_angle);
454
455/*!
456 * Used to rotate a derivative like a angular velocity.
457 *
458 * @relates xrt_quat
459 * @see xrt_vec3
460 * @ingroup aux_math
461 */
462void
463math_quat_rotate_derivative(const struct xrt_quat *quat, const struct xrt_vec3 *deriv, struct xrt_vec3 *result);
464
465
466/*!
467 * Slerp (spherical linear interpolation) between two quaternions
468 *
469 * @relates xrt_quat
470 * @ingroup aux_math
471 */
472void
473math_quat_slerp(const struct xrt_quat *left, const struct xrt_quat *right, float t, struct xrt_quat *result);
474
475
476/*!
477 * Converts a 2D vector to a quaternion
478 *
479 * @relates xrt_quat
480 * @ingroup aux_math
481 */
482void
483math_quat_from_swing(const struct xrt_vec2 *swing, struct xrt_quat *result);
484
485
486/*!
487 * Converts a 2D vector and a float to a quaternion
488 *
489 * @relates xrt_quat
490 * @ingroup aux_math
491 */
492void
493math_quat_from_swing_twist(const struct xrt_vec2 *swing, const float twist, struct xrt_quat *result);
494
495/*!
496 * Converts a quaternion to XY-swing and Z-twist
497 *
498 * @relates xrt_quat
499 * @ingroup aux_math
500 */
501void
502math_quat_to_swing_twist(const struct xrt_quat *in, struct xrt_vec2 *out_swing, float *out_twist);
503
504/*!
505 * Decompose a quaternion to swing and twist component rotations around a target
506 * axis. The swing is always orthogonal to the target axis, and twist rotation is always
507 * around the axis.
508 *
509 * swing * twist gives back the original quat
510 * (e.g. math_quat_rotate(&swing, &twist, &orig_q))
511 *
512 * See https://arxiv.org/pdf/1506.05481.pdf
513 *
514 * @relates xrt_quat
515 * @ingroup aux_math
516 */
517void
519 const struct xrt_vec3 *twist_axis,
520 struct xrt_quat *swing,
521 struct xrt_quat *twist);
522
523/*
524 *
525 * Matrix functions
526 *
527 */
528
529/*!
530 * Initialize a 3x3 matrix to the identity matrix
531 *
532 * @see xrt_matrix_3x3
533 * @ingroup aux_math
534 */
535void
537
538/*!
539 * Initialize a 3x3 matrix from a quaternion
540 *
541 * @see xrt_matrix_3x3
542 * @ingroup aux_math
543 */
544void
545math_matrix_3x3_from_quat(const struct xrt_quat *q, struct xrt_matrix_3x3 *result_out);
546
547/*!
548 * Initialize a double 3x3 matrix to the identity matrix
549 *
550 * @see xrt_matrix_3x3
551 * @ingroup aux_math
552 */
553void
555
556/*!
557 * Transform a vec3 by a 3x3 matrix
558 *
559 * @see xrt_matrix_3x3
560 * @ingroup aux_math
561 */
562void
564 const struct xrt_vec3 *right,
565 struct xrt_vec3 *result_out);
566
567/*!
568 * Transform a vec3 by a 4x4 matrix, extending the vector with w = 1.0
569 *
570 * @see xrt_matrix_4x4
571 * @ingroup aux_math
572 */
573void
575 const struct xrt_vec3 *right,
576 struct xrt_vec3 *result_out);
577
578/*!
579 * Transform a double vec3 by a 3x3 double matrix
580 *
581 * @see xrt_matrix_3x3
582 * @ingroup aux_math
583 */
584void
586 const struct xrt_vec3_f64 *right,
587 struct xrt_vec3_f64 *result_out);
588
589/*!
590 * Multiply Matrix3x3.
591 *
592 * @relates xrt_matrix_3x3
593 * @ingroup aux_math
594 */
595void
597 const struct xrt_matrix_3x3 *right,
598 struct xrt_matrix_3x3 *result_out);
599
600/*!
601 * Invert Matrix3x3
602 *
603 * @relates xrt_matrix_3x3
604 * @ingroup aux_math
605 */
606void
607math_matrix_3x3_inverse(const struct xrt_matrix_3x3 *in, struct xrt_matrix_3x3 *result);
608
609/*!
610 * Transpose Matrix3x3
611 *
612 * @relates xrt_matrix_3x3
613 * @ingroup aux_math
614 */
615void
616math_matrix_3x3_transpose(const struct xrt_matrix_3x3 *in, struct xrt_matrix_3x3 *result);
617
618/*!
619 * Create a rotation from two vectors plus x and z, by
620 * creating a rotation matrix by crossing z and x to
621 * get the y axis.
622 *
623 * Input vectors should be normalized.
624 *
625 * @relates xrt_matrix_3x3
626 * @ingroup aux_math
627 */
628void
630 const struct xrt_vec3_f64 *plus_z,
631 struct xrt_matrix_3x3_f64 *result);
632
633/*!
634 * Get the rotation matrix from an isomertry matrix (col-major).
635 *
636 * @relates xrt_matrix_4x4
637 * @ingroup aux_math
638 */
639void
640math_matrix_3x3_rotation_from_isometry(const struct xrt_matrix_4x4 *isometry, struct xrt_matrix_3x3 *result);
641
642/*!
643 * Initialize Matrix4x4 with identity.
644 *
645 * @relates xrt_matrix_4x4
646 * @ingroup aux_math
647 */
648void
650
651/*!
652 * Multiply Matrix4x4.
653 *
654 * @relates xrt_matrix_4x4
655 * @ingroup aux_math
656 */
657void
659 const struct xrt_matrix_4x4 *right,
660 struct xrt_matrix_4x4 *result);
661
662/*!
663 * Invert Matrix4x4.
664 *
665 * @relates xrt_matrix_4x4
666 * @ingroup aux_math
667 */
668void
669math_matrix_4x4_inverse(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result);
670
671/*!
672 * Invert a homogeneous isometry 4x4 (col-major) matrix in SE(3).
673 *
674 * @relates xrt_matrix_4x4
675 * @ingroup aux_math
676 */
677void
678math_matrix_4x4_isometry_inverse(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result);
679
680/*!
681 * Transpose Matrix4x4
682 *
683 * @relates xrt_matrix_4x4
684 * @ingroup aux_math
685 */
686void
687math_matrix_4x4_transpose(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result);
688
689/*!
690 * Compute view matrix from xrt_pose.
691 *
692 * @relates xrt_matrix_4x4
693 * @ingroup aux_math
694 */
695void
696math_matrix_4x4_view_from_pose(const struct xrt_pose *pose, struct xrt_matrix_4x4 *result);
697
698/*!
699 * Get an isometry matrix —in SE(3)— from a rotation matrix —SO(3)— and a
700 * translation vector. All col-major matrices.
701 *
702 * @relates xrt_matrix_4x4
703 * @ingroup aux_math
704 */
705void
707 const struct xrt_vec3 *translation,
708 struct xrt_matrix_4x4 *result);
709
710/*!
711 * Get a col-major isometry matrix —in SE(3)— from a pose.
712 *
713 * @relates xrt_matrix_4x4
714 * @ingroup aux_math
715 */
716void
717math_matrix_4x4_isometry_from_pose(const struct xrt_pose *pose, struct xrt_matrix_4x4 *result);
718
719/*!
720 * Compute quad layer model matrix from xrt_pose and xrt_vec2 size.
721 *
722 * @relates xrt_matrix_4x4
723 * @ingroup aux_math
724 */
725void
726math_matrix_4x4_model(const struct xrt_pose *pose, const struct xrt_vec3 *size, struct xrt_matrix_4x4 *result);
727
728/*!
729 * Compute inverse view projection matrix,
730 * using only the starting 3x3 block of the view.
731 *
732 * @relates xrt_matrix_4x4
733 * @ingroup aux_math
734 */
735void
737 const struct xrt_matrix_4x4 *projection,
738 struct xrt_matrix_4x4 *result);
739
740/*!
741 * Compute a projection matrix with settings for Vulkan, it will also have it's
742 * far plane at infinite and the NDC depth will be reversed.
743 *
744 * @relates xrt_matrix_4x4
745 * @ingroup aux_math
746 */
747void
749 float near_plane,
750 struct xrt_matrix_4x4 *result);
751
752
753/*
754 *
755 * Pose functions.
756 *
757 */
758
759
760/*!
761 * Somewhat laboriously make an xrt_pose identity.
762 *
763 * @relates xrt_pose
764 * @ingroup aux_math
765 */
766void
767math_pose_identity(struct xrt_pose *pose);
768
769/*!
770 * Check if this pose can be used in transformation operations.
771 *
772 * @relates xrt_pose
773 * @ingroup aux_math
774 */
775bool
776math_pose_validate(const struct xrt_pose *pose);
777
778/*!
779 * Invert pose.
780 *
781 * OK if input and output are the same addresses.
782 *
783 * @relates xrt_pose
784 * @ingroup aux_math
785 */
786void
787math_pose_invert(const struct xrt_pose *pose, struct xrt_pose *outPose);
788
789/*!
790 * Converts a (col-major) isometry into a pose.
791 *
792 * @relates xrt_pose
793 * @ingroup aux_math
794 */
795void
796math_pose_from_isometry(const struct xrt_matrix_4x4 *transform, struct xrt_pose *result);
797
798/*!
799 * Interpolated pose between poses `a` and `b` by lerping position and slerping
800 * orientation by t.
801 *
802 * @relates xrt_pose
803 * @ingroup aux_math
804 */
805void
806math_pose_interpolate(const struct xrt_pose *a, const struct xrt_pose *b, float t, struct xrt_pose *outPose);
807
808/*!
809 * Apply a rigid-body transformation to a pose.
810 *
811 * OK if input and output are the same addresses.
812 *
813 * @relates xrt_pose
814 * @ingroup aux_math
815 */
816void
817math_pose_transform(const struct xrt_pose *transform, const struct xrt_pose *pose, struct xrt_pose *outPose);
818
819/*!
820 * Apply a rigid-body transformation to a point.
821 *
822 * The input point and output may be the same pointer.
823 *
824 * @relates xrt_pose
825 * @see xrt_vec3
826 * @ingroup aux_math
827 */
828void
829math_pose_transform_point(const struct xrt_pose *transform, const struct xrt_vec3 *point, struct xrt_vec3 *out_point);
830
831
832/*
833 *
834 * Inline functions.
835 *
836 */
837
838/*!
839 * Map a number from one range to another range.
840 * Exactly the same as Arduino's map().
841 */
842static inline double
843math_map_ranges(double value, double from_low, double from_high, double to_low, double to_high)
844{
845 return (value - from_low) * (to_high - to_low) / (from_high - from_low) + to_low;
846}
847
848static inline double
849math_lerp(double from, double to, double amount)
850{
851 return (from * (1.0 - amount)) + (to * (amount));
852}
853
854/*
855 *
856 * Optics functions.
857 *
858 */
859
860/*!
861 * Perform the computations from
862 * "Computing Half-Fields-Of-View from Simpler Display Models",
863 * to get half-FOVs from things we can retrieve from other APIs.
864 * The origin is in the lower-left corner of the display, so w_1 is the width to
865 * the left of CoP, and h_1 is the height below CoP.
866 *
867 * If vertfov_total is set to 0, it will be computed from h_total.
868 *
869 * Distances are in arbitrary but consistent units. Angles are in radians.
870 *
871 *
872 * In the diagram below, treating it like a FOV for horizontal,
873 * the top angle is horizfov_total, the length of the bottom
874 * is w_total, and the distance between the vertical line and the left corner is
875 * w_1. Vertical is similar - h_1 is above the center line.
876 * The triangle need not be symmetrical, despite how the diagram looks.
877 *
878 * ```
879 * horizfov_total
880 * *
881 * angle_left (neg) -> / | \ <- angle_right
882 * / | \
883 * / | \
884 * / | \
885 * -------------
886 * [ w_1 ]
887 * [ --- w --- ]
888 *
889 * ------- --- |\
890 * | \
891 * h_1 | \ angle_up
892 * h_total ___ |-------* vertfov_total
893 * | / angle_down (neg)
894 * | /
895 * | /
896 * ------- |/
897 * ```
898 *
899 * @return true if successful.
900 * @ingroup aux_math
901 */
902bool
903math_compute_fovs(double w_total,
904 double w_1,
905 double horizfov_total,
906 double h_total,
907 double h_1,
908 double vertfov_total,
909 struct xrt_fov *fov);
910
911#ifdef __cplusplus
912}
913#endif
void math_quat_rotate_derivative(const struct xrt_quat *quat, const struct xrt_vec3 *deriv, struct xrt_vec3 *result)
Used to rotate a derivative like a angular velocity.
Definition: m_base.cpp:415
void math_vec3_normalize(struct xrt_vec3 *in)
Normalize a vec3 in place.
Definition: m_base.cpp:173
void math_matrix_4x4_projection_vulkan_infinite_reverse(const struct xrt_fov *fov, float near_plane, struct xrt_matrix_4x4 *result)
Compute a projection matrix with settings for Vulkan, it will also have it's far plane at infinite an...
Definition: m_matrix_projection.cpp:76
void math_matrix_4x4_isometry_from_pose(const struct xrt_pose *pose, struct xrt_matrix_4x4 *result)
Get a col-major isometry matrix —in SE(3)— from a pose.
Definition: m_base.cpp:818
void math_vec3_f64_normalize(struct xrt_vec3_f64 *in)
Normalize a vec3_f64 in place.
Definition: m_base.cpp:199
void math_quat_from_swing(const struct xrt_vec2 *swing, struct xrt_quat *result)
Converts a 2D vector to a quaternion.
Definition: m_base.cpp:445
void math_quat_from_vec_a_to_vec_b(const struct xrt_vec3 *vec_a, const struct xrt_vec3 *vec_b, struct xrt_quat *result)
Create a rotation from two vectors vec_a and vec_b that would rotate vec_a into vec_b.
Definition: m_base.cpp:272
void math_quat_decompose_swing_twist(const struct xrt_quat *in, const struct xrt_vec3 *twist_axis, struct xrt_quat *swing, struct xrt_quat *twist)
Decompose a quaternion to swing and twist component rotations around a target axis.
Definition: m_base.cpp:575
void math_matrix_4x4_isometry_inverse(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result)
Invert a homogeneous isometry 4x4 (col-major) matrix in SE(3).
Definition: m_base.cpp:788
void math_pose_transform_point(const struct xrt_pose *transform, const struct xrt_vec3 *point, struct xrt_vec3 *out_point)
Apply a rigid-body transformation to a point.
Definition: m_base.cpp:997
void math_matrix_3x3_multiply(const struct xrt_matrix_3x3 *left, const struct xrt_matrix_3x3 *right, struct xrt_matrix_3x3 *result_out)
Multiply Matrix3x3.
Definition: m_base.cpp:723
void math_vec3_subtract(const struct xrt_vec3 *subtrahend, struct xrt_vec3 *inAndOut)
Subtract from a vector in-place.
Definition: m_base.cpp:150
void math_matrix_3x3_f64_identity(struct xrt_matrix_3x3_f64 *mat)
Initialize a double 3x3 matrix to the identity matrix.
Definition: m_base.cpp:647
void math_matrix_3x3_transpose(const struct xrt_matrix_3x3 *in, struct xrt_matrix_3x3 *result)
Transpose Matrix3x3.
Definition: m_base.cpp:753
void math_vec3_f64_cross(const struct xrt_vec3_f64 *l, const struct xrt_vec3_f64 *r, struct xrt_vec3_f64 *result)
Cross product of a vec3_f64.
Definition: m_base.cpp:193
void math_quat_from_plus_x_z(const struct xrt_vec3 *plus_x, const struct xrt_vec3 *plus_z, struct xrt_quat *result)
Create a rotation from two vectors plus x and z, by creating a rotation matrix by crossing z and x to...
Definition: m_base.cpp:251
bool math_vec3_validate(const struct xrt_vec3 *vec3)
Check if this vec3 is valid for math operations.
Definition: m_base.cpp:133
void math_pose_transform(const struct xrt_pose *transform, const struct xrt_pose *pose, struct xrt_pose *outPose)
Apply a rigid-body transformation to a pose.
Definition: m_base.cpp:986
bool math_compute_fovs(double w_total, double w_1, double horizfov_total, double h_total, double h_1, double vertfov_total, struct xrt_fov *fov)
Perform the computations from "Computing Half-Fields-Of-View from Simpler Display Models",...
Definition: m_optics.c:118
void math_matrix_4x4_identity(struct xrt_matrix_4x4 *result)
Initialize Matrix4x4 with identity.
Definition: m_base.cpp:760
void math_quat_to_swing_twist(const struct xrt_quat *in, struct xrt_vec2 *out_swing, float *out_twist)
Converts a quaternion to XY-swing and Z-twist.
Definition: m_base.cpp:553
void math_quat_to_euler_angles(const struct xrt_quat *quat, struct xrt_vec3 *euler_angles)
Create a rotation from a quaternion to euler angles.
Definition: m_base.cpp:226
void math_vec3_cross(const struct xrt_vec3 *l, const struct xrt_vec3 *r, struct xrt_vec3 *result)
Cross product of a vector.
Definition: m_base.cpp:167
void math_matrix_3x3_identity(struct xrt_matrix_3x3 *mat)
Initialize a 3x3 matrix to the identity matrix.
Definition: m_base.cpp:621
void math_matrix_4x4_inverse_view_projection(const struct xrt_matrix_4x4 *view, const struct xrt_matrix_4x4 *projection, struct xrt_matrix_4x4 *result)
Compute inverse view projection matrix, using only the starting 3x3 block of the view.
Definition: m_base.cpp:839
void math_matrix_4x4_transpose(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result)
Transpose Matrix4x4.
Definition: m_base.cpp:781
void math_quat_rotate_vec3(const struct xrt_quat *left, const struct xrt_vec3 *right, struct xrt_vec3 *result)
Rotate a vector.
Definition: m_base.cpp:400
void math_matrix_4x4_view_from_pose(const struct xrt_pose *pose, struct xrt_matrix_4x4 *result)
Compute view matrix from xrt_pose.
Definition: m_base.cpp:795
void math_vec3_scalar_mul(float scalar, struct xrt_vec3 *inAndOut)
Multiply a vector in-place.
Definition: m_base.cpp:159
void math_quat_finite_difference(const struct xrt_quat *quat0, const struct xrt_quat *quat1, float dt, struct xrt_vec3 *out_ang_vel)
Compute a global angular velocity vector (exponential map format) by taking the finite difference of ...
Definition: m_quatexpmap.cpp:158
void math_quat_exp(const struct xrt_vec3 *axis_angle, struct xrt_quat *out_quat)
Takes a rotation vector equal to half of a Rodrigues rotation vector and returns its corresponding un...
Definition: m_quatexpmap.cpp:174
size_t math_hash_string(const char *str_c, size_t length)
Generate a hash value from the given string, trailing zero not included.
Definition: m_hash.cpp:15
void math_matrix_3x3_inverse(const struct xrt_matrix_3x3 *in, struct xrt_matrix_3x3 *result)
Invert Matrix3x3.
Definition: m_base.cpp:746
bool math_quat_validate_within_1_percent(const struct xrt_quat *quat)
Check if this quat is within 1% of unit length.
Definition: m_base.cpp:323
void math_matrix_3x3_f64_transform_vec3_f64(const struct xrt_matrix_3x3_f64 *left, const struct xrt_vec3_f64 *right, struct xrt_vec3_f64 *result_out)
Transform a double vec3 by a 3x3 double matrix.
Definition: m_base.cpp:653
void math_quat_from_swing_twist(const struct xrt_vec2 *swing, const float twist, struct xrt_quat *result)
Converts a 2D vector and a float to a quaternion.
Definition: m_base.cpp:473
void math_matrix_4x4_transform_vec3(const struct xrt_matrix_4x4 *left, const struct xrt_vec3 *right, struct xrt_vec3 *result_out)
Transform a vec3 by a 4x4 matrix, extending the vector with w = 1.0.
Definition: m_base.cpp:705
void math_pose_from_isometry(const struct xrt_matrix_4x4 *transform, struct xrt_pose *result)
Converts a (col-major) isometry into a pose.
Definition: m_base.cpp:938
static float math_quat_dot(const struct xrt_quat *l, const struct xrt_quat *r)
The dot product of 2 quaternions.
Definition: m_api.h:342
void math_quat_from_euler_angles(const struct xrt_vec3 *angles, struct xrt_quat *result)
Create a rotation from euler angles to a quaternion.
Definition: m_base.cpp:218
float math_quat_len(const struct xrt_quat *quat)
The euclidean norm or length of a quaternion.
Definition: m_base.cpp:335
void math_pose_invert(const struct xrt_pose *pose, struct xrt_pose *outPose)
Invert pose.
Definition: m_base.cpp:929
void math_quat_ln(const struct xrt_quat *quat, struct xrt_vec3 *out_axis_angle)
Takes a unit quaternion and returns a rotation vector equal to half of its corresponding Rodrigues ro...
Definition: m_quatexpmap.cpp:180
void math_quat_normalize(struct xrt_quat *inout)
Normalize a quaternion.
Definition: m_base.cpp:341
bool math_quat_is_identity(const struct xrt_quat *quat, float epsilon)
Check if this quat is (approximately) identity.
Definition: m_base.cpp:317
void math_quat_rotate(const struct xrt_quat *left, const struct xrt_quat *right, struct xrt_quat *result)
Rotate a quaternion (compose rotations).
Definition: m_base.cpp:370
void math_quat_from_angle_vector(float angle_rads, const struct xrt_vec3 *vector, struct xrt_quat *result)
Create a rotation from an angle in radians and a unit vector.
Definition: m_base.cpp:212
bool math_pose_validate(const struct xrt_pose *pose)
Check if this pose can be used in transformation operations.
Definition: m_base.cpp:921
void math_matrix_4x4_inverse(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result)
Invert Matrix4x4.
Definition: m_base.cpp:774
void math_pose_interpolate(const struct xrt_pose *a, const struct xrt_pose *b, float t, struct xrt_pose *outPose)
Interpolated pose between poses a and b by lerping position and slerping orientation by t.
Definition: m_base.cpp:946
void math_matrix_3x3_transform_vec3(const struct xrt_matrix_3x3 *left, const struct xrt_vec3 *right, struct xrt_vec3 *result_out)
Transform a vec3 by a 3x3 matrix.
Definition: m_base.cpp:692
void math_matrix_4x4_isometry_from_rt(const struct xrt_matrix_3x3 *rotation, const struct xrt_vec3 *translation, struct xrt_matrix_4x4 *result)
Get an isometry matrix —in SE(3)— from a rotation matrix —SO(3)— and a translation vector.
Definition: m_base.cpp:807
bool math_quat_validate(const struct xrt_quat *quat)
Check if this quat can be used in transformation operations.
Definition: m_base.cpp:310
void math_quat_integrate_velocity(const struct xrt_quat *quat, const struct xrt_vec3 *ang_vel, float dt, struct xrt_quat *result)
Integrate a local angular velocity vector (exponential map) and apply to a quaternion.
Definition: m_quatexpmap.cpp:141
void math_quat_from_matrix_3x3(const struct xrt_matrix_3x3 *mat, struct xrt_quat *result)
Create a rotation from a 3x3 rotation (row major) matrix.
Definition: m_base.cpp:241
void math_matrix_3x3_rotation_from_isometry(const struct xrt_matrix_4x4 *isometry, struct xrt_matrix_3x3 *result)
Get the rotation matrix from an isomertry matrix (col-major).
Definition: m_base.cpp:685
void math_matrix_4x4_multiply(const struct xrt_matrix_4x4 *left, const struct xrt_matrix_4x4 *right, struct xrt_matrix_4x4 *result)
Multiply Matrix4x4.
Definition: m_base.cpp:766
void math_quat_invert(const struct xrt_quat *quat, struct xrt_quat *out_quat)
Invert a quaternion.
Definition: m_base.cpp:329
void math_quat_unrotate(const struct xrt_quat *left, const struct xrt_quat *right, struct xrt_quat *result)
Inverse of math_quat_rotate.
Definition: m_base.cpp:385
bool math_quat_ensure_normalized(struct xrt_quat *inout)
Normalizes a quaternion if it has accumulated float precision errors.
Definition: m_base.cpp:348
void math_matrix_3x3_from_quat(const struct xrt_quat *q, struct xrt_matrix_3x3 *result_out)
Initialize a 3x3 matrix from a quaternion.
Definition: m_base.cpp:627
void math_quat_slerp(const struct xrt_quat *left, const struct xrt_quat *right, float t, struct xrt_quat *result)
Slerp (spherical linear interpolation) between two quaternions.
Definition: m_base.cpp:432
void math_matrix_3x3_f64_from_plus_x_z(const struct xrt_vec3_f64 *plus_x, const struct xrt_vec3_f64 *plus_z, struct xrt_matrix_3x3_f64 *result)
Create a rotation from two vectors plus x and z, by creating a rotation matrix by crossing z and x to...
Definition: m_base.cpp:666
void math_pose_identity(struct xrt_pose *pose)
Somewhat laboriously make an xrt_pose identity.
Definition: m_base.cpp:953
void math_vec3_translation_from_isometry(const struct xrt_matrix_4x4 *isometry, struct xrt_vec3 *result)
Get translation vector from isometry matrix (col-major).
Definition: m_base.cpp:179
void math_matrix_4x4_model(const struct xrt_pose *pose, const struct xrt_vec3 *size, struct xrt_matrix_4x4 *result)
Compute quad layer model matrix from xrt_pose and xrt_vec2 size.
Definition: m_base.cpp:825
void math_vec3_accum(const struct xrt_vec3 *additional, struct xrt_vec3 *inAndOut)
Accumulate a vector by adding in-place.
Definition: m_base.cpp:141
static double math_map_ranges(double value, double from_low, double from_high, double to_low, double to_high)
Map a number from one range to another range.
Definition: m_api.h:843
Wrapper header for <math.h> to ensure pi-related math constants are defined.
Describes a projection matrix fov.
Definition: xrt_defines.h:499
A tightly packed 3x3 matrix of doubles.
Definition: xrt_defines.h:556
A tightly packed 3x3 matrix of floats.
Definition: xrt_defines.h:546
A tightly packed 4x4 matrix of floats.
Definition: xrt_defines.h:573
A pose composed of a position and orientation.
Definition: xrt_defines.h:479
A quaternion with single floats.
Definition: xrt_defines.h:235
A 2 element vector with single floats.
Definition: xrt_defines.h:268
A 3 element vector with single doubles.
Definition: xrt_defines.h:301
A 3 element vector with single floats.
Definition: xrt_defines.h:289
Common defines and enums for XRT.