Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MathDefs.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2005 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *                                                                           *
00009  *                                License                                    *
00010  *                                                                           *
00011  *  This library is free software; you can redistribute it and/or modify it  *
00012  *  under the terms of the GNU Library General Public License as published   *
00013  *  by the Free Software Foundation, version 2.                              *
00014  *                                                                           *
00015  *  This library is distributed in the hope that it will be useful, but      *
00016  *  WITHOUT ANY WARRANTY; without even the implied warranty of               *
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
00018  *  Library General Public License for more details.                         *
00019  *                                                                           *
00020  *  You should have received a copy of the GNU Library General Public        *
00021  *  License along with this library; if not, write to the Free Software      *
00022  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
00023  *                                                                           *
00024 \*===========================================================================*/
00025 
00026 #ifndef MATHDEFS_HH
00027 #define MATHDEFS_HH
00028 
00029 #include <math.h>
00030 //#include <float.h>
00031 
00032 #ifndef M_PI
00033   #define M_PI      3.14159265359
00034 #endif
00035 
00036 namespace OpenMesh
00037 {
00038 
00041 template <class T, typename Real>
00042 inline bool                             is_zero(const T& _a, Real _eps)
00043 {
00044   return fabs(_a) < _eps;
00045 }
00046 
00047 template <class T1, class T2, typename Real>
00048 inline bool                             is_eq(const T1& a, const T2& b, Real _eps)
00049 {
00050   return is_zero(a-b, _eps);
00051 }
00052 
00053 template <class T1, class T2, typename Real>
00054 inline bool                             is_gt(const T1& a, const T2& b, Real _eps)
00055 {
00056   return (a > b) && !is_eq(a,b,_eps);
00057 }
00058 
00059 template <class T1, class T2, typename Real>
00060 inline bool                             is_ge(const T1& a, const T2& b, Real _eps)
00061 {
00062   return (a > b) || is_eq(a,b,_eps);
00063 }
00064 
00065 template <class T1, class T2, typename Real>
00066 inline bool                             is_lt(const T1& a, const T2& b, Real _eps)
00067 {
00068   return (a < b) && !is_eq(a,b,_eps);
00069 }
00070 
00071 template <class T1, class T2, typename Real>
00072 inline bool                             is_le(const T1& a, const T2& b, Real _eps)
00073 {
00074   return (a < b) || is_eq(a,b,_eps);
00075 }
00076 
00083 const double __flt_eps = 1e-5;
00084 const double __dbl_eps = 1e-11;
00085 
00086 inline bool                             is_zero(const float a)
00087 {
00088   return is_zero(a, __flt_eps);
00089 }
00090 
00091 inline bool                             is_zero(const double a)
00092 {
00093   return is_zero(a, __dbl_eps);
00094 }
00095 
00096 template <class T1, class T2>
00097 inline bool                             is_eq(const T1& a, const T2& b)
00098 {
00099   return is_zero(a-b);
00100 }
00101 
00102 template <class T1, class T2>
00103 inline bool                             is_gt(const T1& a, const T2& b)
00104 {
00105   return (a > b) && !is_eq(a,b);
00106 }
00107 
00108 template <class T1, class T2>
00109 inline bool                             is_ge(const T1& a, const T2& b)
00110 {
00111   return (a > b) || is_eq(a,b);
00112 }
00113 
00114 template <class T1, class T2>
00115 inline bool                             is_lt(const T1& a, const T2& b)
00116 {
00117   return (a < b) && !is_eq(a,b);
00118 }
00119 
00120 template <class T1, class T2>
00121 inline bool                             is_le(const T1& a, const T2& b)
00122 {
00123   return (a < b) || is_eq(a,b);
00124 }
00125 
00127 
00128 template <class T>
00129 T                                      sane_aarg(T _aarg)
00130 {
00131   if (_aarg < -1)
00132   {
00133     _aarg = -1;
00134   }
00135   else if (_aarg >  1)
00136   {
00137     _aarg = 1;
00138   }
00139   return _aarg;
00140 }
00141 
00146 template <class T>
00147 T                                           angle(T _cos_angle, T _sin_angle)
00148 {//sanity checks - otherwise acos will return nan
00149   _cos_angle = sane_aarg(_cos_angle);
00150   return (T) _sin_angle >= 0 ? acos(_cos_angle) : -acos(_cos_angle);
00151 }
00152 
00153 template <class T>
00154 inline T                               positive_angle(T _angle)
00155 {
00156   return _angle < 0 ? (2*M_PI + _angle) : _angle;
00157 }
00158 
00159 template <class T>
00160 inline T                               positive_angle(T _cos_angle, T _sin_angle)
00161 {
00162   return positive_angle(angle(_cos_angle, _sin_angle));
00163 }
00164 
00165 template <class T>
00166 inline T                               deg_to_rad(const T& _angle)
00167 {
00168   return M_PI*(_angle/180);
00169 }
00170 
00171 template <class T>
00172 inline T                               rad_to_deg(const T& _angle)
00173 {
00174   return 180*(_angle/M_PI);
00175 }
00176 
00177 };//namespace OpenMesh
00178 
00179 #endif//MATHDEFS_HH

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .