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