Monado OpenXR Runtime
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 
22 namespace xrt::auxiliary::math {
23 
24 /*!
25  * @brief Wrap an internal quaternion struct in an Eigen type, const overload.
26  *
27  * Permits zero-overhead manipulation of `const xrt_quat&` by Eigen routines as
28  * if it were a `const Eigen::Quaternionf&`.
29  */
30 static inline Eigen::Map<const Eigen::Quaternionf>
31 map_quat(const struct xrt_quat &q)
32 {
33  return Eigen::Map<const Eigen::Quaternionf>{&q.x};
34 }
35 
36 /*!
37  * @brief Wrap an internal quaternion struct in an Eigen type, non-const
38  * overload.
39  *
40  * Permits zero-overhead manipulation of `xrt_quat&` by Eigen routines as if it
41  * were a `Eigen::Quaternionf&`.
42  */
43 static inline Eigen::Map<Eigen::Quaternionf>
44 map_quat(struct xrt_quat &q)
45 {
46  return Eigen::Map<Eigen::Quaternionf>{&q.x};
47 }
48 
49 
50 /*!
51  * @brief Wrap an internal 3D vector struct in an Eigen type, const overload.
52  *
53  * Permits zero-overhead manipulation of `const xrt_vec3&` by Eigen routines as
54  * if it were a `const Eigen::Vector3f&`.
55  */
56 static inline Eigen::Map<const Eigen::Vector3f>
57 map_vec3(const struct xrt_vec3 &v)
58 {
59  return Eigen::Map<const Eigen::Vector3f>{&v.x};
60 }
61 
62 /*!
63  * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
64  * overload.
65  *
66  * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
67  * if it were a `Eigen::Vector3f&`.
68  */
69 static inline Eigen::Map<Eigen::Vector3f>
70 map_vec3(struct xrt_vec3 &v)
71 {
72  return Eigen::Map<Eigen::Vector3f>{&v.x};
73 }
74 
75 /*!
76  * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
77  * overload.
78  *
79  * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
80  * if it were a `const Eigen::Vector3_f64&`.
81  */
82 static inline Eigen::Map<const Eigen::Vector3d>
83 map_vec3_f64(const struct xrt_vec3_f64 &v)
84 {
85  return Eigen::Map<const Eigen::Vector3d>{&v.x};
86 }
87 
88 /*!
89  * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
90  * overload.
91  *
92  * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
93  * if it were a `Eigen::Vector3_f64&`.
94  */
95 static inline Eigen::Map<Eigen::Vector3d>
97 {
98  return Eigen::Map<Eigen::Vector3d>{&v.x};
99 }
100 
101 /*!
102  * @brief Wrap an internal 3x3 matrix struct in an Eigen type, const overload.
103  *
104  * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
105  * if it were a `Eigen::Matrix3f&`.
106  */
107 static inline Eigen::Map<const Eigen::Matrix3f>
109 {
110  return Eigen::Map<const Eigen::Matrix3f>(m.v);
111 }
112 
113 /*!
114  * @brief Wrap an internal 3x3 matrix struct in an Eigen type, non-const
115  * overload.
116  *
117  * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
118  * if it were a `Eigen::Matrix3f&`.
119  */
120 static inline Eigen::Map<Eigen::Matrix3f>
122 {
123  return Eigen::Map<Eigen::Matrix3f>(m.v);
124 }
125 
126 /*!
127  * @brief Wrap an internal 3x3 matrix struct in an Eigen type, non-const
128  * overload.
129  *
130  * Permits zero-overhead manipulation of `xrt_matrix_3x3&` by Eigen routines as
131  * if it were a `Eigen::Matrix3_f64&`.
132  */
133 static inline Eigen::Map<Eigen::Matrix3d>
135 {
136  return Eigen::Map<Eigen::Matrix3d>(m.v);
137 }
138 
139 /*!
140  * @brief Wrap an internal 4x4 matrix struct in an Eigen type, const
141  * overload.
142  *
143  * Permits zero-overhead manipulation of `xrt_matrix_4x4&` by Eigen routines as
144  * if it were a `Eigen::Matrix4f&`.
145  */
146 static inline Eigen::Map<const Eigen::Matrix4f>
148 {
149  return Eigen::Map<const Eigen::Matrix4f>(m.v);
150 }
151 
152 /*!
153  * @brief Wrap an internal 4x4 matrix struct in an Eigen type, non-const
154  * overload.
155  *
156  * Permits zero-overhead manipulation of `xrt_matrix_4x4&` by Eigen routines as
157  * if it were a `Eigen::Matrix4f&`.
158  */
159 static inline Eigen::Map<Eigen::Matrix4f>
161 {
162  return Eigen::Map<Eigen::Matrix4f>(m.v);
163 }
164 
165 /*!
166  * @brief Wrap an internal 4x4 matrix f64 struct in an Eigen type, const overload.
167  *
168  * Permits zero-overhead manipulation of `const xrt_matrix_4x4_f64&` by Eigen routines as if it were a
169  * `const Eigen::Matrix4d&`.
170  */
171 static inline Eigen::Map<const Eigen::Matrix4d>
173 {
174  return Eigen::Map<const Eigen::Matrix4d>(m.v);
175 }
176 
177 /*!
178  * @brief Wrap an internal 4x4 matrix struct in an Eigen type, non-const overload.
179  *
180  * Permits zero-overhead manipulation of `xrt_matrix_4x4_f64&` by Eigen routines as if it were a `Eigen::Matrix4d&`.
181  */
182 static inline Eigen::Map<Eigen::Matrix4d>
184 {
185  return Eigen::Map<Eigen::Matrix4d>(m.v);
186 }
187 
188 
189 /*
190  *
191  * Pose deconstruction helpers.
192  *
193  */
194 
195 /*!
196  * Return a Eigen type wrapping a pose's orientation (const).
197  */
198 static inline Eigen::Map<const Eigen::Quaternionf>
199 orientation(const struct xrt_pose &pose)
200 {
201  return map_quat(pose.orientation);
202 }
203 
204 /*!
205  * Return a Eigen type wrapping a pose's orientation.
206  */
207 static inline Eigen::Map<Eigen::Quaternionf>
208 orientation(struct xrt_pose &pose)
209 {
210  return map_quat(pose.orientation);
211 }
212 
213 /*!
214  * Return a Eigen type wrapping a pose's position (const).
215  */
216 static inline Eigen::Map<const Eigen::Vector3f>
217 position(const struct xrt_pose &pose)
218 {
219  return map_vec3(pose.position);
220 }
221 
222 /*!
223  * Return a Eigen type wrapping a pose's position.
224  */
225 static inline Eigen::Map<Eigen::Vector3f>
226 position(struct xrt_pose &pose)
227 {
228  return map_vec3(pose.position);
229 }
230 
231 } // 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::Vector3f > position(const struct xrt_pose &pose)
Return a Eigen type wrapping a pose's position (const).
Definition: m_eigen_interop.hpp:217
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:172
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:199
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:134
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:31
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:57
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:147
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:83
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:108
A tightly packed 3x3 matrix of doubles.
Definition: xrt_defines.h:543
A tightly packed 3x3 matrix of floats.
Definition: xrt_defines.h:533
A tightly packed 4x4 matrix of double.
Definition: xrt_defines.h:570
A tightly packed 4x4 matrix of floats.
Definition: xrt_defines.h:560
A pose composed of a position and orientation.
Definition: xrt_defines.h:465
A quaternion with single floats.
Definition: xrt_defines.h:216
A 3 element vector with single doubles.
Definition: xrt_defines.h:283
A 3 element vector with single floats.
Definition: xrt_defines.h:271