Monado OpenXR Runtime
m_eigen_interop.hpp
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Interoperability helpers connecting internal math types and Eigen.
6  * @author Ryan Pavlik <ryan.pavlik@collabora.com>
7  * @ingroup aux_math
8  */
9 
10 #pragma once
11 
12 #ifndef __cplusplus
13 #error "This header only usable from C++"
14 #endif
15 
16 #include "math/m_api.h"
17 
18 #include <Eigen/Core>
19 #include <Eigen/Geometry>
20 
21 
22 /*!
23  * @brief Wrap an internal quaternion struct in an Eigen type, const overload.
24  *
25  * Permits zero-overhead manipulation of `const xrt_quat&` by Eigen routines as
26  * if it were a `const Eigen::Quaternionf&`.
27  */
28 static inline Eigen::Map<const Eigen::Quaternionf>
29 map_quat(const struct xrt_quat &q)
30 {
31  return Eigen::Map<const Eigen::Quaternionf>{&q.x};
32 }
33 
34 /*!
35  * @brief Wrap an internal quaternion struct in an Eigen type, non-const
36  * overload.
37  *
38  * Permits zero-overhead manipulation of `xrt_quat&` by Eigen routines as if it
39  * were a `Eigen::Quaternionf&`.
40  */
41 static inline Eigen::Map<Eigen::Quaternionf>
42 map_quat(struct xrt_quat &q)
43 {
44  return Eigen::Map<Eigen::Quaternionf>{&q.x};
45 }
46 
47 
48 /*!
49  * @brief Wrap an internal 3D vector struct in an Eigen type, const overload.
50  *
51  * Permits zero-overhead manipulation of `const xrt_vec3&` by Eigen routines as
52  * if it were a `const Eigen::Vector3f&`.
53  */
54 static inline Eigen::Map<const Eigen::Vector3f>
55 map_vec3(const struct xrt_vec3 &v)
56 {
57  return Eigen::Map<const Eigen::Vector3f>{&v.x};
58 }
59 
60 /*!
61  * @brief Wrap an internal 3D vector struct in an Eigen type, non-const
62  * overload.
63  *
64  * Permits zero-overhead manipulation of `xrt_vec3&` by Eigen routines as
65  * if it were a `Eigen::Vector3f&`.
66  */
67 static inline Eigen::Map<Eigen::Vector3f>
68 map_vec3(struct xrt_vec3 &v)
69 {
70  return Eigen::Map<Eigen::Vector3f>{&v.x};
71 }
72 
73 /*
74  *
75  * Pose deconstruction helpers.
76  *
77  */
78 
79 /*!
80  * Return a Eigen type wrapping a pose's orientation (const).
81  */
82 static inline Eigen::Map<const Eigen::Quaternionf>
83 orientation(const struct xrt_pose &pose)
84 {
85  return map_quat(pose.orientation);
86 }
87 
88 /*!
89  * Return a Eigen type wrapping a pose's orientation.
90  */
91 static inline Eigen::Map<Eigen::Quaternionf>
92 orientation(struct xrt_pose &pose)
93 {
94  return map_quat(pose.orientation);
95 }
96 
97 /*!
98  * Return a Eigen type wrapping a pose's position (const).
99  */
100 static inline Eigen::Map<const Eigen::Vector3f>
101 position(const struct xrt_pose &pose)
102 {
103  return map_vec3(pose.position);
104 }
105 
106 /*!
107  * Return a Eigen type wrapping a pose's position.
108  */
109 static inline Eigen::Map<Eigen::Vector3f>
110 position(struct xrt_pose &pose)
111 {
112  return map_vec3(pose.position);
113 }
A 3 element vector with single floats.
Definition: xrt_defines.h:131
A pose composed of a position and orientation.
Definition: xrt_defines.h:229
A quaternion with single floats.
Definition: xrt_defines.h:97
C interface to math library.