OpenMesh
OpenMesh/Core/Geometry/MathDefs.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 393 $                                                         *
00038  *   $Date: 2011-05-24 12:22:17 +0200 (Di, 24 Mai 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 #ifndef MATHDEFS_HH
00043 #define MATHDEFS_HH
00044 
00045 #include <math.h>
00046 #include <float.h>
00047 
00048 #ifndef M_PI
00049   #define M_PI      3.14159265359
00050 #endif
00051 
00052 namespace OpenMesh
00053 {
00054 
00057 template <class T, typename Real>
00058 inline bool is_zero(const T& _a, Real _eps)
00059 { return fabs(_a) < _eps; }
00060 
00061 template <class T1, class T2, typename Real>
00062 inline bool is_eq(const T1& a, const T2& b, Real _eps)
00063 { return is_zero(a-b, _eps); }
00064 
00065 template <class T1, class T2, typename Real>
00066 inline bool is_gt(const T1& a, const T2& b, Real _eps)
00067 { return (a > b) && !is_eq(a,b,_eps); }
00068 
00069 template <class T1, class T2, typename Real>
00070 inline bool is_ge(const T1& a, const T2& b, Real _eps)
00071 { return (a > b) || is_eq(a,b,_eps); }
00072 
00073 template <class T1, class T2, typename Real>
00074 inline bool is_lt(const T1& a, const T2& b, Real _eps)
00075 { return (a < b) && !is_eq(a,b,_eps); }
00076 
00077 template <class T1, class T2, typename Real>
00078 inline bool is_le(const T1& a, const T2& b, Real _eps)
00079 { return (a < b) || is_eq(a,b,_eps); }
00080 
00081 /*const float flt_eps__ = 10*FLT_EPSILON;
00082 const double dbl_eps__ = 10*DBL_EPSILON;*/
00083 const float flt_eps__ = (float)1e-05;
00084 const double dbl_eps__ = 1e-09;
00085 
00086 inline float eps__(float) 
00087 { return flt_eps__; }
00088 
00089 inline double eps__(double)
00090 { return dbl_eps__; }
00091 
00092 template <class T>
00093 inline bool is_zero(const T& a)
00094 { return is_zero(a, eps__(a)); }
00095 
00096 template <class T1, class T2>
00097 inline bool is_eq(const T1& a, const T2& b)
00098 { return is_zero(a-b); }
00099 
00100 template <class T1, class T2>
00101 inline bool is_gt(const T1& a, const T2& b)
00102 { return (a > b) && !is_eq(a,b); }
00103 
00104 template <class T1, class T2>
00105 inline bool is_ge(const T1& a, const T2& b)
00106 { return (a > b) || is_eq(a,b); }
00107 
00108 template <class T1, class T2>
00109 inline bool is_lt(const T1& a, const T2& b)
00110 { return (a < b) && !is_eq(a,b); }
00111 
00112 template <class T1, class T2>
00113 inline bool is_le(const T1& a, const T2& b)
00114 { return (a < b) || is_eq(a,b); }
00115 
00117 
00118 template <class T>
00119 inline T sane_aarg(T _aarg)
00120 {
00121   if (_aarg < -1)
00122   {
00123     _aarg = -1;
00124   }
00125   else if (_aarg >  1)
00126   {
00127     _aarg = 1;
00128   }
00129   return _aarg;
00130 }
00131 
00136 template <class T>
00137 T angle(T _cos_angle, T _sin_angle)
00138 {//sanity checks - otherwise acos will return nan
00139   _cos_angle = sane_aarg(_cos_angle);
00140   return (T) _sin_angle >= 0 ? acos(_cos_angle) : -acos(_cos_angle);
00141 }
00142 
00143 template <class T>
00144 inline T positive_angle(T _angle)
00145 { return _angle < 0 ? (2*M_PI + _angle) : _angle; }
00146 
00147 template <class T>
00148 inline T positive_angle(T _cos_angle, T _sin_angle)
00149 { return positive_angle(angle(_cos_angle, _sin_angle)); }
00150 
00151 template <class T>
00152 inline T deg_to_rad(const T& _angle)
00153 { return M_PI*(_angle/180); }
00154 
00155 template <class T>
00156 inline T rad_to_deg(const T& _angle)
00157 { return 180*(_angle/M_PI); }
00158 
00159 inline double log_(double _value)
00160 { return log(_value); }
00161 
00162 }//namespace OpenMesh
00163 
00164 #endif//MATHDEFS_HH
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines