Skeletal Animation Multithread Face
euler_rotations.cpp
Go to the documentation of this file.
1#include "../include/euler_rotations.h"
2
9Eigen::Matrix3d EulerRotations::basic_rotation_x(double x)
10{
11 // Matrix initialization
12 Eigen::Matrix3d m;
13
14 m << 1.0f, 0.0f, 0.0f,
15 0.0f, cos(x), -sin(x),
16 0.0f, sin(x), cos(x);
17 return m;
18}
19
26Eigen::Matrix3d EulerRotations::basic_rotation_y(double x)
27{
28 // Matrix initialization
29 Eigen::Matrix3d m;
30
31 m << cos(x), 0.0f, sin(x),
32 0.0f, 1.0f, 0.0f,
33 -sin(x), 0.0f, cos(x);
34 return m;
35}
36
43Eigen::Matrix3d EulerRotations::basic_rotation_z(double x)
44{
45 // Matrix initialization
46 Eigen::Matrix3d m;
47
48 m << cos(x), -sin(x), 0.0f,
49 sin(x), cos(x), 0.0f,
50 0.0f, 0.0f, 1.0f;
51 return m;
52}
53
54
66Eigen::Matrix3d EulerRotations::rotation(double phi, double theta, double psi)
67{
68 // Matrix initialization
69 Eigen::Matrix3d m;
70
71 // Basic rotations
72 Eigen::Matrix3d rotx = basic_rotation_x(phi);
73 Eigen::Matrix3d roty = basic_rotation_y(theta);
74 Eigen::Matrix3d rotz = basic_rotation_z(psi);
75
76 // Total rotation matrix
77 m = rotz * roty * rotx;
78
79 return m;
80}
81
85Eigen::Matrix3d EulerRotations::rotation(Eigen::Vector3d euler_angles)
86{
87 return rotation(euler_angles(0), euler_angles(1), euler_angles(2));
88}
89
93Eigen::Matrix3d EulerRotations::rotation(std::vector<double> euler_angles)
94{
95 return rotation(euler_angles[0], euler_angles[1], euler_angles[2]);
96}
97
101Eigen::Matrix3d EulerRotations::rotation(Euler euler_angles)
102{
103 return rotation(euler_angles.phi, euler_angles.theta, euler_angles.psi);
104}
105
118 double theta, double psi)
119{
121
122 double cy = cos(psi * 0.5); double sy = sin(psi * 0.5);
123 double cp = cos(theta * 0.5); double sp = sin(theta * 0.5);
124 double cr = cos(phi * 0.5); double sr = sin(phi * 0.5);
125
126 quatern.w = cr * cp * cy + sr * sp * sy;
127 quatern.x = sr * cp * cy - cr * sp * sy;
128 quatern.y = cr * sp * cy + sr * cp * sy;
129 quatern.z = cr * cp * sy - sr * sp * cy;
130
131 // Return Quaternions
132 return quatern;
133}
134
135
149 double y, double z)
150{
151 EulerRotations::Euler euler_angles;
152
153 // Roll (x-axis rotation)
154 double sinr_cosp = 2.0 * (w * x + y * z);
155 double cosr_cosp = 1.0 - 2.0 * (x * x + y * y);
156 euler_angles.phi = std::atan2(sinr_cosp, cosr_cosp);
157
158 // Pitch (y-axis rotation)
159 double sinp = 2.0 * (w * y - z * x);
160 if (std::abs(sinp) >= 1.0)
161 {
162 euler_angles.theta = std::copysign(M_PI / 2.0, sinp); // use 90 degrees if out of range
163 }
164 else
165 {
166 euler_angles.theta = std::asin(sinp);
167 }
168
169 // Yaw (z-axis rotation)
170 double siny_cosp = 2.0 * (w * z + x * y);
171 double cosy_cosp = 1.0 - 2.0 * (y * y + z * z);
172 euler_angles.psi = std::atan2(siny_cosp, cosy_cosp);
173
174 // Return Euler angles
175 return euler_angles;
176}
static Euler quaternions_to_euler(double w, double x, double y, double z)
Convert quaternions to Euler angles.
static Eigen::Matrix3d rotation(double phi, double theta, double psi)
Euler rotation matrix z-y'-x''.
static Eigen::Matrix3d basic_rotation_y(double x)
Basic rotation matrix wrt y axis.
static Quaternions euler_to_quaternions(double phi, double theta, double psi)
Convert Euler angles to quaternions.
static Eigen::Matrix3d basic_rotation_x(double x)
Basic rotation matrix wrt x axis.
static Eigen::Matrix3d basic_rotation_z(double x)
Basic rotation matrix wrt z axis.
Custom Euler angles struct.
Custom quaternions struct: .