Signal/Geometry Processing Library (SPL)  1.1.24
math.hpp
Go to the documentation of this file.
1 // Copyright (c) 2011 Michael D. Adams
2 // All rights reserved.
3 
4 // __START_OF_LICENSE__
5 //
6 // Copyright (c) 2015 Michael D. Adams
7 // All rights reserved.
8 //
9 // This file is part of the Signal Processing Library (SPL).
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 3,
14 // or (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public
22 // License along with this program; see the file LICENSE. If not,
23 // see <http://www.gnu.org/licenses/>.
24 //
25 // __END_OF_LICENSE__
26 
32 #ifndef SPL_math_hpp
33 #define SPL_math_hpp
34 
36 // Header Files.
38 
39 #include <SPL/config.hpp>
40 #include <iostream>
41 #include <cmath>
42 #include <cassert>
43 #if defined(SPL_HAVE_TR1_BESSEL)
44 #include <tr1/cmath>
45 #else
46 //#include <boost/math/special_functions.hpp>
47 #include <boost/tr1/cmath.hpp>
48 #endif
49 
51 //
53 
54 namespace SPL {
55 
61 // Basic Mathematical Functions.
64 
72 template <class T>
73 inline T absVal(T x)
74 {
75  return (x >= 0) ? x : (-x);
76 }
77 
85 template <class T>
86 inline T signum(T x)
87 {
88  T result;
89  if (x > T(0)) {
90  result = T(1);
91  } else if (x < T(0)) {
92  result = T(-1);
93  } else {
94  result = T(0);
95  }
96  return result;
97 }
98 
106 template <class T>
107 T sqr(const T& x)
108 {
109  return x * x;
110 }
111 
115 template <class T>
116 inline T clip(T x, T min, T max)
117 {
118  assert(min <= max);
119  T result = x;
120  if (result < min) {
121  result = min;
122  }
123  if (result > max) {
124  result = max;
125  }
126  assert(result >= min && result <= max);
127  return result;
128 }
129 
137 inline double sinc(double x)
138 {
139  if (x) {
140  return sin(x) / x;
141  } else {
142  return 1.0;
143  }
144 }
145 
152 inline long roundTowardZeroDiv(long x, long y)
153 {
154  return x / y;
155 }
156 
164 inline long floorDiv(long x, long y)
165 {
166  assert(y);
167  long result;
168  if (y < 0) {
169  x = -x;
170  y = -y;
171  }
172  if (x >= 0) {
173  result = x / y;
174  } else {
175  result = x / y + ((x % y) ? (-1) : 0);
176  }
177  return result;
178 }
179 
183 template <class T>
184 inline T mod(T x, T y)
185 {
186  assert(y > 0);
187  if (x < 0) {
188  x += (((-x) / y) + 1) * y;
189  }
190  assert(x >= 0 && y > 0);
191  return x % y;
192 }
193 #if 0
194 inline long mod(long x, long y)
195 {
196  assert(y > 0);
197  if (x < 0) {
198  x += (((-x) / y) + 1) * y;
199  }
200  assert(x >= 0 && y > 0);
201  return x % y;
202 }
203 #endif
204 
212 inline long ceilDiv(long x, long y)
213 {
214  return floorDiv(x + y - 1, y);
215 }
216 
224 inline double radToDeg(double x)
225 {
226  return x * 180.0 / M_PI;
227 }
228 
236 inline double degToRad(double x)
237 {
238  return x * M_PI / 180.0;
239 }
240 
243 
244 #if defined(SPL_HAVE_TR1_BESSEL)
245 // Use the bessel functions from TR1.
246 using std::tr1::cyl_bessel_i;
247 #else
248 // Use the bessel functions from BOOST.
249 using std::tr1::cyl_bessel_i;
250 #endif
251 
254 
259 }
260 
261 #endif
long roundTowardZeroDiv(long x, long y)
Compute a quotient with the result rounded towards zero.
Definition: math.hpp:152
Definition: Arcball.hpp:48
T mod(T x, T y)
Compute the remainder after division.
Definition: math.hpp:184
T absVal(T x)
The absolute value function.
Definition: math.hpp:73
T clip(T x, T min, T max)
The clip function.
Definition: math.hpp:116
double radToDeg(double x)
Convert from radians to degrees.
Definition: math.hpp:224
double degToRad(double x)
Convert from degrees to radians.
Definition: math.hpp:236
long ceilDiv(long x, long y)
Compute the ceiling of a quotient.
Definition: math.hpp:212
long floorDiv(long x, long y)
Compute the floor of a quotient.
Definition: math.hpp:164
T signum(T x)
The signum function.
Definition: math.hpp:86
double sinc(double x)
The cardinal sine function.
Definition: math.hpp:137
T sqr(const T &x)
The square function.
Definition: math.hpp:107