OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
EigenExtras.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_EigenExtras_h_GUID_7AE6CABA_333B_408A_C898_A2CBBE5BCE5D
26 #define INCLUDED_EigenExtras_h_GUID_7AE6CABA_333B_408A_C898_A2CBBE5BCE5D
27 
28 // Internal Includes
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <type_traits>
36 
37 namespace osvr {
38 namespace util {
39 
40  using ColMatrix44d = Eigen::Matrix4d;
41  using ColVector3d = Eigen::Vector3d;
42  using ColVector4d = Eigen::Vector4d;
43  using RowMatrix44d = Eigen::Matrix<double, 4, 4, Eigen::RowMajor>;
44  using Eigen::RowVector3d;
45  using Eigen::RowVector4d;
46 
47  template <typename Scalar>
48  using Isometry3 = Eigen::Transform<Scalar, 3, Eigen::Isometry>;
49  template <typename Scalar>
50  using Translation3 = Eigen::Translation<Scalar, 3>;
51 
52  namespace detail {
53  template <int Size, typename PrototypeVector>
54  using SameLayoutVector =
56  PrototypeVector::IsRowMajor ? Size : 1,
57  PrototypeVector::IsRowMajor ? 1 : Size>;
58  } // namespace detail
59 
62  template <typename Derived>
63  inline detail::SameLayoutVector<4, Derived>
64  makeHomogeneousPoint(Eigen::MatrixBase<Derived> const &vec) {
65  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
66  return (detail::SameLayoutVector<4, Derived>() << vec, 1).finished();
67  }
68 
71  template <typename Derived>
72  inline detail::SameLayoutVector<4, Derived>
73  makeHomogeneousVector(Eigen::MatrixBase<Derived> const &vec) {
74  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
75  return (detail::SameLayoutVector<4, Derived>() << vec, 0).finished();
76  }
77 
80  template <typename Derived>
81  inline detail::SameLayoutVector<3, Derived>
82  extractPoint(Eigen::MatrixBase<Derived> const &homogenous) {
83  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 4);
84  return homogenous[3] == 0
85  ? homogenous.template head<3>().eval()
86  : (homogenous.template head<3>() / homogenous[3]).eval();
87  }
88 
91  template <typename Derived1, typename Derived2>
92  inline Isometry3<typename Derived1::Scalar>
93  makeIsometry(Eigen::MatrixBase<Derived1> const &translation,
94  Eigen::RotationBase<Derived2, 3> const &rotation) {
95  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived1, 3);
96  static_assert(
97  std::is_same<typename Derived1::Scalar,
98  typename Derived2::Scalar>::value,
99  "Translation and rotation do not have the same scalar type.");
100  using Scalar = typename Derived1::Scalar;
101  return Translation3<Scalar>(translation) * Isometry3<Scalar>(rotation);
102  }
103 
104  inline Eigen::Quaterniond
105  flipQuatSignToMatch(Eigen::Quaterniond const &refQ,
106  Eigen::Quaterniond const &q) {
107  if (refQ.dot(q) < 0) {
108  return Eigen::Quaterniond(-q.coeffs());
109  } else {
110  return q;
111  }
112  }
113 
114 } // namespace util
115 } // namespace osvr
116 
117 #endif // INCLUDED_EigenExtras_h_GUID_7AE6CABA_333B_408A_C898_A2CBBE5BCE5D
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
Header wrapping include of and for warning quieting.
detail::SameLayoutVector< 3, Derived > extractPoint(Eigen::MatrixBase< Derived > const &homogenous)
Pulls the 3D point or vector from a 4D vec, performing division by w if it is nonzero.
Definition: EigenExtras.h:82
Isometry3< typename Derived1::Scalar > makeIsometry(Eigen::MatrixBase< Derived1 > const &translation, Eigen::RotationBase< Derived2, 3 > const &rotation)
A simpler, functional-style alternative to .fromPositionOrientationScale when no scaling is performed...
Definition: EigenExtras.h:93
detail::SameLayoutVector< 4, Derived > makeHomogeneousPoint(Eigen::MatrixBase< Derived > const &vec)
Makes a 3D vector into a 4D homogeneous point, with the same options (scalar, row vs col vector) as t...
Definition: EigenExtras.h:64
detail::SameLayoutVector< 4, Derived > makeHomogeneousVector(Eigen::MatrixBase< Derived > const &vec)
Makes a 3D vector into a 4D homogeneous vector, with the same options (scalar, row vs col vector) as ...
Definition: EigenExtras.h:73
double Scalar
Common scalar type.
Eigen::Matrix< Scalar, m, n > Matrix
A matrix with rows = m, cols = n.