vector_angle.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 vector_angle_h
00014 #define vector_angle_h
00015 
00016 #include <cml/mathlib/checking.h>
00017 
00018 /* Functions for finding the signed and unsigned angles between vectors in
00019  * 3D and 2D.
00020  *
00021  * Note that the input vectors for these functions are not required to be
00022  * unit length.
00023  *
00024  * @todo: Clean up promotions, conversions, and return types.
00025  */
00026 
00027 namespace cml {
00028 
00030 template< class VecT_1, class VecT_2, class VecT_3 >
00031 typename detail::DotPromote<
00032     typename detail::CrossPromote<VecT_1,VecT_2>::promoted_vector, VecT_3
00033 >::promoted_scalar
00034 signed_angle(const VecT_1& v1, const VecT_2& v2, const VecT_3& reference)
00035 {
00036     typedef typename detail::CrossPromote<VecT_1,VecT_2>::promoted_vector
00037         vector_type;
00038     typedef typename detail::DotPromote<vector_type,VecT_3>::promoted_scalar
00039         value_type;
00040 
00041     vector_type c = cross(v1,v2);
00042     value_type angle = std::atan2(double(length(c)),double(dot(v1,v2)));
00043     return dot(c,reference) < value_type(0) ? -angle : angle;
00044 }
00045 
00047 template< class VecT_1, class VecT_2 >
00048 typename detail::DotPromote< VecT_1, VecT_2 >::promoted_scalar
00049 unsigned_angle(const VecT_1& v1, const VecT_2& v2) {
00050     return std::atan2(double(length(cross(v1,v2))),double(dot(v1,v2)));
00051 }
00052 
00054 template< class VecT_1, class VecT_2 >
00055 typename detail::DotPromote< VecT_1, VecT_2 >::promoted_scalar
00056 signed_angle_2D(const VecT_1& v1, const VecT_2& v2) {
00057     return std::atan2(double(perp_dot(v1,v2)),double(dot(v1,v2)));
00058 }
00059 
00061 template< class VecT_1, class VecT_2 >
00062 typename detail::DotPromote< VecT_1, VecT_2 >::promoted_scalar
00063 unsigned_angle_2D(const VecT_1& v1, const VecT_2& v2) {
00064     return std::fabs(signed_angle_2D(v1,v2));
00065 }
00066 
00067 } // namespace cml
00068 
00069 #endif

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