Developer Documentation
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 #ifndef MATHDEFS_HH
46 #define MATHDEFS_HH
47 
48 #include <cmath>
49 #include <cfloat>
50 
51 #ifndef M_PI
52  #define M_PI 3.14159265359
53 #endif
54 
55 namespace OpenMesh
56 {
57 
60 template <class T, typename Real>
61 inline bool is_zero(const T& _a, Real _eps)
62 { return fabs(_a) < _eps; }
63 
64 template <class T1, class T2, typename Real>
65 inline bool is_eq(const T1& a, const T2& b, Real _eps)
66 { return is_zero(a-b, _eps); }
67 
68 template <class T1, class T2, typename Real>
69 inline bool is_gt(const T1& a, const T2& b, Real _eps)
70 { return (a > b) && !is_eq(a,b,_eps); }
71 
72 template <class T1, class T2, typename Real>
73 inline bool is_ge(const T1& a, const T2& b, Real _eps)
74 { return (a > b) || is_eq(a,b,_eps); }
75 
76 template <class T1, class T2, typename Real>
77 inline bool is_lt(const T1& a, const T2& b, Real _eps)
78 { return (a < b) && !is_eq(a,b,_eps); }
79 
80 template <class T1, class T2, typename Real>
81 inline bool is_le(const T1& a, const T2& b, Real _eps)
82 { return (a < b) || is_eq(a,b,_eps); }
83 
84 /*const float flt_eps__ = 10*FLT_EPSILON;
85 const double dbl_eps__ = 10*DBL_EPSILON;*/
86 const float flt_eps__ = (float)1e-05;
87 const double dbl_eps__ = 1e-09;
88 
89 inline float eps__(float)
90 { return flt_eps__; }
91 
92 inline double eps__(double)
93 { return dbl_eps__; }
94 
95 template <class T>
96 inline bool is_zero(const T& a)
97 { return is_zero(a, eps__(a)); }
98 
99 template <class T1, class T2>
100 inline bool is_eq(const T1& a, const T2& b)
101 { return is_zero(a-b); }
102 
103 template <class T1, class T2>
104 inline bool is_gt(const T1& a, const T2& b)
105 { return (a > b) && !is_eq(a,b); }
106 
107 template <class T1, class T2>
108 inline bool is_ge(const T1& a, const T2& b)
109 { return (a > b) || is_eq(a,b); }
110 
111 template <class T1, class T2>
112 inline bool is_lt(const T1& a, const T2& b)
113 { return (a < b) && !is_eq(a,b); }
114 
115 template <class T1, class T2>
116 inline bool is_le(const T1& a, const T2& b)
117 { return (a < b) || is_eq(a,b); }
118 
120 
121 template <class T>
122 inline T sane_aarg(T _aarg)
123 {
124  if (_aarg < -1)
125  {
126  _aarg = -1;
127  }
128  else if (_aarg > 1)
129  {
130  _aarg = 1;
131  }
132  return _aarg;
133 }
134 
139 template <class T>
140 T angle(T _cos_angle, T _sin_angle)
141 {//sanity checks - otherwise acos will return nan
142  _cos_angle = sane_aarg(_cos_angle);
143  return (T) _sin_angle >= 0 ? acos(_cos_angle) : -acos(_cos_angle);
144 }
145 
146 template <class T>
147 inline T positive_angle(T _angle)
148 { return _angle < 0 ? (2*M_PI + _angle) : _angle; }
149 
150 template <class T>
151 inline T positive_angle(T _cos_angle, T _sin_angle)
152 { return positive_angle(angle(_cos_angle, _sin_angle)); }
153 
154 template <class T>
155 inline T deg_to_rad(const T& _angle)
156 { return M_PI*(_angle/180); }
157 
158 template <class T>
159 inline T rad_to_deg(const T& _angle)
160 { return 180*(_angle/M_PI); }
161 
162 inline double log_(double _value)
163 { return log(_value); }
164 
165 }//namespace OpenMesh
166 
167 #endif//MATHDEFS_HH
T sane_aarg(T _aarg)
Trigonometry/angles - related.
Definition: MathDefs.hh:122
T angle(T _cos_angle, T _sin_angle)
Definition: MathDefs.hh:140
bool is_zero(const T &_a, Real _eps)
Definition: MathDefs.hh:61