robotoc
robotoc - efficient ROBOT Optimal Control solvers
Loading...
Searching...
No Matches
rotation.hpp
Go to the documentation of this file.
1#ifndef ROBOTOC_ROTATION_HPP_
2#define ROBOTOC_ROTATION_HPP_
3
4#include "Eigen/Core"
5#include "Eigen/Geometry"
6
7namespace robotoc {
8namespace rotation {
9
15template <typename VectorType>
16inline Eigen::Matrix3d RotationMatrixFromQuaternion(
17 const Eigen::MatrixBase<VectorType>& quat_xyzw) {
18 assert(quat_xyzw.size() == 4);
19 return Eigen::Quaterniond(quat_xyzw).toRotationMatrix();
20}
21
22
28template <typename VectorType>
29inline Eigen::Matrix3d RotationMatrixFromNormalVector(
30 const Eigen::MatrixBase<VectorType>& normal_vector) {
31 assert(normal_vector.size() == 3);
32 Eigen::Matrix3d R;
33 const double nx = normal_vector.coeff(0);
34 const double ny = normal_vector.coeff(1);
35 const double nz = normal_vector.coeff(2);
36 const double nxny_norm = std::sqrt(nx*nx + ny*ny);
37 constexpr double eps = std::numeric_limits<double>::epsilon();
38 if (nxny_norm < eps) return Eigen::Matrix3d::Identity();
39
40 R << ny/nxny_norm, -nx/nxny_norm, 0.,
41 nx*nz/nxny_norm, ny*nz/nxny_norm, -nxny_norm,
42 nx, ny, nz;
43 return R;
44}
45
51template <typename MatrixType>
52inline Eigen::Vector4d QuaternionFromRotationMatrix(
53 const Eigen::MatrixBase<MatrixType>& R) {
54 assert(R.rows() == 3);
55 assert(R.cols() == 3);
56 return Eigen::Quaterniond(R).coeffs();
57}
58
64template <typename VectorType>
65inline Eigen::Vector4d QuaternionFromNormalVector(
66 const Eigen::MatrixBase<VectorType>& normal_vector) {
68}
69
74enum class ProjectionAxis {
75 X, Y, Z,
76};
77
78
84inline void ProjectRotationMatrix(Eigen::Matrix3d& R, const ProjectionAxis axis) {
85 if (axis == ProjectionAxis::X) {
86 const double norm = R.coeff(1, 1) * R.coeff(1, 1) + R.coeff(1, 2) + R.coeff(1, 2);
87 R.array() /= norm;
88 R.coeffRef(0, 0) = 1.0;
89 R.coeffRef(0, 1) = 0.0;
90 R.coeffRef(0, 2) = 0.0;
91 R.coeffRef(1, 0) = 0.0;
92 R.coeffRef(2, 0) = 0.0;
93 }
94 else if (axis == ProjectionAxis::Y) {
95 const double norm = R.coeff(0, 0) * R.coeff(0, 0) + R.coeff(0, 2) + R.coeff(0, 2);
96 R.array() /= norm;
97 R.coeffRef(0, 1) = 0.0;
98 R.coeffRef(1, 0) = 0.0;
99 R.coeffRef(1, 1) = 1.0;
100 R.coeffRef(1, 2) = 0.0;
101 R.coeffRef(2, 1) = 0.0;
102 }
103 else { // ProjectionAxis::Z
104 const double norm = R.coeff(0, 0) * R.coeff(0, 0) + R.coeff(0, 1) + R.coeff(0, 1);
105 R.array() /= norm;
106 R.coeffRef(0, 2) = 0.0;
107 R.coeffRef(1, 2) = 0.0;
108 R.coeffRef(2, 0) = 0.0;
109 R.coeffRef(2, 1) = 0.0;
110 R.coeffRef(2, 2) = 1.0;
111 }
112}
113
114} // namespace rotation
115} // namespace robotoc
116
117#endif // ROBOTOC_ROTATION_HPP_
void ProjectRotationMatrix(Eigen::Matrix3d &R, const ProjectionAxis axis)
Projects a rotation matrix onto a specified axis.
Definition: rotation.hpp:84
Eigen::Vector4d QuaternionFromRotationMatrix(const Eigen::MatrixBase< MatrixType > &R)
Convert a rotation matrix to a quaternion vector (x, y, z, w).
Definition: rotation.hpp:52
ProjectionAxis
Projection axis of the rotation.
Definition: rotation.hpp:74
Eigen::Matrix3d RotationMatrixFromNormalVector(const Eigen::MatrixBase< VectorType > &normal_vector)
Convert a normal vector to its surface Rotation matrix.
Definition: rotation.hpp:29
Eigen::Vector4d QuaternionFromNormalVector(const Eigen::MatrixBase< VectorType > &normal_vector)
Convert a normal vector to its surface quaternion (x, y, z, w).
Definition: rotation.hpp:65
Eigen::Matrix3d RotationMatrixFromQuaternion(const Eigen::MatrixBase< VectorType > &quat_xyzw)
Convert quaternion vector (x, y, z, w) to a Rotation matrix.
Definition: rotation.hpp:16
Definition: constraint_component_base.hpp:17