Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
attribute_filter.hh
1 // Copyright (C) 2007, 2008, 2009 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_CANVAS_MORPHO_ATTRIBUTE_FILTER_HH
28 # define MLN_CANVAS_MORPHO_ATTRIBUTE_FILTER_HH
29 
33 
34 
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/neighborhood.hh>
37 # include <mln/core/concept/accumulator.hh>
38 
39 # include <mln/data/sort_offsets.hh>
40 # include <mln/trait/accumulators.hh>
41 # include <mln/util/pix.hh>
42 
43 # include <mln/border/get.hh>
44 # include <mln/data/fill.hh>
45 # include <mln/extension/adjust_fill.hh>
46 # include <mln/data/sort_psites.hh>
47 
48 namespace mln
49 {
50 
51  namespace canvas
52  {
53 
54  namespace morpho
55  {
56 
57  // FIXME: Doc!
58 
59  template <typename I, typename N, typename A>
60  mln_concrete(I)
61  attribute_filter(const Image<I>& input, const Neighborhood<N>& nbh,
62  const Accumulator<A>& a, const mln_result(A)& lambda,
63  bool increasing);
64 
65 
66 
67 # ifndef MLN_INCLUDE_ONLY
68 
69  namespace impl
70  {
71 
72  template <typename A, typename I>
73  void take_as_init_fastest(trait::accumulator::when_pix::use_none, A& accu,
74  const I& input, const unsigned p)
75  {
76  (void)input;
77  (void)p;
78  accu.take_as_init();
79  }
80 
81  template <typename A, typename I, typename P>
82  void take_as_init(trait::accumulator::when_pix::use_p, A& accu,
83  const I& input, const P& p)
84  {
85  (void)input;
86  accu.take_as_init(p);
87  }
88 
89  template <typename A, typename I, typename P>
90  void take_as_init(trait::accumulator::when_pix::use_none, A& accu,
91  const I& input, const P& p)
92  {
93  (void)input;
94  (void)p;
95  accu.take_as_init();
96  }
97 
98  template <typename A, typename I, typename P>
99  void take_as_init(trait::accumulator::when_pix::use_pix, A& accu,
100  const I& input, const P& p)
101  {
102  accu.take_as_init(make::pix(input, p));
103  }
104 
105  template <typename A, typename I, typename P>
106  void take_as_init(trait::accumulator::when_pix::use_v, A& accu,
107  const I& input, const P& p)
108  {
109  accu.take_as_init(input(p));
110  }
111 
112  template <typename A, typename I>
113  void take_as_init_fastest(trait::accumulator::when_pix::use_v, A& accu,
114  const I& input, const unsigned p)
115  {
116  accu.take_as_init(input.element(p));
117  }
118 
119 
120  template <typename A, typename I, typename P>
121  void take_as_init(A& accu, const I& input, const P& p)
122  {
123  take_as_init(mln_trait_accumulator_when_pix(A)(), accu, input, p);
124  }
125 
126  template <typename A, typename I, typename P>
127  void take_as_init_fastest(A& accu, const I& input, const P& p)
128  {
129  take_as_init_fastest(mln_trait_accumulator_when_pix(A)(), accu, input, p);
130  }
131 
132 
133 
134  namespace generic
135  {
136 
140 
141 
142  template <typename I>
143  static inline
144  mln_psite(I)
145  find_root(I& parent, const mln_psite(I) & x)
146  {
147  if (parent(x) == x)
148  return x;
149  else
150  return parent(x) = find_root(parent, parent(x));
151  }
152 
153  template <typename I, typename N, typename S, typename A>
154  mln_concrete(I)
155  attribute_filter(const Image<I>& input_,
156  const Neighborhood<N>& nbh_,
157  const Site_Set<S>& s_,
158  const Accumulator<A>& a_,
159  const mln_result(A)& lambda)
160  {
161  trace::entering("canvas::morpho::impl::generic::attribute_filter");
162  // FIXME: Test?!
163 
164  const I& input = exact(input_);
165  const N& nbh = exact(nbh_);
166  const S& s = exact(s_);
167  (void)a_; // To avoid warning at compilation
168 
169  mln_concrete(I) output;
170  initialize(output, input);
171 
172  // Local type.
173  typedef mln_psite(I) P;
174 
175 
176  // Auxiliary data.
177  mln_ch_value(I, bool) deja_vu;
178  mln_ch_value(I, bool) activity;
179  mln_ch_value(I, P) parent;
180  mln_ch_value(I, A) data;
181 
182  // Initialization.
183  {
184  initialize(deja_vu, input);
185  data::fill(deja_vu, false);
186  initialize(activity, input);
187  data::fill(activity, true);
188  initialize(parent, input);
189  initialize(data, input);
190  //a.init(); // init required.
191  }
192 
193  // First pass.
194  {
195  mln_fwd_piter(S) p(s); // s required.
196  mln_niter(N) n(nbh, p);
197 
198  for_all(p)
199  {
200  // Make set.
201  {
202  parent(p) = p;
203 
204  // Check accumulator trait to handle argument type (Pix or Site).
205  take_as_init(data(p), input, p);
206  }
207 
208  for_all(n)
209  if (input.domain().has(n) && deja_vu(n))
210  {
211  //do_union(n, p);
212  P r = find_root(parent, n);
213  if (r != p)
214  {
215  if (input(r) == input(p) || (activity(r) && (data(r) < lambda))) // Equiv(r, p)
216  // Either a flat zone or the component of r is still growing.
217  {
218  /* FIXME: Same remark as above concerning the
219  initialization of data(p); instead of
220 
221  data(p).take(data(r));
222 
223  we should (or could) have
224 
225  unite_data(p, r);
226 
227  so as to keep the generic aspect of this canvas
228  (as long as the set of acceptable types for the
229  template parameter A is not bound). */
230 
231  data(p).take(data(r));
232  parent(r) = p;
233  if (activity(r) == false)
234  activity(p) = false;
235  }
236  else
237  {
238  activity(p) = false;
239  }
240  }
241  }
242  deja_vu(p) = true;
243  }
244  }
245 
246  // Second pass.
247  {
248  mln_bkd_piter(S) p(s);
249  for_all(p)
250  if (parent(p) == p) // p is root.
251  output(p) = input(p);
252  else
253  output(p) = output(parent(p));
254  }
255 
256  trace::exiting("canvas::morpho::impl::generic::attribute_filter");
257  return output;
258  }
259 
260  } // end of namespace mln::canvas::morpho::impl::generic
261 
262 
266 
267  template <typename I>
268  inline
269  unsigned
270  find_root_fastest(I& parent, unsigned x)
271  {
272  if (parent.element(x) == x)
273  return x;
274  else
275  return parent.element(x) = find_root_fastest(parent, parent.element(x));
276  }
277 
278  template <typename I, typename N, typename A>
279  mln_concrete(I)
280  attribute_filter_fastest(const Image<I>& input_,
281  const Neighborhood<N>& nbh_,
282  const util::array<unsigned>& s,
283  const Accumulator<A>& a_,
284  const mln_result(A)& lambda)
285  {
286  trace::entering("canvas::morpho::impl::attribute_filter_fastest");
287  // FIXME: Tests?
288 
289  const I& input = exact(input_);
290  const N& nbh = exact(nbh_);
291  (void)a_;
292 
293  // The border adaptation is performed in the calling scope since
294  // we want offsets of 's' to match the image structure / dimensions.
295  mln_precondition(border::get(input) >= nbh.delta());
296 
297  mln_concrete(I) output;
298  initialize(output, input);
299 
300  // Local type.
301  typedef mln_psite(I) P;
302 
303  // Auxiliary data.
304  mln_ch_value(I, bool) deja_vu;
305  mln_ch_value(I, bool) activity;
306  mln_ch_value(I, unsigned) parent;
307  mln_ch_value(I, A) data;
308 
309  // Initialization.
310  {
311  initialize(deja_vu, input);
312  data::fill(deja_vu, false);
313  extension::fill(deja_vu, false); // So the border is neutral.
314 
315  initialize(activity, input);
316  data::fill(activity, true);
317  initialize(parent, input);
318  data::fill(parent, 0);
319  initialize(data, input);
320  }
321 
322  util::array<int> dp = offsets_wrt(input, nbh);
323  const unsigned n_nbhs = dp.nelements();
324  const unsigned n_points = s.nelements();
325 
326  // First pass.
327  {
328  for (unsigned i = 0; i < n_points; ++i)
329  {
330  unsigned p = s[i]; // An offset.
331 
332  // Make set.
333  parent.element(p) = p;
334 
335  // Check accumulator trait to handle argument type (Value or None).
336  take_as_init_fastest(data.element(p), input, p);
337 
338  for (unsigned j = 0; j < n_nbhs; ++j)
339  {
340  unsigned n = p + dp[j];
341  if (! deja_vu.element(n))
342  continue;
343 
344  unsigned r = find_root_fastest(parent, n);
345  if (r != p)
346  {
347  if (input.element(r) == input.element(p)
348  || (activity.element(r)
349  && (data.element(r) < lambda)))
350  {
351  data.element(p).take(data.element(r));
352  parent.element(r) = p;
353  if (activity.element(r) == false)
354  activity.element(p) = false;
355  }
356  else
357  activity.element(p) = false;
358  }
359  }
360 
361  deja_vu.element(p) = true;
362  }
363  }
364 
365 
366  // Second pass.
367  {
368  for (int i = n_points - 1; i >= 0 ; --i)
369  {
370  unsigned p = s[i];
371  if (parent.element(p) == p) // p is root.
372  output.element(p) = input.element(p);
373  else
374  output.element(p) = output.element(parent.element(p));
375  }
376  }
377 
378  trace::exiting("canvas::morpho::impl::attribute_filter_fastest");
379  return output;
380  }
381 
382  } // end of namespace mln::canvas::morpho::impl
383 
384 
385 
386 
387  // Dispatch.
388 
389 
390  namespace internal
391  {
392 
393  // Dispatch to generic.
394 
395  template <typename I, typename N, typename A>
396  inline
397  mln_concrete(I)
398  attribute_filter_dispatch(metal::false_,
399  const Image<I>& input,
400  const Neighborhood<N>& nbh,
401  const Accumulator<A>& a,
402  const mln_result(A)& lambda,
403  bool increasing)
404  {
405  p_array<mln_psite(I)> s = increasing ?
408 
409  return impl::generic::attribute_filter(input, nbh, s, a, lambda);
410  }
411 
412 
413  // Dispatch to fastest.
414 
415  template <typename I, typename N, typename A>
416  inline
417  mln_concrete(I)
418  attribute_filter_dispatch(metal::true_,
419  const Image<I>& input,
420  const Neighborhood<N>& nbh,
421  const Accumulator<A>& a,
422  const mln_result(A)& lambda,
423  bool increasing)
424  {
425  extension::adjust(input, nbh);
426 
427  util::array<unsigned> s =
428  increasing ?
430  data::sort_offsets_decreasing(input);
431 
432  return impl::attribute_filter_fastest(input, nbh, s, a, lambda);
433  }
434 
435 
436 
437  template <typename I, typename N, typename A>
438  inline
439  mln_concrete(I)
440  attribute_filter_dispatch(const Image<I>& input,
441  const Neighborhood<N>& nbh,
442  const Accumulator<A>& a,
443  const mln_result(A)& lambda,
444  bool increasing)
445  {
446  enum {
447  test = (mlc_equal(mln_trait_image_speed(I),
448  trait::image::speed::fastest)::value &&
449  mln_is_simple_neighborhood(N)::value &&
450  (mlc_equal(mln_trait_accumulator_when_pix(A),
451  trait::accumulator::when_pix::use_none)::value ||
452  mlc_equal(mln_trait_accumulator_when_pix(A),
453  trait::accumulator::when_pix::use_v)::value))
454  };
455  return attribute_filter_dispatch(metal::bool_<test>(), input, nbh, a, lambda, increasing);
456  }
457 
458  } // end of namespace mln::canvas::morpho::internal
459 
460 
461 
462  // Facade.
463 
464  template <typename I, typename N, typename A>
465  inline
466  mln_concrete(I)
467  attribute_filter(const Image<I>& input,
468  const Neighborhood<N>& nbh,
469  const Accumulator<A>& a,
470  const mln_result(A)& lambda,
471  bool increasing)
472  {
473  return internal::attribute_filter_dispatch(input, nbh, a, lambda, increasing);
474  }
475 
476 
477 # endif // ! MLN_INCLUDE_ONLY
478 
479  } // end of namespace mln::canvas::morpho
480 
481  } // end of namespace mln::canvas
482 
483 } // end of namespace mln
484 
485 
486 #endif // ! MLN_CANVAS_MORPHO_ATTRIBUTE_FILTER_HH