00001
00007 #ifndef __RADIAL_FUNCTION_H__
00008 #define __RADIAL_FUNCTION_H__
00009
00010 #include <cmath>
00011 #include <string>
00012
00013 #ifdef ANSI_COMPATIBLE
00014
00015 #include "ind_types.h"
00016 #endif
00017
00018
00019
00034 namespace RadialFunction {
00035
00037 template<class T>
00038 inline T Constant( T, T )
00039 { return 1.0; }
00040
00042 template<class T>
00043 inline T Gaussian( T x_sq, T sig_sq)
00044 { return exp( -x_sq/(2*M_PI*sig_sq) ); }
00045
00051 template<class T>
00052 inline T LoG( T x_sq, T sig_sq) {
00053 const T exponent = -x_sq/(2*sig_sq);
00054 return 1/(M_PI*sig_sq*sig_sq)*(1.0+exponent) * exp(exponent);
00055 }
00056
00057
00059 template<class matrix_type, class radial_function, class radius_type>
00060 matrix_type matrix
00061 (
00062 radial_function radial_fn,
00063 radius_type radius,
00064 typename matrix_type::size_type max_radius,
00065 string& errors,
00066 bool circular=true,
00067 bool normalize=true
00068 )
00069 {
00070 typedef typename matrix_type::value_type value_type;
00071 typedef typename matrix_type::size_type size_type;
00072
00073 const radius_type radius_sq = radius*radius;
00074 const radius_type max_radius_sq = max_radius*max_radius;
00075 const size_type kernelwidth = max_radius*2+1;
00076 matrix_type kernel(kernelwidth,kernelwidth);
00077
00078
00079 if (max_radius<=1) circular=false;
00080
00081
00082 value_type kernelsum=0;
00083
00084 int ki = -max_radius;
00085 for (size_type i=0; i < max_radius*2+1; ki++,i++) {
00086 int kj = -max_radius;
00087 for (size_type j=0; j < max_radius*2+1; kj++,j++) {
00088 const size_type ki_sq = ki*ki;
00089 const size_type kj_sq = kj*kj;
00090 const size_type r_sq = ki_sq + kj_sq;
00091
00092 if (circular && (r_sq > max_radius_sq))
00093 kernel[i][j] = 0;
00094 else {
00095 const value_type val = (radial_fn)(r_sq,radius_sq);
00096 kernelsum += val;
00097 kernel[i][j] = val;
00098 }
00099 }
00100 }
00101
00102
00103
00104 if (normalize) {
00105 if (kernelsum==0)
00106 errors += "Can't normalize a kernel that sums to zero";
00107 else
00108 kernel *= 1/kernelsum;
00109 }
00110
00111 return kernel;
00112 }
00113
00114 }
00115
00116
00117 #endif