Milena (Olena)  User documentation 2.0a Id
from_image_to_site_set.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_CONVERT_IMPL_FROM_IMAGE_TO_SITE_SET_HH
00028 # define MLN_CONVERT_IMPL_FROM_IMAGE_TO_SITE_SET_HH
00029 
00037 
00038 # include <utility>
00039 # include <mln/core/site_set/p_run.hh>
00040 # include <mln/core/site_set/p_array.hh>
00041 # include <mln/metal/converts_to.hh>
00042 
00043 
00044 
00045 namespace mln
00046 {
00047 
00048   // Forward declarations.
00049   template <typename E> struct Site_Set;
00050   template <typename E> struct Image;
00051   template <typename E> struct Gpoint;
00052 
00053   namespace convert
00054   {
00055 
00056     namespace impl
00057     {
00058 
00060       template <typename I, typename S>
00061       void
00062       from_image_to_site_set(const Image<I>& from, Site_Set<S>& to);
00063 
00064 
00065 
00066 # ifndef MLN_INCLUDE_ONLY
00067 
00068       namespace internal
00069       {
00070 
00071         // Case: binary image -> set of point runs.
00072 
00073         template <typename I, typename P, typename S>
00074         void
00075         from_image_to_site_set(const I& input, const Gpoint<P>&,
00076                                S& s,           const mln::p_run<P>&)
00077         {
00078           s.clear();
00079           mln_fwd_piter(I) p(input.domain());
00080           p.start();
00081           for (;;)
00082           {
00083             // Skip background.
00084             while (p.is_valid() && input(p) == false)
00085               p.next();
00086             if (! p.is_valid()) // The end.
00087               break;
00088             mln_invariant(input(p) == true);
00089             P start = p, q;
00090             // Go to run end.
00091             do
00092             {
00093               q = p;
00094               p.next();
00095             }
00096             while (p.is_valid() && input(p) == true &&
00097                 // p follows q in a run, i.e., "p == q + right":
00098                 cut_(p.to_site()) == cut_(q) && p.last_coord() == q.last_coord() + 1);
00099             s.insert(p_run<P>(start, q));
00100           }
00101         }
00102 
00103 
00104         template <typename I, typename P, typename S>
00105         void
00106         from_image_to_site_set(const I& input, const Gpoint<P>&,
00107                                S& s,
00108                                const std::pair< mln_value(I), p_run<P> >&)
00109         {
00110           s.clear();
00111           mln_fwd_piter(I) p(input.domain());
00112           p.start();
00113           for (;;)
00114           {
00115             if (! p.is_valid()) // The end.
00116               break;
00117             mln_value(I) v = input(p);
00118             P start = p, q;
00119             // Go to run end.
00120             do
00121             {
00122               q = p;
00123               p.next();
00124             }
00125             while (p.is_valid() && input(p) == v &&
00126                 cut_(p.to_site()) == cut_(q) && p.last_coord() == q.last_coord() + 1);
00127             s.insert(v, p_run<P>(start, q));
00128           }
00129         }
00130 
00131 
00132         template <typename I, typename P, typename S>
00133         void
00134         from_image_to_site_set(const I& input, const Gpoint<P>&,
00135                                S& s,
00136                                const std::pair< mln_value(I), P >&)
00137         {
00138           s.clear();
00139           mln_fwd_piter(I) p(input.domain());
00140           for_all(p)
00141             s.insert(input(p), p);
00142         }
00143 
00144 
00145         template <typename I, typename S>
00146         inline
00147         void
00148         from_image_to_site_set(const Image<I>& from, Site_Set<S>& to)
00149         {
00150           from_image_to_site_set(exact(from), mln_deduce(I, domain_t, element)(),
00151                                  exact(to),   mln_i_element(S)());
00152         }
00153 
00154         template <typename I>
00155         inline
00156         void
00157         from_image_to_site_set(const Image<I>& from_, p_array<mln_psite(I)>& to)
00158         {
00159           const I& from = exact(from_);
00160 
00161           mln_piter(I) p(from.domain());
00162           for_all(p)
00163             if (from(p))
00164               to.append(p);
00165         }
00166 
00167 
00168       } // end of namespace mln::convert::impl::internal
00169 
00170 
00171       // Facade.
00172 
00173       template <typename I, typename S>
00174       inline
00175       void
00176       from_image_to_site_set(const Image<I>& from, Site_Set<S>& to)
00177       {
00178         internal::from_image_to_site_set(exact(from), exact(to));
00179       }
00180 
00181 # endif // ! MLN_INCLUDE_ONLY
00182 
00183     } // end of namespace mln::convert::impl
00184 
00185   } // end of namespace mln::convert
00186 
00187 } // end of namespace mln
00188 
00189 
00190 #endif // ! MLN_CONVERT_IMPL_FROM_IMAGE_TO_SITE_SET_HH
 All Classes Namespaces Functions Variables Typedefs Enumerator