MoveG 1.0.0
A modern C++ library for Robotics
Loading...
Searching...
No Matches
pose_example.cpp
Go to the documentation of this file.
1
6#include "pose_lib.h"
7#include "rotation_lib.h"
8#include <iomanip>
9#include <iostream>
10#include <vector>
11
12using namespace MoveG;
13
14// Utility function to print a vector
15void printVector3d(const Eigen::Vector3d &vec, const std::string &description)
16{
17 std::cout << std::fixed << std::setprecision(4);
18 std::cout << description << ": [" << vec.x() << ", " << vec.y() << ", " << vec.z() << "]"
19 << std::endl;
20}
21
22// Utility function to print a quaternion
23void printQuaternion(const Eigen::Quaterniond &quat, const std::string &description)
24{
25 std::cout << std::fixed << std::setprecision(4);
26 std::cout << description << " [w, x, y, z]: [" << quat.w() << ", " << quat.x() << ", "
27 << quat.y() << ", " << quat.z() << "]" << std::endl;
28}
29
30// Utility function to print a homogeneous transformation matrix
31void printHomogeneousMatrix(const Eigen::Matrix4d &mat, const std::string &description)
32{
33 std::cout << std::fixed << std::setprecision(4);
34 std::cout << description << ":\n" << mat << std::endl;
35}
36
37int main()
38{
39 std::cout << "==============================================" << std::endl;
40 std::cout << "MoveG Pose Library Example Usage" << std::endl;
41 std::cout << "==============================================" << std::endl;
42
43 // Example 1: Creating poses using different constructors
44 std::cout << "\n1. Creating poses using different constructors:" << std::endl;
45
46 // Default constructor (identity pose at origin)
47 Pose identity_pose;
48 std::cout << "Identity pose at origin:" << std::endl;
49 printVector3d(identity_pose.getPosition(), "Position");
50 printQuaternion(identity_pose.getQuaternion(), "Orientation");
51
52 // From position and quaternion
53 Eigen::Vector3d position(1.0, 2.0, 3.0);
54 Eigen::Quaterniond orientation(Eigen::AngleAxisd(M_PI / 4, Eigen::Vector3d::UnitZ()));
55
56 Pose pose_from_quat(position, orientation);
57 std::cout << "\nPose from position and quaternion:" << std::endl;
58 printVector3d(pose_from_quat.getPosition(), "Position");
59 printQuaternion(pose_from_quat.getQuaternion(), "Orientation");
60
61 // From position and rotation matrix
62 Eigen::Matrix3d rot_matrix =
63 Eigen::AngleAxisd(M_PI / 3, Eigen::Vector3d::UnitY()).toRotationMatrix();
64 Pose pose_from_matrix(position, rot_matrix);
65 std::cout << "\nPose from position and rotation matrix:" << std::endl;
66 printVector3d(pose_from_matrix.getPosition(), "Position");
67 std::cout << "Rotation matrix:\n" << pose_from_matrix.getRotationMatrix() << std::endl;
68
69 // From Affine3d transformation
70 Eigen::Affine3d affine = Eigen::Affine3d::Identity();
71 affine.translation() = Eigen::Vector3d(4.0, 5.0, 6.0);
72 affine.rotate(Eigen::AngleAxisd(M_PI / 2, Eigen::Vector3d::UnitX()));
73
74 Pose pose_from_affine(affine);
75 std::cout << "\nPose from Affine3d transformation:" << std::endl;
76 printVector3d(pose_from_affine.getPosition(), "Position");
77 std::cout << "Rotation matrix:\n" << pose_from_affine.getRotationMatrix() << std::endl;
78
79 // From position and Rotation object
80 Rotation rotation =
81 Rotation::fromAngleAxis(Eigen::AngleAxisd(M_PI / 6, Eigen::Vector3d::UnitX()));
82 Pose pose_from_rotation(position, rotation);
83 std::cout << "\nPose from position and Rotation object:" << std::endl;
84 printVector3d(pose_from_rotation.getPosition(), "Position");
85 printQuaternion(pose_from_rotation.getQuaternion(), "Orientation");
86
87 // From homogeneous transformation matrix
88 Eigen::Matrix4d homogeneous = Eigen::Matrix4d::Identity();
89 homogeneous.block<3, 3>(0, 0) =
90 Eigen::AngleAxisd(M_PI / 3, Eigen::Vector3d::UnitZ()).toRotationMatrix();
91 homogeneous.block<3, 1>(0, 3) = Eigen::Vector3d(7.0, 8.0, 9.0);
92
93 Pose pose_from_homogeneous(homogeneous);
94 std::cout << "\nPose from homogeneous transformation matrix:" << std::endl;
95 printVector3d(pose_from_homogeneous.getPosition(), "Position");
96 printQuaternion(pose_from_homogeneous.getQuaternion(), "Orientation");
97
98 // Example 2: Getting different representations of a pose
99 std::cout << "\n2. Getting different representations of a pose:" << std::endl;
100
101 Pose example_pose(Eigen::Vector3d(1.0, 2.0, 3.0),
102 Eigen::Quaterniond(Eigen::AngleAxisd(M_PI / 4, Eigen::Vector3d::UnitZ())));
103
104 // Get individual position components
105 std::cout << "Individual position components: x=" << example_pose.getX()
106 << ", y=" << example_pose.getY() << ", z=" << example_pose.getZ() << std::endl;
107
108 // Get individual quaternion components
109 std::cout << "Individual quaternion components: w=" << example_pose.getQw()
110 << ", x=" << example_pose.getQx() << ", y=" << example_pose.getQy()
111 << ", z=" << example_pose.getQz() << std::endl;
112
113 // Get as rotation matrix
114 std::cout << "Rotation matrix:\n" << example_pose.getRotationMatrix() << std::endl;
115
116 // Get as affine transformation
117 Eigen::Affine3d example_affine = example_pose.getAffineTransformation();
118 std::cout << "Affine transformation translation: [" << example_affine.translation().x() << ", "
119 << example_affine.translation().y() << ", " << example_affine.translation().z() << "]"
120 << std::endl;
121 std::cout << "Affine transformation rotation:\n" << example_affine.rotation() << std::endl;
122
123 // Get as homogeneous transformation matrix
124 Eigen::Matrix4d example_homogeneous = example_pose.getHomogeneousT();
125 printHomogeneousMatrix(example_homogeneous, "Homogeneous transformation matrix");
126
127 // Example 3: Modifying poses
128 std::cout << "\n3. Modifying poses:" << std::endl;
129
130 Pose modifiable_pose;
131 std::cout << "Initial pose:" << std::endl;
132 printVector3d(modifiable_pose.getPosition(), "Position");
133 printQuaternion(modifiable_pose.getQuaternion(), "Orientation");
134
135 // Set position
136 modifiable_pose.setPosition(Eigen::Vector3d(5.0, 6.0, 7.0));
137
138 // Set orientation with quaternion
139 modifiable_pose.setOrientation(
140 Eigen::Quaterniond(Eigen::AngleAxisd(M_PI / 3, Eigen::Vector3d::UnitY())));
141
142 std::cout << "\nAfter modification:" << std::endl;
143 printVector3d(modifiable_pose.getPosition(), "Position");
144 printQuaternion(modifiable_pose.getQuaternion(), "Orientation");
145
146 // Set orientation with rotation matrix
147 Eigen::Matrix3d new_rotation =
148 Eigen::AngleAxisd(M_PI / 2, Eigen::Vector3d::UnitZ()).toRotationMatrix();
149 modifiable_pose.setRotationMatrix(new_rotation);
150
151 std::cout << "\nAfter setting rotation matrix:" << std::endl;
152 printVector3d(modifiable_pose.getPosition(), "Position");
153 std::cout << "Rotation matrix:\n" << modifiable_pose.getRotationMatrix() << std::endl;
154
155 // Set with homogeneous transformation
156 Eigen::Matrix4d new_homogeneous = Eigen::Matrix4d::Identity();
157 new_homogeneous.block<3, 3>(0, 0) =
158 Eigen::AngleAxisd(M_PI / 6, Eigen::Vector3d::UnitX()).toRotationMatrix();
159 new_homogeneous.block<3, 1>(0, 3) = Eigen::Vector3d(10.0, 11.0, 12.0);
160
161 modifiable_pose.setHomogeneousT(new_homogeneous);
162
163 std::cout << "\nAfter setting homogeneous transformation:" << std::endl;
164 printVector3d(modifiable_pose.getPosition(), "Position");
165 printQuaternion(modifiable_pose.getQuaternion(), "Orientation");
166
167 // Example 4: Pose operations
168 std::cout << "\n4. Pose operations:" << std::endl;
169
170 // Create two poses for operations
171 Pose pose1(Eigen::Vector3d(1.0, 0.0, 0.0),
172 Eigen::Quaterniond(Eigen::AngleAxisd(M_PI / 2, Eigen::Vector3d::UnitZ())));
173
174 Pose pose2(Eigen::Vector3d(0.0, 1.0, 0.0),
175 Eigen::Quaterniond(Eigen::AngleAxisd(M_PI / 2, Eigen::Vector3d::UnitZ())));
176
177 std::cout << "Pose 1:" << std::endl;
178 printVector3d(pose1.getPosition(), "Position");
179 printQuaternion(pose1.getQuaternion(), "Orientation");
180
181 std::cout << "\nPose 2:" << std::endl;
182 printVector3d(pose2.getPosition(), "Position");
183 printQuaternion(pose2.getQuaternion(), "Orientation");
184
185 // Compose poses
186 Pose composed = pose1 * pose2;
187 std::cout << "\nComposed pose (pose1 * pose2):" << std::endl;
188 printVector3d(composed.getPosition(), "Position");
189 printQuaternion(composed.getQuaternion(), "Orientation");
190
191 // Inverse pose
192 Pose inverse = pose1.inverse();
193 std::cout << "\nInverse of pose1:" << std::endl;
194 printVector3d(inverse.getPosition(), "Position");
195 printQuaternion(inverse.getQuaternion(), "Orientation");
196
197 // Check that pose * inverse = identity
198 Pose identity_check = pose1 * inverse;
199 std::cout << "\nPose1 * Inverse (should be identity):" << std::endl;
200 printVector3d(identity_check.getPosition(), "Position");
201 printQuaternion(identity_check.getQuaternion(), "Orientation");
202
203 // Example 5: Distance metrics
204 std::cout << "\n5. Distance metrics:" << std::endl;
205
206 Pose pose_a(Eigen::Vector3d(1.0, 2.0, 3.0),
207 Eigen::Quaterniond(Eigen::AngleAxisd(0.0, Eigen::Vector3d::UnitZ())));
208
209 Pose pose_b(Eigen::Vector3d(4.0, 6.0, 8.0),
210 Eigen::Quaterniond(Eigen::AngleAxisd(M_PI / 2, Eigen::Vector3d::UnitZ())));
211
212 double position_distance = pose_a.positionDistance(pose_b);
213 double orientation_distance = pose_a.orientationDistance(pose_b);
214
215 std::cout << "Position distance: " << position_distance << " meters" << std::endl;
216 std::cout << "Orientation distance: " << orientation_distance << " radians ("
217 << Rotation::rad2deg(orientation_distance) << " degrees)" << std::endl;
218
219 // Example 6: Coordinate transformations
220 std::cout << "\n6. Coordinate transformations:" << std::endl;
221
222 // Create a pose representing a robot's pose in the world
223 Pose robot_in_world(
224 Eigen::Vector3d(2.0, 3.0, 0.0), // Position in world frame
225 Eigen::AngleAxisd(M_PI / 4, Eigen::Vector3d::UnitZ()) // 45° rotation around Z
226 );
227
228 // Create a point in the robot's local frame
229 Eigen::Vector3d point_in_robot(1.0, 0.0, 0.0); // 1 meter in front of the robot
230
231 // Transform point to world coordinates
232 Eigen::Vector3d point_in_world = robot_in_world.localToGlobal(point_in_robot);
233
234 std::cout << "Robot pose in world:" << std::endl;
235 printVector3d(robot_in_world.getPosition(), "Position");
236 std::cout << "Rotation: "
237 << Rotation::rad2deg(Eigen::AngleAxisd(robot_in_world.getQuaternion()).angle())
238 << "° around " << Eigen::AngleAxisd(robot_in_world.getQuaternion()).axis().transpose()
239 << std::endl;
240
241 std::cout << "\nPoint in robot's local frame: [" << point_in_robot.x() << ", "
242 << point_in_robot.y() << ", " << point_in_robot.z() << "]" << std::endl;
243
244 std::cout << "Same point in world frame: [" << point_in_world.x() << ", " << point_in_world.y()
245 << ", " << point_in_world.z() << "]" << std::endl;
246
247 // Transform point back to robot coordinates
248 Eigen::Vector3d point_back_in_robot = robot_in_world.globalToLocal(point_in_world);
249
250 std::cout << "Point transformed back to robot frame: [" << point_back_in_robot.x() << ", "
251 << point_back_in_robot.y() << ", " << point_back_in_robot.z() << "]" << std::endl;
252
253 // Transform between coordinate frames
254 // Create a second pose representing a sensor mounted on the robot
255 Pose sensor_in_robot(
256 Eigen::Vector3d(0.0, 0.0, 0.5), // Sensor is 0.5m above the robot's origin
257 Eigen::AngleAxisd(0.0, Eigen::Vector3d::UnitZ()) // No rotation relative to robot
258 );
259
260 // Calculate sensor pose in world frame
261 Pose sensor_in_world = robot_in_world.transformPose(sensor_in_robot);
262
263 std::cout << "\nSensor pose in robot frame:" << std::endl;
264 printVector3d(sensor_in_robot.getPosition(), "Position");
265 printQuaternion(sensor_in_robot.getQuaternion(), "Orientation");
266
267 std::cout << "\nSensor pose in world frame:" << std::endl;
268 printVector3d(sensor_in_world.getPosition(), "Position");
269 printQuaternion(sensor_in_world.getQuaternion(), "Orientation");
270
271 std::cout << "==============================================" << std::endl;
272
273 return 0;
274}
Class representing a pose in 3D space.
Definition pose_lib.h:35
Eigen::Vector3d globalToLocal(const Eigen::Vector3d &global_point) const
Transforms a point from the global frame to the pose's local frame.
Definition pose_lib.cpp:305
double getQw() const
Gets the W component of the quaternion.
Definition pose_lib.cpp:165
double getQz() const
Gets the Z component of the quaternion.
Definition pose_lib.cpp:160
double getY() const
Gets the Y coordinate of the position.
Definition pose_lib.cpp:140
double getQy() const
Gets the Y component of the quaternion.
Definition pose_lib.cpp:155
double positionDistance(const Pose &other) const
Calculates the Euclidean distance between the positions of two poses.
Definition pose_lib.cpp:206
void setOrientation(const Eigen::Quaterniond &orientation)
Sets the orientation via quaternion.
Definition pose_lib.cpp:177
double getX() const
Gets the X coordinate of the position.
Definition pose_lib.cpp:135
Pose transformPose(const Pose &transform) const
Transforms this pose from one coordinate frame to another.
Definition pose_lib.cpp:291
Eigen::Vector3d getPosition() const
Gets the position.
Definition pose_lib.cpp:102
void setRotationMatrix(const Eigen::Matrix3d &rotation_matrix)
Sets the rotation matrix.
Definition pose_lib.cpp:182
Eigen::Affine3d getAffineTransformation() const
Gets the affine transformation.
Definition pose_lib.cpp:117
Eigen::Vector3d localToGlobal(const Eigen::Vector3d &local_point) const
Transforms a point from the pose's local frame to the global frame.
Definition pose_lib.cpp:298
void setPosition(const Eigen::Vector3d &position)
Sets the position.
Definition pose_lib.cpp:172
double getQx() const
Gets the X component of the quaternion.
Definition pose_lib.cpp:150
void setHomogeneousT(const Eigen::Matrix4d &homogeneousT)
Sets the homogeneous transformation matrix.
Definition pose_lib.cpp:193
Pose inverse() const
Calculates the inverse of the pose.
Definition pose_lib.cpp:229
Eigen::Quaterniond getQuaternion() const
Gets the orientation quaternion.
Definition pose_lib.cpp:107
double getZ() const
Gets the Z coordinate of the position.
Definition pose_lib.cpp:145
double orientationDistance(const Pose &other) const
Calculates the angular distance between the orientations of two poses.
Definition pose_lib.cpp:211
Eigen::Matrix3d getRotationMatrix() const
Gets the rotation matrix.
Definition pose_lib.cpp:112
Eigen::Matrix4d getHomogeneousT() const
Gets the homogeneous transformation matrix.
Definition pose_lib.cpp:125
Class for representing and managing rotations in three-dimensional space.
static Rotation fromAngleAxis(const Eigen::AngleAxisd &angle_axis)
Creates a rotation from an axis-angle representation.
static double rad2deg(double radian)
Converts radians to degrees.
Namespace for movement and manipulation of poses.
Definition pose_lib.cpp:12
void printHomogeneousMatrix(const Eigen::Matrix4d &mat, const std::string &description)
void printQuaternion(const Eigen::Quaterniond &quat, const std::string &description)
void printVector3d(const Eigen::Vector3d &vec, const std::string &description)
int main()
Class for representing Poses.
Class for representing and manipulating 3D rotations.