libcruft-util/quaternion.hpp

179 lines
4.7 KiB
C++
Raw Normal View History

2012-07-31 14:40:21 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2012-07-31 14:40:21 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2012-07-31 14:40:21 +10:00
*
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
2012-07-31 14:40:21 +10:00
*/
#ifndef __UTIL_QUATERNION_HPP
#define __UTIL_QUATERNION_HPP
#include "coord/traits.hpp"
#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"
#include <ostream>
2015-07-13 16:27:35 +10:00
2012-07-31 14:40:21 +10:00
namespace util {
// 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.
//
// issues include:
// * strictly 4 dimensions
// * scalar operations sometimes don't make sense on the w component
// * objects must be normalised to make sense
template <typename T>
struct quaternion {
T w, x, y, z;
2012-07-31 14:40:21 +10:00
static constexpr std::size_t size (void) { return 4; }
static quaternion angle_axis (T radians, vector<3,T> axis);
static quaternion from_euler (vector<3,T>);
static quaternion from_to (vector<3,T>, vector<3,T>);
static quaternion look (vector<3,T> fwd, vector<3,T> up);
2015-07-13 16:27:35 +10:00
matrix4<T> as_matrix (void) const;
2015-07-13 16:27:35 +10:00
static constexpr
quaternion<T> identity (void)
{
return { 1, 0, 0, 0 };
}
};
2015-07-13 16:27:35 +10:00
//-------------------------------------------------------------------------
template <typename T>
struct arity<quaternion<T>,void>
:std::integral_constant<std::size_t, 4>
{ };
///////////////////////////////////////////////////////////////////////////
template <typename T>
vector3<T>
rotate (vector3<T>, quaternion<T>);
///////////////////////////////////////////////////////////////////////////
2016-09-15 21:33:45 +10:00
template <typename T>
constexpr
T
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
//-------------------------------------------------------------------------
template <typename T>
constexpr
T
norm (quaternion<T> q)
{
return std::sqrt (norm2 (q));
}
2015-07-13 16:27:35 +10:00
//-------------------------------------------------------------------------
template <typename T>
constexpr
bool
is_normalised (quaternion<T> q)
{
return almost_equal (T{1}, norm2 (q));
}
2015-07-13 16:27:35 +10:00
//-------------------------------------------------------------------------
template <typename T>
constexpr
quaternion<T>
normalised (quaternion<T> q)
{
return q / norm (q);
}
///////////////////////////////////////////////////////////////////////////
template <typename T>
quaternion<T>
conjugate (quaternion<T>);
///////////////////////////////////////////////////////////////////////////
template <typename T>
quaternion<T>
operator* (quaternion<T>, quaternion<T>);
//-------------------------------------------------------------------------
template <typename T>
quaternion<T>&
operator*= (quaternion<T>&, quaternion<T>);
//-------------------------------------------------------------------------
template <typename T>
quaternion<T>
operator/ (quaternion<T>, quaternion<T>);
//-------------------------------------------------------------------------
template <typename T>
constexpr
quaternion<T>
operator/ (quaternion<T> q, T t)
{
return { q.w / t, q.x / t, q.y / t, q.z / t };
}
///////////////////////////////////////////////////////////////////////////
template <typename T>
constexpr
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);
}
//-------------------------------------------------------------------------
template <typename T>
bool almost_equal (quaternion<T>, quaternion<T>);
///////////////////////////////////////////////////////////////////////////
typedef quaternion<float> quaternionf;
typedef quaternion<double> quaterniond;
///////////////////////////////////////////////////////////////////////////
template <typename T>
std::ostream&
operator<< (std::ostream&, quaternion<T>);
2012-07-31 14:40:21 +10:00
}
#endif