Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
is_simple_2d.hh
1 // Copyright (C) 2008, 2009, 2011 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_TOPO_IS_SIMPLE_2D_HH
28 # define MLN_TOPO_IS_SIMPLE_2D_HH
29 
34 
38 
39 
40 #include <mln/core/concept/image.hh>
41 #include <mln/core/concept/neighborhood.hh>
42 
43 #include <mln/core/alias/point2d.hh>
44 #include <mln/core/alias/neighb2d.hh>
45 
46 
47 namespace mln
48 {
49 
50  namespace topo
51  {
52 
65  template <typename N>
67  {
68 
69  is_simple_2d_t(const Neighborhood<N>& nbh);
70 
71  template <typename I>
72  bool check(const I& ima, const mln_psite(I)& p) const;
73 
74  template <typename I>
75  bool check__(const I& ima, unsigned p) const;
76 
77  template <typename I, typename N2>
78  unsigned nb_connectivity2d(const I&, const N2& nbh,
79  const mln_psite(I)& p, bool object) const;
80  template <typename I, typename N2>
81  unsigned nb_connectivity2d__(const I&, const N2& nbh,
82  unsigned p, bool object) const;
83 
84  protected:
85  const N& nbh_;
86  };
87 
88 
89 
90 # ifndef MLN_INCLUDE_ONLY
91 
92  namespace internal
93  {
94 
95  static const unsigned char connectivity_number_c8[256] =
96  {
97  0, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1,
98  1, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1,
99  1, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1,
100  2, 3, 2, 2, 2, 3, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1,
101 
102  1, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1,
103  1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
104  1, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1,
105  1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
106 
107  1, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2,
108  1, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1,
109  2, 3, 3, 3, 3, 4, 3, 3, 2, 2, 2, 2, 3, 3, 2, 2,
110  2, 3, 2, 2, 2, 3, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1,
111 
112  1, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1,
113  1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
114  1, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1,
115  1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
116  };
117 
118 
119  static const unsigned char connectivity_number_c4[256] =
120  {
121  0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1,
122  1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 2, 1,
123  0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1,
124  1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 2, 1,
125 
126  1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 3, 2, 2, 2, 3, 2,
127  2, 2, 3, 3, 2, 2, 2, 2, 3, 3, 4, 3, 3, 3, 3, 2,
128  1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 2, 1,
129  2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 1,
130 
131  0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1,
132  1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 2, 1,
133  0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1,
134  1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 2, 1,
135 
136  1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 3, 2, 2, 2, 3, 2,
137  1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 2, 1,
138  1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 2, 1,
139  1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1
140  };
141 
142  } // end of namespace mln::topo::internal
143 
144  template <typename N>
145  is_simple_2d_t<N>::is_simple_2d_t(const Neighborhood<N>& nbh)
146  : nbh_(exact(nbh))
147  {
148  mln_precondition(nbh_.is_valid());
149  }
150 
151 
152 
153  template <typename N>
154  template <typename I, typename N2>
155  unsigned
156  is_simple_2d_t<N>::nb_connectivity2d(const I& ima, const N2& nbh,
157  const mln_psite(I)& p, bool b) const
158  {
159  mln_precondition(ima.is_valid());
160  mln_precondition(nbh.is_valid());
161 
162  unsigned res = 0;
163 
164  // Note: fwd here but bkd in is_simple_point.hh...
165  mln_fwd_niter(N2) n(c8(), p);
166  for_all(n)
167  {
168  res = (res << 1);
169  if (ima.has(n) && ima(n) == b)
170  res = res | 1;
171  }
172 
173  switch (nbh.size())
174  {
175  case 4: // C4
176  return internal::connectivity_number_c4[res];
177  case 8: // C8
178  return internal::connectivity_number_c8[res];
179  default:
180  mln_assertion(0);
181 
182  }
183 
184  return 0;
185  }
186 
187 
188  template <typename N>
189  template <typename I, typename N2>
190  unsigned
191  is_simple_2d_t<N>::nb_connectivity2d__(const I& ima, const N2& nbh,
192  unsigned p, bool b) const
193  {
194  mln_precondition(ima.is_valid());
195  mln_precondition(nbh.is_valid());
196 
197  unsigned res = 0;
198 
199  static util::array<int>
200  noffset = mln::offsets_wrt(ima, c8());
201 
202  for (unsigned i = 0; i < noffset.nelements(); ++i)
203  {
204  res = (res << 1);
205  if (ima.element(p + noffset[i]) == b)
206  res = res | 1;
207  }
208 
209  switch (nbh.size())
210  {
211  case 4: // C4
212  return internal::connectivity_number_c4[res];
213  case 8: // C8
214  return internal::connectivity_number_c8[res];
215  default:
216  mln_assertion(0);
217 
218  }
219 
220  return 0;
221  }
222 
223 
224  template <typename N>
225  template <typename I>
226  inline
227  bool
228  is_simple_2d_t<N>::check(const I& ima, const mln_psite(I)& p) const
229  {
230  mln_precondition(ima.is_valid());
231  return (nb_connectivity2d(ima, nbh_.foreground(), p, true) == 1) // Consider neighbor.
232  && (nb_connectivity2d(ima, nbh_.background(), p, false) == 1); // Consider complement neighbor.
233  }
234 
235 
236 
237 
238  template <typename N>
239  template <typename I>
240  inline
241  bool
242  is_simple_2d_t<N>::check__(const I& ima, unsigned p) const
243  {
244  mln_precondition(ima.is_valid());
245  return (nb_connectivity2d__(ima, nbh_.foreground(), p, true) == 1) // Consider neighbor
246  && (nb_connectivity2d__(ima, nbh_.background(), p, false) == 1); // Consider complement neighbor.
247  }
248 
249 
250 
251 # endif // MLN_INCLUDE_ONLY
252 
253  } // end of namespace mln::topo
254 
255 } // end of namespace mln
256 
257 #endif // ! MLN_TOPO_IS_SIMPLE_2D_HH