libcruft-util/quaternion.hpp

69 lines
2.1 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 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_QUATERNION_HPP
#define __UTIL_QUATERNION_HPP
#include "coord.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 {
// 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;
2012-07-31 14:40:21 +10:00
static quaternion rotation (T radians, vector<3,T> axis);
2015-07-13 16:27:35 +10:00
static quaternion rotation (vector<3,T> src, vector<3,T> dst);
matrix4<T> as_matrix (void) const;
2015-07-13 16:27:35 +10:00
static const quaternion IDENTITY;
};
2015-07-13 16:27:35 +10:00
template <size_t S, typename T>
quaternion<S,T>
operator* (const quaternion<S,T>, const quaternion<S,T>);
2015-07-13 16:27:35 +10:00
template <size_t S, typename T>
quaternion<S,T>
operator/ (const quaternion<S,T>, const quaternion<S,T>);
2015-07-13 16:27:35 +10:00
typedef quaternion<4,float> quaternionf;
template <size_t S, typename T>
std::ostream&
operator<< (std::ostream&, quaternion<S,T>);
2012-07-31 14:40:21 +10:00
}
#endif