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, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 
43 /*===========================================================================*\
44  * *
45  * $Revision$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 #ifndef MATHDEFS_HH
51 #define MATHDEFS_HH
52 
53 #include <cmath>
54 #include <cfloat>
55 
56 #ifndef M_PI
57  #define M_PI 3.14159265359
58 #endif
59 
60 namespace OpenMesh
61 {
62 
65 template <class T, typename Real>
66 inline bool is_zero(const T& _a, Real _eps)
67 { return fabs(_a) < _eps; }
68 
69 template <class T1, class T2, typename Real>
70 inline bool is_eq(const T1& a, const T2& b, Real _eps)
71 { return is_zero(a-b, _eps); }
72 
73 template <class T1, class T2, typename Real>
74 inline bool is_gt(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_ge(const T1& a, const T2& b, Real _eps)
79 { return (a > b) || is_eq(a,b,_eps); }
80 
81 template <class T1, class T2, typename Real>
82 inline bool is_lt(const T1& a, const T2& b, Real _eps)
83 { return (a < b) && !is_eq(a,b,_eps); }
84 
85 template <class T1, class T2, typename Real>
86 inline bool is_le(const T1& a, const T2& b, Real _eps)
87 { return (a < b) || is_eq(a,b,_eps); }
88 
89 /*const float flt_eps__ = 10*FLT_EPSILON;
90 const double dbl_eps__ = 10*DBL_EPSILON;*/
91 const float flt_eps__ = (float)1e-05;
92 const double dbl_eps__ = 1e-09;
93 
94 inline float eps__(float)
95 { return flt_eps__; }
96 
97 inline double eps__(double)
98 { return dbl_eps__; }
99 
100 template <class T>
101 inline bool is_zero(const T& a)
102 { return is_zero(a, eps__(a)); }
103 
104 template <class T1, class T2>
105 inline bool is_eq(const T1& a, const T2& b)
106 { return is_zero(a-b); }
107 
108 template <class T1, class T2>
109 inline bool is_gt(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_ge(const T1& a, const T2& b)
114 { return (a > b) || is_eq(a,b); }
115 
116 template <class T1, class T2>
117 inline bool is_lt(const T1& a, const T2& b)
118 { return (a < b) && !is_eq(a,b); }
119 
120 template <class T1, class T2>
121 inline bool is_le(const T1& a, const T2& b)
122 { return (a < b) || is_eq(a,b); }
123 
125 
126 template <class T>
127 inline T sane_aarg(T _aarg)
128 {
129  if (_aarg < -1)
130  {
131  _aarg = -1;
132  }
133  else if (_aarg > 1)
134  {
135  _aarg = 1;
136  }
137  return _aarg;
138 }
139 
144 template <class T>
145 T angle(T _cos_angle, T _sin_angle)
146 {//sanity checks - otherwise acos will return nan
147  _cos_angle = sane_aarg(_cos_angle);
148  return (T) _sin_angle >= 0 ? acos(_cos_angle) : -acos(_cos_angle);
149 }
150 
151 template <class T>
152 inline T positive_angle(T _angle)
153 { return _angle < 0 ? (2*M_PI + _angle) : _angle; }
154 
155 template <class T>
156 inline T positive_angle(T _cos_angle, T _sin_angle)
157 { return positive_angle(angle(_cos_angle, _sin_angle)); }
158 
159 template <class T>
160 inline T deg_to_rad(const T& _angle)
161 { return M_PI*(_angle/180); }
162 
163 template <class T>
164 inline T rad_to_deg(const T& _angle)
165 { return 180*(_angle/M_PI); }
166 
167 inline double log_(double _value)
168 { return log(_value); }
169 
170 }//namespace OpenMesh
171 
172 #endif//MATHDEFS_HH
bool is_zero(const T &_a, Real _eps)
Definition: MathDefs.hh:66
T angle(T _cos_angle, T _sin_angle)
Definition: MathDefs.hh:145
T sane_aarg(T _aarg)
Trigonometry/angles - related.
Definition: MathDefs.hh:127