OpenMesh
MathDefs.hh
1/* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2023, 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
55namespace OpenMesh
56{
57
60template <class T, typename Real>
61inline bool is_zero(const T& _a, Real _eps)
62{ return fabs(_a) < _eps; }
63
64template <class T1, class T2, typename Real>
65inline bool is_eq(const T1& a, const T2& b, Real _eps)
66{ return is_zero(a-b, _eps); }
67
68template <class T1, class T2, typename Real>
69inline bool is_gt(const T1& a, const T2& b, Real _eps)
70{ return (a > b) && !is_eq(a,b,_eps); }
71
72template <class T1, class T2, typename Real>
73inline bool is_ge(const T1& a, const T2& b, Real _eps)
74{ return (a > b) || is_eq(a,b,_eps); }
75
76template <class T1, class T2, typename Real>
77inline bool is_lt(const T1& a, const T2& b, Real _eps)
78{ return (a < b) && !is_eq(a,b,_eps); }
79
80template <class T1, class T2, typename Real>
81inline 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;
85const double dbl_eps__ = 10*DBL_EPSILON;*/
86const float flt_eps__ = (float)1e-05;
87const double dbl_eps__ = 1e-09;
88
89inline float eps__(float)
90{ return flt_eps__; }
91
92inline double eps__(double)
93{ return dbl_eps__; }
94
95template <class T>
96inline bool is_zero(const T& a)
97{ return is_zero(a, eps__(a)); }
98
99template <class T1, class T2>
100inline bool is_eq(const T1& a, const T2& b)
101{ return is_zero(a-b); }
102
103template <class T1, class T2>
104inline bool is_gt(const T1& a, const T2& b)
105{ return (a > b) && !is_eq(a,b); }
106
107template <class T1, class T2>
108inline bool is_ge(const T1& a, const T2& b)
109{ return (a > b) || is_eq(a,b); }
110
111template <class T1, class T2>
112inline bool is_lt(const T1& a, const T2& b)
113{ return (a < b) && !is_eq(a,b); }
114
115template <class T1, class T2>
116inline bool is_le(const T1& a, const T2& b)
117{ return (a < b) || is_eq(a,b); }
118
120
121template <class T>
122inline 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
139template <class T>
140T 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
146template <class T>
147inline T positive_angle(T _angle)
148{ return _angle < 0 ? (2*M_PI + _angle) : _angle; }
149
150template <class T>
151inline T positive_angle(T _cos_angle, T _sin_angle)
152{ return positive_angle(angle(_cos_angle, _sin_angle)); }
153
154template <class T>
155inline T deg_to_rad(const T& _angle)
156{ return M_PI*(_angle/180); }
157
158template <class T>
159inline T rad_to_deg(const T& _angle)
160{ return 180*(_angle/M_PI); }
161
162inline double log_(double _value)
163{ return log(_value); }
164
165}//namespace OpenMesh
166
167#endif//MATHDEFS_HH
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
T angle(T _cos_angle, T _sin_angle)
returns the angle determined by its cos and the sign of its sin result is positive if the angle is in...
Definition: MathDefs.hh:140
bool is_zero(const T &_a, Real _eps)
comparison operators with user-selected precision control
Definition: MathDefs.hh:61
T sane_aarg(T _aarg)
Trigonometry/angles - related.
Definition: MathDefs.hh:122

Project OpenMesh, ©  Visual Computing Institute, RWTH Aachen. Documentation generated using doxygen .