Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
compute_with_weights.hh
1 // Copyright (C) 2008, 2009, 2010 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_SET_COMPUTE_WITH_WEIGHTS_HH
28 # define MLN_SET_COMPUTE_WITH_WEIGHTS_HH
29 
35 
36 # include <mln/core/concept/meta_accumulator.hh>
37 # include <mln/core/concept/image.hh>
38 # include <mln/core/concept/site_set.hh>
39 # include <mln/util/array.hh>
40 # include <mln/convert/from_to.hh>
41 # include <mln/value/next.hh>
42 
43 
44 namespace mln
45 {
46 
47  namespace set
48  {
49 
55  //
56  template <typename A, typename I>
57  mln_result(A)
58  compute_with_weights(const Accumulator<A>& a, const Image<I>& w);
59 
60 
69  //
70  template <typename A, typename I, typename L>
71  util::array<mln_result(A)>
72  compute_with_weights(const Accumulator<A>& a,
73  const Image<I>& w,
74  const Image<L>& label,
75  const mln_value(L)& nlabels);
76 
77 
83  //
84  template <typename A, typename I>
85  mln_meta_accu_result(A, mln_site(I))
86  compute_with_weights(const Meta_Accumulator<A>& a, const Image<I>& w);
87 
88 
89 
90 # ifndef MLN_INCLUDE_ONLY
91 
92 
93  // Tests.
94 
95 
96  namespace internal
97  {
98 
99  template <typename A, typename I, typename L>
100  void
101  compute_with_weights_tests(const Accumulator<A>& a_,
102  const Image<I>& w_,
103  const Image<L>& label_)
104  {
105  const A& a = exact(a_);
106  const I& w = exact(w_);
107  const L& label = exact(label_);
108 
109  mln_precondition(w.is_valid());
110  mln_precondition(label.is_valid());
111  mln_precondition(w.domain() <= label.domain());
112 
113  (void) a;
114  (void) w;
115  (void) label;
116  }
117 
118  } // end of namespace mln::internal
119 
120 
121 
122  // Implementations.
123 
124 
125  namespace impl
126  {
127 
128  namespace generic
129  {
130 
138  //
139  template <typename A, typename I>
140  inline
141  mln_result(A)
142  compute_with_weights(const Accumulator<A>& a_, const Image<I>& w_)
143  {
144  trace::entering("set::impl::generic::compute_with_weights");
145 
146  mlc_converts_to(mln_site(I), mln_argument(A))::check();
147  mlc_converts_to(mln_value(I), unsigned)::check();
148 
149  A a = exact(a_);
150  const I& w = exact(w_);
151 
152  a.init();
153  mln_piter(I) p(w.domain());
154  for_all(p)
155  a.take_n_times(w(p), p);
156 
157  trace::exiting("set::impl::generic::compute_with_weights");
158  return a.to_result();
159  }
160 
161 
162 
172  //
173  template <typename A, typename I, typename L>
174  util::array<mln_result(A)>
175  compute_with_weights(const Accumulator<A>& a_,
176  const Image<I>& w_,
177  const Image<L>& label_,
178  const mln_value(L)& nlabels)
179  {
180  trace::entering("set::impl::generic::compute_with_weights");
181 
182  mlc_equal(mln_site(I), mln_site(L))::check();
183  mlc_converts_to(mln_site(I), mln_argument(A))::check();
184  mlc_converts_to(mln_value(I), unsigned)::check();
185 
186  A a = exact(a_);
187  const I& w = exact(w_);
188  const L& label = exact(label_);
189 
190  internal::compute_with_weights_tests(a, w, label);
191 
192  util::array<A> accus(value::next(nlabels), a);
193 
194  mln_piter(I) p(w.domain());
195  for_all(p)
196  accus[label(p)].take_n_times(w(p), p);
197 
198  util::array<mln_result(A)> r;
199  convert::from_to(accus, r);
200 
201  trace::exiting("set::impl::generic::compute_with_weights");
202  return r;
203  }
204 
205  } // end of namespace mln::set::impl::generic
206 
207  } // end of namespace mln::set::impl
208 
209 
210 
211  // Facades.
212 
213 
214  template <typename A, typename I>
215  inline
216  mln_result(A)
217  compute_with_weights(const Accumulator<A>& a, const Image<I>& w)
218  {
219  trace::entering("set::compute_with_weights");
220 
221  mlc_converts_to(mln_site(I), mln_argument(A))::check();
222  mlc_converts_to(mln_value(I), unsigned)::check();
223  mln_precondition(exact(w).is_valid());
224 
225  mln_result(A) r = impl::generic::compute_with_weights(a, w);
226 
227  trace::exiting("set::compute_with_weights");
228  return r;
229  }
230 
231 
232  template <typename A, typename I, typename L>
235  const Image<I>& w,
236  const Image<L>& label,
237  const mln_value(L)& nlabels)
238  {
239  trace::entering("set::compute_with_weights");
240 
241  mlc_equal(mln_site(I), mln_site(L))::check();
242  mlc_converts_to(mln_site(I), mln_argument(A))::check();
243  mlc_converts_to(mln_value(I), unsigned)::check();
244 
245  internal::compute_with_weights_tests(a, w, label);
246 
248  r = impl::generic::compute_with_weights(a, w, label, nlabels);
249 
250  trace::exiting("set::compute_with_weights");
251  return r;
252  }
253 
254 
255  template <typename A, typename I>
256  inline
257  mln_meta_accu_result(A, mln_site(I))
258  compute_with_weights(const Meta_Accumulator<A>& a, const Image<I>& w)
259  {
260  trace::entering("set::compute_with_weights");
261 
262  mlc_converts_to(mln_value(I), unsigned)::check();
263 
264  mln_precondition(exact(w).is_valid());
265 
266  typedef mln_site(I) P;
267  typedef mln_accu_with(A, P) A_;
268  A_ a_ = accu::unmeta(exact(a), P());
269 
270  mln_result(A_) r = impl::generic::compute_with_weights(a_, w);
271 
272  trace::exiting("set::compute_with_weights");
273  return r;
274  }
275 
276 # endif // ! MLN_INCLUDE_ONLY
277 
278  } // end of namespace mln::set
279 
280 } // end of namespace mln
281 
282 
283 #endif // ! MLN_SET_COMPUTE_WITH_WEIGHTS_HH