Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
like_ero_fun.hh
1 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_MORPHO_ELEMENTARY_LIKE_ERO_FUN_HH
27 # define MLN_MORPHO_ELEMENTARY_LIKE_ERO_FUN_HH
28 
32 
33 
34 # include <mln/morpho/includes.hh>
35 
36 
37 namespace mln
38 {
39 
40  namespace morpho
41  {
42 
43  namespace elementary
44  {
45 
46  template <typename A, typename F,
47  typename I, typename N>
48  mln_concrete(I)
49  like_ero_fun(const Meta_Accumulator<A>& a, const F& f,
50  const Image<I>& input, const Neighborhood<N>& nbh);
51 
52 
53 
54 # ifndef MLN_INCLUDE_ONLY
55 
56  namespace internal
57  {
58 
59  struct f_accu
60  {
61  template <typename V, typename A>
62  V operator()(const V& /*input_p*/, const A& a) const
63  {
64  return a.to_result();
65  }
66  };
67 
68  } // end of namespace mln::morpho::elementary::internal
69 
70 
71  namespace impl
72  {
73 
74  namespace generic
75  {
76 
77  template <typename A, typename F,
78  typename I, typename N>
79  mln_concrete(I)
80  like_ero_fun(const Meta_Accumulator<A>& a_, const F& f,
81  const Image<I>& input_, const Neighborhood<N>& nbh_)
82  {
83  trace::entering("morpho::elementary::impl::generic::like_ero_fun");
84 
85  const I& input = exact(input_);
86  const N& nbh = exact(nbh_);
87 
88  mln_accu_with(A, mln_value(I)) a = accu::unmeta(exact(a_), mln_value(I)());
89  extension::adjust_fill(input, nbh, a);
90 
91  mln_concrete(I) output;
92  initialize(output, input);
93 
94  mln_piter(I) p(input.domain());
95  mln_niter(N) n(nbh, p);
96  for_all(p)
97  {
98  a.take_as_init(input(p));
99  for_all(n) if (input.has(n))
100  a.take(input(n));
101  output(p) = f(input(p), a);
102  }
103 
104  trace::exiting("morpho::elementary::impl::generic::like_ero_fun");
105  return output;
106  }
107 
108  } // end of namespace mln::morpho::elementary::impl::generic
109 
110 
111  template <typename A, typename F,
112  typename I, typename N>
113  mln_concrete(I)
114  like_ero_fun_fastest(const Meta_Accumulator<A>& a_, const F& f,
115  const Image<I>& input_, const Neighborhood<N>& nbh_)
116  {
117  trace::entering("morpho::elementary::impl::like_ero_fun_fastest");
118 
119  const I& input = exact(input_);
120  const N& nbh = exact(nbh_);
121 
122  mln_accu_with(A, mln_value(I)) a = accu::unmeta(exact(a_), mln_value(I)());
123  extension::adjust_fill(input, nbh, a);
124 
125  mln_concrete(I) output;
126  initialize(output, input);
127 
128  mln_pixter(const I) p_in(input);
129  mln_pixter(I) p_out(output);
130  mln_nixter(const I, N) n(p_in, nbh);
131  for_all_2(p_in, p_out)
132  {
133  a.take_as_init(p_in.val());
134  for_all(n)
135  a.take(n.val());
136  p_out.val() = f(p_in.val(), a);
137  }
138 
139  trace::exiting("morpho::elementary::impl::like_ero_fun_fastest");
140  return output;
141  }
142 
143  } // end of namespace mln::morpho::elementary::impl
144 
145 
146  namespace internal
147  {
148 
149  template <typename A, typename F,
150  typename I, typename N>
151  mln_concrete(I)
152  like_ero_fun_dispatch(metal::false_,
153  const A& a, const F& f,
154  const I& input, const N& nbh)
155  {
156  return impl::generic::like_ero_fun(a, f, input, nbh);
157  }
158 
159  template <typename A, typename F,
160  typename I, typename N>
161  mln_concrete(I)
162  like_ero_fun_dispatch(metal::true_,
163  const A& a, const F& f,
164  const I& input, const N& nbh)
165  {
166  return impl::like_ero_fun_fastest(a, f, input, nbh);
167  }
168 
169  template <typename A, typename F,
170  typename I, typename N>
171  mln_concrete(I)
172  like_ero_fun_dispatch(const A& a, const F& f,
173  const I& input, const N& nbh)
174  {
175  typedef mlc_equal(mln_trait_image_speed(I),
176  trait::image::speed::fastest) I_fastest;
177  typedef mln_window(N) W;
178  typedef mln_is_simple_window(W) N_simple;
179 
180  return like_ero_fun_dispatch(mlc_and(I_fastest, N_simple)(),
181  a, f, input, nbh);
182  }
183 
184  } // end of namespace mln::morpho::elementary::internal
185 
186 
187  // Facade.
188 
189  template <typename A, typename F,
190  typename I, typename N>
191  mln_concrete(I)
192  like_ero_fun(const Meta_Accumulator<A>& a, const F& f,
193  const Image<I>& input, const Neighborhood<N>& nbh)
194  {
195  return internal::like_ero_fun_dispatch(a, f,
196  exact(input), exact(nbh));
197  }
198 
199 # endif // ! MLN_INCLUDE_ONLY
200 
201  } // end of namespace mln::morpho::elementary
202 
203  } // end of namespace mln::morpho
204 
205 } // end of namespace mln
206 
207 
208 #endif // ! MLN_MORPHO_ELEMENTARY_LIKE_ERO_FUN_HH