hpx_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_HPX_MAXTREE_HH
00029 # define OLENA_LRDE_UFMT_HPX_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 hpx_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         hpx_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 = histogram_reverse_sort_p(f, H);
00090           nb = split<I>(nbh, dp_pre, dp_post);
00091           nnodes = 1;
00092         }
00093 
00094         void compute_parent()
00095         {
00096           int i = 0;
00097           for (int h = uint_nvalues(f) - 1; h >= 0; --h)
00098             {
00099               int i_first = i;
00100               int i_last  = i_first + H[h];
00101 
00102               // union-find
00103 
00104               for (i = i_first; i < i_last; ++i)
00105                 {
00106                   point p = S[i];
00107                   make_x_set(p);
00108                   // pre
00109                   for (unsigned j = 0; j < nb; ++j)
00110                     {
00111                       point n = p + dp_pre[j];
00112                       if (f.hold(n) and f[n] >= h)
00113                         do_x_union(n, p);
00114                     }
00115                   // post
00116                   for (unsigned j = 0; j < nb; ++j)
00117                     {
00118                       point n = p + dp_post[j];
00119                       if (f.hold(n) and f[n] > h)
00120                         do_x_union(n, p);
00121                     }
00122                 }
00123 
00124               // enhancing level compression:
00125               //   we compress within 'level h' but we can end up with:
00126               //   p' @ h+ --> p @ h --> lr @ h
00127               //   so we have p -> lr for all p at level h
00128               //   but p' --> p cannot be compressed by the code below
00129               for (int k = i_last - 1; k >= i_first; --k)
00130                 {
00131                   point p = S[k];
00132 //                if (is_root(p))
00133 //                  ++nnodes;
00134 //                else
00135                     cpar[p] = cpar[cpar[p]];
00136                 }
00137               
00138             } // end of "for all h"
00139         }
00140 
00141 
00142         void make_x_set(const point& x)
00143         {
00144           par[x] = x;
00145           cpar[x] = x;
00146         }
00147 
00148         point find_x_root(point x)
00149         {
00150           if (cpar[x] == x)
00151             return x;
00152           else
00153             return cpar[x] = find_x_root(cpar[x]);
00154         }
00155 
00156         void do_x_union(const point& n, const point& p)
00157         {
00158           point r = find_x_root(n);
00159           if (r != p)
00160             {
00161               par[r] = p;
00162               cpar[r] = p;
00163               if (f[p] < f[r])
00164                 ++nnodes;
00165             }
00166         }
00167 
00168 
00169       }; // end of class hpx_maxtree
00170 
00171 
00172 
00173     } // end of namespace oln::lrde::ufmt
00174 
00175   } // end of namespace oln::lrde
00176 
00177 } // end of namespace oln
00178 
00179 
00180 #endif // ! OLENA_LRDE_UFMT_HPX_MAXTREE_HH

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