32Rotation::Rotation(
const Eigen::Matrix3d &rotation_matrix) : q(Eigen::Quaterniond(rotation_matrix))
51 const std::string &sequence,
53 : q(Eigen::Quaterniond::Identity())
64 const Eigen::AngleAxisd aa1 =
angleAxis(sequence[0], angle1);
65 const Eigen::AngleAxisd aa2 =
angleAxis(sequence[1], angle2);
66 const Eigen::AngleAxisd aa3 =
angleAxis(sequence[2], angle3);
71 q = Eigen::Quaterniond(aa3) * Eigen::Quaterniond(aa2) * Eigen::Quaterniond(aa1);
76 q = Eigen::Quaterniond(aa1) * Eigen::Quaterniond(aa2) * Eigen::Quaterniond(aa3);
90 return Rotation(quaternion.normalized());
102 const std::string &sequence,
105 return Rotation(angle1, angle2, angle3, intrinsic, sequence, degree);
111 return q.toRotationMatrix();
123 return Eigen::AngleAxisd(
q);
135 Eigen::Vector3d angles = Eigen::Vector3d::Zero();
140 angles =
q.toRotationMatrix().eulerAngles(a2, a1, a0).reverse();
145 angles =
q.toRotationMatrix().eulerAngles(a0, a1, a2);
168 q = std::move(other.q);
176 os <<
"Rotation (quaternion x,y,z,w): [" << quat.x() <<
", " << quat.y() <<
", " << quat.z()
177 <<
", " << quat.w() <<
"]";
185 angle = std::fmod(angle, 2 * M_PI);
192 else if (angle < -M_PI)
203 const double width = maxVal - minVal;
208 throw std::invalid_argument(
"Invalid range: maxVal must be greater than minVal");
212 const double offsetAngle = std::fmod(angle - minVal, width);
217 return offsetAngle + maxVal;
221 return offsetAngle + minVal;
240 return Eigen::AngleAxisd(angle, Eigen::Vector3d::UnitX());
243 return Eigen::AngleAxisd(angle, Eigen::Vector3d::UnitY());
246 return Eigen::AngleAxisd(angle, Eigen::Vector3d::UnitZ());
248 throw std::invalid_argument(
"Invalid axis. Valid axes are 'X', 'Y', or 'Z'.");
267 throw std::invalid_argument(
"Invalid axis. Valid axes are 'X', 'Y', or 'Z'.");
274 if (sequence.length() != 3 || sequence.find_first_not_of(
"XYZxyz") != std::string::npos)
276 throw std::invalid_argument(
"The axis sequence must be a string of three characters "
277 "from 'X', 'Y', and 'Z' or 'x', 'y', and 'z'.");
285 return degree * M_PI / 180.0;
291 return radian * 180.0 / M_PI;
295 const Eigen::Quaterniond &b)
297 return a.inverse() * b;
302 return Eigen::Quaterniond(-v.w(), -v.x(), -v.y(), -v.z());
307 return Eigen::Quaterniond(t * v.w(), t * v.x(), t * v.y(), t * v.z());
311 const Eigen::Quaterniond &v0)
313 return Eigen::Quaterniond(v1.w() - v0.w(), v1.x() - v0.x(), v1.y() - v0.y(), v1.z() - v0.z());
317 const Eigen::Quaterniond &v0)
319 return Eigen::Quaterniond(v1.w() + v0.w(), v1.x() + v0.x(), v1.y() + v0.y(), v1.z() + v0.z());
325 Eigen::Matrix3d rot = Eigen::Matrix3d::Identity();
326 rot << 1, 0, 0, 0, cos(angle), -sin(angle), 0, sin(angle), cos(angle);
332 Eigen::Matrix3d rot = Eigen::Matrix3d::Identity();
333 rot << cos(angle), 0, sin(angle), 0, 1, 0, -sin(angle), 0, cos(angle);
339 Eigen::Matrix3d rot = Eigen::Matrix3d::Identity();
340 rot << cos(angle), -sin(angle), 0, sin(angle), cos(angle), 0, 0, 0, 1;
347 Eigen::Matrix3d S = Eigen::Matrix3d::Zero();
348 S << 0, -omega.z(), omega.y(), omega.z(), 0, -omega.x(), -omega.y(), omega.x(), 0;
360 const Eigen::Matrix3d S =
matrixS(omega);
368 Eigen::Matrix3d T = Eigen::Matrix3d::Zero();
370 std::array<Eigen::Vector3d, 3> theta;
372 for (std::size_t i = 0; i < 3; ++i)
374 const char c = sequence[i];
379 theta[i] = Eigen::Vector3d::UnitX();
383 theta[i] = Eigen::Vector3d::UnitY();
387 theta[i] = Eigen::Vector3d::UnitZ();
396 Eigen::Matrix3d R = Eigen::Matrix3d::Identity();
398 for (std::size_t i = 0; i < 2; ++i)
402 const char c = sequence[i];
418 Ri = Eigen::Matrix3d::Identity();
423 T.col(
static_cast<Eigen::Index
>(i + 1)) = R * theta[i + 1];
427 if (std::abs(T.determinant()) < 1e-6)
429 std::cerr <<
"WARNING: The matrix is singular\n";
430 throw std::runtime_error(
"Matrix T is singular, which may cause numerical instability.");
Class for representing and managing rotations in three-dimensional space.
Rotation operator*(const Rotation &other) const
Composition of two rotations.
Eigen::Quaterniond q
Internal representation of rotation via quaternion.
static int axisToIndex(char axis)
Converts an axis character to index.
static Eigen::Vector3d normalizeEulerAngles(const Eigen::Vector3d &angles)
Normalizes Euler angles to the range [-π, π].
static Rotation fromRotationMatrix(const Eigen::Matrix3d &rotation_matrix)
Creates a rotation from a rotation matrix.
Rotation()
Default constructor.
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 fromAngleAxis(const Eigen::AngleAxisd &angle_axis)
Creates a rotation from an axis-angle representation.
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 Eigen::Quaterniond quaternion_difference(const Eigen::Quaterniond &a, const Eigen::Quaterniond &b)
Calculates the difference between two quaternions.
Rotation & operator=(const Rotation &other)
Assignment operator.
static double deg2rad(double degree)
Converts degrees to radians.
static double rad2deg(double radian)
Converts radians to degrees.
static Eigen::Quaterniond quaternion_negation(const Eigen::Quaterniond &v)
Negates the quaternion.
static Eigen::Matrix3d matrixS(const Eigen::Vector3d &omega)
Calculates the S matrix.
static Eigen::Quaterniond quaternion_minus(const Eigen::Quaterniond &v1, const Eigen::Quaterniond &v0)
Calculates the difference between two quaternions (v1 - v0).
static double normalizeAngleRange(double angle, double minVal, double maxVal)
Normalizes an angle to a specified range.
static Eigen::Quaterniond scalar_product(const Eigen::Quaterniond &v, double t)
Calculates the scalar product of a quaternion by a scalar.
static int checkSequence(const std::string &sequence)
Checks the validity of the Euler angle sequence.
static Eigen::Quaterniond quaternion_plus(const Eigen::Quaterniond &v1, const Eigen::Quaterniond &v0)
Calculates the sum of two quaternions.
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.
static Eigen::AngleAxisd angleAxis(char axis, double angle)
Creates an axis-angle representation.
Namespace for movement and manipulation of poses.
std::ostream & operator<<(std::ostream &os, const Pose &pose)
Class for representing and manipulating 3D rotations.