Monado OpenXR Runtime
m_vec3.h
Go to the documentation of this file.
1// Copyright 2019-2021, Collabora, Ltd.
2// Copyright 2020, Nova King.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief C vec3 math library.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @author Nova King <technobaboo@gmail.com>
9 * @author Moses Turner <moses@collabora.com>>
10 *
11 * @see xrt_vec3
12 * @ingroup aux_math
13 */
14
15#pragma once
16
17#include "xrt/xrt_defines.h"
18
19#include "m_mathinclude.h"
20#include <float.h>
21
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27
28static inline struct xrt_vec3
29m_vec3_mul(struct xrt_vec3 l, struct xrt_vec3 r)
30{
31 struct xrt_vec3 ret = {l.x * r.x, l.y * r.y, l.z * r.z};
32 return ret;
33}
34
35static inline struct xrt_vec3
36m_vec3_mul_scalar(struct xrt_vec3 l, float r)
37{
38 struct xrt_vec3 ret = {l.x * r, l.y * r, l.z * r};
39 return ret;
40}
41
42static inline struct xrt_vec3
43m_vec3_add(struct xrt_vec3 l, struct xrt_vec3 r)
44{
45 struct xrt_vec3 ret = {l.x + r.x, l.y + r.y, l.z + r.z};
46 return ret;
47}
48
49static inline struct xrt_vec3
50m_vec3_sub(struct xrt_vec3 l, struct xrt_vec3 r)
51{
52 struct xrt_vec3 ret = {l.x - r.x, l.y - r.y, l.z - r.z};
53 return ret;
54}
55
56static inline struct xrt_vec3
57m_vec3_div(struct xrt_vec3 l, struct xrt_vec3 r)
58{
59 struct xrt_vec3 ret = {l.x / r.x, l.y / r.y, l.z / r.z};
60 return ret;
61}
62
63static inline struct xrt_vec3
64m_vec3_div_scalar(struct xrt_vec3 l, float r)
65{
66 struct xrt_vec3 ret = {l.x / r, l.y / r, l.z / r};
67 return ret;
68}
69
70static inline struct xrt_vec3
71m_vec3_inverse(struct xrt_vec3 v)
72{
73 struct xrt_vec3 ret = {-v.x, -v.y, -v.z};
74 return ret;
75}
76
77static inline float
78m_vec3_dot(struct xrt_vec3 l, struct xrt_vec3 r)
79{
80 return l.x * r.x + l.y * r.y + l.z * r.z;
81}
82
83static inline float
84m_vec3_len_sqrd(struct xrt_vec3 l)
85{
86 return m_vec3_dot(l, l);
87}
88
89static inline float
90m_vec3_len(struct xrt_vec3 l)
91{
92 return sqrtf(m_vec3_len_sqrd(l));
93}
94
95static inline struct xrt_vec3
96m_vec3_normalize(struct xrt_vec3 l)
97{
98 float len = m_vec3_len(l);
99 if (len <= FLT_EPSILON) {
100 return l;
101 }
102
103 struct xrt_vec3 ret = {
104 l.x / len,
105 l.y / len,
106 l.z / len,
107 };
108 return ret;
109}
110
111static inline float
112m_vec3_angle(struct xrt_vec3 l, struct xrt_vec3 r)
113{
114 float dot = m_vec3_dot(l, r);
115 float lengths = m_vec3_len_sqrd(l) * m_vec3_len_sqrd(r);
116
117 if (lengths == 0) {
118 return 0;
119 }
120 lengths = sqrtf(lengths);
121
122 return acosf(dot / lengths);
123}
124
125static inline struct xrt_vec3
126m_vec3_project(struct xrt_vec3 project_this, struct xrt_vec3 onto_this)
127{
128
129 float amnt = (m_vec3_dot(project_this, onto_this) / m_vec3_len_sqrd(onto_this));
130
131 return m_vec3_mul_scalar(onto_this, amnt);
132}
133
134static inline struct xrt_vec3
135m_vec3_orthonormalize(struct xrt_vec3 leave_this_alone, struct xrt_vec3 change_this_one)
136{
137 return m_vec3_normalize(m_vec3_sub(change_this_one, m_vec3_project(change_this_one, leave_this_alone)));
138}
139
140static inline struct xrt_vec3
141m_vec3_lerp(struct xrt_vec3 from, struct xrt_vec3 to, float amount)
142{
143 // Recommend amount being in [0,1]
144 return m_vec3_add(m_vec3_mul_scalar(from, 1.0f - amount), m_vec3_mul_scalar(to, amount));
145}
146
147static inline bool
148m_vec3_equal_exact(struct xrt_vec3 l, struct xrt_vec3 r)
149{
150 return l.x == r.x && l.y == r.y && l.z == r.z;
151}
152
153typedef float m_vec3_float_arr[3];
154
155static inline m_vec3_float_arr *
156m_vec3_ptr_to_float_arr_ptr(struct xrt_vec3 *ptr)
157{
158 return (m_vec3_float_arr *)ptr;
159}
160
161
162#ifdef __cplusplus
163}
164
165
166static inline struct xrt_vec3
167operator+(const struct xrt_vec3 &a, const struct xrt_vec3 &b)
168{
169 return m_vec3_add(a, b);
170}
171
172static inline struct xrt_vec3
173operator-(const struct xrt_vec3 &a, const struct xrt_vec3 &b)
174{
175 return m_vec3_sub(a, b);
176}
177
178static inline struct xrt_vec3
179operator*(const struct xrt_vec3 &a, const float &b)
180{
181 return m_vec3_mul_scalar(a, b);
182}
183
184static inline struct xrt_vec3
185operator*(const struct xrt_vec3 &a, const struct xrt_vec3 &b)
186{
187 return m_vec3_mul(a, b);
188}
189
190static inline struct xrt_vec3
191operator/(const struct xrt_vec3 &a, const struct xrt_vec3 &b)
192{
193 return m_vec3_div(a, b);
194}
195
196static inline struct xrt_vec3
197operator/(const struct xrt_vec3 &a, const float &b)
198{
199 return m_vec3_div_scalar(a, b);
200}
201
202static inline void
203operator+=(struct xrt_vec3 &a, const struct xrt_vec3 &b)
204{
205 a = a + b;
206}
207
208static inline void
209operator-=(struct xrt_vec3 &a, const struct xrt_vec3 &b)
210{
211 a = a - b;
212}
213
214static inline void
215operator*=(struct xrt_vec3 &a, const struct xrt_vec3 &b)
216{
217 a = a * b;
218}
219
220static inline void
221operator*=(struct xrt_vec3 &a, const float &b)
222{
223 a = a * b;
224}
225
226static inline void
227operator/=(struct xrt_vec3 &a, const struct xrt_vec3 &b)
228{
229 a = a / b;
230}
231
232
233#endif
Wrapper header for <math.h> to ensure pi-related math constants are defined.
A 3 element vector with single floats.
Definition: xrt_defines.h:271
Common defines and enums for XRT.