Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
breadth_first_thinning.hh
1 // Copyright (C) 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_TOPO_SKELETON_BREADTH_FIRST_THINNING_HH
27 # define MLN_TOPO_SKELETON_BREADTH_FIRST_THINNING_HH
28 
32 
33 # include <algorithm>
34 
35 # include <mln/core/routine/duplicate.hh>
36 
37 # include <mln/core/concept/image.hh>
38 # include <mln/core/concept/neighborhood.hh>
39 
40 # include <mln/core/site_set/p_set.hh>
41 
42 # include <mln/fun/p2b/tautology.hh>
43 
44 namespace mln
45 {
46 
47  namespace topo
48  {
49 
50  namespace skeleton
51  {
52 
67  template <typename I, typename N, typename F, typename G, typename H>
68  mln_concrete(I)
69  breadth_first_thinning(const Image<I>& input,
70  const Neighborhood<N>& nbh,
71  Function_v2b<F>& is_simple,
72  G detach,
73  const Function_v2b<H>& constraint =
74  fun::p2b::tautology());
75 
76 
77 # ifndef MLN_INCLUDE_ONLY
78 
79  template <typename I, typename N, typename F, typename G, typename H>
80  inline
81  mln_concrete(I)
82  breadth_first_thinning(const Image<I>& input_,
83  const Neighborhood<N>& nbh_,
84  Function_v2b<F>& is_simple_,
85  G detach,
86  const Function_v2b<H>& constraint_)
87  {
88  const I& input = exact(input_);
89  const N& nbh = exact(nbh_);
90  F& is_simple = exact(is_simple_);
91  const H& constraint = exact(constraint_);
92 
93  I output = duplicate(input);
94  // Attach the work image to IS_SIMPLE.
95  is_simple.set_image(output);
96 
97  typedef mln_psite(I) psite;
98  typedef p_set<psite> set_t;
99  set_t set;
100  // Populate SET with candidate simple points.
101  mln_piter(I) p_(output.domain());
102  for_all(p_)
103  {
104  /* CONSTRAINTS and IS_SIMPLE are site-to-boolean (p2b)
105  predicate functors; passing an iterator as argument might
106  not be possible (C++ cannot resolve template routines if
107  an implicit conversion of the argument is needed). Help
108  the compiler and pass an actual, explicit psite. */
109  psite p = p_;
110  if (output(p) && constraint(p) && is_simple(p))
111  set.insert(p);
112  }
113 
114  while (!set.is_empty())
115  {
116  set_t next_set;
117 
118  /* FIXME: Using the following code does not work (it does
119  compiles, but does not behave like the code using a
120  hand-made loop). There must be a bug somewhere in
121  p_set or p_indexed_psite. */
122 # if 0
123  mln_piter(set_t) ps(set);
124  for_all(ps);
125  {
126  // Same remark as above.
127  psite p = p_;
128 # endif
129  for (unsigned i = 0; i < set.nsites(); ++i)
130  {
131  psite p = set[i];
132 
133  /* FIXME: We compute the cell and attachment of P twice:
134  during the call to is_simple() and within detach().
135  How could we reuse this elegantly, without breaking
136  the genericity of the skeleton algorithm? */
137  if (constraint(p) && is_simple(p))
138  {
139  detach(p, output);
140  mln_niter(N) n(nbh, p);
141  for_all(n)
142  if (output.domain().has(n)
143  && output(n) && constraint(p) && is_simple(n))
144  next_set.insert(n);
145  }
146  }
147  set.clear();
148  std::swap(set, next_set);
149  }
150  return output;
151  }
152 
153 # endif // MLN_INCLUDE_ONLY
154 
155  } // end of namespace mln::topo::skeleton
156 
157  } // end of namespace mln::topo
158 
159 } // end of namespace mln
160 
161 #endif // ! MLN_TOPO_SKELETON_BREADTH_FIRST_THINNING_HH