Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_MATH_GENERAL_MATH_H 00020 #define __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_MATH_GENERAL_MATH_H 00021 00022 #include "algorithms/localization/distance_based/util/localization_defutils.h" 00023 #include "algorithms/localization/distance_based/math/vec.h" 00024 #include <math.h> 00025 #include <limits.h> 00026 00027 namespace wiselib 00028 { 00029 00049 template <typename Arithmatic_P> 00050 static Arithmatic_P collinear_measure( Arithmatic_P, Arithmatic_P, Arithmatic_P, Arithmatic_P = 0 ); 00062 template <typename Arithmatic_P> 00063 static bool is_collinear ( Arithmatic_P, Arithmatic_P, Arithmatic_P, Arithmatic_P = 0 ); 00070 template <typename Arithmatic_P> 00071 static void rotate_2D( Arithmatic_P, Vec<Arithmatic_P>&, const Vec<Arithmatic_P>& = Vec<Arithmatic_P>( 0.0, 0.0, 0.0 ) ); 00077 template <typename Arithmatic_P> 00078 static Arithmatic_P angle_vec( const Vec<Arithmatic_P>& ); 00084 template <typename Arithmatic_P> 00085 static Arithmatic_P normalize_angle( Arithmatic_P ); 00086 // ----------------------------------------------------------------------- 00087 // ----------------------------------------------------------------------- 00088 // ----------------------------------------------------------------------- 00089 template <typename Arithmatic_P> 00090 Arithmatic_P 00091 collinear_measure( Arithmatic_P a, Arithmatic_P b, Arithmatic_P c, Arithmatic_P error ) 00092 { 00093 if ( a <= c && b <= c ) 00094 { 00095 return a + b - ( 1 + error ) * c; 00096 } 00097 else if ( a <= b && c <= b ) 00098 { 00099 return a + c - ( 1 + error ) * b; 00100 } 00101 else if ( b <= a && c <= a ) 00102 { 00103 return b + c - ( 1 + error ) * a; 00104 } 00105 00106 return DBL_MIN; 00107 } 00108 // ---------------------------------------------------------------------- 00109 template <typename Arithmatic_P> 00110 inline bool 00111 is_collinear( Arithmatic_P a, Arithmatic_P b, Arithmatic_P c, Arithmatic_P error ) 00112 { 00113 return collinear_measure( a, b, c, error ) <= 0; 00114 } 00115 // ---------------------------------------------------------------------- 00116 template <typename Arithmatic_P> 00117 void 00118 rotate_2D( Arithmatic_P angle, Vec<Arithmatic_P>& coord, const Vec<Arithmatic_P>& origin ) 00119 { 00120 Arithmatic_P cos_angle = cos( angle ); 00121 Arithmatic_P sin_angle = sin( angle ); 00122 00123 coord = Vec<Arithmatic_P>( 00124 ((coord.x() - origin.x()) * cos_angle) - ((coord.y() - origin.y()) * sin_angle) + origin.x(), 00125 ((coord.y() - origin.y()) * cos_angle) + ((coord.x() - origin.x()) * sin_angle) + origin.y(), 00126 coord.z() ); 00127 } 00128 // ---------------------------------------------------------------------- 00129 template <typename Arithmatic_P> 00130 Arithmatic_P 00131 angle_vec( const Vec<Arithmatic_P>& vec ) 00132 { 00133 if ( vec.x() == 0 && vec.y() >= 0 ) 00134 return 0.5 * M_PI; 00135 else if ( vec.x() == 0 && vec.y() < 0 ) 00136 return 1.5 * M_PI; 00137 00138 if ( vec.y() == 0 && vec.x() >= 0 ) 00139 return 0; 00140 else if ( vec.y() == 0 && vec.x() < 0 ) 00141 return M_PI; 00142 00143 if ( vec.y() >= 0 ) 00144 return acos( vec.x() / vec.euclidean_norm() ); 00145 else 00146 return 2.0*M_PI - acos( vec.x() / vec.euclidean_norm() ); 00147 } 00148 // ---------------------------------------------------------------------- 00149 template <typename Arithmatic_P > 00150 Arithmatic_P 00151 normalize_angle( Arithmatic_P angle ) 00152 { 00153 while ( angle < 0 ) angle += 2 * M_PI; 00154 while ( angle >= (2 * M_PI) ) angle -= (2 * M_PI); 00155 00156 return angle; 00157 } 00158 00159 00160 }// namespace wiselib 00161 #endif