OSVR-Core
Transform.h
Go to the documentation of this file.
1 
11 // Copyright 2014 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_Transform_h_GUID_8BF4BBD8_CDC1_48BC_DC27_BFDA42A3212E
26 #define INCLUDED_Transform_h_GUID_8BF4BBD8_CDC1_48BC_DC27_BFDA42A3212E
27 
28 // Internal Includes
31 
32 // Library/third-party includes
34 
35 // Standard includes
36 // - none
37 
38 namespace osvr {
40 namespace common {
41 
44  class Transform {
45  public:
46  Transform()
47  : m_pre(Eigen::Matrix4d::Identity()),
48  m_post(Eigen::Matrix4d::Identity()) {}
49 
50  template <typename T1, typename T2>
51  Transform(T1 const &pre_matrix, T2 const &post_matrix)
52  : m_pre(pre_matrix), m_post(post_matrix) {}
53 
54  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
55 
56  template <typename T> void concatPre(T const &xform) { m_pre *= xform; }
57 
58  template <typename T> void concatPost(T const &xform) {
59  m_post = (xform * m_post).eval();
60  }
61 
64  void transform(Transform const &other) {
65  concatPre(other.m_pre);
66  concatPost(other.m_post);
67  }
68 
70  Eigen::Matrix4d transform(Eigen::Matrix4d const &input) const {
71  return m_post * input * m_pre;
72  }
73 
76  Eigen::Vector3d
78  return transformDerivativeImpl(Eigen::Translation3d(vec))
79  .translation();
80  }
81 
85  return Eigen::Quaterniond(transformDerivativeImpl(quat).rotation());
86  }
87 
88  Eigen::Matrix4d const &getPre() const { return m_pre; }
89 
90  Eigen::Matrix4d const &getPost() const { return m_post; }
91 
92  private:
94  template <typename T>
95  Eigen::Isometry3d transformDerivativeImpl(T const &input) const {
96  // Because this is a differential transform, it is both to and
97  // from the same space. That is to say that its left and right
98  // sides both represent its "to" space, and it has no "from"
99  // space. Thus, we can ignore its pre-transforms. But we want
100  // a differential transform in world space, the space on the other
101  // side of its pre transform. So we end up inverting the post-
102  // transform and applying it where the pre-transform would
103  // normally go. This makes the resulting matrix be world space
104  // on both sides.
105  Eigen::Isometry3d post =
106  Eigen::Isometry3d(m_post.topLeftCorner<3, 3>());
107  return Eigen::Isometry3d(post * input * post.inverse());
108  }
109  Eigen::Matrix4d m_pre;
110  Eigen::Matrix4d m_post;
111  };
112 
113  template <typename T>
114  inline Eigen::Matrix4d rotate(double degrees, T const &axis) {
115  return Eigen::Isometry3d(
116  Eigen::AngleAxisd(degreesToRadians(degrees), axis))
117  .matrix();
118  }
119 
120 } // namespace common
121 } // namespace osvr
122 
123 #endif // INCLUDED_Transform_h_GUID_8BF4BBD8_CDC1_48BC_DC27_BFDA42A3212E
void transform(Transform const &other)
Update this transformation by the application of another transformation around it.
Definition: Transform.h:64
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
ConstTranslationPart translation() const
Definition: Transform.h:153
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
const MatrixType & matrix() const
Definition: Transform.h:143
Eigen::Quaterniond transformDerivative(Eigen::Quaterniond const &quat)
Transform a rotational derivative: angular velocity or acceleration.
Definition: Transform.h:84
Header wrapping include of <Eigen/Core> and <Eigen/Geometry> for warning quieting.
Definition: ForwardDeclarations.h:236
A matrix or vector expression mapping an existing expressions.
Definition: Ref.h:17
Definition: Quaternion.h:47
Eigen::Vector3d transformDerivative(Eigen::Ref< Eigen::Vector3d const > const &vec)
Apply only the rotation/basis change (not the translation) to a vector representing a velocity or acc...
Definition: Transform.h:77
Eigen::Matrix4d transform(Eigen::Matrix4d const &input) const
Apply the transformation to a matrix representing a pose.
Definition: Transform.h:70
Quaternion< double > Quaterniond
double precision quaternion type
Definition: Quaternion.h:211
Spatial transformation, consisting of both pre and post components.
Definition: Transform.h:44
Definition: ForwardDeclarations.h:235
Definition: Transform.h:43
const MatrixType inverse(TransformTraits traits=Affine) const
Definition: Transform.h:706