misc.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. typedef unsigned char uchar;
  23. typedef struct { uchar r, g, b; } rgb;
  24. inline bool operator==(const rgb &a, const rgb &b) {
  25. return ((a.r == b.r) && (a.g == b.g) && (a.b == b.b));
  26. }
  27. template <class T>
  28. inline T abs(const T &x) { return (x > 0 ? x : -x); };
  29. template <class T>
  30. inline int sign(const T &x) { return (x >= 0 ? 1 : -1); };
  31. template <class T>
  32. inline T square(const T &x) { return x*x; };
  33. template <class T>
  34. inline T bound(const T &x, const T &min, const T &max) {
  35. return (x < min ? min : (x > max ? max : x));
  36. }
  37. template <class T>
  38. inline bool check_bound(const T &x, const T&min, const T &max) {
  39. return ((x < min) || (x > max));
  40. }
  41. inline int vlib_round(float x) { return (int)(x + 0.5F); }
  42. inline int vlib_round(double x) { return (int)(x + 0.5); }
  43. inline double gaussian(double val, double sigma) {
  44. return exp(-square(val/sigma)/2)/(sqrt(2*M_PI)*sigma);
  45. }
  46. #endif