OSVR-Core
EigenInterop.h
Go to the documentation of this file.
1 
12 // Copyright 2014 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 #ifndef INCLUDED_EigenInterop_h_GUID_A07DFDC3_BA71_4894_6593_732B364A2AE7
27 #define INCLUDED_EigenInterop_h_GUID_A07DFDC3_BA71_4894_6593_732B364A2AE7
28 
29 // Internal Includes
30 #include <osvr/Util/Pose3C.h>
31 
32 // Library/third-party includes
34 #include <osvr/Util/EigenExtras.h>
35 
36 // Standard includes
37 // - none
38 
39 namespace osvr {
40 namespace util {
41 
53  return Eigen::Map<Eigen::Vector3d>(&(vec.data[0]));
54  }
55 
59  return Eigen::Map<const Eigen::Vector3d>(&(vec.data[0]));
60  }
61 
64  return Eigen::Quaterniond(osvrQuatGetW(&q), osvrQuatGetX(&q),
65  osvrQuatGetY(&q), osvrQuatGetZ(&q));
66  }
67 
69  inline void toQuat(Eigen::Quaterniond const &src, OSVR_Quaternion &q) {
70  osvrQuatSetW(&q, src.w());
71  osvrQuatSetX(&q, src.x());
72  osvrQuatSetY(&q, src.y());
73  osvrQuatSetZ(&q, src.z());
74  }
75 
81  inline Eigen::Isometry3d fromPose(OSVR_Pose3 const &pose) {
82  Eigen::Isometry3d newPose;
83  newPose.fromPositionOrientationScale(vecMap(pose.translation),
84  fromQuat(pose.rotation),
85  Eigen::Vector3d::Constant(1));
86  return newPose;
87  }
88 
93  inline void toPose(Eigen::Isometry3d const &xform, OSVR_Pose3 &pose) {
94  vecMap(pose.translation) = xform.translation();
95  Eigen::Quaterniond quat(xform.rotation());
96  toQuat(quat, pose.rotation);
97  }
98 
104  inline void toPose(Eigen::Matrix4d const &mat, OSVR_Pose3 &pose) {
105  Eigen::Affine3d xform(mat);
106  vecMap(pose.translation) = xform.translation();
107  Eigen::Quaterniond quat(xform.rotation());
108  toQuat(quat, pose.rotation);
109  }
110 
114  namespace eigen_interop {
116  const_map(OSVR_Vec3 const &vec) {
117  return vecMap(vec);
118  }
119 
120  inline Eigen::Map<Eigen::Vector3d> map(OSVR_Vec3 &vec) {
121  return vecMap(vec);
122  }
123 
124  inline Eigen::Map<const Eigen::Vector3d> map(OSVR_Vec3 const &vec) {
125  return const_map(vec);
126  }
127  namespace detail {
128  template <char Coefficient> struct QuatAccessor;
129  template <> struct QuatAccessor<'W'> {
130  static double get(OSVR_Quaternion const &q) {
131  return osvrQuatGetW(&q);
132  }
133 
134  static void set(OSVR_Quaternion &q, double v) {
135  return osvrQuatSetW(&q, v);
136  }
137  };
138  template <> struct QuatAccessor<'X'> {
139  static double get(OSVR_Quaternion const &q) {
140  return osvrQuatGetX(&q);
141  }
142 
143  static void set(OSVR_Quaternion &q, double v) {
144  return osvrQuatSetX(&q, v);
145  }
146  };
147 
148  template <> struct QuatAccessor<'Y'> {
149  static double get(OSVR_Quaternion const &q) {
150  return osvrQuatGetY(&q);
151  }
152 
153  static void set(OSVR_Quaternion &q, double v) {
154  return osvrQuatSetY(&q, v);
155  }
156  };
157 
158  template <> struct QuatAccessor<'Z'> {
159  static double get(OSVR_Quaternion const &q) {
160  return osvrQuatGetZ(&q);
161  }
162 
163  static void set(OSVR_Quaternion &q, double v) {
164  return osvrQuatSetZ(&q, v);
165  }
166  };
167 
168  template <char Coefficient, typename T = OSVR_Quaternion>
170  public:
171  QuatCoefficientMap(T &quat) : m_quat(&quat) {}
172  operator double() const {
173  return QuatAccessor<Coefficient>::get(*m_quat);
174  }
175  QuatCoefficientMap const &operator=(double v) const {
176  QuatAccessor<Coefficient>::set(*m_quat, v);
177  return *this;
178  }
179 
180  private:
181  T *m_quat;
182  };
183  template <typename T = OSVR_Quaternion const> class BaseQuatMap {
184  public:
185  BaseQuatMap(T &quat) : m_quat(&quat) {}
186 #if 0
187  BaseQuatMap(BaseQuatMap const&) = delete;
188  BaseQuatMap(BaseQuatMap && other) : m_quat(other.m_quat) {}
189 #endif
190 
193  Eigen::Quaterniond quat() const { return fromQuat(*m_quat); }
194 
197  operator Eigen::Quaterniond() const { return quat(); }
198 
200  double w() const { return osvrQuatGetW(m_quat); }
202  double x() const { return osvrQuatGetX(m_quat); }
204  double y() const { return osvrQuatGetY(m_quat); }
206  double z() const { return osvrQuatGetZ(m_quat); }
207 
208  protected:
209  T *m_quat;
210  };
211 
212  typedef BaseQuatMap<> ConstQuatMap;
213  class QuatMap : public BaseQuatMap<OSVR_Quaternion> {
214  public:
215  QuatMap(OSVR_Quaternion &quat) : BaseQuatMap(quat) {}
216 #if 0
217  QuatMap(QuatMap const&) = delete;
218  QuatMap(QuatMap && other) : BaseQuatMap(std::move(other)) {}
219 #endif
220  template <typename T>
222  QuatMap const &operator=(BaseQuatMap<T> const &other) const {
223  *m_quat = *other.m_quat;
224  return *this;
225  }
227  template <typename Derived>
228  QuatMap const &
230  osvrQuatSetW(m_quat, other.w());
231  osvrQuatSetX(m_quat, other.x());
232  osvrQuatSetY(m_quat, other.y());
233  osvrQuatSetZ(m_quat, other.z());
234  return *this;
235  }
236 
239  return QuatCoefficientMap<'W'>(*m_quat);
240  }
243  return QuatCoefficientMap<'X'>(*m_quat);
244  }
247  return QuatCoefficientMap<'Y'>(*m_quat);
248  }
251  return QuatCoefficientMap<'Z'>(*m_quat);
252  }
253  };
254  } // namespace detail
255 
256  inline detail::ConstQuatMap const_map(OSVR_Quaternion const &quat) {
257  return detail::ConstQuatMap(quat);
258  }
259 
260  inline detail::ConstQuatMap map(OSVR_Quaternion const &quat) {
261  return const_map(quat);
262  }
263 
264  inline detail::QuatMap map(OSVR_Quaternion &quat) {
265  return detail::QuatMap(quat);
266  }
267 
268  namespace detail {
269  template <typename T = OSVR_Pose3 const> class BasePoseMap {
270  public:
271  BasePoseMap(T &pose) : m_pose(&pose) {}
272 #if 0
273  BasePoseMap(BasePoseMap const&) = delete;
274  BasePoseMap(BasePoseMap && other) : m_pose(other.m_pose) {}
275 #endif
277  typedef typename TransformType::MatrixType MatrixType;
278 
280  TransformType transform() const { return fromPose(*m_pose); }
283  operator TransformType() const { return transform(); }
284 
286  MatrixType matrix() const { return fromPose(*m_pose).matrix(); }
287 
291  return const_map(m_pose->translation);
292  }
296  return const_map(m_pose->rotation);
297  }
298 
299  protected:
300  T *m_pose;
301  };
302 
303  typedef BasePoseMap<> ConstPoseMap;
304 
305  class PoseMap : public BasePoseMap<OSVR_Pose3> {
306  public:
307  PoseMap(OSVR_Pose3 &pose) : BasePoseMap(pose) {}
308 #if 0
309  PoseMap(PoseMap const&) = delete;
310  PoseMap(PoseMap && other) : BasePoseMap(std::move(other)) {}
311 #endif
312  template <typename T>
314  PoseMap const &operator=(BasePoseMap<T> const &other) const {
315  *m_pose = *other.m_pose;
316  return *this;
317  }
318 
320  PoseMap const &operator=(Eigen::Isometry3d const &other) const {
321  toPose(other, *m_pose);
322  return *this;
323  }
324 
327  PoseMap const &operator=(Eigen::Matrix4d const &other) const {
328  toPose(other, *m_pose);
329  return *this;
330  }
331 
335  return map(m_pose->translation);
336  }
337 
340  QuatMap rotation() const { return map(m_pose->rotation); }
341  };
342  } // namespace detail
343 
344  inline detail::ConstPoseMap const_map(OSVR_Pose3 const &pose) {
345  return detail::ConstPoseMap(pose);
346  }
347 
348  inline detail::ConstPoseMap map(OSVR_Pose3 const &pose) {
349  return const_map(pose);
350  }
351 
352  inline detail::PoseMap map(OSVR_Pose3 &pose) {
353  return detail::PoseMap(pose);
354  }
355  } // namespace eigen_interop
358 } // namespace util
359 } // namespace osvr
360 #endif // INCLUDED_EigenInterop_h_GUID_A07DFDC3_BA71_4894_6593_732B364A2AE7
Scalar z() const
Definition: Quaternion.h:64
Definition: RunLoopManager.h:42
ConstTranslationPart translation() const
Definition: Transform.h:153
QuatCoefficientMap< 'X'> x() const
Read-write accessor for the x component of the quaternion.
Definition: EigenInterop.h:242
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:104
TransformType transform() const
Read-only accessor for the pose as an Eigen transform.
Definition: EigenInterop.h:280
A structure defining a 3D vector, often a position/translation.
Definition: Vec3C.h:48
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
MatrixType matrix() const
Read-only accessor for the pose as an Eigen Matrix.
Definition: EigenInterop.h:286
OSVR_Quaternion rotation
Orientation as a unit quaternion.
Definition: Pose3C.h:58
const MatrixType & matrix() const
Definition: Transform.h:143
Scalar y() const
Definition: Quaternion.h:71
Header wrapping include of <Eigen/Core> and <Eigen/Geometry> for warning quieting.
A structure defining a quaternion, often a unit quaternion representing 3D rotation.
Definition: QuaternionC.h:49
void toPose(Eigen::Isometry3d const &xform, OSVR_Pose3 &pose)
Turn an Eigen::Isometry3d (transform) into an OSVR_Pose3.
Definition: EigenInterop.h:93
Eigen::Map< Eigen::Vector3d > vecMap(OSVR_Vec3 &vec)
Wrap an OSVR_Vec3 in an Eigen object that allows it to interoperate with Eigen as though it were an E...
Definition: EigenInterop.h:52
Eigen::Isometry3d fromPose(OSVR_Pose3 const &pose)
Turn an OSVR_Pose3 into an Eigen::Transform.
Definition: EigenInterop.h:81
PoseMap const & operator=(BasePoseMap< T > const &other) const
Assignment operator from another PoseMap (const or not)
Definition: EigenInterop.h:314
double z() const
Read-only accessor for the z component of the quaternion.
Definition: EigenInterop.h:206
Scalar x() const
Definition: Quaternion.h:60
Definition: EigenInterop.h:213
double data[3]
Internal array data.
Definition: Vec3C.h:50
Scalar x() const
Definition: Quaternion.h:69
QuatCoefficientMap< 'W'> w() const
Read-write accessor for the w component of the quaternion.
Definition: EigenInterop.h:238
LinearMatrixType rotation() const
Definition: Transform.h:598
PoseMap const & operator=(Eigen::Isometry3d const &other) const
Assignment operator from an Eigen isometry.
Definition: EigenInterop.h:320
Definition: newuoa.h:1888
t_< detail::transform_< List, Fun >> transform
Given a list and an alias class, apply the alias class to each element in the list and return the res...
Definition: Transform.h:54
QuatCoefficientMap< 'Y'> y() const
Read-write accessor for the y component of the quaternion.
Definition: EigenInterop.h:246
QuatMap const & operator=(BaseQuatMap< T > const &other) const
Assignment from another QuatMap (whether const or not)
Definition: EigenInterop.h:222
double w() const
Read-only accessor for the w component of the quaternion.
Definition: EigenInterop.h:200
PoseMap const & operator=(Eigen::Matrix4d const &other) const
Assignment operator from an Eigen 4x4 matrix - note that it is assumed to be an isometry! ...
Definition: EigenInterop.h:327
QuatMap rotation() const
Read-write accessor for the rotation portion of the pose as an Eigen quaternion (map) ...
Definition: EigenInterop.h:340
Definition: ForwardDeclarations.h:233
QuatCoefficientMap< 'Z'> z() const
Read-write accessor for the z component of the quaternion.
Definition: EigenInterop.h:250
Scalar w() const
Definition: Quaternion.h:75
Eigen::Map< Eigen::Vector3d > translation() const
Read-write accessor for the translation portion of the pose as an Eigen vector (map) ...
Definition: EigenInterop.h:334
Eigen::Quaterniond fromQuat(OSVR_Quaternion const &q)
Convert an OSVR_Quaternion to an Eigen::Quaterniond.
Definition: EigenInterop.h:63
Definition: Quaternion.h:47
Eigen::Quaterniond quat() const
Read-only accessor for the quaternion as an Eigen quaternion.
Definition: EigenInterop.h:193
A structure defining a 3D (6DOF) rigid body pose: translation and rotation.
Definition: Pose3C.h:54
Scalar z() const
Definition: Quaternion.h:73
void toQuat(Eigen::Quaterniond const &src, OSVR_Quaternion &q)
Convert an Eigen::Quaterniond to a OSVR_Quaternion.
Definition: EigenInterop.h:69
ConstQuatMap rotation() const
Read-only accessor for the translation as an Eigen quaternion.
Definition: EigenInterop.h:295
Quaternion< double > Quaterniond
double precision quaternion type
Definition: Quaternion.h:211
Eigen::Map< const Eigen::Vector3d > translation() const
Read-only accessor for the translation as an Eigen vector map.
Definition: EigenInterop.h:290
Scalar y() const
Definition: Quaternion.h:62
Header.
QuatMap const & operator=(Eigen::QuaternionBase< Derived > const &other) const
Assignment operator from an Eigen quaternion.
Definition: EigenInterop.h:229
double y() const
Read-only accessor for the y component of the quaternion.
Definition: EigenInterop.h:204
OSVR_Vec3 translation
Position vector.
Definition: Pose3C.h:56
Definition: EigenInterop.h:305
Definition: Transform.h:43
Scalar w() const
Definition: Quaternion.h:66
double x() const
Read-only accessor for the x component of the quaternion.
Definition: EigenInterop.h:202