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