misc.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /* random stuff */
  16. #ifndef MISC_H
  17. #define MISC_H
  18. #include <cmath>
  19. #ifndef M_PI
  20. #define M_PI 3.141592653589793
  21. #endif
  22. namespace felzenszwalb{
  23. typedef unsigned char uchar;
  24. typedef struct { uchar r, g, b; } rgb;
  25. inline bool operator==(const rgb &a, const rgb &b) {
  26. return ((a.r == b.r) && (a.g == b.g) && (a.b == b.b));
  27. }
  28. template <class T>
  29. inline T abs(const T &x) { return (x > 0 ? x : -x); };
  30. template <class T>
  31. inline int sign(const T &x) { return (x >= 0 ? 1 : -1); };
  32. template <class T>
  33. inline T square(const T &x) { return x*x; };
  34. template <class T>
  35. inline T bound(const T &x, const T &min, const T &max) {
  36. return (x < min ? min : (x > max ? max : x));
  37. }
  38. template <class T>
  39. inline bool check_bound(const T &x, const T&min, const T &max) {
  40. return ((x < min) || (x > max));
  41. }
  42. inline int vlib_round(float x) { return (int)(x + 0.5F); }
  43. inline int vlib_round(double x) { return (int)(x + 0.5); }
  44. inline double gaussian(double val, double sigma) {
  45. return exp(-square(val/sigma)/2)/(sqrt(2*M_PI)*sigma);
  46. }
  47. }//namespace
  48. #endif