Monado OpenXR Runtime
Loading...
Searching...
No Matches
m_eigen_interop.hpp
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 Interoperability helpers connecting internal math types and Eigen.
6 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
7 * @author Nis Madsen <nima_zero_one@protonmail.com>
8 * @ingroup aux_math
9 */
10
11#pragma once
12
13#ifndef __cplusplus
14#error "This header only usable from C++"
15#endif
16
17#include "math/m_api.h"
18
19#include <Eigen/Core>
20#include <Eigen/Geometry>
21
22namespace xrt::auxiliary::math {
23
24/*
25 * This file uses an Eigen::Map to directly reference the memory of our own internal math types as Eigen types.
26 * This means that our own internal math types *must* be memory-layout compatible with the types laid out here. If they
27 * are not, memory corruption or other nastiness will occur when attempting to read/mutate these maps. This is why we
28 * are passing a pointer to the *first* element into the Map constructors, so that when Eigen interprets that raw data
29 * pointer as it's math type, it is reading from the start of the data type.
30 */
31
32
33/*!
34 * @brief Wrap an internal quaternion struct in an Eigen type, const overload.
35 *
36 * Permits zero-overhead manipulation of `const xrt_quat&` by Eigen routines as
37 * if it were a `const Eigen::Quaternionf&`.
38 */
39static inline Eigen::Map<const Eigen::Quaternionf>
40map_quat(const struct xrt_quat &q)
41{
42 return Eigen::Map<const Eigen::Quaternionf>{&q.x};
43}
44
45/*!
46 * @brief Wrap an internal quaternion struct in an Eigen type, non-const
47 * overload.
48 *
49 * Permits zero-overhead manipulation of `xrt_quat&` by Eigen routines as if it
50 * were a `Eigen::Quaternionf&`.
51 */
52static inline Eigen::Map<Eigen::Quaternionf>
54{
55 return Eigen::Map<Eigen::Quaternionf>{&q.x};
56}
57
58/*!
59 * @brief Wrap an internal quaternion struct in an Eigen type, const overload.
60 *
61 * Permits zero-overhead manipulation of `const xrt_quat_f64&` by Eigen routines as
62 * if it were a `const Eigen::Quaterniond&`.
63 */
64static inline Eigen::Map<const Eigen::Quaterniond>
65map_quat_f64(const struct xrt_quat_f64 &q)
66{
67 return Eigen::Map<const Eigen::Quaterniond>{&q.x};
68}
69
70/*!
71 * @brief Wrap an internal quaternion struct in an Eigen type, non-const
72 * overload.
73 *
74 * Permits zero-overhead manipulation of `xrt_quat_f64&` by Eigen routines as if it
75 * were a `Eigen::Quaterniond&`.
76 */
77static inline Eigen::Map<Eigen::Quaterniond>
79{
80 return Eigen::Map<Eigen::Quaterniond>{&q.x};
81}
82
83/*!
84 * @brief Wrap an internal 2D vector struct in an Eigen type, const overload.
85 *
86 * Permits zero-overhead manipulation of `const xrt_vec2&` by Eigen routines as
87 * if it were a `const Eigen::Vector2f&`.
88 */
89static inline Eigen::Map<const Eigen::Vector2f>
90map_vec2(const struct xrt_vec2 &v)
91{
92 return Eigen::Map<const Eigen::Vector2f>{&v.x};
93}
94
95/*!
96 * @brief Wrap an internal 2D vector struct in an Eigen type, non-const
97 * overload.
98 *
99 * Permits zero-overhead manipulation of `xrt_vec2&` by Eigen routines as
100 * if it were a `Eigen::Vector2f&`.
101 */
102static inline Eigen::Map<Eigen::Vector2f>
104{
105 return Eigen::Map<Eigen::Vector2f>{&v.x};
106}
107
108/*!
109 * @brief Wrap an internal 3D vector struct in an Eigen type, const overload.
110 *
111 * Permits zero-overhead manipulation of `const xrt_vec3&` by Eigen routines as
112 * if it were a `const Eigen::Vector3f&`.
113 */
114static inline Eigen::Map<const Eigen::Vector3f>
115map_vec3(const struct xrt_vec3 &v)
116{
117 return Eigen::Map<const Eigen::Vector3f>{&v.x};
118}
119
120/*!
121 * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
122 * overload.
123 *
124 * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
125 * if it were a `Eigen::Vector3f&`.
126 */
127static inline Eigen::Map<Eigen::Vector3f>
129{
130 return Eigen::Map<Eigen::Vector3f>{&v.x};
131}
132
133/*!
134 * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
135 * overload.
136 *
137 * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
138 * if it were a `const Eigen::Vector3_f64&`.
139 */
140static inline Eigen::Map<const Eigen::Vector3d>
142{
143 return Eigen::Map<const Eigen::Vector3d>{&v.x};
144}
145
146/*!
147 * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
148 * overload.
149 *
150 * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
151 * if it were a `Eigen::Vector3_f64&`.
152 */
153static inline Eigen::Map<Eigen::Vector3d>
155{
156 return Eigen::Map<Eigen::Vector3d>{&v.x};
157}
158
159/*!
160 * @brief Wrap an internal 3x3 matrix struct in an Eigen type, const overload.
161 *
162 * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
163 * if it were a `Eigen::Matrix3f&`.
164 */
165static inline Eigen::Map<const Eigen::Matrix3f>
167{
168 return Eigen::Map<const Eigen::Matrix3f>(m.v);
169}
170
171/*!
172 * @brief Wrap an internal 3x3 matrix struct in an Eigen type, non-const
173 * overload.
174 *
175 * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
176 * if it were a `Eigen::Matrix3f&`.
177 */
178static inline Eigen::Map<Eigen::Matrix3f>
180{
181 return Eigen::Map<Eigen::Matrix3f>(m.v);
182}
183
184/*!
185 * @brief Wrap an internal 3x3 matrix struct in an Eigen type, non-const
186 * overload.
187 *
188 * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
189 * if it were a `Eigen::Matrix3_f64&`.
190 */
191static inline Eigen::Map<Eigen::Matrix3d>
193{
194 return Eigen::Map<Eigen::Matrix3d>(m.v);
195}
196
197/*!
198 * @brief Wrap an internal 4x4 matrix struct in an Eigen type, const
199 * overload.
200 *
201 * Permits zero-overhead manipulation of `xrt_matrix_4x4&` by Eigen routines as
202 * if it were a `Eigen::Matrix4f&`.
203 */
204static inline Eigen::Map<const Eigen::Matrix4f>
206{
207 return Eigen::Map<const Eigen::Matrix4f>(m.v);
208}
209
210/*!
211 * @brief Wrap an internal 4x4 matrix struct in an Eigen type, non-const
212 * overload.
213 *
214 * Permits zero-overhead manipulation of `xrt_matrix_4x4&` by Eigen routines as
215 * if it were a `Eigen::Matrix4f&`.
216 */
217static inline Eigen::Map<Eigen::Matrix4f>
219{
220 return Eigen::Map<Eigen::Matrix4f>(m.v);
221}
222
223/*!
224 * @brief Wrap an internal 4x4 matrix f64 struct in an Eigen type, const overload.
225 *
226 * Permits zero-overhead manipulation of `const xrt_matrix_4x4_f64&` by Eigen routines as if it were a
227 * `const Eigen::Matrix4d&`.
228 */
229static inline Eigen::Map<const Eigen::Matrix4d>
231{
232 return Eigen::Map<const Eigen::Matrix4d>(m.v);
233}
234
235/*!
236 * @brief Wrap an internal 4x4 matrix struct in an Eigen type, non-const overload.
237 *
238 * Permits zero-overhead manipulation of `xrt_matrix_4x4_f64&` by Eigen routines as if it were a `Eigen::Matrix4d&`.
239 */
240static inline Eigen::Map<Eigen::Matrix4d>
242{
243 return Eigen::Map<Eigen::Matrix4d>(m.v);
244}
245
246
247/*
248 *
249 * Pose deconstruction helpers.
250 *
251 */
252
253/*!
254 * Return a Eigen type wrapping a pose's orientation (const).
255 */
256static inline Eigen::Map<const Eigen::Quaternionf>
257orientation(const struct xrt_pose &pose)
258{
259 return map_quat(pose.orientation);
260}
261
262/*!
263 * Return a Eigen type wrapping a pose's orientation.
264 */
265static inline Eigen::Map<Eigen::Quaternionf>
267{
268 return map_quat(pose.orientation);
269}
270
271/*!
272 * Return a Eigen type wrapping a pose's position (const).
273 */
274static inline Eigen::Map<const Eigen::Vector3f>
275position(const struct xrt_pose &pose)
276{
277 return map_vec3(pose.position);
278}
279
280/*!
281 * Return a Eigen type wrapping a pose's position.
282 */
283static inline Eigen::Map<Eigen::Vector3f>
284position(struct xrt_pose &pose)
285{
286 return map_vec3(pose.position);
287}
288
289} // namespace xrt::auxiliary::math
C interface to math library.
C++-only functionality in the Math helper library.
Definition m_documentation.hpp:15
static Eigen::Map< const Eigen::Vector2f > map_vec2(const struct xrt_vec2 &v)
Wrap an internal 2D vector struct in an Eigen type, const overload.
Definition m_eigen_interop.hpp:90
static Eigen::Map< const Eigen::Vector3d > map_vec3_f64(const struct xrt_vec3_f64 &v)
Wrap an internal 3D vector struct in an Eigen type, non-const overload.
Definition m_eigen_interop.hpp:141
static Eigen::Map< Eigen::Matrix3d > map_matrix_3x3_f64(struct xrt_matrix_3x3_f64 &m)
Wrap an internal 3x3 matrix struct in an Eigen type, non-const overload.
Definition m_eigen_interop.hpp:192
static Eigen::Map< const Eigen::Vector3f > position(const struct xrt_pose &pose)
Return a Eigen type wrapping a pose's position (const).
Definition m_eigen_interop.hpp:275
static Eigen::Map< const Eigen::Matrix4f > map_matrix_4x4(const struct xrt_matrix_4x4 &m)
Wrap an internal 4x4 matrix struct in an Eigen type, const overload.
Definition m_eigen_interop.hpp:205
static Eigen::Map< const Eigen::Quaterniond > map_quat_f64(const struct xrt_quat_f64 &q)
Wrap an internal quaternion struct in an Eigen type, const overload.
Definition m_eigen_interop.hpp:65
static Eigen::Map< const Eigen::Quaternionf > map_quat(const struct xrt_quat &q)
Wrap an internal quaternion struct in an Eigen type, const overload.
Definition m_eigen_interop.hpp:40
static Eigen::Map< const Eigen::Matrix3f > map_matrix_3x3(const struct xrt_matrix_3x3 &m)
Wrap an internal 3x3 matrix struct in an Eigen type, const overload.
Definition m_eigen_interop.hpp:166
static Eigen::Map< const Eigen::Quaternionf > orientation(const struct xrt_pose &pose)
Return a Eigen type wrapping a pose's orientation (const).
Definition m_eigen_interop.hpp:257
static Eigen::Map< const Eigen::Matrix4d > map_matrix_4x4_f64(const struct xrt_matrix_4x4_f64 &m)
Wrap an internal 4x4 matrix f64 struct in an Eigen type, const overload.
Definition m_eigen_interop.hpp:230
static Eigen::Map< const Eigen::Vector3f > map_vec3(const struct xrt_vec3 &v)
Wrap an internal 3D vector struct in an Eigen type, const overload.
Definition m_eigen_interop.hpp:115
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 double.
Definition xrt_defines.h:639
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 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