$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
rd.hh
1 // Copyright (C) 2010 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 SCRIBO_PRIMITIVE_INTERNAL_RD_HH
27 # define SCRIBO_PRIMITIVE_INTERNAL_RD_HH
28 
32 
33 #include <mln/core/image/image2d.hh>
34 #include <mln/core/alias/neighb2d.hh>
35 #include <mln/border/equalize.hh>
36 #include <mln/border/fill.hh>
37 
38 #include <mln/io/pbm/load.hh>
39 #include <mln/io/pbm/save.hh>
40 #include <mln/util/timer.hh>
41 
42 
43 namespace scribo
44 {
45 
46  namespace primitive
47  {
48 
49  namespace internal
50  {
51 
52  using namespace mln;
53 
55  template <typename I, typename J>
56  mln_concrete(I)
57  rd(const Image<I>& ima, const Image<J>& constraint);
58 
59 
60 # ifndef MLN_INCLUDE_ONLY
61 
62  template <typename Par>
63  inline
64  unsigned
65  Rd_find_root_(Par& parent, unsigned x)
66  {
67  if (parent.element(x) == x)
68  return x;
69  else
70  return parent.element(x) = Rd_find_root_(parent, parent.element(x));
71  }
72 
73  template <typename I, typename J, typename N>
74  inline
75  mln_concrete(I)
76  Rd(const Image<I>& f_, const Image<J>& g_, const Neighborhood<N>& nbh_)
77  {
78  mln_trace("Rd");
79 
80  const I& f = exact(f_);
81  const J& g = exact(g_);
82  const N& nbh = exact(nbh_);
83 
84  typedef mln_psite(I) P;
85  typedef mln_value(I) V;
86 
87  // Auxiliary data.
88  mln_ch_value(I, bool) deja_vu;
89  mln_ch_value(I, unsigned) parent;
90  mln_concrete(I) output;
91 
92  // Initialization.
93  {
94  border::equalize(f, g, nbh.delta());
95  mln::border::fill(g, false);
96 
97  initialize(output, f);
98  mln::data::fill(output, false);
99 
100  initialize(parent, f);
101  }
102 
103  mln::util::array<int> dp = negative_offsets_wrt(f, nbh);
104  const unsigned n_nbhs = dp.nelements();
105 
106  // First pass.
107  {
108  mln_fwd_pixter(const I) pxl_g(g);
109  for_all(pxl_g)
110  {
111  if (pxl_g.val() == false)
112  continue;
113 
114  unsigned p = pxl_g.offset();
115 
116  // Make-Set.
117  parent.element(p) = p;
118  // Init.
119  output.element(p) = f.element(p);
120 
121  for (unsigned j = 0; j < n_nbhs; ++j)
122  {
123  unsigned n = p + dp[j];
124  if (g.element(n) == true)
125  {
126  mln_invariant(f.domain().has(f.point_at_offset(n)));
127  // Do-Union.
128  unsigned r = Rd_find_root_(parent, n);
129  if (r != p)
130  {
131  // Set-Parent:
132  parent.element(r) = p;
133  // Merge:
134  output.element(p) = output.element(p) || output.element(r);
135  }
136  }
137  }
138  }
139  }
140 
141  // Second pass.
142  {
143  mln_bkd_pixter(const I) pxl_g(g);
144  for_all(pxl_g)
145  {
146  if (pxl_g.val() == false)
147  continue;
148  unsigned p = pxl_g.offset();
149  output.element(p) = output.element(parent.element(p));
150  }
151  }
152 
153  return output;
154  }
155 
156 
157 
158 
159  template <typename I, typename J>
160  mln_concrete(I)
161  rd(const Image<I>& ima, const Image<J>& constraint)
162  {
163  mln_trace("scribo::primitive::internal::rd");
164 
165  mln_precondition(exact(ima).is_valid());
166  mln_precondition(exact(constraint).is_valid());
167 
168  using namespace mln;
169 
170  // FIXME: not generic.
171  neighb2d nbh = c4();
172 
173  mln_concrete(I) output = Rd(ima, constraint, nbh);
174 
175  return output;
176  }
177 
178 
179 # endif // ! MLN_INCLUDE_ONLY
180 
181  } // end of namespace scribo::primitive::internal
182 
183  } // end of namespace scribo::primitive
184 
185 } // end of namespace scribo
186 
187 
188 
189 
190 #endif // ! SCRIBO_PRIMITIVE_INTERNAL_RD_HH