Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
transform_stop.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_ACCU_TRANSFORM_STOP_HH
27 # define MLN_ACCU_TRANSFORM_STOP_HH
28 
33 
34 # include <mln/core/concept/meta_accumulator.hh>
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/window.hh>
37 # include <mln/extension/adjust.hh>
38 
39 
40 namespace mln
41 {
42 
43  namespace accu
44  {
45 
46  template <typename I, typename A, typename W>
47  mln_ch_value(I, mln_result(A))
48  transform_stop(const Image<I>& input, const Accumulator<A>& a, const Window<W>& win);
49 
50  template <typename I, typename A, typename W>
51  mln_ch_value(I, mln_meta_accu_result(A, mln_value(I)))
52  transform_stop(const Image<I>& input, const Meta_Accumulator<A>& a, const Window<W>& win);
53 
54 
55 
56 # ifndef MLN_INCLUDE_ONLY
57 
58 
59  namespace impl
60  {
61 
62  // Generic version.
63 
64  namespace generic
65  {
66 
67  template <typename I, typename A, typename W>
68  mln_ch_value(I, mln_result(A))
69  transform_stop(const Image<I>& input_,
70  const Accumulator<A>& a_,
71  const Window<W>& win_)
72  {
73  trace::entering("accu::impl::generic::transform_stop");
74 
75  const I& input = exact(input_);
76  const W& win = exact(win_);
77  A a = exact(a_);
78 
79  mln_precondition(input.is_valid());
80  mln_precondition(win.is_valid());
81 
82  extension::adjust(input, win);
83 
84  mln_ch_value(I, mln_result(A)) output;
85  initialize(output, input);
86 
87  mln_piter(I) p(input.domain());
88  mln_qiter(W) q(win, p);
89  for_all(p)
90  {
91  a.init();
92  for_all(q) if (input.has(q))
93  {
94  a.take(input(q));
95  if (a.can_stop())
96  break;
97  }
98  output(p) = a.to_result();
99  }
100 
101  trace::exiting("accu::impl::generic::transform_stop");
102  return output;
103  }
104 
105  } // end of namespace mln::accu::impl::generic
106 
107 
108  // Fastest version.
109 
110  template <typename I, typename A, typename W>
111  mln_ch_value(I, mln_result(A))
112  transform_stop_fastest(const Image<I>& input_, const Accumulator<A>& a_, const Window<W>& win_)
113  {
114  trace::entering("accu::impl::transform_stop_fastest");
115 
116  const I& input = exact(input_);
117  const W& win = exact(win_);
118  A a = exact(a_);
119 
120  mln_precondition(input.is_valid());
121  mln_precondition(win.is_valid());
122 
123  extension::adjust(input, win);
124 
125  typedef mln_ch_value(I, mln_result(A)) O;
126  O output;
127  initialize(output, input);
128  mln_pixter(O) o(output);
129 
130  mln_pixter(const I) p(input);
131  mln_qixter(const I, W) q(p, win);
132  for_all_2(p, o)
133  {
134  a.init();
135  for_all(q)
136  {
137  a.take(q.val());
138  if (a.can_stop())
139  break;
140  }
141  o.val() = a.to_result();
142  }
143 
144  trace::exiting("accu::impl::transform_stop_fastest");
145  return output;
146  }
147 
148 
149  } // end of namespace mln::accu::impl
150 
151 
152  // Dispatch.
153 
154  namespace internal
155  {
156 
157  template <typename I, typename A, typename W>
158  mln_ch_value(I, mln_result(A))
159  transform_stop_dispatch(metal::false_,
160  const Image<I>& input, const Accumulator<A>& a, const Window<W>& win)
161  {
162  return impl::generic::transform_stop(input, a, win);
163  }
164 
165  template <typename I, typename A, typename W>
166  mln_ch_value(I, mln_result(A))
167  transform_stop_dispatch(metal::true_,
168  const Image<I>& input, const Accumulator<A>& a, const Window<W>& win)
169  {
170  return impl::transform_stop_fastest(input, a, win);
171  }
172 
173  template <typename I, typename A, typename W>
174  mln_ch_value(I, mln_result(A))
175  transform_stop_dispatch(const Image<I>& input, const Accumulator<A>& a, const Window<W>& win)
176  {
177  return transform_stop_dispatch(mln_is_fastest_IW(I, W)(),
178  input, a, win);
179  }
180 
181  } // end of namespace mln::accu::internal
182 
183 
184  // Facades.
185 
186  template <typename I, typename A, typename W>
187  inline
188  mln_ch_value(I, mln_result(A))
189  transform_stop(const Image<I>& input, const Accumulator<A>& a, const Window<W>& win)
190  {
191  trace::entering("accu::transform_stop");
192 
193  mln_precondition(exact(input).is_valid());
194  mln_precondition(exact(win).is_valid());
195 
196  mln_ch_value(I, mln_result(A)) output;
197  output = internal::transform_stop_dispatch(input, a, win);
198 
199  trace::exiting("accu::transform_stop");
200  return output;
201  }
202 
203  template <typename I, typename A, typename W>
204  mln_ch_value(I, mln_meta_accu_result(A, mln_value(I)))
205  transform_stop(const Image<I>& input, const Meta_Accumulator<A>& a, const Window<W>& win)
206  {
207  trace::entering("accu::transform_stop");
208 
209  mln_precondition(exact(input).is_valid());
210  mln_precondition(exact(win).is_valid());
211 
212  typedef mln_accu_with(A, mln_value(I)) A_;
213  A_ a_ = accu::unmeta(exact(a), mln_value(I)());
214 
215  mln_ch_value(I, mln_result(A_)) output;
216  output = internal::transform_stop_dispatch(input, a_, win);
217 
218  trace::exiting("accu::transform_stop");
219  return output;
220  }
221 
222 # endif // ! MLN_INCLUDE_ONLY
223 
224  } // end of namespace mln::accu
225 
226 } // end of namespace mln
227 
228 
229 #endif // ! MLN_ACCU_TRANSFORM_STOP_HH