Monado OpenXR Runtime
m_matrix_2x2.h
Go to the documentation of this file.
1// Copyright 2023, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief C matrix_2x2 math library.
6 * @author Moses Turner <moses@collabora.com>
7 *
8 * @see xrt_matrix_2x2
9 * @ingroup aux_math
10 */
11
12#pragma once
13
14#include "xrt/xrt_defines.h"
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20static inline void
21m_mat2x2_multiply(const struct xrt_matrix_2x2 *left,
22 const struct xrt_matrix_2x2 *right,
23 struct xrt_matrix_2x2 *result_out)
24{
25 const struct xrt_matrix_2x2 l = *left;
26 const struct xrt_matrix_2x2 r = *right;
27
28 // Initialisers: struct, union, v[4]
29 struct xrt_matrix_2x2 result = {{{
30 l.v[0] * r.v[0] + l.v[1] * r.v[2],
31 l.v[0] * r.v[1] + l.v[1] * r.v[3],
32 l.v[2] * r.v[0] + l.v[3] * r.v[2],
33 l.v[2] * r.v[1] + l.v[3] * r.v[3],
34 }}};
35
36 *result_out = result;
37}
38
39static inline void
40m_mat2x2_transform_vec2(const struct xrt_matrix_2x2 *left, const struct xrt_vec2 *right, struct xrt_vec2 *result_out)
41{
42 const struct xrt_matrix_2x2 l = *left;
43 const struct xrt_vec2 r = *right;
44 struct xrt_vec2 result = {l.v[0] * r.x + l.v[1] * r.y, l.v[2] * r.x + l.v[3] * r.y};
45 *result_out = result;
46}
47
48static inline void
49m_mat2x2_invert(const struct xrt_matrix_2x2 *matrix, struct xrt_matrix_2x2 *invertedMatrix)
50{
51 float determinant = matrix->v[0] * matrix->v[3] - matrix->v[1] * matrix->v[2];
52 invertedMatrix->v[0] = matrix->v[3] / determinant;
53 invertedMatrix->v[1] = -matrix->v[1] / determinant;
54 invertedMatrix->v[2] = -matrix->v[2] / determinant;
55 invertedMatrix->v[3] = matrix->v[0] / determinant;
56}
57
58#ifdef __cplusplus
59}
60#endif
A tightly packed 2x2 matrix of floats.
Definition: xrt_defines.h:513
A 2 element vector with single floats.
Definition: xrt_defines.h:250
Common defines and enums for XRT.