sp_maxtree.hh

00001 // Copyright (C) 2006  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
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       // FIXME: doc.
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         // aux data
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         // ctor
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> // void if no attributes
00081         void go()
00082         {
00083           init();
00084           compute_parent();  // 1st pass
00085           // extract_maxtree<A>(); // 2nd pass
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               // pre
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               // post
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             } // end of "for all p"
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       }; // end of class sp_maxtree
00150 
00151 
00152 
00153     } // end of namespace oln::lrde::ufmt
00154 
00155   } // end of namespace oln::lrde
00156 
00157 } // end of namespace oln
00158 
00159 
00160 #endif // ! OLENA_LRDE_UFMT_SP_MAXTREE_HH

Generated on Tue Feb 20 20:20:32 2007 for Olena by  doxygen 1.5.1