00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MATHDEFS_HH
00027 #define MATHDEFS_HH
00028
00029 #include <math.h>
00030
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 {
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 };
00178
00179 #endif//MATHDEFS_HH