Signal/Geometry Processing Library (SPL)  2.0.8
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_STD_BESSEL)
44 # if defined(SPL_HAVE_TR1_BESSEL)
45 # include <tr1/cmath>
46 # else
47 # include <boost/tr1/cmath.hpp>
48 # endif
49 #endif
50 
52 //
54 
55 namespace SPL {
56 
62 // Basic Mathematical Functions.
65 
73 template <class T>
74 inline T absVal(T x)
75 {
76  return (x >= 0) ? x : (-x);
77 }
78 
86 template <class T>
87 inline T signum(T x)
88 {
89  T result;
90  if (x > T(0)) {
91  result = T(1);
92  } else if (x < T(0)) {
93  result = T(-1);
94  } else {
95  result = T(0);
96  }
97  return result;
98 }
99 
107 template <class T>
108 inline T sqr(const T& x)
109 {
110  return x * x;
111 }
112 
116 template <class T>
117 inline T clip(T x, T min, T max)
118 {
119  assert(min <= max);
120  T result = x;
121  if (result < min) {
122  result = min;
123  }
124  if (result > max) {
125  result = max;
126  }
127  assert(result >= min && result <= max);
128  return result;
129 }
130 
138 inline double sinc(double x)
139 {
140  if (x) {
141  return sin(x) / x;
142  } else {
143  return 1.0;
144  }
145 }
146 
153 inline long roundTowardZeroDiv(long x, long y)
154 {
155  return x / y;
156 }
157 
165 inline long floorDiv(long x, long y)
166 {
167  assert(y);
168  long result;
169  if (y < 0) {
170  x = -x;
171  y = -y;
172  }
173  if (x >= 0) {
174  result = x / y;
175  } else {
176  result = x / y + ((x % y) ? (-1) : 0);
177  }
178  return result;
179 }
180 
184 template <class T>
185 inline T mod(T x, T y)
186 {
187  assert(y > 0);
188  if (x < 0) {
189  x += (((-x) / y) + 1) * y;
190  }
191  assert(x >= 0 && y > 0);
192  return x % y;
193 }
194 #if 0
195 inline long mod(long x, long y)
196 {
197  assert(y > 0);
198  if (x < 0) {
199  x += (((-x) / y) + 1) * y;
200  }
201  assert(x >= 0 && y > 0);
202  return x % y;
203 }
204 #endif
205 
213 inline long ceilDiv(long x, long y)
214 {
215  return floorDiv(x + y - 1, y);
216 }
217 
225 inline double radToDeg(double x)
226 {
227  return x * 180.0 / M_PI;
228 }
229 
237 inline double degToRad(double x)
238 {
239  return x * M_PI / 180.0;
240 }
241 
244 
245 #if defined(SPL_HAVE_STD_BESSEL)
246  using std::cyl_bessel_i;
247 #else
248 # if defined(SPL_HAVE_TR1_BESSEL)
249  // Use the bessel functions from TR1.
250  using std::tr1::cyl_bessel_i;
251 # else
252  // Use the bessel functions from BOOST.
253  using std::tr1::cyl_bessel_i;
254 # endif
255 #endif
256 
259 
264 }
265 
266 #endif
long roundTowardZeroDiv(long x, long y)
Compute a quotient with the result rounded towards zero.
Definition: math.hpp:153
Definition: Arcball.hpp:48
T mod(T x, T y)
Compute the remainder after division.
Definition: math.hpp:185
T absVal(T x)
The absolute value function.
Definition: math.hpp:74
T clip(T x, T min, T max)
The clip function.
Definition: math.hpp:117
double radToDeg(double x)
Convert from radians to degrees.
Definition: math.hpp:225
double degToRad(double x)
Convert from degrees to radians.
Definition: math.hpp:237
long ceilDiv(long x, long y)
Compute the ceiling of a quotient.
Definition: math.hpp:213
long floorDiv(long x, long y)
Compute the floor of a quotient.
Definition: math.hpp:165
T signum(T x)
The signum function.
Definition: math.hpp:87
double sinc(double x)
The cardinal sine function.
Definition: math.hpp:138
T sqr(const T &x)
The square function.
Definition: math.hpp:108