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

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