Apollo 10.0
自动驾驶开放平台
quaternion.h
浏览该文件的文档.
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_msgs/basic_msgs/geometry.pb.h"
36
41namespace apollo {
42namespace common {
43namespace 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 */
56inline 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 */
72template <typename T>
73inline 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 */
87template <typename T>
88inline 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 */
103inline 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
110inline 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
Any orientation of a rigid body on a 3-D space can be achieved by composing three rotations about the...
Eigen::Quaternion< T > ToQuaternion() const
Converts to a quaternion with a non-negative scalar part
Defines the EulerAnglesZXY class.
Math-related util functions.
Eigen::Vector3d QuaternionRotate(const Quaternion &orientation, const Eigen::Vector3d &original)
Definition quaternion.h:103
Eigen::Quaternion< T > HeadingToQuaternion(T heading)
Definition quaternion.h:88
EulerAnglesZXY< double > EulerAnglesZXYd
Eigen::Vector3d InverseQuaternionRotate(const Quaternion &orientation, const Eigen::Vector3d &rotated)
Definition quaternion.h:110
double NormalizeAngle(const double angle)
Normalize angle to [-PI, PI).
Definition math_utils.cc:53
class register implement
Definition arena_queue.h:37