Milena (Olena)  User documentation 2.0a Id
compute_attribute_image.hh
00001 // Copyright (C) 2008, 2009, 2011 EPITA Research and Development
00002 // Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project 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 produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_MORPHO_TREE_COMPUTE_ATTRIBUTE_IMAGE_HH
00028 # define MLN_MORPHO_TREE_COMPUTE_ATTRIBUTE_IMAGE_HH
00029 
00035 
00036 # include <mln/core/routine/duplicate.hh>
00037 # include <mln/core/concept/image.hh>
00038 # include <mln/morpho/tree/data.hh>
00039 # include <mln/trait/accumulators.hh>
00040 # include <mln/util/pix.hh>
00041 # include <mln/data/fill.hh>
00042 
00043 
00044 namespace mln
00045 {
00046 
00047   namespace morpho
00048   {
00049 
00050     namespace tree
00051     {
00052 
00080       template <typename A, typename T>
00081       mln_ch_value(typename T::function, mln_result(A))
00082       compute_attribute_image(const Accumulator<A>& a,
00083                               const T& t,
00084                               mln_ch_value(typename T::function, A)* accu_image = 0);
00085 
00086 
00087 
00098       template <typename A, typename T, typename V>
00099       mln_ch_value(typename T::function, mln_result(A))
00100       compute_attribute_image_from(const Accumulator<A>& a,
00101                                    const T& t,
00102                                    const Image<V>& values,
00103                                    mln_ch_value(typename T::function, A)* accu_image = 0);
00104 
00105 
00106 
00107 # ifndef MLN_INCLUDE_ONLY
00108       // Take_as_init specialization
00109 
00110       namespace internal
00111       {
00112         template <typename A, typename I, typename P>
00113         void take_as_init (trait::accumulator::when_pix::use_none, A& accu,
00114                            const I& input, const P& p)
00115         {
00116           (void)input;
00117           (void)p;
00118           accu.take_as_init();
00119         }
00120 
00121         template <typename A, typename I, typename P>
00122         void take_as_init (trait::accumulator::when_pix::use_pix, A& accu,
00123                            const I& input, const P& p)
00124         {
00125           accu.take_as_init(make::pix(input, p));
00126         }
00127 
00128         template <typename A, typename I, typename P>
00129         void take_as_init (trait::accumulator::when_pix::use_v, A& accu,
00130                            const I& input, const P& p)
00131         {
00132           accu.take_as_init(input(p));
00133         }
00134 
00135         template <typename A, typename I, typename P>
00136         void take_as_init (trait::accumulator::when_pix::use_p, A& accu,
00137                            const I& input, const P& p)
00138         {
00139           (void) input;
00140           accu.take_as_init(p);
00141         }
00142 
00143 
00144         template <typename A, typename I, typename P>
00145         void take_as_init (A& accu, const I& input, const P& p)
00146         {
00147           take_as_init (mln_trait_accumulator_when_pix(A)(), accu, input, p);
00148         }
00149 
00150 
00151         template <typename A, typename T, typename V>
00152         inline
00153         mln_ch_value(typename T::function, mln_result(A))
00154           compute_attribute_image(const A& a,
00155                                   const T& t,
00156                                   const V& values,
00157                                   mln_ch_value(typename T::function, A)* accu_image = 0)
00158         {
00159 
00160           typedef typename T::function I;
00161           mln_ch_value(I, A) acc;
00162           initialize(acc, t.f());
00163 
00164           {
00165             // Transmit "dynamic data" (state) of 'a' to every values of
00166             // 'acc'.  It is usually a no-op (so useless) except for a
00167             // few accumulators, e.g., for accu::stat::rank which has the 'k'
00168             // attribute.
00169             mln::data::fill(acc, a);
00170           }
00171 
00172           {
00173             // Initialize every attribute with the corresponding pixel.
00174             mln_site_piter(T) p(t);
00175             for_all(p)
00176               take_as_init(acc(p), values, p);
00177           }
00178 
00179           {
00180             mln_up_site_piter(T) p(t);
00181             // Propagate attribute from a site to its parent.
00182             for_all(p)
00183               if (! t.is_root(p))
00184                 acc(t.parent(p)).take(acc(p));
00185 
00186             // Back-propagate attribute from a node to sites of its
00187             // component.  Below, p is a non-node component site and
00188             // parent(p) is a node, that is, the site representative of
00189             // the component p belongs to.
00190             for_all(p)
00191               if (! t.is_a_node(p))
00192                 {
00193                   mln_assertion(t.is_a_node(t.parent(p)));
00194                   acc(p) = acc(t.parent(p));
00195                 }
00196           }
00197 
00198 
00199           // Store accumulator image.
00200           if (accu_image)
00201             *accu_image = duplicate(acc);
00202 
00203           typedef typename T::function I;
00204           mln_ch_value(I, mln_result(A)) output;
00205           initialize(output, acc);
00206           mln::data::fill(output, acc);
00207 
00208           return output;
00209         }
00210       }
00211 
00212       // Facade.
00213 
00214       template <typename A, typename T>
00215       inline
00216       mln_ch_value(typename T::function, mln_result(A))
00217       compute_attribute_image(const Accumulator<A>& a_,
00218                               const T& t,
00219                               mln_ch_value(typename T::function, A)* accu_image)
00220       {
00221         trace::entering("morpho::tree::compute_attribute_image");
00222 
00223         mln_ch_value(typename T::function, mln_result(A)) output;
00224         output = internal::compute_attribute_image(exact(a_), t, t.f(),
00225                                                    accu_image);
00226 
00227         trace::exiting("morpho::tree::compute_attribute_image");
00228         return (output);
00229       }
00230 
00231       template <typename A, typename T, typename V>
00232       inline
00233       mln_ch_value(typename T::function, mln_result(A))
00234       compute_attribute_image_from(const Accumulator<A>& a_,
00235                                    const T& t,
00236                                    const Image<V>& values,
00237                                    mln_ch_value(typename T::function, A)* accu_image)
00238       {
00239         trace::entering("morpho::tree::compute_attribute_image_from");
00240 
00241 
00242         mln_ch_value(typename T::function, mln_result(A)) output;
00243         output = internal::compute_attribute_image(exact(a_), t, exact(values),
00244                                                    accu_image);
00245 
00246         trace::exiting("morpho::tree::compute_attribute_image_from");
00247         return output;
00248       }
00249 
00250 
00251 
00252 
00253 # endif // ! MLN_INCLUDE_ONLY
00254 
00255     }  // end of namespace mln::morpho::tree
00256 
00257   }  // end of namespace mln::morpho
00258 
00259 }  // end of namespace mln
00260 
00261 
00262 #endif // ! MLN_MORPHO_TREE_COMPUTE_ATTRIBUTE_IMAGE_HH
 All Classes Namespaces Functions Variables Typedefs Enumerator