Monado OpenXR Runtime
m_vec2.h
Go to the documentation of this file.
1// Copyright 2019, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief C vec2 math library.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 *
8 * @see xrt_vec2
9 * @ingroup aux_math
10 */
11
12#pragma once
13
14#include "xrt/xrt_defines.h"
15
16#include "m_mathinclude.h"
17
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23
24static inline struct xrt_vec2
25m_vec2_mul(struct xrt_vec2 l, struct xrt_vec2 r)
26{
27 struct xrt_vec2 ret = {l.x * r.x, l.y * r.y};
28 return ret;
29}
30
31static inline struct xrt_vec2
32m_vec2_mul_scalar(struct xrt_vec2 l, float r)
33{
34 struct xrt_vec2 ret = {l.x * r, l.y * r};
35 return ret;
36}
37
38static inline struct xrt_vec2
39m_vec2_add(struct xrt_vec2 l, struct xrt_vec2 r)
40{
41 struct xrt_vec2 ret = {l.x + r.x, l.y + r.y};
42 return ret;
43}
44
45static inline struct xrt_vec2
46m_vec2_add_scalar(struct xrt_vec2 l, float r)
47{
48 struct xrt_vec2 ret = {l.x + r, l.y + r};
49 return ret;
50}
51
52static inline struct xrt_vec2
53m_vec2_sub(struct xrt_vec2 l, struct xrt_vec2 r)
54{
55 struct xrt_vec2 ret = {l.x - r.x, l.y - r.y};
56 return ret;
57}
58
59static inline struct xrt_vec2
60m_vec2_sub_scalar(struct xrt_vec2 l, float r)
61{
62 struct xrt_vec2 ret = {l.x - r, l.y - r};
63 return ret;
64}
65
66static inline struct xrt_vec2
67m_vec2_div(struct xrt_vec2 l, struct xrt_vec2 r)
68{
69 struct xrt_vec2 ret = {l.x / r.x, l.y / r.y};
70 return ret;
71}
72
73static inline struct xrt_vec2
74m_vec2_div_scalar(struct xrt_vec2 l, float r)
75{
76 struct xrt_vec2 ret = {l.x / r, l.y / r};
77 return ret;
78}
79
80static inline float
81m_vec2_len_sqrd(struct xrt_vec2 l)
82{
83 return l.x * l.x + l.y * l.y;
84}
85
86
87static inline float
88m_vec2_len(struct xrt_vec2 l)
89{
90 return sqrtf(m_vec2_len_sqrd(l));
91}
92
93static inline void
94m_vec2_normalize(struct xrt_vec2 *inout)
95{
96 *inout = m_vec2_div_scalar(*inout, m_vec2_len(*inout));
97}
98
99static inline float
100m_vec2_dot(struct xrt_vec2 l, struct xrt_vec2 r)
101{
102 return l.x * r.x + l.y * r.y;
103}
104
105static inline struct xrt_vec2
106m_vec2_lerp(struct xrt_vec2 from, struct xrt_vec2 to, float amount)
107{
108 // Recommend amount being in [0,1]
109 return m_vec2_add(m_vec2_mul_scalar(from, 1.0f - amount), m_vec2_mul_scalar(to, amount));
110}
111
112typedef float m_vec2_float_arr[2];
113
114static inline m_vec2_float_arr *
115m_vec2_ptr_to_float_arr_ptr(struct xrt_vec2 *ptr)
116{
117 return (m_vec2_float_arr *)ptr;
118}
119
120
121#ifdef __cplusplus
122}
123
124
125static inline struct xrt_vec2
126operator+(const struct xrt_vec2 &a, const struct xrt_vec2 &b)
127{
128 return m_vec2_add(a, b);
129}
130
131static inline struct xrt_vec2
132operator-(const struct xrt_vec2 &a, const struct xrt_vec2 &b)
133{
134 return m_vec2_sub(a, b);
135}
136
137static inline struct xrt_vec2 // Until clang-format-11 is on the CI.
138operator*(const struct xrt_vec2 &a, const struct xrt_vec2 &b)
139{
140 return m_vec2_mul(a, b);
141}
142
143static inline struct xrt_vec2
144operator/(const struct xrt_vec2 &a, const struct xrt_vec2 &b)
145{
146 return m_vec2_div(a, b);
147}
148
149static inline void
150operator+=(struct xrt_vec2 &a, const struct xrt_vec2 &b)
151{
152 a = a + b;
153}
154
155static inline void
156operator-=(struct xrt_vec2 &a, const struct xrt_vec2 &b)
157{
158 a = a - b;
159}
160
161static inline void
162operator*=(struct xrt_vec2 &a, const struct xrt_vec2 &b)
163{
164 a = a * b;
165}
166
167static inline void
168operator/=(struct xrt_vec2 &a, const struct xrt_vec2 &b)
169{
170 a = a / b;
171}
172
173
174#endif
Wrapper header for <math.h> to ensure pi-related math constants are defined.
A 2 element vector with single floats.
Definition: xrt_defines.h:250
Common defines and enums for XRT.