2023-10-21 03:45:39 +00:00
|
|
|
///
|
|
|
|
/// \file include/scmp/geom/Quaternion.h
|
|
|
|
/// \author K. Isom <kyle@imap.cc>
|
|
|
|
/// \date 2019-08-05
|
|
|
|
/// \brief Quaternion implementation suitable for navigation in R3.
|
|
|
|
///
|
|
|
|
/// Copyright 2019 K. Isom <kyle@imap.cc>
|
|
|
|
///
|
|
|
|
/// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
/// any purpose with or without fee is hereby granted, provided that
|
|
|
|
/// the above copyright notice and this permission notice appear in all /// copies.
|
|
|
|
///
|
|
|
|
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
|
|
|
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
/// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
///
|
|
|
|
|
|
|
|
#ifndef SCMATH_GEOM_QUATERNION_H
|
|
|
|
#define SCMATH_GEOM_QUATERNION_H
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <iostream>
|
|
|
|
#include <ostream>
|
|
|
|
|
2023-10-19 06:57:50 +00:00
|
|
|
#include <scmp/Math.h>
|
|
|
|
#include <scmp/geom/Vector.h>
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
|
2023-10-19 06:57:50 +00:00
|
|
|
namespace scmp {
|
2023-10-19 06:44:05 +00:00
|
|
|
namespace geom {
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Quaternions provide a representation of Orientation
|
|
|
|
/// and rotations in three dimensions.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Quaternions encode rotations in three-dimensional space. While
|
|
|
|
/// technically a MakeQuaternion is comprised of a real element and a
|
|
|
|
/// complex vector<3>, for the purposes of this library, it is modeled
|
|
|
|
/// as a floating point 4D vector of the form <w, x, y, z>, where x, y,
|
|
|
|
/// and z represent an Axis of rotation in R3 and w the Angle, in
|
|
|
|
/// radians, of the rotation about that Axis. Where Euler angles are
|
|
|
|
/// concerned, the ZYX (or yaw, pitch, roll) sequence is used.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// For information on the underlying vector type, see the
|
|
|
|
/// documentation for scmp::geom::Vector.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Like vectors, quaternions carry an internal tolerance value ε that
|
|
|
|
/// is used for floating point comparisons. The math namespace contains
|
|
|
|
/// the default values used for this; generally, a tolerance of 0.0001
|
|
|
|
/// is considered appropriate for the uses of this library. The
|
|
|
|
/// tolerance can be explicitly set with the SetEpsilon method.
|
2023-10-19 06:44:05 +00:00
|
|
|
template<typename T>
|
|
|
|
class Quaternion {
|
|
|
|
public:
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Construct an identity Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion() : v(Vector<T, 3>{0.0, 0.0, 0.0}), w(1.0)
|
|
|
|
{
|
2023-10-19 06:57:50 +00:00
|
|
|
scmp::DefaultEpsilon(this->eps);
|
2023-10-21 03:45:39 +00:00
|
|
|
v.SetEpsilon(this->eps);
|
2023-10-19 06:44:05 +00:00
|
|
|
};
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Construct a Quaternion with an Axis and Angle of
|
2023-10-21 03:45:39 +00:00
|
|
|
/// rotation.
|
|
|
|
///
|
|
|
|
/// A Quaternion may be initialised with a Vector<T, 3> Axis
|
|
|
|
/// of rotation and an Angle of rotation. This doesn't do the
|
|
|
|
/// Angle transforms to simplify internal operations.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param _axis A three-dimensional vector of the same type as
|
|
|
|
/// the Quaternion.
|
|
|
|
/// \param _angle The Angle of rotation about the Axis of
|
|
|
|
/// rotation.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion(Vector<T, 3> _axis, T _angle) : v(_axis), w(_angle)
|
|
|
|
{
|
|
|
|
this->constrainAngle();
|
2023-10-19 06:57:50 +00:00
|
|
|
scmp::DefaultEpsilon(this->eps);
|
2023-10-21 03:45:39 +00:00
|
|
|
v.SetEpsilon(this->eps);
|
2023-10-19 06:44:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// A Quaternion may be initialised with a Vector<T, 4> comprised of
|
2023-10-21 03:45:39 +00:00
|
|
|
/// the Axis of rotation followed by the Angle of rotation.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param vector A vector in the form <w, x, y, z>.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion(Vector<T, 4> vector) :
|
|
|
|
v(Vector<T, 3>{vector[1], vector[2], vector[3]}),
|
|
|
|
w(vector[0])
|
|
|
|
{
|
|
|
|
this->constrainAngle();
|
2023-10-19 06:57:50 +00:00
|
|
|
scmp::DefaultEpsilon(this->eps);
|
2023-10-21 03:45:39 +00:00
|
|
|
v.SetEpsilon(this->eps);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief An initializer list containing values for w, x, y,
|
|
|
|
/// and z.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param ilst An initial set of values in the form
|
|
|
|
/// <w, x, y, z>.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion(std::initializer_list<T> ilst)
|
|
|
|
{
|
|
|
|
auto it = ilst.begin();
|
|
|
|
|
|
|
|
this->v = Vector<T, 3>{it[1], it[2], it[3]};
|
|
|
|
this->w = it[0];
|
|
|
|
|
|
|
|
this->constrainAngle();
|
2023-10-19 06:57:50 +00:00
|
|
|
scmp::DefaultEpsilon(this->eps);
|
2023-10-21 03:45:39 +00:00
|
|
|
v.SetEpsilon(this->eps);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Set the comparison tolerance for this Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param epsilon A tolerance value.
|
2023-10-19 06:44:05 +00:00
|
|
|
void
|
2023-10-21 03:45:39 +00:00
|
|
|
SetEpsilon(T epsilon)
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
this->eps = epsilon;
|
2023-10-21 03:45:39 +00:00
|
|
|
this->v.SetEpsilon(epsilon);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Return the Axis of rotation of this Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return The Axis of rotation of this Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector<T, 3>
|
2023-10-21 03:45:39 +00:00
|
|
|
Axis() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
return this->v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Return the Angle of rotation of this Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return the Angle of rotation of this Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
T
|
2023-10-21 03:45:39 +00:00
|
|
|
Angle() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
return this->w;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Compute the Dot product of two quaternions.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param other Another Quaternion.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \return The Dot product between the two quaternions.
|
2023-10-19 06:44:05 +00:00
|
|
|
T
|
2023-10-21 03:45:39 +00:00
|
|
|
Dot(const Quaternion<T> &other) const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
double innerProduct = this->v[0] * other.v[0];
|
|
|
|
|
|
|
|
innerProduct += (this->v[1] * other.v[1]);
|
|
|
|
innerProduct += (this->v[2] * other.v[2]);
|
|
|
|
innerProduct += (this->w * other.w);
|
|
|
|
return innerProduct;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Compute the Norm of a Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Treating the Quaternion as a Vector<T, 4>, this is the same
|
|
|
|
/// process as computing the Magnitude.
|
|
|
|
///
|
|
|
|
/// \return A non-negative real number.
|
2023-10-19 06:44:05 +00:00
|
|
|
T
|
2023-10-21 03:45:39 +00:00
|
|
|
Norm() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
T n = 0;
|
|
|
|
|
|
|
|
n += (this->v[0] * this->v[0]);
|
|
|
|
n += (this->v[1] * this->v[1]);
|
|
|
|
n += (this->v[2] * this->v[2]);
|
|
|
|
n += (this->w * this->w);
|
|
|
|
|
|
|
|
return std::sqrt(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Return the unit Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return The unit Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion
|
2023-10-21 03:45:39 +00:00
|
|
|
UnitQuaternion()
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
return *this / this->Norm();
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Compute the Conjugate of a Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return The Conjugate of this Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion
|
2023-10-21 03:45:39 +00:00
|
|
|
Conjugate() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
return Quaternion(Vector<T, 4>{this->w, -this->v[0], -this->v[1], -this->v[2]});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Compute the Inverse of a Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return The Inverse of this Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion
|
2023-10-21 03:45:39 +00:00
|
|
|
Inverse() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
T _norm = this->Norm();
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
return this->Conjugate() / (_norm * _norm);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Determine whether this is an identity Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return true if this is an identity Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
bool
|
2023-10-21 03:45:39 +00:00
|
|
|
IsIdentity() const {
|
|
|
|
return this->v.IsZero() &&
|
2023-10-19 06:57:50 +00:00
|
|
|
scmp::WithinTolerance(this->w, (T)1.0, this->eps);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Determine whether this is a unit Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return true if this is a unit Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
bool
|
2023-10-21 03:45:39 +00:00
|
|
|
IsUnitQuaternion() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-22 02:45:07 +00:00
|
|
|
auto normal = this->Norm();
|
|
|
|
return scmp::WithinTolerance(normal, (T) 1.0, this->eps);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Convert to Vector form.
|
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// Return the Quaternion as a Vector<T, 4>, with the Axis of
|
2023-10-21 03:45:39 +00:00
|
|
|
/// rotation followed by the Angle of rotation.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return A vector representation of the Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector<T, 4>
|
2023-10-21 03:45:39 +00:00
|
|
|
AsVector() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
return Vector<T, 4>{this->w, this->v[0], this->v[1], this->v[2]};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Rotate Vector vr about this Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param vr The vector to be rotated.
|
|
|
|
/// \return The rotated vector.
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector<T, 3>
|
2023-10-21 03:45:39 +00:00
|
|
|
Rotate(Vector<T, 3> vr) const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
return (this->Conjugate() * vr * (*this)).Axis();
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Return Euler angles for this Quaternion.
|
2023-10-21 03:45:39 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// Return the Euler angles for this Quaternion as a vector of
|
2023-10-21 03:45:39 +00:00
|
|
|
/// <yaw, pitch, roll>.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 04:17:18 +00:00
|
|
|
/// \warning Users of this function should watch out for gimbal
|
|
|
|
/// lock.
|
2023-10-21 03:45:39 +00:00
|
|
|
///
|
|
|
|
/// \return A vector<T, 3> containing <yaw, pitch, roll>
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector<T, 3>
|
2023-10-21 03:45:39 +00:00
|
|
|
Euler() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
T yaw, pitch, roll;
|
|
|
|
T a = this->w, a2 = a * a;
|
|
|
|
T b = this->v[0], b2 = b * b;
|
|
|
|
T c = this->v[1], c2 = c * c;
|
|
|
|
T d = this->v[2], d2 = d * d;
|
|
|
|
|
|
|
|
yaw = std::atan2(2 * ((a * b) + (c * d)), a2 - b2 - c2 + d2);
|
|
|
|
pitch = std::asin(2 * ((b * d) - (a * c)));
|
|
|
|
roll = std::atan2(2 * ((a * d) + (b * c)), a2 + b2 - c2 - d2);
|
|
|
|
|
|
|
|
return Vector<T, 3>{yaw, pitch, roll};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Quaternion addition.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param other The Quaternion to be added with this one.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \return The result of adding the two quaternions together.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion
|
|
|
|
operator+(const Quaternion<T> &other) const
|
|
|
|
{
|
|
|
|
return Quaternion(this->v + other.v, this->w + other.w);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Quaternion subtraction.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param other The Quaternion to be subtracted from this one.
|
|
|
|
/// \return The result of subtracting the other Quaternion from this one.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion
|
|
|
|
operator-(const Quaternion<T> &other) const
|
|
|
|
{
|
|
|
|
return Quaternion(this->v - other.v, this->w - other.w);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Scalar multiplication.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param k The scaling value.
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return A scaled Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion
|
|
|
|
operator*(const T k) const
|
|
|
|
{
|
|
|
|
return Quaternion(this->v * k, this->w * k);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Scalar division.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param k The scalar divisor.
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return A scaled Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion
|
|
|
|
operator/(const T k) const
|
|
|
|
{
|
|
|
|
return Quaternion(this->v / k, this->w / k);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Quaternion Hamilton multiplication with a three-
|
|
|
|
/// dimensional vector.
|
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// This is done by treating the vector as a pure Quaternion
|
2023-10-21 03:45:39 +00:00
|
|
|
/// (e.g. with an Angle of rotation of 0).
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param vector The vector to multiply with this Quaternion.
|
|
|
|
/// \return The Hamilton product of the Quaternion and vector.
|
2023-10-19 06:44:05 +00:00
|
|
|
Quaternion
|
|
|
|
operator*(const Vector<T, 3> &vector) const
|
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
return Quaternion(vector * this->w + this->v.Cross(vector),
|
2023-10-19 06:44:05 +00:00
|
|
|
(T) 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Quaternion Hamilton multiplication.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param other The other Quaternion to multiply with this one.
|
2023-10-19 06:44:05 +00:00
|
|
|
/// @result The Hamilton product of the two quaternions.
|
|
|
|
Quaternion
|
|
|
|
operator*(const Quaternion<T> &other) const
|
|
|
|
{
|
|
|
|
T angle = (this->w * other.w) -
|
|
|
|
(this->v * other.v);
|
|
|
|
Vector<T, 3> axis = (other.v * this->w) +
|
|
|
|
(this->v * other.w) +
|
2023-10-21 03:45:39 +00:00
|
|
|
(this->v.Cross(other.v));
|
2023-10-19 06:44:05 +00:00
|
|
|
return Quaternion(axis, angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Quaternion equivalence.
|
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param other The Quaternion to check equality against.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \return True if the two quaternions are equal within their tolerance.
|
2023-10-19 06:44:05 +00:00
|
|
|
bool
|
|
|
|
operator==(const Quaternion<T> &other) const
|
|
|
|
{
|
|
|
|
return (this->v == other.v) &&
|
2023-10-19 06:57:50 +00:00
|
|
|
(scmp::WithinTolerance(this->w, other.w, this->eps));
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Quaternion non-equivalence.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param other The Quaternion to check inequality against.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \return True if the two quaternions are unequal within their tolerance.
|
2023-10-19 06:44:05 +00:00
|
|
|
bool
|
|
|
|
operator!=(const Quaternion<T> &other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \brief Output a Quaternion to a stream in the form
|
2023-10-21 03:45:39 +00:00
|
|
|
/// `a + <i, j, k>`.
|
|
|
|
///
|
2023-10-19 06:44:05 +00:00
|
|
|
/// \todo improve the formatting.
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param outs An output stream
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param q A Quaternion
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \return The output stream
|
2023-10-19 06:44:05 +00:00
|
|
|
friend std::ostream &
|
|
|
|
operator<<(std::ostream &outs, const Quaternion<T> &q)
|
|
|
|
{
|
|
|
|
outs << q.w << " + " << q.v;
|
|
|
|
return outs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr T minRotation = -4 * M_PI;
|
|
|
|
static constexpr T maxRotation = 4 * M_PI;
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
Vector<T, 3> v; // Axis of rotation
|
|
|
|
T w; // Angle of rotation
|
2023-10-19 06:44:05 +00:00
|
|
|
T eps;
|
|
|
|
|
|
|
|
void
|
|
|
|
constrainAngle()
|
|
|
|
{
|
|
|
|
if (this->w < 0.0) {
|
|
|
|
this->w = std::fmod(this->w, this->minRotation);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->w = std::fmod(this->w, this->maxRotation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
/// \defgroup quaternion_aliases Quaternion type aliases.
|
|
|
|
/// Type aliases are provided for float and double quaternions.
|
|
|
|
///
|
|
|
|
|
|
|
|
/// \ingroup quaternion_aliases
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Type alias for a float Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
typedef Quaternion<float> Quaternionf;
|
|
|
|
|
|
|
|
/// \ingroup quaternion_aliases
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Type alias for a double Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
typedef Quaternion<double> Quaterniond;
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Convenience Quaternion construction function.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// Return a float Quaternion scaled appropriately from a vector and
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Angle, e.g.
|
|
|
|
/// angle = cos(Angle / 2),
|
|
|
|
/// Axis.UnitVector() * sin(Angle / 2).
|
|
|
|
///
|
|
|
|
/// \param axis The Axis of rotation.
|
|
|
|
/// \param angle The Angle of rotation.
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return A Quaternion.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \relatesalso Quaternion
|
|
|
|
Quaternionf MakeQuaternion(Vector3F axis, float angle);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Convience Quaternion construction function.
|
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// Return a double Quaternion scaled appropriately from a vector and
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Angle, e.g.
|
|
|
|
/// Angle = cos(Angle / 2),
|
|
|
|
/// Axis.UnitVector() * sin(Angle / 2).
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param axis The Axis of rotation.
|
|
|
|
/// \param angle The Angle of rotation.
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return A Quaternion.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \relatesalso Quaternion
|
|
|
|
Quaterniond MakeQuaternion(Vector3D axis, double angle);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Convience Quaternion construction function.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-22 02:45:07 +00:00
|
|
|
/// Return a double Quaternion scaled appropriately from a vector and
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Angle, e.g.
|
|
|
|
/// Angle = cos(Angle / 2),
|
|
|
|
/// Axis.UnitVector() * sin(Angle / 2).
|
|
|
|
///
|
|
|
|
/// \param axis The Axis of rotation.
|
|
|
|
/// \param angle The Angle of rotation.
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \return A Quaternion.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \relatesalso Quaternion
|
2023-10-19 06:44:05 +00:00
|
|
|
template <typename T>
|
|
|
|
Quaternion<T>
|
2023-10-21 03:45:39 +00:00
|
|
|
MakeQuaternion(Vector<T, 3> axis, T angle)
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
return Quaternion<T>(axis.UnitVector() * std::sin(angle / (T)2.0),
|
2023-10-19 06:44:05 +00:00
|
|
|
std::cos(angle / (T)2.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief COnstruct a Quaternion from Euler angles.
|
|
|
|
///
|
|
|
|
/// Given a vector of Euler angles in ZYX sequence (e.g. yaw, pitch,
|
|
|
|
/// roll), return a Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param euler A vector Euler Angle in ZYX sequence.
|
|
|
|
/// \return A Quaternion representation of the Orientation represented
|
2023-10-19 06:44:05 +00:00
|
|
|
/// by the Euler angles.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \relatesalso Quaternion
|
2023-10-21 04:17:18 +00:00
|
|
|
Quaternionf FloatQuaternionFromEuler(Vector3F euler);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief COnstruct a Quaternion from Euler angles.
|
|
|
|
///
|
|
|
|
/// Given a vector of Euler angles in ZYX sequence (e.g. yaw, pitch,
|
|
|
|
/// roll), return a Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param euler A vector Euler Angle in ZYX sequence.
|
|
|
|
/// \return A Quaternion representation of the Orientation represented
|
2023-10-19 06:44:05 +00:00
|
|
|
/// by the Euler angles.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \relatesalso Quaternion
|
2023-10-21 04:17:18 +00:00
|
|
|
Quaterniond DoubleQuaternionFromEuler(Vector3D euler);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Linear interpolation for two Quaternions.
|
|
|
|
///
|
|
|
|
/// LERP computes the linear interpolation of two quaternions At some
|
2023-10-19 06:44:05 +00:00
|
|
|
/// fraction of the distance between them.
|
|
|
|
///
|
|
|
|
/// \tparam T
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param p The starting Quaternion.
|
|
|
|
/// \param q The ending Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
/// \param t The fraction of the distance between the two quaternions to
|
|
|
|
/// interpolate.
|
|
|
|
/// \return A Quaternion representing the linear interpolation of the
|
|
|
|
/// two quaternions.
|
|
|
|
template <typename T>
|
|
|
|
Quaternion<T>
|
|
|
|
LERP(Quaternion<T> p, Quaternion<T> q, T t)
|
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
return (p + (q - p) * t).UnitQuaternion();
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Shortest distance spherical linear interpolation.
|
|
|
|
///
|
2023-10-19 06:44:05 +00:00
|
|
|
/// ShortestSLERP computes the shortest distance spherical linear
|
2023-10-21 03:45:39 +00:00
|
|
|
/// interpolation between two unit quaternions At some fraction of the
|
2023-10-19 06:44:05 +00:00
|
|
|
/// distance between them.
|
|
|
|
///
|
|
|
|
/// \tparam T
|
2023-10-22 02:45:07 +00:00
|
|
|
/// \param p The starting Quaternion.
|
|
|
|
/// \param q The ending Quaternion.
|
2023-10-19 06:44:05 +00:00
|
|
|
/// \param t The fraction of the distance between the two quaternions
|
|
|
|
/// to interpolate.
|
|
|
|
/// \return A Quaternion representing the shortest path between two
|
|
|
|
/// quaternions.
|
|
|
|
template <typename T>
|
|
|
|
Quaternion<T>
|
|
|
|
ShortestSLERP(Quaternion<T> p, Quaternion<T> q, T t)
|
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
assert(p.IsUnitQuaternion());
|
|
|
|
assert(q.IsUnitQuaternion());
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
T dp = p.Dot(q);
|
2023-10-19 06:44:05 +00:00
|
|
|
T sign = dp < 0.0 ? -1.0 : 1.0;
|
|
|
|
T omega = std::acos(dp * sign);
|
|
|
|
T sin_omega = std::sin(omega); // Compute once.
|
|
|
|
|
|
|
|
if (dp > 0.99999) {
|
|
|
|
return LERP(p, q * sign, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (p * std::sin((1.0 - t) * omega) / sin_omega) +
|
|
|
|
(q * sign * std::sin(omega*t) / sin_omega);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Internal consistency check.
|
|
|
|
///
|
2023-10-19 06:44:05 +00:00
|
|
|
/// Run a quick self test to exercise basic functionality of the Quaternion
|
|
|
|
/// class to verify correct operation. Note that if \#NDEBUG is defined, the
|
|
|
|
/// self test is disabled.
|
2023-10-21 03:45:39 +00:00
|
|
|
void QuaternionSelfTest();
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace geom
|
|
|
|
} // namespace wr
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
#endif // SCMATH_GEOM_QUATERNION_H
|