Horus Doc || C++ Reference || Class Overview   Pixels   Images   Detector   Geometry   Registry || Doxygen's quick Index  

HxUtilityMath.h

00001 /*
00002  *  Copyright (c) 1998, University of Amsterdam, The Netherlands.
00003  *  All rights reserved.
00004  *
00005  *
00006  *  Author(s):
00007  *  Silvia D. Olabarriaga  (silvia@wins.uva.nl)
00008  */
00009 
00010 #ifndef HxUtilityMath_h
00011 #define HxUtilityMath_h
00012 
00013 #include <cmath>
00014 
00015 #define EPS 1.0e-10
00016 
00017 inline int
00018 roundd(double a)
00019 {
00020      return ( a >= 0.0 ? ( (int) floor(a) + 0.5 ) :
00021         ( (int) ceil(a) - 0.5 ) );
00022 }
00023 
00024 //
00025 // square two numbers
00026 //
00027 
00028 inline 
00029 int square(int x)
00030 {
00031    return x*x;
00032 }
00033 
00034 inline 
00035 double square(double x)
00036 {
00037    return x*x;
00038 }
00039 
00040 //
00041 // swap two numbers
00042 //
00043 
00044 inline void 
00045 swap(int& x, int& y)
00046 {
00047    int tmp = x;
00048    x = y;
00049    y = tmp;
00050 }
00051 
00052 inline void 
00053 swap(double& x, double& y)
00054 {
00055    double tmp = x;
00056    x = y;
00057    y = tmp;
00058 }
00059 
00060 //
00061 // smaller of two numbers
00062 //
00063 
00064 inline int 
00065 minimum(int x, int y)
00066 {
00067    if (x < y)
00068       return x;
00069    else
00070       return y;
00071 }
00072 
00073 inline double 
00074 minimum(double x, double y)
00075 {
00076    if (x < y)
00077       return x;
00078    else
00079       return y;
00080 }
00081 
00082 //
00083 // larger of two numbers
00084 //
00085 
00086 inline int 
00087 maximum(int x, int y)
00088 {
00089    if (x > y)
00090       return x;
00091    else
00092       return y;
00093 }
00094 
00095 inline double 
00096 maximum(double x, double y)
00097 {
00098    if (x > y)
00099       return x;
00100    else
00101       return y;
00102 }
00103 
00104 //
00105 // absolute value
00106 //
00107 
00108 inline int 
00109 absolute(int x)
00110 {
00111    if (x < 0)
00112       x = -x;
00113    return x;
00114 }
00115 
00116 inline double 
00117 absolute(double x)
00118 {
00119    if (x < 0.0)
00120       x = -x;
00121    return x;
00122 }
00123 
00124 //
00125 // check if number in interval 
00126 //
00127 
00128 inline int 
00129 inRange(double x, double min, double max)
00130 {
00131    return (x >= min && x <= max);
00132 }
00133 
00134 //
00135 // compare doubles with precision
00136 //
00137 
00138 inline int 
00139 compare(double x, double y)
00140 {
00141    if (absolute(x - y) < EPS)
00142       return 1;
00143    else
00144       return 0;
00145 }
00146 
00147 //
00148 // clip number to interval
00149 //
00150 
00151 inline int 
00152 clip(int val, int min, int max)
00153 {
00154    if(val < min)
00155       val = min;
00156    if(val > max)
00157       val = max;
00158    return val;
00159 }
00160 
00161 inline double 
00162 clip(double val, double min, double max)
00163 {
00164    if(val < min)
00165       val = min;
00166    if(val > max)
00167       val = max;
00168    return val;
00169 }
00170 
00171 //
00172 // sign of a number
00173 //
00174 
00175 inline int 
00176 sign(double x)
00177 {
00178    if (x < 0.0)
00179     return -1;
00180    else if (x == 0.0)
00181         return 0;
00182    else return 1;
00183 }
00184 
00185 #endif 

Generated on Tue Feb 3 14:18:43 2004 for C++Reference by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001