MoveG 1.0.0
A modern C++ library for Robotics
Loading...
Searching...
No Matches
rotation_example.cpp
Go to the documentation of this file.
1
6#include "rotation_lib.h"
7#include <iomanip>
8#include <iostream>
9
10using namespace MoveG;
11
12// Utility function to print Euler angles in degrees
13void printEulerAngles(const Eigen::Vector3d &angles,
14 const std::string &description,
15 const std::string &sequence)
16{
17 std::cout << description << " (" << sequence << "): [" << std::fixed << std::setprecision(2)
18 << Rotation::rad2deg(angles[0]) << "°, " << Rotation::rad2deg(angles[1]) << "°, "
19 << Rotation::rad2deg(angles[2]) << "°]" << std::endl;
20}
21
22// Utility function to print quaternion
23void printQuaternion(const Eigen::Quaterniond &quat, const std::string &description)
24{
25 std::cout << description << " [w, x, y, z]: [" << std::fixed << std::setprecision(4) << quat.w()
26 << ", " << quat.x() << ", " << quat.y() << ", " << quat.z() << "]" << std::endl;
27}
28
29int main()
30{
31 std::cout << "==============================================" << std::endl;
32 std::cout << "MoveG Rotation Library Example Usage" << std::endl;
33 std::cout << "==============================================" << std::endl;
34
35 // Example 1: Creating rotations using different constructors
36 std::cout << "\n1. Creating rotations using different constructors:" << std::endl;
37
38 // Default constructor (identity rotation)
39 Rotation identity;
40 std::cout << "Identity rotation matrix:\n" << identity.toRotationMatrix() << std::endl;
41
42 // From Euler angles (ZYX sequence, intrinsic)
43 double roll = Rotation::deg2rad(30.0); // Around X-axis
44 double pitch = Rotation::deg2rad(45.0); // Around Y-axis
45 double yaw = Rotation::deg2rad(60.0); // Around Z-axis
46
47 Rotation rot_euler(yaw, pitch, roll, true, "ZYX", false);
48 std::cout << "Rotation from Euler angles (ZYX, intrinsic):\n"
49 << rot_euler.toRotationMatrix() << std::endl;
50
51 // From rotation matrix
52 Eigen::Matrix3d R = Eigen::AngleAxisd(M_PI / 4, Eigen::Vector3d::UnitZ()).toRotationMatrix();
53 Rotation rot_matrix(R);
54 std::cout << "Rotation from matrix (45° around Z):\n"
55 << rot_matrix.toRotationMatrix() << std::endl;
56
57 // From quaternion
58 Eigen::Quaterniond q =
59 Eigen::Quaterniond(Eigen::AngleAxisd(M_PI / 3, Eigen::Vector3d::UnitY()));
60 Rotation rot_quat(q);
61 std::cout << "Rotation from quaternion (60° around Y):\n"
62 << rot_quat.toRotationMatrix() << std::endl;
63
64 // From angle-axis
65 Eigen::AngleAxisd aa(M_PI / 6, Eigen::Vector3d::UnitX());
66 Rotation rot_aa(aa);
67 std::cout << "Rotation from angle-axis (30° around X):\n"
68 << rot_aa.toRotationMatrix() << std::endl;
69
70 // Example 2: Using static methods to create rotations
71 std::cout << "\n2. Using static methods to create rotations:" << std::endl;
72
73 Rotation rot_static_euler = Rotation::fromEulerAngles(yaw, pitch, roll, true, "ZYX", false);
74 std::cout << "Static method Euler angles rotation:\n"
75 << rot_static_euler.toRotationMatrix() << std::endl;
76
77 Rotation rot_static_quat = Rotation::fromQuaternion(q);
78 std::cout << "Static method quaternion rotation:\n"
79 << rot_static_quat.toRotationMatrix() << std::endl;
80
81 // Example 3: Converting between different rotation representations
82 std::cout << "\n3. Converting between different rotation representations:" << std::endl;
83
84 // Create a rotation from Euler angles
85 Rotation rot_convert(M_PI / 4, M_PI / 6, M_PI / 3, true, "ZYX", false);
86
87 // Convert to different representations
88 Eigen::Matrix3d rot_mat = rot_convert.toRotationMatrix();
89 Eigen::Quaterniond rot_q = rot_convert.toQuaternion();
90 Eigen::AngleAxisd rot_aa_convert = rot_convert.toAngleAxis();
91 Eigen::Vector3d rot_euler_angles = rot_convert.toEulerAngles(true, "ZYX");
92
93 std::cout << "Original rotation from Euler angles (ZYX):" << std::endl;
94 printEulerAngles(Eigen::Vector3d(M_PI / 4, M_PI / 6, M_PI / 3), "Input angles", "ZYX");
95
96 std::cout << "\nAs a rotation matrix:\n" << rot_mat << std::endl;
97
98 printQuaternion(rot_q, "As a quaternion");
99
100 std::cout << "As an angle-axis: " << Rotation::rad2deg(rot_aa_convert.angle())
101 << "° around axis [" << rot_aa_convert.axis().x() << ", " << rot_aa_convert.axis().y()
102 << ", " << rot_aa_convert.axis().z() << "]" << std::endl;
103
104 printEulerAngles(rot_euler_angles, "As Euler angles", "ZYX");
105
106 // Example 4: Composition of rotations
107 std::cout << "\n4. Composition of rotations:" << std::endl;
108
109 // Create two rotations
110 Rotation rot1 = Rotation::fromEulerAngles(0, 0, M_PI / 2, true, "ZYX", false); // 90° around Z
111 Rotation rot2 = Rotation::fromEulerAngles(0, M_PI / 2, 0, true, "ZYX", false); // 90° around Y
112
113 // Compose them
114 Rotation rot_combined = rot1 * rot2;
115
116 std::cout << "Rotation 1 (90° around Z):\n" << rot1.toRotationMatrix() << std::endl;
117 std::cout << "Rotation 2 (90° around Y):\n" << rot2.toRotationMatrix() << std::endl;
118 std::cout << "Combined rotation (rot1 * rot2):\n"
119 << rot_combined.toRotationMatrix() << std::endl;
120
121 // Order matters!
122 Rotation rot_combined2 = rot2 * rot1;
123 std::cout << "Different order (rot2 * rot1):\n"
124 << rot_combined2.toRotationMatrix() << std::endl;
125
126 // Example 5: Utility functions
127 std::cout << "\n5. Utility functions:" << std::endl;
128
129 // Converting between degrees and radians
130 double angle_deg = 45.0;
131 double angle_rad = Rotation::deg2rad(angle_deg);
132 std::cout << angle_deg << "° = " << angle_rad << " radians" << std::endl;
133 std::cout << angle_rad << " radians = " << Rotation::rad2deg(angle_rad) << "°" << std::endl;
134
135 // Normalizing angles
136 double big_angle = 5 * M_PI; // 5π radians (900°)
137 double normalized = Rotation::normalizeAngle(big_angle);
138 std::cout << "Normalizing " << big_angle << " radians to [-π, π]: " << normalized
139 << " radians (" << Rotation::rad2deg(normalized) << "°)" << std::endl;
140
141 // Elementary rotation matrices
142 std::cout << "\nElementary rotation matrices:" << std::endl;
143 std::cout << "Rotation around X (30°):\n" << Rotation::rotationX(M_PI / 6) << std::endl;
144 std::cout << "Rotation around Y (45°):\n" << Rotation::rotationY(M_PI / 4) << std::endl;
145 std::cout << "Rotation around Z (60°):\n" << Rotation::rotationZ(M_PI / 3) << std::endl;
146
147 // Matrix S and R_dot
148 Eigen::Vector3d omega(0.1, 0.2, 0.3); // Angular velocity
149 std::cout << "\nMatrix S for angular velocity [0.1, 0.2, 0.3]:\n"
150 << Rotation::matrixS(omega) << std::endl;
151
152 Eigen::Matrix3d R_current = Eigen::Matrix3d::Identity();
153 Eigen::Matrix3d R_dot = Rotation::matrixR_dot(R_current, omega);
154 std::cout << "Matrix R_dot:\n" << R_dot << std::endl;
155
156 // Matrix T
157 try
158 {
159 Eigen::Vector3d angles_for_T(M_PI / 4, M_PI / 6, M_PI / 3);
160 Eigen::Matrix3d T = Rotation::matrixT(angles_for_T, "ZYX");
161 std::cout << "\nMatrix T for ZYX Euler angles [" << Rotation::rad2deg(angles_for_T[0])
162 << "°, " << Rotation::rad2deg(angles_for_T[1]) << "°, "
163 << Rotation::rad2deg(angles_for_T[2]) << "°]:\n"
164 << T << std::endl;
165 }
166 catch (const std::exception &e)
167 {
168 std::cerr << "Error calculating matrix T: " << e.what() << std::endl;
169 }
170
171 std::cout << "==============================================" << std::endl;
172
173 return 0;
174}
Class for representing and managing rotations in three-dimensional space.
static double normalizeAngle(double angle)
Normalizes an angle to the range [-π, π].
static Eigen::Matrix3d matrixT(const Eigen::Vector3d &angles, const std::string &sequence)
Calculates the T matrix.
Eigen::Quaterniond toQuaternion() const
Converts the rotation to a quaternion.
Eigen::Matrix3d toRotationMatrix() const
Converts the rotation to a rotation matrix.
static Rotation fromEulerAngles(double angle1, double angle2, double angle3, bool intrinsic=true, const std::string &sequence="XYZ", bool degree=false)
Creates a rotation from Euler angles.
Eigen::AngleAxisd toAngleAxis() const
Converts the rotation to an axis-angle representation.
static Rotation fromQuaternion(const Eigen::Quaterniond &quaternion)
Creates a rotation from a quaternion.
static Eigen::Matrix3d rotationY(double angle)
Rotation matrix for a rotation around the Y axis.
static double deg2rad(double degree)
Converts degrees to radians.
static double rad2deg(double radian)
Converts radians to degrees.
static Eigen::Matrix3d matrixS(const Eigen::Vector3d &omega)
Calculates the S matrix.
static Eigen::Matrix3d rotationZ(double angle)
Rotation matrix for a rotation around the Z axis.
static Eigen::Matrix3d rotationX(double angle)
Rotation matrix for a rotation around the X axis.
static Eigen::Matrix3d matrixR_dot(const Eigen::Matrix3d &R, const Eigen::Matrix3d &S)
Calculates the R_dot matrix.
Eigen::Vector3d toEulerAngles(bool intrinsic=true, const std::string &sequence="ZYX") const
Converts the rotation to Euler angles.
Namespace for movement and manipulation of poses.
Definition pose_lib.cpp:12
void printQuaternion(const Eigen::Quaterniond &quat, const std::string &description)
void printEulerAngles(const Eigen::Vector3d &angles, const std::string &description, const std::string &sequence)
int main()
Class for representing and manipulating 3D rotations.