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 2D vector struct in an Eigen type, const overload.
60 *
61 * Permits zero-overhead manipulation of `const xrt_vec2&` by Eigen routines as
62 * if it were a `const Eigen::Vector2f&`.
63 */
64static inline Eigen::Map<const Eigen::Vector2f>
65map_vec2(const struct xrt_vec2 &v)
66{
67 return Eigen::Map<const Eigen::Vector2f>{&v.x};
68}
69
70/*!
71 * @brief Wrap an internal 2D vector struct in an Eigen type, non-const
72 * overload.
73 *
74 * Permits zero-overhead manipulation of `xrt_vec2&` by Eigen routines as
75 * if it were a `Eigen::Vector2f&`.
76 */
77static inline Eigen::Map<Eigen::Vector2f>
79{
80 return Eigen::Map<Eigen::Vector2f>{&v.x};
81}
82
83/*!
84 * @brief Wrap an internal 3D vector struct in an Eigen type, const overload.
85 *
86 * Permits zero-overhead manipulation of `const xrt_vec3&` by Eigen routines as
87 * if it were a `const Eigen::Vector3f&`.
88 */
89static inline Eigen::Map<const Eigen::Vector3f>
90map_vec3(const struct xrt_vec3 &v)
91{
92 return Eigen::Map<const Eigen::Vector3f>{&v.x};
93}
94
95/*!
96 * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
97 * overload.
98 *
99 * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
100 * if it were a `Eigen::Vector3f&`.
101 */
102static inline Eigen::Map<Eigen::Vector3f>
104{
105 return Eigen::Map<Eigen::Vector3f>{&v.x};
106}
107
108/*!
109 * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
110 * overload.
111 *
112 * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
113 * if it were a `const Eigen::Vector3_f64&`.
114 */
115static inline Eigen::Map<const Eigen::Vector3d>
117{
118 return Eigen::Map<const Eigen::Vector3d>{&v.x};
119}
120
121/*!
122 * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
123 * overload.
124 *
125 * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
126 * if it were a `Eigen::Vector3_f64&`.
127 */
128static inline Eigen::Map<Eigen::Vector3d>
130{
131 return Eigen::Map<Eigen::Vector3d>{&v.x};
132}
133
134/*!
135 * @brief Wrap an internal 3x3 matrix struct in an Eigen type, const overload.
136 *
137 * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
138 * if it were a `Eigen::Matrix3f&`.
139 */
140static inline Eigen::Map<const Eigen::Matrix3f>
142{
143 return Eigen::Map<const Eigen::Matrix3f>(m.v);
144}
145
146/*!
147 * @brief Wrap an internal 3x3 matrix struct in an Eigen type, non-const
148 * overload.
149 *
150 * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
151 * if it were a `Eigen::Matrix3f&`.
152 */
153static inline Eigen::Map<Eigen::Matrix3f>
155{
156 return Eigen::Map<Eigen::Matrix3f>(m.v);
157}
158
159/*!
160 * @brief Wrap an internal 3x3 matrix struct in an Eigen type, non-const
161 * overload.
162 *
163 * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
164 * if it were a `Eigen::Matrix3_f64&`.
165 */
166static inline Eigen::Map<Eigen::Matrix3d>
168{
169 return Eigen::Map<Eigen::Matrix3d>(m.v);
170}
171
172/*!
173 * @brief Wrap an internal 4x4 matrix struct in an Eigen type, const
174 * overload.
175 *
176 * Permits zero-overhead manipulation of `xrt_matrix_4x4&` by Eigen routines as
177 * if it were a `Eigen::Matrix4f&`.
178 */
179static inline Eigen::Map<const Eigen::Matrix4f>
181{
182 return Eigen::Map<const Eigen::Matrix4f>(m.v);
183}
184
185/*!
186 * @brief Wrap an internal 4x4 matrix struct in an Eigen type, non-const
187 * overload.
188 *
189 * Permits zero-overhead manipulation of `xrt_matrix_4x4&` by Eigen routines as
190 * if it were a `Eigen::Matrix4f&`.
191 */
192static inline Eigen::Map<Eigen::Matrix4f>
194{
195 return Eigen::Map<Eigen::Matrix4f>(m.v);
196}
197
198/*!
199 * @brief Wrap an internal 4x4 matrix f64 struct in an Eigen type, const overload.
200 *
201 * Permits zero-overhead manipulation of `const xrt_matrix_4x4_f64&` by Eigen routines as if it were a
202 * `const Eigen::Matrix4d&`.
203 */
204static inline Eigen::Map<const Eigen::Matrix4d>
206{
207 return Eigen::Map<const Eigen::Matrix4d>(m.v);
208}
209
210/*!
211 * @brief Wrap an internal 4x4 matrix struct in an Eigen type, non-const overload.
212 *
213 * Permits zero-overhead manipulation of `xrt_matrix_4x4_f64&` by Eigen routines as if it were a `Eigen::Matrix4d&`.
214 */
215static inline Eigen::Map<Eigen::Matrix4d>
217{
218 return Eigen::Map<Eigen::Matrix4d>(m.v);
219}
220
221
222/*
223 *
224 * Pose deconstruction helpers.
225 *
226 */
227
228/*!
229 * Return a Eigen type wrapping a pose's orientation (const).
230 */
231static inline Eigen::Map<const Eigen::Quaternionf>
232orientation(const struct xrt_pose &pose)
233{
234 return map_quat(pose.orientation);
235}
236
237/*!
238 * Return a Eigen type wrapping a pose's orientation.
239 */
240static inline Eigen::Map<Eigen::Quaternionf>
242{
243 return map_quat(pose.orientation);
244}
245
246/*!
247 * Return a Eigen type wrapping a pose's position (const).
248 */
249static inline Eigen::Map<const Eigen::Vector3f>
250position(const struct xrt_pose &pose)
251{
252 return map_vec3(pose.position);
253}
254
255/*!
256 * Return a Eigen type wrapping a pose's position.
257 */
258static inline Eigen::Map<Eigen::Vector3f>
259position(struct xrt_pose &pose)
260{
261 return map_vec3(pose.position);
262}
263
264} // 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:65
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:116
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:167
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:250
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:180
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:141
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:232
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:205
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:90
A tightly packed 3x3 matrix of doubles.
Definition xrt_defines.h:590
A tightly packed 3x3 matrix of floats.
Definition xrt_defines.h:580
A tightly packed 4x4 matrix of double.
Definition xrt_defines.h:617
A tightly packed 4x4 matrix of floats.
Definition xrt_defines.h:607
A pose composed of a position and orientation.
Definition xrt_defines.h:513
A quaternion with single floats.
Definition xrt_defines.h:246
A 2 element vector with single floats.
Definition xrt_defines.h:279
A 3 element vector with single doubles.
Definition xrt_defines.h:322
A 3 element vector with single floats.
Definition xrt_defines.h:310