quaternion_basis.h

Go to the documentation of this file.
00001 /* -*- C++ -*- ------------------------------------------------------------
00002  
00003 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
00004 
00005 The Configurable Math Library (CML) is distributed under the terms of the
00006 Boost Software License, v1.0 (see cml/LICENSE for details).
00007 
00008  *-----------------------------------------------------------------------*/
00013 #ifndef quaternion_basis_h
00014 #define quaternion_basis_h
00015 
00016 #include <cml/mathlib/checking.h>
00017 
00018 /* Functions for getting the basis vectors of a quaternion rotation. */
00019 
00020 namespace cml {
00021 
00023 template < class QuatT > vector< typename QuatT::value_type, fixed<3> >
00024 quaternion_get_basis_vector(const QuatT& q, size_t i)
00025 {
00026     typedef QuatT quaternion_type;
00027     typedef typename quaternion_type::value_type value_type;
00028     typedef typename quaternion_type::order_type order_type;
00029     typedef vector< value_type, fixed<3> > vector_type;
00030 
00031     /* Checking */
00032     detail::CheckQuat(q);
00033     detail::CheckIndex3(i);
00034 
00035     size_t j, k;
00036     cyclic_permutation(i, i, j, k);
00037     
00038     /* @todo: Clean this up. */
00039     const size_t W = order_type::W;
00040     const size_t I = order_type::X + i;
00041     const size_t J = order_type::X + j;
00042     const size_t K = order_type::X + k;
00043     
00044     value_type j2 = q[J] + q[J];
00045     value_type k2 = q[K] + q[K];
00046     
00047     /* @todo: use set_permuted() for the following when available. */
00048 
00049     vector_type result;
00050     result[i] = value_type(1) - q[J] * j2 - q[K] * k2;
00051     result[j] = q[I] * j2 + q[W] * k2;
00052     result[k] = q[I] * k2 - q[W] * j2;
00053     return result;
00054 }
00055 
00057 template < class QuatT > vector< typename QuatT::value_type, fixed<3> >
00058 quaternion_get_x_basis_vector(const QuatT& q) {
00059     return quaternion_get_basis_vector(q,0);
00060 }
00061 
00063 template < class QuatT > vector< typename QuatT::value_type, fixed<3> >
00064 quaternion_get_y_basis_vector(const QuatT& q) {
00065     return quaternion_get_basis_vector(q,1);
00066 }
00067 
00069 template < class QuatT > vector< typename QuatT::value_type, fixed<3> >
00070 quaternion_get_z_basis_vector(const QuatT& q) {
00071     return quaternion_get_basis_vector(q,2);
00072 }
00073 
00075 template < class QuatT, typename E, class A > void
00076 quaternion_get_basis_vectors(
00077     const QuatT& q,
00078     vector<E,A>& x,
00079     vector<E,A>& y,
00080     vector<E,A>& z)
00081 {
00082     x = quaternion_get_x_basis_vector(q);
00083     y = quaternion_get_y_basis_vector(q);
00084     z = quaternion_get_z_basis_vector(q);
00085 }
00086 
00087 } // namespace cml
00088 
00089 #endif

Generated on Sat Jul 18 19:35:22 2009 for CML 1.0 by  doxygen 1.5.9