Apollo  6.0
Open source self driving car software
quaternion.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
26 #pragma once
27 
28 #include <cmath>
29 
30 #include "Eigen/Dense"
31 #include "Eigen/Geometry"
32 
35 #include "modules/common/proto/geometry.pb.h"
36 
41 namespace apollo {
42 namespace common {
43 namespace math {
44 
45 /*
46  * @brief Returns heading (in radians) in [-PI, PI), with 0 being East.
47  * Note that x/y/z is East/North/Up.
48  *
49  * @param qw Quaternion w-coordinate
50  * @param qx Quaternion x-coordinate
51  * @param qy Quaternion y-coordinate
52  * @param qz Quaternion z-coordinate
53  *
54  * @return Heading encoded by given quaternion
55  */
56 inline double QuaternionToHeading(const double qw, const double qx,
57  const double qy, const double qz) {
58  EulerAnglesZXYd euler_angles(qw, qx, qy, qz);
59  // euler_angles.yaw() is zero when the car is pointing North, but
60  // the heading is zero when the car is pointing East.
61  return NormalizeAngle(euler_angles.yaw() + M_PI_2);
62 }
63 
64 /*
65  * @brief Returns heading (in radians) in [-PI, PI), with 0 being East.
66  * Note that x/y/z is East/North/Up.
67  *
68  * @param q Eigen::Quaternion
69  *
70  * @return Heading encoded by given quaternion
71  */
72 template <typename T>
73 inline T QuaternionToHeading(const Eigen::Quaternion<T> &q) {
74  return static_cast<T>(QuaternionToHeading(q.w(), q.x(), q.y(), q.z()));
75 }
76 
77 /*
78  * @brief Returns a quaternion encoding a rotation with zero roll, zero pitch,
79  * and the specified heading/yaw.
80  * Note that heading is zero at East and yaw is zero at North.
81  * Satisfies QuaternionToHeading(HeadingToQuaternion(h)) = h.
82  *
83  * @param heading The heading to encode in the rotation
84  *
85  * @return Quaternion encoding rotation by given heading
86  */
87 template <typename T>
88 inline Eigen::Quaternion<T> HeadingToQuaternion(T heading) {
89  // Note that heading is zero at East and yaw is zero at North.
90  EulerAnglesZXY<T> euler_angles(heading - M_PI_2);
91  return euler_angles.ToQuaternion();
92 }
93 
94 /*
95  * @brief Applies the rotation defined by a quaternion to a given vector.
96  * Note that x/y/z is East/North/Up.
97  *
98  * @param orientation Quaternion
99  * @param original Vector (in East-North-Up frame)
100  *
101  * @return Rotated vector
102  */
103 inline Eigen::Vector3d QuaternionRotate(const Quaternion &orientation,
104  const Eigen::Vector3d &original) {
105  Eigen::Quaternion<double> quaternion(orientation.qw(), orientation.qx(),
106  orientation.qy(), orientation.qz());
107  return static_cast<Eigen::Vector3d>(quaternion.toRotationMatrix() * original);
108 }
109 
110 inline Eigen::Vector3d InverseQuaternionRotate(const Quaternion &orientation,
111  const Eigen::Vector3d &rotated) {
112  Eigen::Quaternion<double> quaternion(orientation.qw(), orientation.qx(),
113  orientation.qy(), orientation.qz());
114  return static_cast<Eigen::Vector3d>(quaternion.toRotationMatrix().inverse() *
115  rotated);
116 }
117 
118 } // namespace math
119 } // namespace common
120 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Eigen::Quaternion< T > ToQuaternion() const
Converts to a quaternion with a non-negative scalar part.
Definition: euler_angles_zxy.h:154
double NormalizeAngle(const double angle)
Normalize angle to [-PI, PI).
Eigen::Vector3d Vector3d
Definition: frame_transform.h:27
Eigen::Vector3d InverseQuaternionRotate(const Quaternion &orientation, const Eigen::Vector3d &rotated)
Definition: quaternion.h:110
double QuaternionToHeading(const double qw, const double qx, const double qy, const double qz)
Definition: quaternion.h:56
Eigen::Quaternion< T > HeadingToQuaternion(T heading)
Definition: quaternion.h:88
T yaw() const
Getter for yaw_.
Definition: euler_angles_zxy.h:130
Defines the EulerAnglesZXY class.
Eigen::Vector3d QuaternionRotate(const Quaternion &orientation, const Eigen::Vector3d &original)
Definition: quaternion.h:103
Math-related util functions.
Implements a class of Euler angles (actually, Tait-Bryan angles), with intrinsic sequence ZXY...
Definition: euler_angles_zxy.h:66