Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_GRID_FAST_BLUR_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_GRID_FAST_BLUR_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 namespace OpenTissue
00013 {
00014 namespace grid
00015 {
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 namespace detail
00063 {
00064 template <typename grid_type>
00065 inline void fast_blur ( grid_type & image, grid_type & tmp, double si, double sj, double sk, double log_base)
00066 {
00067 using std::min;
00068 using std::ceil;
00069 using std::floor;
00070
00071 typedef typename grid_type::value_type value_type;
00072 typedef typename grid_type::iterator iterator;
00073 typedef typename grid_type::index_iterator index_iterator;
00074 typedef typename grid_type::math_types math_types;
00075 typedef typename math_types::real_type real_type;
00076
00077 size_t I = image.I();
00078 size_t J = image.J();
00079 size_t K = image.K();
00080
00081 index_iterator s = image.begin();
00082 index_iterator s_end = image.end();
00083 iterator t = tmp.begin();
00084 for( ; s != s_end; ++s, ++t)
00085 {
00086 size_t i = s.i();
00087 size_t j = s.j();
00088 size_t k = s.k();
00089
00090 double dip = i + si;
00091 double dim = i - si;
00092 int ip = static_cast<int>( floor(dip) );
00093 int im = static_cast<int>( ceil(dim) );
00094
00095 double djp = j + sj;
00096 double djm = j - sj;
00097 int jp = static_cast<int>( floor(djp) );
00098 int jm = static_cast<int>( ceil(djm) );
00099
00100 double dkp = k + sk;
00101 double dkm = k - sk;
00102 int kp = static_cast<int>( floor(dkp) );
00103 int km = static_cast<int>( ceil(dkm) );
00104
00105 im = ( i ) ? im : 0;
00106 jm = ( j ) ? jm : 0;
00107 km = ( k ) ? km : 0;
00108 ip = min( ip, I - 1 );
00109 jp = min( jp, J - 1 );
00110 kp = min( kp, K - 1 );
00111
00112
00113 size_t idx_im = ( k * J + j ) * I + im;
00114 size_t idx_ip = ( k * J + j ) * I + ip;
00115 size_t idx_jm = ( k * J + jm ) * I + i;
00116 size_t idx_jp = ( k * J + jp ) * I + i;
00117 size_t idx_km = ( km * J + j ) * I + i;
00118 size_t idx_kp = ( kp * J + j ) * I + i;
00119
00120 real_type v000 = *s;
00121 real_type vm00 = image(idx_im);
00122 real_type vp00 = image(idx_ip);
00123 real_type v0m0 = image(idx_jm);
00124 real_type v0p0 = image(idx_jp);
00125 real_type v00m = image(idx_km);
00126 real_type v00p = image(idx_kp);
00127
00128
00129 double isum = -2.0*v000 + vp00 + vm00;
00130 if ( ip < I-1 )
00131 {
00132 real_type vpp00 = image( (k*J+j)*I+(ip+1) );
00133 isum += (dip-ip)*( vpp00 - vp00 );
00134 }
00135 if ( im > 0 )
00136 {
00137 real_type vmm00 = image( (k*J+j)*I+(im-1) );
00138 isum += (dim-im)*( vm00 - vmm00 );
00139 }
00140
00141
00142 double jsum = -2.0*v000 + v0p0 + v0m0;
00143 if ( jp < J-1 )
00144 {
00145 real_type v0pp0 = image( (k*J+(jp+1))*I+i );
00146 jsum += (djp-jp)*( v0pp0 - v0p0 );
00147 }
00148 if ( jm > 0 )
00149 {
00150 real_type v0mm0 = image( (k*J+(jm-1))*I+i );
00151 jsum += (djm-jm)*( v0m0 - v0mm0 );
00152 }
00153
00154
00155 double ksum = -2.0*v000 + v00p + v00m;
00156 if ( kp < K-1 )
00157 {
00158 real_type v00pp = image( ((kp+1)*J+j)*I+i );
00159 ksum += (dkp-kp)*( v00pp - v00p );
00160 }
00161 if ( km > 0 )
00162 {
00163 real_type v00mm = image( ((km-1)*J+j)*I+i );
00164 ksum += (dkm-km)*( v00m - v00mm );
00165 }
00166
00167 *t = static_cast<value_type>(v000 + log_base*(isum+jsum+ksum));
00168 }
00169
00170 image = tmp;
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 }
00269
00270 template <typename grid_type>
00271 inline void fast_blur ( grid_type & image, double sx, double sy, double sz, double log_base, size_t iterations)
00272 {
00273 using std::exp;
00274
00275 double base = exp(log_base);
00276 grid_type tmp(image);
00277 for( size_t i=0; i<iterations; ++i, sx*=base, sy*=base, sz*=base)
00278 {
00279 detail::fast_blur(image, tmp, sz, sy, sz, log_base);
00280 }
00281 }
00282
00283 template <typename grid_type>
00284 inline void fast_blur ( grid_type & image, double s, double log_base, size_t iterations)
00285 {
00286 fast_blur(image, s, s, s, log_base, iterations);
00287 }
00288 }
00289 }
00290
00291
00292 #endif