Monado OpenXR Runtime
Loading...
Searching...
No Matches
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 * Convert a vec3 from the OpenCV coordinate system to the OpenXR coordinate system and back. OpenCV camera space
198 * coordinates has +Y down and +Z away from the user.
199 *
200 * The input and output may be the same pointer.
201 *
202 * @relates xrt_vec3
203 * @ingroup aux_math
204 */
205void
206math_vec3_convert_from_opencv(const struct xrt_vec3 *in, struct xrt_vec3 *out);
207
208
209/*
210 *
211 * 64 bit vector functions.
212 *
213 */
214
215/*!
216 * Cross product of a vec3_f64.
217 *
218 * @relates xrt_vec3_f64
219 * @ingroup aux_math
220 */
221void
222math_vec3_f64_cross(const struct xrt_vec3_f64 *l, const struct xrt_vec3_f64 *r, struct xrt_vec3_f64 *result);
223
224/*!
225 * Normalize a vec3_f64 in place.
226 *
227 * @relates xrt_vec3_f64
228 * @ingroup aux_math
229 */
230void
231math_vec3_f64_normalize(struct xrt_vec3_f64 *in);
232
233/*!
234 * Convert a vec3_f64 from the OpenCV coordinate system to the OpenXR coordinate system. OpenCV camera space coordinates
235 * has +Y down and +Z away from the user.
236 *
237 * The input and output may be the same pointer.
238 *
239 * @relates xrt_vec3
240 * @ingroup aux_math
241 */
242void
243math_vec3_f64_convert_from_opencv(const struct xrt_vec3_f64 *in, struct xrt_vec3_f64 *out);
244
245
246/*
247 *
248 * Quat functions.
249 *
250 */
251
252/*!
253 * Create a single quaternion from a double quaternion.
254 *
255 * @relates xrt_quat
256 * @see xrt_quat_f64
257 * @ingroup aux_math
258 */
259void
260math_quat_from_quat_f64(const struct xrt_quat_f64 *in, struct xrt_quat *out);
261
262/*!
263 * Create a rotation from an angle in radians and a unit vector.
264 *
265 * @relates xrt_quat
266 * @see xrt_vec3
267 * @ingroup aux_math
268 */
269void
270math_quat_from_angle_vector(float angle_rads, const struct xrt_vec3 *vector, struct xrt_quat *result);
271
272/*!
273 * Create a rotation from euler angles to a quaternion
274 * @relates xrt_quat
275 * @ingroup aux_math
276 */
277void
278math_quat_from_euler_angles(const struct xrt_vec3 *angles, struct xrt_quat *result);
279
280/*!
281 * Create a rotation from a quaternion to euler angles
282 * @relates xrt_quat
283 * @ingroup aux_math
284 */
285void
286math_quat_to_euler_angles(const struct xrt_quat *quat, struct xrt_vec3 *euler_angles);
287
288/*!
289 * Create a rotation from a 3x3 rotation (row major) matrix.
290 *
291 * @relates xrt_quat
292 * @see xrt_matrix_3x3
293 * @ingroup aux_math
294 */
295void
296math_quat_from_matrix_3x3(const struct xrt_matrix_3x3 *mat, struct xrt_quat *result);
297
298/*!
299 * Create a rotation from two vectors plus x and z, by creating a rotation
300 * matrix by crossing z and x to get the y axis.
301 *
302 * Input vectors should be normalized.
303 *
304 * @relates xrt_quat
305 * @see xrt_vec3
306 * @ingroup aux_math
307 */
308void
309math_quat_from_plus_x_z(const struct xrt_vec3 *plus_x, const struct xrt_vec3 *plus_z, struct xrt_quat *result);
310
311/*!
312 * Create a rotation from two vectors vec_a and vec_b that would
313 * rotate vec_a into vec_b
314 *
315 * @relates xrt_quat
316 * @see xrt_vec3
317 * @ingroup aux_math
318 */
319void
320math_quat_from_vec_a_to_vec_b(const struct xrt_vec3 *vec_a, const struct xrt_vec3 *vec_b, struct xrt_quat *result);
321
322/*!
323 * Check if this quat can be used in transformation operations.
324 *
325 * @relates xrt_quat
326 * @ingroup aux_math
327 */
328bool
329math_quat_validate(const struct xrt_quat *quat);
330
331/*!
332 * Check if this quat is (approximately) identity.
333 *
334 * @relates xrt_quat
335 * @ingroup aux_math
336 */
337bool
338math_quat_is_identity(const struct xrt_quat *quat, float epsilon);
339
340/*!
341 * Check if this quat is within 1% of unit length.
342 *
343 * @relates xrt_quat
344 * @ingroup aux_math
345 */
346bool
347math_quat_validate_within_1_percent(const struct xrt_quat *quat);
348
349/*!
350 * Invert a quaternion.
351 *
352 * @relates xrt_quat
353 * @ingroup aux_math
354 */
355void
356math_quat_invert(const struct xrt_quat *quat, struct xrt_quat *out_quat);
357
358/*!
359 * The euclidean norm or length of a quaternion. Same as if it were a vec4.
360 *
361 * @relates xrt_quat
362 * @ingroup aux_math
363 */
364float
365math_quat_len(const struct xrt_quat *quat);
366
367/*!
368 * The dot product of 2 quaternions. It has a analogous interpretation
369 * as for vec3. For unit quaternions, it provides cos(theta) of the
370 * angle between the 2 quaternion rotations.
371 *
372 * @relates xrt_quat
373 * @ingroup aux_math
374 */
375static inline float
376math_quat_dot(const struct xrt_quat *l, const struct xrt_quat *r)
377{
378 return l->x * r->x + l->y * r->y + l->z * r->z + l->w * r->w;
379}
380
381/*!
382 * Normalize a quaternion.
383 *
384 * @relates xrt_quat
385 * @ingroup aux_math
386 */
387void
388math_quat_normalize(struct xrt_quat *inout);
389
390/*!
391 * Normalizes a quaternion if it has accumulated float precision errors.
392 * Returns true if the quaternion was already normalized or was normalized after
393 * being found within a small float precision tolerance.
394 * Returns false if the quaternion was not at all normalized.
395 *
396 * @relates xrt_quat
397 * @ingroup aux_math
398 */
399bool
400math_quat_ensure_normalized(struct xrt_quat *inout);
401
402/*!
403 * Rotate a vector.
404 *
405 * @relates xrt_quat
406 * @see xrt_vec3
407 * @ingroup aux_math
408 */
409void
410math_quat_rotate_vec3(const struct xrt_quat *left, const struct xrt_vec3 *right, struct xrt_vec3 *result);
411
412/*!
413 * Rotate a quaternion (compose rotations).
414 *
415 * @relates xrt_quat
416 * @ingroup aux_math
417 */
418void
419math_quat_rotate(const struct xrt_quat *left, const struct xrt_quat *right, struct xrt_quat *result);
420
421/*!
422 * Inverse of @ref math_quat_rotate. Removes @p left rotation from @p right.
423 *
424 * @relates xrt_quat
425 * @ingroup aux_math
426 */
427void
428math_quat_unrotate(const struct xrt_quat *left, const struct xrt_quat *right, struct xrt_quat *result);
429
430/*!
431 * Integrate a local angular velocity vector (exponential map) and apply to a
432 * quaternion.
433 *
434 * ang_vel and dt should share the same units of time, and the ang_vel
435 * vector should be in radians per unit of time.
436 *
437 * @relates xrt_quat
438 * @see xrt_vec3
439 * @ingroup aux_math
440 */
441void
442math_quat_integrate_velocity(const struct xrt_quat *quat,
443 const struct xrt_vec3 *ang_vel,
444 float dt,
445 struct xrt_quat *result);
446
447/*!
448 * Compute a global angular velocity vector (exponential map format) by taking
449 * the finite difference of two quaternions.
450 *
451 * quat1 is the orientation dt time after the orientation was quat0
452 *
453 * out_ang_vel and dt share the same units of time, and out_ang_vel is be in
454 * radians per unit of time.
455 *
456 * @relates xrt_quat
457 * @see xrt_vec3
458 * @ingroup aux_math
459 */
460void
461math_quat_finite_difference(const struct xrt_quat *quat0,
462 const struct xrt_quat *quat1,
463 float dt,
464 struct xrt_vec3 *out_ang_vel);
465
466/*!
467 * Takes a Rodrigues rotation vector and returns its corresponding unit quaternion.
468 *
469 * Useful for head tracking and pose-prediction.
470 *
471 * @relates xrt_quat
472 * @see xrt_vec3
473 * @ingroup aux_math
474 */
475void
476math_quat_exp_so3(const struct xrt_vec3 *axis_angle, struct xrt_quat *out_quat);
477
478
479/*!
480 * Takes a unit quaternion and returns its corresponding Rodrigues rotation vector.
481 *
482 * Useful for head tracking and pose-prediction.
483 *
484 * @relates xrt_quat
485 * @see xrt_vec3
486 * @ingroup aux_math
487 */
488void
489math_quat_ln_so3(const struct xrt_quat *quat, struct xrt_vec3 *out_axis_angle);
490
491/*!
492 * Used to rotate a derivative like a angular velocity.
493 *
494 * @relates xrt_quat
495 * @see xrt_vec3
496 * @ingroup aux_math
497 */
498void
499math_quat_rotate_derivative(const struct xrt_quat *quat, const struct xrt_vec3 *deriv, struct xrt_vec3 *result);
500
501
502/*!
503 * Slerp (spherical linear interpolation) between two quaternions
504 *
505 * @relates xrt_quat
506 * @ingroup aux_math
507 */
508void
509math_quat_slerp(const struct xrt_quat *left, const struct xrt_quat *right, float t, struct xrt_quat *result);
510
511
512/*!
513 * Converts a 2D vector to a quaternion
514 *
515 * @relates xrt_quat
516 * @ingroup aux_math
517 */
518void
519math_quat_from_swing(const struct xrt_vec2 *swing, struct xrt_quat *result);
520
521
522/*!
523 * Converts a 2D vector and a float to a quaternion
524 *
525 * @relates xrt_quat
526 * @ingroup aux_math
527 */
528void
529math_quat_from_swing_twist(const struct xrt_vec2 *swing, const float twist, struct xrt_quat *result);
530
531/*!
532 * Converts a quaternion to XY-swing and Z-twist
533 *
534 * @relates xrt_quat
535 * @ingroup aux_math
536 */
537void
538math_quat_to_swing_twist(const struct xrt_quat *in, struct xrt_vec2 *out_swing, float *out_twist);
539
540/*!
541 * Decompose a quaternion to swing and twist component rotations around a target
542 * axis. The swing is always orthogonal to the target axis, and twist rotation is always
543 * around the axis.
544 *
545 * swing * twist gives back the original quat
546 * (e.g. math_quat_rotate(&swing, &twist, &orig_q))
547 *
548 * See https://arxiv.org/pdf/1506.05481.pdf
549 *
550 * @relates xrt_quat
551 * @ingroup aux_math
552 */
553void
554math_quat_decompose_swing_twist(const struct xrt_quat *in,
555 const struct xrt_vec3 *twist_axis,
556 struct xrt_quat *swing,
557 struct xrt_quat *twist);
558
559/*
560 *
561 * Matrix functions
562 *
563 */
564
565/*!
566 * Initialize a 3x3 matrix to the identity matrix
567 *
568 * @see xrt_matrix_3x3
569 * @ingroup aux_math
570 */
571void
573
574/*!
575 * Initialize a 3x3 matrix from a quaternion
576 *
577 * @see xrt_matrix_3x3
578 * @ingroup aux_math
579 */
580void
581math_matrix_3x3_from_quat(const struct xrt_quat *q, struct xrt_matrix_3x3 *result_out);
582
583/*!
584 * Initialize a double 3x3 matrix to the identity matrix
585 *
586 * @see xrt_matrix_3x3
587 * @ingroup aux_math
588 */
589void
591
592/*!
593 * Transform a vec3 by a 3x3 matrix
594 *
595 * @see xrt_matrix_3x3
596 * @ingroup aux_math
597 */
598void
600 const struct xrt_vec3 *right,
601 struct xrt_vec3 *result_out);
602
603/*!
604 * Transform a vec3 by a 4x4 matrix, extending the vector with w = 1.0
605 *
606 * @see xrt_matrix_4x4
607 * @ingroup aux_math
608 */
609void
611 const struct xrt_vec3 *right,
612 struct xrt_vec3 *result_out);
613
614/*!
615 * Transform a double vec3 by a 3x3 double matrix
616 *
617 * @see xrt_matrix_3x3
618 * @ingroup aux_math
619 */
620void
622 const struct xrt_vec3_f64 *right,
623 struct xrt_vec3_f64 *result_out);
624
625/*!
626 * Multiply Matrix3x3.
627 *
628 * @relates xrt_matrix_3x3
629 * @ingroup aux_math
630 */
631void
632math_matrix_3x3_multiply(const struct xrt_matrix_3x3 *left,
633 const struct xrt_matrix_3x3 *right,
634 struct xrt_matrix_3x3 *result_out);
635
636/*!
637 * Invert Matrix3x3
638 *
639 * @relates xrt_matrix_3x3
640 * @ingroup aux_math
641 */
642void
643math_matrix_3x3_inverse(const struct xrt_matrix_3x3 *in, struct xrt_matrix_3x3 *result);
644
645/*!
646 * Transpose Matrix3x3
647 *
648 * @relates xrt_matrix_3x3
649 * @ingroup aux_math
650 */
651void
652math_matrix_3x3_transpose(const struct xrt_matrix_3x3 *in, struct xrt_matrix_3x3 *result);
653
654/*!
655 * Create a rotation from two vectors plus x and z, by
656 * creating a rotation matrix by crossing z and x to
657 * get the y axis.
658 *
659 * Input vectors should be normalized.
660 *
661 * @relates xrt_matrix_3x3
662 * @ingroup aux_math
663 */
664void
665math_matrix_3x3_f64_from_plus_x_z(const struct xrt_vec3_f64 *plus_x,
666 const struct xrt_vec3_f64 *plus_z,
667 struct xrt_matrix_3x3_f64 *result);
668
669/*!
670 * Get the rotation matrix from an isomertry matrix (col-major).
671 *
672 * @relates xrt_matrix_4x4
673 * @ingroup aux_math
674 */
675void
676math_matrix_3x3_rotation_from_isometry(const struct xrt_matrix_4x4 *isometry, struct xrt_matrix_3x3 *result);
677
678/*!
679 * Initialize Matrix4x4 with identity.
680 *
681 * @relates xrt_matrix_4x4
682 * @ingroup aux_math
683 */
684void
685math_matrix_4x4_identity(struct xrt_matrix_4x4 *result);
686
687/*!
688 * Multiply Matrix4x4.
689 *
690 * @relates xrt_matrix_4x4
691 * @ingroup aux_math
692 */
693void
694math_matrix_4x4_multiply(const struct xrt_matrix_4x4 *left,
695 const struct xrt_matrix_4x4 *right,
696 struct xrt_matrix_4x4 *result);
697
698/*!
699 * Invert Matrix4x4.
700 *
701 * @relates xrt_matrix_4x4
702 * @ingroup aux_math
703 */
704void
705math_matrix_4x4_inverse(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result);
706
707/*!
708 * Invert a homogeneous isometry 4x4 (col-major) matrix in SE(3).
709 *
710 * @relates xrt_matrix_4x4
711 * @ingroup aux_math
712 */
713void
714math_matrix_4x4_isometry_inverse(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result);
715
716/*!
717 * Transpose Matrix4x4
718 *
719 * @relates xrt_matrix_4x4
720 * @ingroup aux_math
721 */
722void
723math_matrix_4x4_transpose(const struct xrt_matrix_4x4 *in, struct xrt_matrix_4x4 *result);
724
725/*!
726 * Compute view matrix from xrt_pose.
727 *
728 * @relates xrt_matrix_4x4
729 * @ingroup aux_math
730 */
731void
732math_matrix_4x4_view_from_pose(const struct xrt_pose *pose, struct xrt_matrix_4x4 *result);
733
734/*!
735 * Get an isometry matrix —in SE(3)— from a rotation matrix —SO(3)— and a
736 * translation vector. All col-major matrices.
737 *
738 * @relates xrt_matrix_4x4
739 * @ingroup aux_math
740 */
741void
742math_matrix_4x4_isometry_from_rt(const struct xrt_matrix_3x3 *rotation,
743 const struct xrt_vec3 *translation,
744 struct xrt_matrix_4x4 *result);
745
746/*!
747 * Get a col-major isometry matrix —in SE(3)— from a pose.
748 *
749 * @relates xrt_matrix_4x4
750 * @ingroup aux_math
751 */
752void
753math_matrix_4x4_isometry_from_pose(const struct xrt_pose *pose, struct xrt_matrix_4x4 *result);
754
755/*!
756 * Compute quad layer model matrix from xrt_pose and xrt_vec2 size.
757 *
758 * @relates xrt_matrix_4x4
759 * @ingroup aux_math
760 */
761void
762math_matrix_4x4_model(const struct xrt_pose *pose, const struct xrt_vec3 *size, struct xrt_matrix_4x4 *result);
763
764/*!
765 * Compute inverse view projection matrix,
766 * using only the starting 3x3 block of the view.
767 *
768 * @relates xrt_matrix_4x4
769 * @ingroup aux_math
770 */
771void
772math_matrix_4x4_inverse_view_projection(const struct xrt_matrix_4x4 *view,
773 const struct xrt_matrix_4x4 *projection,
774 struct xrt_matrix_4x4 *result);
775
776/*!
777 * Compute a projection matrix with settings for Vulkan, it will also have it's
778 * far plane at infinite and the NDC depth will be reversed.
779 *
780 * @relates xrt_matrix_4x4
781 * @ingroup aux_math
782 */
783void
784math_matrix_4x4_projection_vulkan_infinite_reverse(const struct xrt_fov *fov,
785 float near_plane,
786 struct xrt_matrix_4x4 *result);
787
788
789/*
790 *
791 * Pose functions.
792 *
793 */
794
795
796/*!
797 * Somewhat laboriously make an xrt_pose identity.
798 *
799 * @relates xrt_pose
800 * @ingroup aux_math
801 */
802void
803math_pose_identity(struct xrt_pose *pose);
804
805/*!
806 * Check if this pose can be used in transformation operations.
807 *
808 * @relates xrt_pose
809 * @ingroup aux_math
810 */
811bool
812math_pose_validate(const struct xrt_pose *pose);
813
814/*!
815 * Invert pose.
816 *
817 * OK if input and output are the same addresses.
818 *
819 * @relates xrt_pose
820 * @ingroup aux_math
821 */
822void
823math_pose_invert(const struct xrt_pose *pose, struct xrt_pose *outPose);
824
825/*!
826 * Converts a (col-major) isometry into a pose.
827 *
828 * @relates xrt_pose
829 * @ingroup aux_math
830 */
831void
832math_pose_from_isometry(const struct xrt_matrix_4x4 *transform, struct xrt_pose *result);
833
834/*!
835 * Interpolated pose between poses `a` and `b` by lerping position and slerping
836 * orientation by t.
837 *
838 * @relates xrt_pose
839 * @ingroup aux_math
840 */
841void
842math_pose_interpolate(const struct xrt_pose *a, const struct xrt_pose *b, float t, struct xrt_pose *outPose);
843
844/*!
845 * Apply a rigid-body transformation to a pose.
846 *
847 * OK if input and output are the same addresses.
848 *
849 * @relates xrt_pose
850 * @ingroup aux_math
851 */
852void
853math_pose_transform(const struct xrt_pose *transform, const struct xrt_pose *pose, struct xrt_pose *outPose);
854
855/*!
856 * Apply a rigid-body transformation to a point.
857 *
858 * The input point and output may be the same pointer.
859 *
860 * @relates xrt_pose
861 * @see xrt_vec3
862 * @ingroup aux_math
863 */
864void
865math_pose_transform_point(const struct xrt_pose *transform, const struct xrt_vec3 *point, struct xrt_vec3 *out_point);
866
867/*!
868 * Convert a pose from the OpenCV coordinate system to the OpenXR coordinate system and back. OpenCV camera space
869 * coordinates has +Y down and +Z away from the user.
870 *
871 * The input and output may be the same pointer.
872 *
873 * @relates xrt_pose
874 * @ingroup aux_math
875 */
876void
877math_pose_convert_from_opencv(const struct xrt_pose *in, struct xrt_pose *out);
878
879
880/*
881 *
882 * Space relation functions.
883 *
884 */
885
886
887/*!
888 * Convert a space relation from the OpenCV coordinate system to the OpenXR coordinate system and back. OpenCV camera
889 * space coordinates has +Y down and +Z away from the user.
890 *
891 * The input and output may be the same pointer.
892 *
893 * @relates xrt_space_relation
894 * @ingroup aux_math
895 */
896void
897math_space_relation_convert_from_opencv(const struct xrt_space_relation *in, struct xrt_space_relation *out);
898
899
900/*
901 *
902 * Inline functions.
903 *
904 */
905
906/*!
907 * Map a number from one range to another range.
908 * Exactly the same as Arduino's map().
909 */
910static inline double
911math_map_ranges(double value, double from_low, double from_high, double to_low, double to_high)
912{
913 return (value - from_low) * (to_high - to_low) / (from_high - from_low) + to_low;
914}
915
916static inline double
917math_lerp(double from, double to, double amount)
918{
919 return (from * (1.0 - amount)) + (to * (amount));
920}
921
922static inline struct xrt_uv_triplet
923uv_triplet_lerp(struct xrt_uv_triplet from, struct xrt_uv_triplet to, float amount)
924{
925 struct xrt_uv_triplet out;
926 out.r.x = math_lerp(from.r.x, to.r.x, amount);
927 out.r.y = math_lerp(from.r.y, to.r.y, amount);
928 out.g.x = math_lerp(from.g.x, to.g.x, amount);
929 out.g.y = math_lerp(from.g.y, to.g.y, amount);
930 out.b.x = math_lerp(from.b.x, to.b.x, amount);
931 out.b.y = math_lerp(from.b.y, to.b.y, amount);
932 return out;
933}
934
935/*
936 *
937 * Optics functions.
938 *
939 */
940
941/*!
942 * Perform the computations from
943 * "Computing Half-Fields-Of-View from Simpler Display Models",
944 * to get half-FOVs from things we can retrieve from other APIs.
945 * The origin is in the lower-left corner of the display, so w_1 is the width to
946 * the left of CoP, and h_1 is the height below CoP.
947 *
948 * If vertfov_total is set to 0, it will be computed from h_total.
949 *
950 * Distances are in arbitrary but consistent units. Angles are in radians.
951 *
952 *
953 * In the diagram below, treating it like a FOV for horizontal,
954 * the top angle is horizfov_total, the length of the bottom
955 * is w_total, and the distance between the vertical line and the left corner is
956 * w_1. Vertical is similar - h_1 is above the center line.
957 * The triangle need not be symmetrical, despite how the diagram looks.
958 *
959 * ```
960 * horizfov_total
961 * *
962 * angle_left (neg) -> / | \ <- angle_right
963 * / | \
964 * / | \
965 * / | \
966 * -------------
967 * [ w_1 ]
968 * [ --- w --- ]
969 *
970 * ------- --- |\
971 * | \
972 * h_1 | \ angle_up
973 * h_total ___ |-------* vertfov_total
974 * | / angle_down (neg)
975 * | /
976 * | /
977 * ------- |/
978 * ```
979 *
980 * @return true if successful.
981 * @ingroup aux_math
982 */
983bool
984math_compute_fovs(double w_total,
985 double w_1,
986 double horizfov_total,
987 double h_total,
988 double h_1,
989 double vertfov_total,
990 struct xrt_fov *fov);
991
992/*!
993 * Compute the FOV to use when parallelizing canted views.
994 *
995 * Some applications do not support rendering for view orientations that are
996 * not parallel to each other.
997 * When using a headset with physically canted displays, such applications
998 * require parallelizing the views, i.e. forcing the orientations of the views
999 * to be parallel.
1000 * When the application passes content rendered for parallel views to the
1001 * compositor, the compositor typically reprojects this content such that it
1002 * matches the physical canting of the displays, effectively rotating the view
1003 * orientation.
1004 *
1005 * When rotating the view orientation, parts of the FOV are cut off on the side
1006 * the view rotates away from and parts of previously unseen content is pulled
1007 * in from the side the view rotates towards. Therefore, when parallezing
1008 * views, the application should render with an adjusted FOV that covers the
1009 * area that will be in the FOV of the view *after* the compositor reprojects
1010 * it back to the physical canted orientation.
1011 *
1012 * @ingroup aux_math
1013 */
1014void
1015math_compute_parallelized_fov(const struct xrt_fov *fov,
1016 const struct xrt_quat *canted_view_orientation,
1017 struct xrt_fov *out_parallelized_fov);
1018
1019#ifdef __cplusplus
1020}
1021#endif
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:669
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:119
void math_matrix_3x3_identity(struct xrt_matrix_3x3 *mat)
Initialize a 3x3 matrix to the identity matrix.
Definition m_base.cpp:643
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_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:675
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:727
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:376
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:714
void math_compute_parallelized_fov(const struct xrt_fov *fov, const struct xrt_quat *canted_view_orientation, struct xrt_fov *out_parallelized_fov)
Compute the FOV to use when parallelizing canted views.
Definition m_optics.c:165
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:649
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:911
Wrapper header for <math.h> to ensure pi-related math constants are defined.
Describes a projection matrix fov.
Definition xrt_defines.h:555
A tightly packed 3x3 matrix of doubles.
Definition xrt_defines.h:612
A tightly packed 3x3 matrix of floats.
Definition xrt_defines.h:602
A tightly packed 4x4 matrix of floats.
Definition xrt_defines.h:629
A pose composed of a position and orientation.
Definition xrt_defines.h:535
A quaternion with double floats.
Definition xrt_defines.h:269
A quaternion with single floats.
Definition xrt_defines.h:246
A relation with two spaces, includes velocity and acceleration.
Definition xrt_defines.h:726
Represents a uv triplet for distortion, basically just three xrt_vec2.
Definition xrt_defines.h:313
A 2 element vector with single floats.
Definition xrt_defines.h:292
A 3 element vector with single doubles.
Definition xrt_defines.h:335
A 3 element vector with single floats.
Definition xrt_defines.h:323
Common defines and enums for XRT.