misc.h
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 // 
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 // 
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef STUFF_MISC_H
00018 #define STUFF_MISC_H
00019 
00020 #include <cmath>
00021 
00022 #ifndef M_PI
00023 #define M_PI 3.14159265358979323846
00024 #endif
00025 
00027 // @{
00028 
00035 namespace g2o {
00036 
00040 template <typename T>
00041 inline T square(T x)
00042 {
00043   return x*x;
00044 }
00045 
00049 template <typename T>
00050 inline T hypot(T x, T y)
00051 {
00052   return (T) (sqrt(x*x + y*y));
00053 }
00054 
00058 template <typename T>
00059 inline T hypot_sqr(T x, T y)
00060 {
00061   return x*x + y*y;
00062 }
00063 
00067 inline double deg2rad(double degree)
00068 {
00069   return degree * 0.01745329251994329576;
00070 }
00071 
00075 inline double rad2deg(double rad)
00076 {
00077   return rad * 57.29577951308232087721;
00078 }
00079 
00083 inline double normalize_theta(double theta)
00084 {
00085   if (theta >= -M_PI && theta < M_PI)
00086     return theta;
00087   
00088   double multiplier = floor(theta / (2*M_PI));
00089   theta = theta - multiplier*2*M_PI;
00090   if (theta >= M_PI)
00091     theta -= 2*M_PI;
00092   if (theta < -M_PI)
00093     theta += 2*M_PI;
00094 
00095   return theta;
00096 }
00097 
00101 inline double inverse_theta(double th)
00102 {
00103   return normalize_theta(th + M_PI);
00104 }
00105 
00109 inline double average_angle(double theta1, double theta2)
00110 {
00111   double x, y;
00112 
00113   x = cos(theta1) + cos(theta2);
00114   y = sin(theta1) + sin(theta2);
00115   if(x == 0 && y == 0)
00116     return 0;
00117   else
00118     return std::atan2(y, x);
00119 }
00120 
00125 template <typename T>
00126 inline int sign(T x)
00127 {
00128   if (x > 0)
00129     return 1;
00130   else if (x < 0)
00131     return -1;
00132   else
00133     return 0;
00134 }
00135 
00139 template <typename T>
00140 inline T clamp(T l, T x, T u) 
00141 {
00142   if (x < l)
00143     return l;
00144   if (x > u)
00145     return u;
00146   return x;
00147 }
00148 
00152 template <typename T>
00153 inline T wrap(T l, T x, T u) 
00154 {
00155   T intervalWidth = u - l;
00156   while (x < l)
00157     x += intervalWidth;
00158   while (x > u)
00159     x -= intervalWidth;
00160   return x;
00161 }
00162 
00163 } // end namespace
00164 
00165 // @}
00166 
00167 #endif


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:59