• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

compute_with_weights.hh

00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_SET_COMPUTE_WITH_WEIGHTS_HH
00027 # define MLN_SET_COMPUTE_WITH_WEIGHTS_HH
00028 
00034 
00035 # include <mln/core/concept/meta_accumulator.hh>
00036 # include <mln/core/concept/image.hh>
00037 # include <mln/core/concept/site_set.hh>
00038 # include <mln/util/array.hh>
00039 # include <mln/convert/from_to.hh>
00040 
00041 
00042 namespace mln
00043 {
00044 
00045   namespace set
00046   {
00047 
00053     //
00054     template <typename A, typename I>
00055     mln_result(A)
00056     compute_with_weights(const Accumulator<A>& a, const Image<I>& w);
00057 
00058 
00067     //
00068     template <typename A, typename I, typename L>
00069     util::array<mln_result(A)>
00070     compute_with_weights(const Accumulator<A>& a,
00071                          const Image<I>& w,
00072                          const Image<L>& label,
00073                          const mln_value(L)& nlabels);
00074 
00075 
00081     //
00082     template <typename A, typename I>
00083     mln_meta_accu_result(A, mln_site(I))
00084     compute_with_weights(const Meta_Accumulator<A>& a, const Image<I>& w);
00085 
00086 
00087 
00088 # ifndef MLN_INCLUDE_ONLY
00089 
00090 
00091     // Tests.
00092 
00093 
00094     namespace internal
00095     {
00096 
00097       template <typename A, typename I, typename L>
00098       void
00099       compute_with_weights_tests(const Accumulator<A>& a_,
00100                                  const Image<I>& w_,
00101                                  const Image<L>& label_)
00102       {
00103         const A& a = exact(a_);
00104         const I& w = exact(w_);
00105         const L& label = exact(label_);
00106 
00107         mln_precondition(w.is_valid());
00108         mln_precondition(label.is_valid());
00109         mln_precondition(w.domain() <= label.domain());
00110 
00111         (void) a;
00112         (void) w;
00113         (void) label;
00114       }
00115 
00116     } // end of namespace mln::internal
00117 
00118 
00119 
00120     // Implementations.
00121 
00122 
00123     namespace impl
00124     {
00125 
00126       namespace generic
00127       {
00128 
00136         //
00137         template <typename A, typename I>
00138         inline
00139         mln_result(A)
00140         compute_with_weights(const Accumulator<A>& a_, const Image<I>& w_)
00141         {
00142           trace::entering("set::impl::generic::compute_with_weights");
00143 
00144           mlc_converts_to(mln_site(I), mln_argument(A))::check();
00145           mlc_converts_to(mln_value(I), unsigned)::check();
00146 
00147           A a = exact(a_);
00148           const I& w = exact(w_);
00149 
00150           a.init();
00151           mln_piter(I) p(w.domain());
00152           for_all(p)
00153             a.take_n_times(w(p), p);
00154 
00155           trace::exiting("set::impl::generic::compute_with_weights");
00156           return a.to_result();
00157         }
00158 
00159 
00160 
00170         //
00171         template <typename A, typename I, typename L>
00172         util::array<mln_result(A)>
00173         compute_with_weights(const Accumulator<A>& a_,
00174                              const Image<I>& w_,
00175                              const Image<L>& label_,
00176                              const mln_value(L)& nlabels)
00177         {
00178           trace::entering("set::impl::generic::compute_with_weights");
00179 
00180           mlc_equal(mln_site(I), mln_site(L))::check();
00181           mlc_converts_to(mln_site(I), mln_argument(A))::check();
00182           mlc_converts_to(mln_value(I), unsigned)::check();
00183 
00184           A a = exact(a_);
00185           const I& w = exact(w_);
00186           const L& label = exact(label_);
00187 
00188           internal::compute_with_weights_tests(a, w, label);
00189 
00190           util::array<A> accus(static_cast<unsigned>(nlabels) + 1, a);
00191 
00192           mln_piter(I) p(w.domain());
00193           for_all(p)
00194             accus[label(p)].take_n_times(w(p), p);
00195 
00196           util::array<mln_result(A)> r;
00197           convert::from_to(accus, r);
00198 
00199           trace::exiting("set::impl::generic::compute_with_weights");
00200           return r;
00201         }
00202 
00203       } // end of namespace mln::set::impl::generic
00204 
00205     } // end of namespace mln::set::impl
00206 
00207 
00208 
00209     // Facades.
00210 
00211 
00212     template <typename A, typename I>
00213     inline
00214     mln_result(A)
00215     compute_with_weights(const Accumulator<A>& a, const Image<I>& w)
00216     {
00217       trace::entering("set::compute_with_weights");
00218 
00219       mlc_converts_to(mln_site(I), mln_argument(A))::check();
00220       mlc_converts_to(mln_value(I), unsigned)::check();
00221       mln_precondition(exact(w).is_valid());
00222 
00223       mln_result(A) r = impl::generic::compute_with_weights(a, w);
00224 
00225       trace::exiting("set::compute_with_weights");
00226       return r;
00227     }
00228 
00229 
00230     template <typename A, typename I, typename L>
00231     util::array<mln_result(A)>
00232     compute_with_weights(const Accumulator<A>& a,
00233                          const Image<I>& w,
00234                          const Image<L>& label,
00235                          const mln_value(L)& nlabels)
00236     {
00237       trace::entering("set::compute_with_weights");
00238 
00239       mlc_equal(mln_site(I), mln_site(L))::check();
00240       mlc_converts_to(mln_site(I), mln_argument(A))::check();
00241       mlc_converts_to(mln_value(I), unsigned)::check();
00242 
00243       internal::compute_with_weights_tests(a, w, label);
00244 
00245       util::array<mln_result(A)> r;
00246       r = impl::generic::compute_with_weights(a, w, label, nlabels);
00247 
00248       trace::exiting("set::compute_with_weights");
00249       return r;
00250     }
00251 
00252 
00253     template <typename A, typename I>
00254     inline
00255     mln_meta_accu_result(A, mln_site(I))
00256     compute_with_weights(const Meta_Accumulator<A>& a, const Image<I>& w)
00257     {
00258       trace::entering("set::compute_with_weights");
00259 
00260       mlc_converts_to(mln_value(I), unsigned)::check();
00261 
00262       mln_precondition(exact(w).is_valid());
00263 
00264       typedef mln_site(I) P;
00265       typedef mln_accu_with(A, P) A_;
00266       A_ a_ = accu::unmeta(exact(a), P());
00267 
00268       mln_result(A_) r = impl::generic::compute_with_weights(a_, w);
00269 
00270       trace::exiting("set::compute_with_weights");
00271       return r;
00272     }
00273 
00274 # endif // ! MLN_INCLUDE_ONLY
00275 
00276   } // end of namespace mln::set
00277 
00278 } // end of namespace mln
00279 
00280 
00281 #endif // ! MLN_SET_COMPUTE_WITH_WEIGHTS_HH

Generated on Thu Sep 8 2011 18:31:44 for Milena (Olena) by  doxygen 1.7.1