Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

/home/slang/XVision2/src/Tools/XVTools.h

00001 // *** BEGIN_XVISION2_COPYRIGHT_NOTICE ***
00002 // *** END_XVISION2_COPYRIGHT_NOTICE ***
00003 
00004 //-----------------------------------------------------------------------------
00005 //
00006 //  XVTools.h
00007 //
00008 //  Globally useful definitions for Tools system.
00009 //
00010 //  6 - 6 - 93
00011 //  Sidd Puri
00012 //
00013 //  2.7.01  
00014 //  Sam Lang  -  Added modifications
00015 //
00016 //-----------------------------------------------------------------------------
00017 
00018 #ifndef _XVTOOLS_H_
00019 #define _XVTOOLS_H_
00020 
00021 #include <sys/types.h>
00022 #include <math.h>
00023 
00024 //-----------------------------------------------------------------------------
00025 //  Global inline functions
00026 //-----------------------------------------------------------------------------
00027 
00028 template<class T> inline T min2 (T a, T b)
00029 { return a < b? a: b; }
00030 
00031 template<class T> inline T max2 (T a, T b)
00032 { return a > b? a: b; }
00033 
00034 #ifndef __SC__
00035 template<class T> inline T abs (T x)
00036 { return x > 0? x: -x; }
00037 
00038 template<class T> inline int signum (T x)
00039 { return x > 0? 1: -1; }
00040 
00041 #else
00042 #include <stdlib.h>
00043 #define signum(x) (x >= 0)
00044 #endif
00045 
00046 inline bool odd (int n)
00047 { return (bool)(n & 1); }
00048 
00049 template <class T>
00050 inline int round (T x)
00051 { return signum(x)*int (abs(x) + .5); }
00052 
00053 template <class T>
00054 inline T rint (T x)
00055 { return (T)signum(x)*int (abs(x) + .5); }
00056 
00057 inline float half (int l)
00058 { return float (l) / 2; }
00059 
00060 inline float rad_deg (float r)
00061 { return r / M_PI * 180; }
00062 
00063 inline float deg_rad (float d)
00064 { return d / 180 * M_PI; }
00065 
00066 inline float radmod (float op, float div)
00067 { return abs(op - (((int)(op / div)) * div)); }
00068 
00069 template<class T> inline T sqr (T x)
00070 { return x*x;}
00071 
00072 template<class T> inline void copy (T *dest, T *src,int n)
00073 {memcpy(dest,src,n*sizeof(T));}
00074 
00075 inline void bitset(unsigned &bits, int n) {
00076   bits |= (0x1 << n);
00077 }
00078 
00079 inline int isset(unsigned bits, int n) {
00080   return ((bits & (0x1 << n)) != 0);
00081 }
00082 
00083 inline void bitclear(unsigned &bits, int n) {
00084   bits &= ~(0x1 << n);
00085 }
00086 
00087 inline void bittoggle(unsigned &bits,int n, int flag) {
00088   if (flag)
00089     bitset(bits,n);
00090   else
00091     bitclear(bits,n);
00092 }
00093 
00094 template <class T>
00095 inline T avg(const T * values, int num){
00096 
00097   T sum = 0;
00098   for(int i = 0; i < num; i++){
00099 
00100     sum += pixels[i];
00101   }
00102   return sum / num;
00103 };
00104 
00105 template <class T>
00106 inline T max(const T * arr, const int size, T & max, int & index){
00107 
00108   max = arr[0];
00109   index = 0;
00110   for(int i = 1; i < size; i++){
00111 
00112     if(max < arr[i]){ index = i; max = arr[i]; }
00113   }
00114   return max;
00115 };
00116 
00117 template <class T>
00118 inline T min(const T * arr, const int size, T & min = NULL, int & index = NULL){
00119 
00120   min = arr[0];
00121   index = 0;
00122   for(int i = 1; i < size; i++){
00123 
00124     if(min > arr[i]){ index = i; min = arr[i]; }
00125   }
00126   return min;
00127 };
00128 
00129 #include <XVGeometry.h>
00130 
00131 template <class T>
00132 inline XV2Vec<T> min(const XV2Vec<T> * arr, const int size){
00133 
00134   XV2Vec<T> minres = arr[0];
00135   for(int i = 1; i < size; ++i){
00136     if(minres.x() > arr[i].x()) minres.setX(arr[i].x());
00137     if(minres.y() > arr[i].y()) minres.setY(arr[i].y());
00138   }
00139   return minres;
00140 };
00141 
00142 template <class T>
00143 inline XV2Vec<T> max(const XV2Vec<T> * arr, const int size){
00144 
00145   XV2Vec<T> maxres = arr[0];
00146   for(int i = 1; i < size; ++i){
00147     if(maxres.x() < arr[i].x()) maxres.setX(arr[i].x());
00148     if(maxres.y() < arr[i].y()) maxres.setY(arr[i].y());
00149   }
00150   return maxres;
00151 };
00152 
00153 #endif

Generated at Thu Mar 29 22:37:28 2001 for XVision by doxygen1.2.0 written by Dimitri van Heesch, © 1997-2000