$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
link.hh
1 // Copyright (C) 2010, 2013 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_AFP_LINK_HH
27 # define SCRIBO_AFP_LINK_HH
28 
32 
33 # include <mln/geom/ncols.hh>
34 # include <mln/geom/nrows.hh>
35 # include <mln/util/couple.hh>
36 # include <mln/math/max.hh>
37 
38 # include <scribo/core/object_links.hh>
39 # include <scribo/core/component_set.hh>
40 # include <scribo/core/macros.hh>
41 
42 // FIXME: Move the contents of this file in the right namespace and/or
43 // refactor.
44 
45 namespace scribo
46 {
47 
48  namespace primitive
49  {
50 
51  namespace link
52  {
53 
54  using namespace mln;
55 
56 
57  template <typename L>
59  left_right(const component_set<L>& components)
60  {
62  right(components, static_cast<unsigned>(components.nelements()) + 1);
63  right.init();
64 
66  left(components, static_cast<unsigned>(components.nelements()) + 1);
67  left.init();
68 
69  const L& lbl_ima = components.labeled_image();
70 
71  for_all_comps(i, components)
72  {
73  float
74  w = components(i).bbox().width(),
75  h = components(i).bbox().height();
76  unsigned dmax = (w / 2.0f) + (3 * math::max(w, h));
77 
78 
79  const mln_site(L) c = components(i).mass_center();
80 
81  int
82  midcol = (components(i).bbox().pmax().col()
83  - components(i).bbox().pmin().col()) / 2;
84  int
85  nrightima = geom::ncols(lbl_ima) - c.col(),
86  nleftima = c.col(),
87  nright = std::min(static_cast<unsigned>(nrightima), midcol + dmax),
88  nleft = std::min(static_cast<unsigned>(nleftima), midcol + dmax);
89 
90  // Right
91  {
92  const mln_value(L)
93  *p = &lbl_ima(c),
94  *pstop = p + nright + 1;
95 
96  for (; p != pstop; ++p)
97  {
98  if (*p != literal::zero // Not the background
99  && *p != i // Not the current component
100  && right(*p) != i) // No loops
101  {
102  right.update(i, *p);
103  break;
104  }
105  }
106  }
107 
108 
109  // Left
110  {
111  const mln_value(L)
112  *p = &lbl_ima(c),
113  *pstop = p - nleft - 1;
114 
115  for (; p != pstop; --p)
116  {
117  if (*p != literal::zero // Not the background
118  && *p != i // Not the current component
119  && left(*p) != i) // No loops
120  {
121  left.update(i, *p);
122  break;
123  }
124  }
125  }
126  }
127 
128  return mln::make::couple(left, right);
129  }
130 
131 
132  template <typename L>
134  left(const component_set<L>& components, unsigned dmax)
135  {
137  left(components, static_cast<unsigned>(components.nelements()) + 1);
138  left.init();
139 
140  const L& lbl_ima = components.labeled_image();
141 
142  for_all_comps(i, components)
143  {
144  const mln_site(L) c = components(i).mass_center();
145 
146  int
147  midcol = (components(i).bbox().pmax().col()
148  - components(i).bbox().pmin().col()) / 2;
149  int
150  nleftima = c.col(),
151  nleft = std::min(static_cast<unsigned>(nleftima), midcol + dmax);
152 
153  // Left
154  {
155  const mln_value(L)
156  *p = &lbl_ima(c),
157  *pstop = p - nleft - 1;
158 
159  for (; p != pstop; --p)
160  {
161  if (*p != literal::zero // Not the background
162  && *p != i // Not the current component
163  && left(*p) != i) // No loops
164  {
165  left(i) = *p;
166  break;
167  }
168  }
169  }
170  }
171 
172  return left;
173  }
174 
175  } // end of namespace scribo::primitive::link
176 
177  } // end of namespace scribo::primitive
178 
179 } // end of namespace scribo
180 
181 #endif // ! SCRIBO_AFP_LINK_HH