OSVR-Core
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;
44  using Eigen::RowVector3d;
45  using Eigen::RowVector4d;
46 
47  template <typename Scalar>
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>
65  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
66  return (detail::SameLayoutVector<4, Derived>() << vec, 1).finished();
67  }
68 
71  template <typename Derived>
74  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
75  return (detail::SameLayoutVector<4, Derived>() << vec, 0).finished();
76  }
77 
80  template <typename Derived>
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>
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
Definition: RunLoopManager.h:42
const Coefficients & coeffs() const
Definition: Quaternion.h:93
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Scalar dot(const QuaternionBase< OtherDerived > &other) const
Definition: Quaternion.h:133
Header wrapping include of <Eigen/Core> and <Eigen/Geometry> for warning quieting.
Definition: ForwardDeclarations.h:236
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
Definition: newuoa.h:1888
Common base class for compact rotation representations.
Definition: ForwardDeclarations.h:231
Definition: Quaternion.h:47
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
Quaternion< double > Quaterniond
double precision quaternion type
Definition: Quaternion.h:211
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: Transform.h:43
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48