00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00047
00048
00049
00050 #include "SingleRegionAlgorithm.h"
00051
00052 #include "BorderCondition.h"
00053 #include <assert.h>
00054 #include <limits>
00055 #include "Mask.h"
00056 #include "simple_tools.h"
00057 #include "level_set.h"
00058 #include "cimg_dependent.h"
00059
00060 using namespace std;
00061
00062 namespace lsseg {
00063
00064
00065 double develop_single_region_2D(Region& reg, int num_iter, int reinit_modulo, const Mask* geom_mask)
00066
00067 {
00068
00069 const int MASK_WIDTH = 2;
00070 const BorderCondition bcond = NEUMANN;
00071
00072 assert(int(reg.phi.dimz()) == 1);
00073 assert(!geom_mask || geom_mask->numChannels() == 1);
00074 assert(!geom_mask || reg.phi.size_compatible(*geom_mask));
00075
00076 LevelSetFunction curv_term(reg.phi, false);
00077 LevelSetFunction norm_term(reg.phi, false);
00078 Mask old_mask, mask(reg.phi, false);
00079 if (geom_mask) {
00080 mask = *geom_mask;
00081 } else {
00082 mask = 1;
00083 }
00084 const double mu = reg.mu;
00085 double time_accum = 0;
00086 for (int i = 0; i < num_iter; ++i) {
00087
00088
00089 mask.swap(old_mask);
00090 make_border_mask(reg.phi, mask, MASK_WIDTH, &old_mask);
00091 if (geom_mask) {
00092 mask.intersect(*geom_mask);
00093 }
00094
00095
00096 reg.phi.curvatureTimesGrad2D(curv_term, &mask);
00097
00098
00099 reg.fgen->update(reg.phi);
00100 reg.fgen->force(norm_term, &mask);
00101
00102 double H1, H2;
00103
00104 normal_direction_flow_2D(reg.phi,
00105 norm_term,
00106 bcond,
00107 H1,
00108 H2,
00109 &mask,
00110 geom_mask);
00111
00112
00113 const double dt = double(1) / (4 * reg.mu + H1 + H2);
00114
00115 const double* nt = norm_term.begin();
00116 const double* ct = curv_term.begin();
00117 const char* msk = mask.begin();
00118 const double* end = reg.phi.end();
00119 for (double* cur = reg.phi.begin(); cur != end; ++cur, ++nt, ++msk, ++ct) {
00120 if (*msk) {
00121 *cur += dt * (mu * (*ct) + (*nt));
00122 }
00123 }
00124 time_accum += dt;
00125 if (reinit_modulo && ((i+1) % reinit_modulo == 0)) {
00126 reg.phi.reinitialize2D(&mask);
00127 }
00128 }
00129 return time_accum;
00130 }
00131
00132
00133 double develop_single_region_3D(Region& reg, int num_iter, int reinit_modulo, const Mask* geom_mask)
00134
00135 {
00136
00137 const int MASK_WIDTH = 2;
00138 const BorderCondition bcond = NEUMANN;
00139
00140 assert(!geom_mask || geom_mask->numChannels() == 1);
00141 assert(!geom_mask || reg.phi.size_compatible(*geom_mask));
00142
00143 LevelSetFunction curv_term(reg.phi, false);
00144 LevelSetFunction norm_term(reg.phi, false);
00145 Mask old_mask, mask(reg.phi, false);
00146 if (geom_mask) {
00147 mask = *geom_mask;
00148 } else {
00149 mask = 1;
00150 }
00151
00152 const double mu = reg.mu;
00153 double time_accum = 0;
00154 for (int i = 0; i < num_iter; ++i) {
00155
00156
00157
00158 mask.swap(old_mask);
00159 make_border_mask(reg.phi, mask, MASK_WIDTH, &old_mask);
00160 if (geom_mask) {
00161 mask.intersect(*geom_mask);
00162 }
00163
00164
00165 reg.phi.curvatureTimesGrad3D(curv_term, &mask);
00166
00167
00168 reg.fgen->update(reg.phi);
00169 reg.fgen->force(norm_term, &mask);
00170
00171 double H1, H2, H3;
00172
00173 normal_direction_flow_3D(reg.phi,
00174 norm_term,
00175 bcond,
00176 H1,
00177 H2,
00178 H3,
00179 &mask,
00180 geom_mask);
00181
00182
00183 const double dt = double(1) / (4 * reg.mu + H1 + H2 + H3);
00184
00185 const double* nt = norm_term.begin();
00186 const double* ct = curv_term.begin();
00187 const char* msk = mask.begin();
00188 const double* end = reg.phi.end();
00189 for (double* cur = reg.phi.begin(); cur != end; ++cur, ++nt, ++msk, ++ct) {
00190 if (*msk) {
00191 *cur += dt * (mu * (*ct) + (*nt));
00192 }
00193 }
00194 time_accum += dt;
00195 if (reinit_modulo && ((i+1) % reinit_modulo == 0)) {
00196 reg.phi.reinitialize3D(&mask);
00197 }
00198 }
00199 return time_accum;
00200 }
00201
00202
00203
00204 };
00205
00206