Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_GRID_AVERAGE_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_GRID_AVERAGE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <cmath>
00013
00014 namespace OpenTissue
00015 {
00016 namespace grid
00017 {
00018
00019 template < typename grid_type >
00020 inline void average( grid_type & phi )
00021 {
00022 using std::min;
00023
00024 typedef typename grid_type::index_iterator iterator;
00025 typedef typename grid_type::value_type value_type;
00026
00027 grid_type tmp = phi;
00028
00029 size_t I = phi.I();
00030 size_t J = phi.J();
00031 size_t K = phi.K();
00032
00033 iterator pend = phi.end();
00034 iterator p = phi.begin();
00035 iterator t = tmp.begin();
00036
00037 for(;p!=pend;++p,++t)
00038 {
00039 size_t i = p.i();
00040 size_t j = p.j();
00041 size_t k = p.k();
00042
00043 static size_t idx[27];
00044 size_t im1 = ( i ) ? i - 1 : 0;
00045 size_t jm1 = ( j ) ? j - 1 : 0;
00046 size_t km1 = ( k ) ? k - 1 : 0;
00047 size_t ip1 = min( i + 1u, I - 1u );
00048 size_t jp1 = min( j + 1u, J - 1u );
00049 size_t kp1 = min( k + 1u, K - 1u );
00050 idx[0] = ( kp1 * J + jp1 ) * I + im1;
00051 idx[1] = ( k * J + jp1 ) * I + im1;
00052 idx[2] = ( km1 * J + jp1 ) * I + im1;
00053 idx[3] = ( kp1 * J + j ) * I + im1;
00054 idx[4] = ( k * J + j ) * I + im1;
00055 idx[5] = ( km1 * J + j ) * I + im1;
00056 idx[6] = ( kp1 * J + jm1 ) * I + im1;
00057 idx[7] = ( k * J + jm1 ) * I + im1;
00058 idx[8] = ( km1 * J + jm1 ) * I + im1;
00059 idx[9] = ( kp1 * J + jp1 ) * I + i;
00060 idx[10] = ( k * J + jp1 ) * I + i;
00061 idx[11] = ( km1 * J + jp1 ) * I + i;
00062 idx[12] = ( kp1 * J + j ) * I + i;
00063 idx[13] = ( k * J + j ) * I + i;
00064 idx[14] = ( km1 * J + j ) * I + i;
00065 idx[15] = ( kp1 * J + jm1 ) * I + i;
00066 idx[16] = ( k * J + jm1 ) * I + i;
00067 idx[17] = ( km1 * J + jm1 ) * I + i;
00068 idx[18] = ( kp1 * J + jp1 ) * I + ip1;
00069 idx[19] = ( k * J + jp1 ) * I + ip1;
00070 idx[20] = ( km1 * J + jp1 ) * I + ip1;
00071 idx[21] = ( kp1 * J + j ) * I + ip1;
00072 idx[22] = ( k * J + j ) * I + ip1;
00073 idx[23] = ( km1 * J + j ) * I + ip1;
00074 idx[24] = ( kp1 * J + jm1 ) * I + ip1;
00075 idx[25] = ( k * J + jm1 ) * I + ip1;
00076 idx[26] = ( km1 * J + jm1 ) * I + ip1;
00077 value_type avg = value_type();
00078 for(size_t i=0;i<27u;++i)
00079 avg += phi(idx[i]);
00080 avg /= value_type(25);
00081 *t = avg;
00082 }
00083 phi = tmp;
00084 }
00085
00086 }
00087 }
00088
00089
00090 #endif