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

compute.hh

00001 // Copyright (C) 2008, 2009, 2010 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_LABELING_COMPUTE_HH
00028 # define MLN_LABELING_COMPUTE_HH
00029 
00047 
00048 # include <mln/core/concept/image.hh>
00049 # include <mln/core/concept/accumulator.hh>
00050 # include <mln/core/concept/meta_accumulator.hh>
00051 # include <mln/util/array.hh>
00052 # include <mln/convert/from_to.hh>
00053 # include <mln/geom/ncols.hh>
00054 # include <mln/value/next.hh>
00055 
00056 
00057 namespace mln
00058 {
00059 
00060   namespace labeling
00061   {
00062 
00071     //
00072     template <typename A, typename I, typename L>
00073     util::array<mln_result(A)>
00074     compute(const Accumulator<A>& a,
00075             const Image<I>& input,
00076             const Image<L>& label,
00077             const mln_value(L)& nlabels);
00078 
00079 
00088     //
00089     template <typename A, typename I, typename L>
00090     util::array<mln_meta_accu_result(A, mln_value(I))>
00091     compute(const Meta_Accumulator<A>& a,
00092             const Image<I>& input,
00093             const Image<L>& label,
00094             const mln_value(L)& nlabels);
00095 
00096 
00104     //
00105     template <typename A, typename L>
00106     util::array<mln_result(A)>
00107     compute(const Accumulator<A>& a,
00108             const Image<L>& label,
00109             const mln_value(L)& nlabels);
00110 
00111 
00119     //
00120     template <typename A, typename L>
00121     util::array<mln_meta_accu_result(A, mln_psite(L))>
00122     compute(const Meta_Accumulator<A>& a,
00123             const Image<L>& label,
00124             const mln_value(L)& nlabels);
00125 
00126 
00135     //
00136     template <typename A, typename I, typename L>
00137     util::array<mln_result(A)>
00138     compute(util::array<A>& a,
00139             const Image<I>& input,
00140             const Image<L>& label,
00141             const mln_value(L)& nlabels);
00142 
00143 
00144 
00145 # ifndef MLN_INCLUDE_ONLY
00146 
00147     namespace internal
00148     {
00149 
00150       template <typename A, typename L>
00151       inline
00152       void
00153       compute_tests(const Accumulator<A>& a,
00154                     const Image<L>& label,
00155                     const mln_value(L)& nlabels)
00156       {
00157         mln_precondition(exact(label).is_valid());
00158         // mlc_is_a(mln_value(L), mln::value::Symbolic)::check();
00159         (void) a;
00160         (void) label;
00161         (void) nlabels;
00162       }
00163 
00164 
00165       template <typename A, typename I, typename L>
00166       inline
00167       void
00168       compute_tests(const Accumulator<A>& a,
00169                     const Image<I>& input,
00170                     const Image<L>& label,
00171                     const mln_value(L)& nlabels)
00172       {
00173         mln_precondition(exact(input).is_valid());
00174         mln_precondition(exact(label).is_valid());
00175         // mlc_is_a(mln_value(L), mln::value::Symbolic)::check();
00176         (void) a;
00177         (void) input;
00178         (void) label;
00179         (void) nlabels;
00180       }
00181 
00182     } // end of namespace mln::labeling::internal
00183 
00184 
00185 
00186     namespace impl
00187     {
00188 
00189       namespace generic
00190       {
00191 
00192 
00200         //
00201         template <typename A, typename L>
00202         inline
00203         util::array<mln_result(A)>
00204         compute(const Accumulator<A>& a_,
00205                 const Image<L>& label_,
00206                 const mln_value(L)& nlabels)
00207         {
00208           trace::entering("labeling::impl::generic::compute");
00209           internal::compute_tests(a_, label_, nlabels);
00210 
00211           const A& a = exact(a_);
00212           const L& label = exact(label_);
00213 
00214           util::array<A> accus(value::next(nlabels), a);
00215 
00216           mln_piter(L) p(label.domain());
00217           for_all(p)
00218             accus[label(p)].take(p);
00219 
00220           util::array<mln_result(A)> res;
00221           convert::from_to(accus, res);
00222 
00223           trace::exiting("labeling::impl::generic::compute");
00224           return res;
00225         }
00226 
00237         //
00238         template <typename A, typename L>
00239         inline
00240         util::array<mln_result(A)>
00241         compute(util::array<A>& accus,
00242                 const Image<L>& label_,
00243                 const mln_value(L)& nlabels)
00244         {
00245           trace::entering("labeling::impl::generic::compute");
00246           internal::compute_tests(A(), label_, nlabels);
00247 
00248           if (value::next(nlabels) != accus.size())
00249           {
00250             accus.resize(0); // Make sure all the accumulators are
00251                              // re-initialized when resizing on next
00252                              // line.
00253             accus.resize(value::next(nlabels));
00254           }
00255 
00256           const L& label = exact(label_);
00257 
00258           mln_piter(L) p(label.domain());
00259           for_all(p)
00260             accus[label(p)].take(p);
00261 
00262           util::array<mln_result(A)> res;
00263           convert::from_to(accus, res);
00264 
00265           trace::exiting("labeling::impl::generic::compute");
00266           return res;
00267         }
00268 
00269 
00270 
00279         //
00280         template <typename A, typename I, typename L>
00281         inline
00282         util::array<mln_result(A)>
00283         compute(const Accumulator<A>& a_,
00284                 const Image<I>& input_,
00285                 const Image<L>& label_,
00286                 const mln_value(L)& nlabels)
00287         {
00288           trace::entering("labeling::impl::generic::compute");
00289           internal::compute_tests(a_, input_, label_, nlabels);
00290 
00291           const A& a = exact(a_);
00292           const I& input = exact(input_);
00293           const L& label = exact(label_);
00294 
00295           util::array<A> accus(value::next(nlabels), a);
00296 
00297           mln_piter(I) p(input.domain());
00298           for_all(p)
00299             accus[label(p)].take(input(p));
00300 
00301           util::array<mln_result(A)> res;
00302           convert::from_to(accus, res);
00303 
00304           trace::exiting("labeling::impl::generic::compute");
00305           return res;
00306         }
00307 
00308 
00317         //
00318         template <typename A, typename I, typename L>
00319         inline
00320         util::array<mln_result(A)>
00321         compute(util::array<A>& accus,
00322                 const Image<I>& input_,
00323                 const Image<L>& label_,
00324                 const mln_value(L)& nlabels)
00325         {
00326           trace::entering("labeling::impl::generic::compute");
00327           //internal::compute_tests(a_, input_, label_, nlabels);
00328 
00329           //const A& a = exact(a_);
00330           const I& input = exact(input_);
00331           const L& label = exact(label_);
00332           (void) nlabels;
00333 
00334           if (value::next(nlabels) != accus.size())
00335           {
00336             accus.resize(0); // Make sure all the accumulators are
00337                              // re-initialized when resizing on next
00338                              // line.
00339             accus.resize(value::next(nlabels));
00340           }
00341 
00342           mln_piter(I) p(input.domain());
00343           for_all(p)
00344             accus[label(p)].take(input(p));
00345 
00346           util::array<mln_result(A)> res;
00347           convert::from_to(accus, res);
00348 
00349           trace::exiting("labeling::impl::generic::compute");
00350           return res;
00351         }
00352 
00353       } // end of namespace mln::labeling::impl::generic
00354 
00355 
00356 
00357         // FIXME: is there any optimization if border::get(input) ==
00358         // border::get(label)) ?
00359         //
00360 
00369         //
00370       template <typename A, typename I, typename L>
00371       inline
00372       util::array<mln_result(A)>
00373       compute_fastest(const Accumulator<A>& a_,
00374                       const Image<I>& input_,
00375                       const Image<L>& label_,
00376                       const mln_value(L)& nlabels)
00377       {
00378         trace::entering("labeling::impl::compute_fastest");
00379         internal::compute_tests(a_, input_, label_, nlabels);
00380 
00381         const A& a = exact(a_);
00382         const I& input = exact(input_);
00383         const L& label = exact(label_);
00384 
00385         // FIXME: check image properties + add doc.
00386 
00387         util::array<A> accus(value::next(nlabels), a);
00388 
00389         unsigned ncols = geom::ncols(label);
00390 
00391         typedef mln_site(I) P;
00392         typedef const mln_value(I) * iptr_t;
00393         typedef const mln_value(L) * lptr_t;
00394         box_runstart_piter<P> p(label.domain());
00395         for_all(p)
00396         {
00397           iptr_t iptr = & input(p);
00398           lptr_t lptr = & label(p);
00399 
00400           for (unsigned i = 0; i < ncols; ++i)
00401             accus[*lptr++].take(*iptr++);
00402         }
00403 
00404         util::array<mln_result(A)> res;
00405         convert::from_to(accus, res);
00406 
00407         trace::exiting("labeling::impl::generic::compute_fastest");
00408         return res;
00409       }
00410 
00411       // FIXME: is there any optimization if border::get(input) ==
00412       // border::get(label)) ?
00413       //
00414 
00423       //
00424       template <typename A, typename I, typename L>
00425       inline
00426       util::array<mln_result(A)>
00427       compute_fastest(util::array<A>& accus,
00428                       const Image<I>& input_,
00429                       const Image<L>& label_,
00430                       const mln_value(L)& nlabels)
00431       {
00432         trace::entering("labeling::impl::generic::compute_fastest");
00433         //internal::compute_tests(a_, input_, label_, nlabels);
00434 
00435         // FIXME: check image properties + add doc.
00436 
00437         //const A& a = exact(a_);
00438         const I& input = exact(input_);
00439         const L& label = exact(label_);
00440         (void) nlabels;
00441 
00442         if (value::next(nlabels) != accus.size())
00443         {
00444           accus.resize(0); // Make sure all the accumulators are
00445           // re-initialized when resizing on next
00446           // line.
00447           accus.resize(value::next(nlabels));
00448         }
00449 
00450         unsigned ncols = geom::ncols(label);
00451 
00452         typedef mln_site(I) P;
00453         typedef const mln_value(I) * iptr_t;
00454         typedef const mln_value(L) * lptr_t;
00455         box_runstart_piter<P> p(label.domain());
00456         for_all(p)
00457         {
00458           iptr_t iptr = & input(p);
00459           lptr_t lptr = & label(p);
00460 
00461           for (unsigned i = 0; i < ncols; ++i)
00462             accus[*lptr++].take(*iptr++);
00463         }
00464 
00465         util::array<mln_result(A)> res;
00466         convert::from_to(accus, res);
00467 
00468         trace::exiting("labeling::impl::generic::compute_fastest");
00469         return res;
00470       }
00471 
00472 
00473 
00474     } // end of namespace mln::labeling::impl
00475 
00476 
00477 
00478     namespace internal
00479     {
00480 
00481 
00482       template <typename A, typename L>
00483       inline
00484       util::array<mln_result(A)>
00485       compute_dispatch(const Accumulator<A>& a,
00486                        const Image<L>& label,
00487                        const mln_value(L)& nlabels)
00488       {
00489         return impl::generic::compute(a, label, nlabels);
00490       }
00491 
00492 
00493 
00494 
00495 
00496       template <typename A, typename I, typename L>
00497       inline
00498       util::array<mln_result(A)>
00499       compute_dispatch(
00500         mln::trait::image::value_access::any,
00501         mln::trait::image::value_access::any,
00502         mln::trait::image::ext_domain::any,
00503         mln::trait::image::ext_domain::any,
00504         const Accumulator<A>& a,
00505         const Image<I>& input,
00506         const Image<L>& label,
00507         const mln_value(L)& nlabels)
00508       {
00509         return impl::generic::compute(a, input, label, nlabels);
00510       }
00511 
00512 
00513       template <typename A, typename I, typename L>
00514       inline
00515       util::array<mln_result(A)>
00516       compute_dispatch(
00517         mln::trait::image::value_access::direct,
00518         mln::trait::image::value_access::direct,
00519         mln::trait::image::ext_domain::some,
00520         mln::trait::image::ext_domain::some,
00521         const Accumulator<A>& a,
00522         const Image<I>& input,
00523         const Image<L>& label,
00524         const mln_value(L)& nlabels)
00525       {
00526         return impl::compute_fastest(a, input, label, nlabels);
00527       }
00528 
00529 
00530       template <typename A, typename I, typename L>
00531       inline
00532       util::array<mln_result(A)>
00533       compute_dispatch(mln::trait::image::value_storage::one_block,
00534                        mln::trait::image::value_storage::one_block,
00535                        const Accumulator<A>& a,
00536                        const Image<I>& input_,
00537                        const Image<L>& label_,
00538                        const mln_value(L)& nlabels)
00539       {
00540         const I& input  = exact(input_);
00541         const L& label = exact(label_);
00542 
00544         if (mlc_is(mln_trait_image_value_alignment(I),
00545                    trait::image::value_alignment::with_grid)::value &&
00546             mlc_is(mln_trait_image_value_alignment(L),
00547                    trait::image::value_alignment::with_grid)::value)
00548           {
00549             return compute_dispatch(
00550               mln_trait_image_value_access(I)(),
00551               mln_trait_image_value_access(L)(),
00552               mln_trait_image_ext_domain(I)(),
00553               mln_trait_image_ext_domain(L)(),
00554               a, input, label, nlabels);
00555           }
00556         else
00557           return impl::generic::compute(a, input, label, nlabels);
00558       }
00559 
00560 
00561       template <typename A, typename I, typename L>
00562       inline
00563       util::array<mln_result(A)>
00564       compute_dispatch(const Accumulator<A>& a,
00565                        const Image<I>& input,
00566                        const Image<L>& label,
00567                        const mln_value(L)& nlabels)
00568       {
00569         return compute_dispatch(mln_trait_image_value_storage(I)(),
00570                                 mln_trait_image_value_storage(L)(),
00571                                 a, input, label, nlabels);
00572       }
00573 
00574 
00575 
00576 
00577 
00578 
00579 
00580 
00581       template <typename A, typename I, typename L>
00582       inline
00583       util::array<mln_result(A)>
00584       compute_dispatch(
00585         mln::trait::image::value_access::any,
00586         mln::trait::image::value_access::any,
00587         mln::trait::image::ext_domain::any,
00588         mln::trait::image::ext_domain::any,
00589         util::array<A>& a,
00590         const Image<I>& input,
00591         const Image<L>& label,
00592         const mln_value(L)& nlabels)
00593       {
00594         return impl::generic::compute(a, input, label, nlabels);
00595       }
00596 
00597 
00598       template <typename A, typename I, typename L>
00599       inline
00600       util::array<mln_result(A)>
00601       compute_dispatch(
00602         mln::trait::image::value_access::direct,
00603         mln::trait::image::value_access::direct,
00604         mln::trait::image::ext_domain::some,
00605         mln::trait::image::ext_domain::some,
00606         util::array<A>& a,
00607         const Image<I>& input,
00608         const Image<L>& label,
00609         const mln_value(L)& nlabels)
00610       {
00611         return impl::compute_fastest(a, input, label, nlabels);
00612       }
00613 
00614 
00615       template <typename A, typename I, typename L>
00616       inline
00617       util::array<mln_result(A)>
00618       compute_dispatch(mln::trait::image::value_storage::one_block,
00619                        mln::trait::image::value_storage::one_block,
00620                        util::array<A>& a,
00621                        const Image<I>& input_,
00622                        const Image<L>& label_,
00623                        const mln_value(L)& nlabels)
00624       {
00625         const I& input  = exact(input_);
00626         const L& label = exact(label_);
00627 
00629         if (mlc_is(mln_trait_image_value_alignment(I),
00630                    trait::image::value_alignment::with_grid)::value &&
00631             mlc_is(mln_trait_image_value_alignment(L),
00632                    trait::image::value_alignment::with_grid)::value)
00633           {
00634             return compute_dispatch(
00635               mln_trait_image_value_access(I)(),
00636               mln_trait_image_value_access(L)(),
00637               mln_trait_image_ext_domain(I)(),
00638               mln_trait_image_ext_domain(L)(),
00639               a, input, label, nlabels);
00640           }
00641         else
00642           return impl::generic::compute(a, input, label, nlabels);
00643       }
00644 
00645 
00646       template <typename A, typename I, typename L>
00647       inline
00648       util::array<mln_result(A)>
00649       compute_dispatch(util::array<A>& a,
00650                        const Image<I>& input,
00651                        const Image<L>& label,
00652                        const mln_value(L)& nlabels)
00653       {
00654         return compute_dispatch(mln_trait_image_value_storage(I)(),
00655                                 mln_trait_image_value_storage(L)(),
00656                                 a, input, label, nlabels);
00657       }
00658 
00659 
00660 
00661 
00662 
00663       template <typename A, typename L>
00664       inline
00665       util::array<mln_result(A)>
00666       compute_dispatch(util::array<A>& accus,
00667                        const Image<L>& label,
00668                        const mln_value(L)& nlabels)
00669       {
00670         return impl::generic::compute(accus, label, nlabels);
00671       }
00672 
00673     } // end of namespace mln::labeling::internal
00674 
00675 
00676 
00677     // Facades.
00678 
00679     template <typename A, typename I, typename L>
00680     inline
00681     util::array<mln_result(A)>
00682     compute(util::array<A>& a,
00683             const Image<I>& input,
00684             const Image<L>& label,
00685             const mln_value(L)& nlabels)
00686     {
00687       trace::entering("labeling::compute");
00688 
00689       //internal::compute_tests(a, input, label, nlabels);
00690 
00691       typedef util::array<mln_result(A)> R;
00692       R res = internal::compute_dispatch(a, input, label, nlabels);
00693 
00694       trace::exiting("labeling::compute");
00695       return res;
00696     }
00697 
00698     template <typename A, typename I, typename L>
00699     inline
00700     util::array<mln_result(A)>
00701     compute(const Accumulator<A>& a,
00702             const Image<I>& input,
00703             const Image<L>& label,
00704             const mln_value(L)& nlabels)
00705     {
00706       trace::entering("labeling::compute");
00707 
00708       internal::compute_tests(a, input, label, nlabels);
00709 
00710       typedef util::array<mln_result(A)> R;
00711       R res = internal::compute_dispatch(a, input, label, nlabels);
00712 
00713       trace::exiting("labeling::compute");
00714       return res;
00715     }
00716 
00717     template <typename A, typename I, typename L>
00718     inline
00719     util::array<mln_meta_accu_result(A, mln_value(I))>
00720     compute(const Meta_Accumulator<A>& a,
00721             const Image<I>& input,
00722             const Image<L>& label,
00723             const mln_value(L)& nlabels)
00724     {
00725       typedef mln_accu_with(A, mln_value(I)) A_;
00726       A_ a_ = accu::unmeta(exact(a), mln_value(I)());
00727 
00728       return compute(a_, input, label, nlabels);
00729     }
00730 
00731 
00732     template <typename A, typename L>
00733     inline
00734     util::array<mln_result(A)>
00735     compute(util::array<A>& accus,
00736             const Image<L>& label,
00737             const mln_value(L)& nlabels)
00738     {
00739       trace::entering("labeling::compute");
00740 
00741       internal::compute_tests(A(), label, nlabels);
00742 
00743       typedef util::array<mln_result(A)> R;
00744       R res = internal::compute_dispatch(accus, label, nlabels);
00745 
00746       mln_postcondition(res.nelements() == value::next(nlabels));
00747 
00748       trace::exiting("labeling::compute");
00749       return res;
00750     }
00751 
00752 
00753 
00754     template <typename A, typename L>
00755     inline
00756     util::array<mln_result(A)>
00757     compute(const Accumulator<A>& a,
00758             const Image<L>& label,
00759             const mln_value(L)& nlabels)
00760     {
00761       trace::entering("labeling::compute");
00762 
00763       internal::compute_tests(a, label, nlabels);
00764 
00765       typedef util::array<mln_result(A)> R;
00766       R res = internal::compute_dispatch(a, label, nlabels);
00767 
00768       mln_postcondition(res.nelements() == value::next(nlabels));
00769 
00770       trace::exiting("labeling::compute");
00771       return res;
00772     }
00773 
00774 
00775     template <typename A, typename L>
00776     inline
00777     util::array<mln_meta_accu_result(A, mln_psite(L))>
00778     compute(const Meta_Accumulator<A>& a,
00779             const Image<L>& label,
00780             const mln_value(L)& nlabels)
00781     {
00782       typedef mln_accu_with(A, mln_psite(L)) A_;
00783       A_ a_ = accu::unmeta(exact(a), mln_psite(L)());
00784 
00785       return compute(a_, label, nlabels);
00786     }
00787 
00788 
00789 # endif // ! MLN_INCLUDE_ONLY
00790 
00791   } // end of namespace mln::labeling
00792 
00793 } // end of namespace mln
00794 
00795 
00796 #endif // ! MLN_LABELING_COMPUTE_HH

Generated on Fri Oct 19 2012 04:15:42 for Milena (Olena) by  doxygen 1.7.1