SLIP  1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
macros.hpp
Go to the documentation of this file.
1 /*
2  * Copyright(c):
3  * Signal Image and Communications (SIC) Department
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * - University of Poitiers, France http://www.univ-poitiers.fr
6  * - XLIM Institute UMR CNRS 7252 http://www.xlim.fr/
7  *
8  * and
9  *
10  * D2 Fluid, Thermic and Combustion
11  * - University of Poitiers, France http://www.univ-poitiers.fr
12  * - PPRIME Institute - UPR CNRS 3346 http://www.pprime.fr
13  * - ISAE-ENSMA http://www.ensma.fr
14  *
15  * Contributor(s):
16  * The SLIP team,
17  * Benoit Tremblais <tremblais_AT_sic.univ-poitiers.fr>,
18  * Laurent David <laurent.david_AT_lea.univ-poitiers.fr>,
19  * Ludovic Chatellier <ludovic.chatellier_AT_univ-poitiers.fr>,
20  * Lionel Thomas <lionel.thomas_AT_univ-poitiers.fr>,
21  * Denis Arrivault <arrivault_AT_sic.univ-poitiers.fr>,
22  * Julien Dombre <julien.dombre_AT_univ-poitiers.fr>.
23  *
24  * Description:
25  * The Simple Library of Image Processing (SLIP) is a new image processing
26  * library. It is written in the C++ language following as much as possible
27  * the ISO/ANSI C++ standard. It is consequently compatible with any system
28  * satisfying the ANSI C++ complience. It works on different Unix , Linux ,
29  * Mircrosoft Windows and Mac OS X plateforms. SLIP is a research library that
30  * was created by the Signal, Image and Communications (SIC) departement of
31  * the XLIM, UMR 7252 CNRS Institute in collaboration with the Fluids, Thermic
32  * and Combustion departement of the P', UPR 3346 CNRS Institute of the
33  * University of Poitiers.
34  *
35  * The SLIP Library source code has been registered to the APP (French Agency
36  * for the Protection of Programs) by the University of Poitiers and CNRS,
37  * under registration number IDDN.FR.001.300034.000.S.P.2010.000.21000.
38 
39  * http://www.sic.sp2mi.univ-poitiers.fr/slip/
40  *
41  * This software is governed by the CeCILL-C license under French law and
42  * abiding by the rules of distribution of free software. You can use,
43  * modify and/ or redistribute the software under the terms of the CeCILL-C
44  * license as circulated by CEA, CNRS and INRIA at the following URL
45  * http://www.cecill.info.
46  * As a counterpart to the access to the source code and rights to copy,
47  * modify and redistribute granted by the license, users are provided only
48  * with a limited warranty and the software's author, the holder of the
49  * economic rights, and the successive licensors have only limited
50  * liability.
51  *
52  * In this respect, the user's attention is drawn to the risks associated
53  * with loading, using, modifying and/or developing or reproducing the
54  * software by the user in light of its specific status of free software,
55  * that may mean that it is complicated to manipulate, and that also
56  * therefore means that it is reserved for developers and experienced
57  * professionals having in-depth computer knowledge. Users are therefore
58  * encouraged to load and test the software's suitability as regards their
59  * requirements in conditions enabling the security of their systems and/or
60  * data to be ensured and, more generally, to use and operate it in the
61  * same conditions as regards security.
62  *
63  * The fact that you are presently reading this means that you have had
64  * knowledge of the CeCILL-C license and that you accept its terms.
65  */
66 
67 
68 
76 #ifndef SLIP_MACROS_HPP
77 #define SLIP_MACROS_HPP
78 
79 #include <complex>
80 #include <cstring>
81 #include <limits>
82 #include <cmath>
83 
84 
85 
86 namespace slip
87 {
98  template<typename T1, typename T2> struct un_real : public std::unary_function<T1,T2>
99 {
101  T2 operator() (const T1& x){return std::real(x);}
102 };
112 template<typename T1, typename T2> struct un_imag : public std::unary_function<T1,T2>
113 {
115  T2 operator() (const T1& x){return std::imag(x);}
116 };
127 template<typename T1, typename T2>
128 struct un_abs : public std::unary_function<T1,T2>
129 {
130  un_abs(){}
131  T2 operator() (const T1& x){return (std::abs(x));}
132 };
146  template <class _Tp>
147  struct less_abs : public std::binary_function<_Tp, _Tp, bool>
148  {
149  inline
150  bool
151  operator()(const _Tp& __x, const _Tp& __y) const
152  {
153  return std::abs(__x) < std::abs( __y);
154  }
155  };
156 
170  template <class _Tp>
171  struct greater_abs : public std::binary_function<_Tp, _Tp, bool>
172  {
173  inline
174  bool
175  operator()(const _Tp& __x, const _Tp& __y) const
176  {
177  return std::abs(__x) > std::abs( __y);
178  }
179  };
180 
193  template <class _Tp>
194  struct mini : public std::binary_function<_Tp, _Tp, _Tp>
195  {
196  _Tp
197  operator()(const _Tp& __x, const _Tp& __y) const
198  { return std::min(__x,__y); }
199  };
200 
213  template <class _Tp>
214  struct maxi : public std::binary_function<_Tp, _Tp, _Tp>
215  {
216  _Tp
217  operator()(const _Tp& __x, const _Tp& __y) const
218  { return std::max(__x,__y); }
219  };
220 
221 
231  template<typename T>
232  inline
233  T sign(T a)
234  {
236  return (a/std::abs(a));
237  return T(1);
238  }
239 
240 template <typename T,typename R>
241 struct Sign : public std::unary_function<R,T>
242 {
244  {}
245  R operator() (const T& x)
246  {
247  R s = (x > T(0) ? R(1) : R(-1));
248  if(x == T(0))
249  {
250  s = R(0);
251  }
252  return s;
253  }
254 };
255 
256 
257 template <typename Complex>
258 struct complex_sign : public std::unary_function<Complex,Complex>
259 {
261  {}
262  Complex operator() (const Complex& x)
263  {
264  Complex r = Complex(1);
265  if( x != Complex(0))
266  {
267  r = x/std::abs(x);
268  }
269  return r;
270  }
271 };
272 
273 
287  template <typename Real>
288  inline
289  Real pythagore(const Real& x, const Real& y)
290  {
291  Real absx = std::abs(x), absy = std::abs(y);
292  if (absx > absy)
293  {
294  return(absx * std::sqrt(1.0+(absy/absx)*(absy/absx)));
295  }
296  else
297  {
298  return(absy == 0.0 ? 0.0 : absy * std::sqrt(1.0+(absx/absy)*(absx/absy)));
299  }
300  }
301 
302 
306  template<typename Iterator>
307  struct gt_it
308  {
309  bool operator()(Iterator s1, Iterator s2) const
310  {
311  return *s1 > *s2;
312  }
313  };
314 
318  template<typename Iterator>
319  struct lt_it
320  {
321  bool operator()(Iterator s1, Iterator s2) const
322  {
323  return *s1 < *s2;
324  }
325  };
326 
340  template <int N, typename T>
341  inline
342  T nth_power(T x)
343  {
344  T result = x;
345  for(int i = 1; i < N; ++i)
346  {
347  result *= x;
348  }
349  return result;
350  }
366  template <typename T,typename Integer>
367  inline
368  T power(T x, Integer N)
369  {
370  assert( N >= 0);
371  T result = T(1);
372  if(N !=0)
373  {
374  result = x;
375  for(Integer i = 1; i < N; ++i)
376  {
377  result = result * x;
378  }
379  }
380 
381  return result;
382  }
383 
384 
398 template<typename _Tp>
399 struct constants
400 {
402  static _Tp pi() throw()
403  { return static_cast<_Tp>(3.1415926535897932384626433832795029L); }
405  static _Tp pi_2() throw()
406  { return static_cast<_Tp>(1.5707963267948966192313216916397514L); }
408  static _Tp pi_3() throw()
409  { return static_cast<_Tp>(1.0471975511965977461542144610931676L); }
411  static _Tp pi_4() throw()
412  { return static_cast<_Tp>(0.7853981633974483096156608458198757L); }
414  static _Tp _1_pi() throw()
415  { return static_cast<_Tp>(0.3183098861837906715377675267450287L); }
417  static _Tp _2_sqrtpi() throw()
418  { return static_cast<_Tp>(1.1283791670955125738961589031215452L); }
420  static _Tp one() throw()
421  { return static_cast<_Tp>(1.0L); }
423  static _Tp two() throw()
424  { return static_cast<_Tp>(2.0L); }
426  static _Tp half() throw()
427  { return static_cast<_Tp>(0.5L); }
429  static _Tp sqrt2() throw()
430  { return static_cast<_Tp>(1.4142135623730950488016887242096981L); }
432  static _Tp sqrt3() throw()
433  { return static_cast<_Tp>(1.7320508075688772935274463415058723L); }
435  static _Tp sqrtpi_2() throw()
436  { return static_cast<_Tp>(1.2533141373155002512078826424055226L); }
438  static _Tp sqrt1_2() throw()
439  { return static_cast<_Tp>(0.7071067811865475244008443621048490L); }
441  static _Tp lnpi() throw()
442  { return static_cast<_Tp>(1.1447298858494001741434273513530587L); }
444  static _Tp gamma_e() throw()
445  { return static_cast<_Tp>(0.5772156649015328606065120900824024L); }
447  static _Tp euler() throw()
448  { return static_cast<_Tp>(2.7182818284590452353602874713526625L); }
449 };
450 
451 
452 } //::slip
453 #endif //SLIP_MACROS_HPP
T2 operator()(const T1 &x)
Definition: macros.hpp:115
T sign(T a)
Computes the sign of a.
Definition: macros.hpp:233
Computes the maximum value between two values.
Definition: macros.hpp:214
static _Tp _1_pi()
Constant .
Definition: macros.hpp:414
T power(T x, Integer N)
function to compute.
Definition: macros.hpp:368
T2 operator()(const T1 &x)
Definition: macros.hpp:101
static _Tp sqrtpi_2()
Constant .
Definition: macros.hpp:435
A structure for numeric constants.
Definition: macros.hpp:399
T & min(const GrayscaleImage< T > &M1)
Returns the min element of a GrayscaleImage.
static _Tp sqrt1_2()
Constant .
Definition: macros.hpp:438
slip::lin_alg_traits< T >::value_type epsilon()
Returns the epsilon value of a real or a complex.
bool operator()(Iterator s1, Iterator s2) const
Definition: macros.hpp:321
bool operator()(const _Tp &__x, const _Tp &__y) const
Definition: macros.hpp:151
_Tp operator()(const _Tp &__x, const _Tp &__y) const
Definition: macros.hpp:197
Real functor. Return the real part of x.
Definition: macros.hpp:98
R operator()(const T &x)
Definition: macros.hpp:245
static _Tp two()
Constant .
Definition: macros.hpp:423
Computes the minimum value between two values.
Definition: macros.hpp:194
HyperVolume< T > abs(const HyperVolume< T > &M)
static _Tp pi()
Constant .
Definition: macros.hpp:402
static _Tp sqrt2()
Constant .
Definition: macros.hpp:429
T nth_power(T x)
Computes the nth power of an element .
Definition: macros.hpp:342
Real pythagore(const Real &x, const Real &y)
Computes without destructive underflow or overflow.
Definition: macros.hpp:289
static _Tp lnpi()
Constant .
Definition: macros.hpp:441
static _Tp pi_4()
Constant .
Definition: macros.hpp:411
static _Tp half()
Constant .
Definition: macros.hpp:426
static _Tp pi_2()
Constant .
Definition: macros.hpp:405
Complex operator()(const Complex &x)
Definition: macros.hpp:262
static _Tp sqrt3()
Constant .
Definition: macros.hpp:432
Imag functor. Return the imaginary part of x.
Definition: macros.hpp:112
HyperVolume< T > sqrt(const HyperVolume< T > &M)
Compare two element according to their absolute value. Return true if std::abs(__x) > std::abs( __y)...
Definition: macros.hpp:171
Compare two element according to their absolute value. Return true if std::abs(__x) < std::abs( __y)...
Definition: macros.hpp:147
static _Tp pi_3()
Constant .
Definition: macros.hpp:408
static _Tp one()
Constant .
Definition: macros.hpp:420
T2 operator()(const T1 &x)
Definition: macros.hpp:131
_Tp operator()(const _Tp &__x, const _Tp &__y) const
Definition: macros.hpp:217
Abs functor. Return the absolute value of x.
Definition: macros.hpp:128
static _Tp _2_sqrtpi()
Constant .
Definition: macros.hpp:417
bool operator()(Iterator s1, Iterator s2) const
Definition: macros.hpp:309
static _Tp euler()
Constant Euler-Mascheroni .
Definition: macros.hpp:447
static _Tp gamma_e()
Constant Euler's constant .
Definition: macros.hpp:444
T & max(const GrayscaleImage< T > &M1)
Returns the max element of a GrayscaleImage.
bool operator()(const _Tp &__x, const _Tp &__y) const
Definition: macros.hpp:175