Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MathDefs.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2015 by Computer Graphics Group, RWTH Aachen *
5  * www.openmesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenMesh. *
9  * *
10  * OpenMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 1188 $ *
38  * $Date: 2015-01-05 16:34:10 +0100 (Mon, 05 Jan 2015) $ *
39  * *
40 \*===========================================================================*/
41 
42 #ifndef MATHDEFS_HH
43 #define MATHDEFS_HH
44 
45 #include <cmath>
46 #include <cfloat>
47 
48 #ifndef M_PI
49  #define M_PI 3.14159265359
50 #endif
51 
52 namespace OpenMesh
53 {
54 
57 template <class T, typename Real>
58 inline bool is_zero(const T& _a, Real _eps)
59 { return fabs(_a) < _eps; }
60 
61 template <class T1, class T2, typename Real>
62 inline bool is_eq(const T1& a, const T2& b, Real _eps)
63 { return is_zero(a-b, _eps); }
64 
65 template <class T1, class T2, typename Real>
66 inline bool is_gt(const T1& a, const T2& b, Real _eps)
67 { return (a > b) && !is_eq(a,b,_eps); }
68 
69 template <class T1, class T2, typename Real>
70 inline bool is_ge(const T1& a, const T2& b, Real _eps)
71 { return (a > b) || is_eq(a,b,_eps); }
72 
73 template <class T1, class T2, typename Real>
74 inline bool is_lt(const T1& a, const T2& b, Real _eps)
75 { return (a < b) && !is_eq(a,b,_eps); }
76 
77 template <class T1, class T2, typename Real>
78 inline bool is_le(const T1& a, const T2& b, Real _eps)
79 { return (a < b) || is_eq(a,b,_eps); }
80 
81 /*const float flt_eps__ = 10*FLT_EPSILON;
82 const double dbl_eps__ = 10*DBL_EPSILON;*/
83 const float flt_eps__ = (float)1e-05;
84 const double dbl_eps__ = 1e-09;
85 
86 inline float eps__(float)
87 { return flt_eps__; }
88 
89 inline double eps__(double)
90 { return dbl_eps__; }
91 
92 template <class T>
93 inline bool is_zero(const T& a)
94 { return is_zero(a, eps__(a)); }
95 
96 template <class T1, class T2>
97 inline bool is_eq(const T1& a, const T2& b)
98 { return is_zero(a-b); }
99 
100 template <class T1, class T2>
101 inline bool is_gt(const T1& a, const T2& b)
102 { return (a > b) && !is_eq(a,b); }
103 
104 template <class T1, class T2>
105 inline bool is_ge(const T1& a, const T2& b)
106 { return (a > b) || is_eq(a,b); }
107 
108 template <class T1, class T2>
109 inline bool is_lt(const T1& a, const T2& b)
110 { return (a < b) && !is_eq(a,b); }
111 
112 template <class T1, class T2>
113 inline bool is_le(const T1& a, const T2& b)
114 { return (a < b) || is_eq(a,b); }
115 
117 
118 template <class T>
119 inline T sane_aarg(T _aarg)
120 {
121  if (_aarg < -1)
122  {
123  _aarg = -1;
124  }
125  else if (_aarg > 1)
126  {
127  _aarg = 1;
128  }
129  return _aarg;
130 }
131 
136 template <class T>
137 T angle(T _cos_angle, T _sin_angle)
138 {//sanity checks - otherwise acos will return nan
139  _cos_angle = sane_aarg(_cos_angle);
140  return (T) _sin_angle >= 0 ? acos(_cos_angle) : -acos(_cos_angle);
141 }
142 
143 template <class T>
144 inline T positive_angle(T _angle)
145 { return _angle < 0 ? (2*M_PI + _angle) : _angle; }
146 
147 template <class T>
148 inline T positive_angle(T _cos_angle, T _sin_angle)
149 { return positive_angle(angle(_cos_angle, _sin_angle)); }
150 
151 template <class T>
152 inline T deg_to_rad(const T& _angle)
153 { return M_PI*(_angle/180); }
154 
155 template <class T>
156 inline T rad_to_deg(const T& _angle)
157 { return 180*(_angle/M_PI); }
158 
159 inline double log_(double _value)
160 { return log(_value); }
161 
162 }//namespace OpenMesh
163 
164 #endif//MATHDEFS_HH
bool is_zero(const T &_a, Real _eps)
Definition: MathDefs.hh:58
T angle(T _cos_angle, T _sin_angle)
Definition: MathDefs.hh:137
T sane_aarg(T _aarg)
Trigonometry/angles - related.
Definition: MathDefs.hh:119