$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends 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  mln_trace("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  return a.to_result();
158  }
159 
160 
161 
171  //
172  template <typename A, typename I, typename L>
173  util::array<mln_result(A)>
174  compute_with_weights(const Accumulator<A>& a_,
175  const Image<I>& w_,
176  const Image<L>& label_,
177  const mln_value(L)& nlabels)
178  {
179  mln_trace("set::impl::generic::compute_with_weights");
180 
181  mlc_equal(mln_site(I), mln_site(L))::check();
182  mlc_converts_to(mln_site(I), mln_argument(A))::check();
183  mlc_converts_to(mln_value(I), unsigned)::check();
184 
185  A a = exact(a_);
186  const I& w = exact(w_);
187  const L& label = exact(label_);
188 
189  internal::compute_with_weights_tests(a, w, label);
190 
191  util::array<A> accus(value::next(nlabels), a);
192 
193  mln_piter(I) p(w.domain());
194  for_all(p)
195  accus[label(p)].take_n_times(w(p), p);
196 
197  util::array<mln_result(A)> r;
198  convert::from_to(accus, r);
199 
200  return r;
201  }
202 
203  } // end of namespace mln::set::impl::generic
204 
205  } // end of namespace mln::set::impl
206 
207 
208 
209  // Facades.
210 
211 
212  template <typename A, typename I>
213  inline
214  mln_result(A)
215  compute_with_weights(const Accumulator<A>& a, const Image<I>& w)
216  {
217  mln_trace("set::compute_with_weights");
218 
219  mlc_converts_to(mln_site(I), mln_argument(A))::check();
220  mlc_converts_to(mln_value(I), unsigned)::check();
221  mln_precondition(exact(w).is_valid());
222 
223  mln_result(A) r = impl::generic::compute_with_weights(a, w);
224 
225  return r;
226  }
227 
228 
229  template <typename A, typename I, typename L>
230  util::array<mln_result(A)>
231  compute_with_weights(const Accumulator<A>& a,
232  const Image<I>& w,
233  const Image<L>& label,
234  const mln_value(L)& nlabels)
235  {
236  mln_trace("set::compute_with_weights");
237 
238  mlc_equal(mln_site(I), mln_site(L))::check();
239  mlc_converts_to(mln_site(I), mln_argument(A))::check();
240  mlc_converts_to(mln_value(I), unsigned)::check();
241 
242  internal::compute_with_weights_tests(a, w, label);
243 
244  util::array<mln_result(A)> r;
245  r = impl::generic::compute_with_weights(a, w, label, nlabels);
246 
247  return r;
248  }
249 
250 
251  template <typename A, typename I>
252  inline
253  mln_meta_accu_result(A, mln_site(I))
254  compute_with_weights(const Meta_Accumulator<A>& a, const Image<I>& w)
255  {
256  mln_trace("set::compute_with_weights");
257 
258  mlc_converts_to(mln_value(I), unsigned)::check();
259 
260  mln_precondition(exact(w).is_valid());
261 
262  typedef mln_site(I) P;
263  typedef mln_accu_with(A, P) A_;
264  A_ a_ = accu::unmeta(exact(a), P());
265 
266  mln_result(A_) r = impl::generic::compute_with_weights(a_, w);
267 
268  return r;
269  }
270 
271 # endif // ! MLN_INCLUDE_ONLY
272 
273  } // end of namespace mln::set
274 
275 } // end of namespace mln
276 
277 
278 #endif // ! MLN_SET_COMPUTE_WITH_WEIGHTS_HH