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_SP_MAXTREE_HH
00029 # define OLENA_LRDE_UFMT_SP_MAXTREE_HH
00030
00031 # include <algorithm>
00032 # include <vector>
00033
00034 # include <oln/level/fill.hh>
00035 # include <oln/lrde/ufmt/utils.hh>
00036 # include <oln/lrde/ufmt/ap_maxtree.hh>
00037 # include <oln/lrde/ufmt/attributes.hh>
00038
00039
00040 namespace oln
00041 {
00042
00043 namespace lrde
00044 {
00045
00046 namespace ufmt
00047 {
00048
00049
00050
00051
00052 template <class I>
00053 struct sp_maxtree : public ap_maxtree<I>
00054 {
00055 typedef ap_maxtree<I> super;
00056 using super::f;
00057 using super::nbh;
00058 using super::par;
00059 oln_lrde_ufmt_import_ap_maxtree_typedefs;
00060
00061
00062 std::vector<point> S;
00063 std::vector<size_t> H;
00064 std::vector<dpoint> dp_pre, dp_post;
00065 unsigned nb;
00066 unsigned nnodes;
00067 typename mute<I, unsigned>::ret label;
00068
00069
00070
00071
00072 sp_maxtree(const abstract::image<I>& f,
00073 const oln_neighborhood_type(I)& nbh)
00074 :
00075 super(f, nbh),
00076 label(f.size())
00077 {
00078 }
00079
00080 template <class A>
00081 void go()
00082 {
00083 init();
00084 compute_parent();
00085
00086 }
00087
00088 void init()
00089 {
00090 S.reserve(f.npoints());
00091 oln_iter_type(I) p(f);
00092 for_all(p)
00093 S.push_back(p);
00094 std::sort(S.begin(), S.end(), rev_sort<I>(f));
00095
00096 nb = split<I>(nbh, dp_pre, dp_post);
00097 nnodes = 0;
00098 }
00099
00100 void compute_parent()
00101 {
00102 for (unsigned i = 0; i < f.npoints(); ++i)
00103 {
00104 point p = S[i];
00105 make_set(p);
00106
00107 for (unsigned j = 0; j < nb; ++j)
00108 {
00109 point n = p + dp_pre[j];
00110 if (f.hold(n) and f[n] >= f[p])
00111 do_union(n, p);
00112 }
00113
00114 for (unsigned j = 0; j < nb; ++j)
00115 {
00116 point n = p + dp_post[j];
00117 if (f.hold(n) and f[n] > f[p])
00118 do_union(n, p);
00119 }
00120 }
00121 }
00122
00123
00124 void make_set(const point& x)
00125 {
00126 par[x] = x;
00127 }
00128
00129 bool is_root(const point& x) const
00130 {
00131 return par[x] == x;
00132 }
00133
00134 point find_root(point x)
00135 {
00136 while (not is_root(x))
00137 x = par[x];
00138 return x;
00139 }
00140
00141 void do_union(const point& n, const point& p)
00142 {
00143 point r = find_root(n);
00144 if (r != p)
00145 par[r] = p;
00146 }
00147
00148
00149 };
00150
00151
00152
00153 }
00154
00155 }
00156
00157 }
00158
00159
00160 #endif // ! OLENA_LRDE_UFMT_SP_MAXTREE_HH