00001
00040 #ifndef __FILL_2D__
00041 #define __FILL_2D__
00042
00043 namespace Function_2D{
00044
00056 template <typename Function,typename Array>
00057 void fill(const Function& f,
00058 Array* const array,
00059 const bool normalize_sum = false,
00060 const bool zero_in_the_middle = false);
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 template <typename Function,typename Array>
00083 void fill(const Function& f,
00084 Array* const array,
00085 const bool normalize_sum,
00086 const bool zero_in_the_middle){
00087
00088 typedef unsigned int size_type;
00089 typedef double real_type;
00090
00091 const size_type width = array->x_size();
00092 const size_type height = array->y_size();
00093
00094 const size_type half_width = width/2;
00095 const size_type half_height = height/2;
00096
00097 real_type sum = 0;
00098
00099 if (zero_in_the_middle){
00100
00101 for(size_type x=0;x<width;x++){
00102 for(size_type y=0;y<height;y++){
00103
00104 const real_type X = static_cast<real_type>(x) - 0.5*width;
00105 const real_type Y = static_cast<real_type>(y) - 0.5*height;
00106
00107 const real_type v = f(X,Y);
00108 (*array)(x,y) = v;
00109 sum += v;
00110 }
00111 }
00112
00113 }
00114 else{
00115
00116 for(size_type x=0;x<width;x++){
00117 for(size_type y=0;y<height;y++){
00118
00119 const real_type X = static_cast<real_type>(x) -
00120 width*((x+half_width)/width);
00121
00122 const real_type Y = static_cast<real_type>(y) -
00123 height*((y+half_height)/height);
00124
00125 const real_type v = f(X,Y);
00126 (*array)(x,y) = v;
00127 sum += v;
00128 }
00129 }
00130
00131 }
00132
00133 if (normalize_sum){
00134
00135 for(size_type x=0;x<width;x++){
00136 for(size_type y=0;y<height;y++){
00137 (*array)(x,y) /= sum;
00138 }
00139 }
00140
00141 }
00142
00143 }
00144
00145 }
00146
00147
00148 #endif