2012-07-31 14:40:21 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2012-07-31 14:40:21 +10:00
|
|
|
*
|
2018-04-13 18:49:09 +10:00
|
|
|
* Copyright 2011-2018 Danny Robson <danny@nerdcruft.net>
|
2012-07-31 14:40:21 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_QUATERNION_HPP
|
|
|
|
#define __UTIL_QUATERNION_HPP
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
#include "coord/traits.hpp"
|
2016-08-10 17:42:52 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#include "maths.hpp"
|
2012-07-31 14:40:21 +10:00
|
|
|
#include "vector.hpp"
|
2015-07-13 16:27:35 +10:00
|
|
|
#include "matrix.hpp"
|
|
|
|
|
2018-03-22 16:10:06 +11:00
|
|
|
#include <iosfwd>
|
2015-07-13 16:27:35 +10:00
|
|
|
|
2012-07-31 14:40:21 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Represents a quaternion value.
|
|
|
|
///
|
|
|
|
/// Quaternion's are _just_ different enough to other coord types that we
|
|
|
|
/// special case as a distinct POD type and provide many of the same
|
|
|
|
/// functions as distinct declarations.
|
|
|
|
///
|
|
|
|
/// Considerations include:
|
|
|
|
/// * strictly 4 dimensions
|
|
|
|
/// * scalar operations sometimes don't make sense on the w component
|
|
|
|
/// * objects must be normalised to make sense
|
2016-10-17 22:14:57 +11:00
|
|
|
template <typename T>
|
|
|
|
struct quaternion {
|
|
|
|
T w, x, y, z;
|
2012-07-31 14:40:21 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
static constexpr std::size_t size (void) { return 4; }
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Construct a quaternion that represents a rotation of `radians`
|
|
|
|
/// around the unit-vector `axis`.
|
2016-10-12 23:00:47 +11:00
|
|
|
static quaternion angle_axis (T radians, vector<3,T> axis);
|
2018-12-26 15:10:21 +11:00
|
|
|
|
|
|
|
/// Construct a quaternion that represents a sequence of euler angle
|
|
|
|
/// rotations in radians. Equivalent to the concatentation of x, y,
|
|
|
|
/// and then z rotations.
|
2016-10-12 23:00:47 +11:00
|
|
|
static quaternion from_euler (vector<3,T>);
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Constructs a quaternion that represents the rotation of a unit
|
|
|
|
/// vector `a` to the unit vector `b`.
|
2018-04-13 18:49:09 +10:00
|
|
|
static quaternion from_to (vector<3,T> a, vector<3,T> b);
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Constructs a quaternion that represents the rotation needed to
|
|
|
|
/// look along the unit vector `fwd` with the unit vector `up`
|
|
|
|
/// denoting the upward direction.
|
2016-10-12 23:00:47 +11:00
|
|
|
static quaternion look (vector<3,T> fwd, vector<3,T> up);
|
2015-07-13 16:27:35 +10:00
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Converts a rotation quaternion into an equivalent matrix form.
|
2016-08-10 17:42:52 +10:00
|
|
|
matrix4<T> as_matrix (void) const;
|
2015-07-13 16:27:35 +10:00
|
|
|
|
2019-02-07 15:44:15 +11:00
|
|
|
/// Negate all components
|
|
|
|
quaternion operator- () const noexcept;
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Constructs a identity rotation quaternion.
|
2018-02-28 11:49:13 +11:00
|
|
|
static constexpr
|
2018-04-13 18:49:09 +10:00
|
|
|
quaternion<T>
|
|
|
|
identity (void)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
|
|
|
return { 1, 0, 0, 0 };
|
|
|
|
}
|
2016-08-10 17:42:52 +10:00
|
|
|
};
|
2015-07-13 16:27:35 +10:00
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2017-11-22 17:03:00 +11:00
|
|
|
template <typename T>
|
|
|
|
struct arity<quaternion<T>,void>
|
|
|
|
:std::integral_constant<std::size_t, 4>
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Returns the result of rotating the unit vector `dir` using the
|
|
|
|
/// quaternion `q`.
|
2016-10-17 22:14:57 +11:00
|
|
|
template <typename T>
|
|
|
|
vector3<T>
|
2018-12-26 15:10:21 +11:00
|
|
|
rotate (vector3<T> dir, quaternion<T> q);
|
2016-10-17 22:14:57 +11:00
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns the result of rotating the unit vector `v` by the quaternion `q`.
|
2018-05-08 15:51:39 +10:00
|
|
|
template <typename T>
|
|
|
|
vector3<T>
|
|
|
|
operator* (quaternion<T> const q, vector3<T> const v)
|
|
|
|
{
|
|
|
|
return rotate (v, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Returns the square of the L2 norm of the quaternion `q`.
|
2016-09-15 21:33:45 +10:00
|
|
|
template <typename T>
|
2016-10-17 22:14:57 +11:00
|
|
|
constexpr
|
|
|
|
T
|
2018-02-28 11:49:13 +11:00
|
|
|
norm2 (quaternion<T> q)
|
|
|
|
{
|
|
|
|
return q.w * q.w +
|
|
|
|
q.x * q.x +
|
|
|
|
q.y * q.y +
|
|
|
|
q.z * q.z;
|
|
|
|
}
|
|
|
|
|
2016-09-15 21:33:45 +10:00
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns the L2 norm of the quaternion `q`.
|
2016-10-17 22:14:57 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
T
|
2018-02-28 11:49:13 +11:00
|
|
|
norm (quaternion<T> q)
|
|
|
|
{
|
|
|
|
return std::sqrt (norm2 (q));
|
|
|
|
}
|
|
|
|
|
2015-07-13 16:27:35 +10:00
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns true if the quaternion has a unit L2 norm.
|
2016-10-17 22:14:57 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
bool
|
2018-02-28 11:49:13 +11:00
|
|
|
is_normalised (quaternion<T> q)
|
|
|
|
{
|
|
|
|
return almost_equal (T{1}, norm2 (q));
|
|
|
|
}
|
|
|
|
|
2015-07-13 16:27:35 +10:00
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns the normalised form of quaternion `q`.
|
2016-10-17 22:14:57 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
quaternion<T>
|
2018-02-28 11:49:13 +11:00
|
|
|
normalised (quaternion<T> q)
|
|
|
|
{
|
|
|
|
return q / norm (q);
|
|
|
|
}
|
2016-10-12 23:00:47 +11:00
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
|
2019-02-07 15:45:08 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
quaternion<T>
|
|
|
|
slerp (quaternion<T> a, quaternion<T> b, T t);
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
quaternion<T>
|
|
|
|
nlerp (quaternion<T> a, quaternion<T> b, T t);
|
|
|
|
|
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-10-12 23:00:47 +11:00
|
|
|
template <typename T>
|
2016-10-17 22:14:57 +11:00
|
|
|
quaternion<T>
|
|
|
|
conjugate (quaternion<T>);
|
|
|
|
|
2015-07-21 01:39:38 +10:00
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
2019-02-07 15:44:15 +11:00
|
|
|
T dot (quaternion<T>, quaternion<T>);
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T> quaternion<T> operator* (quaternion<T>, T);
|
|
|
|
template <typename T> quaternion<T> operator* (T, quaternion<T>);
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
quaternion<T>
|
|
|
|
operator+ (quaternion<T>, quaternion<T>);
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2016-10-17 22:14:57 +11:00
|
|
|
quaternion<T>
|
|
|
|
operator* (quaternion<T>, quaternion<T>);
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2016-10-18 16:45:38 +11:00
|
|
|
template <typename T>
|
|
|
|
quaternion<T>&
|
|
|
|
operator*= (quaternion<T>&, quaternion<T>);
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
|
2016-10-18 16:45:38 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2016-10-17 22:14:57 +11:00
|
|
|
template <typename T>
|
|
|
|
quaternion<T>
|
|
|
|
operator/ (quaternion<T>, quaternion<T>);
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
quaternion<T>
|
2018-02-28 11:49:13 +11:00
|
|
|
operator/ (quaternion<T> q, T t)
|
|
|
|
{
|
|
|
|
return { q.w / t, q.x / t, q.y / t, q.z / t };
|
|
|
|
}
|
2016-10-17 22:14:57 +11:00
|
|
|
|
|
|
|
|
2018-04-18 21:44:56 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
matrix<4,4,T>
|
|
|
|
operator* (quaternion<T> q, matrix<4,4,T> m)
|
|
|
|
{
|
|
|
|
return q.as_matrix () * m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
matrix<4,4,T>
|
|
|
|
operator* (matrix<4,4,T> m, quaternion<T> q)
|
|
|
|
{
|
|
|
|
return m * q.as_matrix ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
constexpr
|
2018-02-28 11:49:13 +11:00
|
|
|
bool operator== (quaternion<T> a, quaternion<T> b)
|
|
|
|
{
|
|
|
|
return exactly_equal (a.w, b.w) &&
|
|
|
|
exactly_equal (a.x, b.x) &&
|
|
|
|
exactly_equal (a.y, b.y) &&
|
|
|
|
exactly_equal (a.z, b.z);
|
|
|
|
}
|
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
bool almost_equal (quaternion<T>, quaternion<T>);
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
typedef quaternion<float> quaternionf;
|
|
|
|
typedef quaternion<double> quaterniond;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
2016-03-11 13:01:57 +11:00
|
|
|
std::ostream&
|
2016-10-17 22:14:57 +11:00
|
|
|
operator<< (std::ostream&, quaternion<T>);
|
2012-07-31 14:40:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|