00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_GRID_AOF_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_GRID_AOF_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012
00013 #include <OpenTissue/core/containers/grid/util/grid_gradient.h>
00014 #include <OpenTissue/core/containers/grid/util/grid_coord2idx.h>
00015 #include <OpenTissue/core/containers/grid/util/grid_idx2coord.h>
00016 #include <OpenTissue/core/containers/grid/util/grid_gradient_at_point.h>
00017
00018 #include <cmath>
00019
00020 namespace OpenTissue
00021 {
00022 namespace grid
00023 {
00024
00025 namespace detail
00026 {
00027
00040 template<typename grid_type>
00041 inline typename grid_type::value_type
00042 compute_aof_value(
00043 grid_type const & phi
00044 , size_t const & i
00045 , size_t const & j
00046 , size_t const & k
00047 , double scale = 0.5
00048 )
00049 {
00050 using std::min;
00051 using std::sqrt;
00052
00053 typedef typename grid_type::iterator iterator;
00054 typedef typename grid_type::const_index_iterator const_index_iterator;
00055 typedef typename grid_type::value_type value_type;
00056 typedef typename grid_type::math_types math_types;
00057 typedef typename math_types::vector3_type vector3_type;
00058 typedef typename math_types::real_type real_type;
00059
00060 real_type dx = phi.dx()*scale;
00061 real_type dy = phi.dy()*scale;
00062 real_type dz = phi.dz()*scale;
00063
00064 vector3_type n[26];
00065 vector3_type dp[26];
00066
00067 dp[0] = vector3_type( dx, 0, 0);
00068 dp[1] = vector3_type(-dx, 0, 0);
00069 dp[2] = vector3_type( 0, dy, 0);
00070 dp[3] = vector3_type( 0,-dy, 0);
00071 dp[4] = vector3_type( 0, 0, dz);
00072 dp[5] = vector3_type( 0, 0,-dz);
00073 dp[6] = vector3_type( dx, dy, dz);
00074 dp[7] = vector3_type( dx, dy,-dz);
00075 dp[8] = vector3_type( dx,-dy, dz);
00076 dp[9] = vector3_type( dx,-dy,-dz);
00077 dp[10] = vector3_type(-dx, dy, dz);
00078 dp[11] = vector3_type(-dx, dy,-dz);
00079 dp[12] = vector3_type(-dx,-dy, dz);
00080 dp[13] = vector3_type(-dx,-dy,-dz);
00081 dp[14] = vector3_type( dx, dy, 0);
00082 dp[15] = vector3_type( dx,-dy, 0);
00083 dp[16] = vector3_type(-dx, dy, 0);
00084 dp[17] = vector3_type(-dx,-dy, 0);
00085 dp[18] = vector3_type( dx, 0, dz);
00086 dp[19] = vector3_type( dx, 0,-dz);
00087 dp[20] = vector3_type(-dx, 0, dz);
00088 dp[21] = vector3_type(-dx, 0,-dz);
00089 dp[22] = vector3_type( 0, dy, dz);
00090 dp[23] = vector3_type( 0, dy,-dz);
00091 dp[24] = vector3_type( 0,-dy, dz);
00092 dp[25] = vector3_type( 0,-dy,-dz);
00093 for(size_t cnt=0;cnt<26u;++cnt)
00094 n[cnt] = unit(dp[cnt]);
00095
00096 vector3_type p;
00097 idx2coord(phi,i,j,k,p);
00098 real_type flux = real_type();
00099 for(size_t cnt=0;cnt<26u;++cnt)
00100 {
00101 vector3_type q = p + dp[cnt];
00102 vector3_type g = gradient_at_point(phi,q);
00103 flux += unit(g)*n[cnt];
00104 }
00105 flux /= 26.0;
00106 return static_cast<value_type>(flux);
00107 }
00108
00109 }
00110
00111
00118 template<typename grid_type>
00119 inline void aof(
00120 grid_type const & phi
00121 , grid_type & F
00122 )
00123 {
00124 using std::min;
00125 using std::sqrt;
00126
00127 typedef typename grid_type::iterator iterator;
00128 typedef typename grid_type::const_index_iterator const_index_iterator;
00129 typedef typename grid_type::value_type value_type;
00130 typedef typename grid_type::math_types math_types;
00131 typedef typename math_types::vector3_type vector3_type;
00132 typedef typename math_types::real_type real_type;
00133
00134 value_type const unused = phi.unused();
00135 size_t const & I = phi.I();
00136 size_t const & J = phi.J();
00137 size_t const & K = phi.K();
00138 F.create(phi.min_coord(),phi.max_coord(),I,J,K);
00139
00140 real_type dx = phi.dx()*0.5;
00141 real_type dy = phi.dy()*0.5;
00142 real_type dz = phi.dz()*0.5;
00143
00144 vector3_type n[26];
00145 vector3_type dp[26];
00146 dp[0] = vector3_type( dx, 0, 0);
00147 dp[1] = vector3_type(-dx, 0, 0);
00148 dp[2] = vector3_type( 0, dy, 0);
00149 dp[3] = vector3_type( 0,-dy, 0);
00150 dp[4] = vector3_type( 0, 0, dz);
00151 dp[5] = vector3_type( 0, 0,-dz);
00152 dp[6] = vector3_type( dx, dy, dz);
00153 dp[7] = vector3_type( dx, dy,-dz);
00154 dp[8] = vector3_type( dx,-dy, dz);
00155 dp[9] = vector3_type( dx,-dy,-dz);
00156 dp[10] = vector3_type(-dx, dy, dz);
00157 dp[11] = vector3_type(-dx, dy,-dz);
00158 dp[12] = vector3_type(-dx,-dy, dz);
00159 dp[13] = vector3_type(-dx,-dy,-dz);
00160 dp[14] = vector3_type( dx, dy, 0);
00161 dp[15] = vector3_type( dx,-dy, 0);
00162 dp[16] = vector3_type(-dx, dy, 0);
00163 dp[17] = vector3_type(-dx,-dy, 0);
00164 dp[18] = vector3_type( dx, 0, dz);
00165 dp[19] = vector3_type( dx, 0,-dz);
00166 dp[20] = vector3_type(-dx, 0, dz);
00167 dp[21] = vector3_type(-dx, 0,-dz);
00168 dp[22] = vector3_type( 0, dy, dz);
00169 dp[23] = vector3_type( 0, dy,-dz);
00170 dp[24] = vector3_type( 0,-dy, dz);
00171 dp[25] = vector3_type( 0,-dy,-dz);
00172 for(size_t i=0;i<26u;++i)
00173 n[i] = unit(dp[i]);
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 iterator f = F.begin();
00203 const_index_iterator v = phi.begin();
00204 const_index_iterator vend = phi.end();
00205 for(;v!=vend;++v,++f)
00206 {
00207 if((*v)==unused)
00208 {
00209 (*f) = value_type();
00210 continue;
00211 }
00212 size_t i = v.i();
00213 size_t j = v.j();
00214 size_t k = v.k();
00215 vector3_type p;
00216 idx2coord(phi,i,j,k,p);
00217 real_type flux = real_type();
00218 for(size_t i=0;i<26u;++i)
00219 {
00220 vector3_type q = p + dp[i];
00221 vector3_type g = gradient_at_point(phi,q);
00222 flux += unit(g)*n[i];
00223 }
00224 flux /= 26.0;
00225 (*f) = static_cast<value_type>(flux);
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
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 }
00361
00362 }
00363 }
00364
00365
00366 #endif