libcruft-util/quaternion.hpp

81 lines
2.4 KiB
C++

/*
* 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
*
* 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.
*
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_QUATERNION_HPP
#define __UTIL_QUATERNION_HPP
#include "coord.hpp"
#include "vector.hpp"
#include "matrix.hpp"
#include <ostream>
namespace util {
// quaternions must be 4 elements, but we include a size parameter so it
// fits with the generic coord infrastructure more easily.
//
// specifically:
// large regions of base code require a template template parameter with
// size and type arguments, which is annoying to work around for this one
// case.
//
// we protect against invalid instantiations through static_assert
template <size_t S, typename T>
struct quaternion : public coord::base<4,T,quaternion,coord::wxyz,coord::abcd> {
static_assert (S == 4, "quaternions must be 4 elements");
using coord::base<S,T,::util::quaternion,::util::coord::wxyz,::util::coord::abcd>::base;
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);
matrix4<T> as_matrix (void) const;
static const quaternion IDENTITY;
};
template <typename T>
quaternion<4,T>
conjugate (quaternion<4,T>);
template <size_t S, typename T>
quaternion<S,T>
operator* (const quaternion<S,T>, const quaternion<S,T>);
template <size_t S, typename T>
quaternion<S,T>
operator/ (const quaternion<S,T>, const quaternion<S,T>);
typedef quaternion<4,float> quaternionf;
typedef quaternion<4,double> quaterniond;
template <typename T>
vector3<T>
rotate (vector3<T>, quaternion<4,T>);
template <size_t S, typename T>
std::ostream&
operator<< (std::ostream&, quaternion<S,T>);
}
#endif