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 #ifndef OLENA_LRDE_UFMT_GENERIC_SIMPLIFIED_SALEMBIER_HH
00029 # define OLENA_LRDE_UFMT_GENERIC_SIMPLIFIED_SALEMBIER_HH
00030
00031
00032 # include <climits>
00033 # include <cfloat>
00034 # include <queue>
00035 # include <set>
00036 # include <map>
00037
00038 # include <algorithm>
00039 # include <iterator>
00040
00041 # include <oln/level/fill.hh>
00042 # include <oln/lrde/ufmt/utils.hh>
00043
00044
00045
00046 namespace oln
00047 {
00048
00049 namespace lrde
00050 {
00051
00052 namespace ufmt
00053 {
00054
00055
00056
00057
00058
00059 template <typename I, typename P>
00060 struct point_less
00061 {
00062
00063 };
00064
00065 template <typename I>
00066 struct point_less<I, oln::point2d>
00067 {
00068 point_less (const abstract::image<I>& f) :
00069 f_(f.exact())
00070 {
00071 }
00072
00073 bool
00074 operator() (const oln::point2d& p, const oln::point2d& q)
00075 {
00076
00077
00078
00079 if (p == q)
00080 return false;
00081
00082
00083
00084 float val_diff = f_[p] - f_[q];
00085 if (val_diff < 0.f)
00086 return true;
00087 else if (val_diff > 0.f)
00088 return false;
00089 else
00090 {
00091
00092 int row_diff = p.row() - q.row();
00093 if (row_diff < 0)
00094 return true;
00095 else if (row_diff > 0)
00096 return false;
00097 else
00098 {
00099
00100 int col_diff = p.col() - q.col();
00101 if (col_diff < 0)
00102 return true;
00103 else
00104 return false;
00105 }
00106 }
00107 }
00108
00109 const I& f_;
00110 };
00111
00112
00113
00114
00115
00116
00117 template <typename I, typename P>
00118 struct point_greater
00119 {
00120
00121 };
00122
00123 template <typename I>
00124 struct point_greater<I, oln::point2d>
00125 {
00126 point_greater (const abstract::image<I>& f) :
00127 f_(f.exact())
00128 {
00129 }
00130
00131 bool
00132 operator() (const oln::point2d& p, const oln::point2d& q)
00133 {
00134
00135
00136
00137 if (p == q)
00138 return false;
00139
00140
00141
00142 float val_diff = f_[p] - f_[q];
00143 if (val_diff > 0.f)
00144 return true;
00145 else if (val_diff < 0.f)
00146 return false;
00147 else
00148 {
00149
00150 int row_diff = p.row() - q.row();
00151 if (row_diff > 0)
00152 return true;
00153 else if (row_diff < 0)
00154 return false;
00155 else
00156 {
00157
00158 int col_diff = p.col() - q.col();
00159 if (col_diff > 0)
00160 return true;
00161 else
00162 return false;
00163 }
00164 }
00165 }
00166
00167 const I& f_;
00168 };
00169
00170
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 template <class I>
00219 struct generic_simplified_salembier
00220 {
00221 typedef oln_point_type(I) point;
00222 typedef oln_value_type(I) value;
00223 typedef oln_neighborhood_type(I) Nbh;
00224 typedef oln_iter_type(Nbh) niter;
00225
00226 const I& f;
00227 const Nbh& nbh;
00228
00229
00230
00231 typename mute<I, bool>::ret status;
00232
00233
00234
00235 typedef std::set< point, point_less<I, point> > node_at_level_t;
00236 node_at_level_t node_at_level;
00237
00238
00239 typedef std::set< point, point_less<I, point> > pset_t;
00240 typedef typename pset_t::const_iterator pset_iterator;
00241
00242 pset_t pset;
00243 point p_min;
00244
00245
00246 typedef std::set< point, point_greater<I, point> > sites_t;
00247 typedef typename sites_t::const_iterator site_iterator;
00248
00249
00250 sites_t sites;
00251
00252 std::map<point, point, point_less<I, point> > father;
00253
00254 # ifdef OLN_DEBUG
00255 unsigned npoints;
00256 # endif // OLN_DEBUG
00257
00258 generic_simplified_salembier(const abstract::image<I>& f,
00259 const oln_neighborhood_type(I)& nbh) :
00260 f(f.exact()),
00261 nbh(nbh),
00262 status(f.size()),
00263 node_at_level(point_less<I, point>(f.exact())),
00264 pset(point_less<I, point>(f.exact())),
00265 sites(point_greater<I, point>(f.exact())),
00266 father(point_less<I, point>(f.exact()))
00267 # ifdef OLN_DEBUG
00268 , npoints(f.npoints())
00269 # endif
00270 {
00271 }
00272
00273
00274 void go()
00275 {
00276 init();
00277
00278
00279
00280
00281 flood(p_min);
00282 }
00283
00284
00285 void init()
00286 {
00287 level::fill(status, false);
00288
00289 oln_iter_type(I) p(f);
00290 p = mlc::begin;
00291 p_min = p;
00292 value h_min = f[p];
00293 for_all(p)
00294 {
00295
00296 sites.insert(p);
00297
00298 if (f[p] < h_min)
00299 {
00300 h_min = f[p];
00301 p_min = p;
00302 }
00303 }
00304
00305
00306
00307
00308 pset.insert(p_min);
00309
00310
00311
00312
00313 }
00314
00315 # ifdef OLN_DEBUG
00316 void update_status (std::ostream& ostr)
00317 {
00318 static unsigned nupdates = 0;
00319 static const unsigned step = 1;
00320 static unsigned next_step = step;
00321 ++nupdates;
00322 unsigned percent = nupdates * 100 / npoints;
00323 if (percent >= next_step)
00324 {
00325
00326 std::cerr << percent << "% points processed" << std::endl;
00327
00328 while (percent >= next_step)
00329 next_step += step;
00330 }
00331 }
00332 # endif // OLN_DEBUG
00333
00334
00335
00336
00337 site_iterator flood(const point& h)
00338 {
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351 site_iterator m;
00352
00353 pset_iterator i;
00354 i = pset.find(h);
00355 while (i != pset.end())
00356 {
00357
00358
00359
00360
00361
00362
00363
00364
00365 point p = *i;
00366
00367
00368
00369 pset.erase(i);
00370
00371 status[p] = true;
00372 #ifdef OLN_DEBUG
00373 update_status(std::cerr);
00374 #endif // OLN_DEBUG
00375
00376
00377
00378
00379
00380
00381 oln_neighb_type(Nbh) q(nbh, p);
00382 for_all(q)
00383 {
00384
00385
00386
00387
00388 if (f.hold(q))
00389 {
00390 if (status[q] == false)
00391 {
00392 pset.insert(q);
00393
00394
00395
00396
00397
00398
00399
00400 status[q] = true;
00401 node_at_level.insert(q);
00402 if (f[q] > f[p])
00403 {
00404 m = sites.find(q);
00405 if (m == sites.end())
00406 abort();
00407 while (m != sites.end() and
00408 f[*m] > f[h])
00409 m = flood(*m);
00410 }
00411 }
00412 }
00413 }
00414 i = pset.find(h);
00415 }
00416
00417 m = sites.upper_bound(h);
00418 if (m != sites.end())
00419 ++m;
00420 while (m != sites.end() and
00421 node_at_level.find(*m) == node_at_level.end())
00422 ++m;
00423 if (m != sites.end())
00424 {
00425
00426
00427
00428 father[h] = *m;
00429 }
00430 else
00431 {
00432
00433 std::cerr << "father[" << h << "] is root" << std::endl;
00434
00435 father[h] = h;
00436 }
00437 node_at_level.erase(h);
00438 return m;
00439 }
00440
00441
00442 unsigned n_level_roots() const
00443 {
00444 return father.size();
00445 }
00446
00447 template <typename T>
00448 void
00449 print_image (const image2d<T>& ima, std::ostream& o)
00450 {
00451 for (coord row = 0; row < ima.nrows(); ++row)
00452 {
00453 for (coord col = 0; col < ima.ncols(); ++col)
00454 o << ima(row, col) << " ";
00455 o << std::endl;
00456 }
00457 }
00458
00459
00460 };
00461
00462
00463 }
00464
00465 }
00466
00467 }
00468
00469
00470 #endif // ! OLENA_LRDE_UFMT_GENERIC_SIMPLIFIED_SALEMBIER_HH