Rotation example
This example explains how to use quaternions and euler angles, and how to convert between them.
Compiling and running the code
Go to gz-math/examples
and use cmake
to compile the code:
git clone https://github.com/gazebosim/gz-math/ -b gz-math8
cd gz-math/examples
mkdir build
cd build
cmake ..
# Linux and macOS
make
# Windows
cd ..
cmake --build build --config Release
When the code is compiled, you can run two different examples, one which converts from quaternion to euler angles:
Usage:
./quaternion_to_euler <float_w> <float_x> <float_y> <float_z>
Example
./quaternion_to_euler 0.5 0.5 0.5 0.5
And the other which converts from euler to quaternion:
Usage (angles specified in radians):
quaternion_from_euler <float_roll> <float_pitch> <float_yaw>
Example
quaternion_from_euler 0 0 1.57
The ouput of each program, respectively:
# Linux and macOS
./quaternion_to_euler 0.5 0.5 0.5 0.5
# Windows
.\build\Release\quaternion_to_euler.exe 0.5 0.5 0.5 0.5
Normalizing Quaternion components:
W 0.5
X 0.5
Y 0.5
Z 0.5
to
W 0.5
X 0.5
Y 0.5
Z 0.5
Converting to Euler angles
roll 1.570796 radians
pitch -0.000000 radians
yaw 1.570796 radians
roll 90.000000 degrees
pitch -0.000000 degrees
yaw 90.000000 degrees
to Rotation matrix
0.000000 0.000000 1.000000
1.000000 0.000000 0.000000
0.000000 1.000000 0.000000
# Linux and macOS
./quaternion_from_euler 0 0 1.57
# Windows
.\build\Release\quaternion_from_euler.exe 0 0 1.57
Converting Euler angles:
roll 0.000000 radians
pitch 0.000000 radians
yaw 1.570000 radians
roll 0.000000 degrees
pitch 0.000000 degrees
yaw 89.954374 degrees
to Quaternion
W 0.707388
X 0.000000
Y 0.000000
Z 0.706825
to Rotation matrix
0.000796 -1.000000 0.000000
1.000000 0.000796 0.000000
0.000000 0.000000 1.000000
Code
You can create some objects to express rotations:
gz::math::Quaterniond q(roll, pitch, yaw);
gz::math::Matrix3d m(q);
gz::math::Vector3d euler(q.Euler());
To access the quaterions attributes:
printf(" W % .6f\n X % .6f\n Y % .6f\n Z % .6f\n",
q.W(), q.X(), q.Y(), q.Z());
Or to acccess the rotation matrix elements:
std::cout << "\nto Rotation matrix\n";
printf(" % .6f % .6f % .6f\n"
" % .6f % .6f % .6f\n"
" % .6f % .6f % .6f\n",
m(0, 0), m(0, 1), m(0, 2),
m(1, 0), m(1, 1), m(1, 2),
m(2, 0), m(2, 1), m(2, 2));