11 #ifndef __RDL_QUATERNION_H__ 12 #define __RDL_QUATERNION_H__ 76 EIGEN_STRONG_INLINE
double&
x()
78 return this->data()[0];
81 EIGEN_STRONG_INLINE
double x()
const 83 return this->data()[0];
86 EIGEN_STRONG_INLINE
double&
y()
88 return this->data()[1];
91 EIGEN_STRONG_INLINE
double y()
const 93 return this->data()[1];
96 EIGEN_STRONG_INLINE
double&
z()
98 return this->data()[2];
101 EIGEN_STRONG_INLINE
double z()
const 103 return this->data()[2];
106 EIGEN_STRONG_INLINE
double&
w()
108 return this->data()[3];
111 EIGEN_STRONG_INLINE
double w()
const 113 return this->data()[3];
116 void set(
double x,
double y,
double z,
double w)
130 return Vector3d(this->data()[0], this->data()[1], this->data()[2]);
138 [[deprecated(
"Use Quaternion::w() instead")]] EIGEN_STRONG_INLINE
double getScalarPart()
const {
return this->data()[3]; }
143 set(q.x(), q.y(), q.z(), q.w());
152 if (!std::signbit(
w()))
165 set(q.
x(), q.
y(), q.
z(), q.
w());
171 set(q[0], q[1], q[2], q[3]);
194 return Quaternion((*
this)[0] * s, (*
this)[1] * s, (*
this)[2] * s, (*
this)[3] * s);
205 (*
this)[3] * q[0] + (*
this)[0] * q[3] + (*
this)[1] * q[2] - (*
this)[2] * q[1], (*
this)[3] * q[1] + (*
this)[1] * q[3] + (*
this)[2] * q[0] - (*
this)[0] * q[2],
206 (*
this)[3] * q[2] + (*
this)[2] * q[3] + (*
this)[0] * q[1] - (*
this)[1] * q[0], (*
this)[3] * q[3] - (*
this)[0] * q[0] - (*
this)[1] * q[1] - (*
this)[2] * q[2]);
216 set((*this)[3] * q[0] + (*this)[0] * q[3] + (*this)[1] * q[2] - (*this)[2] * q[1], (*this)[3] * q[1] + (*this)[1] * q[3] + (*this)[2] * q[0] - (*this)[0] * q[2],
217 (*this)[3] * q[2] + (*this)[2] * q[3] + (*this)[0] * q[1] - (*this)[1] * q[0], (*this)[3] * q[3] - (*this)[0] * q[0] - (*this)[1] * q[1] - (*this)[2] * q[2]);
233 set(axis_angle.axis(), axis_angle.angle());
238 double d = axis.norm();
239 double s2 = std::sin(angle * 0.5) / d;
240 set(axis[0] * s2, axis[1] * s2, axis[2] * s2, std::cos(angle * 0.5));
261 double s = std::sqrt(squaredNorm() * quat.squaredNorm());
266 double angle = acos(dot(quat) / s);
268 if ((angle == 0.) || std::isnan(angle))
272 assert(!std::isnan(angle));
274 double d = 1. / std::sin(angle);
275 double p0 = std::sin((1. - alpha) * angle);
276 double p1 = std::sin(alpha * angle);
287 return Quaternion(-(*
this)[0], -(*
this)[1], -(*
this)[2], (*
this)[3]);
292 double omega_norm = omega.norm();
294 return fromAxisAngle(omega / omega_norm, dt * omega_norm) * (*this);
300 Quaternion vec_quat(vn[0], vn[1], vn[2], 0.f), res_quat;
302 res_quat = vec_quat * (*this);
305 return Vector3d(res_quat[0], res_quat[1], res_quat[2]);
318 double u = twist_axis.dot(
vec());
319 double n = twist_axis.squaredNorm();
321 double l = std::sqrt(std::pow(m, 2) + std::pow(u, 2) * n);
322 twist.
set(twist_axis.x() * (u / l), twist_axis.y() * (u / l), twist_axis.z() * (u / l), m / l);
329 double trace = mat.trace();
333 double s = 2. * std::sqrt(trace + 1.);
334 return Quaternion((mat(1, 2) - mat(2, 1)) / s, (mat(2, 0) - mat(0, 2)) / s, (mat(0, 1) - mat(1, 0)) / s, 0.25 * s);
336 else if ((mat(0, 0) > mat(1, 1)) && (mat(0, 0) > mat(2, 2)))
338 double s = 2. * std::sqrt(1. + mat(0, 0) - mat(1, 1) - mat(2, 2));
339 return Quaternion(-0.25 * s, (-mat(0, 1) - mat(1, 0)) / s, (-mat(0, 2) - mat(2, 0)) / s, (mat(2, 1) - mat(1, 2)) / s);
341 else if (mat(1, 1) > mat(2, 2))
343 double s = 2. * std::sqrt(1. + mat(1, 1) - mat(0, 0) - mat(2, 2));
344 return Quaternion((-mat(0, 1) - mat(1, 0)) / s, -0.25 * s, (-mat(1, 2) - mat(2, 1)) / s, (mat(0, 2) - mat(2, 0)) / s);
348 double s = 2. * std::sqrt(1. + mat(2, 2) - mat(0, 0) - mat(1, 1));
349 return Quaternion((-mat(0, 2) - mat(2, 0)) / s, (-mat(1, 2) - mat(2, 1)) / s, -0.25 * s, (mat(1, 0) - mat(0, 1)) / s);
355 double d = axis.norm();
356 double s2 = std::sin(angle_rad * 0.5) / d;
358 return Quaternion(axis[0] * s2, axis[1] * s2, axis[2] * s2, std::cos(angle_rad * 0.5));
367 #endif // ifndef __RDL_QUATERNION_H__ EIGEN_STRONG_INLINE double & w()
Quaternion timeStep(const Vector3d &omega, double dt)
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(RobotDynamics::Joint)
Quaternion & operator=(const Matrix3d &E)
void swingTwistDecomposition(const Vector3d &twist_axis, Quaternion &swing, Quaternion &twist)
Decompose a quaternion into a swing then twist quaternion where the twist is about the given axis...
EIGEN_STRONG_INLINE double y() const
Quaternion & operator=(const Eigen::Quaterniond &q)
Quaternion(const Eigen::Quaterniond &q)
EIGEN_STRONG_INLINE double w() const
Quaternion(const Vector4d &q)
EIGEN_STRONG_INLINE Vector3d vec() const
Get vector part.
void transform(const RobotDynamics::Math::SpatialTransform &X)
Pure virtual object. This object forces objects that inherit from it to have a method that tells how ...
Quaternion & operator=(const RobotDynamics::Math::Quaternion &q)
EIGEN_STRONG_INLINE double z() const
Quaternion toQuaternion(const Matrix3d &mat)
Vector3d rotate(const Vector3d &vec) const
EIGEN_STRONG_INLINE double & y()
void sanitize()
sanitize the quaternion by negating each element if the w element is less than zero ...
Quaternion that are used for singularity free joints.
Quaternion(const Vector3d &axis, double angle)
EIGEN_STRONG_INLINE double getScalarPart() const
Get scalar part.
Quaternion(double x, double y, double z, double w)
Constructor.
EIGEN_STRONG_INLINE double & x()
EIGEN_STRONG_INLINE double x() const
Quaternion(const RobotDynamics::Math::Matrix3d &E)
Quaternion operator*(const double &s) const
Method to scale the elements of a quaternion by a constant. Normalization is NOT performed.
EIGEN_STRONG_INLINE Vector4d()
Quaternion slerp(double alpha, const Quaternion &quat) const
From Wikipedia: In computer graphics, Slerp is shorthand for spherical linear interpolation, introduced by Ken Shoemake in the context of quaternion interpolation for the purpose of animating 3D rotation. It refers to constant-speed motion along a unit-radius great circle arc, given the ends and an interpolation parameter between 0 and 1.
Quaternion(const AxisAngle &axis_angle)
EIGEN_STRONG_INLINE double & z()
Quaternion operator*(const Quaternion &q) const
Quaternion multiplication.
Quaternion conjugate() const
void set(double x, double y, double z, double w)
Quaternion & operator=(const Vector4d &q)
Eigen::AngleAxisd AxisAngle
Quaternion & operator=(const AxisAngle &axis_angle)
Namespace for all structures of the RobotDynamics library.
Quaternion fromAxisAngle(const Vector3d &axis, double angle_rad)
Quaternion(const RobotDynamics::Math::Quaternion &q)
Quaternion & operator*=(const Quaternion &q)
Overloaded *= operator for quaternion multiplication.